Changes done during tetris development
This commit is contained in:
parent
27274c7242
commit
6acfff3e8f
7
main.cpp
7
main.cpp
@ -72,8 +72,8 @@ public:
|
|||||||
}
|
}
|
||||||
if(jump_ < 0 || jump_ > jump_speed)
|
if(jump_ < 0 || jump_ > jump_speed)
|
||||||
jump_ = 0;
|
jump_ = 0;
|
||||||
y_ += grav * time_portion;
|
og_y += grav * time_portion;
|
||||||
y_ -= jump_ * time_portion;
|
og_y -= jump_ * time_portion;
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
double max_gravity = 1.0;
|
double max_gravity = 1.0;
|
||||||
@ -147,7 +147,8 @@ void addStuff(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) {
|
|||||||
scene.addObject(z);
|
scene.addObject(z);
|
||||||
auto y = std::make_shared<SDLPP::RectangleRender>(0, 0, 0.2, 0.1, r);
|
auto y = std::make_shared<SDLPP::RectangleRender>(0, 0, 0.2, 0.1, r);
|
||||||
y->setTexture(*font, "DEMO", "#FFFFFF", "#000000", 5);
|
y->setTexture(*font, "DEMO", "#FFFFFF", "#000000", 5);
|
||||||
y->setId(0);
|
y->setPermanent(true);
|
||||||
|
y->setId(123);
|
||||||
scene.addObject(y);
|
scene.addObject(y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,10 +58,10 @@ bool infinityIntersection(const SDLPP::CollisionPolygon &infinite, const SDLPP::
|
|||||||
int iright = infinite.rightmost();
|
int iright = infinite.rightmost();
|
||||||
int itop = infinite.topmost();
|
int itop = infinite.topmost();
|
||||||
int ibottom = infinite.bottommost();
|
int ibottom = infinite.bottommost();
|
||||||
bool ret = ileft != -1 && ileft <= other.rightmost();
|
bool ret = ileft != -1 && ileft <= other.rightmost() && ileft >= other.leftmost();
|
||||||
ret |= iright != -1 && iright >= other.leftmost();
|
ret |= iright != -1 && iright >= other.leftmost() && iright <= other.rightmost();
|
||||||
ret |= itop != -1 && itop <= other.bottommost();
|
ret |= itop != -1 && itop <= other.bottommost() && itop >= other.topmost();
|
||||||
ret |= ibottom != -1 && ibottom >= other.topmost();
|
ret |= ibottom != -1 && ibottom >= other.topmost() && ibottom <= other.bottommost();
|
||||||
ret |= ileft == -1 && iright == -1 && itop == -1 && ibottom == -1;
|
ret |= ileft == -1 && iright == -1 && itop == -1 && ibottom == -1;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
151
sdlpp.hpp
151
sdlpp.hpp
@ -10,6 +10,7 @@
|
|||||||
#include <limits>
|
#include <limits>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
#include <unordered_set>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace SDLPP {
|
namespace SDLPP {
|
||||||
@ -223,13 +224,17 @@ public:
|
|||||||
void setColor(const std::string &color) {
|
void setColor(const std::string &color) {
|
||||||
sdl_color = getSDLColorHEX(color);
|
sdl_color = getSDLColorHEX(color);
|
||||||
}
|
}
|
||||||
|
void setOutlineColor(const std::string &color) {
|
||||||
|
sdl_outline = getSDLColorHEX(color);
|
||||||
|
}
|
||||||
protected:
|
protected:
|
||||||
double original_x;
|
double original_x;
|
||||||
double original_y;
|
double original_y;
|
||||||
int position_x;
|
int position_x;
|
||||||
int position_y;
|
int position_y;
|
||||||
bool infinite = false;
|
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;
|
class Scene;
|
||||||
@ -250,6 +255,7 @@ public:
|
|||||||
virtual void specialAction(int code) = 0;
|
virtual void specialAction(int code) = 0;
|
||||||
virtual std::pair<std::pair<double,double>,std::pair<double,double>> getDoubleRect() = 0;
|
virtual std::pair<std::pair<double,double>,std::pair<double,double>> getDoubleRect() = 0;
|
||||||
virtual void setPos(double x, double y) = 0;
|
virtual void setPos(double x, double y) = 0;
|
||||||
|
virtual std::pair<double, double> getPos() = 0;
|
||||||
bool colidesWith(const RenderObject &other) const {
|
bool colidesWith(const RenderObject &other) const {
|
||||||
if(!hasCollisions() || !other.hasCollisions() || getHidden() || other.getHidden()) {
|
if(!hasCollisions() || !other.hasCollisions() || getHidden() || other.getHidden()) {
|
||||||
return false;
|
return false;
|
||||||
@ -273,16 +279,17 @@ public:
|
|||||||
const std::vector<std::shared_ptr<CollisionPolygon>> &getCollisions() const {
|
const std::vector<std::shared_ptr<CollisionPolygon>> &getCollisions() const {
|
||||||
return collisions;
|
return collisions;
|
||||||
}
|
}
|
||||||
void setTexture(std::shared_ptr<Texture> &t) {
|
virtual void setTexture(std::shared_ptr<Texture> &t) {
|
||||||
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);
|
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);
|
texture = std::make_shared<Texture>(renderer, font, text, color, outline_color, outline_size);
|
||||||
}
|
}
|
||||||
virtual void setColor(const std::string &color) = 0;
|
virtual void setColor(const std::string &color) = 0;
|
||||||
|
virtual void setOutlineColor(const std::string &color) = 0;
|
||||||
virtual void unsetTexture() {
|
virtual void unsetTexture() {
|
||||||
texture.reset();
|
texture.reset();
|
||||||
}
|
}
|
||||||
@ -397,6 +404,9 @@ public:
|
|||||||
std::shared_ptr<RenderObject> getObject(int index) {
|
std::shared_ptr<RenderObject> getObject(int index) {
|
||||||
return renderObjects[index];
|
return renderObjects[index];
|
||||||
}
|
}
|
||||||
|
std::vector<std::shared_ptr<RenderObject>> getObjects() {
|
||||||
|
return renderObjects;
|
||||||
|
}
|
||||||
void movement() {
|
void movement() {
|
||||||
checkKilled();
|
checkKilled();
|
||||||
render_mutex.lock();
|
render_mutex.lock();
|
||||||
@ -418,6 +428,17 @@ public:
|
|||||||
}
|
}
|
||||||
return ret;
|
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) {
|
void renderScene(bool clear_scene = true) {
|
||||||
checkKilled();
|
checkKilled();
|
||||||
render_mutex.lock();
|
render_mutex.lock();
|
||||||
@ -454,6 +475,8 @@ public:
|
|||||||
checkKilled();
|
checkKilled();
|
||||||
render_mutex.lock();
|
render_mutex.lock();
|
||||||
for( auto &obj : renderObjects ) {
|
for( auto &obj : renderObjects ) {
|
||||||
|
if(obj->getPermanent())
|
||||||
|
continue;
|
||||||
auto curPos = obj->getDoubleRect();
|
auto curPos = obj->getDoubleRect();
|
||||||
obj->setPos( curPos.first.first + x, curPos.first.second + y );
|
obj->setPos( curPos.first.first + x, curPos.first.second + y );
|
||||||
}
|
}
|
||||||
@ -546,6 +569,8 @@ public:
|
|||||||
auto rect = getRect();
|
auto rect = getRect();
|
||||||
SDL_SetRenderDrawColor(renderer.getRendererPtr(), sdl_color.r, sdl_color.g, sdl_color.b, sdl_color.a);
|
SDL_SetRenderDrawColor(renderer.getRendererPtr(), sdl_color.r, sdl_color.g, sdl_color.b, sdl_color.a);
|
||||||
SDL_RenderFillRect(renderer.getRendererPtr(), &rect);
|
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:
|
private:
|
||||||
SDL_Rect getRect() {
|
SDL_Rect getRect() {
|
||||||
@ -623,6 +648,9 @@ public:
|
|||||||
auto allowed_rad = sqrt(radsq - xdist);
|
auto allowed_rad = sqrt(radsq - xdist);
|
||||||
SDL_SetRenderDrawColor(renderer.getRendererPtr(), sdl_color.r, sdl_color.g, sdl_color.b, sdl_color.a);
|
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_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:
|
private:
|
||||||
@ -638,15 +666,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 dimension = renderer->getSmallerSide();
|
|
||||||
rect.x = x * dimension;
|
|
||||||
rect.y = y * dimension;
|
|
||||||
rect.w = w * dimension;
|
|
||||||
rect.h = h * dimension;
|
|
||||||
og_x = x_ = x;
|
og_x = x_ = x;
|
||||||
og_y = y_ = y;
|
og_y = y_ = y;
|
||||||
og_w = w_ = w;
|
og_w = w_ = w;
|
||||||
og_h = h_ = h;
|
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) {
|
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);
|
setTexture(t);
|
||||||
@ -658,80 +682,86 @@ public:
|
|||||||
setColor(img_or_color);
|
setColor(img_or_color);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
virtual void setColor(const std::string &color) {
|
virtual void setColor(const std::string &color) override {
|
||||||
polygon = std::make_shared<Rect>(0,0,1,1);
|
if(!polygon) {
|
||||||
polygon->setColor(color);
|
polygon = std::make_shared<Rect>(0,0,1,1);
|
||||||
polygon->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight());
|
polygon->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight());
|
||||||
}
|
}
|
||||||
virtual void specialAction(int /*UNUSED*/) {};
|
polygon->setColor(color);
|
||||||
virtual void render() {
|
}
|
||||||
if(polygon) {
|
virtual void setOutlineColor(const std::string &color) override {
|
||||||
polygon->render(*renderer);
|
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()) {
|
if(hasCollisions() && renderer->getRenderColiders() && !getHidden()) {
|
||||||
for(const auto &col : getCollisions())
|
for(const auto &col : getCollisions())
|
||||||
col->render(*renderer, colider_color);
|
col->render(*renderer, colider_color);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
virtual void move(int ticks) {
|
virtual void move(int ticks) override {
|
||||||
if(permanent)
|
if(permanent)
|
||||||
return;
|
return;
|
||||||
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;
|
if(std::isnan(addx) || std::isnan(addy))
|
||||||
y_ += addy;
|
return;
|
||||||
|
|
||||||
|
og_x += addx;
|
||||||
|
og_y += addy;
|
||||||
|
|
||||||
custom_move(ticks);
|
custom_move(ticks);
|
||||||
|
|
||||||
rect.x = x_ * dimension;
|
updateSizeAndPosition();
|
||||||
rect.y = y_ * dimension;
|
|
||||||
for( auto &x : collisions ) {
|
|
||||||
x->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight());
|
|
||||||
}
|
|
||||||
if(polygon)
|
|
||||||
polygon->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight());
|
|
||||||
}
|
}
|
||||||
virtual void custom_move(int /*UNUSED*/) {}
|
virtual void custom_move(int /*UNUSED*/) override {}
|
||||||
virtual std::pair<std::pair<double,double>,std::pair<double,double>> getDoubleRect() {
|
virtual std::pair<std::pair<double,double>,std::pair<double,double>> getDoubleRect() override {
|
||||||
return {{x_,y_}, {w_,h_}};
|
return {{og_x,og_y}, {og_w,og_h}};
|
||||||
}
|
}
|
||||||
virtual void setPos(double x, double y) {
|
virtual void setPos(double x, double y) override {
|
||||||
auto dimension = renderer->getSmallerSide();
|
og_x = x;
|
||||||
x_ = x;
|
og_y = y;
|
||||||
y_ = y;
|
updateSizeAndPosition();
|
||||||
rect.x = x_ * dimension;
|
|
||||||
rect.y = y_ * dimension;
|
|
||||||
}
|
}
|
||||||
virtual int leftmost() {
|
virtual std::pair<double, double> getPos() override {
|
||||||
|
return {og_x, og_y};
|
||||||
|
}
|
||||||
|
virtual int leftmost() override {
|
||||||
return rect.x;
|
return rect.x;
|
||||||
}
|
}
|
||||||
virtual int topmost() {
|
virtual int topmost() override {
|
||||||
return rect.y;
|
return rect.y;
|
||||||
}
|
}
|
||||||
virtual int rightmost() {
|
virtual int rightmost() override {
|
||||||
return rect.x + rect.w;
|
return rect.x + rect.w;
|
||||||
}
|
}
|
||||||
virtual int bottommost() {
|
virtual int bottommost() override {
|
||||||
return rect.y + rect.h;
|
return rect.y + rect.h;
|
||||||
}
|
}
|
||||||
virtual int collisionPushX() {
|
virtual int collisionPushX() override {
|
||||||
return rect.x;
|
return rect.x;
|
||||||
}
|
}
|
||||||
virtual int collisionPushY() {
|
virtual int collisionPushY() override {
|
||||||
return rect.y;
|
return rect.y;
|
||||||
}
|
}
|
||||||
virtual int collisionWidth() {
|
virtual int collisionWidth() override {
|
||||||
return rect.w;
|
return rect.w;
|
||||||
}
|
}
|
||||||
virtual int collisionHeight() {
|
virtual int collisionHeight() override {
|
||||||
return rect.h;
|
return rect.h;
|
||||||
}
|
}
|
||||||
virtual void updateSizeAndPosition() {
|
virtual void updateSizeAndPosition() override {
|
||||||
if(centerx)
|
updateXY();
|
||||||
actuallyCenterX();
|
|
||||||
auto dimension = renderer->getSmallerSide();
|
auto dimension = renderer->getSmallerSide();
|
||||||
rect.x = x_ * dimension;
|
rect.x = x_ * dimension;
|
||||||
rect.y = y_ * dimension;
|
rect.y = y_ * dimension;
|
||||||
@ -739,26 +769,33 @@ public:
|
|||||||
rect.h = h_ * dimension;
|
rect.h = h_ * dimension;
|
||||||
if(polygon)
|
if(polygon)
|
||||||
polygon->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight());
|
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;
|
return rect;
|
||||||
}
|
}
|
||||||
virtual void centerX() {
|
virtual void centerX() override {
|
||||||
centerx = true;
|
centerx = true;
|
||||||
updateSizeAndPosition();
|
updateSizeAndPosition();
|
||||||
}
|
}
|
||||||
protected:
|
protected:
|
||||||
void actuallyCenterX() {
|
void updateXY() {
|
||||||
|
if(!centerx) {
|
||||||
|
x_ = og_x;
|
||||||
|
y_ = og_y;
|
||||||
|
return;
|
||||||
|
}
|
||||||
auto width = renderer->getWidth();
|
auto width = renderer->getWidth();
|
||||||
auto height = renderer->getHeight();
|
auto height = renderer->getHeight();
|
||||||
if(width > height) {
|
if(width > height) {
|
||||||
auto multiplier = static_cast<double>(width)/static_cast<double>(height);
|
auto multiplier = static_cast<double>(width)/static_cast<double>(height);
|
||||||
x_ = og_x * multiplier;
|
x_ = og_x + static_cast<double>(multiplier - 1)/2;
|
||||||
w_ = og_w * multiplier;
|
|
||||||
} else {
|
} else {
|
||||||
x_ = og_x;
|
x_ = og_x;
|
||||||
w_ = og_w;
|
|
||||||
}
|
}
|
||||||
|
y_ = og_y;
|
||||||
}
|
}
|
||||||
double og_x;
|
double og_x;
|
||||||
double og_y;
|
double og_y;
|
||||||
|
Loading…
Reference in New Issue
Block a user