-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime_api.h
More file actions
188 lines (153 loc) · 5.25 KB
/
runtime_api.h
File metadata and controls
188 lines (153 loc) · 5.25 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#pragma once
/**
* Bit HCI Runtime API
*
* 这个文件定义了 Native 运行时提供给 Examples 组件的接口
* Examples 中的组件通过这些接口与底层渲染引擎交互
*/
namespace BitHCI {
// ============================================================================
// 窗口管理接口
// ============================================================================
/**
* 窗口管理接口
* 提供窗口尺寸、状态等信息
*/
class IWindow {
public:
virtual ~IWindow() = default;
// 获取窗口尺寸
virtual int getWidth() const = 0;
virtual int getHeight() const = 0;
// 窗口状态
virtual bool shouldClose() const = 0;
virtual bool isMinimized() const = 0;
virtual bool isFocused() const = 0;
// 窗口控制
virtual void setTitle(const char* title) = 0;
virtual void close() = 0;
};
// ============================================================================
// 渲染接口
// ============================================================================
/**
* 渲染接口
* 提供基础图形绘制能力
*/
class IRenderer {
public:
virtual ~IRenderer() = default;
// 帧控制
virtual void beginFrame() = 0;
virtual void endFrame() = 0;
virtual void setClearColor(float r, float g, float b, float a) = 0;
// 基础图形
virtual void drawTriangle(const float* vertices, const float* colors) = 0;
virtual void drawRectangle(float x, float y, float w, float h, const float* color) = 0;
virtual void drawLine(float x1, float y1, float x2, float y2, const float* color, float width = 1.0f) = 0;
virtual void drawCircle(float x, float y, float radius, const float* color) = 0;
// 文本渲染
virtual void drawText(const char* text, float x, float y, const float* color, float size = 16.0f) = 0;
// 纹理渲染
virtual void drawTexture(void* texture, float x, float y, float w, float h) = 0;
};
// ============================================================================
// 输入接口
// ============================================================================
/**
* 输入接口
* 提供键盘、鼠标输入查询
*/
class IInput {
public:
virtual ~IInput() = default;
// 键盘输入
virtual bool isKeyPressed(int keyCode) const = 0;
virtual bool isKeyJustPressed(int keyCode) const = 0;
virtual bool isKeyJustReleased(int keyCode) const = 0;
// 鼠标输入
virtual void getMousePosition(float& x, float& y) const = 0;
virtual bool isMouseButtonPressed(int button) const = 0;
virtual bool isMouseButtonJustPressed(int button) const = 0;
virtual float getMouseScrollDelta() const = 0;
};
// ============================================================================
// 资源管理接口
// ============================================================================
/**
* 资源管理接口
* 提供纹理、字体等资源加载
*/
class IResourceManager {
public:
virtual ~IResourceManager() = default;
// 纹理加载
virtual void* loadTexture(const char* filepath) = 0;
virtual void unloadTexture(void* texture) = 0;
// 字体加载
virtual void* loadFont(const char* filepath, float size) = 0;
virtual void unloadFont(void* font) = 0;
};
// ============================================================================
// 组件接口
// ============================================================================
/**
* 组件基类
* 所有 Examples 组件都应该继承此接口
*/
class IComponent {
public:
virtual ~IComponent() = default;
/**
* 组件初始化
* 在组件加载后调用一次
*/
virtual void onInit(IWindow* window,
IRenderer* renderer,
IInput* input,
IResourceManager* resources) = 0;
/**
* 组件更新
* 每帧调用,用于更新组件状态
* @param deltaTime 距离上一帧的时间(秒)
*/
virtual void onUpdate(float deltaTime) = 0;
/**
* 组件渲染
* 每帧调用,用于绘制组件
*/
virtual void onRender() = 0;
/**
* 组件销毁
* 在组件卸载前调用一次
*/
virtual void onDestroy() = 0;
/**
* 输入事件
* 可选实现,用于处理特殊输入
*/
virtual void onKeyEvent(int key, bool pressed) {}
virtual void onMouseEvent(int button, bool pressed, float x, float y) {}
};
// ============================================================================
// 组件工厂
// ============================================================================
/**
* 组件创建函数类型
* 每个组件 DLL 应该导出这个函数
*/
using ComponentFactory = IComponent* (*)();
} // namespace BitHCI
// ============================================================================
// 导出宏
// ============================================================================
#ifdef _WIN32
#define BITHCI_EXPORT extern "C" __declspec(dllexport)
#else
#define BITHCI_EXPORT extern "C"
#endif
// 组件必须实现这个函数
#define BITHCI_COMPONENT(ClassName) \
BITHCI_EXPORT BitHCI::IComponent* createComponent() { \
return new ClassName(); \
}