Compare commits
2 Commits
optimizati
...
display-re
| Author | SHA1 | Date | |
|---|---|---|---|
| 22526c1b90 | |||
| ac5313a6ca |
@@ -1,8 +1,13 @@
|
|||||||
#include "DisplaySDL.hpp"
|
#include "DisplaySDL.hpp"
|
||||||
|
#include "Peripherals.hpp"
|
||||||
|
|
||||||
|
#include <SDL2/SDL_surface.h>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
DisplaySDL::DisplaySDL(int w, int h, uint32_t fgColor, uint32_t bgColor) {
|
DisplaySDL::DisplaySDL(int w, int h, uint32_t fgColor, uint32_t bgColor):
|
||||||
|
mpFramebuffer{std::make_unique<Framebuffer>()},
|
||||||
|
mpDisplayState{std::make_unique<Framebuffer>()},
|
||||||
|
mDoClear{true} {
|
||||||
// Create SDL Window
|
// Create SDL Window
|
||||||
mpWindow = SDL_CreateWindow(
|
mpWindow = SDL_CreateWindow(
|
||||||
"ChocoChip-8",
|
"ChocoChip-8",
|
||||||
@@ -36,30 +41,52 @@ DisplaySDL::~DisplaySDL() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void DisplaySDL::clear() {
|
void DisplaySDL::clear() {
|
||||||
Display::clear();
|
for(auto &scanline : *mpFramebuffer) {
|
||||||
SDL_FillRect(SDL_GetWindowSurface(mpWindow), nullptr, mBgColor);
|
scanline.reset();
|
||||||
SDL_UpdateWindowSurface(mpWindow);
|
}
|
||||||
miTopDirtyScanline = chocochip8::gcHeight - 1;
|
mDoClear = true;
|
||||||
miBottomDirtyScanline = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DisplaySDL::updateWindow() const {
|
int DisplaySDL::blit(const chocochip8::Scanline &spriteScanline, int y) {
|
||||||
if(miTopDirtyScanline > miBottomDirtyScanline) {
|
using chocochip8::Scanline;
|
||||||
// No changes since the last update
|
Scanline &targetScanline = mpFramebuffer->at(y);
|
||||||
return;
|
bool collision = (spriteScanline & targetScanline).any();
|
||||||
|
targetScanline ^= spriteScanline;
|
||||||
|
return collision;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DisplaySDL::updateWindow(bool doWindowUpdate) const {
|
||||||
|
using chocochip8::Scanline;
|
||||||
SDL_Surface *pSurface = SDL_GetWindowSurface(mpWindow);
|
SDL_Surface *pSurface = SDL_GetWindowSurface(mpWindow);
|
||||||
for(int y = 0; y < chocochip8::gcHeight; y++) {
|
if(mDoClear) {
|
||||||
if(y < miTopDirtyScanline || miBottomDirtyScanline < y) {
|
for(auto &scanline : *mpDisplayState) {
|
||||||
|
scanline.reset();
|
||||||
|
}
|
||||||
|
SDL_FillRect(pSurface, NULL, mBgColor);
|
||||||
|
mDoClear = false;
|
||||||
|
doWindowUpdate = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int y = 0; y < mpFramebuffer->size(); y++) {
|
||||||
|
Scanline &rNewScanline = (*mpFramebuffer)[y];
|
||||||
|
Scanline &rOldScanline = (*mpDisplayState)[y];
|
||||||
|
if(rNewScanline == rOldScanline) {
|
||||||
// Skip scanlines that haven't changed since the last update
|
// Skip scanlines that haven't changed since the last update
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(int x = 0; x < chocochip8::gcWidth; x++) {
|
for(int x = 0; x < rNewScanline.size(); x++) {
|
||||||
// Map framebuffer pixel to SDL surface rectangle
|
bool isSet = rNewScanline._Unchecked_test(x);
|
||||||
int x1 = ( x * pSurface->w) / chocochip8::gcWidth;
|
bool wasSet = rOldScanline._Unchecked_test(x);
|
||||||
int x2 = ((x + 1) * pSurface->w) / chocochip8::gcWidth;
|
if(isSet == wasSet) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Map framebuffer pixel to SDL surface rectangle,
|
||||||
|
// note that the MSB of an scanline's bitset is the leftmost pixel.
|
||||||
|
int z = (chocochip8::gcWidth - 1) - x;
|
||||||
|
int x1 = ( z * pSurface->w) / chocochip8::gcWidth;
|
||||||
|
int x2 = ((z + 1) * pSurface->w) / chocochip8::gcWidth;
|
||||||
int y1 = ( y * pSurface->h) / chocochip8::gcHeight;
|
int y1 = ( y * pSurface->h) / chocochip8::gcHeight;
|
||||||
int y2 = ((y + 1) * pSurface->h) / chocochip8::gcHeight;
|
int y2 = ((y + 1) * pSurface->h) / chocochip8::gcHeight;
|
||||||
SDL_Rect rect;
|
SDL_Rect rect;
|
||||||
@@ -68,14 +95,14 @@ void DisplaySDL::updateWindow() const {
|
|||||||
rect.w = x2 - x1;
|
rect.w = x2 - x1;
|
||||||
rect.h = y2 - y1;
|
rect.h = y2 - y1;
|
||||||
|
|
||||||
// Read Chocochip8 Framebuffer and choose appropriate color,
|
Uint32 color = (isSet ? mFgColor : mBgColor);
|
||||||
// note that the MSB of an scanline's bitset is the leftmost pixel.
|
|
||||||
Uint32 color = (mpFramebuffer->at(y)[(chocochip8::gcWidth - 1) - x] ? mFgColor : mBgColor);
|
|
||||||
SDL_FillRect(pSurface, &rect, color);
|
SDL_FillRect(pSurface, &rect, color);
|
||||||
|
doWindowUpdate = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
miTopDirtyScanline = chocochip8::gcHeight - 1;
|
if(doWindowUpdate) {
|
||||||
miBottomDirtyScanline = 0;
|
*mpDisplayState = *mpFramebuffer;
|
||||||
SDL_UpdateWindowSurface(mpWindow);
|
SDL_UpdateWindowSurface(mpWindow);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,7 +2,9 @@
|
|||||||
|
|
||||||
#include "Peripherals.hpp"
|
#include "Peripherals.hpp"
|
||||||
|
|
||||||
|
#include <array>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <memory>
|
||||||
#include <SDL2/SDL_video.h>
|
#include <SDL2/SDL_video.h>
|
||||||
|
|
||||||
class DisplaySDL : public chocochip8::Display {
|
class DisplaySDL : public chocochip8::Display {
|
||||||
@@ -10,10 +12,15 @@ public:
|
|||||||
DisplaySDL(int w, int h, uint32_t fgCol = 0xffffff, uint32_t bgCol = 0x000000);
|
DisplaySDL(int w, int h, uint32_t fgCol = 0xffffff, uint32_t bgCol = 0x000000);
|
||||||
~DisplaySDL() override;
|
~DisplaySDL() override;
|
||||||
void clear() override;
|
void clear() override;
|
||||||
void updateWindow() const;
|
int blit(const chocochip8::Scanline &scanline, int y) override;
|
||||||
|
void updateWindow(bool forceWindowUpdate = false) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
using Framebuffer = std::array<chocochip8::Scanline, chocochip8::gcHeight>;
|
||||||
|
std::unique_ptr<Framebuffer> mpFramebuffer;
|
||||||
|
mutable std::unique_ptr<Framebuffer> mpDisplayState;
|
||||||
SDL_Window *mpWindow;
|
SDL_Window *mpWindow;
|
||||||
Uint32 mFgColor;
|
Uint32 mFgColor;
|
||||||
Uint32 mBgColor;
|
Uint32 mBgColor;
|
||||||
|
mutable bool mDoClear;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -290,12 +290,10 @@ void Interpreter::executeDraw(uint8_t x, uint8_t y, uint8_t n) {
|
|||||||
|
|
||||||
// we draw one scanline per sprite row in high-res mode, but twice in low-res mode
|
// we draw one scanline per sprite row in high-res mode, but twice in low-res mode
|
||||||
for(int j = 0; j < (mIsHighResMode ? 1 : 2); j++) {
|
for(int j = 0; j < (mIsHighResMode ? 1 : 2); j++) {
|
||||||
Scanline &targetScanline = mrDisplay.getModifyableScanline(y + i * (mIsHighResMode ? 1 : 2) + j);
|
// blit the sprite bitset into the screen
|
||||||
if((targetScanline & spriteScanline) != 0) {
|
if(mrDisplay.blit(spriteScanline, y + i * (mIsHighResMode ? 1 : 2) + j) != 0) {
|
||||||
collisionCount += 1;
|
collisionCount += 1;
|
||||||
}
|
}
|
||||||
// blit the sprite bitset into the screen
|
|
||||||
targetScanline ^= spriteScanline;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// update state flags
|
// update state flags
|
||||||
|
|||||||
@@ -1,15 +1,12 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <array>
|
|
||||||
#include <bitset>
|
#include <bitset>
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
namespace chocochip8 {
|
namespace chocochip8 {
|
||||||
|
|
||||||
constexpr size_t gcWidth = 128;
|
constexpr size_t gcWidth = 128;
|
||||||
constexpr size_t gcHeight = 64;
|
constexpr size_t gcHeight = 64;
|
||||||
using Scanline = std::bitset<gcWidth>;
|
using Scanline = std::bitset<gcWidth>;
|
||||||
using Framebuffer = std::array<Scanline, gcHeight>;
|
|
||||||
|
|
||||||
enum class Key {
|
enum class Key {
|
||||||
KEY_0, KEY_1, KEY_2, KEY_3,
|
KEY_0, KEY_1, KEY_2, KEY_3,
|
||||||
@@ -20,32 +17,9 @@ namespace chocochip8 {
|
|||||||
|
|
||||||
class Display {
|
class Display {
|
||||||
public:
|
public:
|
||||||
Display():
|
|
||||||
mpFramebuffer{std::make_unique<Framebuffer>()},
|
|
||||||
miTopDirtyScanline{0},
|
|
||||||
miBottomDirtyScanline{gcHeight - 1} {}
|
|
||||||
|
|
||||||
virtual ~Display() = default;
|
virtual ~Display() = default;
|
||||||
|
virtual int blit(const Scanline& spriteScanline, int y) = 0;
|
||||||
virtual void clear() {
|
virtual void clear() = 0;
|
||||||
for(auto &scanline : *mpFramebuffer) {
|
|
||||||
scanline.reset();
|
|
||||||
}
|
|
||||||
miTopDirtyScanline = 0;
|
|
||||||
miBottomDirtyScanline = gcHeight - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual Scanline& getModifyableScanline(size_t y) {
|
|
||||||
Scanline& res = mpFramebuffer->at(y);
|
|
||||||
miTopDirtyScanline = std::min(miTopDirtyScanline, y);
|
|
||||||
miBottomDirtyScanline = std::max(miBottomDirtyScanline, y);
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
std::unique_ptr<Framebuffer> mpFramebuffer;
|
|
||||||
mutable size_t miTopDirtyScanline;
|
|
||||||
mutable size_t miBottomDirtyScanline;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class Buzzer {
|
class Buzzer {
|
||||||
|
|||||||
4
main.cpp
4
main.cpp
@@ -9,8 +9,8 @@
|
|||||||
#include "DisplaySDL.hpp"
|
#include "DisplaySDL.hpp"
|
||||||
#include "Interpreter.hpp"
|
#include "Interpreter.hpp"
|
||||||
#include "Peripherals.hpp"
|
#include "Peripherals.hpp"
|
||||||
#include "SDL_events.h"
|
#include "SDL2/SDL_events.h"
|
||||||
#include "SDL_timer.h"
|
#include "SDL2/SDL_timer.h"
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
|
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user