diff --git a/05_sdl_software_renderer/renderer.cpp b/05_sdl_software_renderer/renderer.cpp index 98b73cc..04f74d2 100644 --- a/05_sdl_software_renderer/renderer.cpp +++ b/05_sdl_software_renderer/renderer.cpp @@ -93,6 +93,8 @@ void Renderer::draw_sprite(const Sprite &sprite) { return; } + t->set_color_mod(sprite.get_color_mod()); + double angle = sprite.get_angle(); if (Math::is_zero_approx(angle)) { @@ -114,12 +116,17 @@ void Renderer::draw_sprite(const Sprite &sprite) { } 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)) { diff --git a/05_sdl_software_renderer/sprite.cpp b/05_sdl_software_renderer/sprite.cpp index dcfe623..38d57f5 100644 --- a/05_sdl_software_renderer/sprite.cpp +++ b/05_sdl_software_renderer/sprite.cpp @@ -1,5 +1,7 @@ #include "sprite.h" +#include "renderer.h" + Rect2 Sprite::get_texture_clip_rect() const { return _texture_clip_rect; } @@ -75,6 +77,13 @@ 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; } @@ -85,6 +94,10 @@ void Sprite::set_texture(Texture *texture) { _texture = texture; } +void Sprite::draw() { + Renderer::get_singleton()->draw_sprite(this); +} + Sprite::Sprite() { _angle = 0; @@ -94,6 +107,8 @@ Sprite::Sprite() { _flip = SDL_FLIP_NONE; _texture = nullptr; + + _color_mod = Color(255, 255, 255, 255); } Sprite::Sprite(Texture *texture) { _angle = 0; @@ -112,7 +127,31 @@ Sprite::Sprite(Texture *texture) { _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; @@ -133,6 +172,8 @@ Sprite::Sprite(Texture *texture, const float x, const float y, const double angl _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; @@ -153,6 +194,8 @@ Sprite::Sprite(Texture *texture, const float x, const float y, const Rect2 &text } _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; @@ -166,6 +209,8 @@ Sprite::Sprite(Texture *texture, const Rect2 &transform, const Rect2 &texture_cl _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; @@ -186,6 +231,8 @@ Sprite::Sprite(Texture *texture, const float x, const float y, const float w, co _texture_clip_rect.w = texture->get_width(); _texture_clip_rect.h = texture->get_height(); } + + _color_mod = Color(255, 255, 255, 255); } Sprite::~Sprite() { } \ No newline at end of file diff --git a/05_sdl_software_renderer/sprite.h b/05_sdl_software_renderer/sprite.h index e212727..50edd9e 100644 --- a/05_sdl_software_renderer/sprite.h +++ b/05_sdl_software_renderer/sprite.h @@ -5,6 +5,7 @@ #include "rect2.h" #include "texture.h" +#include "color.h" class Sprite { public: @@ -35,22 +36,28 @@ public: float get_anchor_y() const; void set_anchor_y(const float val); - void set_anchor(const float x, const float y); + 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); - Sprite(); - Sprite(Texture *texture); - 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(); + 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; @@ -62,6 +69,8 @@ private: SDL_RendererFlip _flip; + Color _color_mod; + Texture *_texture; }; diff --git a/06_sdl_application/renderer.cpp b/06_sdl_application/renderer.cpp index 98b73cc..04f74d2 100644 --- a/06_sdl_application/renderer.cpp +++ b/06_sdl_application/renderer.cpp @@ -93,6 +93,8 @@ void Renderer::draw_sprite(const Sprite &sprite) { return; } + t->set_color_mod(sprite.get_color_mod()); + double angle = sprite.get_angle(); if (Math::is_zero_approx(angle)) { @@ -114,12 +116,17 @@ void Renderer::draw_sprite(const Sprite &sprite) { } 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)) { diff --git a/06_sdl_application/sprite.cpp b/06_sdl_application/sprite.cpp index dcfe623..38d57f5 100644 --- a/06_sdl_application/sprite.cpp +++ b/06_sdl_application/sprite.cpp @@ -1,5 +1,7 @@ #include "sprite.h" +#include "renderer.h" + Rect2 Sprite::get_texture_clip_rect() const { return _texture_clip_rect; } @@ -75,6 +77,13 @@ 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; } @@ -85,6 +94,10 @@ void Sprite::set_texture(Texture *texture) { _texture = texture; } +void Sprite::draw() { + Renderer::get_singleton()->draw_sprite(this); +} + Sprite::Sprite() { _angle = 0; @@ -94,6 +107,8 @@ Sprite::Sprite() { _flip = SDL_FLIP_NONE; _texture = nullptr; + + _color_mod = Color(255, 255, 255, 255); } Sprite::Sprite(Texture *texture) { _angle = 0; @@ -112,7 +127,31 @@ Sprite::Sprite(Texture *texture) { _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; @@ -133,6 +172,8 @@ Sprite::Sprite(Texture *texture, const float x, const float y, const double angl _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; @@ -153,6 +194,8 @@ Sprite::Sprite(Texture *texture, const float x, const float y, const Rect2 &text } _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; @@ -166,6 +209,8 @@ Sprite::Sprite(Texture *texture, const Rect2 &transform, const Rect2 &texture_cl _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; @@ -186,6 +231,8 @@ Sprite::Sprite(Texture *texture, const float x, const float y, const float w, co _texture_clip_rect.w = texture->get_width(); _texture_clip_rect.h = texture->get_height(); } + + _color_mod = Color(255, 255, 255, 255); } Sprite::~Sprite() { } \ No newline at end of file diff --git a/06_sdl_application/sprite.h b/06_sdl_application/sprite.h index e212727..50edd9e 100644 --- a/06_sdl_application/sprite.h +++ b/06_sdl_application/sprite.h @@ -5,6 +5,7 @@ #include "rect2.h" #include "texture.h" +#include "color.h" class Sprite { public: @@ -35,22 +36,28 @@ public: float get_anchor_y() const; void set_anchor_y(const float val); - void set_anchor(const float x, const float y); + 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); - Sprite(); - Sprite(Texture *texture); - 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(); + 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; @@ -62,6 +69,8 @@ private: SDL_RendererFlip _flip; + Color _color_mod; + Texture *_texture; }; diff --git a/07_sdl_moving_sprites/renderer.cpp b/07_sdl_moving_sprites/renderer.cpp index 98b73cc..04f74d2 100644 --- a/07_sdl_moving_sprites/renderer.cpp +++ b/07_sdl_moving_sprites/renderer.cpp @@ -93,6 +93,8 @@ void Renderer::draw_sprite(const Sprite &sprite) { return; } + t->set_color_mod(sprite.get_color_mod()); + double angle = sprite.get_angle(); if (Math::is_zero_approx(angle)) { @@ -114,12 +116,17 @@ void Renderer::draw_sprite(const Sprite &sprite) { } 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)) { diff --git a/07_sdl_moving_sprites/sprite.cpp b/07_sdl_moving_sprites/sprite.cpp index dcfe623..38d57f5 100644 --- a/07_sdl_moving_sprites/sprite.cpp +++ b/07_sdl_moving_sprites/sprite.cpp @@ -1,5 +1,7 @@ #include "sprite.h" +#include "renderer.h" + Rect2 Sprite::get_texture_clip_rect() const { return _texture_clip_rect; } @@ -75,6 +77,13 @@ 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; } @@ -85,6 +94,10 @@ void Sprite::set_texture(Texture *texture) { _texture = texture; } +void Sprite::draw() { + Renderer::get_singleton()->draw_sprite(this); +} + Sprite::Sprite() { _angle = 0; @@ -94,6 +107,8 @@ Sprite::Sprite() { _flip = SDL_FLIP_NONE; _texture = nullptr; + + _color_mod = Color(255, 255, 255, 255); } Sprite::Sprite(Texture *texture) { _angle = 0; @@ -112,7 +127,31 @@ Sprite::Sprite(Texture *texture) { _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; @@ -133,6 +172,8 @@ Sprite::Sprite(Texture *texture, const float x, const float y, const double angl _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; @@ -153,6 +194,8 @@ Sprite::Sprite(Texture *texture, const float x, const float y, const Rect2 &text } _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; @@ -166,6 +209,8 @@ Sprite::Sprite(Texture *texture, const Rect2 &transform, const Rect2 &texture_cl _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; @@ -186,6 +231,8 @@ Sprite::Sprite(Texture *texture, const float x, const float y, const float w, co _texture_clip_rect.w = texture->get_width(); _texture_clip_rect.h = texture->get_height(); } + + _color_mod = Color(255, 255, 255, 255); } Sprite::~Sprite() { } \ No newline at end of file diff --git a/07_sdl_moving_sprites/sprite.h b/07_sdl_moving_sprites/sprite.h index e212727..50edd9e 100644 --- a/07_sdl_moving_sprites/sprite.h +++ b/07_sdl_moving_sprites/sprite.h @@ -5,6 +5,7 @@ #include "rect2.h" #include "texture.h" +#include "color.h" class Sprite { public: @@ -35,22 +36,28 @@ public: float get_anchor_y() const; void set_anchor_y(const float val); - void set_anchor(const float x, const float y); + 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); - Sprite(); - Sprite(Texture *texture); - 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(); + 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; @@ -62,6 +69,8 @@ private: SDL_RendererFlip _flip; + Color _color_mod; + Texture *_texture; };