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

  1. // PROG5_6.CPP - random rectangles
  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. RECT     rect;      // generic rectangle
  79. HBRUSH   hbrush;    // generic brush handle
  80.  
  81. // first fill in the window class stucture
  82. winclass.style            = CS_DBLCLKS | CS_OWNDC | 
  83.                           CS_HREDRAW | CS_VREDRAW;
  84. winclass.lpfnWndProc    = WindowProc;
  85. winclass.cbClsExtra        = 0;
  86. winclass.cbWndExtra        = 0;
  87. winclass.hInstance        = hinstance;
  88. winclass.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
  89. winclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  90. winclass.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
  91. winclass.lpszMenuName    = NULL;
  92. winclass.lpszClassName    = WINDOW_CLASS_NAME;
  93.  
  94. // register the window class
  95. if (!RegisterClass(&winclass))
  96.     return(0);
  97.  
  98. // create the window
  99. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  100.                           "Random Rectangles!",     // title
  101.                           WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  102.                            0,0,       // x,y
  103.                           WINDOW_WIDTH,  // width
  104.                           WINDOW_HEIGHT, // height
  105.                           NULL,       // handle to parent 
  106.                           NULL,       // handle to menu
  107.                           hinstance,// instance
  108.                           NULL)))    // creation parms
  109. return(0);
  110.  
  111. // save the window handle in a global
  112. main_window_handle = hwnd;
  113.  
  114. // enter main event loop
  115. while(1)
  116.     {
  117.     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  118.         { 
  119.         // test if this is a quit
  120.         if (msg.message == WM_QUIT)
  121.            break;
  122.     
  123.         // translate any accelerator keys
  124.         TranslateMessage(&msg);
  125.  
  126.         // send the message to the window proc
  127.         DispatchMessage(&msg);
  128.         } // end if
  129.     else
  130.     {
  131.     // main game processing goes here /////////////////////
  132.     
  133.     // get the graphics device context 
  134.     hdc = GetDC(hwnd);
  135.  
  136.     // create a random rectangle
  137.     rect.left   = rand()%WINDOW_WIDTH;
  138.     rect.top    = rand()%WINDOW_HEIGHT;
  139.     rect.right  = rand()%WINDOW_WIDTH;
  140.     rect.bottom = rand()%WINDOW_HEIGHT;
  141.  
  142.     // create a random brush
  143.     hbrush = CreateSolidBrush(RGB(rand()%256,rand()%256,rand()%256));
  144.  
  145.     // draw either a filled rect or a wireframe rect
  146.     if ((rand()%2)==1)
  147.         FillRect(hdc,&rect,hbrush);
  148.     else
  149.         FrameRect(hdc,&rect,hbrush);
  150.  
  151.     // now delete the brush
  152.     DeleteObject(hbrush);
  153.     
  154.     // release the device context
  155.     ReleaseDC(hwnd,hdc);
  156.     } // end else
  157.  
  158.     } // end while
  159.  
  160. // return to Windows like this
  161. return(msg.wParam);
  162.  
  163. } // end WinMain
  164.  
  165. ///////////////////////////////////////////////////////////
  166.  
  167.