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

  1. // PROG5_7.CPP - a bouncing ball demo of ellipse()
  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. // general defines
  20. #define BALL_RADIUS 20
  21.  
  22. // GLOBALS ////////////////////////////////////////////////
  23. HWND main_window_handle = NULL; // save the window handle
  24. HPEN blue_pen, black_pen;       // pens to draw with
  25.  
  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.  
  36. // what is the message 
  37. switch(msg)
  38.     {    
  39.     case WM_CREATE: 
  40.         {
  41.         // do initialization stuff here
  42.         // create the pens here
  43.         blue_pen  = CreatePen(PS_SOLID,0, RGB(0,0,255));
  44.         black_pen = CreatePen(PS_SOLID,0, RGB(0,0,0));
  45.         return(0);
  46.         } break;
  47.  
  48.     case WM_PAINT: 
  49.         {
  50.         // simply validate the window
  51.         hdc = BeginPaint(hwnd,&ps);     
  52.            EndPaint(hwnd,&ps);
  53.         return(0);
  54.            } break;
  55.  
  56.     case WM_DESTROY: 
  57.         {
  58.         // kill the application            
  59.  
  60.         // delete the pens
  61.         DeleteObject(blue_pen);
  62.         DeleteObject(black_pen);
  63.  
  64.         PostQuitMessage(0);
  65.         return(0);
  66.         } break;
  67.  
  68.     default:break;
  69.  
  70.     } // end switch
  71.  
  72. // process any messages that we didn't take care of 
  73. return (DefWindowProc(hwnd, msg, wparam, lparam));
  74.  
  75. } // end WinProc
  76.  
  77. // WINMAIN ////////////////////////////////////////////////
  78. int WINAPI WinMain(    HINSTANCE hinstance,
  79.                     HINSTANCE hprevinstance,
  80.                     LPSTR lpcmdline,
  81.                     int ncmdshow)
  82. {
  83.  
  84. WNDCLASS winclass;    // this will hold the class we create
  85. HWND     hwnd;        // generic window handle
  86. MSG         msg;        // generic message
  87. HDC      hdc;       // generic dc
  88. PAINTSTRUCT ps;     // generic paintstruct
  89. RECT     rect;      // generic rectangle
  90. HBRUSH   hbrush;    // generic brush handle
  91.  
  92. int ball_x = WINDOW_WIDTH/2, // initial position of ball
  93.     ball_y = WINDOW_WIDTH/2;
  94.  
  95. int dx = 4, // initial velocity of ball               
  96.     dy = 2;
  97.  
  98. // first fill in the window class stucture
  99. winclass.style            = CS_DBLCLKS | CS_OWNDC | 
  100.                           CS_HREDRAW | CS_VREDRAW;
  101. winclass.lpfnWndProc    = WindowProc;
  102. winclass.cbClsExtra        = 0;
  103. winclass.cbWndExtra        = 0;
  104. winclass.hInstance        = hinstance;
  105. winclass.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
  106. winclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  107. winclass.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
  108. winclass.lpszMenuName    = NULL;
  109. winclass.lpszClassName    = WINDOW_CLASS_NAME;
  110.  
  111. // register the window class
  112. if (!RegisterClass(&winclass))
  113.     return(0);
  114.  
  115. // create the window
  116. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  117.                           "Bouncing Ball Screen Saver",     // title
  118.                           WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  119.                            0,0,       // x,y
  120.                           WINDOW_WIDTH,  // width
  121.                           WINDOW_HEIGHT, // height
  122.                           NULL,       // handle to parent 
  123.                           NULL,       // handle to menu
  124.                           hinstance,// instance
  125.                           NULL)))    // creation parms
  126. return(0);
  127.  
  128. // save the window handle in a global
  129. main_window_handle = hwnd;
  130.  
  131. // enter main event loop
  132. while(1)
  133.     {
  134.     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  135.         { 
  136.         // test if this is a quit
  137.         if (msg.message == WM_QUIT)
  138.            break;
  139.     
  140.         // translate any accelerator keys
  141.         TranslateMessage(&msg);
  142.  
  143.         // send the message to the window proc
  144.         DispatchMessage(&msg);
  145.         } // end if
  146.  
  147.     // main game processing goes here /////////////////////
  148.     
  149.     // get the graphics device context 
  150.     hdc = GetDC(hwnd);
  151.  
  152.     // fill in rect structure
  153.     rect.left  = ball_x - BALL_RADIUS;
  154.     rect.right = ball_x + BALL_RADIUS;
  155.     rect.top   = ball_y - BALL_RADIUS;
  156.     rect.bottom= ball_y + BALL_RADIUS;
  157.     
  158.     // erase the ball /////////////////////////////////////
  159.     SelectObject(hdc, GetStockObject(BLACK_BRUSH));
  160.     SelectObject(hdc, black_pen);
  161.     Ellipse(hdc, rect.left, rect.top, rect.right, rect.bottom);
  162.  
  163.     // move the ball //////////////////////////////////////
  164.     ball_x+=dx;
  165.     ball_y+=dy;
  166.  
  167.     // test for collision with wall, if so reverse direction
  168.     if (ball_x >= WINDOW_WIDTH || ball_x <=0)
  169.        {
  170.        // reverse x velocity
  171.        dx=-dx;
  172.  
  173.        // move ball back
  174.        ball_x+=dx;
  175.        } // end if
  176.     
  177.     if (ball_y >= WINDOW_HEIGHT || ball_y <=0)
  178.        {
  179.        // reverse y velocity
  180.        dy=-dy;
  181.  
  182.        // move ball back
  183.        ball_y+=dy;
  184.        } // end if
  185.  
  186.         // fill in rect structure with new position
  187.     rect.left  = ball_x - BALL_RADIUS;
  188.     rect.right = ball_x + BALL_RADIUS;
  189.     rect.top   = ball_y - BALL_RADIUS;
  190.     rect.bottom= ball_y + BALL_RADIUS;
  191.     
  192.     // draw the ball
  193.     SelectObject(hdc, blue_pen);
  194.     Ellipse(hdc, rect.left, rect.top, rect.right, rect.bottom);
  195.     
  196.     // release the device context
  197.     ReleaseDC(hwnd,hdc);
  198.  
  199.     // slow things down a bit
  200.     Sleep(10);
  201.  
  202.     } // end while
  203.  
  204. // return to Windows like this
  205. return(msg.wParam);
  206.  
  207. } // end WinMain
  208.  
  209. ///////////////////////////////////////////////////////////
  210.  
  211.