Skip to content

Commit

Permalink
Limit draw speed of sprites
Browse files Browse the repository at this point in the history
  • Loading branch information
JustDoom committed Dec 19, 2024
1 parent 6b6a0c6 commit 7b896a7
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/emulator/Cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Cpu::Cpu(Renderer* renderer, Keyboard* keyboard, Speaker * speaker) {
this->delay = 0;
this->soundTimer = 0;
this->pc = 0x200; // Starting position for reading instructions. At least for most programs
this->drawn = false;
this->paused = false;
this->speed = 20; // Game speed

Expand Down Expand Up @@ -58,12 +59,13 @@ void Cpu::loadProgramIntoMemory(std::ifstream* file) {

void Cpu::cycle() {
for (int i = 0; i < this->speed; i++) {
if (this->paused) {
if (this->paused || this->drawn) {
break;
}

runInstruction((this->memory[this->pc] << 8) | this->memory[this->pc + 1]);
}
this->drawn = false;

if (this->paused && this->keyboard->onNextKeyPress == nullptr) {
this->paused = false;
Expand Down Expand Up @@ -232,13 +234,14 @@ void Cpu::runInstruction(const uint16_t opcode) {

if (drawX >= 64 || drawY >= 32) continue;

if ( this->renderer->setPixel(drawX, drawY)) {
if (this->renderer->setPixel(drawX, drawY)) {
this->registers[0xF] = 1;
}
}
sprite <<= 1;
}
}
this->drawn = true;
break;
}
case 0xE000:
Expand Down
1 change: 1 addition & 0 deletions src/emulator/Cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Cpu {
uint8_t delay;
uint8_t soundTimer;

bool drawn;
bool paused;
uint8_t speed;
uint8_t seed;
Expand Down
4 changes: 0 additions & 4 deletions src/emulator/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ void Renderer::render(SDL_Renderer* renderer) {
}

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 = y * this->columns + x;
const bool oldValue = this->display[pixelLoc];
this->display[pixelLoc] = !oldValue;
Expand Down

0 comments on commit 7b896a7

Please sign in to comment.