Converting MikeNet code from DBP to DGDK is very easy. To demonstrate, here is an example conversion of the cube world demo:
DBP Client Code
`Codes used to identify what each packet is supposed to be for
` Tcp
CODE_NEWPLAYER as integer
CODE_NEWPLAYER = 0
CODE_LEFTPLAYER as integer
CODE_LEFTPLAYER = 1
` Udp
CODE_POSITIONPLAYER as integer
CODE_POSITIONPLAYER = 0
` Variables
` Set velocity and turn speed for player movement
iVelocity as integer
iVelocity = 5
iTurnSpeed as integer
iTurnSpeed = 5
` To be filled during connection
iMaximumClients as integer
iMaximumOperations as integer
` Set port of the server, which is 6789
ConnectPort as integer
ConnectPort = 6789
` Set timeout value which is the length of time
` in seconds that the command will wait for the
` connection to complete until giving up
Timeout as integer
Timeout = 10
` Set the local port of the client, we set this
` to NULL so that MikeNet can find an available
` port for us automatically
LocalPort as integer
LocalPort = NULL
` Set the local IP of the client, if we set this
` to nothing then MikeNet will find our IP for us
LocalIP as string
LocalIP = ""
` Set the error mode which decides whether or not
` to display message boxes when an error occurs.
` we set this to 2 in order to enable message boxes
ErrorMode as integer
ErrorMode = 2
` Set the UDP receive buffer size which determines
` the maximum size of incoming UDP packets
UdpRecvSize as integer
UdpRecvSize = 1024
` Set the TCP receive buffer size which determines
` the maximum amount of data we can receive in one
` receive operation.
TcpRecvSize as integer
TcpRecvSize = 1024
` Set the number of threads that will be created
` to deal with incoming data. In most cases this
` should be set to the number of processors on your
` computer. MikeNet can determine this for you
` automatically if you set it to 0
NoThreads as integer
NoThreads = 0
` Get IP address to connect to
print "Enter the IP that you would like to connect to: "
ConnectIP as string
Input ConnectIP
` Try to connect
iConnect as integer
iConnect = mn Connect(ConnectIP, ConnectPort, Timeout, LocalIP, LocalPort, ErrorMode, UdpRecvSize, TcpRecvSize, NoThreads)
` If the connection was successful
if iConnect = 1
print "Connection was successful!"
` Find maximum number of clients and operations
` necassary for udp receiving
iMaximumClients = mn Get Max Clients()
iMaximumOperations = mn Get Max Operations()
` Print our local IP and port
printc "The local IP is: "
LocalIP$ as string
LocalIP$ = mn Get Local IP()
print LocalIP$
printc "The local port is: "
print str$(mn Get Local Port())
print "Press any key to continue"
wait key
endif
` If the connection timed out
if iConnect = 0
print "Connection timed out! Check that the server is switched on"
wait key
end
endif
` If an error occurred during connection
if iConnect = -1
print "An error occurred whilst trying to connect"
wait key
end
endif
` Setup the world for us to run around in
Sync On
Sync Rate 40
AutoCam Off
Make Matrix 1,5000,5000,50,50
Position Matrix 1,Camera Position X(), Camera Position Y()-20,Camera Position Z()
Set Camera Rotation XYZ
` Once connected, we loop until we become disconnected
while mn Client Connected(0) = 1
` Refresh screen
Sync
` Player movement
if UpKey() = 1 then Move Camera iVelocity
if DownKey() = 1 then Move Camera -iVelocity
if LeftKey() = 1 then Turn Camera Left iTurnSpeed
if RightKey() = 1 then Turn Camera Right iTurnSpeed
` Deal with new messages from the server
` Check for new TCP messages
iTcpPackets as integer
iTcpPackets = mn Recv TCP(NULL)
` If there is a new TCP message
if iTcpPackets = 1
` Get code for new message
iCode as integer
iCode = mn Get Int()
iPlayer as integer
iPlayer = mn Get Int()
` If the server is telling us that a new player has joined
` then create a cube for that player
if iCode = CODE_NEWPLAYER
if object exist(iPlayer) = 0
Make Object Cube iPlayer,50
endif
endif
` If the server is telling us that a player has left then
` delete the cube of that player
if iCode = CODE_LEFTPLAYER
if object exist(iPlayer) = 1
Delete Object iPlayer
endif
endif
endif
` Check for new UDP messages on a per client per operation basis
` Check each client
for iPlayer = 1 to iMaximumClients
` Check each operation for current client
for iCode = 0 to iMaximumOperations-1
` Check for new UDP messages
iUdpPackets as integer
iUdpPackets = mn Recv UDP(iPlayer,iCode)
` If there is a new UDP message
if iUdpPackets = 1
` Get the player's position and angle
` and apply them to his/her cube
PosX as float
PosX = mn Get Float()
PosY as float
PosY = mn Get Float()
PosZ as float
PosZ = mn Get Float()
RotX as float
RotX = mn Get Float()
RotY as float
RotY = mn Get Float()
RotZ as float
RotZ = mn Get Float()
if object exist(iPlayer) = 1
Position Object iPlayer,PosX,PosY,PosZ
Rotate Object iPlayer,RotX,RotY,RotZ
endif
endif
next iCode
next iPlayer
` Send our position/angle to the server via UDP
` Formulate packet
mn Add Int CODE_POSITIONPLAYER
mn Add Float Camera Position X()
mn Add Float Camera Position Y()
mn Add Float Camera Position Z()
mn Add Float Camera Angle X()
mn Add Float Camera Angle Y()
mn Add Float Camera Angle Z()
` Send packet
iResult as integer
iResult = mn Send UDP(NULL,0,1)
endwhile
` If we have become disconnected from the server
print "Lost connection to server!"
Wait Key
end
C++ Client Code
#include <winsock2.h>
#include <ws2tcpip.h>
#include <string>
#include <time.h>
#include <DarkGDK.h>
#include <MikeNet.h>
using namespace std;
// Codes used to identify what each packet is supposed to be for
// Tcp
#define CODE_NEWPLAYER 0
#define CODE_LEFTPLAYER 1
// Udp
#define CODE_POSITIONPLAYER 0
// Variables
// Set velocity and turn speed for player movement
const int iVelocity = 5, iTurnSpeed = 5;
// To be filled during connection
int iMaximumClients;
int iMaximumOperations;
void DarkGDK(void)
{
// Set port of the server, which is 6789
unsigned short ConnectPort = 6789;
// Set timeout value which is the length of time
// in seconds that the command will wait for the
// connection to complete until giving up
int Timeout = 10;
// Set the local port of the client, we set this
// to NULL so that MikeNet can find an available
// port for us automatically
unsigned short LocalPort = NULL;
// Set the local IP of the client, if we set this
// to nothing then MikeNet will find our IP for us
char * LocalIP = "";
// Set the error mode which decides whether or not
// to display message boxes when an error occurs
int ErrorMode = ERRORMODE_ENABLE_MESSAGE_BOX;
// Set the UDP receive buffer size which determines
// the maximum size of incoming UDP packets
int UdpRecvSize = 1024;
// Set the TCP receive buffer size which determines
// the maximum amount of data we can receive in one
// receive operation
int TcpRecvSize = 1024;
// Set the number of threads that will be created
// to deal with incoming data. In most cases this
// should be set to the number of processors on your
// computer. MikeNet can determine this for you
// automatically if you set it to 0
int NoThreads = 0;
// Get IP address to connect to
dbPrint("Enter the IP that you would like to connect to: ");
char * ConnectIP;
ConnectIP = dbInput();
// Try to connect
int iConnect = mnConnect(ConnectIP, ConnectPort, Timeout, LocalIP, LocalPort, ErrorMode, UdpRecvSize, TcpRecvSize, NoThreads);
delete[] ConnectIP;
// If the connection was successful
if(iConnect == 1)
{
dbPrint("Connection was successful!");
// Find maximum number of clients and operations
// necassary for udp receiving
iMaximumClients = mnGetMaxClients();
iMaximumOperations = mnGetMaxOperations();
// Print our local IP, port and udp mode
dbPrintC("The local IP is: ");
dbPrint(mnGetLocalIP());
dbPrintC("The local port is: ");
dbPrint(dbStr(mnGetLocalPort()));
dbPrintC("UDP Mode: ");
dbPrint(dbStr(mnGetUdpMode()));
dbPrint("Press any key to continue");
dbWaitKey();
}
// If the connection timed out
if(iConnect == 0)
{
dbPrint("Connection timed out! Check that the server is switched on");
dbWaitKey();
return;
}
// If an error occurred during connection
if(iConnect == -1)
{
dbPrint("An error occurred whilst trying to connect");
dbWaitKey();
return;
}
// Setup the world for us to run around in
dbSyncOn();
dbSyncRate(40);
dbAutoCamOff();
dbMakeMatrix(1,5000,5000,50,50);
dbPositionMatrix(1,dbCameraPositionX(),dbCameraPositionY()-20,dbCameraPositionZ());
// Once connected, we loop until we become disconnected or darkgdk tells us to exit
while( (mnClientConnected(0) == 1) && (LoopGDK() == true) )
{
// Refresh screen
dbSync();
// Player movement
if(dbUpKey() == 1){dbMoveCamera(iVelocity);}
if(dbDownKey() == 1){dbMoveCamera(-iVelocity);}
if(dbLeftKey() == 1){dbTurnCameraLeft(iTurnSpeed);}
if(dbRightKey() == 1){dbTurnCameraRight(iTurnSpeed);}
// Deal with new messages from the server
// Check for new TCP messages
int iTcpPackets = mnRecvTCP(NULL);
// If there is a new TCP message
if(iTcpPackets == 1)
{
// Get code for new message
int iCode = mnGetInt();
int iPlayer = mnGetInt();
// If the server is telling us that a new player has joined
// then create a cube for that player
if(iCode == CODE_NEWPLAYER)
{
dbMakeObjectCube(iPlayer,50);
}
// If the server is telling us that a player has left then
// delete the cube of that player
if(iCode == CODE_LEFTPLAYER)
{
dbDeleteObject(iPlayer);
}
}
// Check for new UDP messages on a per client per operation basis
// Check each client
for(int iPlayer = 1;iPlayer<=iMaximumClients;iPlayer++)
{
// Check each operation for current client
for(int iCode = 0;iCode<iMaximumOperations;iCode++)
{
// Check for new UDP messages
int iUdpPackets = mnRecvUDP(iPlayer,iCode);
// If there is a new UDP message
if(iUdpPackets == 1)
{
// Get the player's position and angle
// and apply them to his/her cube
float PosX = mnGetFloat();
float PosY = mnGetFloat();
float PosZ = mnGetFloat();
float RotX = mnGetFloat();
float RotY = mnGetFloat();
float RotZ = mnGetFloat();
dbPositionObject(iPlayer,PosX,PosY,PosZ);
dbRotateObject(iPlayer,RotX,RotY,RotZ);
}
}
}
// Send our position/angle to the server via UDP
// Formulate packet
mnAddInt(CODE_POSITIONPLAYER);
mnAddFloat(dbCameraPositionX());
mnAddFloat(dbCameraPositionY());
mnAddFloat(dbCameraPositionZ());
mnAddFloat(dbCameraAngleX());
mnAddFloat(dbCameraAngleY());
mnAddFloat(dbCameraAngleZ());
// Send packet
mnSendUDP(NULL,false,true);
}
// If we have become disconnected from the server
dbPrint("Lost connection to server!");
dbWaitKey();
mnFinish();
return;
}