📄 dptools.cpp
字号:
}
return TRUE;
}
return TRUE;
case IDPLAYERS:
DialogBox( (HINSTANCE)g_hInstance, MAKEINTRESOURCE( IDD_PLAYERS ), hWnd,
( DLGPROC )DlgProcEnumPlayers );
return TRUE;
case IDJOIN:
OutputDebugString("*********Joining!\n");
i = SendDlgItemMessage( hWnd, IDC_SESSIONS,
LB_GETCURSEL, 0, 0L );
lpGuid = ( LPGUID )SendDlgItemMessage( hWnd,
IDC_SESSIONS, LB_GETITEMDATA, i, 0 );
ZeroMemory( &dpDesc, sizeof( dpDesc ) );
dpDesc.dwSize = sizeof( dpDesc );
dpDesc.guidInstance = *lpGuid;
dpDesc.guidApplication = ZCAR_GUID;
if FAILED( lpDP->Open( &dpDesc, DPOPEN_JOIN ) )
{
MessageBox( hWnd, "Could not join session.",
"Error", MB_OK );
EndDialog( hWnd, -1 );
}
else
{
EndDialog( hWnd, 0 );
}
return TRUE;
case IDCREATE:
// Create the session with the supplied parameters
GetDlgItemText( hWnd, IDC_SESSIONNAME,
szSessionName, 30 );
ZeroMemory( &dpDesc, sizeof( dpDesc ) );
dpDesc.dwSize = sizeof( dpDesc );
dpDesc.guidApplication = ZCAR_GUID;
dpDesc.dwFlags = DPSESSION_MIGRATEHOST |
DPSESSION_KEEPALIVE;
dpDesc.dwMaxPlayers = 4;
dpDesc.lpszSessionNameA = szSessionName;
if FAILED( lpDP->Open( &dpDesc, DPOPEN_CREATE ) )
{
MessageBox( hWnd, "Could not create session.",
"Error", MB_OK );
EndDialog( hWnd, -1 );
}
else
{
EndDialog( hWnd, 0 );
}
return TRUE;
case IDCANCEL:
EndDialog( hWnd, -2 );
return TRUE;
}
break;
case WM_TIMER:
if ( bEnumerating || bUserCancel ) return TRUE;
// Display the current session list if
// there is a valid DPlay object
SendDlgItemMessage(hWnd, IDC_SESSIONS,
LB_RESETCONTENT, 0, 0L);
ZeroMemory( &dpDesc, sizeof( dpDesc ) );
dpDesc.dwSize = sizeof( dpDesc );
dpDesc.guidApplication = ZCAR_GUID;
dwSessions = DPENUMSESSIONS_AVAILABLE |
DPENUMSESSIONS_ASYNC;
bEnumerating = TRUE;
// Start the enumeration
hr = lpDP->EnumSessions( &dpDesc, 0,
( LPDPENUMSESSIONSCALLBACK2 )EnumSession,
( LPVOID ) GetDlgItem( hWnd, IDC_SESSIONS ),
dwSessions );
if ( hr == DPERR_USERCANCEL ) {
bUserCancel = TRUE;
}
bEnumerating = FALSE;
// Adjust the user interface accordingly if there
// are sessions available
if ( SendDlgItemMessage( hWnd, IDC_SESSIONS,
LB_GETCOUNT, 0, 0 ) > 0 )
{
EnableWindow( GetDlgItem( hWnd, IDPLAYERS ),
TRUE );
EnableWindow( GetDlgItem( hWnd, IDJOIN ),
TRUE );
SendDlgItemMessage( hWnd, IDC_SESSIONS,
LB_SETCURSEL, 0, 0 );
}
else
{
EnableWindow( GetDlgItem( hWnd, IDPLAYERS ),
FALSE );
EnableWindow( GetDlgItem( hWnd, IDJOIN ),
FALSE );
}
return TRUE;
case WM_DESTROY:
// Save the player name for later
GetDlgItemText( hWnd, IDC_PLAYER,
g_szPlayerName, 30 );
KillTimer( hWnd, TIMER_ID );
break;
}
return FALSE;
}
void EvaluateGameMessage( LPGENERICMSG lpGeneric, DPID pidFrom )
{
switch( lpGeneric->byType )
{
case MSG_HOST:
// If we're getting this message, we must be a new
// player. Get the slot from the slot number, fill it.
OutputDebugString("Processing welcome message.\n");
LPHOSTMSG lpHost;
lpHost = (LPHOSTMSG) lpGeneric;
break;
case MSG_FIRE:
OutputDebugString("Getting fire!\n");
LPFIREMSG lpFire;
lpFire = (LPFIREMSG) lpGeneric;
receive_count = lpFire->sddata;
break;
case MSG_SYNC:
LPSYNCMSG lpSync;
lpSync = (LPSYNCMSG) lpGeneric;
break;
case MSG_CONTROL:
LPCONTROLMSG lpControl;
lpControl = (LPCONTROLMSG) lpGeneric;
break;
}
}
int ReceiveFireMessage()
{
return(receive_count);
}
void EvaluateSystemMessage( DPMSG_GENERIC *pGeneric, HWND hWnd )
{
// int ass;
switch( pGeneric->dwType )
{
// The message comes to us cast as DPMSG_GENERIC. We'll examine
// dwType to determine the type of message, then cast to the new
// type and evaluate the rest of the message.
case DPSYS_CREATEPLAYERORGROUP:
{
DPMSG_CREATEPLAYERORGROUP *pMsg;
pMsg = (DPMSG_CREATEPLAYERORGROUP *) pGeneric;
OutputDebugString("A player has joined.\n");
if ( g_bHost )
{
// MsgHost.bySlot = FindPlayerSlot();
SendGameMessage( (LPGENERICMSG)&MsgHost, pMsg->dpId );
OutputDebugString( "Sending welcome message.\n" );
}
break;
}
case DPSYS_DESTROYPLAYERORGROUP:
{
DPMSG_DESTROYPLAYERORGROUP *pMsg;
pMsg = (DPMSG_DESTROYPLAYERORGROUP *) pGeneric;
OutputDebugString("A player has left.\n");
// Remove the player from the game
}
case DPSYS_HOST:
{
g_bHost = TRUE;
OutputDebugString( "This machine is now the session host.\n" );
break;
}
}
}
void ReceiveMessages( HWND hWnd )
{
DPID fromID;
DPID toID;
DWORD nBytes;
DPMSG_GENERIC *pGeneric;
LPGENERICMSG pGameMsg;
HRESULT dprval;
// Don't let Receive work use the global value directly,
// as it changes it.
nBytes = dwReceiveBufferSize;
while( TRUE )
{
dprval = lpDP->Receive( &fromID, &toID,
DPRECEIVE_ALL, lpReceiveBuffer, &nBytes);
if ( dprval == DPERR_BUFFERTOOSMALL )
// The recieve buffer size must be adjusted.
{
if ( lpReceiveBuffer == NULL)
{
// We haven't allocated any buffer yet -- do it.
lpReceiveBuffer = malloc( nBytes );
if ( lpReceiveBuffer == NULL ) {
OutputDebugString( "Couldn't allocate memory.\n" );
return;
}
}
else
{
// The buffer's been allocated, but it's too small so
// it must be enlarged.
free( lpReceiveBuffer );
lpReceiveBuffer = malloc( nBytes );
if ( lpReceiveBuffer == NULL ) {
OutputDebugString( "Couldn't allocate memory.\n" );
return;
}
}
// Update our global to the new buffer size.
dwReceiveBufferSize = nBytes;
}
else if ( dprval == DP_OK )
// A message was successfully retrieved.
{
if ( fromID == DPID_SYSMSG )
{
pGeneric = (DPMSG_GENERIC *) lpReceiveBuffer;
OutputDebugString( "Processing system message.\n" );
EvaluateSystemMessage ( pGeneric, hWnd );
}
else
{
pGameMsg = (LPGENERICMSG) lpReceiveBuffer;
OutputDebugString("Processing game message.\n");
EvaluateGameMessage( pGameMsg, fromID );
}
}
else
{
return;
}
}
}
DWORD WINAPI ReceiveThread( LPVOID lpParameter )
{
HWND hWnd = (HWND) lpParameter;
HANDLE eventHandles[2];
eventHandles[0] = hPlayerEvent;
eventHandles[1] = hKillEvent;
// Wait for either the player or kill event to fire. If it
// is the player event (WAIT_OBJECT_0), process the messages
// and wait again. If it's the kill event, shut down the
// thread and exit
while (WaitForMultipleObjects( 2, eventHandles, FALSE,
INFINITE) == WAIT_OBJECT_0 )
{
OutputDebugString( "Thread awakened.\n" );
ReceiveMessages( hWnd );
}
ExitThread( 0 );
return ( 0 );
}
int StartDPSession( void )
{
int rc;
// Call the dialog for establishing a connection
rc = DialogBox((HINSTANCE) g_hInstance, MAKEINTRESOURCE(IDD_CONNECT),
g_hwnd, (DLGPROC)DlgProcDPStart );
if ( !rc )
{
InitMessageBuffers();
// Create an event that will be used to signal when
// the player has messages
hPlayerEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
hKillEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
hReceiveThread = CreateThread(NULL,
0,
ReceiveThread,
g_hwnd,
0,
&idReceiveThread);
// Get the caps for the session
ZeroMemory( &dpCaps, sizeof( dpCaps ) );
dpCaps.dwSize = sizeof( dpCaps );
lpDP->GetCaps( &dpCaps, 0 );
// Are we the session host?
if ( dpCaps.dwFlags & DPCAPS_ISHOST )
{
g_bHost = TRUE;
OutputDebugString( "We are the session host.\n" );
}
}
return rc;
}
BOOL ShutDownDPSession( void )
{
// Destroy the local player
if ( g_dpPlayerID )
{
lpDP->DestroyPlayer( g_dpPlayerID );
g_dpPlayerID = 0;
}
// Shut down the recieve thread
if ( hReceiveThread )
{
// Signal event to kill receive thread
SetEvent( hKillEvent );
// Wait for the thread to shut down
WaitForSingleObject( hReceiveThread, INFINITE );
CloseHandle( hReceiveThread);
hReceiveThread = NULL;
}
// Close the other events
if ( hKillEvent )
{
CloseHandle( hKillEvent );
hKillEvent = NULL;
}
if ( hPlayerEvent )
{
CloseHandle( hPlayerEvent );
hPlayerEvent = NULL;
}
// Free the receive buffer.
if ( lpReceiveBuffer )
{
free( lpReceiveBuffer );
lpReceiveBuffer = NULL;
}
// Release the DirectPlay object and the Lobby object
if ( lpDP )
{
OutputDebugString( "Releasing the DP object.\n" );
lpDP->Release();
lpDP = NULL;
}
if ( lpDPLobby2 )
{
OutputDebugString( "Releasing the lobby object.\n" );
lpDPLobby2->Release();
lpDPLobby2 = NULL;
}
return TRUE;
}
void SendSyncMessage()
{
//MsgSync.x = vehicle->x;
SendGameMessage( (LPGENERICMSG)&MsgSync, DPID_ALLPLAYERS );
}
void SendFireMessage(int send_me)
{
MsgFire.sddata = send_me;
SendGameMessage( (LPGENERICMSG)&MsgFire, DPID_ALLPLAYERS );
}
void SendControlMessage( BYTE byControl )
{
MsgControl.byState = byControl;
SendGameMessage( (LPGENERICMSG)&MsgControl, DPID_ALLPLAYERS );
}
void SendGameMessage( LPGENERICMSG lpMsg, DPID idTo )
{
int nBytes;
DWORD dwFlags = 0;
switch( lpMsg->byType )
{
case MSG_HOST:
nBytes = sizeof( HOSTMSG );
dwFlags = DPSEND_GUARANTEED;
break;
case MSG_SYNC:
nBytes = sizeof( SYNCMSG );
break;
case MSG_FIRE:
nBytes = sizeof( FIREMSG );
dwFlags = DPSEND_GUARANTEED;
break;
case MSG_CONTROL:
nBytes = sizeof( CONTROLMSG );
break;
default:
return;
}
// Send the message buffer
lpDP->Send( g_dpPlayerID, idTo, dwFlags, (LPVOID)lpMsg, nBytes);
}
///new aggregate stuff, added by SIM(4-08-99)
BOOL InitializeDirectPlay( HANDLE hInstance)
{
CoInitialize( NULL );
int rc;
g_hInstance = hInstance;
// Attempt to initialize DirectPlay
rc = StartDPSession();
switch ( rc )
{
case -1:
// Something failed when attempting DirectPlay init
MessageBox( NULL, "DirectPlay Init FAILED try including the rc file", "ERROR", MB_OK );
// Fall through
case -2:
// The user pressed the cancel button.
OutputDebugString( "The user cancelled DPlay.\n" );
return FALSE;
}
CreateGamePlayer();
return TRUE;
}
void UninitializeDirectPlay()
{
CoUninitialize();
ShutDownDPSession();
//MessageBox(NULL, "uninit", "uninit", MB_OK);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -