From 6fce40c7ef6e8f648e2ea24ad8a29969cf93d6ba Mon Sep 17 00:00:00 2001 From: Relintai Date: Wed, 20 Dec 2023 15:38:53 +0100 Subject: [PATCH] More organization. --- .../input/SCsub | 0 .../input/default_controller_mappings.gen.cpp | 0 .../input/default_controller_mappings.h | 0 .../input/gamecontrollerdb.txt | 0 .../input/input.cpp | 0 .../input/input.h | 0 .../input/input_builders.py | 0 .../input/input_event.cpp | 0 .../input/input_event.h | 0 .../input/input_map.cpp | 0 .../input/input_map.h | 0 .../input/pandemoniumcontrollerdb.txt | 0 .../input/shortcut.cpp | 0 .../input/shortcut.h | 0 sfw/render_immediate/renderer.cpp | 369 ++++++++++++++++++ sfw/render_immediate/renderer.h | 93 +++++ .../camera_2d.cpp | 0 .../camera_2d.h | 0 .../camera_3d.cpp | 0 .../camera_3d.h | 0 .../mesh_instance_2d.cpp | 0 .../mesh_instance_2d.h | 0 .../mesh_instance_3d.cpp | 0 .../mesh_instance_3d.h | 0 .../object_2d.cpp | 0 .../object_2d.h | 0 .../object_3d.cpp | 0 .../object_3d.h | 0 .../sprite.cpp | 0 sfw/{render_core => render_objects}/sprite.h | 0 .../tile_map.cpp | 0 .../tile_map.h | 0 32 files changed, 462 insertions(+) rename sfw/{render_objects => render_core}/input/SCsub (100%) rename sfw/{render_objects => render_core}/input/default_controller_mappings.gen.cpp (100%) rename sfw/{render_objects => render_core}/input/default_controller_mappings.h (100%) rename sfw/{render_objects => render_core}/input/gamecontrollerdb.txt (100%) rename sfw/{render_objects => render_core}/input/input.cpp (100%) rename sfw/{render_objects => render_core}/input/input.h (100%) rename sfw/{render_objects => render_core}/input/input_builders.py (100%) rename sfw/{render_objects => render_core}/input/input_event.cpp (100%) rename sfw/{render_objects => render_core}/input/input_event.h (100%) rename sfw/{render_objects => render_core}/input/input_map.cpp (100%) rename sfw/{render_objects => render_core}/input/input_map.h (100%) rename sfw/{render_objects => render_core}/input/pandemoniumcontrollerdb.txt (100%) rename sfw/{render_objects => render_core}/input/shortcut.cpp (100%) rename sfw/{render_objects => render_core}/input/shortcut.h (100%) create mode 100644 sfw/render_immediate/renderer.cpp create mode 100644 sfw/render_immediate/renderer.h rename sfw/{render_core => render_objects}/camera_2d.cpp (100%) rename sfw/{render_core => render_objects}/camera_2d.h (100%) rename sfw/{render_core => render_objects}/camera_3d.cpp (100%) rename sfw/{render_core => render_objects}/camera_3d.h (100%) rename sfw/{render_core => render_objects}/mesh_instance_2d.cpp (100%) rename sfw/{render_core => render_objects}/mesh_instance_2d.h (100%) rename sfw/{render_core => render_objects}/mesh_instance_3d.cpp (100%) rename sfw/{render_core => render_objects}/mesh_instance_3d.h (100%) rename sfw/{render_core => render_objects}/object_2d.cpp (100%) rename sfw/{render_core => render_objects}/object_2d.h (100%) rename sfw/{render_core => render_objects}/object_3d.cpp (100%) rename sfw/{render_core => render_objects}/object_3d.h (100%) rename sfw/{render_core => render_objects}/sprite.cpp (100%) rename sfw/{render_core => render_objects}/sprite.h (100%) rename sfw/{render_core => render_objects}/tile_map.cpp (100%) rename sfw/{render_core => render_objects}/tile_map.h (100%) diff --git a/sfw/render_objects/input/SCsub b/sfw/render_core/input/SCsub similarity index 100% rename from sfw/render_objects/input/SCsub rename to sfw/render_core/input/SCsub diff --git a/sfw/render_objects/input/default_controller_mappings.gen.cpp b/sfw/render_core/input/default_controller_mappings.gen.cpp similarity index 100% rename from sfw/render_objects/input/default_controller_mappings.gen.cpp rename to sfw/render_core/input/default_controller_mappings.gen.cpp diff --git a/sfw/render_objects/input/default_controller_mappings.h b/sfw/render_core/input/default_controller_mappings.h similarity index 100% rename from sfw/render_objects/input/default_controller_mappings.h rename to sfw/render_core/input/default_controller_mappings.h diff --git a/sfw/render_objects/input/gamecontrollerdb.txt b/sfw/render_core/input/gamecontrollerdb.txt similarity index 100% rename from sfw/render_objects/input/gamecontrollerdb.txt rename to sfw/render_core/input/gamecontrollerdb.txt diff --git a/sfw/render_objects/input/input.cpp b/sfw/render_core/input/input.cpp similarity index 100% rename from sfw/render_objects/input/input.cpp rename to sfw/render_core/input/input.cpp diff --git a/sfw/render_objects/input/input.h b/sfw/render_core/input/input.h similarity index 100% rename from sfw/render_objects/input/input.h rename to sfw/render_core/input/input.h diff --git a/sfw/render_objects/input/input_builders.py b/sfw/render_core/input/input_builders.py similarity index 100% rename from sfw/render_objects/input/input_builders.py rename to sfw/render_core/input/input_builders.py diff --git a/sfw/render_objects/input/input_event.cpp b/sfw/render_core/input/input_event.cpp similarity index 100% rename from sfw/render_objects/input/input_event.cpp rename to sfw/render_core/input/input_event.cpp diff --git a/sfw/render_objects/input/input_event.h b/sfw/render_core/input/input_event.h similarity index 100% rename from sfw/render_objects/input/input_event.h rename to sfw/render_core/input/input_event.h diff --git a/sfw/render_objects/input/input_map.cpp b/sfw/render_core/input/input_map.cpp similarity index 100% rename from sfw/render_objects/input/input_map.cpp rename to sfw/render_core/input/input_map.cpp diff --git a/sfw/render_objects/input/input_map.h b/sfw/render_core/input/input_map.h similarity index 100% rename from sfw/render_objects/input/input_map.h rename to sfw/render_core/input/input_map.h diff --git a/sfw/render_objects/input/pandemoniumcontrollerdb.txt b/sfw/render_core/input/pandemoniumcontrollerdb.txt similarity index 100% rename from sfw/render_objects/input/pandemoniumcontrollerdb.txt rename to sfw/render_core/input/pandemoniumcontrollerdb.txt diff --git a/sfw/render_objects/input/shortcut.cpp b/sfw/render_core/input/shortcut.cpp similarity index 100% rename from sfw/render_objects/input/shortcut.cpp rename to sfw/render_core/input/shortcut.cpp diff --git a/sfw/render_objects/input/shortcut.h b/sfw/render_core/input/shortcut.h similarity index 100% rename from sfw/render_objects/input/shortcut.h rename to sfw/render_core/input/shortcut.h diff --git a/sfw/render_immediate/renderer.cpp b/sfw/render_immediate/renderer.cpp new file mode 100644 index 0000000..ba3adb9 --- /dev/null +++ b/sfw/render_immediate/renderer.cpp @@ -0,0 +1,369 @@ +#include "renderer.h" + +#include + +#include "core/math.h" + +void Renderer::present() { + SDL_RenderPresent(_renderer); +} + +void Renderer::set_draw_color(const Uint8 r, const Uint8 g, const Uint8 b, const Uint8 a) { + SDL_SetRenderDrawColor(_renderer, r, g, b, a); +} + +void Renderer::set_draw_color(const Color &color) { + SDL_SetRenderDrawColor(_renderer, color.r, color.g, color.b, color.a); +} + +void Renderer::clear() { + SDL_RenderClear(_renderer); +} + +SDL_BlendMode Renderer::get_blend_mode() const { + SDL_BlendMode mode; + SDL_GetRenderDrawBlendMode(_renderer, &mode); + + return mode; +} +void Renderer::set_blend_mode(const SDL_BlendMode mode) { + SDL_SetRenderDrawBlendMode(_renderer, mode); +} + +void Renderer::draw_point(const int x, const int y) { + SDL_RenderDrawPoint(_renderer, x, y); +} +void Renderer::draw_point(const float x, const float y) { + SDL_RenderDrawPointF(_renderer, x, y); +} + +void Renderer::draw_line(const int x1, const int x2, const int y1, const int y2) { + SDL_RenderDrawLine(_renderer, x1, x2, y1, y2); +} +void Renderer::draw_line(const float x1, const float x2, const float y1, const float y2) { + SDL_RenderDrawLineF(_renderer, x1, x2, y1, y2); +} + +void Renderer::draw_rect(const Rect2 &rect) { + SDL_Rect r = rect.as_rect(); + + SDL_RenderDrawRect(_renderer, &r); +} + +void Renderer::draw_fill_rect(const Rect2 &rect) { + SDL_Rect r = rect.as_rect(); + + SDL_RenderFillRect(_renderer, &r); +} + +void Renderer::draw_texture(const Texture &texture, const Rect2 &dst_rect) { + SDL_Rect sr; + + sr.x = 0; + sr.y = 0; + sr.w = texture.get_width(); + sr.h = texture.get_height(); + + SDL_Rect dr = dst_rect.as_rect(); + + SDL_RenderCopy(_renderer, texture.get_texture(), &sr, &dr); +} +void Renderer::draw_texture(const Texture &texture, const Rect2 &src_rect, const Rect2 &dst_rect) { + SDL_Rect sr = src_rect.as_rect(); + SDL_Rect dr = dst_rect.as_rect(); + + SDL_RenderCopy(_renderer, texture.get_texture(), &sr, &dr); +} +void Renderer::draw_texture(const Texture &texture, const Rect2 &src_rect, const Rect2 &dst_rect, const double angle, const float cx, const float cy, const SDL_RendererFlip flip) { + SDL_Rect sr = src_rect.as_rect(); + SDL_FRect dr = dst_rect.as_frect(); + + SDL_FPoint p; + + p.x = cx; + p.y = cy; + + SDL_RenderCopyExF(_renderer, texture.get_texture(), &sr, &dr, angle, &p, flip); +} + +void Renderer::draw_sprite(const Sprite &sprite) { + Texture *t = sprite.get_texture(); + + if (!t) { + return; + } + + t->set_color_mod(sprite.get_color_mod()); + + double angle = sprite.get_angle(); + + if (Math::is_zero_approx(angle)) { + SDL_Rect sr = sprite.get_texture_clip_rect().as_rect(); + SDL_Rect dr = sprite.get_transform().as_rect(); + + SDL_RenderCopy(_renderer, t->get_texture(), &sr, &dr); + } else { + SDL_Rect sr = sprite.get_texture_clip_rect().as_rect(); + SDL_FRect dr = sprite.get_transform().as_frect(); + + SDL_FPoint p; + + p.x = sprite.get_anchor_x(); + p.y = sprite.get_anchor_y(); + + SDL_RenderCopyExF(_renderer, t->get_texture(), &sr, &dr, angle, &p, sprite.get_flip()); + } +} + +void Renderer::draw_sprite(const Sprite *sprite) { + if (!sprite) + return; + + Texture *t = sprite->get_texture(); + + if (!t) { + return; + } + + t->set_color_mod(sprite->get_color_mod()); + + double angle = sprite->get_angle(); + + if (Math::is_zero_approx(angle)) { + SDL_Rect sr = sprite->get_texture_clip_rect().as_rect(); + SDL_Rect dr = sprite->get_transform().as_rect(); + + SDL_RenderCopy(_renderer, t->get_texture(), &sr, &dr); + } else { + SDL_Rect sr = sprite->get_texture_clip_rect().as_rect(); + SDL_FRect dr = sprite->get_transform().as_frect(); + + SDL_FPoint p; + + p.x = sprite->get_anchor_x(); + p.y = sprite->get_anchor_y(); + + SDL_RenderCopyExF(_renderer, t->get_texture(), &sr, &dr, angle, &p, sprite->get_flip()); + } +} + +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_window_size_w() const { + int w; + int h; + + SDL_GetWindowSize(_window, &w, &h); + + return w; +} + +int Renderer::get_window_size_h() const { + int w; + int h; + + SDL_GetWindowSize(_window, &w, &h); + + return h; +} + +int Renderer::get_size_w() const { + int w; + int h; + + SDL_RenderGetLogicalSize(_renderer, &w, &h); + + return w; +} + +int Renderer::get_size_h() const { + int w; + int h; + + SDL_RenderGetLogicalSize(_renderer, &w, &h); + + return h; +} + +void Renderer::get_size(int *w, int *h) const { + SDL_RenderGetLogicalSize(_renderer, w, h); +} + +void Renderer::set_size(const int w, const int h) const { + SDL_RenderSetLogicalSize(_renderer, w, h); +} + +float Renderer::get_scale_w() const { + float w; + float h; + + SDL_RenderGetScale(_renderer, &w, &h); + + return w; +} +float Renderer::get_scale_h() const { + float w; + float h; + + SDL_RenderGetScale(_renderer, &w, &h); + + return h; +} +void Renderer::set_scale(const float w, const float h) const { + SDL_RenderSetScale(_renderer, w, h); +} +void Renderer::get_scale(float *w, float *h) const { + SDL_RenderGetScale(_renderer, w, h); +} + +bool Renderer::get_integer_scaling() const { + return SDL_RenderGetIntegerScale(_renderer); +} +void Renderer::set_integer_scaling(const bool enable) { + if (enable) + SDL_RenderSetIntegerScale(_renderer, SDL_TRUE); + else + SDL_RenderSetIntegerScale(_renderer, SDL_FALSE); +} + +Rect2 Renderer::get_viewport() const { + SDL_Rect r; + + SDL_RenderGetViewport(_renderer, &r); + + return Rect2(r.x, r.y, r.w, r.h); +} +void Renderer::set_viewport(const Rect2 &rect) const { + SDL_Rect r = rect.as_rect(); + + SDL_RenderSetViewport(_renderer, &r); +} + +Rect2 Renderer::get_clip_rect() const { + SDL_Rect r; + + SDL_RenderGetClipRect(_renderer, &r); + + return Rect2(r.x, r.y, r.w, r.h); +} +void Renderer::set_clip_rect(Rect2 *rect) const { + if (rect) { + SDL_Rect r = rect->as_rect(); + + SDL_RenderSetClipRect(_renderer, &r); + } else { + SDL_RenderSetClipRect(_renderer, nullptr); + } +} +bool Renderer::clip_rect_enabled() const { + return SDL_RenderIsClipEnabled(_renderer); +} + +bool Renderer::render_target_supported() { + return SDL_RenderTargetSupported(_renderer); +} + +SDL_Texture *Renderer::get_render_target() { + return SDL_GetRenderTarget(_renderer); +} +void Renderer::set_render_target(Texture *texture) { + if (texture) { + SDL_SetRenderTarget(_renderer, texture->get_texture()); + } else { + SDL_SetRenderTarget(_renderer, nullptr); + } +} + +void Renderer::initialize() { + if (SDL_Init(_flags) != 0) { + printf("SDL_Init() hiba!\n"); + + return; + } + + if (SDL_CreateWindowAndRenderer(_initial_window_width, _initial_window_height, _window_flags, &_window, &_renderer) != 0) { + printf("SDL_CreateWindowAndRenderer() hiba!\n"); + + return; + } + + _window_display_index = SDL_GetWindowDisplayIndex(_window); +} + +void Renderer::destroy() { + if (_window) + SDL_DestroyWindow(_window); + + if (_renderer) + SDL_DestroyRenderer(_renderer); + + _window = nullptr; + _renderer = nullptr; +} + +SDL_Window *Renderer::get_window() { + return _window; +} + +SDL_Renderer *Renderer::get_renderer() { + return _renderer; +} + +Renderer::Renderer() { + if (_singleton) { + printf("Renderer::Renderer(): _singleton is not null!\n"); + } + + _initial_window_width = 640; + _initial_window_height = 480; + + _singleton = this; + + _flags = SDL_INIT_VIDEO | SDL_INIT_TIMER; + _window_flags = SDL_WINDOW_SHOWN; + + initialize(); +} + +Renderer::Renderer(unsigned int flags, unsigned int window_flags, int initial_window_width, int initial_window_height) { + if (_singleton) { + printf("Renderer::Renderer(flags): _singleton is not null!\n"); + } + + _initial_window_width = initial_window_width; + _initial_window_height = initial_window_height; + + _singleton = this; + + _flags = flags; + _window_flags = window_flags; + + initialize(); +} + +Renderer::~Renderer() { + if (_singleton != this) { + printf("Renderer::~Renderer(): _singleton is not this!\n"); + } + + _singleton = nullptr; + + destroy(); +} + +Renderer *Renderer::get_singleton() { + return _singleton; +} + +Renderer *Renderer::_singleton = nullptr; \ No newline at end of file diff --git a/sfw/render_immediate/renderer.h b/sfw/render_immediate/renderer.h new file mode 100644 index 0000000..516e553 --- /dev/null +++ b/sfw/render_immediate/renderer.h @@ -0,0 +1,93 @@ +#ifndef RENDERER_H +#define RENDERER_H + +#include "core/color.h" +#include "core/rect2.h" +#include "sprite.h" +#include "texture.h" + +#include + +class Renderer { +public: + void present(); + void set_draw_color(const Uint8 r, const Uint8 g, const Uint8 b, const Uint8 a); + void set_draw_color(const Color &color); + void clear(); + + SDL_BlendMode get_blend_mode() const; + void set_blend_mode(const SDL_BlendMode mode); + + void draw_point(const int x, const int y); + void draw_point(const float x, const float y); + + void draw_line(const int x1, const int x2, const int y1, const int y2); + void draw_line(const float x1, const float x2, const float y1, const float y2); + + void draw_rect(const Rect2 &rect); + + void draw_fill_rect(const Rect2 &rect); + + void draw_texture(const Texture &texture, const Rect2 &dst_rect); + void draw_texture(const Texture &texture, const Rect2 &src_rect, const Rect2 &dst_rect); + void draw_texture(const Texture &texture, const Rect2 &src_rect, const Rect2 &dst_rect, const double angle, const float cx = 0, const float cy = 0, const SDL_RendererFlip flip = SDL_FLIP_NONE); + + void draw_sprite(const Sprite &sprite); + void draw_sprite(const Sprite *sprite); + + int get_dpi() const; + int get_window_size_w() const; + int get_window_size_h() const; + int get_size_w() const; + int get_size_h() const; + void set_size(const int w, const int h) const; + void get_size(int *w, int *h) const; + + float get_scale_w() const; + float get_scale_h() const; + void set_scale(const float w, const float h) const; + void get_scale(float *w, float *h) const; + + bool get_integer_scaling() const; + void set_integer_scaling(const bool enable); + + Rect2 get_viewport() const; + void set_viewport(const Rect2 &rect) const; + + Rect2 get_clip_rect() const; + void set_clip_rect(Rect2 *rect) const; + bool clip_rect_enabled() const; + + bool render_target_supported(); + + SDL_Texture *get_render_target(); + void set_render_target(Texture *texture); + + void initialize(); + void destroy(); + + SDL_Window *get_window(); + SDL_Renderer *get_renderer(); + + Renderer(); + Renderer(unsigned int flags, unsigned int window_flags, int window_width = 640, int window_height = 480); + virtual ~Renderer(); + + static Renderer *get_singleton(); + +private: + int _initial_window_width; + int _initial_window_height; + + unsigned int _flags; + unsigned int _window_flags; + + SDL_Window *_window; + SDL_Renderer *_renderer; + + int _window_display_index; + + static Renderer *_singleton; +}; + +#endif \ No newline at end of file diff --git a/sfw/render_core/camera_2d.cpp b/sfw/render_objects/camera_2d.cpp similarity index 100% rename from sfw/render_core/camera_2d.cpp rename to sfw/render_objects/camera_2d.cpp diff --git a/sfw/render_core/camera_2d.h b/sfw/render_objects/camera_2d.h similarity index 100% rename from sfw/render_core/camera_2d.h rename to sfw/render_objects/camera_2d.h diff --git a/sfw/render_core/camera_3d.cpp b/sfw/render_objects/camera_3d.cpp similarity index 100% rename from sfw/render_core/camera_3d.cpp rename to sfw/render_objects/camera_3d.cpp diff --git a/sfw/render_core/camera_3d.h b/sfw/render_objects/camera_3d.h similarity index 100% rename from sfw/render_core/camera_3d.h rename to sfw/render_objects/camera_3d.h diff --git a/sfw/render_core/mesh_instance_2d.cpp b/sfw/render_objects/mesh_instance_2d.cpp similarity index 100% rename from sfw/render_core/mesh_instance_2d.cpp rename to sfw/render_objects/mesh_instance_2d.cpp diff --git a/sfw/render_core/mesh_instance_2d.h b/sfw/render_objects/mesh_instance_2d.h similarity index 100% rename from sfw/render_core/mesh_instance_2d.h rename to sfw/render_objects/mesh_instance_2d.h diff --git a/sfw/render_core/mesh_instance_3d.cpp b/sfw/render_objects/mesh_instance_3d.cpp similarity index 100% rename from sfw/render_core/mesh_instance_3d.cpp rename to sfw/render_objects/mesh_instance_3d.cpp diff --git a/sfw/render_core/mesh_instance_3d.h b/sfw/render_objects/mesh_instance_3d.h similarity index 100% rename from sfw/render_core/mesh_instance_3d.h rename to sfw/render_objects/mesh_instance_3d.h diff --git a/sfw/render_core/object_2d.cpp b/sfw/render_objects/object_2d.cpp similarity index 100% rename from sfw/render_core/object_2d.cpp rename to sfw/render_objects/object_2d.cpp diff --git a/sfw/render_core/object_2d.h b/sfw/render_objects/object_2d.h similarity index 100% rename from sfw/render_core/object_2d.h rename to sfw/render_objects/object_2d.h diff --git a/sfw/render_core/object_3d.cpp b/sfw/render_objects/object_3d.cpp similarity index 100% rename from sfw/render_core/object_3d.cpp rename to sfw/render_objects/object_3d.cpp diff --git a/sfw/render_core/object_3d.h b/sfw/render_objects/object_3d.h similarity index 100% rename from sfw/render_core/object_3d.h rename to sfw/render_objects/object_3d.h diff --git a/sfw/render_core/sprite.cpp b/sfw/render_objects/sprite.cpp similarity index 100% rename from sfw/render_core/sprite.cpp rename to sfw/render_objects/sprite.cpp diff --git a/sfw/render_core/sprite.h b/sfw/render_objects/sprite.h similarity index 100% rename from sfw/render_core/sprite.h rename to sfw/render_objects/sprite.h diff --git a/sfw/render_core/tile_map.cpp b/sfw/render_objects/tile_map.cpp similarity index 100% rename from sfw/render_core/tile_map.cpp rename to sfw/render_objects/tile_map.cpp diff --git a/sfw/render_core/tile_map.h b/sfw/render_objects/tile_map.h similarity index 100% rename from sfw/render_core/tile_map.h rename to sfw/render_objects/tile_map.h