Files
choco-chip8/main.cpp

63 lines
1.5 KiB
C++

#include <iostream>
#include <iterator>
#include <fstream>
#include <vector>
#include <SDL2/SDL.h>
#include "BuzzerSDL.hpp"
#include "DisplaySDL.hpp"
#include "Interpreter.hpp"
#include "Peripherals.hpp"
#include "SDL_events.h"
#include "SDL_timer.h"
int main(int argc, char* argv[]) {
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
std::cerr << "Couldn't initialize SDL: " << SDL_GetError() << '\n';
return 1;
}
class TestKeypad : public chocochip8::Keypad {
bool isKeyPressed(chocochip8::Key) override { return false; }
};
BuzzerSDL buzzer(440);
DisplaySDL display(1280, 640);
TestKeypad keypad;
chocochip8::Interpreter chip8(1000, display, buzzer, keypad);
auto rom = std::vector<char>();
auto romfile = std::ifstream(argv[1] != NULL ? argv[1] : "/dev/stdin", std::ios_base::in | std::ios_base::binary);
std::copy(
std::istreambuf_iterator<char>(romfile),
std::istreambuf_iterator<char>(),
std::back_insert_iterator(rom)
);
chip8.loadProgram(rom.data(), rom.size());
SDL_Event event;
bool done = false;
Uint64 start, end;
start = SDL_GetTicks64();
while(!done) {
while(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT) {
done = true;
}
}
chip8.tick();
display.updateWindow();
SDL_Delay(1);
end = SDL_GetTicks64();
if(end - start > 3)
std::cout << "Frame took too long: " << end - start << "\n";
start = end;
}
buzzer.off();
SDL_Quit();
return 0;
}