-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathline.cpp
More file actions
261 lines (218 loc) · 9.23 KB
/
line.cpp
File metadata and controls
261 lines (218 loc) · 9.23 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/**
* Line Component
*
* 线段渲染组件示例
* 演示不同角度、宽度、颜色和动画效果的线段
* 注:使用矩形模拟线段渲染
*/
#include "runtime_api.h"
#include <cmath>
#include <cstdio>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
class LineComponent : public BitHCI::IComponent {
private:
// 运行时接口
BitHCI::IRenderer* renderer = nullptr;
BitHCI::IInput* input = nullptr;
BitHCI::IWindow* window = nullptr;
// 动画参数
float time = 0.0f;
bool isPaused = false;
// 颜色预设
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};
float colorWhite[4] = {1.0f, 1.0f, 1.0f, 1.0f};
float colorGray[4] = {0.6f, 0.6f, 0.6f, 1.0f};
/**
* 绘制线段(封装 renderer 的 drawLine)
*/
void drawLineHelper(float x1, float y1, float x2, float y2, float width, float color[4]) {
renderer->drawLine(x1, y1, x2, y2, color, width);
}
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;
isPaused = false;
}
// 更新时间
if (!isPaused) {
time += deltaTime;
}
}
/**
* 组件渲染
*/
void onRender() override {
// 标题
renderer->drawText("Line Rendering Demo", -200, -150, colorWhite, 20.0f);
renderer->drawText("[Space] Pause [R] Reset [ESC] Quit",
-200, -130, colorGray, 12.0f);
// === 1. 不同宽度的线段 ===
renderer->drawText("Widths:", -200, -110, colorCyan, 14.0f);
drawLineHelper(-200, -80, -130, -80, 1, colorWhite);
drawLineHelper(-200, -70, -130, -70, 2, colorWhite);
drawLineHelper(-200, -60, -130, -60, 4, colorWhite);
drawLineHelper(-200, -50, -130, -50, 6, colorWhite);
drawLineHelper(-200, -40, -130, -40, 8, colorWhite);
// === 2. 不同角度的线段 ===
renderer->drawText("Angles:", -100, -110, colorCyan, 14.0f);
float centerX = -50.0f;
float centerY = -60.0f;
float lineLength = 40.0f;
for (int i = 0; i < 8; i++) {
float angle = i * M_PI / 4.0f;
float x1 = centerX;
float y1 = centerY;
float x2 = centerX + lineLength * std::cos(angle);
float y2 = centerY + lineLength * std::sin(angle);
drawLineHelper(x1, y1, x2, y2, 2, colorWhite);
}
// === 3. 不同颜色的线段 ===
renderer->drawText("Colors:", 50, -110, colorCyan, 14.0f);
drawLineHelper(50, -80, 150, -80, 3, colorRed);
drawLineHelper(50, -70, 150, -70, 3, colorGreen);
drawLineHelper(50, -60, 150, -60, 3, colorBlue);
drawLineHelper(50, -50, 150, -50, 3, colorYellow);
drawLineHelper(50, -40, 150, -40, 3, colorMagenta);
// === 4. 旋转动画 ===
renderer->drawText("Rotating:", -200, -10, colorCyan, 14.0f);
float rotCenterX = -150.0f;
float rotCenterY = 20.0f;
float rotRadius = 30.0f;
float rotAngle = time * 2.0f;
float rotX1 = rotCenterX + rotRadius * std::cos(rotAngle);
float rotY1 = rotCenterY + rotRadius * std::sin(rotAngle);
float rotX2 = rotCenterX - rotRadius * std::cos(rotAngle);
float rotY2 = rotCenterY - rotRadius * std::sin(rotAngle);
drawLineHelper(rotX1, rotY1, rotX2, rotY2, 4, colorYellow);
renderer->drawCircle(rotCenterX, rotCenterY, 3, colorRed);
// === 5. 时钟效果 ===
renderer->drawText("Clock:", -70, -10, colorCyan, 14.0f);
float clockX = -20.0f;
float clockY = 20.0f;
float clockRadius = 30.0f;
// 圆形外框
renderer->drawCircle(clockX, clockY, clockRadius + 2, colorGray);
renderer->drawCircle(clockX, clockY, clockRadius - 2, colorWhite);
// 秒针
float secondAngle = time * 2.0f - M_PI / 2.0f;
float secX = clockX + (clockRadius - 5) * std::cos(secondAngle);
float secY = clockY + (clockRadius - 5) * std::sin(secondAngle);
drawLineHelper(clockX, clockY, secX, secY, 1, colorRed);
// 分针
float minuteAngle = time * 0.2f - M_PI / 2.0f;
float minX = clockX + (clockRadius - 10) * std::cos(minuteAngle);
float minY = clockY + (clockRadius - 10) * std::sin(minuteAngle);
drawLineHelper(clockX, clockY, minX, minY, 2, colorGreen);
// 时针
float hourAngle = time * 0.05f - M_PI / 2.0f;
float hourX = clockX + (clockRadius - 15) * std::cos(hourAngle);
float hourY = clockY + (clockRadius - 15) * std::sin(hourAngle);
drawLineHelper(clockX, clockY, hourX, hourY, 3, colorBlue);
// 中心点
renderer->drawCircle(clockX, clockY, 3, colorWhite);
// === 6. 网格效果 ===
renderer->drawText("Grid:", 50, -10, colorCyan, 14.0f);
float gridStartX = 70.0f;
float gridStartY = 0.0f;
float gridSize = 60.0f;
int gridLines = 7;
float gridColor[4] = {0.4f, 0.4f, 0.5f, 0.5f};
for (int i = 0; i < gridLines; i++) {
float offset = i * 10.0f;
// 垂直线
drawLineHelper(gridStartX + offset, gridStartY,
gridStartX + offset, gridStartY + gridSize, 1, gridColor);
// 水平线
drawLineHelper(gridStartX, gridStartY + offset,
gridStartX + gridSize, gridStartY + offset, 1, gridColor);
}
// === 7. 波形效果 ===
renderer->drawText("Wave:", -200, 80, colorCyan, 14.0f);
float waveStartX = -200.0f;
float waveY = 110.0f;
float waveWidth = 180.0f;
int waveSegments = 30;
for (int i = 0; i < waveSegments - 1; i++) {
float t1 = static_cast<float>(i) / static_cast<float>(waveSegments - 1);
float t2 = static_cast<float>(i + 1) / static_cast<float>(waveSegments - 1);
float x1 = waveStartX + t1 * waveWidth;
float y1 = waveY + 15.0f * std::sin(t1 * M_PI * 4.0f + time * 3.0f);
float x2 = waveStartX + t2 * waveWidth;
float y2 = waveY + 15.0f * std::sin(t2 * M_PI * 4.0f + time * 3.0f);
// 彩虹颜色
float hue = t1 * 2.0f + 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 waveColor[4] = {r, g, b, 1.0f};
drawLineHelper(x1, y1, x2, y2, 2, waveColor);
}
// === 8. 星形图案 ===
renderer->drawText("Star:", 50, 80, colorCyan, 14.0f);
float starX = 120.0f;
float starY = 110.0f;
int starPoints = 5;
float outerRadius = 30.0f;
float innerRadius = 12.0f;
for (int i = 0; i < starPoints; i++) {
// 外点到外点(星形边缘)
float angle1 = i * 2.0f * M_PI / starPoints - M_PI / 2.0f + time * 0.5f;
float angle2 = (i + 1) * 2.0f * M_PI / starPoints - M_PI / 2.0f + time * 0.5f;
float x1 = starX + outerRadius * std::cos(angle1);
float y1 = starY + outerRadius * std::sin(angle1);
float x2 = starX + outerRadius * std::cos(angle2);
float y2 = starY + outerRadius * std::sin(angle2);
// 内点
float innerAngle = (i + 0.5f) * 2.0f * M_PI / starPoints - M_PI / 2.0f + time * 0.5f;
float innerX = starX + innerRadius * std::cos(innerAngle);
float innerY = starY + innerRadius * std::sin(innerAngle);
drawLineHelper(x1, y1, innerX, innerY, 2, colorYellow);
drawLineHelper(innerX, innerY, x2, y2, 2, colorYellow);
}
// 统计信息
char stats[128];
sprintf_s(stats, sizeof(stats), "Time: %.1fs", time);
renderer->drawText(stats, -200, 140, colorGray, 12.0f);
}
/**
* 组件销毁
*/
void onDestroy() override {
// 清理资源
}
};
// 导出组件
BITHCI_COMPONENT(LineComponent)