Skip to content

Commit

Permalink
Reimplement command line rom starting. Also cleans up SDL init
Browse files Browse the repository at this point in the history
  • Loading branch information
JustDoom committed Dec 17, 2024
1 parent 4443886 commit 06d1156
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 13 deletions.
64 changes: 59 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,70 @@
#include <SDL3_ttf/SDL_ttf.h>
#include <libconfig.h++>

#include "Timer.h"
#include "sdl/Emulator.h"
#include "sdl/MainMenu.h"
#include "util/MiscUtil.h"
#include "Timer.h"

int main(int argc, char **argv) {
std::string rom;

for (int i = 0; i < argc; i++) {
std::string_view arg = argv[i];
if (arg.rfind("--") != 0) continue; // TODO: Account for --longform or -sf (short form) commands. just needs a better command handler

std::string command = MiscUtil::toLowerCase(std::string(arg));
if (command == "--rom") {
if (i + 1 < argc) {
// return Emulator().launch(argv[++i]);
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
return 1;
}

std::string rom = argv[++i];
Emulator emulator(rom);
emulator.init();

bool debug = false;
bool quit = false;
SDL_Event event;

Timer fpsTimer;
Timer capTimer;

int countedFrames = 0;
fpsTimer.start();

while (!quit) {
capTimer.start();
while (SDL_PollEvent(&event)) {
if (event.type == SDL_EVENT_QUIT) {
quit = true;
}

emulator.handleEvent(event);
}

emulator.update();
emulator.render();

float avgFPS = countedFrames / (fpsTimer.getTicks() / 1000.f);
if (avgFPS > 2000000) {
avgFPS = 0;
}

if (debug) {
std::cout << "FPS: " << avgFPS << std::endl;
}

++countedFrames;
int frameTicks = capTimer.getTicks();
if (frameTicks < 1000 / 60) {
SDL_Delay(1000 / 60 - frameTicks);
}
}

SDL_Quit();

return 0;
}
std::cerr << "Please include the path to the file" << std::endl;
return 0;
Expand Down Expand Up @@ -96,8 +145,13 @@ int main(int argc, char **argv) {
}
}

if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
return 1;
}

if (!TTF_Init()) {
SDL_Log("Couldn't initialize TTF: %s\n",SDL_GetError());
SDL_Log("Couldn't initialize TTF: %s\n", SDL_GetError());
return 1;
}

Expand Down
2 changes: 1 addition & 1 deletion src/sdl/Emulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <iostream>

Emulator::Emulator(const std::string &rom) : cpu(&renderWrapper, &keyboard, &speaker), rom(rom) {}
Emulator::Emulator(const std::string& rom) : cpu(&renderWrapper, &keyboard, &speaker), rom(rom) {}

void Emulator::init() {
Window::init();
Expand Down
4 changes: 2 additions & 2 deletions src/sdl/Emulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class Emulator : public Window {
SdlKeyboard keyboard;
Cpu cpu;

const std::string &rom;
const std::string& rom;
public:
Emulator(const std::string &rom);
Emulator(const std::string& rom);

void init() override;
bool handleEvent(SDL_Event& event) override;
Expand Down
5 changes: 0 additions & 5 deletions src/sdl/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ Window::~Window() {
}

void Window::init() {
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
return;
}

if (!SDL_CreateWindowAndRenderer("8ChocChip - CHIP-8 Emulator", 64 * 15, 32 * 15, SDL_WINDOW_RESIZABLE, &this->window, &this->renderer)) {
std::cerr << "Couldn't create window and renderer. SDL_Error: " << SDL_GetError() << std::endl;
return;
Expand Down

0 comments on commit 06d1156

Please sign in to comment.