home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the 3D Game Programming Gurus / gurus.iso / Articles / LinkingUpDirectPlay / dpex.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-31  |  3.0 KB  |  126 lines

  1. #include "dpex.h"
  2.  
  3. #define WINDOW_CLASS_NAME "WINCLASS1"
  4. #define WINDOW_WIDTH  400
  5. #define WINDOW_HEIGHT 100
  6.  
  7. char                    buffer[80];                    //to hold the print messages
  8. BOOL                    g_bFullScreen = FALSE;
  9. BOOL                    g_bAllowWindowed = TRUE;
  10. BOOL                    g_bReInitialize = FALSE;
  11. int                        send_count=0;                //counter we'll be sending
  12. int                        received_count=0;            //store counter we recieve
  13. int                        length = 0;                    //of message
  14.  
  15.  
  16.  
  17. long FAR PASCAL WindowProc( HWND hWnd, UINT message, 
  18.                             WPARAM wParam, LPARAM lParam )
  19. {
  20.     switch( message )
  21.     {
  22.     case WM_ACTIVATEAPP:
  23.         break;
  24.  
  25.     case WM_CREATE:
  26.         break;
  27.  
  28.     case WM_PAINT:
  29.        break;
  30.  
  31.     case WM_DESTROY:
  32.          PostQuitMessage(0);        
  33.        return 0;
  34.     }
  35.  
  36.     return DefWindowProc(hWnd, message, wParam, lParam);
  37. }
  38.  
  39.  
  40.  
  41. // WINMAIN ////////////////////////////////////////////////
  42. int WINAPI WinMain(    HINSTANCE hinstance,
  43.                     HINSTANCE hprevinstance,
  44.                     LPSTR lpcmdline,
  45.                     int ncmdshow)
  46. {
  47.  
  48. WNDCLASS winclass;    // this will hold the class we create
  49. HWND     hwnd;        // generic window handle
  50. MSG         msg;        // generic message
  51. HDC      hdc;       // generic dc
  52.  
  53.  
  54. winclass.style            = CS_DBLCLKS | CS_OWNDC | 
  55.                           CS_HREDRAW | CS_VREDRAW;
  56. winclass.lpfnWndProc    = WindowProc;
  57. winclass.cbClsExtra        = 0;
  58. winclass.cbWndExtra        = 0;
  59. winclass.hInstance        = hinstance;
  60. winclass.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
  61. winclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  62. winclass.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
  63. winclass.lpszMenuName    = NULL;
  64. winclass.lpszClassName    = WINDOW_CLASS_NAME;
  65.  
  66. // register the window class
  67. if (!RegisterClass(&winclass))
  68.     return(0);
  69.  
  70.  
  71. //initialize Direct Play
  72. if (!(InitializeDirectPlay(hinstance)))
  73.    return(0);
  74.  
  75.  
  76. // create the window
  77. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, 
  78.                           "Direct Play Send/Receive Demo",    
  79.                           WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  80.                            0,0,     
  81.                           WINDOW_WIDTH, 
  82.                           WINDOW_HEIGHT,
  83.                           NULL,       
  84.                           NULL,       
  85.                           hinstance,
  86.                           NULL)))
  87. return(0);
  88.  
  89.     while(1)
  90.     {
  91.         if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
  92.         {
  93.             if (msg.message == WM_QUIT)
  94.               break;
  95.             
  96.             TranslateMessage(&msg);
  97.             DispatchMessage(&msg);
  98.         }
  99.         else
  100.         {    
  101.             
  102.             hdc = GetDC(hwnd);                //to output text
  103.  
  104.                                             //display counters 
  105.             length = sprintf(buffer,"SENT counter: %d   ", send_count++);
  106.             TextOut(hdc,0,0,buffer,length);
  107.             length = sprintf(buffer,"RECIEVED : %d   ", received_count);
  108.             TextOut(hdc,260,0,buffer,length);
  109.  
  110.             if (send_count > 30)
  111.               send_count = 0;
  112.  
  113.             ReleaseDC(hwnd,hdc);            //finish text operations
  114.  
  115.             SendFireMessage(send_count);    //send the counter
  116.             received_count = ReceiveFireMessage();  //get the received count
  117.  
  118.             Sleep(200);                        //wait a sec
  119.  
  120.         }                        
  121.     }
  122.        
  123.     UninitializeDirectPlay();                //uninitialize DP
  124.     return(msg.wParam);                        //quit to windows like so
  125. }
  126.