Platform SDK: DirectX

Basic Steps in Writing a DirectPlay Application

This section is an overview of the basic steps a typical application would take in order to participate in a non-lobbied multiplayer game or other networked application.

[C++]

To see these steps implemented in code, see the source for the Duel sample. See also Tutorial 2: Connecting by Using a Dialog Box.

1. Initialize DirectPlay

The application must first create a DirectPlay object by creating an instance of the IDirectPlay4 interface. The methods of this interface are used to manage all aspects of communication, other than within a lobby.

2. Enumerate the service providers

The application enumerates the service providers available on the system by using the IDirectPlay4::EnumConnections method. Typically, the available connections are presented to the user in a list box, and the user is asked to select one.

3. Initialize the connection

When the user has selected a connection, the application calls IDirectPlay4::InitializeConnection. At this point the service provider may ask the user to supply further information, such as a preferred modem and telephone number. If the user cancels out of any service provider dialogs, he or she can still create a session but cannot join an existing one.

4. Create or join a session

The application typically gives the user the choice of creating a new session or joining an existing one. To get a list of existing sessions, it calls IDirectPlay4::EnumSessions. The IDirectPlay4::Open or IDirectPlay4::SecureOpen method is used to create or join a session. An application that creates a session automatically joins it as well, and normally becomes the host.

5. Create a player

Once the application is part of a session, it must create at least one player for purposes of identification and message management. This is done by using the IDirectPlay4::CreatePlayer method.

As players are created, they can be enumerated by calling IDirectPlay4::EnumPlayers. The application might do this, for example, to display a list of names of players currently enrolled in the session.

Players can also be organized in groups and subgroups. For more information, see Group Management.

6. Start the Game

The application determines how and when the game (or other kind of session) starts. The host user might be permitted to initiate play at any time after a quorum of players has been reached, or play might start automatically when a certain number of players have joined. The host sends a message to all players announcing that play has started. Players now begin exchanging messages in order to update the state of the game, using the IDirectPlay4::Send, IDirectPlay4::SendEx, and IDirectPlay4::Receive methods.

[Visual Basic]

To see these steps implemented in code, see Tutorial 1: Creating a Simple Network Game.

1. Initialize DirectPlay

The application must first create a DirectPlay4 object by calling DirectX7.DirectPlayCreate. The methods of the DirectPlay4 class are used to manage all aspects of communication, other than with a lobby.

2. Enumerate the service providers

The application enumerates the connections available through the various service providers on the system by using the DirectPlay4.GetDPEnumConnections method. Typically, the available connections are presented to the user in a list box, and the user is asked to select one.

3. Initialize the connection

When the user has selected a connection, the application calls DirectPlay4.InitializeConnection. This step binds DirectPlay to a particular service provider. At this point the service provider may ask the user to supply further information, such as a preferred modem and telephone number. If the user cancels out of any service provider dialogs, he or she can still create a session, postponing the establishment of a physical connection until another player is ready to connect, for instance by dialing in.

4. Create or join a session

The application typically gives the user the choice of creating a new session or, if a physical connection has been established, joining an existing one. To get a list of existing sessions, it calls DirectPlay4.GetDPEnumSessions. The DirectPlay4.Open or DirectPlay4.SecureOpen method is used to create or join a session. An application that creates a session automatically joins it as well, and normally becomes the host.

5. Create a player

Once the application is part of a session, it must create at least one player for purposes of identification and message management. This is done by using the DirectPlay4.CreatePlayer method.

As players are created, they can be enumerated by calling DirectPlay4.GetDPEnumPlayers. The application might do this, for example, to display a list of players currently enrolled in the session.

Players can also be organized in groups and subgroups. For more information, see Group Management.

6. Start the Game

The application determines how and when the game (or other kind of session) starts. The host user might be permitted to initiate play at any time after a quorum of players has been reached, or play might start automatically when a certain number of players have joined. The host sends an application-defined message to all players announcing that play has started. Players now begin exchanging messages in order to update the state of the game, using the DirectPlay4.Send, DirectPlay4.SendEx, and DirectPlay4.Receive methods.