-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
62 lines (51 loc) · 1.33 KB
/
main.cpp
File metadata and controls
62 lines (51 loc) · 1.33 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
#include "Chip.h"
#include "ChipFrame.h"
#include <SDL2/SDL.h>
#include <iostream>
int main(int argc, char** argv)
{
if(argc < 2)
{
std::cerr << "Usage: chip8 <ROM file>" << std::endl;
return 1;
}
Chip chip8;
ChipFrame chipFrame;
chip8.loadFile(argv[1]);
const int FPS = 60;
const int frameDelay = 1000 / FPS;
Uint32 frameStart;
int frameTime;
while(1)
{
frameStart = SDL_GetTicks();
chip8.setKeyBuffer(chipFrame.getKeyBuffer());
chip8.run();
if(chip8.needsRedraw())
{
chipFrame.draw(chip8.getDisplay());
chip8.removeDrawFlag();
}
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
return 0;
case SDL_KEYDOWN:
chipFrame.keyPressed( event.key.keysym.sym);
break;
case SDL_KEYUP:
chipFrame.keyReleased( event.key.keysym.sym);
break;
}
}
frameTime = SDL_GetTicks() - frameStart;
if(frameTime < frameDelay)
{
SDL_Delay(frameDelay - frameTime);
}
}
return 0;
}