Changes done during tetris development

This commit is contained in:
zvon 2020-08-23 01:08:10 +02:00
parent 27274c7242
commit 6acfff3e8f
3 changed files with 102 additions and 64 deletions

View File

@ -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<SDLPP::Renderer> &r) {
scene.addObject(z);
auto y = std::make_shared<SDLPP::RectangleRender>(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);
}

View File

@ -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;
}

151
sdlpp.hpp
View File

@ -10,6 +10,7 @@
#include <limits>
#include <memory>
#include <mutex>
#include <unordered_set>
#include <vector>
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<double,double>,std::pair<double,double>> getDoubleRect() = 0;
virtual void setPos(double x, double y) = 0;
virtual std::pair<double, double> 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<std::shared_ptr<CollisionPolygon>> &getCollisions() const {
return collisions;
}
void setTexture(std::shared_ptr<Texture> &t) {
virtual void setTexture(std::shared_ptr<Texture> &t) {
texture = t;
}
void setTexture(const std::string &img_path) {
virtual void setTexture(const std::string &img_path) {
texture = std::make_shared<Texture>(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<Texture>(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<RenderObject> getObject(int index) {
return renderObjects[index];
}
std::vector<std::shared_ptr<RenderObject>> getObjects() {
return renderObjects;
}
void movement() {
checkKilled();
render_mutex.lock();
@ -418,6 +428,17 @@ public:
}
return ret;
}
std::vector<std::shared_ptr<RenderObject>> getCollisions( RenderObject &r, const std::unordered_set<int> &objectIDs ) {
if(r.getHidden())
return {};
std::vector<std::shared_ptr<RenderObject>> 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<Renderer> &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<Renderer> &r, std::shared_ptr<Texture> &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<Rect>(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<Rect>(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<Rect>(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<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;
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<double,double>,std::pair<double,double>> getDoubleRect() {
return {{x_,y_}, {w_,h_}};
virtual void custom_move(int /*UNUSED*/) override {}
virtual std::pair<std::pair<double,double>,std::pair<double,double>> 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<double, double> 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<double>(width)/static_cast<double>(height);
x_ = og_x * multiplier;
w_ = og_w * multiplier;
x_ = og_x + static_cast<double>(multiplier - 1)/2;
} else {
x_ = og_x;
w_ = og_w;
}
y_ = og_y;
}
double og_x;
double og_y;