diff options
| author | kotorifan <kotorifan05@gmail.com> | 2026-01-23 15:15:48 +0100 |
|---|---|---|
| committer | kotorifan <kotorifan05@gmail.com> | 2026-02-04 09:02:16 +0100 |
| commit | 32837712fedf4557ff44df7d5b8ddbd40c5f7990 (patch) | |
| tree | 671d2b82f45d5e6de70ac5d15f59ea94b884d4b6 /src/SDL_stuff.cpp | |
| download | gameboy-32837712fedf4557ff44df7d5b8ddbd40c5f7990.tar.gz | |
Stupid problems with the old git repo. New one, unfortunately.
Diffstat (limited to 'src/SDL_stuff.cpp')
| -rw-r--r-- | src/SDL_stuff.cpp | 86 |
1 files changed, 86 insertions, 0 deletions
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 <SDL2/SDL.h> +#include <SDL2/SDL_ttf.h> +#include <stdexcept> + +#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); + } + +} |
