Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
captain0xff committed Apr 13, 2023
1 parent 86eca3d commit 33160a6
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 83 deletions.
42 changes: 33 additions & 9 deletions src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(pos.x);
y = static_cast<int>(pos.y);
w = static_cast<int>(size.x);
h = static_cast<int>(size.y);
}

ostream& operator<<(ostream &os, Rect const &rect) {
cout << rect.to_str();
Expand Down Expand Up @@ -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<int>(vec.x);
y = static_cast<int>(vec.y);
r = radius;
}

ostream& operator<<(ostream &os, const Circle &circle) {
cout << circle.to_str();
return os;
Expand Down Expand Up @@ -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 &center, 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 &center, const SDL_RendererFlip &flip) {
Expand Down
153 changes: 80 additions & 73 deletions src/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};


Expand Down Expand Up @@ -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 &center={0, 0}, const SDL_RendererFlip &flip=SDL_FLIP_NONE);
Expand Down
1 change: 0 additions & 1 deletion todo.md
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 33160a6

Please sign in to comment.