From 928b203ef529e0de1193bba68c8717cce60f1bac Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 22 Jun 2025 00:59:42 -0400 Subject: [PATCH] Usando el estado en los graficos --- src/Chip8.cpp | 3 ++- src/Graphics.cpp | 33 ++++++++------------------------- src/Graphics.h | 11 +++++++---- src/Interpreter.cpp | 2 +- 4 files changed, 18 insertions(+), 31 deletions(-) diff --git a/src/Chip8.cpp b/src/Chip8.cpp index 294e10c..ff2a68d 100644 --- a/src/Chip8.cpp +++ b/src/Chip8.cpp @@ -8,6 +8,7 @@ #include "bitops.h" Chip8::Chip8(): machine_state{std::make_shared()}, + graphics{machine_state}, interpreter{machine_state, 0}, target_cycle_time{1.0 / 700.0}, last_update_time{0}, @@ -104,7 +105,7 @@ void Chip8::update() { } - graphics.draw(machine_state->display, buffer.str()); + graphics.draw(); } void Chip8::execute_instructions() { diff --git a/src/Graphics.cpp b/src/Graphics.cpp index c0559bd..10fb299 100644 --- a/src/Graphics.cpp +++ b/src/Graphics.cpp @@ -22,9 +22,10 @@ void SDLTextureDestroyer::operator()(SDL_Texture* texture) const { SDL_DestroyTexture(texture); } -Graphics::Graphics(): width(64 * 30), - height(35 * 30), - scale(30) {} +Graphics::Graphics(std::shared_ptr machine_state): machine_state{std::move(machine_state)}, + width{64 * 30}, + height{32 * 30}, + scale{30} {} bool Graphics::init() { @@ -51,18 +52,18 @@ bool Graphics::init() { return true; } -void Graphics::draw(std::array display, std::string info) { +void Graphics::draw() { SDL_SetRenderDrawColor(renderer.get(), 0, 0, 0, SDL_ALPHA_OPAQUE); SDL_RenderClear(renderer.get()); - draw_display(display); - draw_info(info); + draw_display(); SDL_RenderPresent(renderer.get()); } -void Graphics::draw_display(std::array display) { +void Graphics::draw_display() { SDL_Surface* surface = nullptr; + auto& display = machine_state->display; if (SDL_LockTextureToSurface(texture.get(), nullptr, &surface)) { SDL_FillSurfaceRect(surface, nullptr, SDL_MapRGB(SDL_GetPixelFormatDetails(surface->format), nullptr, 0, 0, 0)); @@ -92,21 +93,3 @@ void Graphics::draw_display(std::array display) { SDL_RenderTexture(renderer.get(), texture.get(), nullptr, &destination_rect); } -void Graphics::draw_info(std::string info) { - const int charsize = SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE; - const float screen_scale = static_cast(scale); - const float text_scale = 2.0f; - - const float info_area_start = 32.0f * screen_scale; - const float info_area_height = (35.0f - 32.0f) * screen_scale; - const float info_area_center = info_area_start + (info_area_height / 2.0f); - - const float text_line = info_area_center / text_scale - (static_cast(charsize) / 2.0f); - - SDL_SetRenderScale(renderer.get(), text_scale, text_scale); - SDL_SetRenderDrawColor(renderer.get(), 255, 255, 255, SDL_ALPHA_OPAQUE); - - SDL_RenderDebugText(renderer.get(), 10 / text_scale, text_line, info.data()); - - SDL_SetRenderScale(renderer.get(), 1.0f, 1.0f); -} diff --git a/src/Graphics.h b/src/Graphics.h index 40b0889..4ee4959 100644 --- a/src/Graphics.h +++ b/src/Graphics.h @@ -7,6 +7,7 @@ #include #include +#include "MachineState.h" #include "SDL3/SDL.h" struct SDLWindowDestroyer { @@ -20,6 +21,8 @@ struct SDLTextureDestroyer { }; class Graphics { + std::shared_ptr machine_state; + std::shared_ptr window; std::shared_ptr renderer; std::shared_ptr texture; @@ -28,12 +31,12 @@ class Graphics { int height; int scale; - void draw_display(std::array display); - void draw_info(std::string info); + void draw_display(); public: - Graphics(); + Graphics(std::shared_ptr machine_state); + bool init(); - void draw(std::array display, std::string info); + void draw(); }; diff --git a/src/Interpreter.cpp b/src/Interpreter.cpp index 3784b17..5854519 100644 --- a/src/Interpreter.cpp +++ b/src/Interpreter.cpp @@ -10,7 +10,7 @@ #include "SDL3/SDL_log.h" Interpreter::Interpreter(std::shared_ptr machine_state, uint8_t quirks): - machine_state{machine_state}, + machine_state{std::move(machine_state)}, quirks{quirks} { this->random_generator = std::mt19937(std::random_device{}()); this->load_fonts();