home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / Source / GPCHAP05 / PROG5_5.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  2002-04-25  |  4.1 KB  |  163 lines

  1. // PROG5_5.CPP - random lines
  2.  
  3. // INCLUDES ///////////////////////////////////////////////
  4. #define WIN32_LEAN_AND_MEAN  
  5.  
  6. #include <windows.h>  
  7. #include <windowsx.h>  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <math.h>
  11.  
  12. // DEFINES ////////////////////////////////////////////////
  13.  
  14. // defines for windows 
  15. #define WINDOW_CLASS_NAME "WINCLASS1"
  16. #define WINDOW_WIDTH  400
  17. #define WINDOW_HEIGHT 400
  18.  
  19. // GLOBALS ////////////////////////////////////////////////
  20. HWND main_window_handle = NULL; // save the window handle
  21. char buffer[80];                // used to print text
  22.  
  23. // FUNCTIONS //////////////////////////////////////////////
  24. LRESULT CALLBACK WindowProc(HWND hwnd, 
  25.                             UINT msg, 
  26.                             WPARAM wparam, 
  27.                             LPARAM lparam)
  28. {
  29. // this is the main message handler of the system
  30. PAINTSTRUCT        ps;        // used in WM_PAINT
  31. HDC                hdc;    // handle to a device context
  32.  
  33. // what is the message 
  34. switch(msg)
  35.     {    
  36.     case WM_CREATE: 
  37.         {
  38.         // do initialization stuff here
  39.         return(0);
  40.         } break;
  41.  
  42.     case WM_PAINT: 
  43.         {
  44.         // simply validate the window
  45.         hdc = BeginPaint(hwnd,&ps);     
  46.            EndPaint(hwnd,&ps);
  47.         return(0);
  48.            } break;
  49.  
  50.     case WM_DESTROY: 
  51.         {
  52.         // kill the application            
  53.         PostQuitMessage(0);
  54.         return(0);
  55.         } break;
  56.  
  57.     default:break;
  58.  
  59.     } // end switch
  60.  
  61. // process any messages that we didn't take care of 
  62. return (DefWindowProc(hwnd, msg, wparam, lparam));
  63.  
  64. } // end WinProc
  65.  
  66. // WINMAIN ////////////////////////////////////////////////
  67. int WINAPI WinMain(    HINSTANCE hinstance,
  68.                     HINSTANCE hprevinstance,
  69.                     LPSTR lpcmdline,
  70.                     int ncmdshow)
  71. {
  72.  
  73. WNDCLASS winclass;    // this will hold the class we create
  74. HWND     hwnd;        // generic window handle
  75. MSG         msg;        // generic message
  76. HDC      hdc;       // generic dc
  77. PAINTSTRUCT ps;     // generic paintstruct
  78. HPEN     hpen,old_hpen;      // generic pen handles
  79.  
  80. // first fill in the window class stucture
  81. winclass.style            = CS_DBLCLKS | CS_OWNDC | 
  82.                           CS_HREDRAW | CS_VREDRAW;
  83. winclass.lpfnWndProc    = WindowProc;
  84. winclass.cbClsExtra        = 0;
  85. winclass.cbWndExtra        = 0;
  86. winclass.hInstance        = hinstance;
  87. winclass.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
  88. winclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  89. winclass.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
  90. winclass.lpszMenuName    = NULL;
  91. winclass.lpszClassName    = WINDOW_CLASS_NAME;
  92.  
  93. // register the window class
  94. if (!RegisterClass(&winclass))
  95.     return(0);
  96.  
  97. // create the window
  98. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  99.                           "Random Lines!",     // title
  100.                           WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  101.                            0,0,       // x,y
  102.                           WINDOW_WIDTH,  // width
  103.                           WINDOW_HEIGHT, // height
  104.                           NULL,       // handle to parent 
  105.                           NULL,       // handle to menu
  106.                           hinstance,// instance
  107.                           NULL)))    // creation parms
  108. return(0);
  109.  
  110. // save the window handle in a global
  111. main_window_handle = hwnd;
  112.  
  113. // enter main event loop
  114. while(1)
  115.     {
  116.     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  117.         { 
  118.         // test if this is a quit
  119.         if (msg.message == WM_QUIT)
  120.            break;
  121.     
  122.         // translate any accelerator keys
  123.         TranslateMessage(&msg);
  124.  
  125.         // send the message to the window proc
  126.         DispatchMessage(&msg);
  127.         } // end if
  128.     
  129.     // main game processing goes here /////////////////////
  130.     
  131.     // get the graphics device context 
  132.     hdc = GetDC(hwnd);
  133.  
  134.     // create a random colored pen
  135.     hpen = CreatePen(PS_SOLID,1,RGB(rand()%256,rand()%256,rand()%256));
  136.  
  137.     // select the pen into context
  138.     old_hpen = (HPEN)SelectObject(hdc,hpen);
  139.  
  140.     // move to a random postion
  141.     MoveToEx(hdc, rand()%WINDOW_WIDTH, rand()%WINDOW_HEIGHT, NULL);
  142.  
  143.     // draw a line
  144.     LineTo(hdc,rand()%WINDOW_WIDTH, rand()%WINDOW_HEIGHT);
  145.  
  146.     // now delete the pen
  147.     SelectObject(hdc,old_hpen);
  148.     DeleteObject(hpen);
  149.     
  150.     // release the device context
  151.     ReleaseDC(hwnd,hdc);
  152.     
  153.  
  154.     } // end while
  155.  
  156. // return to Windows like this
  157. return(msg.wParam);
  158.  
  159. } // end WinMain
  160.  
  161. ///////////////////////////////////////////////////////////
  162.  
  163.