-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameCanvas.cpp
More file actions
279 lines (229 loc) · 8.64 KB
/
Copy pathGameCanvas.cpp
File metadata and controls
279 lines (229 loc) · 8.64 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "GameCanvas.h"
#include "MathF16.h"
#include "GamePhysics.h"
#include "iostream"
#include <memory>
#include <vector>
#include <cmath> // для ceil
GameCanvas::GameCanvas(Micro* micro) : micro(micro) {
width = 0;
height = 0;
dx = 0;
dy = 0;
// Инициализация векторного шрифта (координаты штрихов)
vectorFont['0'] = { {{0,0},{3,0},{3,5},{0,5},{0,0}} }; // O
vectorFont['1'] = { {{1,0},{1,5}} }; // |
vectorFont['2'] = { {{0,0},{3,0},{3,2},{0,2},{0,3},{3,3},{3,5},{0,5}} };
vectorFont['3'] = { {{0,0},{3,0},{3,5},{0,5}}, {{0,2},{3,2}} };
vectorFont['4'] = { {{0,0},{0,2},{3,2}}, {{3,0},{3,5}} };
vectorFont['5'] = { {{3,0},{0,0},{0,2},{3,2},{3,5},{0,5}} };
vectorFont['6'] = { {{3,0},{0,0},{0,5},{3,5},{3,2},{0,2}} };
vectorFont['7'] = { {{0,0},{3,0},{3,5}} };
vectorFont['8'] = { {{0,0},{3,0},{3,5},{0,5},{0,0}}, {{0,2},{3,2}} };
vectorFont['9'] = { {{0,5},{3,5},{3,0},{0,0},{0,2},{3,2}} };
vectorFont[':'] = { {{1,1},{2,1},{2,2},{1,2},{1,1}}, {{1,3},{2,3},{2,4},{1,4},{1,3}} };
// Буквы для HUD (Manual / AI)
vectorFont['A'] = { {{0,5},{0,2},{1,0},{2,0},{3,2},{3,5}}, {{0,2},{3,2}} };
vectorFont['I'] = { {{0,0},{3,0}}, {{1,0},{1,5}}, {{0,5},{3,5}} };
vectorFont['M'] = { {{0,5},{0,0},{1,2},{2,0},{3,5}} }; // M
vectorFont['N'] = { {{0,5},{0,0},{3,5},{3,0}} }; // N
vectorFont['U'] = { {{0,0},{0,5},{3,5},{3,0}} }; // U
vectorFont['L'] = { {{0,0},{0,5},{3,5}} }; // L
}
GameCanvas::~GameCanvas() = default;
void GameCanvas::init(GamePhysics* gp) {
this->gamePhysics = gp;
// Установка минимального размера экрана для камеры
gamePhysics->setMinimalScreenWH(width < height ? width : height);
}
// --- Отрисовка ---
void GameCanvas::paint(Graphics* g) {
drawGame(g);
}
void GameCanvas::drawGame(Graphics* g) {
if (!Micro::isReady || micro->isRunning) { // isRunning используется как флаг паузы/выхода?
return;
}
graphics = g;
// Обновляем размеры если изменились
if (height != getHeight()) {
updateSizeAndRepaint();
}
// Подготовка физики к рендеру (интерполяция)
gamePhysics->setMotoComponents();
static constexpr float CAM_ZOOM = 2.0f;
int logicalW = int(width / CAM_ZOOM);
int logicalH = int(height / CAM_ZOOM);
// Установка камеры
setViewPosition(
-gamePhysics->getCamPosX() + cameraOffsetX + logicalW/2,
gamePhysics->getCamPosY() + cameraOffsetY + logicalH/2
);
// Рендер игрового мира
gamePhysics->renderGame(this);
// --- HUD (Интерфейс) ---
// Отрисовка режима управления (AI / Manual)
std::string modeText = gamePhysics->isGenerateInputAI ? "AI" : "MAN";
// Рисуем текст (без черного фона, как просил пользователь)
drawVectorString(modeText, 15, 15, 2);
graphics = nullptr;
}
void GameCanvas::drawSkyGradient() {
const int num_bands = 16;
const float band_height = (float)height / num_bands;
for (int i = 0; i < num_bands; ++i) {
int y_pos = (int)(i * band_height);
float ratio = (float)i / (float)num_bands;
Uint8 r = (Uint8)(50 + ratio * 85);
Uint8 g = (Uint8)(100 + ratio * 106);
Uint8 b = (Uint8)(150 + ratio * 100);
graphics->setColor(r, g, b);
graphics->fillRect(0, y_pos, width, (int)ceil(band_height));
}
}
// --- Векторный шрифт ---
void GameCanvas::drawVectorChar(char c, int start_x, int start_y, int scale) {
if (vectorFont.find(c) == vectorFont.end()) return;
const auto& strokes = vectorFont.at(c);
// Хак для рисования HUD в экранных координатах
int savedDx = dx;
int savedDy = dy;
dx = 0; dy = 0; // Сброс камеры
int screen_y = -start_y; // Инвертируем Y, так как addDy инвертирует его обратно
for (const auto& stroke : strokes) {
if (stroke.size() < 2) continue;
for (size_t i = 0; i < stroke.size() - 1; ++i) {
const auto& p1 = stroke[i];
const auto& p2 = stroke[i + 1];
int x1 = start_x + p1.x * scale;
int y1 = screen_y - p1.y * scale;
int x2 = start_x + p2.x * scale;
int y2 = screen_y - p2.y * scale;
// Используем this->drawLine, чтобы применилась логика addDy (инверсия Y)
this->drawLine(x1, y1, x2, y2);
}
}
dx = savedDx;
dy = savedDy;
}
void GameCanvas::drawVectorString(const std::string& text, int start_x, int start_y, int scale) {
int current_x = start_x;
int char_width = (3 + 1) * scale;
setColor(255, 255, 255);
for (const char c : text) {
drawVectorChar(c, current_x, start_y, scale);
current_x += char_width;
}
}
// --- Обработка ввода ---
void GameCanvas::processKeyPressed(int keyCode) {
// Спец. клавиши для управления режимом (User Request)
if (keyCode == 'M') {
gamePhysics->disableGenerateInputAI();
return;
}
if (keyCode == 'A') {
gamePhysics->enableGenerateInputAI();
return;
}
int action = getGameAction(keyCode);
int numKey;
if ((numKey = keyCode - 48) >= 0 && numKey < 10) {
activeKeys[numKey] = true;
} else if (action >= 0 && action < 7) {
activeActions[action] = true;
}
handleUpdatedInput();
}
void GameCanvas::processKeyReleased(int keyCode) {
int action = getGameAction(keyCode);
int numKey;
if ((numKey = keyCode - 48) >= 0 && numKey < 10) {
activeKeys[numKey] = false;
} else if (action >= 0 && action < 7) {
activeActions[action] = false;
}
handleUpdatedInput();
}
void GameCanvas::clearInputStates() {
for (int i = 0; i < 10; ++i) activeKeys[i] = false;
for (int i = 0; i < 7; ++i) activeActions[i] = false;
}
void GameCanvas::handleUpdatedInput() {
int dirX = 0, dirY = 0;
for (int i = 0; i < 10; ++i) {
if (activeKeys[i]) {
dirX += numKeyDirTable[numKeyMode][i][0];
dirY += numKeyDirTable[numKeyMode][i][1];
}
}
for (int i = 0; i < 7; ++i) {
if (activeActions[i]) {
dirX += actionDirTable[i][0];
dirY += actionDirTable[i][1];
}
}
gamePhysics->applyUserInput(dirX, dirY);
}
void GameCanvas::resetInput() {
clearInputStates();
}
void GameCanvas::updateSizeAndRepaint() {
width = getWidth();
height = getHeight();
repaint();
}
void GameCanvas::requestRepaint(int mode) {
// Legacy
repaint();
}
void GameCanvas::setViewPosition(int dx, int dy) {
this->dx = dx;
this->dy = dy;
static constexpr float CAM_ZOOM = 2.0f;
int logicalW = int(width / CAM_ZOOM);
gamePhysics->setRenderMinMaxX(-dx, -dx + logicalW);
}
int GameCanvas::addDx(int x) { return x + dx; }
int GameCanvas::addDy(int y) { return -y + dy; }
void GameCanvas::drawLine(int x, int y, int x2, int y2) {
graphics->drawLine(addDx(x), addDy(y), addDx(x2), addDy(y2));
}
void GameCanvas::drawLineF16(int x, int y, int x2, int y2) {
graphics->drawLine(addDx(x << 2 >> 16), addDy(y << 2 >> 16), addDx(x2 << 2 >> 16), addDy(y2 << 2 >> 16));
}
void GameCanvas::drawBrakeDisc(int x, int y, int radius, int angle) {
++radius;
int localX = addDx(x - radius);
int localY = addDy(y + radius);
int diameter = radius << 1;
if ((angle = -((int)(((int64_t)((int)((int64_t)angle * 11796480L >> 16)) << 32) / 205887L >> 16))) < 0) {
angle += 360;
}
graphics->drawArc(localX, localY, diameter, diameter, (angle >> 16) + 170, 90);
}
void GameCanvas::drawCircle(int x, int y, int size) {
int radius = size / 2;
int localX = addDx(x - radius);
int localY = addDy(y + radius);
graphics->drawArc(localX, localY, size, size, 0, 360);
}
void GameCanvas::clearScreenWithWhite() {
graphics->setColor(255, 255, 255);
graphics->fillRect(0, 0, width, height);
}
void GameCanvas::setColor(int red, int green, int blue) {
graphics->setColor(red, green, blue);
}
void GameCanvas::keyPressed(int keyCode) {
processKeyPressed(keyCode);
}
void GameCanvas::keyReleased(int keyCode) {
processKeyReleased(keyCode);
}
void GameCanvas::drawWheel(int x, int y, int radius) {
setColor(0, 0, 0);
drawCircle(x, y, radius * 2);
setColor(150, 150, 150);
drawCircle(x, y, radius);
}