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

  1. // PROG9_1.CPP - Creates a DirectDraw object, changes display
  2. // and creates a primary surface, notice the change in 
  3. // the window creation flags this time
  4.  
  5. // INCLUDES ///////////////////////////////////////////////
  6. #define WIN32_LEAN_AND_MEAN  
  7. #define INITGUID
  8.  
  9. #include <windows.h>   // include important windows stuff
  10. #include <windowsx.h> 
  11. #include <mmsystem.h>
  12. #include <iostream.h> // include important C/C++ stuff
  13. #include <conio.h>
  14. #include <stdlib.h>
  15. #include <malloc.h>
  16. #include <memory.h>
  17. #include <string.h>
  18. #include <stdarg.h>
  19. #include <stdio.h>
  20. #include <math.h>
  21. #include <io.h>
  22. #include <fcntl.h>
  23.  
  24. #include <ddraw.h>  // directX includes
  25.  
  26. // DEFINES ////////////////////////////////////////////////
  27.  
  28. // defines for windows 
  29. #define WINDOW_CLASS_NAME "WINXCLASS"  // class name
  30.  
  31. #define WINDOW_WIDTH  800              // size of window
  32. #define WINDOW_HEIGHT 600
  33. #define SCREEN_WIDTH  800              // size of screen
  34. #define SCREEN_HEIGHT 600
  35. #define SCREEN_BPP    16              // bits per pixel
  36.  
  37. // MACROS /////////////////////////////////////////////////
  38.  
  39. // these read the keyboard asynchronously
  40. #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
  41. #define KEY_UP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
  42.  
  43. // TYPES //////////////////////////////////////////////////
  44.  
  45. typedef unsigned short USHORT;
  46. typedef unsigned short WORD;
  47. typedef unsigned char  UCHAR;
  48. typedef unsigned char  BYTE;
  49.  
  50. // PROTOTYPES /////////////////////////////////////////////
  51.  
  52. int Game_Init(void *parms=NULL);
  53. int Game_Shutdown(void *parms=NULL);
  54. int Game_Main(void *parms=NULL);
  55.  
  56. // GLOBALS ////////////////////////////////////////////////
  57.  
  58. HWND main_window_handle = NULL; // save the window handle
  59. HINSTANCE main_instance = NULL; // save the instance
  60. char buffer[80];                // used to print text
  61.  
  62. LPDIRECTDRAW7        lpdd         = NULL;  // dd object
  63. LPDIRECTDRAWSURFACE7 lpddsprimary = NULL;  // dd primary surface
  64. LPDIRECTDRAWSURFACE7 lpddsback    = NULL;  // dd back surface
  65. LPDIRECTDRAWPALETTE  lpddpal      = NULL;  // a pointer to the created dd palette
  66. DDSURFACEDESC2       ddsd;                 // a direct draw surface description struct
  67. DDSCAPS2             ddscaps;              // a direct draw surface capabilities struct
  68. HRESULT              ddrval;               // result back from dd calls
  69.  
  70. // FUNCTIONS //////////////////////////////////////////////
  71.  
  72. LRESULT CALLBACK WindowProc(HWND hwnd, 
  73.                             UINT msg, 
  74.                             WPARAM wparam, 
  75.                             LPARAM lparam)
  76. {
  77. // this is the main message handler of the system
  78. PAINTSTRUCT    ps;           // used in WM_PAINT
  79. HDC            hdc;       // handle to a device context
  80.  
  81. // what is the message 
  82. switch(msg)
  83.     {    
  84.     case WM_CREATE: 
  85.         {
  86.         // do initialization stuff here
  87.         return(0);
  88.         } break;
  89.  
  90.     case WM_PAINT:
  91.          {
  92.          // start painting
  93.          hdc = BeginPaint(hwnd,&ps);
  94.  
  95.          // end painting
  96.          EndPaint(hwnd,&ps);
  97.          return(0);
  98.         } break;
  99.  
  100.     case WM_DESTROY: 
  101.         {
  102.         // kill the application            
  103.         PostQuitMessage(0);
  104.         return(0);
  105.         } break;
  106.  
  107.     default:break;
  108.  
  109.     } // end switch
  110.  
  111. // process any messages that we didn't take care of 
  112. return (DefWindowProc(hwnd, msg, wparam, lparam));
  113.  
  114. } // end WinProc
  115.  
  116. // WINMAIN ////////////////////////////////////////////////
  117.  
  118. int WINAPI WinMain(    HINSTANCE hinstance,
  119.                     HINSTANCE hprevinstance,
  120.                     LPSTR lpcmdline,
  121.                     int ncmdshow)
  122. {
  123.  
  124. WNDCLASS winclass;    // this will hold the class we create
  125. HWND     hwnd;        // generic window handle
  126. MSG         msg;        // generic message
  127. HDC      hdc;       // generic dc
  128. PAINTSTRUCT ps;     // generic paintstruct
  129.  
  130. // first fill in the window class stucture
  131. winclass.style            = CS_DBLCLKS | CS_OWNDC | 
  132.                           CS_HREDRAW | CS_VREDRAW;
  133. winclass.lpfnWndProc    = WindowProc;
  134. winclass.cbClsExtra        = 0;
  135. winclass.cbWndExtra        = 0;
  136. winclass.hInstance        = hinstance;
  137. winclass.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
  138. winclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  139. winclass.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
  140. winclass.lpszMenuName    = NULL; 
  141. winclass.lpszClassName    = WINDOW_CLASS_NAME;
  142.  
  143. // register the window class
  144. if (!RegisterClass(&winclass))
  145.     return(0);
  146.  
  147. // create the window, note the use of WS_POPUP
  148. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  149.                           "WinX Game Console",     // title
  150.                           WS_POPUP | WS_VISIBLE,
  151.                            0,0,       // x,y
  152.                           WINDOW_WIDTH,  // width
  153.                           WINDOW_HEIGHT, // height
  154.                           NULL,       // handle to parent 
  155.                           NULL,       // handle to menu
  156.                           hinstance,// instance
  157.                           NULL)))    // creation parms
  158. return(0);
  159.  
  160. // save the window handle and instance in a global
  161. main_window_handle = hwnd;
  162. main_instance      = hinstance;
  163.  
  164. // perform all game console specific initialization
  165. Game_Init();
  166.  
  167. // enter main event loop
  168. while(1)
  169.     {
  170.     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  171.         { 
  172.         // test if this is a quit
  173.         if (msg.message == WM_QUIT)
  174.            break;
  175.     
  176.         // translate any accelerator keys
  177.         TranslateMessage(&msg);
  178.  
  179.         // send the message to the window proc
  180.         DispatchMessage(&msg);
  181.         } // end if
  182.     
  183.     // main game processing goes here
  184.     Game_Main();
  185.  
  186.     } // end while
  187.  
  188. // shutdown game and release all resources
  189. Game_Shutdown();
  190.  
  191. // return to Windows like this
  192. return(msg.wParam);
  193.  
  194. } // end WinMain
  195.  
  196. // WINX GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////
  197.  
  198. int Game_Init(void *parms)
  199. {
  200. // this function is where you do all the initialization 
  201. // for your game
  202.  
  203. // create object and test for error
  204. if (DirectDrawCreateEx(NULL, (void **)&lpdd, IID_IDirectDraw7, NULL)!=DD_OK)
  205.    return(0);
  206.  
  207. // set cooperation level to windowed mode normal
  208. if (lpdd->SetCooperativeLevel(main_window_handle,
  209.            DDSCL_ALLOWMODEX | DDSCL_FULLSCREEN | 
  210.            DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT)!=DD_OK)
  211.     return(0);
  212.  
  213. // set the display mode
  214. if (lpdd->SetDisplayMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,0,0)!=DD_OK)
  215.    return(0);
  216.  
  217. // Create the primary surface
  218. memset(&ddsd,0,sizeof(ddsd));
  219. ddsd.dwSize         = sizeof(ddsd);
  220. ddsd.dwFlags        = DDSD_CAPS;
  221. ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
  222.  
  223. if (lpdd->CreateSurface(&ddsd,&lpddsprimary,NULL)!=DD_OK)
  224.    return(0);
  225.  
  226. // return success
  227. return(1);
  228. } // end Game_Init
  229.  
  230. ///////////////////////////////////////////////////////////
  231.  
  232. int Game_Shutdown(void *parms)
  233. {
  234. // this function is where you shutdown your game and
  235. // release all resources that you allocated
  236.  
  237. // first release the primary surface
  238. if (lpddsprimary!=NULL)
  239.    lpddsprimary->Release();
  240.        
  241. // release the directdraw object
  242. if (lpdd!=NULL)
  243.    lpdd->Release();
  244.  
  245. // return success
  246. return(1);
  247. } // end Game_Shutdown
  248.  
  249. ///////////////////////////////////////////////////////////
  250.  
  251. int Game_Main(void *parms)
  252. {
  253. // this is the workhorse of your game it will be called
  254. // continuously in real-time this is like main() in C
  255. // all the calls for you game go here!
  256.  
  257. // check of user is trying to exit
  258. if (KEY_DOWN(VK_ESCAPE) || KEY_DOWN(VK_SPACE))
  259.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  260.  
  261. // return success
  262. return(1);
  263. } // end Game_Main
  264.  
  265. ///////////////////////////////////////////////////////////
  266.  
  267.