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

  1. // PROG6_1.CPP - an example of using timers and counters
  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  320
  17. #define WINDOW_HEIGHT 200
  18.  
  19. // general defines
  20. #define TIMER_ID1_SEC  1 // id of 1 sec timer
  21. #define TIMER_ID3_SEC  2 // id of 3 sec timer
  22.  
  23. // GLOBALS ////////////////////////////////////////////////
  24. HWND main_window_handle = NULL; // save the window handle
  25. char buffer[80];                // used to print messages
  26. // FUNCTIONS //////////////////////////////////////////////
  27. LRESULT CALLBACK WindowProc(HWND hwnd, 
  28.                             UINT msg, 
  29.                             WPARAM wparam, 
  30.                             LPARAM lparam)
  31. {
  32. // this is the main message handler of the system
  33. PAINTSTRUCT        ps;        // used in WM_PAINT
  34. HDC                hdc;    // handle to a device context
  35. static int      counter1=0, // counters
  36.                 counter2=0;
  37.  
  38. // what is the message 
  39. switch(msg)
  40.     {    
  41.  
  42.     case WM_CREATE:
  43.          {
  44.          // create a 1 second timer
  45.          SetTimer(hwnd, TIMER_ID1_SEC, 1000,NULL);
  46.  
  47.          // now the 3 second timer
  48.          SetTimer(hwnd, TIMER_ID3_SEC, 3000,NULL);    
  49.  
  50.          // exit event handler
  51.          return(0);
  52.          } break;
  53.  
  54.     case WM_TIMER:
  55.          {
  56.          // there is a timer event, test what timer fired
  57.         switch(wparam)
  58.               {
  59.               case TIMER_ID1_SEC:
  60.                    {
  61.                    // print out a message
  62.                    // get the dc
  63.                    hdc = GetDC(hwnd);
  64.  
  65.                    // set the color
  66.                    SetTextColor(hdc,RGB(0,255,0));
  67.                    SetBkColor(hdc,RGB(0,0,0));
  68.                    SetBkMode(hdc,OPAQUE);
  69.  
  70.                    // build up the messages
  71.                    sprintf(buffer,"1 second timer has fired %d times",++counter1);
  72.  
  73.                    // print the message
  74.                    TextOut(hdc,0,0,buffer,strlen(buffer));
  75.  
  76.                    // release the dc
  77.                    ReleaseDC(hwnd,hdc);
  78.                    } break;
  79.  
  80.               case TIMER_ID3_SEC:
  81.                    {
  82.                    // make a beep
  83.                    MessageBeep(MB_ICONEXCLAMATION);
  84.  
  85.                    // get the dc
  86.                    hdc = GetDC(hwnd);
  87.  
  88.                    // set the color
  89.                    SetTextColor(hdc,RGB(0,255,0));
  90.                    SetBkColor(hdc,RGB(0,0,0));
  91.                    SetBkMode(hdc,OPAQUE);
  92.  
  93.                    // build up the messages
  94.                    sprintf(buffer,"3 second timer has fired %d times",++counter2);
  95.  
  96.                    // print the message
  97.                    TextOut(hdc,0,20,buffer,strlen(buffer));
  98.  
  99.                    // release the dc
  100.                    ReleaseDC(hwnd,hdc);
  101.  
  102.                    } break;
  103.  
  104.                // .. test for other id's
  105.  
  106.                default:break;
  107.  
  108.                } // end switch
  109.           } break;
  110.  
  111.  
  112.     case WM_PAINT: 
  113.         {
  114.         // simply validate the window
  115.         hdc = BeginPaint(hwnd,&ps);     
  116.            EndPaint(hwnd,&ps);
  117.         return(0);
  118.            } break;
  119.  
  120.     case WM_DESTROY: 
  121.         {
  122.         // kill the application            
  123.  
  124.         // kill the timers
  125.         KillTimer(hwnd,TIMER_ID1_SEC);
  126.         KillTimer(hwnd,TIMER_ID3_SEC);
  127.  
  128.         PostQuitMessage(0);
  129.         return(0);
  130.         } break;
  131.  
  132.     default:break;
  133.  
  134.     } // end switch
  135.  
  136. // process any messages that we didn't take care of 
  137. return (DefWindowProc(hwnd, msg, wparam, lparam));
  138.  
  139. } // end WinProc
  140.  
  141. // WINMAIN ////////////////////////////////////////////////
  142. int WINAPI WinMain(    HINSTANCE hinstance,
  143.                     HINSTANCE hprevinstance,
  144.                     LPSTR lpcmdline,
  145.                     int ncmdshow)
  146. {
  147.  
  148. WNDCLASS winclass;    // this will hold the class we create
  149. HWND     hwnd;        // generic window handle
  150. MSG         msg;        // generic message
  151. HDC      hdc;       // generic dc
  152. PAINTSTRUCT ps;     // generic paintstruct
  153. RECT     rect;      // generic rectangle
  154. HBRUSH   hbrush;    // generic brush handle
  155.  
  156. // first fill in the window class stucture
  157. winclass.style            = CS_DBLCLKS | CS_OWNDC | 
  158.                           CS_HREDRAW | CS_VREDRAW;
  159. winclass.lpfnWndProc    = WindowProc;
  160. winclass.cbClsExtra        = 0;
  161. winclass.cbWndExtra        = 0;
  162. winclass.hInstance        = hinstance;
  163. winclass.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
  164. winclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  165. winclass.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
  166. winclass.lpszMenuName    = NULL;
  167. winclass.lpszClassName    = WINDOW_CLASS_NAME;
  168.  
  169. // register the window class
  170. if (!RegisterClass(&winclass))
  171.     return(0);
  172.  
  173. // create the window
  174. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  175.                           "Out of Time",     // title
  176.                           WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  177.                            0,0,       // x,y
  178.                           WINDOW_WIDTH,  // width
  179.                           WINDOW_HEIGHT, // height
  180.                           NULL,       // handle to parent 
  181.                           NULL,       // handle to menu
  182.                           hinstance,// instance
  183.                           NULL)))    // creation parms
  184. return(0);
  185.  
  186. // save the window handle in a global
  187. main_window_handle = hwnd;
  188.  
  189. // enter main event loop
  190. while(1)
  191.     {
  192.     // get current time
  193.  
  194.     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  195.         { 
  196.         // test if this is a quit
  197.         if (msg.message == WM_QUIT)
  198.            break;
  199.     
  200.         // translate any accelerator keys
  201.         TranslateMessage(&msg);
  202.  
  203.         // send the message to the window proc
  204.         DispatchMessage(&msg);
  205.         } // end if
  206.  
  207.     // main game processing goes here /////////////////////
  208.     
  209.     // get the graphics device context 
  210.     hdc = GetDC(hwnd);
  211.     
  212.     // release the device context
  213.     ReleaseDC(hwnd,hdc);
  214.  
  215.     // wait until exatly 1 millisecond has passed from
  216.     // the time the event loop body was entered
  217.  
  218.     Sleep(10);
  219.  
  220.     } // end while
  221.  
  222. // return to Windows like this
  223. return(msg.wParam);
  224.  
  225. } // end WinMain
  226.  
  227. ///////////////////////////////////////////////////////////
  228.  
  229.