From 6acfff3e8fc018bd9e7693dbc6b5c963f149dae2 Mon Sep 17 00:00:00 2001 From: zvon Date: Sun, 23 Aug 2020 01:08:10 +0200 Subject: [PATCH] Changes done during tetris development --- main.cpp | 7 +-- sdlpp.cpp | 8 +-- sdlpp.hpp | 151 +++++++++++++++++++++++++++++++++--------------------- 3 files changed, 102 insertions(+), 64 deletions(-) diff --git a/main.cpp b/main.cpp index d449f8e..daab507 100644 --- a/main.cpp +++ b/main.cpp @@ -72,8 +72,8 @@ public: } if(jump_ < 0 || jump_ > jump_speed) jump_ = 0; - y_ += grav * time_portion; - y_ -= jump_ * time_portion; + og_y += grav * time_portion; + og_y -= jump_ * time_portion; } private: double max_gravity = 1.0; @@ -147,7 +147,8 @@ void addStuff(SDLPP::Scene &scene, std::shared_ptr &r) { scene.addObject(z); auto y = std::make_shared(0, 0, 0.2, 0.1, r); y->setTexture(*font, "DEMO", "#FFFFFF", "#000000", 5); - y->setId(0); + y->setPermanent(true); + y->setId(123); scene.addObject(y); } diff --git a/sdlpp.cpp b/sdlpp.cpp index 29079ba..2a4ec99 100644 --- a/sdlpp.cpp +++ b/sdlpp.cpp @@ -58,10 +58,10 @@ bool infinityIntersection(const SDLPP::CollisionPolygon &infinite, const SDLPP:: int iright = infinite.rightmost(); int itop = infinite.topmost(); int ibottom = infinite.bottommost(); - bool ret = ileft != -1 && ileft <= other.rightmost(); - ret |= iright != -1 && iright >= other.leftmost(); - ret |= itop != -1 && itop <= other.bottommost(); - ret |= ibottom != -1 && ibottom >= other.topmost(); + bool ret = ileft != -1 && ileft <= other.rightmost() && ileft >= other.leftmost(); + ret |= iright != -1 && iright >= other.leftmost() && iright <= other.rightmost(); + ret |= itop != -1 && itop <= other.bottommost() && itop >= other.topmost(); + ret |= ibottom != -1 && ibottom >= other.topmost() && ibottom <= other.bottommost(); ret |= ileft == -1 && iright == -1 && itop == -1 && ibottom == -1; return ret; } diff --git a/sdlpp.hpp b/sdlpp.hpp index 4a5a9f2..9dc5870 100644 --- a/sdlpp.hpp +++ b/sdlpp.hpp @@ -10,6 +10,7 @@ #include #include #include +#include #include namespace SDLPP { @@ -223,13 +224,17 @@ public: void setColor(const std::string &color) { sdl_color = getSDLColorHEX(color); } + void setOutlineColor(const std::string &color) { + sdl_outline = getSDLColorHEX(color); + } protected: double original_x; double original_y; int position_x; int position_y; bool infinite = false; - SDL_Color sdl_color; + SDL_Color sdl_color = {0,0,0,0}; + SDL_Color sdl_outline = {0,0,0,0}; }; class Scene; @@ -250,6 +255,7 @@ public: virtual void specialAction(int code) = 0; virtual std::pair,std::pair> getDoubleRect() = 0; virtual void setPos(double x, double y) = 0; + virtual std::pair getPos() = 0; bool colidesWith(const RenderObject &other) const { if(!hasCollisions() || !other.hasCollisions() || getHidden() || other.getHidden()) { return false; @@ -273,16 +279,17 @@ public: const std::vector> &getCollisions() const { return collisions; } - void setTexture(std::shared_ptr &t) { + virtual void setTexture(std::shared_ptr &t) { texture = t; } - void setTexture(const std::string &img_path) { + virtual void setTexture(const std::string &img_path) { texture = std::make_shared(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) { + virtual 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(renderer, font, text, color, outline_color, outline_size); } virtual void setColor(const std::string &color) = 0; + virtual void setOutlineColor(const std::string &color) = 0; virtual void unsetTexture() { texture.reset(); } @@ -397,6 +404,9 @@ public: std::shared_ptr getObject(int index) { return renderObjects[index]; } + std::vector> getObjects() { + return renderObjects; + } void movement() { checkKilled(); render_mutex.lock(); @@ -418,6 +428,17 @@ public: } return ret; } + std::vector> getCollisions( RenderObject &r, const std::unordered_set &objectIDs ) { + if(r.getHidden()) + return {}; + std::vector> ret{}; + for(const auto &x : collisionObjects) { + if(objectIDs.find(x->getId()) != objectIDs.end() && x->colidesWith(r)) { + ret.push_back(x); + } + } + return ret; + } void renderScene(bool clear_scene = true) { checkKilled(); render_mutex.lock(); @@ -454,6 +475,8 @@ public: checkKilled(); render_mutex.lock(); for( auto &obj : renderObjects ) { + if(obj->getPermanent()) + continue; auto curPos = obj->getDoubleRect(); obj->setPos( curPos.first.first + x, curPos.first.second + y ); } @@ -546,6 +569,8 @@ public: auto rect = getRect(); SDL_SetRenderDrawColor(renderer.getRendererPtr(), sdl_color.r, sdl_color.g, sdl_color.b, sdl_color.a); SDL_RenderFillRect(renderer.getRendererPtr(), &rect); + SDL_SetRenderDrawColor(renderer.getRendererPtr(), sdl_outline.r, sdl_outline.g, sdl_outline.b, sdl_outline.a); + SDL_RenderDrawRect(renderer.getRendererPtr(), &rect); } private: SDL_Rect getRect() { @@ -623,6 +648,9 @@ public: 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); + SDL_SetRenderDrawColor(renderer.getRendererPtr(), sdl_outline.r, sdl_outline.g, sdl_outline.b, sdl_outline.a); + 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); } } private: @@ -638,15 +666,11 @@ public: RectangleRender() = delete; virtual ~RectangleRender() {}; RectangleRender(double x, double y, double w, double h, std::shared_ptr &r) : RenderObject(r) { - auto dimension = renderer->getSmallerSide(); - rect.x = x * dimension; - rect.y = y * dimension; - rect.w = w * dimension; - rect.h = h * dimension; og_x = x_ = x; og_y = y_ = y; og_w = w_ = w; og_h = h_ = h; + updateSizeAndPosition(); } RectangleRender(double x, double y, double w, double h, std::shared_ptr &r, std::shared_ptr &t) : RectangleRender(x, y, w, h, r) { setTexture(t); @@ -658,80 +682,86 @@ public: setColor(img_or_color); } } - virtual void setColor(const std::string &color) { - polygon = std::make_shared(0,0,1,1); - polygon->setColor(color); - polygon->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight()); - } - virtual void specialAction(int /*UNUSED*/) {}; - virtual void render() { - if(polygon) { - polygon->render(*renderer); + virtual void setColor(const std::string &color) override { + if(!polygon) { + polygon = std::make_shared(0,0,1,1); + polygon->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight()); + } + polygon->setColor(color); + } + virtual void setOutlineColor(const std::string &color) override { + if(!polygon) { + polygon = std::make_shared(0,0,1,1); + polygon->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight()); + } + polygon->setOutlineColor(color); + } + virtual void specialAction(int /*UNUSED*/) override {}; + virtual void render() override { + if(!getHidden()) { + if(polygon) + polygon->render(*renderer); + if(texture != NULL) + SDL_RenderCopy(renderer->getRendererPtr(), texture->getTexturePtr(), NULL, &rect); } - if(texture != NULL && !getHidden()) - SDL_RenderCopy(renderer->getRendererPtr(), texture->getTexturePtr(), NULL, &rect); if(hasCollisions() && renderer->getRenderColiders() && !getHidden()) { for(const auto &col : getCollisions()) col->render(*renderer, colider_color); } } - virtual void move(int ticks) { + virtual void move(int ticks) override { if(permanent) return; - auto dimension = renderer->getSmallerSide(); auto addx = static_cast(movementSpeed * movementDirection.first)*(static_cast(ticks)/1000); auto addy = static_cast(movementSpeed * movementDirection.second)*(static_cast(ticks)/1000); - x_ += addx; - y_ += addy; + if(std::isnan(addx) || std::isnan(addy)) + return; + + og_x += addx; + og_y += addy; custom_move(ticks); - rect.x = x_ * dimension; - rect.y = y_ * dimension; - for( auto &x : collisions ) { - x->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight()); - } - if(polygon) - polygon->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight()); + updateSizeAndPosition(); } - virtual void custom_move(int /*UNUSED*/) {} - virtual std::pair,std::pair> getDoubleRect() { - return {{x_,y_}, {w_,h_}}; + virtual void custom_move(int /*UNUSED*/) override {} + virtual std::pair,std::pair> getDoubleRect() override { + return {{og_x,og_y}, {og_w,og_h}}; } - virtual void setPos(double x, double y) { - auto dimension = renderer->getSmallerSide(); - x_ = x; - y_ = y; - rect.x = x_ * dimension; - rect.y = y_ * dimension; + virtual void setPos(double x, double y) override { + og_x = x; + og_y = y; + updateSizeAndPosition(); } - virtual int leftmost() { + virtual std::pair getPos() override { + return {og_x, og_y}; + } + virtual int leftmost() override { return rect.x; } - virtual int topmost() { + virtual int topmost() override { return rect.y; } - virtual int rightmost() { + virtual int rightmost() override { return rect.x + rect.w; } - virtual int bottommost() { + virtual int bottommost() override { return rect.y + rect.h; } - virtual int collisionPushX() { + virtual int collisionPushX() override { return rect.x; } - virtual int collisionPushY() { + virtual int collisionPushY() override { return rect.y; } - virtual int collisionWidth() { + virtual int collisionWidth() override { return rect.w; } - virtual int collisionHeight() { + virtual int collisionHeight() override { return rect.h; } - virtual void updateSizeAndPosition() { - if(centerx) - actuallyCenterX(); + virtual void updateSizeAndPosition() override { + updateXY(); auto dimension = renderer->getSmallerSide(); rect.x = x_ * dimension; rect.y = y_ * dimension; @@ -739,26 +769,33 @@ public: rect.h = h_ * dimension; if(polygon) polygon->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight()); + for( auto &x : collisions ) { + x->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight()); + } } - virtual SDL_Rect getRect() { + virtual SDL_Rect getRect() override { return rect; } - virtual void centerX() { + virtual void centerX() override { centerx = true; updateSizeAndPosition(); } protected: - void actuallyCenterX() { + void updateXY() { + if(!centerx) { + x_ = og_x; + y_ = og_y; + return; + } auto width = renderer->getWidth(); auto height = renderer->getHeight(); if(width > height) { auto multiplier = static_cast(width)/static_cast(height); - x_ = og_x * multiplier; - w_ = og_w * multiplier; + x_ = og_x + static_cast(multiplier - 1)/2; } else { x_ = og_x; - w_ = og_w; } + y_ = og_y; } double og_x; double og_y;