From 05279c54fbbe04d0b9f7d5b99126988b28b4b33b Mon Sep 17 00:00:00 2001 From: Relintai Date: Mon, 5 Apr 2021 17:18:50 +0200 Subject: [PATCH] 17, 18, 19, 20 txts. Also moved out the sdl app filder from temp. --- .../.clang-format | 0 .../camera.cpp | 0 .../camera.h | 0 .../color.cpp | 0 .../color.h | 0 .../compile.sh | 0 .../image.cpp | 0 .../image.h | 0 .../main.cpp | 0 .../math.cpp | 0 .../math.h | 0 .../rect2.cpp | 0 .../rect2.h | 0 .../renderer.cpp | 0 .../renderer.h | 0 .../sprite.cpp | 0 .../sprite.h | 0 .../string.cpp | 0 .../string.h | 0 .../texture.cpp | 0 .../texture.h | 0 .../ti.bmp | Bin .../vector.h | 0 17_renderer.txt | 456 ++++++++++++++++++ 18_texture.txt | 206 ++++++++ 19_sprite.txt | 307 ++++++++++++ 20_camera.txt | 41 ++ .../.clang-format | 0 .../application.cpp | 0 .../application.h | 0 .../camera.cpp | 0 .../camera.h | 0 .../color.cpp | 0 .../color.h | 0 .../compile.sh | 0 .../image.cpp | 0 .../image.h | 0 .../impl_application.h | 0 .../main.cpp | 0 .../main_scene.cpp | 0 .../main_scene.h | 0 .../math.cpp | 0 .../math.h | 0 .../rect2.cpp | 0 .../rect2.h | 0 .../renderer.cpp | 0 .../renderer.h | 0 .../scene.cpp | 0 .../scene.h | 0 .../sprite.cpp | 0 .../sprite.h | 0 .../string.cpp | 0 .../string.h | 0 .../texture.cpp | 0 .../texture.h | 0 .../ti.bmp | Bin .../vector.h | 0 57 files changed, 1010 insertions(+) rename {14_sdl_software_renderer => 14_20_sdl_software_renderer}/.clang-format (100%) rename {14_sdl_software_renderer => 14_20_sdl_software_renderer}/camera.cpp (100%) rename {14_sdl_software_renderer => 14_20_sdl_software_renderer}/camera.h (100%) rename {14_sdl_software_renderer => 14_20_sdl_software_renderer}/color.cpp (100%) rename {14_sdl_software_renderer => 14_20_sdl_software_renderer}/color.h (100%) rename {14_sdl_software_renderer => 14_20_sdl_software_renderer}/compile.sh (100%) rename {14_sdl_software_renderer => 14_20_sdl_software_renderer}/image.cpp (100%) rename {14_sdl_software_renderer => 14_20_sdl_software_renderer}/image.h (100%) rename {14_sdl_software_renderer => 14_20_sdl_software_renderer}/main.cpp (100%) rename {14_sdl_software_renderer => 14_20_sdl_software_renderer}/math.cpp (100%) rename {14_sdl_software_renderer => 14_20_sdl_software_renderer}/math.h (100%) rename {14_sdl_software_renderer => 14_20_sdl_software_renderer}/rect2.cpp (100%) rename {14_sdl_software_renderer => 14_20_sdl_software_renderer}/rect2.h (100%) rename {14_sdl_software_renderer => 14_20_sdl_software_renderer}/renderer.cpp (100%) rename {14_sdl_software_renderer => 14_20_sdl_software_renderer}/renderer.h (100%) rename {14_sdl_software_renderer => 14_20_sdl_software_renderer}/sprite.cpp (100%) rename {14_sdl_software_renderer => 14_20_sdl_software_renderer}/sprite.h (100%) rename {14_sdl_software_renderer => 14_20_sdl_software_renderer}/string.cpp (100%) rename {14_sdl_software_renderer => 14_20_sdl_software_renderer}/string.h (100%) rename {14_sdl_software_renderer => 14_20_sdl_software_renderer}/texture.cpp (100%) rename {14_sdl_software_renderer => 14_20_sdl_software_renderer}/texture.h (100%) rename {14_sdl_software_renderer => 14_20_sdl_software_renderer}/ti.bmp (100%) rename {14_sdl_software_renderer => 14_20_sdl_software_renderer}/vector.h (100%) create mode 100644 17_renderer.txt create mode 100644 18_texture.txt create mode 100644 19_sprite.txt create mode 100644 20_camera.txt rename {temp/06_sdl_application => 21_sdl_application}/.clang-format (100%) rename {temp/06_sdl_application => 21_sdl_application}/application.cpp (100%) rename {temp/06_sdl_application => 21_sdl_application}/application.h (100%) rename {temp/06_sdl_application => 21_sdl_application}/camera.cpp (100%) rename {temp/06_sdl_application => 21_sdl_application}/camera.h (100%) rename {temp/06_sdl_application => 21_sdl_application}/color.cpp (100%) rename {temp/06_sdl_application => 21_sdl_application}/color.h (100%) rename {temp/06_sdl_application => 21_sdl_application}/compile.sh (100%) rename {temp/06_sdl_application => 21_sdl_application}/image.cpp (100%) rename {temp/06_sdl_application => 21_sdl_application}/image.h (100%) rename {temp/06_sdl_application => 21_sdl_application}/impl_application.h (100%) rename {temp/06_sdl_application => 21_sdl_application}/main.cpp (100%) rename {temp/06_sdl_application => 21_sdl_application}/main_scene.cpp (100%) rename {temp/06_sdl_application => 21_sdl_application}/main_scene.h (100%) rename {temp/06_sdl_application => 21_sdl_application}/math.cpp (100%) rename {temp/06_sdl_application => 21_sdl_application}/math.h (100%) rename {temp/06_sdl_application => 21_sdl_application}/rect2.cpp (100%) rename {temp/06_sdl_application => 21_sdl_application}/rect2.h (100%) rename {temp/06_sdl_application => 21_sdl_application}/renderer.cpp (100%) rename {temp/06_sdl_application => 21_sdl_application}/renderer.h (100%) rename {temp/06_sdl_application => 21_sdl_application}/scene.cpp (100%) rename {temp/06_sdl_application => 21_sdl_application}/scene.h (100%) rename {temp/06_sdl_application => 21_sdl_application}/sprite.cpp (100%) rename {temp/06_sdl_application => 21_sdl_application}/sprite.h (100%) rename {temp/06_sdl_application => 21_sdl_application}/string.cpp (100%) rename {temp/06_sdl_application => 21_sdl_application}/string.h (100%) rename {temp/06_sdl_application => 21_sdl_application}/texture.cpp (100%) rename {temp/06_sdl_application => 21_sdl_application}/texture.h (100%) rename {temp/06_sdl_application => 21_sdl_application}/ti.bmp (100%) rename {temp/06_sdl_application => 21_sdl_application}/vector.h (100%) diff --git a/14_sdl_software_renderer/.clang-format b/14_20_sdl_software_renderer/.clang-format similarity index 100% rename from 14_sdl_software_renderer/.clang-format rename to 14_20_sdl_software_renderer/.clang-format diff --git a/14_sdl_software_renderer/camera.cpp b/14_20_sdl_software_renderer/camera.cpp similarity index 100% rename from 14_sdl_software_renderer/camera.cpp rename to 14_20_sdl_software_renderer/camera.cpp diff --git a/14_sdl_software_renderer/camera.h b/14_20_sdl_software_renderer/camera.h similarity index 100% rename from 14_sdl_software_renderer/camera.h rename to 14_20_sdl_software_renderer/camera.h diff --git a/14_sdl_software_renderer/color.cpp b/14_20_sdl_software_renderer/color.cpp similarity index 100% rename from 14_sdl_software_renderer/color.cpp rename to 14_20_sdl_software_renderer/color.cpp diff --git a/14_sdl_software_renderer/color.h b/14_20_sdl_software_renderer/color.h similarity index 100% rename from 14_sdl_software_renderer/color.h rename to 14_20_sdl_software_renderer/color.h diff --git a/14_sdl_software_renderer/compile.sh b/14_20_sdl_software_renderer/compile.sh similarity index 100% rename from 14_sdl_software_renderer/compile.sh rename to 14_20_sdl_software_renderer/compile.sh diff --git a/14_sdl_software_renderer/image.cpp b/14_20_sdl_software_renderer/image.cpp similarity index 100% rename from 14_sdl_software_renderer/image.cpp rename to 14_20_sdl_software_renderer/image.cpp diff --git a/14_sdl_software_renderer/image.h b/14_20_sdl_software_renderer/image.h similarity index 100% rename from 14_sdl_software_renderer/image.h rename to 14_20_sdl_software_renderer/image.h diff --git a/14_sdl_software_renderer/main.cpp b/14_20_sdl_software_renderer/main.cpp similarity index 100% rename from 14_sdl_software_renderer/main.cpp rename to 14_20_sdl_software_renderer/main.cpp diff --git a/14_sdl_software_renderer/math.cpp b/14_20_sdl_software_renderer/math.cpp similarity index 100% rename from 14_sdl_software_renderer/math.cpp rename to 14_20_sdl_software_renderer/math.cpp diff --git a/14_sdl_software_renderer/math.h b/14_20_sdl_software_renderer/math.h similarity index 100% rename from 14_sdl_software_renderer/math.h rename to 14_20_sdl_software_renderer/math.h diff --git a/14_sdl_software_renderer/rect2.cpp b/14_20_sdl_software_renderer/rect2.cpp similarity index 100% rename from 14_sdl_software_renderer/rect2.cpp rename to 14_20_sdl_software_renderer/rect2.cpp diff --git a/14_sdl_software_renderer/rect2.h b/14_20_sdl_software_renderer/rect2.h similarity index 100% rename from 14_sdl_software_renderer/rect2.h rename to 14_20_sdl_software_renderer/rect2.h diff --git a/14_sdl_software_renderer/renderer.cpp b/14_20_sdl_software_renderer/renderer.cpp similarity index 100% rename from 14_sdl_software_renderer/renderer.cpp rename to 14_20_sdl_software_renderer/renderer.cpp diff --git a/14_sdl_software_renderer/renderer.h b/14_20_sdl_software_renderer/renderer.h similarity index 100% rename from 14_sdl_software_renderer/renderer.h rename to 14_20_sdl_software_renderer/renderer.h diff --git a/14_sdl_software_renderer/sprite.cpp b/14_20_sdl_software_renderer/sprite.cpp similarity index 100% rename from 14_sdl_software_renderer/sprite.cpp rename to 14_20_sdl_software_renderer/sprite.cpp diff --git a/14_sdl_software_renderer/sprite.h b/14_20_sdl_software_renderer/sprite.h similarity index 100% rename from 14_sdl_software_renderer/sprite.h rename to 14_20_sdl_software_renderer/sprite.h diff --git a/14_sdl_software_renderer/string.cpp b/14_20_sdl_software_renderer/string.cpp similarity index 100% rename from 14_sdl_software_renderer/string.cpp rename to 14_20_sdl_software_renderer/string.cpp diff --git a/14_sdl_software_renderer/string.h b/14_20_sdl_software_renderer/string.h similarity index 100% rename from 14_sdl_software_renderer/string.h rename to 14_20_sdl_software_renderer/string.h diff --git a/14_sdl_software_renderer/texture.cpp b/14_20_sdl_software_renderer/texture.cpp similarity index 100% rename from 14_sdl_software_renderer/texture.cpp rename to 14_20_sdl_software_renderer/texture.cpp diff --git a/14_sdl_software_renderer/texture.h b/14_20_sdl_software_renderer/texture.h similarity index 100% rename from 14_sdl_software_renderer/texture.h rename to 14_20_sdl_software_renderer/texture.h diff --git a/14_sdl_software_renderer/ti.bmp b/14_20_sdl_software_renderer/ti.bmp similarity index 100% rename from 14_sdl_software_renderer/ti.bmp rename to 14_20_sdl_software_renderer/ti.bmp diff --git a/14_sdl_software_renderer/vector.h b/14_20_sdl_software_renderer/vector.h similarity index 100% rename from 14_sdl_software_renderer/vector.h rename to 14_20_sdl_software_renderer/vector.h diff --git a/17_renderer.txt b/17_renderer.txt new file mode 100644 index 0000000..ac558b1 --- /dev/null +++ b/17_renderer.txt @@ -0,0 +1,456 @@ + + + +class Renderer + + 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; + + + + +#include "renderer.h" + +#include + +#include "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; diff --git a/18_texture.txt b/18_texture.txt new file mode 100644 index 0000000..2b03aad --- /dev/null +++ b/18_texture.txt @@ -0,0 +1,206 @@ + + +class Texture +public: + Color get_color_mod() const; + void set_color_mod(const Color &color); + + SDL_BlendMode get_blend_mode() const; + void set_blend_mode(const SDL_BlendMode blend_mode); + + SDL_ScaleMode get_texture_scale_mode() const; + void set_texture_scale_mode(const SDL_ScaleMode scale_mode); + + Image *get_image(); + void set_image(Image *image); + + int get_width() const; + int get_height() const; + Uint32 get_format() const; + int get_access() const; + + void create(const int access, const int w, const int h); + void refresh(); + void free(); + + SDL_Texture *get_texture(); + SDL_Texture *get_texture() const; + + bool is_render_target(); + + Texture(); + Texture(Image *image); + virtual ~Texture(); + +private: + Image *_image; + SDL_Texture *_texture; + + + +#include "texture.h" + +#include "renderer.h" + +Color Texture::get_color_mod() const { + Uint8 r; + Uint8 g; + Uint8 b; + Uint8 a; + + SDL_GetTextureColorMod(_texture, &r, &g, &b); + SDL_GetTextureAlphaMod(_texture, &a); + + return Color(r, g, b, a); +} +void Texture::set_color_mod(const Color &color) { + SDL_SetTextureColorMod(_texture, color.r, color.g, color.b); + SDL_SetTextureAlphaMod(_texture, color.a); +} + +SDL_BlendMode Texture::get_blend_mode() const { + SDL_BlendMode blendMode; + + SDL_GetTextureBlendMode(_texture, &blendMode); + + return blendMode; +} +void Texture::set_blend_mode(const SDL_BlendMode blend_mode) { + SDL_SetTextureBlendMode(_texture, blend_mode); +} + +SDL_ScaleMode Texture::get_texture_scale_mode() const { + SDL_ScaleMode scale_mode; + + SDL_GetTextureScaleMode(_texture, &scale_mode); + + return scale_mode; +} +void Texture::set_texture_scale_mode(const SDL_ScaleMode scale_mode) { + SDL_SetTextureScaleMode(_texture, scale_mode); +} + +Image *Texture::get_image() { + return _image; +} +void Texture::set_image(Image *image) { + if (_texture) { + free(); + } + + _image = image; + + refresh(); +} + +int Texture::get_width() const { + Uint32 format; + int access; + int w; + int h; + + if (SDL_QueryTexture(_texture, &format, &access, &w, &h)) { + return 0; + } + + return w; +} +int Texture::get_height() const { + Uint32 format; + int access; + int w; + int h; + + if (SDL_QueryTexture(_texture, &format, &access, &w, &h)) { + return 0; + } + + return h; +} +Uint32 Texture::get_format() const { + Uint32 format; + int access; + int w; + int h; + + if (SDL_QueryTexture(_texture, &format, &access, &w, &h)) { + return 0; + } + + return format; +} +int Texture::get_access() const { + Uint32 format; + int access; + int w; + int h; + + if (SDL_QueryTexture(_texture, &format, &access, &w, &h)) { + return 0; + } + + return access; +} + +void Texture::create(int access, int w, int h) { + if (_texture) { + free(); + } + + _image = nullptr; + + _texture = SDL_CreateTexture(Renderer::get_singleton()->get_renderer(), SDL_PIXELFORMAT_RGBA8888, access, w, h); +} +void Texture::refresh() { + if (_image == nullptr) { + return; + } + + if (_image->get_surface() == nullptr) { + return; + } + + if (_texture) { + free(); + } + + _texture = SDL_CreateTextureFromSurface(Renderer::get_singleton()->get_renderer(), _image->get_surface()); +} +void Texture::free() { + if (_texture) { + SDL_DestroyTexture(_texture); + + _texture = nullptr; + } +} + +SDL_Texture *Texture::get_texture() { + return _texture; +} +SDL_Texture *Texture::get_texture() const { + return _texture; +} + +bool Texture::is_render_target() { + if (_texture == Renderer::get_singleton()->get_render_target()) { + return true; + } + + return false; +} + +Texture::Texture() { + _image = nullptr; + _texture = nullptr; +} +Texture::Texture(Image *image) { + _image = nullptr; + _texture = nullptr; + + set_image(image); +} +Texture::~Texture() { + if (_texture) { + free(); + } +} diff --git a/19_sprite.txt b/19_sprite.txt new file mode 100644 index 0000000..230e404 --- /dev/null +++ b/19_sprite.txt @@ -0,0 +1,307 @@ + + +class Sprite +public: + Rect2 get_texture_clip_rect() const; + void set_texture_clip_rect(const Rect2 &rect); + + Rect2 get_transform() const; + void set_transform(const Rect2 &rect); + + float get_x() const; + void set_x(const float val); + + float get_y() const; + void set_y(const float val); + + float get_w() const; + void set_w(const float val); + + float get_h() const; + void set_h(const float val); + + double get_angle() const; + void set_angle(const double val); + + float get_anchor_x() const; + void set_anchor_x(const float val); + + float get_anchor_y() const; + void set_anchor_y(const float val); + + void set_anchor(const float x, const float y); + + SDL_RendererFlip get_flip() const; + void set_flip(const SDL_RendererFlip val); + + Color get_color_mod() const; + void set_color_mod(const Color &color); + + Texture *get_texture(); + Texture *get_texture() const; + void set_texture(Texture *texture); + + void draw(); + + Sprite(); + Sprite(Texture *texture); + Sprite(Texture *texture, const Color &color_mod); + Sprite(Texture *texture, const float x, const float y, const double angle = 0); + Sprite(Texture *texture, const float x, const float y, const Rect2 &texture_clip_rect, const double angle = 0); + Sprite(Texture *texture, const Rect2 &transform, const Rect2 &texture_clip_rect, const double angle = 0); + Sprite(Texture *texture, const float x, const float y, const float w, const float h, const double angle = 0); + virtual ~Sprite(); + +private: + Rect2 _texture_clip_rect; + Rect2 _transform; + double _angle; + + float _anchor_x; + float _anchor_y; + + SDL_RendererFlip _flip; + + Color _color_mod; + + Texture *_texture; + + +#include "sprite.h" + +#include "renderer.h" + +Rect2 Sprite::get_texture_clip_rect() const { + return _texture_clip_rect; +} +void Sprite::set_texture_clip_rect(const Rect2 &rect) { + _texture_clip_rect = rect; +} + +Rect2 Sprite::get_transform() const { + return _transform; +} +void Sprite::set_transform(const Rect2 &rect) { + _transform = rect; +} + +float Sprite::get_x() const { + return _transform.x; +} +void Sprite::set_x(const float val) { + _transform.x = val; +} + +float Sprite::get_y() const { + return _transform.y; +} +void Sprite::set_y(const float val) { + _transform.y = val; +} + +float Sprite::get_w() const { + return _transform.w; +} +void Sprite::set_w(const float val) { + _transform.w = val; +} + +float Sprite::get_h() const { + return _transform.h; +} +void Sprite::set_h(const float val) { + _transform.h = val; +} + +double Sprite::get_angle() const { + return _angle; +} +void Sprite::set_angle(const double val) { + _angle = val; +} + +float Sprite::get_anchor_x() const { + return _anchor_x; +} +void Sprite::set_anchor_x(const float val) { + _anchor_x = val; +} + +float Sprite::get_anchor_y() const { + return _anchor_y; +} +void Sprite::set_anchor_y(const float val) { + _anchor_y = val; +} + +void Sprite::set_anchor(const float x, const float y) { + _anchor_x = x; + _anchor_y = y; +} + +SDL_RendererFlip Sprite::get_flip() const { + return _flip; +} +void Sprite::set_flip(const SDL_RendererFlip val) { + _flip = val; +} + +Color Sprite::get_color_mod() const { + return _color_mod; +} +void Sprite::set_color_mod(const Color &color) { + _color_mod = color; +} + +Texture *Sprite::get_texture() { + return _texture; +} +Texture *Sprite::get_texture() const { + return _texture; +} +void Sprite::set_texture(Texture *texture) { + _texture = texture; +} + +void Sprite::draw() { + Renderer::get_singleton()->draw_sprite(this); +} + +Sprite::Sprite() { + _angle = 0; + + _anchor_x = 0; + _anchor_y = 0; + + _flip = SDL_FLIP_NONE; + + _texture = nullptr; + + _color_mod = Color(255, 255, 255, 255); +} +Sprite::Sprite(Texture *texture) { + _angle = 0; + + _anchor_x = 0; + _anchor_y = 0; + + _flip = SDL_FLIP_NONE; + + _texture = texture; + + if (_texture != nullptr) { + _texture_clip_rect.w = texture->get_width(); + _texture_clip_rect.h = texture->get_height(); + + _transform.w = texture->get_width(); + _transform.h = texture->get_height(); + } + + _color_mod = Color(255, 255, 255, 255); +} + +Sprite::Sprite(Texture *texture, const Color &color_mod) { + _angle = 0; + + _anchor_x = 0; + _anchor_y = 0; + + _flip = SDL_FLIP_NONE; + + _texture = texture; + + if (_texture != nullptr) { + _texture_clip_rect.w = texture->get_width(); + _texture_clip_rect.h = texture->get_height(); + + _transform.w = texture->get_width(); + _transform.h = texture->get_height(); + } + + _color_mod = color_mod; +} + +Sprite::Sprite(Texture *texture, const float x, const float y, const double angle) { + _angle = angle; + + _anchor_x = 0; + _anchor_y = 0; + + _flip = SDL_FLIP_NONE; + + _texture = texture; + + _transform.x = x; + _transform.y = y; + + if (_texture != nullptr) { + _texture_clip_rect.w = texture->get_width(); + _texture_clip_rect.h = texture->get_height(); + + _transform.w = texture->get_width(); + _transform.h = texture->get_height(); + } + + _color_mod = Color(255, 255, 255, 255); +} +Sprite::Sprite(Texture *texture, const float x, const float y, const Rect2 &texture_clip_rect, const double angle) { + _angle = angle; + + _anchor_x = 0; + _anchor_y = 0; + + _flip = SDL_FLIP_NONE; + + _texture = texture; + + _transform.x = x; + _transform.y = y; + + if (_texture != nullptr) { + _transform.w = texture->get_width(); + _transform.h = texture->get_height(); + } + + _texture_clip_rect = texture_clip_rect; + + _color_mod = Color(255, 255, 255, 255); +} +Sprite::Sprite(Texture *texture, const Rect2 &transform, const Rect2 &texture_clip_rect, const double angle) { + _angle = angle; + + _anchor_x = 0; + _anchor_y = 0; + + _flip = SDL_FLIP_NONE; + + _texture = texture; + + _transform = transform; + _texture_clip_rect = texture_clip_rect; + + _color_mod = Color(255, 255, 255, 255); +} +Sprite::Sprite(Texture *texture, const float x, const float y, const float w, const float h, const double angle) { + _angle = angle; + + _anchor_x = 0; + _anchor_y = 0; + + _flip = SDL_FLIP_NONE; + + _texture = texture; + + _transform.x = x; + _transform.y = y; + _transform.w = w; + _transform.h = h; + + if (_texture != nullptr) { + _texture_clip_rect.w = texture->get_width(); + _texture_clip_rect.h = texture->get_height(); + } + + _color_mod = Color(255, 255, 255, 255); +} +Sprite::~Sprite() { +} diff --git a/20_camera.txt b/20_camera.txt new file mode 100644 index 0000000..9771dff --- /dev/null +++ b/20_camera.txt @@ -0,0 +1,41 @@ + + +class Camera { +public: + void bind(); + + Camera(); + virtual ~Camera(); + + bool integer_scaling; + float scale_w; + float scale_h; + Rect2 viewport; + Rect2 clip_rect; + + +#include "camera.h" + +#include "renderer.h" + +void Camera::bind() { + Renderer::get_singleton()->set_integer_scaling(integer_scaling); + + Renderer::get_singleton()->set_scale(scale_w, scale_h); + + Renderer::get_singleton()->set_viewport(viewport); + Renderer::get_singleton()->set_clip_rect(&clip_rect); +} + +Camera::Camera() { + integer_scaling = false; + + scale_w = 1; + scale_h = 1; + + viewport = Renderer::get_singleton()->get_viewport(); + clip_rect = Renderer::get_singleton()->get_clip_rect(); +} +Camera::~Camera() { +} + diff --git a/temp/06_sdl_application/.clang-format b/21_sdl_application/.clang-format similarity index 100% rename from temp/06_sdl_application/.clang-format rename to 21_sdl_application/.clang-format diff --git a/temp/06_sdl_application/application.cpp b/21_sdl_application/application.cpp similarity index 100% rename from temp/06_sdl_application/application.cpp rename to 21_sdl_application/application.cpp diff --git a/temp/06_sdl_application/application.h b/21_sdl_application/application.h similarity index 100% rename from temp/06_sdl_application/application.h rename to 21_sdl_application/application.h diff --git a/temp/06_sdl_application/camera.cpp b/21_sdl_application/camera.cpp similarity index 100% rename from temp/06_sdl_application/camera.cpp rename to 21_sdl_application/camera.cpp diff --git a/temp/06_sdl_application/camera.h b/21_sdl_application/camera.h similarity index 100% rename from temp/06_sdl_application/camera.h rename to 21_sdl_application/camera.h diff --git a/temp/06_sdl_application/color.cpp b/21_sdl_application/color.cpp similarity index 100% rename from temp/06_sdl_application/color.cpp rename to 21_sdl_application/color.cpp diff --git a/temp/06_sdl_application/color.h b/21_sdl_application/color.h similarity index 100% rename from temp/06_sdl_application/color.h rename to 21_sdl_application/color.h diff --git a/temp/06_sdl_application/compile.sh b/21_sdl_application/compile.sh similarity index 100% rename from temp/06_sdl_application/compile.sh rename to 21_sdl_application/compile.sh diff --git a/temp/06_sdl_application/image.cpp b/21_sdl_application/image.cpp similarity index 100% rename from temp/06_sdl_application/image.cpp rename to 21_sdl_application/image.cpp diff --git a/temp/06_sdl_application/image.h b/21_sdl_application/image.h similarity index 100% rename from temp/06_sdl_application/image.h rename to 21_sdl_application/image.h diff --git a/temp/06_sdl_application/impl_application.h b/21_sdl_application/impl_application.h similarity index 100% rename from temp/06_sdl_application/impl_application.h rename to 21_sdl_application/impl_application.h diff --git a/temp/06_sdl_application/main.cpp b/21_sdl_application/main.cpp similarity index 100% rename from temp/06_sdl_application/main.cpp rename to 21_sdl_application/main.cpp diff --git a/temp/06_sdl_application/main_scene.cpp b/21_sdl_application/main_scene.cpp similarity index 100% rename from temp/06_sdl_application/main_scene.cpp rename to 21_sdl_application/main_scene.cpp diff --git a/temp/06_sdl_application/main_scene.h b/21_sdl_application/main_scene.h similarity index 100% rename from temp/06_sdl_application/main_scene.h rename to 21_sdl_application/main_scene.h diff --git a/temp/06_sdl_application/math.cpp b/21_sdl_application/math.cpp similarity index 100% rename from temp/06_sdl_application/math.cpp rename to 21_sdl_application/math.cpp diff --git a/temp/06_sdl_application/math.h b/21_sdl_application/math.h similarity index 100% rename from temp/06_sdl_application/math.h rename to 21_sdl_application/math.h diff --git a/temp/06_sdl_application/rect2.cpp b/21_sdl_application/rect2.cpp similarity index 100% rename from temp/06_sdl_application/rect2.cpp rename to 21_sdl_application/rect2.cpp diff --git a/temp/06_sdl_application/rect2.h b/21_sdl_application/rect2.h similarity index 100% rename from temp/06_sdl_application/rect2.h rename to 21_sdl_application/rect2.h diff --git a/temp/06_sdl_application/renderer.cpp b/21_sdl_application/renderer.cpp similarity index 100% rename from temp/06_sdl_application/renderer.cpp rename to 21_sdl_application/renderer.cpp diff --git a/temp/06_sdl_application/renderer.h b/21_sdl_application/renderer.h similarity index 100% rename from temp/06_sdl_application/renderer.h rename to 21_sdl_application/renderer.h diff --git a/temp/06_sdl_application/scene.cpp b/21_sdl_application/scene.cpp similarity index 100% rename from temp/06_sdl_application/scene.cpp rename to 21_sdl_application/scene.cpp diff --git a/temp/06_sdl_application/scene.h b/21_sdl_application/scene.h similarity index 100% rename from temp/06_sdl_application/scene.h rename to 21_sdl_application/scene.h diff --git a/temp/06_sdl_application/sprite.cpp b/21_sdl_application/sprite.cpp similarity index 100% rename from temp/06_sdl_application/sprite.cpp rename to 21_sdl_application/sprite.cpp diff --git a/temp/06_sdl_application/sprite.h b/21_sdl_application/sprite.h similarity index 100% rename from temp/06_sdl_application/sprite.h rename to 21_sdl_application/sprite.h diff --git a/temp/06_sdl_application/string.cpp b/21_sdl_application/string.cpp similarity index 100% rename from temp/06_sdl_application/string.cpp rename to 21_sdl_application/string.cpp diff --git a/temp/06_sdl_application/string.h b/21_sdl_application/string.h similarity index 100% rename from temp/06_sdl_application/string.h rename to 21_sdl_application/string.h diff --git a/temp/06_sdl_application/texture.cpp b/21_sdl_application/texture.cpp similarity index 100% rename from temp/06_sdl_application/texture.cpp rename to 21_sdl_application/texture.cpp diff --git a/temp/06_sdl_application/texture.h b/21_sdl_application/texture.h similarity index 100% rename from temp/06_sdl_application/texture.h rename to 21_sdl_application/texture.h diff --git a/temp/06_sdl_application/ti.bmp b/21_sdl_application/ti.bmp similarity index 100% rename from temp/06_sdl_application/ti.bmp rename to 21_sdl_application/ti.bmp diff --git a/temp/06_sdl_application/vector.h b/21_sdl_application/vector.h similarity index 100% rename from temp/06_sdl_application/vector.h rename to 21_sdl_application/vector.h