现在的位置: 首页 > 综合 > 正文

T3D图形库(一)

2013年07月26日 ⁄ 综合 ⁄ 共 15291字 ⁄ 字号 评论关闭

T3D图形库DDraw及图形算法头文件部分

 

  1. /*2008.9.15
  2. T3D图形库
  3. ——<<windows游戏
  4. 编程大师技巧>>
  5. */
  6. //DDraw_lib.h
  7. #ifndef DDraw_lib
  8. #define DDraw_lib
  9. // DEFINES ////////////////////////////////////////////////
  10. //默认屏幕值,将被DDraw_Init()调用
  11. #define SCREEN_WIDTH  640       //屏宽
  12. #define SCREEN_HEIGHT 480       //屏高
  13. #define SCREEN_BPP    8         //屏幕色深
  14. #define MAX_COLORS_PALETTE 256  //最大调色板项数
  15. #define DEFAULT_PALETTE_FILE "PALDATA2.PAL"   //默认调色板文件
  16. //全屏/窗口模式标志
  17. #define SCREEN_FULLSCREEN 0
  18. #define SCREEN_WINDOWED   1
  19. //位图
  20. #define BITMAP_ID           0x4D42
  21. #define BITMAP_STATE_DEAD   0
  22. #define BITMAP_STATE_ALIVE  1
  23. #define BITMAP_STATE_DYING  2
  24. #define BITMAP_ATTR_LOADED  128
  25. //位图提取模式
  26. #define BITMAP_EXTRACT_MODE_CELL 0    //单元模式(模板化)   
  27. #define BITMAP_EXTRACT_MODE_ABS  1    //绝对模式
  28. //像素格式
  29. #define DD_PIXEL_FORMAT8     8
  30. #define DD_PIXEL_FORMAT555   15
  31. #define DD_PIXEL_FORMAT565   16
  32. #define DD_PIXEL_FORMAT888   24
  33. #define DD_PIXEL_FORMATALPHA 32
  34. //BOBs(子画面)
  35. #define BOB_STATE_DEAD         0    // 死亡
  36. #define BOB_STATE_ALIVE        1    // 存在
  37. #define BOB_STATE_DYING        2    // 正在死亡
  38. #define BOB_STATE_ANIM_DONE    1    // 动画状态
  39. #define MAX_BOB_FRAMES         64   // 最多64帧
  40. #define MAX_BOB_ANIMATIONS     16   // 最多16个动画
  41. //BOB属性
  42. #define BOB_ATTR_SINGLE_FRAME   1   // bob只有1帧
  43. #define BOB_ATTR_MULTI_FRAME    2   // bob有多个帧
  44. #define BOB_ATTR_MULTI_ANIM     4   // bob有多个动画
  45. #define BOB_ATTR_ANIM_ONE_SHOT  8   // bob只播放一次一次
  46. #define BOB_ATTR_VISIBLE        16  // bob对象可见
  47. #define BOB_ATTR_BOUNCE         32  // bob边界动作:反弹
  48. #define BOB_ATTR_WRAPAROUND     64  // bob边界动作:回绕
  49. #define BOB_ATTR_LOADED         128 // bob已被加载
  50. #define BOB_ATTR_CLONE          256 // bob是克隆的
  51. //屏幕过渡命令
  52. #define SCREEN_DARKNESS  0         // 变暗
  53. #define SCREEN_WHITENESS 1         // 变亮
  54. #define SCREEN_SWIPE_X   2         // 横向刷新
  55. #define SCREEN_SWIPE_Y   3         // 纵向刷新
  56. #define SCREEN_DISOLVE   4         // 像素溶解
  57. #define SCREEN_SCRUNCH   5         // 压缩
  58. #define SCREEN_BLUENESS  6         // 变蓝
  59. #define SCREEN_REDNESS   7         // 变红
  60. #define SCREEN_GREENNESS 8         // 变绿
  61. //颜色(灯)闪烁
  62. #define BLINKER_ADD           0    // 增加一个闪烁(灯)  
  63. #define BLINKER_DELETE        1    // 删除一个闪烁(灯) 
  64. #define BLINKER_UPDATE        2    // 更新一个闪烁(灯) 
  65. #define BLINKER_RUN           3    // 正常运行
  66. //pi
  67. #define PI         ((float)3.141592654f)
  68. #define PI2        ((float)6.283185307f)
  69. #define PI_DIV_2   ((float)1.570796327f)
  70. #define PI_DIV_4   ((float)0.785398163f) 
  71. #define PI_INV     ((float)0.318309886f)  //pi/10
  72. // fixed point mathematics constants
  73. #define FIXP16_SHIFT     16
  74. #define FIXP16_MAG       65536
  75. #define FIXP16_DP_MASK   0x0000ffff
  76. #define FIXP16_WP_MASK   0xffff0000
  77. #define FIXP16_ROUND_UP  0x00008000
  78. // MACROS /////////////////////////////////////////////////
  79. //检查键盘按键状态
  80. #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
  81. #define KEY_UP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
  82. //创造16位颜色值(1.5.5.5)
  83. #define _RGB16BIT555(r,g,b) ((b & 31) + ((g & 31) << 5) + ((r & 31) << 10))
  84. //创造16位颜色值(5.6.5)
  85. #define _RGB16BIT565(r,g,b) ((b & 31) + ((g & 63) << 5) + ((r & 31) << 11))
  86. //创造24位颜色值(8.8.8)
  87. #define _RGB24BIT(a,r,g,b) ((b) + ((g) << 8) + ((r) << 16) )
  88. //创造32位颜色值(8.8.8.8)
  89. #define _RGB32BIT(a,r,g,b) ((b) + ((g) << 8) + ((r) << 16) + ((a) << 24))
  90. //位操控
  91. #define SET_BIT(word,bit_flag)  ((word)=((word)|(bit_flag)))
  92. #define RESET_BIT(word,bit_flag)  ((word)=((word)&(~bit_flag)))
  93. //初始化结构体
  94. #define DDRAW_INIT_STRUCT(ddstruct) { memset(&ddstruct,0,sizeof(ddstruct));ddstruct.dwSize=sizeof(ddstruct); }
  95. //最小/大值
  96. #define MIN(a,b)   (((a)<(b))?(a):(b))
  97. #define MAX(a,b)   (((a)>(b))?(a):(b))
  98. //交换值
  99. #define SWAP(a,b,t)  { t=a;a=b;b=t; }
  100. //一些数学宏
  101. #define DEG_TO_RAD(ang)   ((ang)*PI/180.0)    //角度转弧度
  102. #define RAD_TO_DEG(rads)  ((rads)*180.0/PI)   //弧度转角度
  103. //随机取(x,y)中的一个值
  104. #define RAD_RANGE(x,y)    ( (x) + (rand()%((y)-(x)+1)) )
  105. // TYPES //////////////////////////////////////////////////
  106. // 基本无符号类型
  107. typedef unsigned short USHORT;
  108. typedef unsigned short WORD;
  109. typedef unsigned char  UCHAR;
  110. typedef unsigned char  BYTE;
  111. typedef unsigned int   QUAD;
  112. typedef unsigned int   UINT;
  113. //存放位图信息的结构体
  114. typedef struct BITMAP_FILE_TAG
  115.         {
  116.         BITMAPFILEHEADER bitmapfileheader;  // 位图文件头
  117.         BITMAPINFOHEADER bitmapinfoheader;  // 位图信息头
  118.         PALETTEENTRY     palette[256];      // 调色板项
  119.         UCHAR            *buffer;           // 指针指向位图数据
  120.         } BITMAP_FILE, *BITMAP_FILE_PTR;
  121. // BOB对象结构体
  122. typedef struct BOB_TYP
  123.         {
  124.         int state;          // 常规状态
  125.         int anim_state;     // 动画是否可用
  126.         int attr;           // BOB属性
  127.         float x,y;            //初始位置
  128.         float xv,yv;          //速度
  129.         int width, height;  // 宽/高
  130.         int width_fill;     // 宽度填充(使宽度为8的倍数)
  131.         int bpp;            // 色深
  132.         int counter_1;      // 常规计数器
  133.         int counter_2;
  134.         int max_count_1;   
  135.         int max_count_2;
  136.         int varsI[16];      //整数栈
  137.         float varsF[16];    //浮点数栈
  138.         int curr_frame;     // 当前帧
  139.         int num_frames;     // 总帧数
  140.         int curr_animation; // 当前动画索引
  141.         int anim_counter;   // 计数器
  142.         int anim_index;     // 动画内的帧索引
  143.         int anim_count_max; // 在动画前须达到的数
  144.         int *animations[MAX_BOB_ANIMATIONS]; // 动画序列
  145.         LPDIRECTDRAWSURFACE7 images[MAX_BOB_FRAMES]; // DD 表面
  146.         } BOB, *BOB_PTR;
  147. //位图数据图像结构体
  148. typedef struct BITMAP_IMAGE_TYP 
  149.         {
  150.         int state;
  151.         int attr;
  152.         int x,y;
  153.         int width,height;
  154.         int num_bytes;
  155.         int bpp;
  156.         UCHAR* buffer;
  157.         
  158.         }BITMAP_IMAGE,*BITMAP_IMAGE_PTR;
  159. // 闪烁(灯)结构体
  160. typedef struct BLINKER_TYP
  161.         {
  162.          //需设置成员
  163.         int color_index;         // 颜色索引
  164.         PALETTEENTRY on_color;   // RGB值(开)
  165.         PALETTEENTRY off_color;  // RGB值(关)
  166.         int on_time;             // 帧数(开)
  167.         int off_time;            // 帧数(关)
  168.         //内部成员
  169.         int counter;             // counter for state transitions
  170.         int state;               // 灯状态, -1 off, 1 on, 0 dead
  171.         } BLINKER, *BLINKER_PTR;
  172. //2D向量(整型)
  173. typedef struct VERTEX2DI_TYP 
  174.         {
  175.         int x,y;
  176.         }VERTEX2DI,*VERTEX2DI_PTR;
  177. //2D向量(浮点型)
  178. typedef struct VERTEX2DF_TYP 
  179.         {
  180.         float x,y;
  181.         }VERTEX2DF,*VERTEX2DF_PTR;
  182. //2D多边形
  183. typedef struct POLYGON2D_TYP 
  184.         {
  185.         int state;
  186.         int num_verts;
  187.         int x0,y0;
  188.         int xv,yv;
  189.         DWORD color;
  190.         VERTEX2DF* vlist;
  191.         }POLYGON2D,*POLYGON2D_PTR;
  192. // 矩阵描述
  193. //3X3矩阵
  194. typedef struct MATRIX3X3_TYP 
  195.       {
  196.       union
  197.       {
  198.         float M[3][3];
  199.         struct  
  200.         {
  201.           float M00,M01,M02;
  202.           float M10,M11,M12;
  203.           float M20,M21,M22;
  204.         };
  205.       };
  206.       }MATRIX3X3,*MATRIX3X3_PTR;
  207. //1X3矩阵
  208. typedef struct MATRIX1X3_TYP 
  209.       {
  210.       union
  211.       {
  212.         float M[3];
  213.         struct  
  214.         {
  215.           float M00,M01,M02;
  216.         };
  217.       };
  218.       }MATRIX1X3,*MATRIX1X3_PTR;
  219. //3X2矩阵
  220. typedef struct MATRIX3X2_TYP 
  221.       {
  222.       union
  223.       {
  224.         float M[3][2];
  225.         struct  
  226.         {
  227.           float M00,M01;
  228.           float M10,M11;
  229.           float M20,M21;
  230.         };
  231.       };
  232.       }MATRIX3X2,*MATRIX3X2_PTR;
  233. //1X2矩阵
  234. typedef struct MATRIX1X2_TYP 
  235.       {
  236.       union
  237.       {
  238.         float M[2];
  239.         struct  
  240.         {
  241.           float M00,M01;
  242.         };
  243.       };
  244.       }MATRIX1X2,*MATRIX1X2_PTR;
  245. // 函数原型(接口) /////////////////////////////////////////////
  246. // DirectDraw 函数
  247. int DDraw_Init(int width, int height, int bpp, int windowed=0);
  248. int DDraw_Shutdown(void);
  249. LPDIRECTDRAWCLIPPER DDraw_Attach_Clipper(LPDIRECTDRAWSURFACE7 lpdds, int num_rects, LPRECT clip_list);
  250. LPDIRECTDRAWSURFACE7 DDraw_Create_Surface(int width, int height, int mem_flags=0, USHORT color_key_value=0);
  251. int DDraw_Flip(void);
  252. int DDraw_Wait_For_Vsync(void);
  253. int DDraw_Fill_Surface(LPDIRECTDRAWSURFACE7 lpdds, USHORT color, RECT *client=NULL);
  254. UCHAR *DDraw_Lock_Surface(LPDIRECTDRAWSURFACE7 lpdds,int *lpitch);
  255. int DDraw_Unlock_Surface(LPDIRECTDRAWSURFACE7 lpdds);
  256. UCHAR *DDraw_Lock_Primary_Surface(void);
  257. int DDraw_Unlock_Primary_Surface(void);
  258. UCHAR *DDraw_Lock_Back_Surface(void);
  259. int DDraw_Unlock_Back_Surface(void);
  260. // BOB 函数
  261. int Create_BOB(BOB_PTR bob,int x, int y,int width, int height,int num_frames,int attr,
  262.                int mem_flags=0, USHORT color_key_value=0, int bpp=8);              
  263. int Clone_BOB(BOB_PTR source, BOB_PTR dest);
  264. int Destroy_BOB(BOB_PTR bob);
  265. int Draw_BOB(BOB_PTR bob, LPDIRECTDRAWSURFACE7 dest);
  266. int Draw_Scaled_BOB(BOB_PTR bob, int swidth, int sheight,LPDIRECTDRAWSURFACE7 dest);
  267. int Draw_BOB16(BOB_PTR bob, LPDIRECTDRAWSURFACE7 dest);
  268. int Draw_Scaled_BOB16(BOB_PTR bob, int swidth, int sheight,LPDIRECTDRAWSURFACE7 dest);
  269. int Load_Frame_BOB(BOB_PTR bob, BITMAP_FILE_PTR bitmap, int frame, int cx,int cy,int mode);              
  270. int Load_Frame_BOB16(BOB_PTR bob, BITMAP_FILE_PTR bitmap, int frame, int cx,int cy,int mode);  
  271. int Animate_BOB(BOB_PTR bob);
  272. int Move_BOB(BOB_PTR bob);
  273. int Load_Animation_BOB(BOB_PTR bob, int anim_index, int num_frames, int *sequence);
  274. int Set_Pos_BOB(BOB_PTR bob, int x, int y);
  275. int Set_Vel_BOB(BOB_PTR bob,int xv, int yv);
  276. int Set_Anim_Speed_BOB(BOB_PTR bob,int speed);
  277. int Set_Animation_BOB(BOB_PTR bob, int anim_index);
  278. int Hide_BOB(BOB_PTR bob);
  279. int Show_BOB(BOB_PTR bob);
  280. int Collision_BOBS(BOB_PTR bob1, BOB_PTR bob2);
  281. // 调色板 函数
  282. int Set_Palette_Entry(int color_index, LPPALETTEENTRY color);
  283. int Get_Palette_Entry(int color_index, LPPALETTEENTRY color);
  284. int Load_Palette_From_File(char *filename, LPPALETTEENTRY palette);
  285. int Save_Palette_To_File(char *filename, LPPALETTEENTRY palette);
  286. int Save_Palette(LPPALETTEENTRY sav_palette);
  287. int Set_Palette(LPPALETTEENTRY set_palette);
  288. int Rotate_Colors(int start_index, int end_index);
  289. int Blink_Colors(int command, BLINKER_PTR new_light, int id);
  290. // 位图函数
  291. int Create_Bitmap(BITMAP_IMAGE_PTR image, int x, int y, int width, int height, int bpp=8);
  292. int Destroy_Bitmap(BITMAP_IMAGE_PTR image);
  293. int Draw_Bitmap(BITMAP_IMAGE_PTR source_bitmap,UCHAR *dest_buffer, int lpitch, int transparent);
  294. int Draw_Bitmap16(BITMAP_IMAGE_PTR source_bitmap,UCHAR *dest_buffer, int lpitch, int transparent);
  295. int Load_Image_Bitmap(BITMAP_IMAGE_PTR image,BITMAP_FILE_PTR bitmap,int cx,int cy,int mode);  
  296. int Load_Image_Bitmap16(BITMAP_IMAGE_PTR image,BITMAP_FILE_PTR bitmap,int cx,int cy,int mode);               
  297. int Scroll_Bitmap(BITMAP_IMAGE_PTR image, int dx, int dy=0);
  298. int Copy_Bitmap(BITMAP_IMAGE_PTR dest_bitmap, int dest_x, int dest_y, 
  299.                 BITMAP_IMAGE_PTR source_bitmap, int source_x, int source_y, 
  300.                 int width, int height);
  301. int Flip_Bitmap(UCHAR *image, int bytes_per_line, int height);
  302. // 位图文件函数
  303. int Load_Bitmap_File(BITMAP_FILE_PTR bitmap, char *filename);
  304. int Unload_Bitmap_File(BITMAP_FILE_PTR bitmap);
  305. // GDI 函数
  306. int Draw_Text_GDI(char *text, int x,int y,COLORREF color, LPDIRECTDRAWSURFACE7 lpdds);
  307. int Draw_Text_GDI(char *text, int x,int y,int color, LPDIRECTDRAWSURFACE7 lpdds);
  308. //误差函数
  309. int Open_Error_File(char *filename, FILE *fp_override=NULL);
  310. int Close_Error_File(void);
  311. int Write_Error(char *string, ...);
  312. // 2d 8/16位三角形函数
  313. void Draw_Top_Tri(int x1,int y1,int x2,int y2, int x3,int y3,int color,UCHAR *dest_buffer, int mempitch);
  314. void Draw_Bottom_Tri(int x1,int y1, int x2,int y2, int x3,int y3,int color,UCHAR *dest_buffer, int mempitch);
  315. void Draw_Top_Tri16(int x1,int y1,int x2,int y2, int x3,int y3,int color,UCHAR *dest_buffer, int mempitch);
  316. void Draw_Bottom_Tri16(int x1,int y1, int x2,int y2, int x3,int y3,int color,UCHAR *dest_buffer, int mempitch);
  317. void Draw_Top_TriFP(int x1,int y1,int x2,int y2, int x3,int y3,int color,UCHAR *dest_buffer, int mempitch);
  318. void Draw_Bottom_TriFP(int x1,int y1, int x2,int y2, int x3,int y3,int color,UCHAR *dest_buffer, int mempitch);
  319. void Draw_Triangle_2D(int x1,int y1,int x2,int y2,int x3,int y3,
  320.                       int color,UCHAR *dest_buffer, int mempitch);
  321. void Draw_Triangle_2D16(int x1,int y1,int x2,int y2,int x3,int y3,
  322.                         int color,UCHAR *dest_buffer, int mempitch);
  323. void Draw_TriangleFP_2D(int x1,int y1,int x2,int y2,int x3,int y3,
  324.                         int color,UCHAR *dest_buffer, int mempitch);
  325. inline void Draw_QuadFP_2D(int x0,int y0,int x1,int y1,
  326.                            int x2,int y2,int x3, int y3,
  327.                            int color,UCHAR *dest_buffer, int mempitch);
  328. //实用工具函数
  329. DWORD Get_Clock(void);
  330. DWORD Start_Clock(void);
  331. DWORD Wait_Clock(DWORD count);
  332. int Collision_Test(int x1, int y1, int w1, int h1, 
  333.                    int x2, int y2, int w2, int h2); 
  334. int Color_Scan(int x1, int y1, int x2, int y2, 
  335.                UCHAR scan_start, UCHAR scan_end, 
  336.                UCHAR *scan_buffer, int scan_lpitch);
  337. int Color_Scan16(int x1, int y1, int x2, int y2, 
  338.                   USHORT scan_start, USHORT scan_end, 
  339.                   UCHAR *scan_buffer, int scan_lpitch);
  340. int Scan_Image_Bitmap(BITMAP_FILE_PTR bitmap,    
  341.                       LPDIRECTDRAWSURFACE7 lpdds, 
  342.                       int cx,int cy);            
  343. // 2D基本图元函数
  344. int Draw_Clip_Line(int x0,int y0, int x1, int y1, int color,UCHAR *dest_buffer, int lpitch);
  345. int Draw_Clip_Line16(int x0,int y0, int x1, int y1, int color,UCHAR *dest_buffer, int lpitch);
  346. int Clip_Line(int &x1,int &y1,int &x2, int &y2);
  347. int Draw_Line(int x0, int y0, int x1,int y1, int color,UCHAR *vb_start,int lpitch);
  348. int Draw_Line16(int x0, int y0, int x1,int y1, int color,UCHAR *vb_start,int lpitch);
  349. int Draw_Pixel(int x, int y,int color,UCHAR *video_buffer, int lpitch);
  350. int Draw_Pixel16(int x, int y,int color,UCHAR *video_buffer, int lpitch);
  351. int Draw_Rectangle(int x1, int y1, int x2, int y2, int color,LPDIRECTDRAWSURFACE7 lpdds);
  352. void HLine(int x1,int x2,int y,int color, UCHAR *vbuffer, int lpitch);
  353. void VLine(int y1,int y2,int x,int color, UCHAR *vbuffer, int lpitch);
  354. void HLine16(int x1,int x2,int y,int color, UCHAR *vbuffer, int lpitch);
  355. void VLine16(int y1,int y2,int x,int color, UCHAR *vbuffer, int lpitch);
  356. void Screen_Transitions(int effect, UCHAR *vbuffer, int lpitch);
  357. // 常规2D 8/16位 多边形变换函数
  358. int Draw_Filled_Polygon2D(POLYGON2D_PTR poly, UCHAR *vbuffer, int mempitch);
  359. int Draw_Filled_Polygon2D16(POLYGON2D_PTR poly, UCHAR *vbuffer, int mempitch);
  360. int Translate_Polygon2D(POLYGON2D_PTR poly, int dx, int dy);
  361. int Rotate_Polygon2D(POLYGON2D_PTR poly, int theta);
  362. int Scale_Polygon2D(POLYGON2D_PTR poly, float sx, float sy);
  363. void Build_Sin_Cos_Tables(void);
  364. int Translate_Polygon2D_Mat(POLYGON2D_PTR poly, int dx, int dy);
  365. int Rotate_Polygon2D_Mat(POLYGON2D_PTR poly, int theta);
  366. int Scale_Polygon2D_Mat(POLYGON2D_PTR poly, float sx, float sy);
  367. int Draw_Polygon2D(POLYGON2D_PTR poly, UCHAR *vbuffer, int lpitch);
  368. int Draw_Polygon2D16(POLYGON2D_PTR poly, UCHAR *vbuffer, int lpitch);
  369. // 数学函数(求2点距离)
  370. int Fast_Distance_2D(int x, int y);
  371. float Fast_Distance_3D(float x, float y, float z);
  372. // 碰撞检测函数(找到多边形边框)
  373. int Find_Bounding_Box_Poly2D(POLYGON2D_PTR poly, 
  374.                              float &min_x, float &max_x, 
  375.                              float &min_y, float &max_y);
  376. // 基础矩阵函数
  377. int Mat_Mul_1X2_3X2(MATRIX1X2_PTR ma, 
  378.                    MATRIX3X2_PTR mb,
  379.                    MATRIX1X2_PTR mprod);
  380. int Mat_Mul_1X3_3X3(MATRIX1X3_PTR ma, 
  381.                    MATRIX3X3_PTR mb,
  382.                    MATRIX1X3_PTR mprod);
  383. int Mat_Mul_3X3(MATRIX3X3_PTR ma, 
  384.                MATRIX3X3_PTR mb,
  385.                MATRIX3X3_PTR mprod);
  386. inline int Mat_Init_3X2(MATRIX3X2_PTR ma, 
  387.                         float m00, float m01,
  388.                         float m10, float m11,
  389.                         float m20, float m21);
  390. // 全局变量 ////////////////////////////////////////////////
  391. // 这里还是英文注释,容易理解
  392. extern FILE *fp_error;                           // general error file
  393. extern char error_filename[80];                  // error file name
  394. // notice that interface 4.0 is used on 

抱歉!评论已关闭.