home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / Source / GPCHAP12 / GPDUMB1.H < prev    next >
C/C++ Source or Header  |  2002-04-29  |  13KB  |  273 lines

  1. // GPDUMB1.H - Header file for GPDUMB1.CPP game engine library
  2.  
  3. // watch for multiple inclusions
  4. #ifndef GPDUMB1
  5. #define GPDUMB1
  6.  
  7. // DEFINES ////////////////////////////////////////////////
  8.  
  9. // default screen size
  10. #define SCREEN_WIDTH    640  // size of screen
  11. #define SCREEN_HEIGHT   480
  12. #define SCREEN_BPP      8    // bits per pixel
  13.  
  14. // bitmap defines
  15. #define BITMAP_ID            0x4D42 // universal id for a bitmap
  16. #define BITMAP_STATE_DEAD    0
  17. #define BITMAP_STATE_ALIVE   1
  18. #define BITMAP_STATE_DYING   2 
  19. #define BITMAP_ATTR_LOADED   128
  20.  
  21. #define BITMAP_EXTRACT_MODE_CELL  0
  22. #define BITMAP_EXTRACT_MODE_ABS   1
  23.  
  24. // defines for BOBs
  25. #define BOB_STATE_DEAD         0    // this is a dead bob
  26. #define BOB_STATE_ALIVE        1    // this is a live bob
  27. #define BOB_STATE_DYING        2    // this bob is dying
  28. #define BOB_STATE_ANIM_DONE    1    // done animation state
  29. #define MAX_BOB_FRAMES         64   // maximum number of bob frames
  30. #define MAX_BOB_ANIMATIONS     16   // maximum number of animation sequeces
  31.  
  32. #define BOB_ATTR_SINGLE_FRAME   1   // bob has single frame
  33. #define BOB_ATTR_MULTI_FRAME    2   // bob has multiple frames
  34. #define BOB_ATTR_MULTI_ANIM     4   // bob has multiple animations
  35. #define BOB_ATTR_ANIM_ONE_SHOT  8   // bob will perform the animation once
  36. #define BOB_ATTR_VISIBLE        16  // bob is visible
  37. #define BOB_ATTR_BOUNCE         32  // bob bounces off edges
  38. #define BOB_ATTR_WRAPAROUND     64  // bob wraps around edges
  39. #define BOB_ATTR_LOADED         128 // the bob has been loaded
  40.  
  41. // MACROS /////////////////////////////////////////////////
  42.  
  43. // these read the keyboard asynchronously
  44. #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
  45. #define KEY_UP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
  46.  
  47. // this builds a 16 bit color value in 5.5.5 format (1-bit alpha mode)
  48. #define _RGB16BIT555(r,g,b) ((b & 31) + ((g & 31) << 5) + ((r & 31) << 10))
  49.  
  50. // this builds a 16 bit color value in 5.6.5 format (green dominate mode)
  51. #define _RGB16BIT565(r,g,b) ((b & 31) + ((g & 63) << 5) + ((r & 31) << 11))
  52.  
  53. // this builds a 24 bit color value in 8.8.8 format 
  54. #define _RGB24BIT(r,g,b) ((b) + ((g) << 8) + ((r) << 16) )
  55.  
  56. // this builds a 32 bit color value in A.8.8.8 format (8-bit alpha mode)
  57. // this is the most common format of all new video cards
  58. #define _RGB32BIT(a,r,g,b) ((b) + ((g) << 8) + ((r) << 16) + ((a) << 24))
  59.  
  60. // bit manipulation macros
  61. #define SET_BIT(word,bit_flag)   ((word)=((word) | (bit_flag)))
  62. #define RESET_BIT(word,bit_flag) ((word)=((word) & (~bit_flag)))
  63.  
  64. // initializes a direct draw struct
  65. #define DD_INIT_STRUCT(ddstruct) { memset(&ddstruct,0,sizeof(ddstruct)); ddstruct.dwSize=sizeof(ddstruct); }
  66.  
  67. // TYPES //////////////////////////////////////////////////
  68.  
  69. // basic unsigned types
  70. typedef unsigned short USHORT;
  71. typedef unsigned short WORD;
  72. typedef unsigned char  UCHAR;
  73. typedef unsigned char  BYTE;
  74.  
  75. // container structure for bitmaps .BMP file
  76. typedef struct BITMAP_FILE_TAG
  77.         {
  78.         BITMAPFILEHEADER bitmapfileheader;  // this contains the bitmapfile header
  79.         BITMAPINFOHEADER bitmapinfoheader;  // this is all the info including the palette
  80.         PALETTEENTRY     palette[256];      // we will store the palette here
  81.         UCHAR            *buffer;           // this is a pointer to the data
  82.  
  83.         } BITMAP_FILE, *BITMAP_FILE_PTR;
  84.  
  85. // the blitter object structure BOB
  86. typedef struct BOB_TYP
  87.         {
  88.         int state;          // the state of the object (general)
  89.         int anim_state;     // an animation state variable, up to you
  90.         int attr;           // attributes pertaining to the object (general)
  91.         float x,y;          // position bitmap will be displayed at
  92.         float xv,yv;        // velocity of object
  93.         int bpp;            // bits per pixel
  94.         int width, height;  // the width and height of the bob
  95.         int width_fill;     // internal, used to force 8*x wide surfaces
  96.         int counter_1;      // general counters
  97.         int counter_2;
  98.         int max_count_1;    // general threshold values;
  99.         int max_count_2;
  100.         int varsI[16];      // stack of 16 integers
  101.         float varsF[16];    // stack of 16 floats
  102.         int curr_frame;     // current animation frame
  103.         int num_frames;     // total number of animation frames
  104.         int curr_animation; // index of current animation
  105.         int anim_counter;   // used to time animation transitions
  106.         int anim_index;     // animation element index
  107.         int anim_count_max; // number of cycles before animation
  108.         int *animations[MAX_BOB_ANIMATIONS]; // animation sequences
  109.  
  110.         LPDIRECTDRAWSURFACE7 images[MAX_BOB_FRAMES]; // the bitmap images DD surfaces
  111.  
  112.         } BOB, *BOB_PTR;
  113.  
  114. // the simple bitmap image
  115. typedef struct BITMAP_IMAGE_TYP
  116.         {
  117.         int state;          // state of bitmap
  118.         int attr;           // attributes of bitmap
  119.         float x,y;          // position of bitmap
  120.         int bpp;            // bits per pixel
  121.         int width, height;  // size of bitmap
  122.         int num_bytes;      // total bytes of bitmap
  123.         UCHAR *buffer;      // pixels of bitmap
  124.  
  125.         } BITMAP_IMAGE, *BITMAP_IMAGE_PTR;
  126.  
  127. // PROTOTYPES /////////////////////////////////////////////
  128.  
  129. // DirectDraw functions
  130. int DD_Init(int width, int height, int bpp);
  131. int DD_Shutdown(void);
  132. LPDIRECTDRAWCLIPPER DD_Attach_Clipper(LPDIRECTDRAWSURFACE7 lpdds, int num_rects, LPRECT clip_list);
  133. LPDIRECTDRAWSURFACE7 DD_Create_Surface(int width, int height, int bpp, int mem_flags);
  134. int DD_Flip(void);
  135. int DD_Wait_For_Vsync(void);
  136. int DD_Fill_Surface(LPDIRECTDRAWSURFACE7 lpdds,int color);
  137. UCHAR *DD_Lock_Surface(LPDIRECTDRAWSURFACE7 lpdds,int *lpitch);
  138. int DD_Unlock_Surface(LPDIRECTDRAWSURFACE7 lpdds, UCHAR *surface_buffer);
  139. UCHAR *DD_Lock_Primary_Surface(void);
  140. int DD_Unlock_Primary_Surface(void);
  141. UCHAR *DD_Lock_Back_Surface(void);
  142. int DD_Unlock_Back_Surface(void);
  143.  
  144. // 8-bit BOB functions
  145. int Create_BOB(BOB_PTR bob,float x, float y,int width, int height,int num_frames,int attr,int mem_flags);              
  146. int Destroy_BOB(BOB_PTR bob);
  147. int Draw_BOB(BOB_PTR bob, LPDIRECTDRAWSURFACE7 dest);
  148. int Draw_Scaled_BOB(BOB_PTR bob, int swidth, int sheight,LPDIRECTDRAWSURFACE7 dest);
  149. int Load_Frame_BOB(BOB_PTR bob, BITMAP_FILE_PTR bitmap, int frame, int cx,int cy,int mode);             
  150. int Animate_BOB(BOB_PTR bob);
  151. int Scroll_BOB(void); // ni
  152. int Move_BOB(BOB_PTR bob);
  153. int Load_Animation_BOB(BOB_PTR bob, int anim_index, int num_frames, int *sequence);
  154. int Set_Pos_BOB(BOB_PTR bob, int x, int y);
  155. int Set_Vel_BOB(BOB_PTR bob,int xv, int yv);
  156. int Set_Anim_Speed_BOB(BOB_PTR bob,int speed);
  157. int Set_Animation_BOB(BOB_PTR bob, int anim_index);
  158. int Hide_BOB(BOB_PTR bob);
  159. int Show_BOB(BOB_PTR bob);
  160. int Collision_BOBS(BOB_PTR bob1, BOB_PTR bob2);
  161.  
  162. // 16-bit BOB functions
  163. int Create_BOB16(BOB_PTR bob,float x, float y,int width, int height,int num_frames,int attr,int mem_flags);              
  164. int Destroy_BOB16(BOB_PTR bob);
  165. int Draw_BOB16(BOB_PTR bob, LPDIRECTDRAWSURFACE7 dest);
  166. int Draw_Scaled_BOB16(BOB_PTR bob, int swidth, int sheight,LPDIRECTDRAWSURFACE7 dest);
  167. int Load_Frame_BOB16(BOB_PTR bob, BITMAP_FILE_PTR bitmap, int frame, int cx,int cy,int mode);             
  168. int Animate_BOB16(BOB_PTR bob);
  169. int Scroll_BOB16(void); // ni
  170. int Move_BOB16(BOB_PTR bob);
  171. int Load_Animation_BOB16(BOB_PTR bob, int anim_index, int num_frames, int *sequence);
  172. int Set_Pos_BOB16(BOB_PTR bob, int x, int y);
  173. int Set_Vel_BOB16(BOB_PTR bob,int xv, int yv);
  174. int Set_Anim_Speed_BOB16(BOB_PTR bob,int speed);
  175. int Set_Animation_BOB16(BOB_PTR bob, int anim_index);
  176. int Hide_BOB16(BOB_PTR bob);
  177. int Show_BOB16(BOB_PTR bob);
  178. int Collision_BOBS16(BOB_PTR bob1, BOB_PTR bob2);
  179.  
  180. // general utility functions
  181. DWORD Get_Clock(void);
  182. DWORD Start_Clock(void);
  183. DWORD Wait_Clock(DWORD count);
  184.  
  185. // 8-bit graphics functions
  186. int Draw_Clip_Line(int x0,int y0, int x1, int y1,USHORT color,UCHAR *dest_buffer, int lpitch);
  187. int Clip_Line(int &x1,int &y1,int &x2, int &y2);
  188. int Draw_Line(int xo, int yo, int x1,int y1, USHORT color,UCHAR *vb_start,int lpitch);
  189. int Draw_Pixel(int x, int y,int color,UCHAR *video_buffer, int lpitch);
  190. int Draw_Rectangle(int x1, int y1, int x2, int y2, int color,LPDIRECTDRAWSURFACE7 lpdds);
  191. int Screen_Transition(void); // ni
  192.  
  193. // 16-bit graphics functions
  194. int Draw_Clip_Line16(int x0,int y0, int x1, int y1,USHORT color,UCHAR *dest_buffer, int lpitch);
  195. int Draw_Line16(int xo, int yo, int x1,int y1, USHORT color,UCHAR *vb_start,int lpitch);
  196. int Draw_Pixel16(int x, int y,int color,UCHAR *video_buffer, int lpitch);
  197.  
  198. // 8-bit palette functions
  199. int Set_Palette_Entry(int color_index, LPPALETTEENTRY color);
  200. int Get_Palette_Entry(int color_index, LPPALETTEENTRY color);
  201. int Load_Palette_From_File(char *filename, LPPALETTEENTRY palette);
  202. int Save_Palette_To_File(char *filename, LPPALETTEENTRY palette);
  203. int Save_Palette(LPPALETTEENTRY sav_palette);
  204. int Set_Palette(LPPALETTEENTRY set_palette);
  205. int Rotate_Colors(int start_index, int colors);
  206. int Blink_Colors(void); // ni
  207.  
  208. // simple 8-bit bitmap image functions
  209. int Create_Bitmap(BITMAP_IMAGE_PTR image, int x, int y, int width, int height);
  210. int Destroy_Bitmap(BITMAP_IMAGE_PTR image);
  211. int Draw_Bitmap(BITMAP_IMAGE_PTR source_bitmap,UCHAR *dest_buffer, int lpitch, int transparent);
  212. int Load_Image_Bitmap(BITMAP_IMAGE_PTR image,BITMAP_FILE_PTR bitmap,int cx,int cy,int mode);               
  213. int Scroll_Bitmap(void); // ni
  214. int Copy_Bitmap(void); // ni
  215. int Flip_Bitmap(UCHAR *image, int bytes_per_line, int height);
  216.  
  217. // simple 16-bit bitmap image functions
  218. int Create_Bitmap16(BITMAP_IMAGE_PTR image, int x, int y, int width, int height);
  219. int Destroy_Bitmap16(BITMAP_IMAGE_PTR image);
  220. int Draw_Bitmap16(BITMAP_IMAGE_PTR source_bitmap,UCHAR *dest_buffer, int lpitch, int transparent);
  221. int Load_Image_Bitmap16(BITMAP_IMAGE_PTR image,BITMAP_FILE_PTR bitmap,int cx,int cy,int mode);   
  222.  
  223. // bitmap file functions
  224. int Load_Bitmap_File(BITMAP_FILE_PTR bitmap, char *filename);
  225. int Unload_Bitmap_File(BITMAP_FILE_PTR bitmap);
  226.  
  227. // gdi functions
  228. int Draw_Text_GDI(char *text, int x,int y,COLORREF color, LPDIRECTDRAWSURFACE7 lpdds);
  229. int Draw_Text_GDI(char *text, int x,int y,int color, LPDIRECTDRAWSURFACE7 lpdds);
  230.  
  231. // error functions
  232. int Open_Error_File(char *filename);
  233. int Close_Error_File(void);
  234. int Write_Error(char *string, ...);
  235.  
  236. // GLOBALS ////////////////////////////////////////////////
  237.  
  238. extern FILE                 *fp_error;            // general error file
  239. extern LPDIRECTDRAW7        lpdd;                 // dd object
  240. extern LPDIRECTDRAWSURFACE7 lpddsprimary;         // dd primary surface
  241. extern LPDIRECTDRAWSURFACE7 lpddsback;            // dd back surface
  242. extern LPDIRECTDRAWPALETTE  lpddpal;              // a pointer to the created dd palette
  243. extern LPDIRECTDRAWCLIPPER  lpddclipper;          // dd clipper
  244. extern PALETTEENTRY         palette[256];         // color palette
  245. extern PALETTEENTRY         save_palette[256];    // used to save palettes
  246. extern DDSURFACEDESC2       ddsd;                 // a direct draw surface description struct
  247. extern DDBLTFX              ddbltfx;              // used to fill
  248. extern DDSCAPS2             ddscaps;              // a direct draw surface capabilities struct
  249. extern HRESULT              ddrval;               // result back from dd calls
  250. extern UCHAR                *primary_buffer;      // primary video buffer
  251. extern UCHAR                *back_buffer;         // secondary back buffer
  252. extern int                  primary_lpitch;       // memory line pitch
  253. extern int                  back_lpitch;          // memory line pitch
  254. extern BITMAP_FILE          bitmap16bit;          // a 16 bit bitmap file
  255. extern BITMAP_FILE          bitmap8bit;           // a 8 bit bitmap file
  256.  
  257. extern DWORD                start_clock_count;    // used for timing
  258.  
  259. // these defined the general clipping rectangle
  260. extern int min_clip_x,                             // clipping rectangle 
  261.            max_clip_x,                  
  262.            min_clip_y,     
  263.            max_clip_y;                  
  264.  
  265. // these are overwritten globally by DD_Init()
  266. extern int screen_width,                            // width of screen
  267.            screen_height,                           // height of screen
  268.            screen_bpp;                              // bits per pixel 
  269.  
  270. #endif
  271.  
  272.  
  273.