|
|
|
@@ -3,6 +3,7 @@
|
|
|
|
|
|
|
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
|
#include <SDL2/SDL_image.h>
|
|
|
|
|
#include <SDL2/SDL_ttf.h>
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <cmath>
|
|
|
|
|
#include <iostream>
|
|
|
|
@@ -15,7 +16,10 @@ namespace SDLPP {
|
|
|
|
|
|
|
|
|
|
int hex2num(char c);
|
|
|
|
|
|
|
|
|
|
std::tuple<int, int, int> getColorsHEX(const std::string &color);
|
|
|
|
|
std::tuple<int, int, int, int> getColorsHEX(const std::string &color);
|
|
|
|
|
SDL_Color getSDLColorHEX(const std::string &color);
|
|
|
|
|
std::tuple<int, int, int, int> getColorsSDLColor(const SDL_Color &color);
|
|
|
|
|
SDL_Color getSDLColorTuple(const std::tuple<int, int, int, int> &tuple);
|
|
|
|
|
|
|
|
|
|
class Window {
|
|
|
|
|
public:
|
|
|
|
@@ -92,6 +96,37 @@ private:
|
|
|
|
|
bool render_coliders = false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class Font {
|
|
|
|
|
public:
|
|
|
|
|
Font() = delete;
|
|
|
|
|
Font(const std::string &font, int size) {
|
|
|
|
|
font_ptr = TTF_OpenFont(font.c_str(), size);
|
|
|
|
|
}
|
|
|
|
|
~Font() {
|
|
|
|
|
TTF_CloseFont( font_ptr );
|
|
|
|
|
}
|
|
|
|
|
const TTF_Font *getFont() const {
|
|
|
|
|
return font_ptr;
|
|
|
|
|
}
|
|
|
|
|
TTF_Font *getFont() {
|
|
|
|
|
return font_ptr;
|
|
|
|
|
}
|
|
|
|
|
void setOutline(int size) {
|
|
|
|
|
TTF_SetFontOutline(font_ptr, size);
|
|
|
|
|
}
|
|
|
|
|
int getOutline() {
|
|
|
|
|
return TTF_GetFontOutline(font_ptr);
|
|
|
|
|
}
|
|
|
|
|
void setStyle(int style) {
|
|
|
|
|
TTF_SetFontStyle(font_ptr, style);
|
|
|
|
|
}
|
|
|
|
|
void setHinting(int hinting) {
|
|
|
|
|
TTF_SetFontHinting(font_ptr, hinting);
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
TTF_Font *font_ptr;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class Texture {
|
|
|
|
|
public:
|
|
|
|
|
Texture() = delete;
|
|
|
|
@@ -106,12 +141,37 @@ public:
|
|
|
|
|
auto colors = getColorsHEX(color_key);
|
|
|
|
|
SDL_SetColorKey(surface, SDL_TRUE, SDL_MapRGB(surface->format, std::get<0>(colors), std::get<1>(colors), std::get<2>(colors)));
|
|
|
|
|
}
|
|
|
|
|
texture = SDL_CreateTextureFromSurface(renderer->getRendererPtr(), surface);
|
|
|
|
|
if( texture == NULL ) {
|
|
|
|
|
std::cerr << "Unable to create texture from '" << img_path << "'! SDL Error: " << SDL_GetError() << std::endl;
|
|
|
|
|
throw "Texture error";
|
|
|
|
|
setTextureFromSurface(renderer, surface);
|
|
|
|
|
}
|
|
|
|
|
Texture(std::shared_ptr<Renderer> &renderer, Font &font, const std::string &text, const std::string &color = "FFFFFF", const std::string &outline_color = "000000", const int outline_size = -1) {
|
|
|
|
|
if(outline_size != -1) {
|
|
|
|
|
font.setOutline(outline_size);
|
|
|
|
|
}
|
|
|
|
|
SDL_FreeSurface(surface);
|
|
|
|
|
int og_outline = 0;
|
|
|
|
|
SDL_Surface *bg_surface = NULL;
|
|
|
|
|
if((og_outline = font.getOutline()) != 0) {
|
|
|
|
|
bg_surface = TTF_RenderUTF8_Blended( font.getFont(), text.c_str(), getSDLColorHEX(outline_color) );
|
|
|
|
|
if( bg_surface == NULL ) {
|
|
|
|
|
std::cerr << "Unable to render text '" << text << "': TTF Error: " << TTF_GetError() << std::endl;
|
|
|
|
|
throw "TTF_RenderUTF8_Shaded error";
|
|
|
|
|
}
|
|
|
|
|
font.setOutline(0);
|
|
|
|
|
}
|
|
|
|
|
SDL_Surface *surface = TTF_RenderUTF8_Blended( font.getFont(), text.c_str(), getSDLColorHEX(color) );
|
|
|
|
|
if( surface == NULL ) {
|
|
|
|
|
std::cerr << "Unable to render text '" << text << "': TTF Error: " << TTF_GetError() << std::endl;
|
|
|
|
|
throw "TTF_RenderUTF8_Shaded error";
|
|
|
|
|
}
|
|
|
|
|
if(og_outline != 0) {
|
|
|
|
|
SDL_Rect rect = {og_outline, og_outline, surface->w, surface->h};
|
|
|
|
|
SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
|
|
|
|
|
SDL_BlitSurface(surface, NULL, bg_surface, &rect);
|
|
|
|
|
SDL_FreeSurface(surface);
|
|
|
|
|
surface = bg_surface;
|
|
|
|
|
bg_surface = NULL;
|
|
|
|
|
font.setOutline(og_outline);
|
|
|
|
|
}
|
|
|
|
|
setTextureFromSurface(renderer, surface);
|
|
|
|
|
}
|
|
|
|
|
~Texture() {
|
|
|
|
|
SDL_DestroyTexture(texture);
|
|
|
|
@@ -120,6 +180,14 @@ public:
|
|
|
|
|
return texture;
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
void setTextureFromSurface(std::shared_ptr<Renderer> &renderer, SDL_Surface *surface) {
|
|
|
|
|
texture = SDL_CreateTextureFromSurface(renderer->getRendererPtr(), surface);
|
|
|
|
|
if( texture == NULL ) {
|
|
|
|
|
std::cerr << "Unable to create texture from surface! SDL Error: " << SDL_GetError() << std::endl;
|
|
|
|
|
throw "Texture error";
|
|
|
|
|
}
|
|
|
|
|
SDL_FreeSurface(surface);
|
|
|
|
|
}
|
|
|
|
|
SDL_Texture *texture = NULL;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
@@ -144,19 +212,24 @@ public:
|
|
|
|
|
position_x = original_x * w + x;
|
|
|
|
|
position_y = original_y * h + y;
|
|
|
|
|
}
|
|
|
|
|
virtual void render(Renderer &renderer, const std::tuple<int,int,int> &color) = 0;
|
|
|
|
|
virtual void render(Renderer &renderer, const std::tuple<int,int,int,int> &color) = 0;
|
|
|
|
|
virtual void render(Renderer &renderer) = 0;
|
|
|
|
|
int getX() const {
|
|
|
|
|
return position_x;
|
|
|
|
|
}
|
|
|
|
|
int getY() const {
|
|
|
|
|
return position_y;
|
|
|
|
|
}
|
|
|
|
|
void setColor(const std::string &color) {
|
|
|
|
|
sdl_color = getSDLColorHEX(color);
|
|
|
|
|
}
|
|
|
|
|
protected:
|
|
|
|
|
double original_x;
|
|
|
|
|
double original_y;
|
|
|
|
|
int position_x;
|
|
|
|
|
int position_y;
|
|
|
|
|
bool infinite = false;
|
|
|
|
|
SDL_Color sdl_color;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class Scene;
|
|
|
|
@@ -206,6 +279,9 @@ public:
|
|
|
|
|
void setTexture(const std::string &img_path) {
|
|
|
|
|
texture = std::make_shared<Texture>(renderer, img_path);
|
|
|
|
|
}
|
|
|
|
|
void setTexture(Font &font, const std::string &text, const std::string &color = "FFFFFF", const std::string &outline_color = "000000", int outline_size = -1) {
|
|
|
|
|
texture = std::make_shared<Texture>(renderer, font, text, color, outline_color, outline_size);
|
|
|
|
|
}
|
|
|
|
|
// per second, relative to window width
|
|
|
|
|
void setMovementSpeed(double speed) {
|
|
|
|
|
movementSpeed = speed;
|
|
|
|
@@ -248,18 +324,26 @@ public:
|
|
|
|
|
virtual void move(int ticks) = 0;
|
|
|
|
|
virtual void updateSizeAndPosition() = 0;
|
|
|
|
|
virtual SDL_Rect getRect() = 0;
|
|
|
|
|
void setPermanent(bool perm) {
|
|
|
|
|
permanent = perm;
|
|
|
|
|
}
|
|
|
|
|
bool getPermanent() const {
|
|
|
|
|
return permanent;
|
|
|
|
|
}
|
|
|
|
|
protected:
|
|
|
|
|
std::vector<std::shared_ptr<CollisionPolygon>> collisions;
|
|
|
|
|
std::shared_ptr<Texture> texture;
|
|
|
|
|
std::shared_ptr<Renderer> renderer;
|
|
|
|
|
std::shared_ptr<CollisionPolygon> polygon;
|
|
|
|
|
double movementSpeed;
|
|
|
|
|
std::pair<int,int> movementDirection;
|
|
|
|
|
std::vector<std::shared_ptr<RenderObject>> colidedWith;
|
|
|
|
|
uint64_t id;
|
|
|
|
|
bool hidden = false;
|
|
|
|
|
bool kill = false;
|
|
|
|
|
std::tuple<int,int,int> colider_color = {0x00, 0xFF, 0xFF};
|
|
|
|
|
std::tuple<int,int,int,int> colider_color = {0x00, 0xFF, 0xFF, 0xFF};
|
|
|
|
|
uint64_t scene_id;
|
|
|
|
|
bool permanent = false;
|
|
|
|
|
private:
|
|
|
|
|
void setSceneID(int id) {
|
|
|
|
|
scene_id = id;
|
|
|
|
@@ -323,7 +407,8 @@ public:
|
|
|
|
|
checkKilled();
|
|
|
|
|
render_mutex.lock();
|
|
|
|
|
SDL_RenderClear(renderer->getRendererPtr());
|
|
|
|
|
SDL_RenderCopy(renderer->getRendererPtr(), background->getTexturePtr(), NULL, NULL);
|
|
|
|
|
if(background && background->getTexturePtr())
|
|
|
|
|
SDL_RenderCopy(renderer->getRendererPtr(), background->getTexturePtr(), NULL, NULL);
|
|
|
|
|
for( const auto &x : renderObjects ) {
|
|
|
|
|
x->render();
|
|
|
|
|
}
|
|
|
|
@@ -405,6 +490,133 @@ private:
|
|
|
|
|
std::mutex render_mutex;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class Rect : public CollisionPolygon {
|
|
|
|
|
public:
|
|
|
|
|
Rect(double x, double y, double w, double h) : CollisionPolygon(x, y) {
|
|
|
|
|
w_ = w;
|
|
|
|
|
h_ = h;
|
|
|
|
|
}
|
|
|
|
|
virtual ~Rect() {}
|
|
|
|
|
virtual bool colidesWith(const CollisionPolygon &other) const override;
|
|
|
|
|
virtual bool isCircle() const override { return false; }
|
|
|
|
|
virtual int topmost() const override {
|
|
|
|
|
return (!isInfinite() || original_y != -1) * getY() + isInfinite() * -1;
|
|
|
|
|
}
|
|
|
|
|
virtual int bottommost() const override {
|
|
|
|
|
return (!isInfinite() || h_ != -1) * (getY() + pixel_h) + isInfinite() * -1;
|
|
|
|
|
};
|
|
|
|
|
virtual int leftmost() const override {
|
|
|
|
|
return (!isInfinite() || original_x != -1) * getX() + isInfinite() * -1;
|
|
|
|
|
}
|
|
|
|
|
virtual int rightmost() const override {
|
|
|
|
|
return (!isInfinite() || w_ != -1) * (getX() + pixel_w) + isInfinite() * -1;
|
|
|
|
|
}
|
|
|
|
|
virtual void updateCollision(int x, int y, int w, int h) override {
|
|
|
|
|
position_x = original_x * w + x;
|
|
|
|
|
position_y = original_y * h + y;
|
|
|
|
|
pixel_w = w_ * w;
|
|
|
|
|
pixel_h = h_ * h;
|
|
|
|
|
}
|
|
|
|
|
virtual void render(Renderer &renderer, const std::tuple<int,int,int,int> &color) override {
|
|
|
|
|
auto rect = getRect();
|
|
|
|
|
// outline with desired color at 50% opacity
|
|
|
|
|
SDL_SetRenderDrawColor(renderer.getRendererPtr(), std::get<0>(color), std::get<1>(color), std::get<2>(color), 0x80);
|
|
|
|
|
SDL_RenderDrawRect(renderer.getRendererPtr(), &rect);
|
|
|
|
|
// fill with desired color at 25% opacity
|
|
|
|
|
SDL_SetRenderDrawColor(renderer.getRendererPtr(), std::get<0>(color), std::get<1>(color), std::get<2>(color), 0x40);
|
|
|
|
|
SDL_RenderFillRect(renderer.getRendererPtr(), &rect);
|
|
|
|
|
}
|
|
|
|
|
virtual void render(Renderer &renderer) override {
|
|
|
|
|
auto rect = getRect();
|
|
|
|
|
SDL_SetRenderDrawColor(renderer.getRendererPtr(), sdl_color.r, sdl_color.g, sdl_color.b, sdl_color.a);
|
|
|
|
|
SDL_RenderFillRect(renderer.getRendererPtr(), &rect);
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
SDL_Rect getRect() {
|
|
|
|
|
if( !isInfinite() )
|
|
|
|
|
return {leftmost(), topmost(), pixel_w, pixel_h};
|
|
|
|
|
|
|
|
|
|
SDL_Rect r = {0,0,0,0};
|
|
|
|
|
if((r.x = leftmost()) == -1)
|
|
|
|
|
r.x = 0;
|
|
|
|
|
if((r.y = topmost()) == -1)
|
|
|
|
|
r.y = 0;
|
|
|
|
|
if(rightmost() == -1)
|
|
|
|
|
r.w = std::numeric_limits<int>::max();
|
|
|
|
|
else
|
|
|
|
|
r.w = pixel_w;
|
|
|
|
|
if(bottommost() == -1)
|
|
|
|
|
r.h = std::numeric_limits<int>::max();
|
|
|
|
|
else
|
|
|
|
|
r.h = pixel_h;
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double w_;
|
|
|
|
|
double h_;
|
|
|
|
|
int pixel_w;
|
|
|
|
|
int pixel_h;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class Circle : public CollisionPolygon {
|
|
|
|
|
public:
|
|
|
|
|
Circle(double x, double y, double rad) : CollisionPolygon(x, y) {
|
|
|
|
|
original_rad = rad;
|
|
|
|
|
}
|
|
|
|
|
virtual ~Circle() {}
|
|
|
|
|
virtual bool colidesWith(const CollisionPolygon &other) const override;
|
|
|
|
|
virtual bool isCircle() const override { return true; }
|
|
|
|
|
virtual int topmost() const override { return getY() - rad_; }
|
|
|
|
|
virtual int bottommost() const override { return getY() + rad_; };
|
|
|
|
|
virtual int leftmost() const override { return getX() - rad_; }
|
|
|
|
|
virtual int rightmost() const override { return getX() + rad_; }
|
|
|
|
|
virtual void updateCollision(int x, int y, int w, int h) override {
|
|
|
|
|
position_x = original_x * w + x;
|
|
|
|
|
position_y = original_y * h + y;
|
|
|
|
|
rad_ = original_rad * w;
|
|
|
|
|
}
|
|
|
|
|
virtual void render(Renderer &renderer, const std::tuple<int,int,int,int> &color) override {
|
|
|
|
|
std::vector<int> rect = {leftmost(), topmost(), rightmost(), bottommost()};
|
|
|
|
|
auto center_x = getX();
|
|
|
|
|
auto center_y = getY();
|
|
|
|
|
auto radsq = rad_ * rad_;
|
|
|
|
|
for(int i = rect[0]; i <= rect[2]; i++) {
|
|
|
|
|
auto xdiff = center_x - i;
|
|
|
|
|
auto xdist = xdiff * xdiff;
|
|
|
|
|
auto allowed_rad = sqrt(radsq - xdist);
|
|
|
|
|
SDL_SetRenderDrawColor(renderer.getRendererPtr(), std::get<0>(color), std::get<1>(color), std::get<2>(color), 0x40);
|
|
|
|
|
SDL_RenderDrawLine(renderer.getRendererPtr(), i, center_y - allowed_rad, i, center_y + allowed_rad);
|
|
|
|
|
SDL_SetRenderDrawColor(renderer.getRendererPtr(), std::get<0>(color), std::get<1>(color), std::get<2>(color), 0x80);
|
|
|
|
|
SDL_RenderDrawLine(renderer.getRendererPtr(), i, center_y - allowed_rad, i, center_y - allowed_rad + 2);
|
|
|
|
|
SDL_RenderDrawLine(renderer.getRendererPtr(), i, center_y + allowed_rad, i, center_y + allowed_rad - 2);
|
|
|
|
|
}
|
|
|
|
|
SDL_SetRenderDrawColor(renderer.getRendererPtr(), 0xFF, 0, 0, 0xFF);
|
|
|
|
|
SDL_RenderDrawLine(renderer.getRendererPtr(), center_x, center_y, center_x + rad_, center_y);
|
|
|
|
|
SDL_RenderDrawLine(renderer.getRendererPtr(), center_x, center_y, center_x, center_y + rad_);
|
|
|
|
|
SDL_RenderDrawLine(renderer.getRendererPtr(), center_x, center_y, center_x - rad_, center_y);
|
|
|
|
|
SDL_RenderDrawLine(renderer.getRendererPtr(), center_x, center_y, center_x, center_y - rad_);
|
|
|
|
|
}
|
|
|
|
|
virtual void render(Renderer &renderer) override {
|
|
|
|
|
std::vector<int> rect = {leftmost(), topmost(), rightmost(), bottommost()};
|
|
|
|
|
auto center_x = getX();
|
|
|
|
|
auto center_y = getY();
|
|
|
|
|
auto radsq = rad_ * rad_;
|
|
|
|
|
for(int i = rect[0]; i <= rect[2]; i++) {
|
|
|
|
|
auto xdiff = center_x - i;
|
|
|
|
|
auto xdist = xdiff * xdiff;
|
|
|
|
|
auto allowed_rad = sqrt(radsq - xdist);
|
|
|
|
|
SDL_SetRenderDrawColor(renderer.getRendererPtr(), sdl_color.r, sdl_color.g, sdl_color.b, sdl_color.a);
|
|
|
|
|
SDL_RenderDrawLine(renderer.getRendererPtr(), i, center_y - allowed_rad, i, center_y + allowed_rad);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
int getRadius() const {
|
|
|
|
|
return rad_;
|
|
|
|
|
}
|
|
|
|
|
double original_rad;
|
|
|
|
|
int rad_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class RectangleRender : public RenderObject {
|
|
|
|
|
public:
|
|
|
|
|
RectangleRender() = delete;
|
|
|
|
@@ -420,12 +632,18 @@ public:
|
|
|
|
|
w_ = w;
|
|
|
|
|
h_ = h;
|
|
|
|
|
}
|
|
|
|
|
RectangleRender(int x, int y, int w, int h, std::shared_ptr<Renderer> &r, std::shared_ptr<Texture> &t) : RectangleRender(x, y, w, h, r) {
|
|
|
|
|
RectangleRender(double x, double y, double w, double h, std::shared_ptr<Renderer> &r, std::shared_ptr<Texture> &t) : RectangleRender(x, y, w, h, r) {
|
|
|
|
|
setTexture(t);
|
|
|
|
|
}
|
|
|
|
|
RectangleRender(int x, int y, int w, int h, std::shared_ptr<Renderer> &r, const std::string &img_path) : RectangleRender(x,y,w,h,r) {
|
|
|
|
|
auto texture = std::make_shared<Texture>(r, img_path);
|
|
|
|
|
setTexture(texture);
|
|
|
|
|
RectangleRender(double x, double y, double w, double h, std::shared_ptr<Renderer> &r, const std::string &img_or_color, bool is_polygon = false) : RectangleRender(x,y,w,h,r) {
|
|
|
|
|
if(!is_polygon) {
|
|
|
|
|
auto texture = std::make_shared<Texture>(r, img_or_color);
|
|
|
|
|
setTexture(texture);
|
|
|
|
|
} else {
|
|
|
|
|
polygon = std::make_shared<Rect>(0,0,1,1);
|
|
|
|
|
polygon->setColor(img_or_color);
|
|
|
|
|
polygon->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
virtual void specialAction(int /*UNUSED*/) {};
|
|
|
|
|
virtual void render() {
|
|
|
|
@@ -435,8 +653,13 @@ public:
|
|
|
|
|
for(const auto &col : getCollisions())
|
|
|
|
|
col->render(*renderer, colider_color);
|
|
|
|
|
}
|
|
|
|
|
if(polygon) {
|
|
|
|
|
polygon->render(*renderer);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
virtual void move(int ticks) {
|
|
|
|
|
if(permanent)
|
|
|
|
|
return;
|
|
|
|
|
auto dimension = renderer->getSmallerSide();
|
|
|
|
|
auto addx = static_cast<double>(movementSpeed * movementDirection.first)*(static_cast<double>(ticks)/1000);
|
|
|
|
|
auto addy = static_cast<double>(movementSpeed * movementDirection.second)*(static_cast<double>(ticks)/1000);
|
|
|
|
@@ -447,6 +670,8 @@ public:
|
|
|
|
|
for( auto &x : collisions ) {
|
|
|
|
|
x->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight());
|
|
|
|
|
}
|
|
|
|
|
if(polygon)
|
|
|
|
|
polygon->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight());
|
|
|
|
|
}
|
|
|
|
|
virtual std::pair<std::pair<double,double>,std::pair<double,double>> getDoubleRect() {
|
|
|
|
|
return {{x_,y_}, {w_,h_}};
|
|
|
|
@@ -488,6 +713,8 @@ public:
|
|
|
|
|
rect.y = y_ * dimension;
|
|
|
|
|
rect.w = w_ * dimension;
|
|
|
|
|
rect.h = h_ * dimension;
|
|
|
|
|
if(polygon)
|
|
|
|
|
polygon->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight());
|
|
|
|
|
}
|
|
|
|
|
virtual SDL_Rect getRect() {
|
|
|
|
|
return rect;
|
|
|
|
@@ -535,115 +762,6 @@ private:
|
|
|
|
|
int rad_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class Rect : public CollisionPolygon {
|
|
|
|
|
public:
|
|
|
|
|
Rect(double x, double y, double w, double h) : CollisionPolygon(x, y) {
|
|
|
|
|
w_ = w;
|
|
|
|
|
h_ = h;
|
|
|
|
|
}
|
|
|
|
|
virtual ~Rect() {}
|
|
|
|
|
virtual bool colidesWith(const CollisionPolygon &other) const override;
|
|
|
|
|
virtual bool isCircle() const override { return false; }
|
|
|
|
|
virtual int topmost() const override {
|
|
|
|
|
return (!isInfinite() || original_y != -1) * getY() + isInfinite() * -1;
|
|
|
|
|
}
|
|
|
|
|
virtual int bottommost() const override {
|
|
|
|
|
return (!isInfinite() || h_ != -1) * (getY() + pixel_h) + isInfinite() * -1;
|
|
|
|
|
};
|
|
|
|
|
virtual int leftmost() const override {
|
|
|
|
|
return (!isInfinite() || original_x != -1) * getX() + isInfinite() * -1;
|
|
|
|
|
}
|
|
|
|
|
virtual int rightmost() const override {
|
|
|
|
|
return (!isInfinite() || w_ != -1) * (getX() + pixel_w) + isInfinite() * -1;
|
|
|
|
|
}
|
|
|
|
|
virtual void updateCollision(int x, int y, int w, int h) override {
|
|
|
|
|
position_x = original_x * w + x;
|
|
|
|
|
position_y = original_y * h + y;
|
|
|
|
|
pixel_w = w_ * w;
|
|
|
|
|
pixel_h = h_ * h;
|
|
|
|
|
}
|
|
|
|
|
virtual void render(Renderer &renderer, const std::tuple<int,int,int> &color) override {
|
|
|
|
|
auto rect = getRect();
|
|
|
|
|
// outline with desired color at 50% opacity
|
|
|
|
|
SDL_SetRenderDrawColor(renderer.getRendererPtr(), std::get<0>(color), std::get<1>(color), std::get<2>(color), 0x80);
|
|
|
|
|
SDL_RenderDrawRect(renderer.getRendererPtr(), &rect);
|
|
|
|
|
// fill with desired color at 25% opacity
|
|
|
|
|
SDL_SetRenderDrawColor(renderer.getRendererPtr(), std::get<0>(color), std::get<1>(color), std::get<2>(color), 0x40);
|
|
|
|
|
SDL_RenderFillRect(renderer.getRendererPtr(), &rect);
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
SDL_Rect getRect() {
|
|
|
|
|
if( !isInfinite() )
|
|
|
|
|
return {leftmost(), topmost(), pixel_w, pixel_h};
|
|
|
|
|
|
|
|
|
|
SDL_Rect r = {0,0,0,0};
|
|
|
|
|
if((r.x = leftmost()) == -1)
|
|
|
|
|
r.x = 0;
|
|
|
|
|
if((r.y = topmost()) == -1)
|
|
|
|
|
r.y = 0;
|
|
|
|
|
if(rightmost() == -1)
|
|
|
|
|
r.w = std::numeric_limits<int>::max();
|
|
|
|
|
else
|
|
|
|
|
r.w = pixel_w;
|
|
|
|
|
if(bottommost() == -1)
|
|
|
|
|
r.h = std::numeric_limits<int>::max();
|
|
|
|
|
else
|
|
|
|
|
r.h = pixel_h;
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double w_;
|
|
|
|
|
double h_;
|
|
|
|
|
int pixel_w;
|
|
|
|
|
int pixel_h;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class Circle : public CollisionPolygon {
|
|
|
|
|
public:
|
|
|
|
|
Circle(double x, double y, double rad) : CollisionPolygon(x, y) {
|
|
|
|
|
original_rad = rad;
|
|
|
|
|
}
|
|
|
|
|
virtual ~Circle() {}
|
|
|
|
|
virtual bool colidesWith(const CollisionPolygon &other) const override;
|
|
|
|
|
virtual bool isCircle() const override { return true; }
|
|
|
|
|
virtual int topmost() const override { return getY() - rad_; }
|
|
|
|
|
virtual int bottommost() const override { return getY() + rad_; };
|
|
|
|
|
virtual int leftmost() const override { return getX() - rad_; }
|
|
|
|
|
virtual int rightmost() const override { return getX() + rad_; }
|
|
|
|
|
virtual void updateCollision(int x, int y, int w, int h) override {
|
|
|
|
|
position_x = original_x * w + x;
|
|
|
|
|
position_y = original_y * h + y;
|
|
|
|
|
rad_ = original_rad * w;
|
|
|
|
|
}
|
|
|
|
|
virtual void render(Renderer &renderer, const std::tuple<int,int,int> &color) override {
|
|
|
|
|
std::vector<int> rect = {leftmost(), topmost(), rightmost(), bottommost()};
|
|
|
|
|
auto center_x = getX();
|
|
|
|
|
auto center_y = getY();
|
|
|
|
|
auto radsq = rad_ * rad_;
|
|
|
|
|
for(int i = rect[0]; i <= rect[2]; i++) {
|
|
|
|
|
auto xdiff = center_x - i;
|
|
|
|
|
auto xdist = xdiff * xdiff;
|
|
|
|
|
auto allowed_rad = sqrt(radsq - xdist);
|
|
|
|
|
SDL_SetRenderDrawColor(renderer.getRendererPtr(), std::get<0>(color), std::get<1>(color), std::get<2>(color), 0x40);
|
|
|
|
|
SDL_RenderDrawLine(renderer.getRendererPtr(), i, center_y - allowed_rad, i, center_y + allowed_rad);
|
|
|
|
|
SDL_SetRenderDrawColor(renderer.getRendererPtr(), std::get<0>(color), std::get<1>(color), std::get<2>(color), 0x80);
|
|
|
|
|
SDL_RenderDrawLine(renderer.getRendererPtr(), i, center_y - allowed_rad, i, center_y - allowed_rad + 2);
|
|
|
|
|
SDL_RenderDrawLine(renderer.getRendererPtr(), i, center_y + allowed_rad, i, center_y + allowed_rad - 2);
|
|
|
|
|
}
|
|
|
|
|
SDL_SetRenderDrawColor(renderer.getRendererPtr(), 0xFF, 0, 0, 0xFF);
|
|
|
|
|
SDL_RenderDrawLine(renderer.getRendererPtr(), center_x, center_y, center_x + rad_, center_y);
|
|
|
|
|
SDL_RenderDrawLine(renderer.getRendererPtr(), center_x, center_y, center_x, center_y + rad_);
|
|
|
|
|
SDL_RenderDrawLine(renderer.getRendererPtr(), center_x, center_y, center_x - rad_, center_y);
|
|
|
|
|
SDL_RenderDrawLine(renderer.getRendererPtr(), center_x, center_y, center_x, center_y - rad_);
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
int getRadius() const {
|
|
|
|
|
return rad_;
|
|
|
|
|
}
|
|
|
|
|
double original_rad;
|
|
|
|
|
int rad_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
bool init();
|
|
|
|
|
bool init(uint32_t SDL_OPTIONS);
|
|
|
|
|
bool init(uint32_t SDL_OPTIONS, int IMAGE_OPTIONS);
|
|
|
|
|