forked from Dhole/miniBoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
158 lines (141 loc) · 2.65 KB
/
main.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
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <SDL2/SDL.h>
#include "main.h"
#include "dmg.h"
#include "screen.h"
#include "keypad.h"
#include "sdl.h"
Uint32 start_time, last_time, elapsed_time;
Uint32 scale;
int pause_emu;
int debug_flag, debug_pause;
void usage(char *argv0) {
printf("Usage: %s [-v] [-d] [-b bios] rom\n", argv0);
exit(1);
}
event_t process_event(SDL_Event *e) {
if (e->type == SDL_QUIT) {
return 1;
}
if (!e->key.repeat) {
if (e->type == SDL_KEYDOWN) {
switch (e->key.keysym.sym) {
case KEY_PAUSE:
pause_emu = pause_emu ^ 1;
break;
case KEY_RESET:
return E_RESET;
break;
case KEY_ENTER_DEBUG:
debug_flag = 1;
debug_pause = 1;
break;
case KEY_SCALE_1:
if (scale > 1) {
scale -= 1;
sdl_change_scale(scale);
}
break;
case KEY_SCALE_2:
if (scale < 6) {
scale += 1;
sdl_change_scale(scale);
}
break;
case KEY_EXIT:
return E_EXIT;
break;
default:
break;
}
}
}
return E_NOTHING;
}
int main_loop(Uint32 delta) {
SDL_Event e;
// Capture events
while (SDL_PollEvent(&e)) {
switch(process_event(&e)) {
case E_RESET:
dmg_reset();
break;
case E_EXIT:
return 1;
break;
default:
break;
}
}
if (!pause_emu) {
//keyboard_update_keys();
// Do sound or whatever
// ...
dmg_run(delta, &debug_flag, &debug_pause);
// Something
//randomize(screen_get_fb());
}
return 0;
}
int main(int argc, char** argv) {
int opt;
//int verbose_flag = 0;
char *bios_path = NULL;
char *rom_path = NULL;
Uint32 start_time, time, frame;
uint32_t delta;
pause_emu = 0;
scale = 1;
while ((opt = getopt(argc, argv, "vdb:")) != -1) {
switch (opt) {
case 'b':
bios_path = optarg;
break;
case 'v':
//verbose_flag = 1;
break;
case 'd':
debug_flag = 1;
debug_pause = 1;
break;
default:
usage(argv[0]);
}
}
if (optind == argc) {
usage(argv[0]);
}
rom_path = argv[optind];
dmg_load_bios(bios_path);
dmg_load_rom(rom_path);
dmg_init();
sdl_init();
time = 0;
frame = 1;
start_time = SDL_GetTicks();
for (;;) {
delta = ((frame * 1000) / SCREEN_FPS) - time;
time += delta;
if (main_loop(delta)) {
break;
}
sdl_render(screen_get_fb());
// Sync to maximum fps
if (SDL_GetTicks() - start_time < time) {
SDL_Delay(time - (SDL_GetTicks() - start_time));
} else {
frame = 0;
start_time = SDL_GetTicks();
}
frame++;
// When Uint32 overflows, restart. This is unlikely to happen:
// Uint32 will overflow after 828 days at 60 fps
if (frame == 0) {
start_time = SDL_GetTicks();
}
}
sdl_stop();
return 0;
}