Skip to content

Commit

Permalink
Reuse main loop for direct ROM boot
Browse files Browse the repository at this point in the history
  • Loading branch information
JustDoom committed Dec 19, 2024
1 parent dd8078b commit 4adc449
Showing 1 changed file with 10 additions and 53 deletions.
63 changes: 10 additions & 53 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,67 +13,20 @@
#include "util/MiscUtil.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 = toLowerCase(std::string(arg));
if (command == "--rom") {
if (i + 1 < argc) {
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) {
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();

rom = argv[++i];
} else {
std::cerr << "Please include the path to the file" << std::endl;
return 0;
}
std::cerr << "Please include the path to the file" << std::endl;
return 0;
}
if (command == "--help") {
std::cerr << "Usage: 8chocchip --rom <rompath>" << std::endl;
Expand Down Expand Up @@ -162,7 +115,11 @@ int main(int argc, char **argv) {
}

std::vector<std::unique_ptr<Window>> windows;
windows.emplace_back(std::make_unique<MainMenu>(font, configFilePath, romFiles, romDirectories, windows))->init();
if (rom.empty()) {
windows.emplace_back(std::make_unique<MainMenu>(font, configFilePath, romFiles, romDirectories, windows))->init();
} else {
windows.emplace_back(std::make_unique<Emulator>(rom))->init();
}

bool debug = false;
bool quit = false;
Expand Down

0 comments on commit 4adc449

Please sign in to comment.