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
|
#include <emulator.h>
#include <SDL.h>
int main(int argc, char** argv);
static void show_help();
int main(int argc, char** argv)
{
if(argc < 2)
{
show_help();
return 1;
}
SDL_Init(SDL_INIT_VIDEO);
SDL_Window * window = SDL_CreateWindow("CHIP8 Emulator", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, 0);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
SDL_Texture * texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 64, 32);
Emulator emulator;
load:
emulator_initialise(&emulator);
if(emulator_load_rom(&emulator, argv[1]) != 0)
return 2;
printf("Hello brother!\n");
uint32_t pixels[32 * 64];
SDL_Event event;
while(emulator.is_on)
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
goto exit;
if(event.type == SDL_WINDOWEVENT)
SDL_RenderPresent(renderer);
if(event.type == SDL_KEYDOWN)
{
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
goto exit;
case SDLK_F5:
emulator_deinitialise(&emulator);
goto load; //join thread or smth??
default:
emulator_handle_key_press(&emulator, event.key.keysym.sym);
}
}
if(event.type == SDL_KEYUP)
emulator_handle_key_release(&emulator, event.key.keysym.sym);
}
emulator_tick(&emulator);
if(emulator.draw_flag == 1)
{
//update pixel buffer with the emulator's display pixels
for(int w = 0; w < 64; ++w)
{
for(int h = 0; h < 32; ++h)
{
uint8_t pixel = emulator.display[w][h];
pixels[64 * h + w] = (FOREGROUND_COLOR * pixel) | BLACKGROUND_COLOR;
}
}
SDL_UpdateTexture(texture, NULL, pixels, sizeof(uint32_t) * 64);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
emulator.draw_flag = 0;
}
if(!DEBUG)
usleep(1000000 / INSTRUCTIONS_PER_SECOND);
}
exit:
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
emulator_deinitialise(&emulator);
return 0;
}
static void show_help()
{
printf("BAD USAGE! -> ./chip8_emulator [ROM]\n");
}
|