-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
76 lines (62 loc) · 2.22 KB
/
main.cpp
File metadata and controls
76 lines (62 loc) · 2.22 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "cpu.h"
#include <iostream>
#include <istream>
#include <math.h>
#include <SDL2/SDL.h>
#include <queue>
#include <memory>
#define IPS 1000 //instructions per second
#define SPI (float)1/IPS //seconds per instruction
#define HEIGHT 320
using namespace std;
int main(int argc, char** argv){
if(argc == 1){
cout << "No ROM supplied. \nUsage is ./Chip8 [-options] ROM_Path\n";
return 0;
}
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* window = SDL_CreateWindow("THIS", 0, 0, 2 * HEIGHT,HEIGHT, 0);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
queue<SDL_Event> eventQueue;
SDL_Event event;
clock_t startOfCycle, endOfCycle;
QApplication app(argc, argv);
CPU processor(window, renderer, &app);
processor.init();
if(processor.LoadRom(argv[1]) == false){
processor.quit();
}
while(processor.running()){
//while there are events in queue
while(SDL_PollEvent(&event)){
//process event is a keypress
//if the event is a key press that isnt space or escapei
if(event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_SPACE ){
//if space is pressed
processor.openWindow();
}
else if(event.type == SDL_KEYDOWN && !(event.key.keysym.sym == SDLK_SPACE || event.key.keysym.sym == SDLK_ESCAPE)){
//toss it into the processor's event queue
processor.push_event(event);
}
else if(event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE ){
//if escape is pressed
processor.quit();
}
}
startOfCycle = clock();
processor.cycle();
processor.draw();
endOfCycle = clock();
if((endOfCycle-startOfCycle) / CLOCKS_PER_SEC < SPI)
SDL_Delay((SPI - (endOfCycle-startOfCycle) / CLOCKS_PER_SEC) * 1000);
else{
cout << "Instruction took longer than expected\n";
}
//processor.printScreen();
}
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
// SDL_Quit();
return 0;
}