diff options
| -rw-r--r-- | roms/Connect 4 [David Winter].ch8 | bin | 0 -> 194 bytes | |||
| -rw-r--r-- | roms/Maze (alt) [David Winter, 199x].ch8 | bin | 0 -> 38 bytes | |||
| -rw-r--r-- | roms/test_opcode.ch8 | bin | 478 -> 0 bytes | |||
| -rw-r--r-- | src/chip8.cpp | 4 | ||||
| -rw-r--r-- | src/main.cpp | 68 | ||||
| -rw-r--r-- | src/platform.cpp | 11 |
6 files changed, 57 insertions, 26 deletions
diff --git a/roms/Connect 4 [David Winter].ch8 b/roms/Connect 4 [David Winter].ch8 Binary files differnew file mode 100644 index 0000000..200a67a --- /dev/null +++ b/roms/Connect 4 [David Winter].ch8 diff --git a/roms/Maze (alt) [David Winter, 199x].ch8 b/roms/Maze (alt) [David Winter, 199x].ch8 Binary files differnew file mode 100644 index 0000000..0dca981 --- /dev/null +++ b/roms/Maze (alt) [David Winter, 199x].ch8 diff --git a/roms/test_opcode.ch8 b/roms/test_opcode.ch8 Binary files differdeleted file mode 100644 index f540f69..0000000 --- a/roms/test_opcode.ch8 +++ /dev/null diff --git a/src/chip8.cpp b/src/chip8.cpp index dc63239..f294383 100644 --- a/src/chip8.cpp +++ b/src/chip8.cpp @@ -7,6 +7,8 @@ #include <thread> #include <mutex> +#include <string.h> + const uint32_t FONTSET_START_ADDRESS = 0x050; const uint32_t ROM_START_ADDRESS = 0x200; @@ -94,7 +96,7 @@ void Chip8::LoadROM(const char* filename) { file.close(); // Load ROM contents into Chip8's memory, from 0x200 - for (uint64_t i = 0; i < size; i++) { + for (size_t i = 0; i < size; i++) { memory[ROM_START_ADDRESS + i] = buffer[i]; } diff --git a/src/main.cpp b/src/main.cpp index 1eb5c30..a7284df 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,45 +5,73 @@ #include "chip8.hpp" #include "platform.hpp" +#ifdef __EMSCRIPTEN__ +#include <emscripten.h> +#endif + #define WINDOW_TITLE "chip8emu" +Platform platform( + WINDOW_TITLE, + VIDEO_WIDTH * 10, + VIDEO_HEIGHT * 10, + VIDEO_WIDTH, VIDEO_HEIGHT +); + +Chip8* chip8 = new Chip8(); + +bool quit = false; + +void mainLoop() { + platform.Update(chip8->video, 10); +} + +void update() { + while (true) { + quit = platform.ProcessInput(&chip8->keypad); + +#ifdef __EMSCRIPTEN__ + if (quit) emscripten_cancel_main_loop(); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); +#else + usleep(2 * 1000); +#endif + chip8->Cycle(); + } +} + +#ifdef __EMSCRIPTEN__ +int main() { + int videoScale = 10; + int cycleDelay = 0; + char const* filename = "pong.ch8"; +#else int main(int argc, char** argv) { if (argc != 4) { - std::cerr << "Usage: " << argv[0] << " <Scale> <Delay> <ROM>\n"; + std::cerr << "Usage: " << argv[0] << " <Scale> <Delay> <ROM>" << std::endl; std::exit(EXIT_FAILURE); } int videoScale = std::stoi(argv[1]); int cycleDelay = std::stoi(argv[2]); char const* filename = argv[3]; +#endif - Chip8* chip8 = new Chip8(); Table* table = new Table(chip8); chip8->SetTable(table); chip8->LoadROM(filename); - Platform platform( - WINDOW_TITLE, - VIDEO_WIDTH * videoScale, - VIDEO_HEIGHT * videoScale, - VIDEO_WIDTH, VIDEO_HEIGHT - ); - std::thread timerThread(&Chip8::TimerUpdateThread, chip8, &platform); - - bool quit = false; - while (!quit) { - quit = platform.ProcessInput(&chip8->keypad); - - usleep(cycleDelay * 1000); - - chip8->Cycle(); - platform.Update(chip8->video, videoScale); - } - + std::thread updateThread(update); +#ifdef __EMSCRIPTEN__ + emscripten_set_main_loop(mainLoop, 0, 1); +#else + while (!quit) + mainLoop(); timerThread.detach(); +#endif return 0; } diff --git a/src/platform.cpp b/src/platform.cpp index 34a39e5..5d77180 100644 --- a/src/platform.cpp +++ b/src/platform.cpp @@ -45,12 +45,13 @@ void Platform::Update(const std::bitset<2048> bitset, int videoScale) { for (int y = 0; y < 32; ++y) { for (int x = 0; x < 64; ++x) { SDL_Rect pixelRect = {x * videoScale, y * videoScale, videoScale, videoScale}; - if (bitset[y * 64 + x]) { SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); - } else { - SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); - } - SDL_RenderFillRect(renderer, &pixelRect); + if (bitset[y * 64 + x]) { + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + } else { + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + } + SDL_RenderFillRect(renderer, &pixelRect); } } SDL_RenderPresent(renderer); |
