From 9d4a5ca431e33111a7bc569a5267750ad6e2e6da Mon Sep 17 00:00:00 2001 From: Relintai Date: Fri, 2 Apr 2021 16:29:05 +0200 Subject: [PATCH] Implemented sprites. --- 05_sdl_alapok/compile.sh | 4 +- 05_sdl_alapok/main.cpp | 9 +- 05_sdl_alapok/renderer.cpp | 31 +++++- 05_sdl_alapok/renderer.h | 3 + 05_sdl_alapok/sprite.cpp | 191 +++++++++++++++++++++++++++++++++++++ 05_sdl_alapok/sprite.h | 68 +++++++++++++ 6 files changed, 303 insertions(+), 3 deletions(-) create mode 100644 05_sdl_alapok/sprite.cpp create mode 100644 05_sdl_alapok/sprite.h diff --git a/05_sdl_alapok/compile.sh b/05_sdl_alapok/compile.sh index 5448537..9193b19 100755 --- a/05_sdl_alapok/compile.sh +++ b/05_sdl_alapok/compile.sh @@ -21,9 +21,11 @@ g++ -Wall -g -c string.cpp -o obj/string.o g++ -Wall -g $(sdl2-config --cflags) -c renderer.cpp -o obj/renderer.o g++ -Wall -g $(sdl2-config --cflags) -c image.cpp -o obj/image.o g++ -Wall -g $(sdl2-config --cflags) -c texture.cpp -o obj/texture.o +g++ -Wall -g $(sdl2-config --cflags) -c sprite.cpp -o obj/sprite.o + g++ -Wall -g $(sdl2-config --cflags) -c texture_editor.cpp -o obj/texture_editor.o g++ -Wall -g $(sdl2-config --cflags) -c main.cpp -o obj/main.o -g++ -o bin/program obj/math.o obj/rect2.o obj/color.o obj/string.o obj/renderer.o obj/image.o obj/texture.o obj/texture_editor.o obj/main.o $(sdl2-config --libs) +g++ -o bin/program obj/math.o obj/rect2.o obj/color.o obj/string.o obj/renderer.o obj/image.o obj/texture.o obj/sprite.o obj/texture_editor.o obj/main.o $(sdl2-config --libs) diff --git a/05_sdl_alapok/main.cpp b/05_sdl_alapok/main.cpp index d36c1c1..14be5d2 100644 --- a/05_sdl_alapok/main.cpp +++ b/05_sdl_alapok/main.cpp @@ -2,6 +2,7 @@ #include "image.h" #include "renderer.h" +#include "sprite.h" #include @@ -30,8 +31,14 @@ int main(int argv, char **args) { r.clear(); Texture t(&i); + Sprite s(&t); - r.draw_texture(t, Rect2(0, 0, t.get_width(), t.get_height())); + s.set_x(30); + s.set_y(30); + + //r.draw_texture(t, Rect2(0, 0, t.get_width(), t.get_height())); + + r.draw_sprite(s); r.present(); diff --git a/05_sdl_alapok/renderer.cpp b/05_sdl_alapok/renderer.cpp index f9f7a93..57b6974 100644 --- a/05_sdl_alapok/renderer.cpp +++ b/05_sdl_alapok/renderer.cpp @@ -2,6 +2,8 @@ #include +#include "math.h" + void Renderer::present() { SDL_RenderPresent(_renderer); } @@ -30,7 +32,7 @@ void Renderer::draw_rect(const Rect2 &rect) { void Renderer::draw_texture(const Texture &texture, const Rect2 &dst_rect) { SDL_Rect sr; - sr.x = 0; + sr.x = 0; sr.y = 0; sr.w = texture.get_width(); sr.h = texture.get_height(); @@ -57,6 +59,33 @@ void Renderer::draw_texture(const Texture &texture, const Rect2 &src_rect, const 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; + } + + 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; diff --git a/05_sdl_alapok/renderer.h b/05_sdl_alapok/renderer.h index d88b54b..ac13961 100644 --- a/05_sdl_alapok/renderer.h +++ b/05_sdl_alapok/renderer.h @@ -4,6 +4,7 @@ #include "color.h" #include "rect2.h" #include "texture.h" +#include "sprite.h" #include @@ -21,6 +22,8 @@ public: 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); + int get_dpi() const; int get_size_w() const; int get_size_h() const; diff --git a/05_sdl_alapok/sprite.cpp b/05_sdl_alapok/sprite.cpp new file mode 100644 index 0000000..dcfe623 --- /dev/null +++ b/05_sdl_alapok/sprite.cpp @@ -0,0 +1,191 @@ +#include "sprite.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; +} + +Texture *Sprite::get_texture() { + return _texture; +} +Texture *Sprite::get_texture() const { + return _texture; +} +void Sprite::set_texture(Texture *texture) { + _texture = texture; +} + +Sprite::Sprite() { + _angle = 0; + + _anchor_x = 0; + _anchor_y = 0; + + _flip = SDL_FLIP_NONE; + + _texture = nullptr; +} +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(); + } +} +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(); + } +} +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; +} +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; +} +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(); + } +} +Sprite::~Sprite() { +} \ No newline at end of file diff --git a/05_sdl_alapok/sprite.h b/05_sdl_alapok/sprite.h new file mode 100644 index 0000000..e212727 --- /dev/null +++ b/05_sdl_alapok/sprite.h @@ -0,0 +1,68 @@ +#ifndef SPRITE_H +#define SPRITE_H + +#include + +#include "rect2.h" +#include "texture.h" + +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); + + 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(); + +private: + Rect2 _texture_clip_rect; + Rect2 _transform; + double _angle; + + float _anchor_x; + float _anchor_y; + + SDL_RendererFlip _flip; + + Texture *_texture; +}; + +#endif \ No newline at end of file