PDA

Click to See Complete Forum and Search --> : Help with This Code (DirectX8)


The Frugal Gourmet
Feb 16th, 2001, 09:17 AM
Ok, I'm going to post my code here and see if anyone can tell me what's wrong with it. I'm not sure why I go through the rigamarole since it's never helped, but I thought -- since I'm so damn frustrated anyway -- I'd give it a shot.

Here's my DirectPlay8 code. It is my attempt at a client/server game. I load the server first. Then, I attempt to run the client which fails with an "already initialized" error (DPNERR_ALREADYINITIALIZED) every time it attempts to connect and run the DirectPlay8Event_ConnectComplete() procedure. Sime I'm a DirectX acolyte, I thought someone could help:

'DirectPlay stuff from Module
Global Dx As New DirectX8
Global Dp_Server As DirectPlay8Server
Global Dp_Address As DirectPlay8Address

Global cur_num_players As Integer
Global available_hosts As Integer

'Made up GUID
Public Const MyGuid As String = "{9E073B51-3493-400c-A56F-FD7BC85E7247}"

'Server
Implements DirectPlay8Event

Private Sub Form Load()

DirectPlay_Setup
End Sub

Private Sub DirectPlay_Setup()
Dim ServerDesc As DPN_APPLICATION_DESC

Set Dp_Server = Dx.DirectPlayServerCreate
Set Dp_Address = Dx.DirectPlayAddressCreate

Dp_Server.RegisterMessageHandler Start

With ServerDesc
.guidApplication = MyGuid
.lMaxPlayers = 10
.SessionName = "Ze Game"
.lFlags = DPNSESSION_CLIENT_SERVER
End With

Dp_Address.SetSP DP8SP_TCPIP

Dp_Address.AddComponentLong DPN_KEY_PORT, 9001
Dp_Address.AddComponentString DPN_KEY_HOSTNAME, "127.0.0.1"

Dp_Server.Host ServerDesc, Dp_Address

End Sub

'Client Code

'DirectPlay stuff
Global Dx As New DirectX8
Global Dp_Client As DirectPlay8Client
Global Dp_Host_Address As DirectPlay8Address
Global Dp_Client_Address As DirectPlay8Address

Global Player_Information As DPN_PLAYER_INFO

'Made up GUID
Public Const MyGuid As String = "{9E073B51-3493-400c-A56F-FD7BC85E7247}"

'Runs when form loads
Private Sub Setup_DirectPlay()

Call DirectPlay.Initialize_DirectPlay

Dp_Client.RegisterMessageHandler Start
Set Dp_Host_Address = Dx.DirectPlayAddressCreate
Set Dp_Client_Address = Dx.DirectPlayAddressCreate

Dp_Client_Address.SetSP DP8SP_TCPIP
Dp_Host_Address.SetSP DP8SP_TCPIP

Dp_Host_Address.AddComponentString DPN_KEY_HOSTNAME, "127.0.0.1"
Dp_Host_Address.AddComponentLong DPN_KEY_PORT, 9001
Dp_Client_Address.AddComponentString DPN_KEY_HOSTNAME, "127.0.0.1"
Dp_Client_Address.AddComponentLong DPN_KEY_PORT, 9001

End Sub

'Runs when user clicks to join game

Private Sub join_Game()

Dim Dp_Application As DPN_APPLICATION_DESC

With Player_Information
.Name = Cur_User.Name
.lInfoFlags = DPNINFO_NAME
End With

Dp_Application.guidApplication = MyGuid
Dp_Application.lFlags = DPNSESSION_CLIENT_SERVER

Dp_Client.SetClientInfo Player_Information, DPNOP_SYNC

'Calls DirectPlay8Event_ConnectComplete()
Dp_Client.Connect Dp_Application, Dp_Host_Address, Dp_Client_Address, 0, ByVal 0&, 0


End Sub

Private Sub DirectPlay8Event_ConnectComplete(dpnotify As DxVBLibA.DPNMSG_CONNECT_COMPLETE, fRejectMsg As Boolean)

'This procedure's dpnotify.hresultcode is the aforementioned error and the connection fails.

End Sub