Add pause scene, make custom movement easier to implement

This commit is contained in:
2020-08-22 14:13:28 +02:00
parent 329ef7f40f
commit bf0d08ae16
2 changed files with 147 additions and 52 deletions
+58 -11
View File
@@ -277,8 +277,11 @@ public:
texture = t;
}
void setTexture(const std::string &img_path) {
if(polygon)
polygon.reset();
texture = std::make_shared<Texture>(renderer, img_path);
}
virtual void setColor(const std::string &color) = 0;
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);
}
@@ -290,6 +293,12 @@ public:
movementDirection.first += x;
movementDirection.second += y;
}
void resetMovementX() {
movementDirection.first = 0;
}
void resetMovementY() {
movementDirection.second = 0;
}
void clearColided() {
colidedWith.clear();
}
@@ -322,6 +331,7 @@ public:
colider_color = getColorsHEX(color);
}
virtual void move(int ticks) = 0;
virtual void custom_move(int ticks) = 0;
virtual void updateSizeAndPosition() = 0;
virtual SDL_Rect getRect() = 0;
void setPermanent(bool perm) {
@@ -330,6 +340,7 @@ public:
bool getPermanent() const {
return permanent;
}
virtual void centerX() = 0;
protected:
std::vector<std::shared_ptr<CollisionPolygon>> collisions;
std::shared_ptr<Texture> texture;
@@ -403,10 +414,11 @@ public:
}
return ret;
}
void renderScene() {
void renderScene(bool clear_scene = true) {
checkKilled();
render_mutex.lock();
SDL_RenderClear(renderer->getRendererPtr());
if(clear_scene)
SDL_RenderClear(renderer->getRendererPtr());
if(background && background->getTexturePtr())
SDL_RenderCopy(renderer->getRendererPtr(), background->getTexturePtr(), NULL, NULL);
for( const auto &x : renderObjects ) {
@@ -627,24 +639,28 @@ public:
rect.y = y * dimension;
rect.w = w * dimension;
rect.h = h * dimension;
x_ = x;
y_ = y;
w_ = w;
h_ = h;
og_x = x_ = x;
og_y = y_ = y;
og_w = w_ = w;
og_h = h_ = h;
}
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(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);
setTexture(img_or_color);
} else {
polygon = std::make_shared<Rect>(0,0,1,1);
polygon->setColor(img_or_color);
polygon->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight());
setColor(img_or_color);
}
}
virtual void setColor(const std::string &color) {
if(texture)
texture.reset();
polygon = std::make_shared<Rect>(0,0,1,1);
polygon->setColor(color);
polygon->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight());
}
virtual void specialAction(int /*UNUSED*/) {};
virtual void render() {
if(texture != NULL && !getHidden())
@@ -665,6 +681,9 @@ public:
auto addy = static_cast<double>(movementSpeed * movementDirection.second)*(static_cast<double>(ticks)/1000);
x_ += addx;
y_ += addy;
custom_move(ticks);
rect.x = x_ * dimension;
rect.y = y_ * dimension;
for( auto &x : collisions ) {
@@ -673,6 +692,7 @@ public:
if(polygon)
polygon->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight());
}
virtual void custom_move(int ticks) {}
virtual std::pair<std::pair<double,double>,std::pair<double,double>> getDoubleRect() {
return {{x_,y_}, {w_,h_}};
}
@@ -708,6 +728,8 @@ public:
return rect.h;
}
virtual void updateSizeAndPosition() {
if(centerx)
actuallyCenterX();
auto dimension = renderer->getSmallerSide();
rect.x = x_ * dimension;
rect.y = y_ * dimension;
@@ -719,11 +741,36 @@ public:
virtual SDL_Rect getRect() {
return rect;
}
virtual void centerX() {
centerx = true;
updateSizeAndPosition();
}
protected:
void actuallyCenterX() {
auto width = renderer->getWidth();
auto height = renderer->getHeight();
if(width > height) {
std::cout << "WIDTH IS LARGER!" << std::endl;
std::cout << "og_x: " << og_x << std::endl;
auto multiplier = static_cast<double>(width)/static_cast<double>(height);
std::cout << "MULTIPLIER: " << multiplier << std::endl;
x_ = og_x * multiplier;
w_ = og_w * multiplier;
std::cout << "X_: " << x_ << std::endl;
} else {
x_ = og_x;
w_ = og_w;
}
}
double og_x;
double og_y;
double og_w;
double og_h;
double x_;
double y_;
double w_;
double h_;
bool centerx = false;
SDL_Rect rect;
};