Added more methods to the renderer.

This commit is contained in:
Relintai 2021-03-29 22:04:42 +02:00
parent b04d22497f
commit 692f0b76da
2 changed files with 66 additions and 13 deletions

View File

@ -11,7 +11,7 @@ void Renderer::set_draw_color(const Uint8 r, const Uint8 g, const Uint8 b, const
} }
void Renderer::set_draw_color(const Color &color) { void Renderer::set_draw_color(const Color &color) {
SDL_SetRenderDrawColor(_renderer, color.r, color.g, color.b, color.a); SDL_SetRenderDrawColor(_renderer, color.r, color.g, color.b, color.a);
} }
void Renderer::clear() { void Renderer::clear() {
@ -19,16 +19,51 @@ void Renderer::clear() {
} }
void Renderer::draw_rect(const SDL_Rect &rect) { void Renderer::draw_rect(const SDL_Rect &rect) {
SDL_RenderFillRect(_renderer, &rect); SDL_RenderFillRect(_renderer, &rect);
} }
void Renderer::draw_rect(const Rect2 &rect) { void Renderer::draw_rect(const Rect2 &rect) {
SDL_Rect r; SDL_Rect r;
r.x = rect.x; r.x = rect.x;
r.y = rect.y; r.y = rect.y;
r.w = rect.w; r.w = rect.w;
r.h = rect.h; r.h = rect.h;
SDL_RenderFillRect(_renderer, &r); SDL_RenderFillRect(_renderer, &r);
}
int Renderer::get_dpi() const {
float ddpi;
float hdpi;
float vdpi;
if (SDL_GetDisplayDPI(_window_display_index, &ddpi, &hdpi, &vdpi)) {
return ddpi;
}
//fallback
return 1;
}
int Renderer::get_size_w() const {
int w;
int h;
SDL_GetWindowSize(_window, &w, &h);
return w;
}
int Renderer::get_size_h() const {
int w;
int h;
SDL_GetWindowSize(_window, &w, &h);
return h;
}
void Renderer::get_size(int *w, int *h) const {
SDL_GetWindowSize(_window, w, h);
} }
void Renderer::initialize() { void Renderer::initialize() {
@ -38,22 +73,24 @@ void Renderer::initialize() {
return; return;
} }
if (SDL_CreateWindowAndRenderer(640, 480, _window_flags, &_window, &_renderer) != 0) { if (SDL_CreateWindowAndRenderer(_initial_window_width, _initial_window_height, _window_flags, &_window, &_renderer) != 0) {
printf("SDL_CreateWindowAndRenderer() hiba!\n"); printf("SDL_CreateWindowAndRenderer() hiba!\n");
return; return;
} }
_window_display_index = SDL_GetWindowDisplayIndex(_window);
} }
void Renderer::destroy() { void Renderer::destroy() {
if (_window) if (_window)
SDL_DestroyWindow(_window); SDL_DestroyWindow(_window);
if (_renderer) if (_renderer)
SDL_DestroyRenderer(_renderer); SDL_DestroyRenderer(_renderer);
_window = nullptr; _window = nullptr;
_renderer = nullptr; _renderer = nullptr;
} }
Renderer::Renderer() { Renderer::Renderer() {
@ -61,6 +98,9 @@ Renderer::Renderer() {
printf("Renderer::Renderer(): _singleton is not null!\n"); printf("Renderer::Renderer(): _singleton is not null!\n");
} }
_initial_window_width = 640;
_initial_window_height = 480;
_singleton = this; _singleton = this;
_flags = SDL_INIT_VIDEO | SDL_INIT_TIMER; _flags = SDL_INIT_VIDEO | SDL_INIT_TIMER;
@ -69,11 +109,14 @@ Renderer::Renderer() {
initialize(); initialize();
} }
Renderer::Renderer(unsigned int flags, unsigned int window_flags) { Renderer::Renderer(unsigned int flags, unsigned int window_flags, int initial_window_width, int initial_window_height) {
if (_singleton) { if (_singleton) {
printf("Renderer::Renderer(flags): _singleton is not null!\n"); printf("Renderer::Renderer(flags): _singleton is not null!\n");
} }
_initial_window_width = initial_window_width;
_initial_window_height = initial_window_height;
_singleton = this; _singleton = this;
_flags = flags; _flags = flags;

View File

@ -16,22 +16,32 @@ public:
void draw_rect(const SDL_Rect &rect); void draw_rect(const SDL_Rect &rect);
void draw_rect(const Rect2 &rect); void draw_rect(const Rect2 &rect);
int get_dpi() const;
int get_size_w() const;
int get_size_h() const;
void get_size(int *w, int *h) const;
void initialize(); void initialize();
void destroy(); void destroy();
Renderer(); Renderer();
Renderer(unsigned int flags, unsigned int window_flags); Renderer(unsigned int flags, unsigned int window_flags, int window_width = 640, int window_height = 480);
virtual ~Renderer(); virtual ~Renderer();
static Renderer *get_singleton(); static Renderer *get_singleton();
private: private:
int _initial_window_width;
int _initial_window_height;
unsigned int _flags; unsigned int _flags;
unsigned int _window_flags; unsigned int _window_flags;
SDL_Window *_window; SDL_Window *_window;
SDL_Renderer *_renderer; SDL_Renderer *_renderer;
int _window_display_index;
static Renderer *_singleton; static Renderer *_singleton;
}; };