T3D图形库(七)

 T3D控制台程序,仅显示一个16位色的空白窗口程序,以Esc或Space关闭程序。

 

  1. /*2008.9.29
  2. T3D图形库
  3. ——<<windows游戏
  4. 编程大师技巧>>
  5. */
  6. //T3Dconsole.cpp
  7. // INCLUDES ///
  8. #define WIN32_LEAN_AND_MEAN   //不使用MFC
  9. #define INITGUID        //初始化COM
  10. #include <windows.h>   // include important windows stuff
  11. #include <windowsx.h> 
  12. #include <mmsystem.h>
  13. #include <iostream.h> // include important C/C++ stuff
  14. #include <conio.h>
  15. #include <stdlib.h>
  16. #include <malloc.h>
  17. #include <memory.h>
  18. #include <string.h>
  19. #include <stdarg.h>
  20. #include <stdio.h> 
  21. #include <math.h>
  22. #include <io.h>
  23. #include <fcntl.h>
  24. #include <ddraw.h>  // directX includes
  25. #include <dinput.h>
  26. #include <dsound.h> 
  27. #include <dmksctrl.h>
  28. #include <dmusici.h>
  29. #include <dmusicc.h>
  30. #include <dmusicf.h>
  31. #include "DDraw_lib.h"  // DDraw以及图形库函数接口与结全局变量/结构声明
  32. #include "DI_lib.h"   // DirectInput函数接口以及全局变量声明
  33. #include "DS&DM_LIB.h"  //DirectSound && DirecMusic 函数接口与全局变量声明
  34. // DEFINES 
  35. #define WINDOW_CLASS_NAME "WIN3DCLASS"  // 类名
  36. #define WINDOW_TITLE      "T3D Graphics Console Ver 2.0"  //标题
  37. #define WINDOW_WIDTH      640   // 窗口大小
  38. #define WINDOW_HEIGHT     480
  39. #define WINDOW_BPP        16   // 仅支持8位/16位,当窗口模式时,色深应与windows桌面色深相同
  40.       
  41. #define WINDOWED_APP      1     // 0 全屏 1 窗口 
  42. // PROTOTYPES /
  43. // 游戏控制台
  44. int Game_Init(void *parms=NULL);
  45. int Game_Shutdown(void *parms=NULL);
  46. int Game_Main(void *parms=NULL);
  47. // 全局变量 
  48. HWND main_window_handle           = NULL; //窗口句柄
  49. HINSTANCE main_instance           = NULL; // 实例
  50. char buffer[256];                         // 输出缓冲区
  51. // FUNCTIONS //
  52. LRESULT CALLBACK WindowProc(HWND hwnd, 
  53.                             UINT msg, 
  54.                             WPARAM wparam, 
  55.                             LPARAM lparam)
  56. {
  57. PAINTSTRUCT ps;        
  58. HDC         hdc;       // 设备环境句柄
  59. // 消息
  60. switch(msg)
  61.     {   
  62.     case WM_CREATE: 
  63.         {
  64.         // 初始化
  65.         return(0);
  66.         } break;
  67.     case WM_PAINT:
  68.          {
  69.          // 开始绘制
  70.          hdc = BeginPaint(hwnd,&ps);
  71.          // 结束绘制
  72.          EndPaint(hwnd,&ps);
  73.          return(0);
  74.         } break;
  75.     case WM_DESTROY: 
  76.         {
  77.         // 销毁程序     
  78.         PostQuitMessage(0);
  79.         return(0);
  80.         } break;
  81.     default:break;
  82.     } 
  83. return (DefWindowProc(hwnd, msg, wparam, lparam));
  84. // end WinProc
  85. // WINMAIN 
  86. int WINAPI WinMain( HINSTANCE hinstance,
  87.                     HINSTANCE hprevinstance,
  88.                     LPSTR lpcmdline,
  89.                     int ncmdshow)
  90. {
  91. WNDCLASSEX winclass; // 窗口类
  92. HWND       hwnd;     
  93. MSG        msg;     
  94. HDC        hdc;      
  95. // 填充窗口类结构
  96. winclass.cbSize         = sizeof(WNDCLASSEX);
  97. winclass.style          = CS_DBLCLKS | CS_OWNDC | 
  98.                           CS_HREDRAW | CS_VREDRAW;
  99. winclass.lpfnWndProc    = WindowProc;
  100. winclass.cbClsExtra     = 0;
  101. winclass.cbWndExtra     = 0;
  102. winclass.hInstance      = hinstance;
  103. winclass.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
  104. winclass.hCursor        = LoadCursor(NULL, IDC_ARROW); 
  105. winclass.hbrBackground  = (HBRUSH)GetStockObject(BLACK_BRUSH);
  106. winclass.lpszMenuName   = NULL;
  107. winclass.lpszClassName  = WINDOW_CLASS_NAME;
  108. winclass.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);
  109. // 注册窗口类
  110. if (!RegisterClassEx(&winclass))
  111.     return(0);
  112. // 创建窗口
  113. if (!(hwnd = CreateWindowEx(NULL,              // 扩展类型
  114.                             WINDOW_CLASS_NAME, // 类名
  115.                             WINDOW_TITLE, // 标题
  116.                             (WINDOWED_APP ? (WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION) : (WS_POPUP | WS_VISIBLE)), 
  117.                             0,0,      // 初始窗口坐标
  118.                             WINDOW_WIDTH,WINDOW_HEIGHT,  // 初始宽/高(px)
  119.                             NULL,     // 父句柄
  120.                             NULL,     // 菜单句柄
  121.                             hinstance,// 应用程序实例
  122.                             NULL)))   
  123. return(0);
  124. // 存储窗口句柄与实例
  125. main_window_handle = hwnd;
  126. main_instance      = hinstance;
  127. //窗口模式
  128. if (WINDOWED_APP)
  129. {
  130. // 定义客户区矩形
  131. RECT window_rect = {0,0,WINDOW_WIDTH-1,WINDOW_HEIGHT-1};
  132. // 调用此函数使窗口自适应上述矩形区域
  133. AdjustWindowRectEx(&window_rect,
  134.      GetWindowStyle(main_window_handle),
  135.      GetMenu(main_window_handle) != NULL,
  136.      GetWindowExStyle(main_window_handle));
  137. // 客户区起始坐标
  138. window_client_x0 = -window_rect.left;
  139. window_client_y0 = -window_rect.top;
  140. // 重置窗口位置
  141. MoveWindow(main_window_handle,
  142.            0, // x position
  143.            0, // y position
  144.            window_rect.right - window_rect.left, // width
  145.            window_rect.bottom - window_rect.top, // height
  146.            TRUE  // 为TRUE则窗口需重绘
  147.            );   
  148. // 显示窗口
  149. ShowWindow(main_window_handle, 
  150.            SW_SHOW      //激活窗口并以当前大小和位置显示它
  151.            );
  152. // end if windowed
  153. // 游戏初始化
  154. Game_Init();
  155. // 进入主循环
  156. while(1)
  157.     {
  158.     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  159.         { 
  160.         if (msg.message == WM_QUIT)
  161.            break;
  162.     
  163.         TranslateMessage(&msg);
  164.         // 发送消息至回调函数!
  165.         DispatchMessage(&msg);
  166.         } 
  167.     
  168.     // 游戏循环
  169.     Game_Main();
  170.     } 
  171. // 关闭游戏,释放资源
  172. Game_Shutdown();
  173. return(msg.wParam);
  174. // T3D II GAME PROGRAMMING CONSOLE FUNCTIONS 
  175. int Game_Init(void *parms)
  176. {
  177. // 初始化DirectDraw
  178. DDraw_Init(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_BPP, WINDOWED_APP);
  179. DInput_Init();
  180. DInput_Init_Keyboard();
  181. // 全屏模式隐藏鼠标
  182. if (!WINDOWED_APP)
  183.     ShowCursor(FALSE);
  184. // 取得随机数种子
  185. srand(Start_Clock());
  186. return(1);
  187. ///
  188. int Game_Shutdown(void *parms)
  189. {
  190.   //现在仅仅是释放DirectDraw资源,以后会释放其他相关的资源
  191.   DInput_Release_Keyboard();
  192.   DInput_Shutdown();
  193.   DDraw_Shutdown();
  194.   return(1);
  195. //
  196. int Game_Main(void *parms)
  197. {
  198. int index; // 循环变量
  199. // 计时开始
  200. Start_Clock();
  201. // 初始化显示表面
  202. DDraw_Fill_Surface(lpddsback, 0);
  203. DInput_Read_Keyboard();
  204. // 游戏逻辑
  205. //表面替换
  206. DDraw_Flip();
  207. // 一帧 30 ms
  208. Wait_Clock(30);
  209. // 按Esc则退出游戏
  210. if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_SPACE])
  211.     {
  212.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  213.     } 
  214. return(1);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值