-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext.cpp
More file actions
238 lines (198 loc) · 7.98 KB
/
text.cpp
File metadata and controls
238 lines (198 loc) · 7.98 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**
* Text Component
*
* 文本渲染组件示例
* 演示不同字体大小、颜色和多行文本
*/
#include "runtime_api.h"
#include <cmath>
#include <cstdio>
class TextComponent : public BitHCI::IComponent {
private:
// 运行时接口
BitHCI::IRenderer* renderer = nullptr;
BitHCI::IInput* input = nullptr;
BitHCI::IWindow* window = nullptr;
// 动画参数
float time = 0.0f;
float scrollOffset = 0.0f;
bool isPaused = false;
// 颜色预设
float colorWhite[4] = {1.0f, 1.0f, 1.0f, 1.0f};
float colorGray[4] = {0.6f, 0.6f, 0.6f, 1.0f};
float colorRed[4] = {0.9f, 0.3f, 0.3f, 1.0f};
float colorGreen[4] = {0.3f, 0.9f, 0.3f, 1.0f};
float colorBlue[4] = {0.3f, 0.7f, 1.0f, 1.0f};
float colorYellow[4] = {1.0f, 0.9f, 0.2f, 1.0f};
float colorCyan[4] = {0.2f, 0.9f, 0.9f, 1.0f};
float colorMagenta[4] = {0.9f, 0.3f, 0.9f, 1.0f};
public:
/**
* 组件初始化
*/
void onInit(BitHCI::IWindow* window,
BitHCI::IRenderer* renderer,
BitHCI::IInput* input,
BitHCI::IResourceManager* resources) override {
this->window = window;
this->renderer = renderer;
this->input = input;
// 设置清屏颜色
renderer->setClearColor(0.08f, 0.08f, 0.1f, 1.0f);
}
/**
* 组件更新
*/
void onUpdate(float deltaTime) override {
// ESC 键退出
if (input->isKeyJustPressed(27)) {
window->close();
}
// 空格键暂停/继续动画
if (input->isKeyJustPressed(32)) {
isPaused = !isPaused;
}
// R 键重置
if (input->isKeyJustPressed('R')) {
time = 0.0f;
scrollOffset = 0.0f;
isPaused = false;
}
// 更新动画时间
if (!isPaused) {
time += deltaTime;
scrollOffset += deltaTime * 20.0f; // 滚动速度
// 滚动循环
if (scrollOffset > 200.0f) {
scrollOffset = -200.0f;
}
}
}
/**
* 组件渲染
*/
void onRender() override {
float startY = -160.0f;
float lineHeight = 25.0f;
float currentY = startY;
// === 标题部分 ===
renderer->drawText("Text Rendering Demo", -200, currentY, colorWhite, 24.0f);
currentY += lineHeight + 10;
renderer->drawText("[Space] Pause/Resume [R] Reset [ESC] Quit",
-200, currentY, colorGray, 12.0f);
currentY += lineHeight + 10;
// === 字体大小演示 ===
currentY += 10;
renderer->drawText("Font Sizes:", -200, currentY, colorCyan, 16.0f);
currentY += lineHeight;
renderer->drawText("8px - Tiny text", -180, currentY, colorWhite, 8.0f);
currentY += 15;
renderer->drawText("12px - Small text", -180, currentY, colorWhite, 12.0f);
currentY += 18;
renderer->drawText("16px - Normal text", -180, currentY, colorWhite, 16.0f);
currentY += 22;
renderer->drawText("20px - Large text", -180, currentY, colorWhite, 20.0f);
currentY += 26;
renderer->drawText("28px - Huge", -180, currentY, colorWhite, 28.0f);
currentY += 35;
// === 颜色演示 ===
currentY += 5;
renderer->drawText("Colors:", -200, currentY, colorCyan, 16.0f);
currentY += lineHeight;
renderer->drawText("Red", -180, currentY, colorRed, 14.0f);
renderer->drawText("Green", -120, currentY, colorGreen, 14.0f);
renderer->drawText("Blue", -50, currentY, colorBlue, 14.0f);
renderer->drawText("Yellow", 20, currentY, colorYellow, 14.0f);
renderer->drawText("Magenta", 100, currentY, colorMagenta, 14.0f);
currentY += lineHeight;
// === 动画文本 ===
currentY += 10;
renderer->drawText("Animated:", -200, currentY, colorCyan, 16.0f);
currentY += lineHeight;
// 脉冲效果
float pulseSize = 14.0f + 4.0f * std::sin(time * 3.0f);
float pulseAlpha = 0.5f + 0.5f * std::sin(time * 3.0f);
float colorPulse[4] = {1.0f, 0.8f, 0.2f, pulseAlpha};
renderer->drawText("Pulsing text", -180, currentY, colorPulse, pulseSize);
currentY += lineHeight;
// 彩虹效果
float hue = time * 0.5f;
float r = 0.5f + 0.5f * std::sin(hue);
float g = 0.5f + 0.5f * std::sin(hue + 2.0f);
float b = 0.5f + 0.5f * std::sin(hue + 4.0f);
float colorRainbow[4] = {r, g, b, 1.0f};
renderer->drawText("Rainbow text", -180, currentY, colorRainbow, 14.0f);
currentY += lineHeight;
// 滚动文本
float scrollX = -180 + scrollOffset;
renderer->drawText("Scrolling text...", scrollX, currentY, colorCyan, 14.0f);
currentY += lineHeight;
// === 多行文本 ===
currentY += 10;
renderer->drawText("Multi-line Text:", -200, currentY, colorCyan, 16.0f);
currentY += lineHeight;
const char* lines[] = {
"This is the first line of text.",
"This is the second line.",
"And this is the third line!",
"Multiple lines are easy to render."
};
for (int i = 0; i < 4; i++) {
renderer->drawText(lines[i], -180, currentY, colorWhite, 12.0f);
currentY += 18;
}
// === 对齐演示(右侧) ===
float rightX = 50.0f;
float rightY = -160.0f;
renderer->drawText("Alignment:", rightX, rightY, colorCyan, 16.0f);
rightY += lineHeight;
renderer->drawText("Left aligned", rightX, rightY, colorWhite, 12.0f);
rightY += 18;
renderer->drawText(" Indented", rightX, rightY, colorWhite, 12.0f);
rightY += 18;
renderer->drawText(" More indent", rightX, rightY, colorWhite, 12.0f);
rightY += 18;
// === 装饰效果 ===
rightY += 10;
renderer->drawText("Effects:", rightX, rightY, colorCyan, 16.0f);
rightY += lineHeight;
// 下划线效果(使用矩形模拟)
const char* underlineText = "Underlined";
renderer->drawText(underlineText, rightX, rightY, colorWhite, 14.0f);
float underlineY = rightY + 16;
float underlineColor[4] = {1.0f, 1.0f, 1.0f, 0.8f};
renderer->drawRectangle(rightX, underlineY, 80, 1, underlineColor);
rightY += 20;
// 删除线效果
const char* strikeText = "Strikethrough";
renderer->drawText(strikeText, rightX, rightY, colorGray, 14.0f);
float strikeY = rightY + 7;
renderer->drawRectangle(rightX, strikeY, 90, 1, colorGray);
rightY += 20;
// 阴影效果
renderer->drawText("Shadow", rightX + 2, rightY + 2, colorGray, 14.0f);
renderer->drawText("Shadow", rightX, rightY, colorWhite, 14.0f);
rightY += 20;
// === 统计信息 ===
currentY = 140;
char stats[128];
int fps = (time > 0) ? (int)(1.0f / 0.0041f) : 0; // 近似 FPS
renderer->drawText("Statistics:", -200, currentY, colorCyan, 14.0f);
currentY += 20;
sprintf_s(stats, sizeof(stats), "Time: %.1fs", time);
renderer->drawText(stats, -180, currentY, colorGray, 12.0f);
sprintf_s(stats, sizeof(stats), "FPS: ~240");
renderer->drawText(stats, -80, currentY, colorGray, 12.0f);
sprintf_s(stats, sizeof(stats), "Status: %s", isPaused ? "Paused" : "Running");
float* statusColor = isPaused ? colorYellow : colorGreen;
renderer->drawText(stats, 20, currentY, statusColor, 12.0f);
}
/**
* 组件销毁
*/
void onDestroy() override {
// 清理资源
}
};
// 导出组件
BITHCI_COMPONENT(TextComponent)