home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / graphics / directx / dpchat / lobby.cpp < prev    next >
C/C++ Source or Header  |  1997-07-14  |  3KB  |  109 lines

  1. /*==========================================================================
  2.  *
  3.  *  Copyright (C) 1996-1997 Microsoft Corporation.  All Rights Reserved.
  4.  *
  5.  *  File:       lobby.cpp
  6.  *  Content:    Uses information from the lobby to establish a connection.
  7.  *
  8.  ***************************************************************************/
  9.  
  10. #include <windows.h>
  11. #include <windowsx.h>
  12. #include <dplobby.h>
  13.  
  14. #include "dpchat.h"
  15.  
  16. HRESULT ConnectUsingLobby(LPDPLAYINFO lpDPInfo)
  17. {
  18.     LPDIRECTPLAY2A        lpDirectPlay2A = NULL;
  19.     LPDIRECTPLAY3A        lpDirectPlay3A = NULL;
  20.     LPDIRECTPLAYLOBBYA    lpDirectPlayLobbyA = NULL;
  21.     LPDPLCONNECTION        lpConnectionSettings = NULL;
  22.     DPID                dpidPlayer;
  23.     DWORD                dwSize;
  24.     HRESULT                hr;
  25.  
  26.     // get an ANSI DirectPlay lobby interface
  27.     hr = DirectPlayLobbyCreate(NULL, &lpDirectPlayLobbyA, NULL, NULL, 0);
  28.     if FAILED(hr)
  29.         goto FAILURE;
  30.  
  31.     // get connection settings from the lobby
  32.     // if this routine returns DPERR_NOTLOBBIED, then a lobby did not
  33.     // launch this application and the user needs to configure the connection.
  34.  
  35.     // pass in a NULL pointer to just get the size of the connection setttings
  36.     hr = lpDirectPlayLobbyA->GetConnectionSettings(0, NULL, &dwSize);
  37.     if (DPERR_BUFFERTOOSMALL != hr)
  38.         goto FAILURE;
  39.  
  40.     // allocate memory for the connection setttings
  41.     lpConnectionSettings = (LPDPLCONNECTION) GlobalAllocPtr(GHND, dwSize);
  42.     if (NULL == lpConnectionSettings)
  43.     {
  44.         hr = DPERR_OUTOFMEMORY;
  45.         goto FAILURE;
  46.     }
  47.  
  48.     // get the connection settings
  49.     hr = lpDirectPlayLobbyA->GetConnectionSettings(0, lpConnectionSettings, &dwSize);
  50.     if FAILED(hr)
  51.         goto FAILURE;
  52.  
  53.     // before connecting, the game should configure the session description
  54.     // with any settings it needs
  55.  
  56.     // set flags and max players used by the game
  57.     lpConnectionSettings->lpSessionDesc->dwFlags = DPSESSION_MIGRATEHOST | 
  58.                                                    DPSESSION_KEEPALIVE;
  59.     lpConnectionSettings->lpSessionDesc->dwMaxPlayers = MAXPLAYERS;
  60.  
  61.     // store the updated connection settings
  62.     hr = lpDirectPlayLobbyA->SetConnectionSettings(0, 0, lpConnectionSettings);
  63.     if FAILED(hr)
  64.         goto FAILURE;
  65.  
  66.     // connect to the session - returns an ANSI IDirectPlay2A interface
  67.     hr = lpDirectPlayLobbyA->Connect(0, &lpDirectPlay2A, NULL);
  68.     if FAILED(hr)
  69.         goto FAILURE;
  70.  
  71.     // Obtain an IDirectPlay3A interface, the IDirectPlay2A interface will
  72.     // be released at the end of the function
  73.     hr = lpDirectPlay2A->QueryInterface(IID_IDirectPlay3A, (LPVOID *) &lpDirectPlay3A);
  74.     if FAILED(hr)
  75.         goto FAILURE;
  76.  
  77.     // create a player with the name returned in the connection settings
  78.     hr = lpDirectPlay3A->CreatePlayer(&dpidPlayer,
  79.                             lpConnectionSettings->lpPlayerName, 
  80.                             lpDPInfo->hPlayerEvent, NULL, 0, 0);
  81.     if FAILED(hr)
  82.         goto FAILURE;
  83.  
  84.     // return connection info
  85.     lpDPInfo->lpDirectPlay3A = lpDirectPlay3A;
  86.     lpDPInfo->dpidPlayer = dpidPlayer;
  87.     if (lpConnectionSettings->dwFlags & DPLCONNECTION_CREATESESSION)
  88.         lpDPInfo->bIsHost = TRUE;
  89.     else
  90.         lpDPInfo->bIsHost = FALSE;
  91.  
  92.     lpDirectPlay3A = NULL;    // set to NULL here so it won't release below
  93.  
  94. FAILURE:
  95.     if (lpDirectPlay2A)
  96.         lpDirectPlay2A->Release();
  97.  
  98.     if (lpDirectPlay3A)
  99.         lpDirectPlay3A->Release();
  100.  
  101.     if (lpDirectPlayLobbyA)
  102.         lpDirectPlayLobbyA->Release();
  103.  
  104.     if (lpConnectionSettings)
  105.         GlobalFreePtr(lpConnectionSettings);
  106.  
  107.     return (hr);
  108. }
  109.