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

  1. // PROG9_3.CPP - a demo of 640x480x16 bit color rendering
  2. // 16-bit version
  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  640              // size of window
  31. #define WINDOW_HEIGHT 480
  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. // TYPES //////////////////////////////////////////////////
  49.  
  50. typedef unsigned short USHORT;
  51. typedef unsigned short WORD;
  52. typedef unsigned char  UCHAR;
  53. typedef unsigned char  BYTE;
  54.  
  55. // PROTOTYPES /////////////////////////////////////////////
  56.  
  57. int Game_Init(void *parms=NULL);
  58. int Game_Shutdown(void *parms=NULL);
  59. int Game_Main(void *parms=NULL);
  60.  
  61. // GLOBALS ////////////////////////////////////////////////
  62.  
  63. HWND main_window_handle = NULL; // save the window handle
  64. HINSTANCE main_instance = NULL; // save the instance
  65. char buffer[80];                // used to print text
  66.  
  67. LPDIRECTDRAW7        lpdd         = NULL;  // dd object
  68. LPDIRECTDRAWSURFACE7 lpddsprimary = NULL;  // dd primary surface
  69. LPDIRECTDRAWSURFACE7 lpddsback    = NULL;  // dd back surface
  70. LPDIRECTDRAWPALETTE  lpddpal      = NULL;  // a pointer to the created dd palette
  71. DDSURFACEDESC2       ddsd;                 // a direct draw surface description struct
  72. DDSCAPS2             ddscaps;              // a direct draw surface capabilities struct
  73. HRESULT              ddrval;               // result back from dd calls
  74.  
  75. // FUNCTIONS //////////////////////////////////////////////
  76.  
  77. LRESULT CALLBACK WindowProc(HWND hwnd, 
  78.                             UINT msg, 
  79.                             WPARAM wparam, 
  80.                             LPARAM lparam)
  81. {
  82. // this is the main message handler of the system
  83. PAINTSTRUCT    ps;           // used in WM_PAINT
  84. HDC            hdc;       // handle to a device context
  85.  
  86. // what is the message 
  87. switch(msg)
  88.     {    
  89.     case WM_CREATE: 
  90.         {
  91.         // do initialization stuff here
  92.         return(0);
  93.         } break;
  94.  
  95.     case WM_PAINT:
  96.          {
  97.          // start painting
  98.          hdc = BeginPaint(hwnd,&ps);
  99.  
  100.          // end painting
  101.          EndPaint(hwnd,&ps);
  102.          return(0);
  103.         } break;
  104.  
  105.     case WM_DESTROY: 
  106.         {
  107.         // kill the application            
  108.         PostQuitMessage(0);
  109.         return(0);
  110.         } break;
  111.  
  112.     default:break;
  113.  
  114.     } // end switch
  115.  
  116. // process any messages that we didn't take care of 
  117. return (DefWindowProc(hwnd, msg, wparam, lparam));
  118.  
  119. } // end WinProc
  120.  
  121. // WINMAIN ////////////////////////////////////////////////
  122.  
  123. int WINAPI WinMain(    HINSTANCE hinstance,
  124.                     HINSTANCE hprevinstance,
  125.                     LPSTR lpcmdline,
  126.                     int ncmdshow)
  127. {
  128.  
  129. WNDCLASS winclass;    // this will hold the class we create
  130. HWND     hwnd;        // generic window handle
  131. MSG         msg;        // generic message
  132. HDC      hdc;       // generic dc
  133. PAINTSTRUCT ps;     // generic paintstruct
  134.  
  135. // first fill in the window class stucture
  136. winclass.style            = CS_DBLCLKS | CS_OWNDC | 
  137.                           CS_HREDRAW | CS_VREDRAW;
  138. winclass.lpfnWndProc    = WindowProc;
  139. winclass.cbClsExtra        = 0;
  140. winclass.cbWndExtra        = 0;
  141. winclass.hInstance        = hinstance;
  142. winclass.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
  143. winclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  144. winclass.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
  145. winclass.lpszMenuName    = NULL; 
  146. winclass.lpszClassName    = WINDOW_CLASS_NAME;
  147.  
  148. // register the window class
  149. if (!RegisterClass(&winclass))
  150.     return(0);
  151.  
  152. // create the window, note the use of WS_POPUP
  153. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  154.                           "WinX Game Console",     // title
  155.                           WS_POPUP | WS_VISIBLE,
  156.                            0,0,       // x,y
  157.                           WINDOW_WIDTH,  // width
  158.                           WINDOW_HEIGHT, // height
  159.                           NULL,       // handle to parent 
  160.                           NULL,       // handle to menu
  161.                           hinstance,// instance
  162.                           NULL)))    // creation parms
  163. return(0);
  164.  
  165. // save the window handle and instance in a global
  166. main_window_handle = hwnd;
  167. main_instance      = hinstance;
  168.  
  169. // perform all game console specific initialization
  170. Game_Init();
  171.  
  172. // enter main event loop
  173. while(1)
  174.     {
  175.     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  176.         { 
  177.         // test if this is a quit
  178.         if (msg.message == WM_QUIT)
  179.            break;
  180.     
  181.         // translate any accelerator keys
  182.         TranslateMessage(&msg);
  183.  
  184.         // send the message to the window proc
  185.         DispatchMessage(&msg);
  186.         } // end if
  187.     
  188.     // main game processing goes here
  189.     Game_Main();
  190.  
  191.     } // end while
  192.  
  193. // shutdown game and release all resources
  194. Game_Shutdown();
  195.  
  196. // return to Windows like this
  197. return(msg.wParam);
  198.  
  199. } // end WinMain
  200.  
  201. // WINX GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////
  202.  
  203. int Game_Init(void *parms)
  204. {
  205. // this function is where you do all the initialization 
  206. // for your game
  207.  
  208. // create object and test for error
  209. if (DirectDrawCreateEx(NULL, (void **)&lpdd, IID_IDirectDraw7, NULL)!=DD_OK)
  210.    return(0);
  211.  
  212. // set cooperation level to windowed mode normal
  213. if (lpdd->SetCooperativeLevel(main_window_handle,
  214.            DDSCL_ALLOWMODEX | DDSCL_FULLSCREEN | 
  215.            DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT)!=DD_OK)
  216.     return(0);
  217.  
  218. // set the display mode
  219. if (lpdd->SetDisplayMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,0,0)!=DD_OK)
  220.    return(0);
  221.  
  222. // Create the primary surface
  223. memset(&ddsd,0,sizeof(ddsd));
  224. ddsd.dwSize         = sizeof(ddsd);
  225. ddsd.dwFlags        = DDSD_CAPS;
  226. ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
  227.  
  228. if (lpdd->CreateSurface(&ddsd,&lpddsprimary,NULL)!=DD_OK)
  229.    return(0);
  230.  
  231. // return success
  232. return(1);
  233. } // end Game_Init
  234.  
  235. ///////////////////////////////////////////////////////////
  236.  
  237. int Game_Shutdown(void *parms)
  238. {
  239. // this function is where you shutdown your game and
  240. // release all resources that you allocated
  241.  
  242. // first release the primary surface
  243. if (lpddsprimary!=NULL)
  244.    lpddsprimary->Release();
  245.        
  246. // release the directdraw object
  247. if (lpdd!=NULL)
  248.    lpdd->Release();
  249.  
  250. // return success
  251. return(1);
  252. } // end Game_Shutdown
  253.  
  254. ///////////////////////////////////////////////////////////
  255.  
  256. int Game_Main(void *parms)
  257. {
  258. // this is the workhorse of your game it will be called
  259. // continuously in real-time this is like main() in C
  260. // all the calls for you game go here!
  261.  
  262. // this will be aliased to the video memory
  263. USHORT *video_buffer = NULL; 
  264.  
  265. // this will hold the number of 16 bit words per video line
  266. int words_per_line = 0;      
  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. // draw into the primary buffer
  273.  
  274. // set up the surface description to lock the surface
  275. memset(&ddsd,0,sizeof(ddsd)); 
  276. ddsd.dwSize = sizeof(ddsd);
  277.  
  278. // lock the primary surface, note in a real game you would
  279. // do this once per frame
  280. lpddsprimary->Lock(NULL,&ddsd, 
  281.              DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,NULL);
  282.  
  283. // get video pointer, we could have just as easily used
  284. // ddsd.lpSurface, but I like to alias the ptr
  285. video_buffer = (USHORT *)ddsd.lpSurface;
  286.  
  287. // compute words per line
  288. words_per_line = (ddsd.lPitch >> 1);
  289.  
  290. //.. use video pointer to write to memory
  291. // notice the use of lPitch (linear pitch)
  292.  
  293. // draw 100 pixels
  294. for (int index=0; index<100; index++)
  295.     {
  296.     // get random x,y
  297.     int x = rand()%SCREEN_WIDTH;
  298.     int y = rand()%SCREEN_HEIGHT;
  299.     
  300.     // get random color
  301.     UCHAR red   = rand()%256;
  302.     UCHAR green = rand()%256;
  303.     UCHAR blue  = rand()%256;
  304.  
  305.     // plot the pixel with a random color
  306.     
  307.     video_buffer[x + (y*words_per_line)] = _RGB16BIT565(red,green,blue);
  308.  
  309.     } // end for index
  310.  
  311. // unlock the surface, very important!
  312. lpddsprimary->Unlock(NULL);
  313.  
  314. // return success
  315. return(1);
  316. } // end Game_Main
  317.  
  318. ///////////////////////////////////////////////////////////
  319.  
  320.