T3D控制台程序,仅显示一个16位色的空白窗口程序,以Esc或Space关闭程序。
- /*2008.9.29
- T3D图形库
- ——<<windows游戏
- 编程大师技巧>>
- */
- //T3Dconsole.cpp
- // INCLUDES ///
- #define WIN32_LEAN_AND_MEAN //不使用MFC
- #define INITGUID //初始化COM
- #include <windows.h> // include important windows stuff
- #include <windowsx.h>
- #include <mmsystem.h>
- #include <iostream.h> // include important C/C++ stuff
- #include <conio.h>
- #include <stdlib.h>
- #include <malloc.h>
- #include <memory.h>
- #include <string.h>
- #include <stdarg.h>
- #include <stdio.h>
- #include <math.h>
- #include <io.h>
- #include <fcntl.h>
- #include <ddraw.h> // directX includes
- #include <dinput.h>
- #include <dsound.h>
- #include <dmksctrl.h>
- #include <dmusici.h>
- #include <dmusicc.h>
- #include <dmusicf.h>
- #include "DDraw_lib.h" // DDraw以及图形库函数接口与结全局变量/结构声明
- #include "DI_lib.h" // DirectInput函数接口以及全局变量声明
- #include "DS&DM_LIB.h" //DirectSound && DirecMusic 函数接口与全局变量声明
- // DEFINES
- #define WINDOW_CLASS_NAME "WIN3DCLASS" // 类名
- #define WINDOW_TITLE "T3D Graphics Console Ver 2.0" //标题
- #define WINDOW_WIDTH 640 // 窗口大小
- #define WINDOW_HEIGHT 480
- #define WINDOW_BPP 16 // 仅支持8位/16位,当窗口模式时,色深应与windows桌面色深相同
- #define WINDOWED_APP 1 // 0 全屏 1 窗口
- // PROTOTYPES /
- // 游戏控制台
- int Game_Init(void *parms=NULL);
- int Game_Shutdown(void *parms=NULL);
- int Game_Main(void *parms=NULL);
- // 全局变量
- HWND main_window_handle = NULL; //窗口句柄
- HINSTANCE main_instance = NULL; // 实例
- char buffer[256]; // 输出缓冲区
- // FUNCTIONS //
- LRESULT CALLBACK WindowProc(HWND hwnd,
- UINT msg,
- WPARAM wparam,
- LPARAM lparam)
- {
- PAINTSTRUCT ps;
- HDC hdc; // 设备环境句柄
- // 消息
- switch(msg)
- {
- case WM_CREATE:
- {
- // 初始化
- return(0);
- } break;
- case WM_PAINT:
- {
- // 开始绘制
- hdc = BeginPaint(hwnd,&ps);
- // 结束绘制
- EndPaint(hwnd,&ps);
- return(0);
- } break;
- case WM_DESTROY:
- {
- // 销毁程序
- PostQuitMessage(0);
- return(0);
- } break;
- default:break;
- }
- return (DefWindowProc(hwnd, msg, wparam, lparam));
- } // end WinProc
- // WINMAIN
- int WINAPI WinMain( HINSTANCE hinstance,
- HINSTANCE hprevinstance,
- LPSTR lpcmdline,
- int ncmdshow)
- {
- WNDCLASSEX winclass; // 窗口类
- HWND hwnd;
- MSG msg;
- HDC hdc;
- // 填充窗口类结构
- winclass.cbSize = sizeof(WNDCLASSEX);
- winclass.style = CS_DBLCLKS | CS_OWNDC |
- CS_HREDRAW | CS_VREDRAW;
- winclass.lpfnWndProc = WindowProc;
- winclass.cbClsExtra = 0;
- winclass.cbWndExtra = 0;
- winclass.hInstance = hinstance;
- winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
- winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
- winclass.lpszMenuName = NULL;
- winclass.lpszClassName = WINDOW_CLASS_NAME;
- winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
- // 注册窗口类
- if (!RegisterClassEx(&winclass))
- return(0);
- // 创建窗口
- if (!(hwnd = CreateWindowEx(NULL, // 扩展类型
- WINDOW_CLASS_NAME, // 类名
- WINDOW_TITLE, // 标题
- (WINDOWED_APP ? (WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION) : (WS_POPUP | WS_VISIBLE)),
- 0,0, // 初始窗口坐标
- WINDOW_WIDTH,WINDOW_HEIGHT, // 初始宽/高(px)
- NULL, // 父句柄
- NULL, // 菜单句柄
- hinstance,// 应用程序实例
- NULL)))
- return(0);
- // 存储窗口句柄与实例
- main_window_handle = hwnd;
- main_instance = hinstance;
- //窗口模式
- if (WINDOWED_APP)
- {
- // 定义客户区矩形
- RECT window_rect = {0,0,WINDOW_WIDTH-1,WINDOW_HEIGHT-1};
- // 调用此函数使窗口自适应上述矩形区域
- AdjustWindowRectEx(&window_rect,
- GetWindowStyle(main_window_handle),
- GetMenu(main_window_handle) != NULL,
- GetWindowExStyle(main_window_handle));
- // 客户区起始坐标
- window_client_x0 = -window_rect.left;
- window_client_y0 = -window_rect.top;
- // 重置窗口位置
- MoveWindow(main_window_handle,
- 0, // x position
- 0, // y position
- window_rect.right - window_rect.left, // width
- window_rect.bottom - window_rect.top, // height
- TRUE // 为TRUE则窗口需重绘
- );
- // 显示窗口
- ShowWindow(main_window_handle,
- SW_SHOW //激活窗口并以当前大小和位置显示它
- );
- } // end if windowed
- // 游戏初始化
- Game_Init();
- // 进入主循环
- while(1)
- {
- if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
- {
- if (msg.message == WM_QUIT)
- break;
- TranslateMessage(&msg);
- // 发送消息至回调函数!
- DispatchMessage(&msg);
- }
- // 游戏循环
- Game_Main();
- }
- // 关闭游戏,释放资源
- Game_Shutdown();
- return(msg.wParam);
- }
- // T3D II GAME PROGRAMMING CONSOLE FUNCTIONS
- int Game_Init(void *parms)
- {
- // 初始化DirectDraw
- DDraw_Init(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_BPP, WINDOWED_APP);
- DInput_Init();
- DInput_Init_Keyboard();
- // 全屏模式隐藏鼠标
- if (!WINDOWED_APP)
- ShowCursor(FALSE);
- // 取得随机数种子
- srand(Start_Clock());
- return(1);
- }
- ///
- int Game_Shutdown(void *parms)
- {
- //现在仅仅是释放DirectDraw资源,以后会释放其他相关的资源
- DInput_Release_Keyboard();
- DInput_Shutdown();
- DDraw_Shutdown();
- return(1);
- }
- //
- int Game_Main(void *parms)
- {
- int index; // 循环变量
- // 计时开始
- Start_Clock();
- // 初始化显示表面
- DDraw_Fill_Surface(lpddsback, 0);
- DInput_Read_Keyboard();
- // 游戏逻辑
- //表面替换
- DDraw_Flip();
- // 一帧 30 ms
- Wait_Clock(30);
- // 按Esc则退出游戏
- if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_SPACE])
- {
- PostMessage(main_window_handle, WM_DESTROY,0,0);
- }
- return(1);
- }