Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
99 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

  Contents

 Introduction
 Let's get started
 Implementation
 Setting up
 the Connection

 Message
 Management

 Putting it
 Together


 Printable version

 


Actual Implementation

Whew. Now we get to more details. If you didn't quite understand any of the above, please read them again till you do. If you have any questions like "What if..." it will be answered soon. So let's move on.

The first thing to do is to include the DirectPlay header files:

#include <dplay.h> // directplay main #include <dplobby.h> // the directplay lobby

Also add DPLAYX.LIB to your project.

If you are wondering why there is a dplay.lib and dplayx.lib, add the dplayx.lib cause I think there are more methods there used in the lobby. Also if you are asking why am I including the dplobby methods when I am not using a lobby server, it will become clearer later.

Also you need to define INITGUID or add the dxguid.lib. Define this at the very top of your project.

#define INITGUID // to use the predefined ids

Next you need to give your application a GUID (Global Unique Id). This ID is to distinguish the application in the computer. You don't want your application to send messages to your browser, only your application. You can use guidgen.exe to create a id for your application. Microsoft guarantees that it will never mathematically create the same GUID twice, so we take their word for it. It will look something like

DEFINE_GUID(our_program_id, 0x5bfdb060, 0x6a4, 0x11d0, 0x9c, 0x4f, 0x0, 0xa0, 0xc9, 0x5, 0x42, 0x5e);

Now to define our globals

// interface pointer to directplay LPDIRECTPLAY3A lpdp = NULL; // lobby interface pointer LPDIRECTPLAYLOBBY2A lpdplobby = NULL;

If you are wondering what the A behind the interface stands for, it means ANSI version. There are two versions for DirectPlay – ANSI and Unicode. (Unicode is a standard for using 16 bits to represent a character instead of 8 bits, just for internationalization. Just use the ANSI version and forget about supporting multiple languages. Makes everybody happy.)

The next thing is the main loop of the program. This is the bare skeleton of what it looks like.

// Get information from local player // Initialize Directplay connection with the // information from above while(1) { if (PeekMessage(&msg,NULL,0,0,PM_REMOVE) { // Your normal translating message } // end if PeekMessage(..) else { // Your game loop // Receive messages – We will // implement this } // end else } // end of while(1) // Close DirectPlay Connection

How you get information from the user is up to you. You can do it via a dialog box or any other way you deem. The main thing you need to get is the name and whether if it is the server or client. If it is the server, you get the session name. If it is the client, you get the TCP/IP address to connect to. Anyway, we store those in a global below:

BOOL gameServer; // flag for client/server char player_name[10]; // local player name char session_name[10]; // name of session char tcp_address[15]; // ip address to connect to

I'm sorry if you are firmly against globals. Feel free encapsulate them, but I think globals simplify the learning process here. Also I do not do much error checking here; theoretically you should test the result of every function call.

Now before we move on, we should implement a list of players in the current session. Although it will not be necessary here, you will need it in larger applications.

You create a list with the following item element:

Class DP_PLAYER_LIST_ELEM{ DPID dpid; // the directplay id of player char name[10]; // name of player DWORD flags; // directplay player flags // any other info you might need };

You need to implement a list class that adds a player and deletes a player with a specific dpid. Do not reference the players by their names because players can have the same name. Use the id to differentiate players. Due to space constraints, I will not include any code here. Alternatively you can use arrays to hold the global player information, but this is not scalable and more troublesome.

Then we define a global pointer to the class:

// list of players in current session DP_PLAYER_LIST *dp_player_list;

That is about all the globals you need. You should also create a local player struct for additional information for local players only.


Next : Setting Up The Connection