-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
95 lines (77 loc) · 2.12 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
#include "cpu.h"
#include "screen.h"
#include "keys.h"
/************************************************************************************
* VARIABLES
************************************************************************************/
SDL_Event event;
int listen();
/************************************************************************************
* MAIN FUNCTION
************************************************************************************/
int main(int argc, char *argv[]) {
// Initialisation ...
int out = initialiseScreen();
if (out)
return out;
initialiseCPU();
// Load ROM file ...
loadROM("roms/Tetris (W) (V1.0) [!].gb");
// Print game info ...
printGameInfo();
// program main loop
int done = 0;
while (!done) {
updateCPU();
// drawScreen();
done = listen();
manageKeys();
}
printf("Exited cleanly\n");
exitProgram();
return 0;
}
/** Key events listening function. */
int listen() {
int done = 0;
int newWidth, newHeight;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE)
done = 1;
break;
case SDL_VIDEORESIZE:
newWidth = event.resize.w;
newHeight = event.resize.h;
// Create a new window
screen = SDL_SetVideoMode(newWidth, newHeight, 16, SDL_HWSURFACE|SDL_RESIZABLE|SDL_DOUBLEBUF);
if (!screen) {
printf("Unable to set %dx%d video: %s\n", WIDTH, HEIGHT, SDL_GetError());
exit(EXIT_FAILURE);
}
updatePixelSize(newWidth, newHeight);
// Create pixel surfaces
int i, j;
for (i = 0; i < SIZEX; i++) {
for (j = 0; j < SIZEY; j++) {
// Surface
SDL_FreeSurface(pixel[i][j].surface);
pixel[i][j].surface = SDL_CreateRGBSurface(SDL_HWSURFACE, pixel[i][j].pixelWidth, pixel[i][j].pixelHeight, 32,0,0,0,0);
if (pixel[i][j].surface == NULL) {
fprintf(stderr, "Error while loading surface for pixel [%d, %d]: %s", i, j, SDL_GetError());
exit(EXIT_FAILURE);
}
}
}
clearScreen();
break;
case SDL_QUIT:
done = 1;
break;
default:
break;
}
}
return done;
}