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 / bellhop / lobby.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-15  |  3.5 KB  |  115 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 "bellhop.h"
  15.  
  16. HRESULT ConnectUsingLobby(LPDPLAYINFO lpDPInfo)
  17. {
  18.     LPDIRECTPLAY2A        lpDirectPlay2A = NULL;
  19.     LPDIRECTPLAY3A        lpDirectPlay3A = NULL;
  20.     LPDIRECTPLAYLOBBY2A    lpDirectPlayLobby2A = NULL;
  21.     LPDPLCONNECTION        lpConnectionSettings = NULL;
  22.     DPID                dpidPlayer;
  23.     DWORD                dwSize;
  24.     HRESULT                hr;
  25.  
  26.     // get an ANSI DirectPlay lobby interface
  27.     hr = CreateDirectPlayLobbyInterface(&lpDirectPlayLobby2A );
  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 = lpDirectPlayLobby2A->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 = lpDirectPlayLobby2A->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 = lpDirectPlayLobby2A->SetConnectionSettings(0, 0, lpConnectionSettings);
  63.     if FAILED(hr)
  64.         goto FAILURE;
  65.  
  66.     // connect to the session - returns an ANSI IDirectPlay2A interface
  67.     hr = lpDirectPlayLobby2A->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 = IDirectPlay2_QueryInterface(lpDirectPlay2A, 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 = IDirectPlay3_CreatePlayer(lpDirectPlay3A, &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->lpDirectPlayLobby2A = lpDirectPlayLobby2A;
  87.  
  88.     lpDPInfo->dpidPlayer = dpidPlayer;
  89.     if (lpConnectionSettings->dwFlags & DPLCONNECTION_CREATESESSION)
  90.         lpDPInfo->bIsHost = TRUE;
  91.     else
  92.         lpDPInfo->bIsHost = FALSE;
  93.  
  94.     // set our interfaces to NULL here 
  95.     // so they won't be released below
  96.  
  97.     lpDirectPlay3A = NULL;
  98.     lpDirectPlayLobby2A = NULL;
  99.  
  100. FAILURE:
  101.     if (lpDirectPlay2A)
  102.         IDirectPlay2_Release(lpDirectPlay2A);
  103.  
  104.     if (lpDirectPlay3A)
  105.         IDirectPlay3_Release(lpDirectPlay3A);
  106.  
  107.     if (lpDirectPlayLobby2A)
  108.         IDirectPlayLobby_Release(lpDirectPlayLobby2A);
  109.  
  110.     if (lpConnectionSettings)
  111.         GlobalFreePtr(lpConnectionSettings);
  112.  
  113.     return (hr);
  114. }
  115.