-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4_performance.cpp
More file actions
434 lines (373 loc) · 13.6 KB
/
4_performance.cpp
File metadata and controls
434 lines (373 loc) · 13.6 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#include <iostream>
#include <fcntl.h>
#include <linux/fb.h>
#include <linux/input.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <vector>
#include <cstring>
#include <poll.h>
#include <time.h>
#if !defined(uint8_t)
#define uint8_t unsigned char
#endif
#if !defined(uint16_t)
#define uint16_t unsigned short
#endif
#if !defined(uint32_t)
#define uint32_t unsigned int
#endif
// 화면 크기
const int WIDTH = 1280;
const int HEIGHT = 720;
const int GROUND_LEVEL = HEIGHT - 60;
const int BOUND_GRAVITY = -10;
// 색상 정의
struct Color {
uint8_t r, g, b, a;
};
// 색상 상수
const Color SKY_BLUE = {135, 206, 235, 0};
const Color BROWN = {139, 69, 19, 0};
const Color RED = {255, 0, 0, 0};
const Color DARK_GREEN = {0, 100, 0, 0};
const Color DARK_GRAY = {169, 169, 169, 0};
const Color PLAYER_COLOR = RED;
const Color BLOCK_COLOR = DARK_GRAY;
// #define USE_FIXEL_FORMAT_32
#if defined(USE_FIXEL_FORMAT_32)
#define FIXEL_FORMAT uint32_t
#define ARGB8888
// #define RGBA8888
#else
#define FIXEL_FORMAT uint16_t
#endif
// 32비트 색상을 16비트 RGB565로 변환
FIXEL_FORMAT convertTo(Color color) {
#if defined(USE_FIXEL_FORMAT_32)
#if defined(RGBA8888)
return (color.r << 24) | (color.g << 16) | (color.b << 8) | color.a;
#elif defined(ARGB8888)
return (color.a << 24) | (color.r << 16) | (color.g << 8) | color.b;
#else
#err
#endif
#else
return ((color.r & 0xF8) << 8) | ((color.g & 0xFC) << 3) | (color.b >> 3);
#endif
}
void fillRect(uint8_t* fb_ptr, fb_var_screeninfo vinfo, fb_fix_screeninfo finfo, int x, int y, int w, int h, FIXEL_FORMAT color) {
for (int j = 0; j < h; ++j) {
for (int i = 0; i < w; ++i) {
int px = x + i;
int py = y + j;
if (px < 0 || px >= WIDTH || py < 0 || py >= HEIGHT) {
continue;
}
long location = (px + vinfo.xoffset) * (vinfo.bits_per_pixel / 8) +
(py + vinfo.yoffset) * finfo.line_length;
*((FIXEL_FORMAT*)(fb_ptr + location)) = color;
}
}
}
// 화면 업데이트 함수
void updateRect(uint8_t* fb_ptr, uint8_t* buffer_ptr, fb_var_screeninfo vinfo, fb_fix_screeninfo finfo, int x, int y, int w, int h) {
for (int j = 0; j < h; ++j) {
for (int i = 0; i < w; ++i) {
int px = x + i;
int py = y + j;
if (px < 0 || px >= WIDTH || py < 0 || py >= HEIGHT) {
continue;
}
long location = (px + vinfo.xoffset) * (vinfo.bits_per_pixel / 8) +
(py + vinfo.yoffset) * finfo.line_length;
*((FIXEL_FORMAT*)(fb_ptr + location)) = *((FIXEL_FORMAT*)(buffer_ptr + location));
}
}
}
void updateScreen(uint8_t* fb_ptr, uint8_t* buffer_ptr, fb_var_screeninfo vinfo, fb_fix_screeninfo finfo) {
updateRect(fb_ptr, buffer_ptr, vinfo, finfo, 0, 0, WIDTH, HEIGHT);
}
// 유닛 클래스
class Unit {
protected:
int x, y;
public:
Unit(int startX, int startY) : x(startX), y(startY) {}
virtual void draw(uint8_t* fb_ptr, fb_var_screeninfo vinfo, fb_fix_screeninfo finfo) = 0;
virtual void move(int dx) {
x += dx;
}
virtual void setY(int targetY) {
y = targetY;
}
int getX() const { return x; }
int getY() const { return y; }
};
// 플레이어 클래스
class Player : public Unit {
private:
// y중력 가속도
int gravity = 1;
public:
const int width = 10;
const int height = 10;
Player(int startX, int startY) : Unit(startX, startY) {}
void draw(uint8_t* fb_ptr, fb_var_screeninfo vinfo, fb_fix_screeninfo finfo) override {
// 간단한 사각형으로 플레이어 그리기
FIXEL_FORMAT playerColor = convertTo(PLAYER_COLOR);
fillRect(fb_ptr, vinfo, finfo, x, y, width, height, playerColor);
}
void remove(uint8_t* fb_ptr, fb_var_screeninfo vinfo, fb_fix_screeninfo finfo) {
FIXEL_FORMAT playerColor = convertTo(SKY_BLUE);
fillRect(fb_ptr, vinfo, finfo, x, y, width, height, playerColor);
}
void moveTo(int targetX) {
x = targetX;
}
int getGravity() {
return this->gravity;
}
void setGravity(int g) {
this->gravity = g;
}
};
enum CrashCode {
NONE = 0,
TOP = 1,
BOTTOM = 2,
LEFT = 3,
RIGHT = 4,
};
class Block: public Unit {
private:
public:
int width, height;
Block(int startX, int startY, int w, int h) : Unit(startX, startY), width(w), height(h) {}
void draw(uint8_t* fb_ptr, fb_var_screeninfo vinfo, fb_fix_screeninfo finfo) override {
FIXEL_FORMAT blockColor = convertTo(BLOCK_COLOR);
fillRect(fb_ptr, vinfo, finfo, x, y, width, height, blockColor);
}
CrashCode checkCrash(Player player) {
if (player.getX() + player.width >= x && player.getX() <= x + width) {
if (player.getY() + player.height >= y && player.getY() <= y + height) {
if (player.getY() + player.height >= y && player.getY() + player.height <= y + height) {
return TOP;
}
if (player.getY() >= y && player.getY() <= y + height) {
return BOTTOM;
}
if (player.getX() + player.width >= x && player.getX() + player.width <= x + width) {
return LEFT;
}
if (player.getX() >= x && player.getX() <= x + width) {
return RIGHT;
}
}
}
return NONE;
}
};
// 배경 색상 채우기 함수
void fillBackground(uint8_t* fb_ptr, fb_var_screeninfo vinfo, fb_fix_screeninfo finfo, Color color) {
FIXEL_FORMAT colorData = convertTo(color);
fillRect(fb_ptr, vinfo, finfo, 0, 0, WIDTH, HEIGHT, colorData);
}
// 땅 색상 채우기 함수
void fillGround(uint8_t* fb_ptr, fb_var_screeninfo vinfo, fb_fix_screeninfo finfo, Color color) {
FIXEL_FORMAT colorData = convertTo(color);
fillRect(fb_ptr, vinfo, finfo, 0, HEIGHT - 50, WIDTH, 50, colorData);
}
// 입력 장치 열기
int openInputDevice(const std::string& device) {
printf("openInputDevice: %s\n", device.c_str());
int fd = open(device.c_str(), O_RDONLY);
if (fd == -1) {
std::cerr << "Error: cannot open input device " << device << "." << std::endl;
return -1;
}
return fd;
}
#include <termios.h>
void disableInputEcho() {
printf("disableInputEcho\n");
struct termios tty;
tcgetattr(STDIN_FILENO, &tty); // 현재 터미널 속성 가져오기
tty.c_lflag &= ~ECHO; // ECHO 플래그를 끄기
tcsetattr(STDIN_FILENO, TCSANOW, &tty); // 변경된 속성 설정
}
void enableInputEcho() {
printf("enableInputEcho\n");
struct termios tty;
tcgetattr(STDIN_FILENO, &tty); // 현재 터미널 속성 가져오기
tty.c_lflag |= ECHO; // ECHO 플래그를 켜기
tcsetattr(STDIN_FILENO, TCSANOW, &tty); // 변경된 속성 설정
}
int main() {
atexit(enableInputEcho);
disableInputEcho();
// 프레임버퍼 장치 열기
int fb_fd = open("/dev/fb0", O_RDWR);
if (fb_fd == -1) {
std::cerr << "Error: cannot open framebuffer device." << std::endl;
return 1;
}
// 가변 화면 정보 가져오기
fb_var_screeninfo vinfo;
if (ioctl(fb_fd, FBIOGET_VSCREENINFO, &vinfo)) {
std::cerr << "Error reading variable information." << std::endl;
close(fb_fd);
return 1;
}
// 고정 화면 정보 가져오기
fb_fix_screeninfo finfo;
if (ioctl(fb_fd, FBIOGET_FSCREENINFO, &finfo)) {
std::cerr << "Error reading fixed information." << std::endl;
close(fb_fd);
return 1;
}
// 화면 크기 계산
long screensize = vinfo.yres_virtual * finfo.line_length * 2;
printf("width = %d, height = %d, xres_virtual = %d, yres_virtual = %d\n",
vinfo.xres, vinfo.yres, vinfo.xres_virtual, vinfo.yres_virtual);
printf("screensize = %ld\n", screensize);
printf("bits_per_pixel = %d\n", vinfo.bits_per_pixel);
// 메모리 매핑
uint8_t* fb_ptr = (uint8_t*)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fb_fd, 0);
if ((intptr_t)fb_ptr == -1) {
std::cerr << "Error: failed to map framebuffer device to memory." << std::endl;
close(fb_fd);
return 1;
}
uint8_t* buffer_ptr = (uint8_t*)malloc(screensize);
// 입력 장치 파일 열기
int keyboard_fd = -1;
// 키보드 파일 찾기
for (int eventid = 0; eventid < 32; ++eventid) {
std::string device = "/dev/input/event" + std::to_string(eventid);
int fd = openInputDevice(device);
if (fd != -1) {
keyboard_fd = fd;
int flags = fcntl(keyboard_fd, F_GETFL, 0);
fcntl(keyboard_fd, F_SETFL, flags | O_NONBLOCK);
break;
}
}
if (
keyboard_fd == -1
) {
munmap(fb_ptr, screensize);
close(fb_fd);
return 1;
}
// 플레이어 초기화
Player player(100, GROUND_LEVEL);
std::vector<Block> blocks;
for (int i = 0; i < 10; i++) {
int x = 120 + i * 100;
int y = (HEIGHT - 80) - 20 * i;
blocks.push_back(Block(x, y, 50, 10));
}
// 키 상태를 저장할 플래그
bool key_left_pressed = false;
bool key_right_pressed = false;
// pollfd 구조체 설정
struct pollfd fds;
fds.fd = keyboard_fd;
fds.events = POLLIN;
// 이벤트 루프
bool running = true;
fillBackground(buffer_ptr, vinfo, finfo, SKY_BLUE);
fillGround(buffer_ptr, vinfo, finfo, BROWN);
updateScreen(fb_ptr, buffer_ptr, vinfo, finfo);
while (running) {
struct input_event ev;
// poll 함수를 사용하여 키보드 이벤트 폴링
int ret = poll(&fds, 1, 1);
if (ret > 0) {
if (fds.revents & POLLIN) {
if (read(keyboard_fd, &ev, sizeof(ev)) > 0) {
if (ev.type == EV_KEY) {
if (ev.value == 1) { // 키가 눌림
switch (ev.code) {
case KEY_LEFT:
key_left_pressed = true;
break;
case KEY_RIGHT:
key_right_pressed = true;
break;
case KEY_ESC:
running = false;
break;
}
} else if (ev.value == 0) { // 키가 떼어짐
switch (ev.code) {
case KEY_LEFT:
key_left_pressed = false;
break;
case KEY_RIGHT:
key_right_pressed = false;
break;
}
}
}
}
}
}
// 키 상태에 따라 플레이어 이동
int moveVal = 0;
if (key_left_pressed) {
moveVal -= 5;
}
if (key_right_pressed) {
moveVal += 5;
}
player.remove(buffer_ptr, vinfo, finfo);
updateRect(fb_ptr, buffer_ptr, vinfo, finfo, player.getX(), player.getY(), player.width, player.height);
player.move(moveVal);
// printf("gravity: %d\n", player.getGravity());
player.setGravity(player.getGravity() + 1);
player.setY(player.getY() + player.getGravity());
if (player.getY() >= GROUND_LEVEL) {
player.setY(GROUND_LEVEL);
player.setGravity(BOUND_GRAVITY);
}
for (Block block : blocks) {
block.draw(buffer_ptr, vinfo, finfo);
updateRect(fb_ptr, buffer_ptr, vinfo, finfo, block.getX(), block.getY(), block.width, block.height);
CrashCode code = block.checkCrash(player);
// if (code != 0)
// printf("code : %d\n", code);
switch (code) {
case TOP:
player.setGravity(BOUND_GRAVITY);
player.setY(block.getY() - player.height);
break;
case BOTTOM:
player.setGravity(0);
player.setY(block.getY() + block.height);
break;
case LEFT:
player.move(block.getX() - player.width);
break;
case RIGHT:
player.move(block.getX() + block.width);
break;
}
}
// 플레이어 그리기
player.draw(buffer_ptr, vinfo, finfo);
updateRect(fb_ptr, buffer_ptr, vinfo, finfo, player.getX(), player.getY(), player.width, player.height);
// player.update(fb_ptr, vinfo, finfo);
// updateScreen(fb_ptr, buffer_ptr, vinfo, finfo);
// 간단한 지연
usleep(16000); // 약 60 FPS
}
// 메모리 매핑 해제 및 파일 닫기
munmap(fb_ptr, screensize);
free(buffer_ptr);
close(fb_fd);
close(keyboard_fd);
return 0;
}