home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / Source / GPCHAP08 / PROG8_3.CPP < prev    next >
C/C++ Source or Header  |  2002-04-25  |  6KB  |  235 lines

  1. // PROG8_3.CPP - Creates a DirectDraw object and changes display
  2.  
  3. // INCLUDES ///////////////////////////////////////////////
  4. #define WIN32_LEAN_AND_MEAN  
  5. #define INITGUID
  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. #include <ddraw.h>  // directX includes
  23.  
  24. // DEFINES ////////////////////////////////////////////////
  25.  
  26. // defines for windows 
  27. #define WINDOW_CLASS_NAME "WINXCLASS"  // class name
  28.  
  29. #define WINDOW_WIDTH  320              // size of window
  30. #define WINDOW_HEIGHT 200
  31.  
  32. // MACROS /////////////////////////////////////////////////
  33.  
  34. // these read the keyboard asynchronously
  35. #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
  36. #define KEY_UP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
  37.  
  38. // PROTOTYPES /////////////////////////////////////////////
  39.  
  40. int Game_Init(void *parms=NULL);
  41. int Game_Shutdown(void *parms=NULL);
  42. int Game_Main(void *parms=NULL);
  43.  
  44. // GLOBALS ////////////////////////////////////////////////
  45.  
  46. HWND main_window_handle = NULL; // save the window handle
  47. HINSTANCE main_instance = NULL; // save the instance
  48. char buffer[80];                // used to print text
  49. LPDIRECTDRAW7 lpdd;   // pointer to direct draw object
  50.  
  51. // FUNCTIONS //////////////////////////////////////////////
  52.  
  53. LRESULT CALLBACK WindowProc(HWND hwnd, 
  54.                             UINT msg, 
  55.                             WPARAM wparam, 
  56.                             LPARAM lparam)
  57. {
  58. // this is the main message handler of the system
  59. PAINTSTRUCT    ps;           // used in WM_PAINT
  60. HDC            hdc;       // handle to a device context
  61.  
  62. // what is the message 
  63. switch(msg)
  64.     {    
  65.     case WM_CREATE: 
  66.         {
  67.         // do initialization stuff here
  68.         return(0);
  69.         } break;
  70.  
  71.     case WM_PAINT:
  72.          {
  73.          // start painting
  74.          hdc = BeginPaint(hwnd,&ps);
  75.  
  76.          // end painting
  77.          EndPaint(hwnd,&ps);
  78.          return(0);
  79.         } break;
  80.  
  81.     case WM_DESTROY: 
  82.         {
  83.         // kill the application            
  84.         PostQuitMessage(0);
  85.         return(0);
  86.         } break;
  87.  
  88.     default:break;
  89.  
  90.     } // end switch
  91.  
  92. // process any messages that we didn't take care of 
  93. return (DefWindowProc(hwnd, msg, wparam, lparam));
  94.  
  95. } // end WinProc
  96.  
  97. // WINMAIN ////////////////////////////////////////////////
  98.  
  99. int WINAPI WinMain(    HINSTANCE hinstance,
  100.                     HINSTANCE hprevinstance,
  101.                     LPSTR lpcmdline,
  102.                     int ncmdshow)
  103. {
  104.  
  105. WNDCLASS winclass;    // this will hold the class we create
  106. HWND     hwnd;        // generic window handle
  107. MSG         msg;        // generic message
  108. HDC      hdc;       // generic dc
  109. PAINTSTRUCT ps;     // generic paintstruct
  110.  
  111. // first fill in the window class stucture
  112. winclass.style            = CS_DBLCLKS | CS_OWNDC | 
  113.                           CS_HREDRAW | CS_VREDRAW;
  114. winclass.lpfnWndProc    = WindowProc;
  115. winclass.cbClsExtra        = 0;
  116. winclass.cbWndExtra        = 0;
  117. winclass.hInstance        = hinstance;
  118. winclass.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
  119. winclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  120. winclass.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
  121. winclass.lpszMenuName    = NULL; 
  122. winclass.lpszClassName    = WINDOW_CLASS_NAME;
  123.  
  124. // register the window class
  125. if (!RegisterClass(&winclass))
  126.     return(0);
  127.  
  128. // create the window
  129. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  130.                           "WinX Game Console",     // title
  131.                           WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  132.                            0,0,       // x,y
  133.                           WINDOW_WIDTH,  // width
  134.                           WINDOW_HEIGHT, // height
  135.                           NULL,       // handle to parent 
  136.                           NULL,       // handle to menu
  137.                           hinstance,// instance
  138.                           NULL)))    // creation parms
  139. return(0);
  140.  
  141. // save the window handle and instance in a global
  142. main_window_handle = hwnd;
  143. main_instance      = hinstance;
  144.  
  145. // perform all game console specific initialization
  146. Game_Init();
  147.  
  148. // enter main event loop
  149. while(1)
  150.     {
  151.     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  152.         { 
  153.         // test if this is a quit
  154.         if (msg.message == WM_QUIT)
  155.            break;
  156.     
  157.         // translate any accelerator keys
  158.         TranslateMessage(&msg);
  159.  
  160.         // send the message to the window proc
  161.         DispatchMessage(&msg);
  162.         } // end if
  163.     
  164.     // main game processing goes here
  165.     Game_Main();
  166.  
  167.     } // end while
  168.  
  169. // shutdown game and release all resources
  170. Game_Shutdown();
  171.  
  172. // return to Windows like this
  173. return(msg.wParam);
  174.  
  175. } // end WinMain
  176.  
  177. // WINX GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////
  178.  
  179. int Game_Init(void *parms)
  180. {
  181. // this function is where you do all the initialization 
  182. // for your game
  183.  
  184. // create object and test for error
  185. if (DirectDrawCreateEx(NULL, (void **)&lpdd, IID_IDirectDraw7, NULL)!=DD_OK)
  186.    return(0);
  187.  
  188. // set cooperation level to windowed mode normal
  189. if ((lpdd->SetCooperativeLevel(main_window_handle,
  190.     DDSCL_ALLOWMODEX | DDSCL_FULLSCREEN | 
  191.     DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT))!=DD_OK)
  192.     return(0);
  193.  
  194. // set the display mode
  195. if ((lpdd->SetDisplayMode(640,480,8,0,0))!=DD_OK)
  196.    return(0);
  197.  
  198. // return success
  199. return(1);
  200. } // end Game_Init
  201.  
  202. ///////////////////////////////////////////////////////////
  203.  
  204. int Game_Shutdown(void *parms)
  205. {
  206. // this function is where you shutdown your game and
  207. // release all resources that you allocated
  208.  
  209. // release the directdraw object
  210. if (lpdd!=NULL)
  211.    lpdd->Release();
  212.  
  213. // return success
  214. return(1);
  215. } // end Game_Shutdown
  216.  
  217. ///////////////////////////////////////////////////////////
  218.  
  219. int Game_Main(void *parms)
  220. {
  221. // this is the workhorse of your game it will be called
  222. // continuously in real-time this is like main() in C
  223. // all the calls for you game go here!
  224.  
  225. // check of user is trying to exit
  226. if (KEY_DOWN(VK_ESCAPE) || KEY_DOWN(VK_SPACE))
  227.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  228.  
  229. // return success
  230. return(1);
  231. } // end Game_Main
  232.  
  233. ///////////////////////////////////////////////////////////
  234.  
  235.