-
Notifications
You must be signed in to change notification settings - Fork 43
/
main_baremetal.c
81 lines (73 loc) · 2.18 KB
/
main_baremetal.c
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
#include <stdint.h>
#include <stdlib.h>
#include "fb.h"
#include "game.h"
#include "mem.h"
#include "raycaster_data.h"
#include "raycaster_fixed.h"
#include "renderer.h"
#include "timer.h"
#include "uart.h"
char *itoa(int value, char *str, int base);
void copy_buffer(uint32_t *fb, uint32_t *buffer)
{
for (uint16_t x = 0; x < SCREEN_WIDTH; ++x) {
for (uint16_t y = 0; y < SCREEN_HEIGHT; ++y) {
uint32_t color = buffer[y * SCREEN_WIDTH + x];
for (int i = 0; i < SCREEN_SCALE; ++i) {
for (int j = 0; j < SCREEN_SCALE; ++j) {
fb[(y * SCREEN_SCALE + j) * FB_WIDTH +
(x * SCREEN_SCALE + i)] = color;
}
}
}
}
}
void main()
{
uint32_t *buffer = malloc(SCREEN_WIDTH * SCREEN_HEIGHT * sizeof(uint32_t));
RayCaster *rayCaster = RayCasterFixedConstruct();
Game game = GameConstruct();
Renderer renderer = RendererConstruct(rayCaster);
uint32_t *fb = fb_create(FB_WIDTH, FB_HEIGHT, 32);
uint64_t tickCounter = timer_clock(), elapsed = 0;
int frameCounter = 0, frameRate = 0;
for (;;) {
RendererTraceFrame(&renderer, &game, buffer);
char fpsbuf[64] = "FPS: ";
itoa(frameRate, fpsbuf + 5, 10);
fb_puts(buffer, SCREEN_WIDTH, SCREEN_HEIGHT, g_font, fpsbuf, 0, 0);
copy_buffer(fb, buffer);
int m = 0, r = 0;
if (!uart_empty()) {
char c = uart_getc();
switch (c) {
case 'w':
m = 1;
break;
case 'a':
r = -1;
break;
case 's':
m = -1;
break;
case 'd':
r = 1;
break;
}
}
uint64_t nextCounter = timer_clock();
uint64_t ticks = nextCounter - tickCounter;
tickCounter = nextCounter;
elapsed += ticks;
++frameCounter;
if (elapsed > 1000000) {
frameRate = frameCounter;
frameCounter = 0;
elapsed -= 1000000;
}
GameMove(&game, m, r, ticks >> 12);
}
rayCaster->Destruct(rayCaster);
free(buffer);
}