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

  1. // PROG11_1_16b.CPP - a demo of blitter objects (BOBS) 
  2. // creates an asteroid field
  3. // 16-bit version bob functions
  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. #define BITMAP_ID     0x4D42      // universal id for a bitmap
  38.  
  39. // defines for BOBs
  40. #define BOB_STATE_DEAD     0      // this is a dead bob
  41. #define BOB_STATE_ALIVE    1      // this is a live bob
  42. #define BOB_STATE_LOADED   2      // the bob has been loaded
  43.  
  44. // defines for asteroids
  45. #define NUM_ASTEROIDS      64     // number of active asteroids
  46.  
  47. // MACROS /////////////////////////////////////////////////
  48.  
  49. // these read the keyboard asynchronously
  50. #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
  51. #define KEY_UP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
  52.  
  53. // this builds a 16 bit color value in 5.5.5 format (1-bit alpha mode)
  54. #define _RGB16BIT555(r,g,b) ((b & 31) + ((g & 31) << 5) + ((r & 31) << 10))
  55.  
  56. // this builds a 16 bit color value in 5.6.5 format (green dominate mode)
  57. #define _RGB16BIT565(r,g,b) ((b & 31) + ((g & 63) << 5) + ((r & 31) << 11))
  58.  
  59. // TYPES //////////////////////////////////////////////////
  60.  
  61. typedef unsigned short USHORT;
  62. typedef unsigned short WORD;
  63. typedef unsigned char  UCHAR;
  64. typedef unsigned char  BYTE;
  65.  
  66. // container structure for bitmaps .BMP file
  67. typedef struct BITMAP_FILE_TAG
  68.         {
  69.         BITMAPFILEHEADER bitmapfileheader;  // this contains the bitmapfile header
  70.         BITMAPINFOHEADER bitmapinfoheader;  // this is all the info including the palette
  71.         PALETTEENTRY     palette[256];      // we will store the palette here
  72.         UCHAR            *buffer;           // this is a pointer to the data
  73.  
  74.         } BITMAP_FILE, *BITMAP_FILE_PTR;
  75.  
  76. // the blitter object structure BOB
  77. typedef struct BITMAP_OBJ_TYP
  78.         {
  79.         int state; // the state of the object (general)
  80.         int attr;  // attributes pertaining to the object (general)
  81.         int x,y;   // position bitmap will be displayed at
  82.         int xv,yv; // velocity of object
  83.         int width, height; // the width and height of the bitmap
  84.         LPDIRECTDRAWSURFACE7 image; // the bitmap surface itself
  85.  
  86.         } BITMAP_OBJ, *BITMAP_OBJ_PTR;
  87.  
  88. // PROTOTYPES /////////////////////////////////////////////
  89.  
  90. // game console
  91. int Game_Init(void *parms=NULL);
  92. int Game_Shutdown(void *parms=NULL);
  93. int Game_Main(void *parms=NULL);
  94.  
  95. // bitmaps
  96. int Load_Bitmap_File(BITMAP_FILE_PTR bitmap, char *filename);
  97. int Unload_Bitmap_File(BITMAP_FILE_PTR bitmap);
  98. int Flip_Bitmap(UCHAR *image, int bytes_per_line, int height);
  99.  
  100. // bobs
  101. int Draw_BOB16(BITMAP_OBJ_PTR bob,LPDIRECTDRAWSURFACE7 dest);
  102. int Destroy_BOB16(BITMAP_OBJ_PTR bob);
  103. int Create_BOB16(BITMAP_OBJ_PTR bob,int width, int height,int attr, int flags);
  104. int Load_BOB16(BITMAP_OBJ_PTR bob,BITMAP_FILE_PTR bitmap,int cx,int cy,int mode); 
  105.  
  106. // GLOBALS ////////////////////////////////////////////////
  107.  
  108. HWND main_window_handle = NULL; // save the window handle
  109. HINSTANCE main_instance = NULL; // save the instance
  110. char buffer[80];                // used to print text
  111.  
  112. LPDIRECTDRAW7        lpdd         = NULL;  // dd object
  113. LPDIRECTDRAWSURFACE7 lpddsprimary = NULL;  // dd primary surface
  114. LPDIRECTDRAWSURFACE7 lpddsback    = NULL;  // dd back surface
  115. LPDIRECTDRAWPALETTE  lpddpal      = NULL;  // a pointer to the created dd palette
  116. PALETTEENTRY         palette[256];         // color palette
  117. DDSURFACEDESC2       ddsd;                 // a direct draw surface description struct
  118. DDBLTFX              ddbltfx;              // used to fill
  119. DDSCAPS2             ddscaps;              // a direct draw surface capabilities struct
  120. HRESULT              ddrval;               // result back from dd calls
  121. UCHAR                *primary_buffer = NULL; // primary video buffer
  122. UCHAR                *back_buffer    = NULL; // secondary back buffer
  123. BITMAP_FILE          bitmap16bit,            // a 16 bit bitmap file
  124.                      bitmap8bit;             // a 8 bit bitmap file
  125.  
  126. BITMAP_OBJ           asteroids[NUM_ASTEROIDS]; // asteroid field
  127.  
  128. // FUNCTIONS //////////////////////////////////////////////
  129.  
  130. int Load_Bitmap_File(BITMAP_FILE_PTR bitmap, char *filename)
  131. {
  132. // this function opens a bitmap file and loads the data into bitmap
  133.  
  134. int file_handle,  // the file handle
  135.     index;        // looping index
  136.  
  137. UCHAR *temp_buffer = NULL; // used to convert 24 bit images to 16 bit
  138. OFSTRUCT file_data;        // the file data information
  139.  
  140. // open the file if it exists
  141. if ((file_handle = OpenFile(filename,&file_data,OF_READ))==-1)
  142.    return(0);
  143.  
  144. // now load the bitmap file header
  145. _lread(file_handle, &bitmap->bitmapfileheader,sizeof(BITMAPFILEHEADER));
  146.  
  147. // test if this is a bitmap file
  148. if (bitmap->bitmapfileheader.bfType!=BITMAP_ID)
  149.    {
  150.    // close the file
  151.    _lclose(file_handle);
  152.  
  153.    // return error
  154.    return(0);
  155.    } // end if
  156.  
  157. // now we know this is a bitmap, so read in all the sections
  158.  
  159. // first the bitmap infoheader
  160.  
  161. // now load the bitmap file header
  162. _lread(file_handle, &bitmap->bitmapinfoheader,sizeof(BITMAPINFOHEADER));
  163.  
  164. // now load the color palette if there is one
  165. if (bitmap->bitmapinfoheader.biBitCount == 8)
  166.    {
  167.    _lread(file_handle, &bitmap->palette,256*sizeof(PALETTEENTRY));
  168.  
  169.    // now set all the flags in the palette correctly and fix the reversed 
  170.    // BGR RGBQUAD data format
  171.    for (index=0; index < 256; index++)
  172.        {
  173.        // reverse the red and green fields
  174.        int temp_color = bitmap->palette[index].peRed;
  175.        bitmap->palette[index].peRed  = bitmap->palette[index].peBlue;
  176.        bitmap->palette[index].peBlue = temp_color;
  177.        
  178.        // always set the flags word to this
  179.        bitmap->palette[index].peFlags = PC_NOCOLLAPSE;
  180.        } // end for index
  181.  
  182.     } // end if
  183.  
  184. // finally the image data itself
  185. _lseek(file_handle,-(int)(bitmap->bitmapinfoheader.biSizeImage),SEEK_END);
  186.  
  187. // now read in the image, if the image is 8 or 16 bit then simply read it
  188. // but if its 24 bit then read it into a temporary area and then convert
  189. // it to a 16 bit image
  190.  
  191. if (bitmap->bitmapinfoheader.biBitCount==8 || bitmap->bitmapinfoheader.biBitCount==16)
  192.    {
  193.    // allocate the memory for the image
  194.    if (!(bitmap->buffer = (UCHAR *)malloc(bitmap->bitmapinfoheader.biSizeImage)))
  195.       {
  196.       // close the file
  197.       _lclose(file_handle);
  198.  
  199.       // return error
  200.       return(0);
  201.       } // end if
  202.  
  203.    // now read it in
  204.    _lread(file_handle,bitmap->buffer,bitmap->bitmapinfoheader.biSizeImage);
  205.  
  206.    } // end if
  207. else
  208.    {
  209.    // this must be a 24 bit image, load it in and convert it to 16 bit
  210. //   printf("\nconverting 24 bit image...");
  211.  
  212.    // allocate temporary buffer
  213.    if (!(temp_buffer = (UCHAR *)malloc(bitmap->bitmapinfoheader.biSizeImage)))
  214.       {
  215.       // close the file
  216.       _lclose(file_handle);
  217.  
  218.       // return error
  219.       return(0);
  220.       } // end if
  221.    
  222.    // allocate final 16 bit storage buffer
  223.    if (!(bitmap->buffer=(UCHAR *)malloc(2*bitmap->bitmapinfoheader.biWidth*bitmap->bitmapinfoheader.biHeight)))
  224.       {
  225.       // close the file
  226.       _lclose(file_handle);
  227.  
  228.       // release working buffer
  229.       free(temp_buffer);
  230.  
  231.       // return error
  232.       return(0);
  233.       } // end if
  234.  
  235.    // now read it in
  236.    _lread(file_handle,temp_buffer,bitmap->bitmapinfoheader.biSizeImage);
  237.  
  238.    // now convert each 24 bit RGB value into a 16 bit value
  239.    for (index=0; index<bitmap->bitmapinfoheader.biWidth*bitmap->bitmapinfoheader.biHeight; index++)
  240.        {
  241.        // extract RGB components (note they are in memory BGR)
  242.        // also, assume 5.6.5 format, so scale appropriately
  243.        UCHAR red    = (temp_buffer[index*3 + 2] >> 3), // 5 bits
  244.              green  = (temp_buffer[index*3 + 1] >> 2), // 6 bits, change to 3 for 5 bits
  245.              blue   = (temp_buffer[index*3 + 0] >> 3); // 5 bits
  246.  
  247.        // build up 16 bit color word assume 5.6.5 format
  248.        USHORT color = _RGB16BIT565(red,green,blue);
  249.  
  250.        // write color to buffer
  251.        ((USHORT *)bitmap->buffer)[index] = color;
  252.  
  253.        } // end for index
  254.  
  255.    // finally write out the correct number of bits
  256.    bitmap->bitmapinfoheader.biBitCount=16;
  257.  
  258.    } // end if
  259.  
  260. #if 0
  261. // write the file info out 
  262. printf("\nfilename:%s \nsize=%d \nwidth=%d \nheight=%d \nbitsperpixel=%d \ncolors=%d \nimpcolors=%d",
  263.         filename,
  264.         bitmap->bitmapinfoheader.biSizeImage,
  265.         bitmap->bitmapinfoheader.biWidth,
  266.         bitmap->bitmapinfoheader.biHeight,
  267.         bitmap->bitmapinfoheader.biBitCount,
  268.         bitmap->bitmapinfoheader.biClrUsed,
  269.         bitmap->bitmapinfoheader.biClrImportant);
  270. #endif
  271.  
  272. // close the file
  273. _lclose(file_handle);
  274.  
  275. // flip the bitmap
  276. Flip_Bitmap(bitmap->buffer, 
  277.             bitmap->bitmapinfoheader.biWidth*(bitmap->bitmapinfoheader.biBitCount/8), 
  278.             bitmap->bitmapinfoheader.biHeight);
  279.  
  280. // return success
  281. return(1);
  282.  
  283. } // end Load_Bitmap_File
  284.  
  285. ///////////////////////////////////////////////////////////
  286.  
  287. int Unload_Bitmap_File(BITMAP_FILE_PTR bitmap)
  288. {
  289. // this function releases all memory associated with "bitmap"
  290. if (bitmap->buffer)
  291.    {
  292.    // release memory
  293.    free(bitmap->buffer);
  294.  
  295.    // reset pointer
  296.    bitmap->buffer = NULL;
  297.  
  298.    } // end if
  299.  
  300. // return success
  301. return(1);
  302.  
  303. } // end Unload_Bitmap_File
  304.  
  305. ///////////////////////////////////////////////////////////
  306.  
  307. int Flip_Bitmap(UCHAR *image, int bytes_per_line, int height)
  308. {
  309. // this function is used to flip upside down .BMP images
  310.  
  311. UCHAR *buffer; // used to perform the image processing
  312. int index;     // looping index
  313.  
  314. // allocate the temporary buffer
  315. if (!(buffer = (UCHAR *)malloc(bytes_per_line*height)))
  316.    return(0);
  317.  
  318. // copy image to work area
  319. memcpy(buffer,image,bytes_per_line*height);
  320.  
  321. // flip vertically
  322. for (index=0; index < height; index++)
  323.     memcpy(&image[((height-1) - index)*bytes_per_line],
  324.            &buffer[index*bytes_per_line], bytes_per_line);
  325.  
  326. // release the memory
  327. free(buffer);
  328.  
  329. // return success
  330. return(1);
  331.  
  332. } // end Flip_Bitmap
  333.  
  334. ///////////////////////////////////////////////////////////
  335.  
  336. int Create_BOB16(BITMAP_OBJ_PTR bob,    // the bob to create
  337.                  int width, int height, // size of bob
  338.                  int attr,              // attrs
  339.                  int flags = 0)         // memory flag
  340. {
  341. // Create the BOB object, note that all BOBs 
  342. // are created as offscreen surfaces in VRAM as the
  343. // default, if you want to use system memory then
  344. // set flags equal to DDSCAPS_SYSTEMMEMORY
  345.  
  346. DDSURFACEDESC2 ddsd; // used to create surface
  347.  
  348. // set state and attributes of BOB
  349. bob->state = BOB_STATE_ALIVE;
  350. bob->attr  = attr;
  351. bob->image = NULL;
  352.  
  353. // set position and velocity to 0
  354. bob->x = bob->y = bob->xv = bob->yv = 0;
  355.  
  356. // set to access caps, width, and height
  357. memset(&ddsd,0,sizeof(ddsd));
  358. ddsd.dwSize  = sizeof(ddsd);
  359. ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
  360.  
  361. // set dimensions of the new bitmap surface
  362. ddsd.dwWidth  = bob->width = width;
  363. ddsd.dwHeight = bob->height = height;
  364.  
  365. // set surface to offscreen plain
  366. ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | flags;
  367.  
  368. // create the surface
  369. if (lpdd->CreateSurface(&ddsd,&(bob->image),NULL)!=DD_OK)
  370.    return(0);
  371.  
  372. // set color key to color to RGB(0,0,0)
  373. DDCOLORKEY color_key; // used to set color key
  374. color_key.dwColorSpaceLowValue  = _RGB16BIT565(0,0,0);
  375. color_key.dwColorSpaceHighValue = _RGB16BIT565(0,0,0);
  376.  
  377. // now set the color key for source blitting
  378. (bob->image)->SetColorKey(DDCKEY_SRCBLT, &color_key);
  379.  
  380. // return success
  381. return(1);
  382.  
  383. } // end Create_BOB16
  384.  
  385. ///////////////////////////////////////////////////////////
  386.  
  387. int Destroy_BOB16(BITMAP_OBJ_PTR bob)
  388. {
  389. // destroy the BOB, simply release the surface
  390.  
  391. if (bob->image)
  392.    (bob->image)->Release();
  393. else
  394.    return(0);
  395.  
  396. // return success
  397. return(1);
  398.  
  399. } // end Destroy_BOB16
  400.  
  401. ///////////////////////////////////////////////////////////
  402.  
  403. int Draw_BOB16(BITMAP_OBJ_PTR bob,       // bob to draw
  404.                LPDIRECTDRAWSURFACE7 dest) // surface to draw the bob on
  405. {
  406. // draw a bob at the x,y defined in the BOB
  407. // on the destination surface defined in dest
  408.  
  409. RECT dest_rect,   // the destination rectangle
  410.      source_rect; // the source rectangle                             
  411.  
  412. // fill in the destination rect
  413. dest_rect.left   = bob->x;
  414. dest_rect.top    = bob->y;
  415. dest_rect.right  = bob->x+bob->width;
  416. dest_rect.bottom = bob->y+bob->height;
  417.  
  418. // fill in the source rect
  419. source_rect.left    = 0;
  420. source_rect.top     = 0;
  421. source_rect.right   = bob->width;
  422. source_rect.bottom  = bob->height;
  423.  
  424. // blt to destination surface
  425. dest->Blt(&dest_rect, bob->image,
  426.           &source_rect,(DDBLT_WAIT | DDBLT_KEYSRC),
  427.           NULL);
  428.  
  429. // return success
  430. return(1);
  431. } // end Draw_BOB16
  432.  
  433. ///////////////////////////////////////////////////////////
  434.  
  435. int Load_BOB16(BITMAP_OBJ_PTR bob, // bob to load with data
  436.              BITMAP_FILE_PTR bitmap, // bitmap to scan image data from
  437.              int cx,int cy,   // cell or absolute pos. to scan image from
  438.              int mode)        // if 0 then cx,cy is cell position, else 
  439.                               // cx,cy are absolute coords
  440. {
  441. // this function extracts a bitmap out of a bitmap file
  442.  
  443. USHORT *source_ptr,   // working pointers
  444.        *dest_ptr;
  445.  
  446. DDSURFACEDESC2 ddsd;  //  direct draw surface description 
  447.  
  448. // test the mode of extraction, cell based or absolute
  449. if (mode==0)
  450.    {
  451.    // re-compute x,y
  452.    cx = cx*(bob->width+1) + 1;
  453.    cy = cy*(bob->height+1) + 1;
  454.    } // end if
  455.  
  456. // extract bitmap data
  457. source_ptr = (USHORT *)bitmap->buffer + cy*bitmap->bitmapinfoheader.biWidth+cx;
  458.  
  459. // get the addr to destination surface memory
  460.  
  461. // set size of the structure
  462. ddsd.dwSize = sizeof(ddsd);
  463.  
  464. // lock the display surface
  465. (bob->image)->Lock(NULL,
  466.                    &ddsd,
  467.                    DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR,
  468.                    NULL);
  469.  
  470. // assign a pointer to the memory surface for manipulation
  471. dest_ptr = (USHORT *)ddsd.lpSurface;
  472.  
  473. // iterate thru each scanline and copy bitmap
  474. for (int index_y=0; index_y<bob->height; index_y++)
  475.     {
  476.     // copy next line of data to destination
  477.     memcpy(dest_ptr, source_ptr,(bob->width*2));
  478.  
  479.     // advance pointers
  480.     dest_ptr   += (ddsd.lPitch/2);
  481.     source_ptr += bitmap->bitmapinfoheader.biWidth;
  482.     } // end for index_y
  483.  
  484. // unlock the surface 
  485. (bob->image)->Unlock(NULL);
  486.  
  487. // set state to loaded
  488. bob->state |= BOB_STATE_LOADED;
  489.  
  490. // return success
  491. return(1);
  492.  
  493. } // end Load_BOB16
  494.  
  495. ///////////////////////////////////////////////////////////
  496.  
  497. LRESULT CALLBACK WindowProc(HWND hwnd, 
  498.                             UINT msg, 
  499.                             WPARAM wparam, 
  500.                             LPARAM lparam)
  501. {
  502. // this is the main message handler of the system
  503. PAINTSTRUCT    ps;           // used in WM_PAINT
  504. HDC            hdc;       // handle to a device context
  505.  
  506. // what is the message 
  507. switch(msg)
  508.     {    
  509.     case WM_CREATE: 
  510.         {
  511.         // do initialization stuff here
  512.         return(0);
  513.         } break;
  514.  
  515.     case WM_PAINT:
  516.          {
  517.          // start painting
  518.          hdc = BeginPaint(hwnd,&ps);
  519.  
  520.          // end painting
  521.          EndPaint(hwnd,&ps);
  522.          return(0);
  523.         } break;
  524.  
  525.     case WM_DESTROY: 
  526.         {
  527.         // kill the application            
  528.         PostQuitMessage(0);
  529.         return(0);
  530.         } break;
  531.  
  532.     default:break;
  533.  
  534.     } // end switch
  535.  
  536. // process any messages that we didn't take care of 
  537. return (DefWindowProc(hwnd, msg, wparam, lparam));
  538.  
  539. } // end WinProc
  540.  
  541. // WINMAIN ////////////////////////////////////////////////
  542.  
  543. int WINAPI WinMain(    HINSTANCE hinstance,
  544.                     HINSTANCE hprevinstance,
  545.                     LPSTR lpcmdline,
  546.                     int ncmdshow)
  547. {
  548.  
  549. WNDCLASS winclass;    // this will hold the class we create
  550. HWND     hwnd;        // generic window handle
  551. MSG         msg;        // generic message
  552. HDC      hdc;       // generic dc
  553. PAINTSTRUCT ps;     // generic paintstruct
  554.  
  555. // first fill in the window class stucture
  556. winclass.style            = CS_DBLCLKS | CS_OWNDC | 
  557.                           CS_HREDRAW | CS_VREDRAW;
  558. winclass.lpfnWndProc    = WindowProc;
  559. winclass.cbClsExtra        = 0;
  560. winclass.cbWndExtra        = 0;
  561. winclass.hInstance        = hinstance;
  562. winclass.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
  563. winclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  564. winclass.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
  565. winclass.lpszMenuName    = NULL; 
  566. winclass.lpszClassName    = WINDOW_CLASS_NAME;
  567.  
  568. // register the window class
  569. if (!RegisterClass(&winclass))
  570.     return(0);
  571.  
  572. // create the window, note the use of WS_POPUP
  573. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  574.                           "WinX Game Console",     // title
  575.                           WS_POPUP | WS_VISIBLE,
  576.                            0,0,       // x,y
  577.                           WINDOW_WIDTH,  // width
  578.                           WINDOW_HEIGHT, // height
  579.                           NULL,       // handle to parent 
  580.                           NULL,       // handle to menu
  581.                           hinstance,// instance
  582.                           NULL)))    // creation parms
  583. return(0);
  584.  
  585. // hide the mouse
  586. ShowCursor(FALSE);
  587.  
  588. // save the window handle and instance in a global
  589. main_window_handle = hwnd;
  590. main_instance      = hinstance;
  591.  
  592. // perform all game console specific initialization
  593. Game_Init();
  594.  
  595. // enter main event loop
  596. while(1)
  597.     {
  598.     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  599.         { 
  600.         // test if this is a quit
  601.         if (msg.message == WM_QUIT)
  602.            break;
  603.     
  604.         // translate any accelerator keys
  605.         TranslateMessage(&msg);
  606.  
  607.         // send the message to the window proc
  608.         DispatchMessage(&msg);
  609.         } // end if
  610.     
  611.     // main game processing goes here
  612.     Game_Main();
  613.  
  614.     } // end while
  615.  
  616. // shutdown game and release all resources
  617. Game_Shutdown();
  618.  
  619. // return to Windows like this
  620. return(msg.wParam);
  621.  
  622. } // end WinMain
  623.  
  624. // WINX GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////
  625.  
  626. int Game_Init(void *parms)
  627. {
  628. // this function is where you do all the initialization 
  629. // for your game
  630.  
  631. int index; // looping var
  632.  
  633. // create object and test for error
  634. if (DirectDrawCreateEx(NULL, (void **)&lpdd, IID_IDirectDraw7, NULL)!=DD_OK)
  635.    return(0);
  636.  
  637. // set cooperation level to windowed mode normal
  638. if (lpdd->SetCooperativeLevel(main_window_handle,
  639.            DDSCL_ALLOWMODEX | DDSCL_FULLSCREEN | 
  640.            DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT)!=DD_OK)
  641.     return(0);
  642.  
  643. // set the display mode
  644. if (lpdd->SetDisplayMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,0,0)!=DD_OK)
  645.    return(0);
  646.  
  647. // Create the primary surface
  648. memset(&ddsd,0,sizeof(ddsd));
  649. ddsd.dwSize = sizeof(ddsd);
  650. ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
  651.  
  652. // we need to let dd know that we want a complex 
  653. // flippable surface structure, set flags for that
  654. ddsd.ddsCaps.dwCaps = 
  655.   DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX;
  656.  
  657. // set the backbuffer count to 1
  658. ddsd.dwBackBufferCount = 1;
  659.  
  660. // create the primary surface
  661. lpdd->CreateSurface(&ddsd,&lpddsprimary,NULL);
  662.  
  663. // query for the backbuffer i.e the secondary surface
  664. ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
  665. lpddsprimary->GetAttachedSurface(&ddscaps,&lpddsback);
  666.  
  667.  
  668. // now load the 16 bit color bitmap
  669. Load_Bitmap_File(&bitmap16bit, "ROCK16.BMP");
  670.  
  671. // create BOBs
  672. for (index=0; index < NUM_ASTEROIDS; index++)
  673.     {
  674.     // create the bob
  675.     Create_BOB16(&asteroids[index],64,64,0,DDSCAPS_SYSTEMMEMORY);
  676.     
  677.     // extract a bitmap image for the BOB from the large bitmap
  678.     Load_BOB16(&asteroids[index],&bitmap16bit,rand()%4,0,0);  
  679.  
  680.     // set random position and velocity
  681.     asteroids[index].x = rand()%SCREEN_WIDTH;
  682.     asteroids[index].y = rand()%SCREEN_HEIGHT;
  683.  
  684.     asteroids[index].xv = -8 + rand()%16;
  685.     asteroids[index].yv = -8 + rand()%16;
  686.  
  687.     } // end for index
  688.  
  689. // clear the primary and secondary surfaces
  690. memset(&ddbltfx,0,sizeof(ddbltfx));
  691. ddbltfx.dwSize = sizeof(ddbltfx);
  692. ddbltfx.dwFillColor = 0;
  693.  
  694. lpddsprimary->Blt(NULL,NULL,NULL,DDBLT_COLORFILL | DDBLT_WAIT,&ddbltfx); 
  695. lpddsback->Blt(NULL,NULL,NULL,DDBLT_COLORFILL | DDBLT_WAIT,&ddbltfx); 
  696.  
  697. // delete the bitmap 
  698. Unload_Bitmap_File(&bitmap16bit);
  699.  
  700. // return success
  701. return(1);
  702.  
  703. } // end Game_Init
  704.  
  705. ///////////////////////////////////////////////////////////
  706.  
  707. int Game_Shutdown(void *parms)
  708. {
  709. // this function is where you shutdown your game and
  710. // release all resources that you allocated
  711.  
  712. // delete bobs
  713. for (int index=0; index<NUM_ASTEROIDS; index++)
  714.     Destroy_BOB16(&asteroids[index]);
  715.  
  716. // first release the secondary surface
  717. if (lpddsback!=NULL)
  718.    lpddsback->Release();
  719.  
  720. // now release the primary surface
  721. if (lpddsprimary!=NULL)
  722.    lpddsprimary->Release();
  723.        
  724. // release the directdraw object
  725. if (lpdd!=NULL)
  726.    lpdd->Release();
  727.  
  728. // return success
  729. return(1);
  730. } // end Game_Shutdown
  731.  
  732. ///////////////////////////////////////////////////////////
  733.  
  734. int Game_Main(void *parms)
  735. {
  736. // this is the workhorse of your game it will be called
  737. // continuously in real-time this is like main() in C
  738. // all the calls for you game go here!
  739. int index; // looping variable
  740.  
  741. // check of user is trying to exit
  742. if (KEY_DOWN(VK_ESCAPE) || KEY_DOWN(VK_SPACE))
  743.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  744.  
  745. // clear secondary buffer
  746. memset(&ddbltfx,0,sizeof(ddbltfx));
  747. ddbltfx.dwSize = sizeof(ddbltfx);
  748. ddbltfx.dwFillColor = 0;
  749. lpddsback->Blt(NULL,NULL,NULL,DDBLT_COLORFILL | DDBLT_WAIT,&ddbltfx); 
  750.  
  751. // move asteroids
  752. for (index=0; index < NUM_ASTEROIDS; index++)
  753.     {
  754.     asteroids[index].x+=asteroids[index].xv;
  755.     asteroids[index].y+=asteroids[index].yv;
  756.     } // end for index
  757.  
  758. // check if asteroids are off screen boundaries
  759. for (index=0; index<NUM_ASTEROIDS; index++)
  760.     {
  761.     // x bounds
  762.     if (asteroids[index].x > SCREEN_WIDTH)
  763.        asteroids[index].x = -asteroids[index].width;
  764.     else
  765.     if (asteroids[index].x < -asteroids[index].width)
  766.         asteroids[index].x = SCREEN_WIDTH;
  767.  
  768.     // y bounds
  769.     if (asteroids[index].y > SCREEN_HEIGHT)
  770.         asteroids[index].y = -asteroids[index].height;
  771.     else
  772.     if (asteroids[index].y < -asteroids[index].height)
  773.        asteroids[index].y = SCREEN_HEIGHT;
  774.     } // end for index
  775.  
  776. // draw all asteroids
  777. for (index=0; index<NUM_ASTEROIDS; index++)
  778.      Draw_BOB16(&asteroids[index], lpddsback);
  779.  
  780. // flip pages
  781. while(lpddsprimary->Flip(NULL, DDFLIP_WAIT)!=DD_OK);
  782.  
  783. // slow things down
  784. Sleep(30);
  785.  
  786. // return success
  787. return(1);
  788.  
  789. } // end Game_Main
  790.  
  791.