aboutsummaryrefslogtreecommitdiffstats
path: root/src/SDL_stuff.cpp
blob: e2323e0e3932e9f192fe6165c4401c935af7e034 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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);
	}

}