From 32837712fedf4557ff44df7d5b8ddbd40c5f7990 Mon Sep 17 00:00:00 2001 From: kotorifan Date: Fri, 23 Jan 2026 15:15:48 +0100 Subject: Stupid problems with the old git repo. New one, unfortunately. --- src/SDL_stuff.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/SDL_stuff.cpp (limited to 'src/SDL_stuff.cpp') diff --git a/src/SDL_stuff.cpp b/src/SDL_stuff.cpp new file mode 100644 index 0000000..e2323e0 --- /dev/null +++ b/src/SDL_stuff.cpp @@ -0,0 +1,86 @@ +#include +#include +#include + +#include "SDL_stuff.hpp" +#include "Common.hpp" + +namespace DMG01 +{ + Generic_SDL_Window::Generic_SDL_Window(const uint32_t win_x, const uint32_t win_y) + { + this->win_x = win_x; + this->win_y = win_y; + } + Generic_SDL_Window::~Generic_SDL_Window() + { + SDL_DestroyTexture(this->texture); + SDL_DestroyRenderer(this->renderer); + SDL_DestroyWindow(this->window); + TTF_Quit(); + SDL_Quit(); + } + void Generic_SDL_Window::create_window() + { + if(SDL_Init(SDL_INIT_EVERYTHING) < 0) { + throw std::runtime_error("SDL couldn't be intitalized"); + } + if(TTF_Init() < 0) { + throw std::runtime_error("SDL_ttf couldn't be initalized"); + } + this->window = SDL_CreateWindow(WINDOW_NAME_DEBUG, + SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, + this->win_x, + this->win_y, + SDL_WINDOW_OPENGL); + if(!this->window) { + throw std::runtime_error("Failed to create SDL window"); + } + this->renderer = SDL_CreateRenderer(this->window, -1, 0); + if(!this->renderer) { + throw std::runtime_error("Failed to create SDL renderer"); + } + this->font = TTF_OpenFont(FONT_DEFAULT_DEBUG, + FONT_SIZE_DEFAULT_DEBUG); + if(!font) { + throw std::runtime_error("Couldn't load font for SDL window"); + } + } + + void Generic_SDL_Window::update_text(const char* message) + { + if(this->texture) SDL_DestroyTexture(this->texture); + SDL_Surface* tmp_surface = TTF_RenderText_Solid(this->font, message, this->white); + if(!tmp_surface) return; + this->texture = SDL_CreateTextureFromSurface(this->renderer, tmp_surface); + this->rectangle.x = 0; + this->rectangle.y = 0; + this->rectangle.w = tmp_surface->w; + this->rectangle.h = tmp_surface->h; + SDL_FreeSurface(tmp_surface); + } + bool Generic_SDL_Window::check_polling_event() + { + while(SDL_PollEvent(&this->event)) { + switch(event.type) { + case SDL_QUIT: return false; + } + } + return true; + } + void Generic_SDL_Window::render_text() + { + SDL_SetRenderDrawColor(this->renderer, + this->black.r, + this->black.g, + this->black.b, + this->black.a); + SDL_RenderClear(this->renderer); + if(this->texture) { + SDL_RenderCopy(this->renderer, this->texture, NULL, &this->rectangle); + } + SDL_RenderPresent(this->renderer); + } + +} -- cgit v1.3