Skip to content

Commit

Permalink
Fix CLIPPING and clean up some rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
JustDoom committed Dec 19, 2024
1 parent 4417f16 commit 6b6a0c6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 37 deletions.
7 changes: 5 additions & 2 deletions src/emulator/Cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
45 changes: 19 additions & 26 deletions src/emulator/Renderer.cpp
Original file line number Diff line number Diff line change
@@ -1,55 +1,48 @@
#include "Renderer.h"

Renderer::Renderer() {
this->display.resize(this->columns * this->rows);
}
#include <iostream>

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);
}
18 changes: 9 additions & 9 deletions src/emulator/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> display;
public:
Renderer();
Renderer() : display(columns * rows) {};
std::vector<bool> 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

0 comments on commit 6b6a0c6

Please sign in to comment.