好久不见 … 怀念在日本的时光

游戏开发基本框架

  1. 主循环
  2. 事件循环
  3. 批量绘图
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<graphics.h>

int main() {

initgraph(1280, 720);

bool running = true;

ExMessage msg;

BeginBatchDraw();

while (running) {
DWORD start_time = GetTickCount();

while (peekmessage(&msg)) {

}

cleardevice();
FlushBatchDraw();

DWORD end_time = GetTickCount();
DWORD delta_time = end_time - start_time;
if (delta_time < 1000 / 144) {
sleep(1000 / 144 - delta_time);
}
}

EndBatchDraw();

return 0;
}
  • 代码已经加入动态延时

数据类型

基础数据类型

用于精确控制内存、性能

这些是 C++ 游戏中经常用来表示固定大小的整型或浮点型,便于和显卡、音频、网络打交道:

类型 大小 含义 典型用途
int8_t / uint8_t 1 字节 有符号/无符号 8位整型 像素值、状态标记
int16_t / uint16_t 2 字节 16位整型 音频样本、ID
int32_t / uint32_t 4 字节 32位整型 游戏逻辑 ID、时间戳、分数
int64_t / uint64_t 8 字节 64位整型 精确时间、全局唯一标识
float 4 字节 单精度浮点 坐标、速度、缩放、旋转角度
double 8 字节 双精度浮点 需要更高精度的位置/计算
  • 来自头文件:#include <cstdint>

Windows / 系统相关类型

WinAPI、EasyX 中常见

类型 等价 说明 来源
BYTE unsigned char 8位字节数据 <windows.h>
WORD unsigned short 16位无符号 Windows
DWORD unsigned long 32位无符号 Windows
LPVOID void* 任意指针 Windows
TCHAR charwchar_t 字符类型,兼容 ANSI/Unicode <tchar.h>
LPTSTR char* / wchar_t* 字符串指针(Windows宏) 字符串处理

这些类型让程序在不同平台/字符集间有更好的兼容性。

图形库中的结构体

EasyX / WinAPI / SDL

类型 来自 用法
IMAGE EasyX 用于加载/绘制图片
RECT Windows API 定义矩形区域(坐标)
POINT Windows API 表示二维坐标 (x, y)
COLORREF WinAPI 表示颜色值(RGB)

容器和 STL 类型

用于逻辑组织

类型 来自 用法
std::vector<T> <vector> 存放动态数组,如帧图列表
std::map<K,V> <map> 存放键值对,如对象索引表
std::string / std::wstring <string> 文本处理
size_t <cstddef> 表示数组/容器大小,无符号

现代 C++ 实用类型

可选了解

类型 来自 用法
std::shared_ptr<T> <memory> 智能指针,自动管理内存
std::function<> <functional> 存储回调函数
std::chrono::milliseconds <chrono> 精确控制动画/节奏