diff --git a/main.cpp b/main.cpp index 86fcbaa..cf8a463 100644 --- a/main.cpp +++ b/main.cpp @@ -37,7 +37,7 @@ public: jumping = true; } virtual void move(int ticks) { - auto dimensions = renderer->getDimensions(); + auto dimension = renderer->getSmallerSide(); auto time_portion = (static_cast(ticks)/1000); auto addx = static_cast(movementSpeed * movementDirection.first)*time_portion; auto addy = static_cast(movementSpeed * movementDirection.second)*time_portion; @@ -52,8 +52,8 @@ public: jump_ = 0; y_ += grav * time_portion; y_ -= jump_ * time_portion; - rect.x = x_ * dimensions.first; - rect.y = y_ * dimensions.second; + rect.x = x_ * dimension; + rect.y = y_ * dimension; for( auto &x : collisions ) { x->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight()); } diff --git a/sdlpp.hpp b/sdlpp.hpp index 23dccf0..0f8bcbf 100644 --- a/sdlpp.hpp +++ b/sdlpp.hpp @@ -56,6 +56,20 @@ public: SDL_GetRendererOutputSize(renderer, &width, &height); return {width, height}; } + int getWidth() { + return getDimensions().first; + } + int getHeight() { + return getDimensions().second; + } + int getSmallerSide() { + auto dimensions = getDimensions(); + return dimensions.first < dimensions.second ? dimensions.first : dimensions.second; + } + int getLargerSide() { + auto dimensions = getDimensions(); + return dimensions.first > dimensions.second ? dimensions.first : dimensions.second; + } private: SDL_Renderer *renderer = NULL; }; @@ -302,12 +316,11 @@ public: RectangleRender() = delete; virtual ~RectangleRender() {}; RectangleRender(double x, double y, double w, double h, std::shared_ptr &r) : RenderObject(r) { - auto dimensions = renderer->getDimensions(); - auto smaller_dimension = dimensions.first < dimensions.second ? dimensions.first : dimensions.second; - rect.x = x * dimensions.first; - rect.y = y * dimensions.second; - rect.w = w * smaller_dimension; - rect.h = h * smaller_dimension; + auto dimension = renderer->getSmallerSide(); + rect.x = x * dimension; + rect.y = y * dimension; + rect.w = w * dimension; + rect.h = h * dimension; x_ = x; y_ = y; w_ = w; @@ -324,13 +337,13 @@ public: SDL_RenderCopy(renderer->getRendererPtr(), texture->getTexturePtr(), NULL, &rect); } virtual void move(int ticks) { - auto dimensions = renderer->getDimensions(); + 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; - rect.x = x_ * dimensions.first; - rect.y = y_ * dimensions.second; + rect.x = x_ * dimension; + rect.y = y_ * dimension; for( auto &x : collisions ) { x->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight()); } @@ -339,11 +352,11 @@ public: return {{x_,y_}, {w_,h_}}; } virtual void setPos(double x, double y) { - auto dimensions = renderer->getDimensions(); + auto dimension = renderer->getSmallerSide(); x_ = x; y_ = y; - rect.x = x_ * dimensions.first; - rect.y = y_ * dimensions.second; + rect.x = x_ * dimension; + rect.y = y_ * dimension; } virtual int leftmost() { return rect.x; @@ -364,12 +377,11 @@ public: return rect.h; } virtual void updateSizeAndPosition() { - auto dimensions = renderer->getDimensions(); - auto smaller_dimension = dimensions.first < dimensions.second ? dimensions.first : dimensions.second; - rect.x = x_ * dimensions.first; - rect.y = y_ * dimensions.second; - rect.w = w_ * smaller_dimension; - rect.h = h_ * smaller_dimension; + auto dimension = renderer->getSmallerSide(); + rect.x = x_ * dimension; + rect.y = y_ * dimension; + rect.w = w_ * dimension; + rect.h = h_ * dimension; } protected: double x_;