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

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