diff --git a/src/engine.cpp b/src/engine.cpp index 9710c6c..a664995 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -199,6 +199,19 @@ void Vector::clamp_ip(const Circle &circle) { } } +Rect::Rect(int x, int y, int w, int h) { + this->x = x; + this->y = y; + this->w = w; + this->h = h; +} + +Rect::Rect(const Vector &pos, const Vector &size) { + x = static_cast(pos.x); + y = static_cast(pos.y); + w = static_cast(size.x); + h = static_cast(size.y); +} ostream& operator<<(ostream &os, Rect const &rect) { cout << rect.to_str(); @@ -426,6 +439,18 @@ void Rect::move_ip(const Vector &vec) { } +Circle::Circle(int x, int y, int r) { + this->x = x; + this->y = y; + this->r = r; +} + +Circle::Circle(const Vector &vec, const int radius) { + x = static_cast(vec.x); + y = static_cast(vec.y); + r = radius; +} + ostream& operator<<(ostream &os, const Circle &circle) { cout << circle.to_str(); return os; @@ -1062,23 +1087,22 @@ void Texture::set_blend_mode(SDL_BlendMode blend_mode) { SDL_SetTextureBlendMode(texture.get(), blend_mode); } +void Texture::render(const Vector &vec) { + render({vec, get_rect().size()}); +} + void Texture::render(const Rect &dst_rect) { - const SDL_Rect src_rect = get_rect(); - const SDL_Rect r2 = dst_rect; - SDL_RenderCopy(tex_renderer -> renderer.get(), texture.get(), &src_rect, &r2); + render(dst_rect, get_rect()); } void Texture::render(const Rect &dst_rect, const Rect &src_rect) { - const SDL_Rect r1 = {src_rect.x, src_rect.y, src_rect.w, src_rect.h}; - const SDL_Rect r2 = {dst_rect.x, dst_rect.y, dst_rect.w, dst_rect.h}; + const SDL_Rect r1 = src_rect; + const SDL_Rect r2 = dst_rect; SDL_RenderCopy(tex_renderer -> renderer.get(), texture.get(), &r1, &r2); } void Texture::render_ex(const Rect &dst_rect, const double &angle, const Vector ¢er, const SDL_RendererFlip &flip) { - const SDL_Rect r1 = {0, 0, w, h}; - const SDL_Rect r2 = {dst_rect.x, dst_rect.y, dst_rect.w, dst_rect.h}; - const SDL_Point p = {(int)center.x, (int)center.y}; - SDL_RenderCopyEx(tex_renderer -> renderer.get(), texture.get(), &r1, &r2, angle, &p, flip); + render_ex(dst_rect, get_rect(), angle, center, flip); } void Texture::render_ex(const Rect &dst_rect, const Rect &src_rect, const double &angle, const Vector ¢er, const SDL_RendererFlip &flip) { diff --git a/src/engine.h b/src/engine.h index 9f78d88..ee9d0c3 100644 --- a/src/engine.h +++ b/src/engine.h @@ -116,87 +116,93 @@ struct Vector { struct Rect { - public: - int x, y, w, h; - - friend ostream& operator<<(ostream &os, Rect const &rect); - - operator SDL_Rect() const; - - const string to_str() const; - - Vector size() const; - void size(const Vector &vec); - void scale(const Vector &vec); - // Scales the height and width by the given factor - void scale(const double val); - double half_width() const; - double half_height() const; - Vector half_size() const; - - double top() const; - void top(const double &val); - double bottom() const; - void bottom(const double &val); - double left() const; - void left(const double &val); - double right() const; - void right(const double &val); - double centerx() const; - void centerx(const double &val); - double centery() const; - void centery(const double &val); - Vector topleft() const; - void topleft(const Vector &vec); - Vector topright() const; - void topright(const Vector &vec); - Vector bottomleft() const; - void bottomleft(const Vector &vec); - Vector bottomright() const; - void bottomright(const Vector &vec); - Vector center() const; - void center(const Vector &vec); - Vector midtop() const; - void midtop(const Vector &vec); - Vector midbottom() const; - void midbottom(const Vector &vec); - Vector midleft() const; - void midleft(const Vector &vec); - Vector midright() const; - void midright(const Vector &vec); - - bool collide_point(const Vector &vec) const; - bool collide_rect(const Rect &rect) const; - // Clamps the rect within the rect passed - Rect clamp(const Rect &rect) const; - // Returns if the rect is clamped or not - bool clamp_ip(const Rect &rect); - Rect move(const Vector &vec) const; - void move_ip(const Vector &vec); + int x, y, w, h; + + Rect() {}; + Rect(int x, int y, int w, int h); + Rect(const Vector &pos, const Vector &size); + + friend ostream& operator<<(ostream &os, Rect const &rect); + + operator SDL_Rect() const; + + const string to_str() const; + + Vector size() const; + void size(const Vector &vec); + void scale(const Vector &vec); + // Scales the height and width by the given factor + void scale(const double val); + double half_width() const; + double half_height() const; + Vector half_size() const; + + double top() const; + void top(const double &val); + double bottom() const; + void bottom(const double &val); + double left() const; + void left(const double &val); + double right() const; + void right(const double &val); + double centerx() const; + void centerx(const double &val); + double centery() const; + void centery(const double &val); + Vector topleft() const; + void topleft(const Vector &vec); + Vector topright() const; + void topright(const Vector &vec); + Vector bottomleft() const; + void bottomleft(const Vector &vec); + Vector bottomright() const; + void bottomright(const Vector &vec); + Vector center() const; + void center(const Vector &vec); + Vector midtop() const; + void midtop(const Vector &vec); + Vector midbottom() const; + void midbottom(const Vector &vec); + Vector midleft() const; + void midleft(const Vector &vec); + Vector midright() const; + void midright(const Vector &vec); + + bool collide_point(const Vector &vec) const; + bool collide_rect(const Rect &rect) const; + // Clamps the rect within the rect passed + Rect clamp(const Rect &rect) const; + // Returns if the rect is clamped or not + bool clamp_ip(const Rect &rect); + Rect move(const Vector &vec) const; + void move_ip(const Vector &vec); }; struct Circle { - public: - int x, y, r; + int x, y, r; - friend ostream& operator<<(ostream &os, const Circle &circle); + Circle() {}; + Circle(int x, int y, int r); + Circle(const Vector &vec, const int radius); - const string to_str() const; + friend ostream& operator<<(ostream &os, const Circle &circle); - double radius() const; - void radius(const double radius); - Vector center() const; - void center(const Vector &vec); + const string to_str() const; - bool collide_point(const Vector &vec) const; - bool collide_circle(const Circle &circle) const; - // Clamps the circle within the circle passed - Circle clamp(const Circle &circle) const; - // Returns if the circle is clamped or not - bool clamp_ip(const Circle &circle); - Circle move(const Vector &vec) const; - void move_ip(const Vector &vec); + double radius() const; + void radius(const double radius); + Vector center() const; + void center(const Vector &vec); + + bool collide_point(const Vector &vec) const; + bool collide_circle(const Circle &circle) const; + // Clamps the circle within the circle passed + Circle clamp(const Circle &circle) const; + // Returns if the circle is clamped or not + bool clamp_ip(const Circle &circle); + Circle move(const Vector &vec) const; + void move_ip(const Vector &vec); }; @@ -392,6 +398,7 @@ class Texture { void set_colour_mod(const Colour &colour); void set_blend_mode(SDL_BlendMode blend_mode); + void render(const Vector &vec); void render(const Rect &dst_rect); void render(const Rect &dst_rect, const Rect &src_rect); void render_ex(const Rect &dst_rect, const double &angle=0, const Vector ¢er={0, 0}, const SDL_RendererFlip &flip=SDL_FLIP_NONE); diff --git a/todo.md b/todo.md index 50bf0ab..f768e15 100644 --- a/todo.md +++ b/todo.md @@ -1,4 +1,3 @@ # Todo -* Check the current status of font class and integrate the header only lib * Add wrappper around the SDL_Logging methods(do this when std::format() is available) * Add a sprite class