Version 0 not fully working

This commit is contained in:
2025-06-21 17:23:02 -04:00
commit 32d70aa657
21 changed files with 1307 additions and 0 deletions

111
src/Graphics.cpp Normal file
View File

@@ -0,0 +1,111 @@
//
// Created by ryuuji on 6/20/25.
//
#include "Graphics.h"
#include <iostream>
void SDLWindowDestroyer::operator()(SDL_Window* window) const {
std::cout << "Destroying window" << std::endl;
SDL_DestroyWindow(window);
}
void SDLRendererDestroyer::operator()(SDL_Renderer* renderer) const {
std::cout << "Destroying renderer" << std::endl;
SDL_DestroyRenderer(renderer);
}
void SDLTextureDestroyer::operator()(SDL_Texture* texture) const {
std::cout << "Destroying texture" << std::endl;
SDL_DestroyTexture(texture);
}
Graphics::Graphics(): width(64 * 30),
height(35 * 30),
scale(30) {}
bool Graphics::init() {
SDL_SetAppMetadata("CHIP-8 Emulator", "0.0.1", "fun.skrd.chip8");
if (!SDL_Init(SDL_INIT_VIDEO)) {
SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
return false;
}
SDL_Window* raw_window = nullptr;
SDL_Renderer* raw_renderer = nullptr;
if (!SDL_CreateWindowAndRenderer("CHIP-8 Emulator", width, height, 0, &raw_window, &raw_renderer)) {
SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
return false;
}
this->window = std::unique_ptr<SDL_Window, SDLWindowDestroyer>(raw_window);
this->renderer = std::unique_ptr<SDL_Renderer, SDLRendererDestroyer>(raw_renderer);
SDL_Texture* raw_texture = SDL_CreateTexture(renderer.get(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, 64 * scale, 32 * scale);
this->texture = std::unique_ptr<SDL_Texture, SDLTextureDestroyer>(raw_texture);
return true;
}
void Graphics::draw(std::bitset<2048> display, std::string info) {
SDL_SetRenderDrawColor(renderer.get(), 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer.get());
draw_display(display);
draw_info(info);
SDL_RenderPresent(renderer.get());
}
void Graphics::draw_display(std::bitset<2048> display) {
SDL_Surface* surface = nullptr;
if (SDL_LockTextureToSurface(texture.get(), nullptr, &surface)) {
SDL_FillSurfaceRect(surface, nullptr, SDL_MapRGB(SDL_GetPixelFormatDetails(surface->format), nullptr, 0, 0, 0));
for (int i = 0; i < display.size(); i++) {
if (display[i]) {
int x = (i % 64) * scale;
int y = (i / 64) * scale;
SDL_Rect rect = {x, y, scale, scale};
Uint32 color = SDL_MapRGB(
SDL_GetPixelFormatDetails(surface->format),
nullptr,
0,
255,
0
);
SDL_FillSurfaceRect(surface, &rect, color);
}
}
SDL_UnlockTexture(texture.get());
}
SDL_FRect destination_rect{0.0, 0.0, static_cast<float>(64 * scale), static_cast<float>(32 * scale)};
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);
}