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

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