diff --git a/src/emulator/Cpu.cpp b/src/emulator/Cpu.cpp index 6f04c13..bcc2e3c 100644 --- a/src/emulator/Cpu.cpp +++ b/src/emulator/Cpu.cpp @@ -227,9 +227,12 @@ void Cpu::runInstruction(const uint16_t opcode) { for (uint8_t col = 0; col < 8; ++col) { if (sprite & 0x80) { - const uint8_t drawX = (uX + col) % 64; + const uint8_t drawX = (uX + col); + const uint8_t drawY = (uY + row); - if (const uint8_t drawY = (uY + row) % 32; this->renderer->setPixel(drawX, drawY)) { + if (drawX >= 64 || drawY >= 32) continue; + + if ( this->renderer->setPixel(drawX, drawY)) { this->registers[0xF] = 1; } } diff --git a/src/emulator/Renderer.cpp b/src/emulator/Renderer.cpp index fb47176..96a80b6 100644 --- a/src/emulator/Renderer.cpp +++ b/src/emulator/Renderer.cpp @@ -1,55 +1,48 @@ #include "Renderer.h" -Renderer::Renderer() { - this->display.resize(this->columns * this->rows); -} +#include void Renderer::render(SDL_Renderer* renderer) { - // Render the display - for (uint16_t y = 0; y < this->rows; ++y) { - for (uint16_t x = 0; x < this->columns; ++x) { + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + + for (uint8_t y = 0; y < this->rows; ++y) { + for (uint8_t x = 0; x < this->columns; ++x) { if (this->display[y * this->columns + x]) { - drawPixel(renderer, x, y); + const SDL_FRect rect = {x * this->scale, y * this->scale, this->scale, this->scale}; + SDL_RenderFillRect(renderer, &rect); } } } } -bool Renderer::setPixel(uint8_t x, uint16_t y) { +bool Renderer::setPixel(const uint8_t x, const uint8_t y) { // Wrap around if x or y is out of bounds // x %= this->columns; // y %= this->rows; - const uint16_t pixelLoc = x + (y * this->columns); - this->display[pixelLoc] = !this->display[pixelLoc]; + const uint16_t pixelLoc = y * this->columns + x; + const bool oldValue = this->display[pixelLoc]; + this->display[pixelLoc] = !oldValue; - return !this->display[pixelLoc]; + return oldValue; +} + +bool Renderer::getPixel(const uint8_t x, const uint8_t y) { + return this->display[y * this-> columns + x]; } void Renderer::clear() { - for (uint16_t y = 0; y < this->rows; ++y) { - for (uint16_t x = 0; x < this->columns; ++x) { - if (this->display[y * this->columns + x]) { - setPixel(x, y); - } - } - } + this->display.assign(this->display.size(), false); } -uint16_t Renderer::getColumns() const { +uint8_t Renderer::getColumns() const { return this->columns; } -uint16_t Renderer::getRows() const { +uint8_t Renderer::getRows() const { return this->rows; } float Renderer::getScale() const { return this->scale; -} - -void Renderer::drawPixel(SDL_Renderer* renderer, const uint16_t x, const uint16_t y) { - const SDL_FRect rect = {x * this->scale, y * this->scale, this->scale, this->scale}; - SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); // Set color to white - SDL_RenderFillRect(renderer, &rect); } \ No newline at end of file diff --git a/src/emulator/Renderer.h b/src/emulator/Renderer.h index 793145f..5e85df2 100644 --- a/src/emulator/Renderer.h +++ b/src/emulator/Renderer.h @@ -9,24 +9,24 @@ class Renderer { private: const float scale = 15; - const uint16_t columns = 64; - const uint16_t rows = 32; + const uint8_t columns = 64; + const uint8_t rows = 32; + - std::vector display; public: - Renderer(); + Renderer() : display(columns * rows) {}; + std::vector display; - bool setPixel(uint8_t x, uint16_t y); + bool setPixel(uint8_t x, uint8_t y); + bool getPixel(uint8_t x, uint8_t y); void clear(); void render(SDL_Renderer* renderer); - uint16_t getColumns() const; - uint16_t getRows() const; + uint8_t getColumns() const; + uint8_t getRows() const; float getScale() const; - - void drawPixel(SDL_Renderer* renderer, uint16_t x, uint16_t y); }; #endif \ No newline at end of file