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

  1. // PROG11_7.CPP - Windowed DirectDraw demo
  2. // works in any mode 8/16/24
  3.  
  4. // INCLUDES ///////////////////////////////////////////////
  5. #define WIN32_LEAN_AND_MEAN  
  6. #define INITGUID
  7.  
  8. #include <windows.h>   // include important windows stuff
  9. #include <windowsx.h> 
  10. #include <mmsystem.h>
  11. #include <iostream.h>  // include important C/C++ stuff
  12. #include <conio.h>
  13. #include <stdlib.h>
  14. #include <malloc.h>
  15. #include <memory.h>
  16. #include <string.h>
  17. #include <stdarg.h>
  18. #include <stdio.h>
  19. #include <math.h>
  20. #include <io.h>
  21. #include <fcntl.h>
  22.  
  23. #include <ddraw.h>    // directX includes
  24.  
  25. // DEFINES ////////////////////////////////////////////////
  26.  
  27. // defines for windows 
  28. #define WINDOW_CLASS_NAME "WINXCLASS"  // class name
  29.  
  30. #define WINDOW_WIDTH  320              // size of window
  31. #define WINDOW_HEIGHT 240
  32. #define SCREEN_WIDTH  640              // size of screen
  33. #define SCREEN_HEIGHT 480
  34. #define SCREEN_BPP    16               // bits per pixel
  35.  
  36. // MACROS /////////////////////////////////////////////////
  37.  
  38. // these read the keyboard asynchronously
  39. #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
  40. #define KEY_UP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
  41.  
  42. // this builds a 16 bit color value in 5.5.5 format (1-bit alpha mode)
  43. #define _RGB16BIT555(r,g,b) ((b & 31) + ((g & 31) << 5) + ((r & 31) << 10))
  44.  
  45. // this builds a 16 bit color value in 5.6.5 format (green dominate mode)
  46. #define _RGB16BIT565(r,g,b) ((b & 31) + ((g & 63) << 5) + ((r & 31) << 11))
  47.  
  48. // this builds a 24 bit color value in 8.8.8 format 
  49. #define _RGB24BIT(r,g,b) ((b) + ((g) << 8) + ((r) << 16) )
  50.  
  51. // this builds a 32 bit color value in A.8.8.8 format (8-bit alpha mode)
  52. // this is the most common format of all new video cards
  53. #define _RGB32BIT(a,r,g,b) ((b) + ((g) << 8) + ((r) << 16) + ((a) << 24))
  54.  
  55. // TYPES //////////////////////////////////////////////////
  56.  
  57. typedef unsigned short USHORT;
  58. typedef unsigned short WORD;
  59. typedef unsigned char  UCHAR;
  60. typedef unsigned char  BYTE;
  61.  
  62. // PROTOTYPES /////////////////////////////////////////////
  63.  
  64. int Game_Init(void *parms=NULL);
  65. int Game_Shutdown(void *parms=NULL);
  66. int Game_Main(void *parms=NULL);
  67.  
  68. // GLOBALS ////////////////////////////////////////////////
  69.  
  70. HWND main_window_handle = NULL; // save the window handle
  71. HINSTANCE main_instance = NULL; // save the instance
  72. char buffer[80];                // used to print text
  73.  
  74. LPDIRECTDRAW7        lpdd         = NULL;  // dd object
  75. LPDIRECTDRAWSURFACE7 lpddsprimary = NULL;  // dd primary surface
  76. LPDIRECTDRAWSURFACE7 lpddsback    = NULL;  // dd back surface
  77. LPDIRECTDRAWPALETTE  lpddpal      = NULL;  // a pointer to the created dd palette
  78. DDSURFACEDESC2       ddsd;                 // a direct draw surface description struct
  79. DDSCAPS2             ddscaps;              // a direct draw surface capabilities struct
  80. HRESULT              ddrval;               // result back from dd calls
  81.          
  82. // FUNCTIONS //////////////////////////////////////////////
  83.  
  84. LRESULT CALLBACK WindowProc(HWND hwnd, 
  85.                             UINT msg, 
  86.                             WPARAM wparam, 
  87.                             LPARAM lparam)
  88. {
  89. // this is the main message handler of the system
  90. PAINTSTRUCT    ps;           // used in WM_PAINT
  91. HDC            hdc;       // handle to a device context
  92.  
  93. // what is the message 
  94. switch(msg)
  95.     {    
  96.     case WM_CREATE: 
  97.         {
  98.         // do initialization stuff here
  99.         return(0);
  100.         } break;
  101.  
  102.     case WM_PAINT:
  103.          {
  104.          // start painting
  105.          hdc = BeginPaint(hwnd,&ps);
  106.  
  107.          // end painting
  108.          EndPaint(hwnd,&ps);
  109.          return(0);
  110.         } break;
  111.  
  112.     case WM_DESTROY: 
  113.         {
  114.         // kill the application            
  115.         PostQuitMessage(0);
  116.         return(0);
  117.         } break;
  118.  
  119.     default:break;
  120.  
  121.     } // end switch
  122.  
  123. // process any messages that we didn't take care of 
  124. return (DefWindowProc(hwnd, msg, wparam, lparam));
  125.  
  126. } // end WinProc
  127.  
  128. // WINMAIN ////////////////////////////////////////////////
  129.  
  130. int WINAPI WinMain(    HINSTANCE hinstance,
  131.                     HINSTANCE hprevinstance,
  132.                     LPSTR lpcmdline,
  133.                     int ncmdshow)
  134. {
  135.  
  136. WNDCLASS winclass;    // this will hold the class we create
  137. HWND     hwnd;        // generic window handle
  138. MSG         msg;        // generic message
  139. HDC      hdc;       // generic dc
  140. PAINTSTRUCT ps;     // generic paintstruct
  141.  
  142. // first fill in the window class stucture
  143. winclass.style            = CS_DBLCLKS | CS_OWNDC | 
  144.                           CS_HREDRAW | CS_VREDRAW;
  145. winclass.lpfnWndProc    = WindowProc;
  146. winclass.cbClsExtra        = 0;
  147. winclass.cbWndExtra        = 0;
  148. winclass.hInstance        = hinstance;
  149. winclass.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
  150. winclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  151. winclass.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
  152. winclass.lpszMenuName    = NULL; 
  153. winclass.lpszClassName    = WINDOW_CLASS_NAME;
  154.  
  155. // register the window class
  156. if (!RegisterClass(&winclass))
  157.     return(0);
  158.  
  159. // create the window, note the use of WS_POPUP
  160. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  161.                           "WinX Game Console",     // title
  162.                           WS_OVERLAPPED | WS_VISIBLE,
  163.                            0,0,       // x,y
  164.                           WINDOW_WIDTH,  // width
  165.                           WINDOW_HEIGHT, // height
  166.                           NULL,       // handle to parent 
  167.                           NULL,       // handle to menu
  168.                           hinstance,// instance
  169.                           NULL)))    // creation parms
  170. return(0);
  171.  
  172. // save the window handle and instance in a global
  173. main_window_handle = hwnd;
  174. main_instance      = hinstance;
  175.  
  176. // perform all game console specific initialization
  177. Game_Init();
  178.  
  179. // enter main event loop
  180. while(1)
  181.     {
  182.     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  183.         { 
  184.         // test if this is a quit
  185.         if (msg.message == WM_QUIT)
  186.            break;
  187.     
  188.         // translate any accelerator keys
  189.         TranslateMessage(&msg);
  190.  
  191.         // send the message to the window proc
  192.         DispatchMessage(&msg);
  193.         } // end if
  194.     
  195.     // main game processing goes here
  196.     Game_Main();
  197.  
  198.     } // end while
  199.  
  200. // shutdown game and release all resources
  201. Game_Shutdown();
  202.  
  203. // return to Windows like this
  204. return(msg.wParam);
  205.  
  206. } // end WinMain
  207.  
  208. // WINX GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////
  209.  
  210. int Game_Init(void *parms)
  211. {
  212. // this function is where you do all the initialization 
  213. // for your game
  214.  
  215. // create object and test for error
  216. if (DirectDrawCreateEx(NULL, (void **)&lpdd, IID_IDirectDraw7, NULL)!=DD_OK)
  217.    return(0);
  218.  
  219. // set cooperation level to windowed mode normal
  220. if (lpdd->SetCooperativeLevel(main_window_handle, DDSCL_NORMAL)!=DD_OK)
  221.     return(0);
  222.  
  223. // Create the primary surface
  224. memset(&ddsd,0,sizeof(ddsd));
  225. ddsd.dwSize         = sizeof(ddsd);
  226. ddsd.dwFlags        = DDSD_CAPS;
  227. ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
  228.  
  229. if (lpdd->CreateSurface(&ddsd,&lpddsprimary,NULL)!=DD_OK)
  230.    return(0);
  231.  
  232. // return success
  233. return(1);
  234.  
  235. } // end Game_Init
  236.  
  237. ///////////////////////////////////////////////////////////
  238.  
  239. int Game_Shutdown(void *parms)
  240. {
  241. // this function is where you shutdown your game and
  242. // release all resources that you allocated
  243.  
  244. // first release the primary surface
  245. if (lpddsprimary!=NULL)
  246.    lpddsprimary->Release();
  247.        
  248. // release the directdraw object
  249. if (lpdd!=NULL)
  250.    lpdd->Release();
  251.  
  252. // return success
  253. return(1);
  254. } // end Game_Shutdown
  255.  
  256. ///////////////////////////////////////////////////////////
  257.  
  258. int Game_Main(void *parms)
  259. {
  260. // this is the workhorse of your game it will be called
  261. // continuously in real-time this is like main() in C
  262. // all the calls for you game go here!
  263.  
  264. UCHAR *primary_buffer = NULL; // used to draw
  265. DDPIXELFORMAT ddpixelformat;  // used to determine bits per pixel
  266. RECT client;                  // the client direct draw rectangle
  267.      
  268. // check of user is trying to exit
  269. if (KEY_DOWN(VK_ESCAPE) || KEY_DOWN(VK_SPACE))
  270.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  271.  
  272. // get client rectangle first
  273. GetWindowRect(main_window_handle, &client);
  274.  
  275. // copy the double buffer into the primary buffer
  276. memset(&ddsd,0,sizeof(ddsd)); 
  277. ddsd.dwSize = sizeof(ddsd);
  278.  
  279. // lock the primary surface
  280. lpddsprimary->Lock(NULL,&ddsd, 
  281.               DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,NULL);
  282.  
  283. // get video pointer to primary surfce
  284. primary_buffer = (UCHAR *)ddsd.lpSurface;       
  285.  
  286. // test if this is a 8 bit or 16 bit video mode
  287. memset(&ddpixelformat,0, sizeof(ddpixelformat));
  288. ddpixelformat.dwSize = sizeof(ddpixelformat);
  289.  
  290. // get the pixel format
  291. lpddsprimary->GetPixelFormat(&ddpixelformat);
  292.  
  293. // perform test to see what mode, 8/16/32
  294. if (ddpixelformat.dwRGBBitCount==32)
  295.     {
  296.     // draw 10 random pixels in 32 bit mode
  297.     for (int index=0; index < 10; index++)
  298.         {
  299.         int x=rand()%(client.right - client.left) + client.left;
  300.         int y=rand()%(client.bottom - client.top) + client.top;
  301.         DWORD color = _RGB32BIT(0,rand()%256, rand()%256, rand()%256);
  302.         *((DWORD *)(primary_buffer + x*4 + y*ddsd.lPitch)) = color;
  303.         } // end for index
  304.     } // end if 32 bit
  305. if (ddpixelformat.dwRGBBitCount==24)
  306.     {
  307.     // draw 10 random pixels in 24 bit mode
  308.     for (int index=0; index < 10; index++)
  309.         {
  310.         int x=rand()%(client.right - client.left) + client.left;
  311.         int y=rand()%(client.bottom - client.top) + client.top;
  312.         UCHAR red   = rand()%256;
  313.         UCHAR green = rand()%256;
  314.         UCHAR blue  = rand()%256;
  315.         
  316.         // write date a byte at a time
  317.         *(primary_buffer + x*3 + y*ddsd.lPitch + 0) = red;
  318.         *(primary_buffer + x*3 + y*ddsd.lPitch + 1) = green;
  319.         *(primary_buffer + x*3 + y*ddsd.lPitch + 2) = blue;
  320.  
  321.         } // end for index
  322.     } // end if 32 bit
  323. if (ddpixelformat.dwRGBBitCount==16)
  324.     {
  325.     // draw 10 random pixels in 16 bit mode
  326.     for (int index=0; index<10; index++)
  327.         {
  328.         int x=rand()%(client.right - client.left) + client.left;
  329.         int y=rand()%(client.bottom - client.top) + client.top;
  330.         USHORT color = _RGB16BIT565(rand()%256, rand()%256, rand()%256);
  331.         *((USHORT *)(primary_buffer + x*2 + y*ddsd.lPitch)) = color;
  332.         } // end for index
  333.     } // end if 16 bit
  334. else
  335.     {// assume 8 bits per pixel
  336.     // draw 10 random pixels in 8 bit mode
  337.     for (int index=0; index<10; index++)
  338.         {
  339.         int x=rand()%(client.right - client.left) + client.left;
  340.         int y=rand()%(client.bottom - client.top) + client.top;
  341.         UCHAR color = rand()%256;
  342.         primary_buffer[x + y*ddsd.lPitch] = color;
  343.         } // end for index
  344.     } // end else
  345.  
  346. // unlock primary buffer
  347. lpddsprimary->Unlock(NULL);
  348.  
  349. // wait a sec
  350. Sleep(30);
  351.  
  352. // return success
  353. return(1);
  354. } // end Game_Main
  355.  
  356. ///////////////////////////////////////////////////////////
  357.  
  358.