Skip to content

Commit

Permalink
Add frame buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaweees committed Oct 4, 2024
1 parent 41e4393 commit a4ae368
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 7 deletions.
4 changes: 2 additions & 2 deletions include/display.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <memory>

// #include "../include/frame_buffer.hpp"
#include "../include/frame_buffer.hpp"

namespace graphics {
class Display {
Expand All @@ -20,7 +20,7 @@ class Display {
// std::unique_ptr<SDL_Surface, decltype(&SDL_FreeSurface)> surface;
// std::unique_ptr<SDL_Event> event;
// std::unique_ptr<SDL_Renderer> renderer;
// std::unique_ptr<ColorBuffer> colorBuffer;
std::unique_ptr<FrameBuffer> frameBuffer;

public:
// Constructor to initialize memory
Expand Down
29 changes: 29 additions & 0 deletions include/frame_buffer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <cstdint>
#include <vector>

#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<uint32_t> 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<uint32_t>& getData() const { return buffer; }
};

} // namespace graphics
33 changes: 29 additions & 4 deletions src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@

#include <SDL2/SDL.h>

#include "../include/constants.hpp"

namespace graphics {
Display::Display() {
// Initialize the frame buffer
frameBuffer = std::make_unique<FrameBuffer>(WIDTH, HEIGHT);

// Initialize the SDL window
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
fprintf(
stderr, "SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
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());
Expand All @@ -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;
Expand All @@ -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);
}
Expand Down
18 changes: 18 additions & 0 deletions src/frame_buffer.cpp
Original file line number Diff line number Diff line change
@@ -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<uint32_t>(color);
}
}

void FrameBuffer::clear(const Color& color) {
uint32_t clearColor = static_cast<uint32_t>(color);
std::fill(buffer.begin(), buffer.end(), clearColor);
}
} // namespace graphics
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit a4ae368

Please sign in to comment.