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

  1. // DRAW.CPP - Drawing program based on the game console
  2.  
  3. // INCLUDES ///////////////////////////////////////////////
  4. #define WIN32_LEAN_AND_MEAN  
  5. #define INITGUID       // for directx
  6.  
  7. #include <windows.h>   // include important windows stuff
  8. #include <windowsx.h> 
  9. #include <mmsystem.h>
  10. #include <iostream.h> // include important C/C++ stuff
  11. #include <conio.h>
  12. #include <stdlib.h>
  13. #include <malloc.h>
  14. #include <memory.h>
  15. #include <string.h>
  16. #include <stdarg.h>
  17. #include <stdio.h>
  18. #include <math.h>
  19. #include <io.h>
  20. #include <fcntl.h>
  21.  
  22. // DEFINES ////////////////////////////////////////////////
  23.  
  24. // defines for windows 
  25. #define WINDOW_CLASS_NAME "WINXCLASS"  // class name
  26.  
  27. #define WINDOW_WIDTH  320              // size of window
  28. #define WINDOW_HEIGHT 200
  29.  
  30. // MACROS /////////////////////////////////////////////////
  31.  
  32. // these read the keyboard asynchronously
  33. #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
  34. #define KEY_UP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
  35.  
  36. // PROTOTYPES /////////////////////////////////////////////
  37.  
  38. int Game_Init(void *parms=NULL);
  39. int Game_Shutdown(void *parms=NULL);
  40. int Game_Main(void *parms=NULL);
  41.  
  42. // GLOBALS ////////////////////////////////////////////////
  43.  
  44. HWND main_window_handle = NULL; // save the window handle
  45. HINSTANCE main_instance = NULL; // save the instance
  46. HDC       main_gdc;             // save the graphics context
  47. char buffer[80];                // used to print text
  48.  
  49. // FUNCTIONS //////////////////////////////////////////////
  50.  
  51. LRESULT CALLBACK WindowProc(HWND hwnd, 
  52.                             UINT msg, 
  53.                             WPARAM wparam, 
  54.                             LPARAM lparam)
  55. {
  56. // this is the main message handler of the system
  57. PAINTSTRUCT    ps;           // used in WM_PAINT
  58. HDC            hdc;       // handle to a device context
  59.  
  60. // what is the message 
  61. switch(msg)
  62.     {    
  63.     case WM_CREATE: 
  64.         {
  65.         // do initialization stuff here
  66.         return(0);
  67.         } break;
  68.  
  69.     case WM_PAINT:
  70.          {
  71.          // start painting
  72.          hdc = BeginPaint(hwnd,&ps);
  73.  
  74.          // end painting
  75.          EndPaint(hwnd,&ps);
  76.          return(0);
  77.         } break;
  78.  
  79.     case WM_DESTROY: 
  80.         {
  81.         // kill the application            
  82.         PostQuitMessage(0);
  83.         return(0);
  84.         } break;
  85.  
  86.     default:break;
  87.  
  88.     } // end switch
  89.  
  90. // process any messages that we didn't take care of 
  91. return (DefWindowProc(hwnd, msg, wparam, lparam));
  92.  
  93. } // end WinProc
  94.  
  95. // WINMAIN ////////////////////////////////////////////////
  96.  
  97. int WINAPI WinMain(    HINSTANCE hinstance,
  98.                     HINSTANCE hprevinstance,
  99.                     LPSTR lpcmdline,
  100.                     int ncmdshow)
  101. {
  102.  
  103. WNDCLASS winclass;    // this will hold the class we create
  104. HWND     hwnd;        // generic window handle
  105. MSG         msg;        // generic message
  106. HDC      hdc;       // generic dc
  107. PAINTSTRUCT ps;     // generic paintstruct
  108.  
  109. // first fill in the window class stucture
  110. winclass.style            = CS_DBLCLKS | CS_OWNDC | 
  111.                           CS_HREDRAW | CS_VREDRAW;
  112. winclass.lpfnWndProc    = WindowProc;
  113. winclass.cbClsExtra        = 0;
  114. winclass.cbWndExtra        = 0;
  115. winclass.hInstance        = hinstance;
  116. winclass.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
  117. winclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  118. winclass.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
  119. winclass.lpszMenuName    = NULL; 
  120. winclass.lpszClassName    = WINDOW_CLASS_NAME;
  121.  
  122. // register the window class
  123. if (!RegisterClass(&winclass))
  124.     return(0);
  125.  
  126. // create the window
  127. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  128.                           "WinX Game Console - Draw Demo",     // title
  129.                           WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  130.                            0,0,       // x,y
  131.                           WINDOW_WIDTH,  // width
  132.                           WINDOW_HEIGHT, // height
  133.                           NULL,       // handle to parent 
  134.                           NULL,       // handle to menu
  135.                           hinstance,// instance
  136.                           NULL)))    // creation parms
  137. return(0);
  138.  
  139. // save the window handle and instance in a global
  140. main_window_handle = hwnd;
  141. main_instance      = hinstance;
  142.  
  143. // perform all game console specific initialization
  144. Game_Init();
  145.  
  146. // enter main event loop
  147. while(1)
  148.     {
  149.     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  150.         { 
  151.         // test if this is a quit
  152.         if (msg.message == WM_QUIT)
  153.            break;
  154.     
  155.         // translate any accelerator keys
  156.         TranslateMessage(&msg);
  157.  
  158.         // send the message to the window proc
  159.         DispatchMessage(&msg);
  160.         } // end if
  161.     
  162.     // main game processing goes here
  163.     Game_Main();
  164.  
  165.     } // end while
  166.  
  167. // shutdown game and release all resources
  168. Game_Shutdown();
  169.  
  170. // return to Windows like this
  171. return(msg.wParam);
  172.  
  173. } // end WinMain
  174.  
  175. // WINX GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////
  176.  
  177. int Game_Init(void *parms)
  178. {
  179. // this function is where you do all the initialization 
  180. // for your game
  181.  
  182. // retrieve the graphics device context once and save it
  183. // we can do this since the winclass has OWN_DC
  184.  
  185. main_gdc = GetDC(main_window_handle);
  186.  
  187. // return success
  188. return(1);
  189. } // end Game_Init
  190.  
  191. ///////////////////////////////////////////////////////////
  192.  
  193. int Game_Shutdown(void *parms)
  194. {
  195. // this function is where you shutdown your game and
  196. // release all resources that you allocated
  197.  
  198. // release the graphics device context
  199. ReleaseDC(main_window_handle, main_gdc);
  200.  
  201. // return success
  202. return(1);
  203. } // end Game_Shutdown
  204.  
  205. ///////////////////////////////////////////////////////////
  206.  
  207. int Game_Main(void *parms)
  208. {
  209. // this is the workhorse of your game it will be called
  210. // continuously in real-time this is like main() in C
  211. // all the calls for you game go here!
  212.  
  213. // notice the use of "static" this is so the variables
  214. // don't change from call to call
  215.  
  216. static int x = WINDOW_WIDTH/2,    // initial position
  217.            y = WINDOW_HEIGHT/2;
  218.  
  219. static COLORREF color = RGB(255,0,0); // initial color
  220.  
  221. // test for user input
  222.  
  223. // trying to escape
  224. if (KEY_DOWN(VK_ESCAPE))
  225.    {
  226.    // exit
  227.    PostMessage(main_window_handle,WM_DESTROY,0,0);
  228.    } // end if
  229.  
  230. // is user moving cursor
  231. // x-axis
  232. if (KEY_DOWN(VK_RIGHT))
  233.    x++;
  234. else
  235. if (KEY_DOWN(VK_LEFT))
  236.    x--;
  237.  
  238. // y-axis
  239. if (KEY_DOWN(VK_UP))
  240.    y--;
  241. else
  242. if (KEY_DOWN(VK_DOWN))
  243.    y++;
  244.  
  245. // is user trying to change color
  246. if (KEY_DOWN('C'))
  247.    color=RGB(rand()%256,rand()%256,rand()%256);
  248.  
  249. // make sure cursor stays within window x-axis
  250. if (x>WINDOW_WIDTH)
  251.    x=0;
  252. else
  253. if (x<0)
  254.    x=WINDOW_WIDTH-1;
  255.  
  256. // make sure cursor stays within window y-axis
  257. if (y>WINDOW_HEIGHT)
  258.    y=0;
  259. else
  260. if (y<0)
  261.    y=WINDOW_HEIGHT-1;
  262.  
  263. // draw pen tip
  264. SetPixel(main_gdc, x,y,color);
  265. SetPixel(main_gdc, x+1,y,color);
  266. SetPixel(main_gdc, x,y+1,color);
  267. SetPixel(main_gdc, x+1,y+1,color);
  268.  
  269. // wait a sec
  270. Sleep(20);
  271.  
  272. // return success
  273. return(1);
  274. } // end Game_Main
  275.  
  276. ///////////////////////////////////////////////////////////
  277.  
  278.