From a4ae368577966c414e84ed2c96987b7210b514f5 Mon Sep 17 00:00:00 2001 From: Miguel Villa Floran Date: Thu, 3 Oct 2024 22:44:48 -0700 Subject: [PATCH] Add frame buffer --- include/display.hpp | 4 ++-- include/frame_buffer.hpp | 29 +++++++++++++++++++++++++++++ src/display.cpp | 33 +++++++++++++++++++++++++++++---- src/frame_buffer.cpp | 18 ++++++++++++++++++ src/main.cpp | 2 +- 5 files changed, 79 insertions(+), 7 deletions(-) create mode 100644 include/frame_buffer.hpp create mode 100644 src/frame_buffer.cpp diff --git a/include/display.hpp b/include/display.hpp index 96ce754..07ced6d 100644 --- a/include/display.hpp +++ b/include/display.hpp @@ -6,7 +6,7 @@ #include -// #include "../include/frame_buffer.hpp" +#include "../include/frame_buffer.hpp" namespace graphics { class Display { @@ -20,7 +20,7 @@ class Display { // std::unique_ptr surface; // std::unique_ptr event; // std::unique_ptr renderer; - // std::unique_ptr colorBuffer; + std::unique_ptr frameBuffer; public: // Constructor to initialize memory diff --git a/include/frame_buffer.hpp b/include/frame_buffer.hpp new file mode 100644 index 0000000..c3fdad4 --- /dev/null +++ b/include/frame_buffer.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include +#include + +#include "color.hpp" + +namespace graphics { + +// Represents the memory that holds the color values of the pixels +class FrameBuffer { + private: + int width; + int height; + std::vector buffer; // Changed from unique_ptr to vector + + public: + FrameBuffer(int width, int height); + ~FrameBuffer() = default; + + void setPixel(int x, int y, const Color& color); + void clear(const Color& color); + + int getWidth() const { return width; } + int getHeight() const { return height; } + const std::vector& getData() const { return buffer; } +}; + +} // namespace graphics \ No newline at end of file diff --git a/src/display.cpp b/src/display.cpp index e9dd106..a00d260 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -2,8 +2,13 @@ #include +#include "../include/constants.hpp" + namespace graphics { Display::Display() { + // Initialize the frame buffer + frameBuffer = std::make_unique(WIDTH, HEIGHT); + // Initialize the SDL window if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { fprintf( @@ -11,7 +16,8 @@ Display::Display() { exit(EXIT_FAILURE); } window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_CENTERED, - SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN); + SDL_WINDOWPOS_CENTERED, frameBuffer->getWidth(), frameBuffer->getHeight(), + SDL_WINDOW_SHOWN); if (window == nullptr) { fprintf( stderr, "Window could not be created! SDL_Error: %s\n", SDL_GetError()); @@ -25,9 +31,22 @@ Display::Display() { SDL_GetError()); exit(EXIT_FAILURE); } + + // Initialize the SDL texture + texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, + SDL_TEXTUREACCESS_STREAMING, frameBuffer->getWidth(), + frameBuffer->getHeight()); + if (texture == nullptr) { + fprintf(stderr, "Texture could not be created! SDL_Error: %s\n", + SDL_GetError()); + exit(EXIT_FAILURE); + } } Display::~Display() { + // Free the frame buffer + frameBuffer.reset(); + // Free the window SDL_DestroyWindow(window); window = nullptr; @@ -48,13 +67,19 @@ void Display::update() { } void Display::render() { - // Update the texture with the color buffer data - // SDL_UpdateTexture(texture, nullptr, colorBuffer->getData().data(), - // colorBuffer->getWidth() * sizeof(uint32_t)); + // Clear the renderer + clear(); + + // Update the texture with the frame buffer data + SDL_UpdateTexture(texture, nullptr, frameBuffer->getData().data(), + frameBuffer->getWidth() * sizeof(uint32_t)); // Copy the texture to the renderer SDL_RenderCopy(renderer, texture, nullptr, nullptr); + // Clear the frame buffer + frameBuffer->clear(Color(0, 0, 0, 255)); + // Present the renderer SDL_RenderPresent(renderer); } diff --git a/src/frame_buffer.cpp b/src/frame_buffer.cpp new file mode 100644 index 0000000..7171706 --- /dev/null +++ b/src/frame_buffer.cpp @@ -0,0 +1,18 @@ +#include "../include/frame_buffer.hpp" + +namespace graphics { + +FrameBuffer::FrameBuffer(int width, int height) + : width(width), height(height), buffer(width * height, 0) {} + +void FrameBuffer::setPixel(int x, int y, const Color& color) { + if (x >= 0 && x < width && y >= 0 && y < height) { + buffer[y * width + x] = static_cast(color); + } +} + +void FrameBuffer::clear(const Color& color) { + uint32_t clearColor = static_cast(color); + std::fill(buffer.begin(), buffer.end(), clearColor); +} +} // namespace graphics \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 080d564..604872a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,7 +15,7 @@ int main(int argc, char **argv) { graphics::Display display; // Main emulation loop - // Loop until window close button or ESC key is pressed + // Loop until window close button is pressed while (!display.shouldClose()) { display.processInput(); display.update();