_Games Programming with DirectPlay_ by Chris Howard Figure 1. (a) // NetRoids a64e8e40-5750-11cf-a4ac-0000c0ec0b9f DEFINE_GUID(NETROIDS_GUID,0xa64e8e40,0x5750,0x11cf, 0xa4,0xac,0x00,0x00, 0xc0,0xec,0x0b,0x9f); (b) DirectPlayEnumerate(EnumSP, (LPVOID) hWndCtl); Figure 2. // Create a new game, so we're the host IsHost = TRUE; // Initialize session description structure memset(&dpDesc, 0x00, sizeof(DPSESSIONDESC)); dpDesc.dwSize = sizeof(dpDesc); dpDesc.dwMaxPlayers = MAXPLAYERS; dpDesc.dwFlags = DPOPEN_CREATESESSION; dpDesc.guidSession = pGuid; strcpy(dpDesc.szSessionName, FullName); // Try to open the session if ((hr = lpIDC->lpVtbl->Open(lpIDC, &dpDesc)) != DP_OK) { // We failed lpIDC->lpVtbl->Release(lpIDC); lpIDC = NULL; return(FALSE); } Figure 3: (a) // Initialize the session description structure memset(&dpDesc, 0x00, sizeof(DPSESSIONDESC)); dpDesc.dwSize = sizeof(dpDesc); dpDesc.guidSession = *g_lpGuid; // Enum sessions with 5 second timeout lpIDC->lpVtbl->EnumSessions(lpIDC, &dpDesc, (DWORD)5000, EnumSession, (LPVOID) hWndCtl, (DWORD)NULL); (b) // Initialize session description struc to open it memset(&dpDesc, 0x00, sizeof(DPSESSIONDESC)); dpDesc.dwSize = sizeof(dpDesc); dpDesc.guidSession = *g_lpGuid; dpDesc.dwFlags = DPOPEN_OPENSESSION; dpDesc.dwSession = SendMessage((HWND) hWndCtl, LB_GETITEMDATA, iIndex, 0); hr = lpIDC->lpVtbl->Open(lpIDC, &dpDesc); (c) // Either way, we have to create a player if ((hr = lpIDC->lpVtbl->CreatePlayer(lpIDC, &dcoID, NickName, "NetRoids Player", &dphEvent)) != DP_OK) { // We failed lpIDC->lpVtbl->Close(lpIDC); lpIDC->lpVtbl->Release(lpIDC); lpIDC = NULL; return(FALSE); } Figure 4. (a) case MSG_HEREIAM: // Tell host we are here lpHereIAm = (LPHEREIAMMSG)CommBuff; lpHereIAm->MsgCode = msg; lpHereIAm->ID = (DWORD)dcoID; nBytes = sizeof(HEREIAMMSG); break; (b) // Send the message lpIDC->lpVtbl->Send(lpIDC, dcoID, send_to, 0, (LPSTR) CommBuff, nBytes); Figure 5. (a) status = lpIDC->lpVtbl->Receive(lpIDC, &fromID, &dcoReceiveID, DPRECEIVE_ALL, CommBuff, &nBytes); (b) case MSG_HEREIAM: // Someone wants to play if (IsHost) { // I'm the host, so find out who is here lpHereIAm = (LPHEREIAMMSG)CommBuff; shipID = lpHereIAm->ID; // Initialize them SendGameMessage(MSG_INIT, shipID, 0); } break;