Make rendering dependant on smaller side only

This commit is contained in:
zvon 2020-07-28 14:46:48 +02:00
parent 9195be85d0
commit 9c1fef7a86
2 changed files with 33 additions and 21 deletions

View File

@ -37,7 +37,7 @@ public:
jumping = true; jumping = true;
} }
virtual void move(int ticks) { virtual void move(int ticks) {
auto dimensions = renderer->getDimensions(); auto dimension = renderer->getSmallerSide();
auto time_portion = (static_cast<double>(ticks)/1000); auto time_portion = (static_cast<double>(ticks)/1000);
auto addx = static_cast<double>(movementSpeed * movementDirection.first)*time_portion; auto addx = static_cast<double>(movementSpeed * movementDirection.first)*time_portion;
auto addy = static_cast<double>(movementSpeed * movementDirection.second)*time_portion; auto addy = static_cast<double>(movementSpeed * movementDirection.second)*time_portion;
@ -52,8 +52,8 @@ public:
jump_ = 0; jump_ = 0;
y_ += grav * time_portion; y_ += grav * time_portion;
y_ -= jump_ * time_portion; y_ -= jump_ * time_portion;
rect.x = x_ * dimensions.first; rect.x = x_ * dimension;
rect.y = y_ * dimensions.second; rect.y = y_ * dimension;
for( auto &x : collisions ) { for( auto &x : collisions ) {
x->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight()); x->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight());
} }

View File

@ -56,6 +56,20 @@ public:
SDL_GetRendererOutputSize(renderer, &width, &height); SDL_GetRendererOutputSize(renderer, &width, &height);
return {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: private:
SDL_Renderer *renderer = NULL; SDL_Renderer *renderer = NULL;
}; };
@ -302,12 +316,11 @@ public:
RectangleRender() = delete; RectangleRender() = delete;
virtual ~RectangleRender() {}; virtual ~RectangleRender() {};
RectangleRender(double x, double y, double w, double h, std::shared_ptr<Renderer> &r) : RenderObject(r) { RectangleRender(double x, double y, double w, double h, std::shared_ptr<Renderer> &r) : RenderObject(r) {
auto dimensions = renderer->getDimensions(); auto dimension = renderer->getSmallerSide();
auto smaller_dimension = dimensions.first < dimensions.second ? dimensions.first : dimensions.second; rect.x = x * dimension;
rect.x = x * dimensions.first; rect.y = y * dimension;
rect.y = y * dimensions.second; rect.w = w * dimension;
rect.w = w * smaller_dimension; rect.h = h * dimension;
rect.h = h * smaller_dimension;
x_ = x; x_ = x;
y_ = y; y_ = y;
w_ = w; w_ = w;
@ -324,13 +337,13 @@ public:
SDL_RenderCopy(renderer->getRendererPtr(), texture->getTexturePtr(), NULL, &rect); SDL_RenderCopy(renderer->getRendererPtr(), texture->getTexturePtr(), NULL, &rect);
} }
virtual void move(int ticks) { virtual void move(int ticks) {
auto dimensions = renderer->getDimensions(); auto dimension = renderer->getSmallerSide();
auto addx = static_cast<double>(movementSpeed * movementDirection.first)*(static_cast<double>(ticks)/1000); 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); auto addy = static_cast<double>(movementSpeed * movementDirection.second)*(static_cast<double>(ticks)/1000);
x_ += addx; x_ += addx;
y_ += addy; y_ += addy;
rect.x = x_ * dimensions.first; rect.x = x_ * dimension;
rect.y = y_ * dimensions.second; rect.y = y_ * dimension;
for( auto &x : collisions ) { for( auto &x : collisions ) {
x->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight()); x->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight());
} }
@ -339,11 +352,11 @@ public:
return {{x_,y_}, {w_,h_}}; return {{x_,y_}, {w_,h_}};
} }
virtual void setPos(double x, double y) { virtual void setPos(double x, double y) {
auto dimensions = renderer->getDimensions(); auto dimension = renderer->getSmallerSide();
x_ = x; x_ = x;
y_ = y; y_ = y;
rect.x = x_ * dimensions.first; rect.x = x_ * dimension;
rect.y = y_ * dimensions.second; rect.y = y_ * dimension;
} }
virtual int leftmost() { virtual int leftmost() {
return rect.x; return rect.x;
@ -364,12 +377,11 @@ public:
return rect.h; return rect.h;
} }
virtual void updateSizeAndPosition() { virtual void updateSizeAndPosition() {
auto dimensions = renderer->getDimensions(); auto dimension = renderer->getSmallerSide();
auto smaller_dimension = dimensions.first < dimensions.second ? dimensions.first : dimensions.second; rect.x = x_ * dimension;
rect.x = x_ * dimensions.first; rect.y = y_ * dimension;
rect.y = y_ * dimensions.second; rect.w = w_ * dimension;
rect.w = w_ * smaller_dimension; rect.h = h_ * dimension;
rect.h = h_ * smaller_dimension;
} }
protected: protected:
double x_; double x_;