Usando el estado en los graficos

This commit is contained in:
2025-06-22 00:59:42 -04:00
parent 64294d01ac
commit 928b203ef5
4 changed files with 18 additions and 31 deletions

View File

@@ -8,6 +8,7 @@
#include "bitops.h" #include "bitops.h"
Chip8::Chip8(): machine_state{std::make_shared<MachineState>()}, Chip8::Chip8(): machine_state{std::make_shared<MachineState>()},
graphics{machine_state},
interpreter{machine_state, 0}, interpreter{machine_state, 0},
target_cycle_time{1.0 / 700.0}, target_cycle_time{1.0 / 700.0},
last_update_time{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() { void Chip8::execute_instructions() {

View File

@@ -22,9 +22,10 @@ void SDLTextureDestroyer::operator()(SDL_Texture* texture) const {
SDL_DestroyTexture(texture); SDL_DestroyTexture(texture);
} }
Graphics::Graphics(): width(64 * 30), Graphics::Graphics(std::shared_ptr<MachineState> machine_state): machine_state{std::move(machine_state)},
height(35 * 30), width{64 * 30},
scale(30) {} height{32 * 30},
scale{30} {}
bool Graphics::init() { bool Graphics::init() {
@@ -51,18 +52,18 @@ bool Graphics::init() {
return true; return true;
} }
void Graphics::draw(std::array<bool, 2048> display, std::string info) { void Graphics::draw() {
SDL_SetRenderDrawColor(renderer.get(), 0, 0, 0, SDL_ALPHA_OPAQUE); SDL_SetRenderDrawColor(renderer.get(), 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer.get()); SDL_RenderClear(renderer.get());
draw_display(display); draw_display();
draw_info(info);
SDL_RenderPresent(renderer.get()); SDL_RenderPresent(renderer.get());
} }
void Graphics::draw_display(std::array<bool, 2048> display) { void Graphics::draw_display() {
SDL_Surface* surface = nullptr; SDL_Surface* surface = nullptr;
auto& display = machine_state->display;
if (SDL_LockTextureToSurface(texture.get(), nullptr, &surface)) { if (SDL_LockTextureToSurface(texture.get(), nullptr, &surface)) {
SDL_FillSurfaceRect(surface, nullptr, SDL_MapRGB(SDL_GetPixelFormatDetails(surface->format), nullptr, 0, 0, 0)); SDL_FillSurfaceRect(surface, nullptr, SDL_MapRGB(SDL_GetPixelFormatDetails(surface->format), nullptr, 0, 0, 0));
@@ -92,21 +93,3 @@ void Graphics::draw_display(std::array<bool, 2048> display) {
SDL_RenderTexture(renderer.get(), texture.get(), nullptr, &destination_rect); 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<float>(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<float>(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);
}

View File

@@ -7,6 +7,7 @@
#include <bitset> #include <bitset>
#include <memory> #include <memory>
#include "MachineState.h"
#include "SDL3/SDL.h" #include "SDL3/SDL.h"
struct SDLWindowDestroyer { struct SDLWindowDestroyer {
@@ -20,6 +21,8 @@ struct SDLTextureDestroyer {
}; };
class Graphics { class Graphics {
std::shared_ptr<MachineState> machine_state;
std::shared_ptr<SDL_Window> window; std::shared_ptr<SDL_Window> window;
std::shared_ptr<SDL_Renderer> renderer; std::shared_ptr<SDL_Renderer> renderer;
std::shared_ptr<SDL_Texture> texture; std::shared_ptr<SDL_Texture> texture;
@@ -28,12 +31,12 @@ class Graphics {
int height; int height;
int scale; int scale;
void draw_display(std::array<bool, 2048> display); void draw_display();
void draw_info(std::string info);
public: public:
Graphics(); Graphics(std::shared_ptr<MachineState> machine_state);
bool init(); bool init();
void draw(std::array<bool, 2048> display, std::string info); void draw();
}; };

View File

@@ -10,7 +10,7 @@
#include "SDL3/SDL_log.h" #include "SDL3/SDL_log.h"
Interpreter::Interpreter(std::shared_ptr<MachineState> machine_state, uint8_t quirks): Interpreter::Interpreter(std::shared_ptr<MachineState> machine_state, uint8_t quirks):
machine_state{machine_state}, machine_state{std::move(machine_state)},
quirks{quirks} { quirks{quirks} {
this->random_generator = std::mt19937(std::random_device{}()); this->random_generator = std::mt19937(std::random_device{}());
this->load_fonts(); this->load_fonts();