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;
}
virtual void move(int ticks) {
auto dimensions = renderer->getDimensions();
auto dimension = renderer->getSmallerSide();
auto time_portion = (static_cast<double>(ticks)/1000);
auto addx = static_cast<double>(movementSpeed * movementDirection.first)*time_portion;
auto addy = static_cast<double>(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());
}

View File

@ -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<Renderer> &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<double>(movementSpeed * movementDirection.first)*(static_cast<double>(ticks)/1000);
auto addy = static_cast<double>(movementSpeed * movementDirection.second)*(static_cast<double>(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_;