Add option to show coliders (only rect so far)

This commit is contained in:
zvon 2020-08-21 18:32:15 +02:00
parent 9db93cd72c
commit 77d5f9a8cb
3 changed files with 104 additions and 35 deletions

View File

@ -80,6 +80,7 @@ void addStuff(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) {
stone->addCollision(SDLPP::Rect(0,0,1,1)); stone->addCollision(SDLPP::Rect(0,0,1,1));
stone->setTexture("stone.png"); stone->setTexture("stone.png");
stone->setId(STONE_ID); stone->setId(STONE_ID);
stone->setColiderColor("FF0000");
scene.addObject(stone); scene.addObject(stone);
posx += 0.45; posx += 0.45;
} }
@ -87,6 +88,7 @@ void addStuff(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) {
x->addCollision(SDLPP::Rect(0,0,1,1)); x->addCollision(SDLPP::Rect(0,0,1,1));
x->setTexture("5.png"); x->setTexture("5.png");
x->setId(PLAYER_ID); x->setId(PLAYER_ID);
x->setColiderColor("00FF00");
scene.addObject(x); scene.addObject(x);
player = x; player = x;
auto z = std::make_shared<SDLPP::RectangleRender>(0,2.5,0,0,r); auto z = std::make_shared<SDLPP::RectangleRender>(0,2.5,0,0,r);
@ -94,6 +96,7 @@ void addStuff(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) {
z_col.setInfinite(); z_col.setInfinite();
z->addCollision(z_col); z->addCollision(z_col);
z->setId(DEATH); z->setId(DEATH);
z->setColiderColor("FF00FF");
scene.addObject(z); scene.addObject(z);
} }
@ -133,6 +136,9 @@ void handleKeyDown(SDL_Keycode key, SDLPP::Scene &scene) {
break; break;
case SDLK_s: case SDLK_s:
break; break;
case SDLK_r:
scene.getRenderer().setRenderColiders(!scene.getRenderer().getRenderColiders());
break;
default: default:
scene.setBackground(bgtextures[4]); scene.setBackground(bgtextures[4]);
break; break;
@ -222,6 +228,7 @@ int main() {
SDLPP::init(); SDLPP::init();
SDLPP::Window w("Oh yeah, boi!"); SDLPP::Window w("Oh yeah, boi!");
auto renderer = std::make_shared<SDLPP::Renderer>(w); auto renderer = std::make_shared<SDLPP::Renderer>(w);
renderer->setBlendMode(SDL_BLENDMODE_ADD);
auto main_scene = std::make_shared<SDLPP::Scene>(renderer); auto main_scene = std::make_shared<SDLPP::Scene>(renderer);
bgtextures.push_back(std::make_shared<SDLPP::Texture>(renderer, "1.bmp")); bgtextures.push_back(std::make_shared<SDLPP::Texture>(renderer, "1.bmp"));
bgtextures.push_back(std::make_shared<SDLPP::Texture>(renderer, "2.bmp")); bgtextures.push_back(std::make_shared<SDLPP::Texture>(renderer, "2.bmp"));

View File

@ -107,3 +107,38 @@ bool SDLPP::Circle::colidesWith(const SDLPP::CollisionPolygon &other) const {
int distancesquared = (pointx - centerx)*(pointx - centerx) + (pointy - centery)*(pointy-centery); int distancesquared = (pointx - centerx)*(pointx - centerx) + (pointy - centery)*(pointy-centery);
return distancesquared <= rad*rad; return distancesquared <= rad*rad;
} }
int SDLPP::hex2num(char c) {
if(c <= '9')
return c - '0';
switch(c) {
case 'a':
case 'A':
return 10;
case 'b':
case 'B':
return 11;
case 'c':
case 'C':
return 12;
case 'd':
case 'D':
return 13;
case 'e':
case 'E':
return 14;
default:
return 15;
}
}
std::tuple<int, int, int> SDLPP::getColorsHEX(const std::string &color) {
int red = 0, green = 0, blue = 0;
const char *color_ptr = color.c_str();
if(color_ptr[0] == '#')
color_ptr++;
red = hex2num(color_ptr[0])*16 + hex2num(color_ptr[1]);
green = hex2num(color_ptr[2])*16 + hex2num(color_ptr[3]);
blue = hex2num(color_ptr[4])*16 + hex2num(color_ptr[5]);
return {red, green, blue};
}

View File

@ -10,6 +10,10 @@
namespace SDLPP { namespace SDLPP {
int hex2num(char c);
std::tuple<int, int, int> getColorsHEX(const std::string &color);
class Window { class Window {
public: public:
Window() : Window("SDL Window", 640, 480, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED) { Window() : Window("SDL Window", 640, 480, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED) {
@ -71,8 +75,18 @@ public:
auto dimensions = getDimensions(); auto dimensions = getDimensions();
return dimensions.first > dimensions.second ? dimensions.first : dimensions.second; return dimensions.first > dimensions.second ? dimensions.first : dimensions.second;
} }
void setBlendMode(SDL_BlendMode blendMode) {
SDL_SetRenderDrawBlendMode(renderer, blendMode);
}
void setRenderColiders(bool render) {
render_coliders = render;
}
bool getRenderColiders() {
return render_coliders;
}
private: private:
SDL_Renderer *renderer = NULL; SDL_Renderer *renderer = NULL;
bool render_coliders = false;
}; };
class Texture { class Texture {
@ -103,41 +117,6 @@ public:
return texture; return texture;
} }
private: private:
int hex2num(char c) {
if(c <= '9')
return c - '0';
switch(c) {
case 'a':
case 'A':
return 10;
case 'b':
case 'B':
return 11;
case 'c':
case 'C':
return 12;
case 'd':
case 'D':
return 13;
case 'e':
case 'E':
return 14;
default:
return 15;
}
}
std::tuple<int, int, int> getColorsHEX(const std::string &color) {
int red = 0, green = 0, blue = 0;
const char *color_ptr = color.c_str();
if(color_ptr[0] == '#')
color_ptr++;
red = hex2num(color_ptr[0])*16 + hex2num(color_ptr[1]);
green = hex2num(color_ptr[2])*16 + hex2num(color_ptr[3]);
blue = hex2num(color_ptr[4])*16 + hex2num(color_ptr[5]);
return {red, green, blue};
}
SDL_Texture *texture = NULL; SDL_Texture *texture = NULL;
}; };
@ -162,6 +141,7 @@ public:
position_x = original_x * w + x; position_x = original_x * w + x;
position_y = original_y * h + y; position_y = original_y * h + y;
} }
virtual void render(Renderer &renderer, const std::tuple<int,int,int> &color) = 0;
int getX() const { int getX() const {
return position_x; return position_x;
} }
@ -243,6 +223,15 @@ public:
uint64_t getId() { uint64_t getId() {
return id; return id;
} }
void setHidden(bool hid) {
hidden = hid;
}
bool getHidden() {
return hidden;
}
void setColiderColor(const std::string &color) {
colider_color = getColorsHEX(color);
}
virtual void move(int ticks) = 0; virtual void move(int ticks) = 0;
virtual void updateSizeAndPosition() = 0; virtual void updateSizeAndPosition() = 0;
virtual SDL_Rect getRect() = 0; virtual SDL_Rect getRect() = 0;
@ -254,6 +243,8 @@ protected:
std::pair<int,int> movementDirection; std::pair<int,int> movementDirection;
std::vector<std::shared_ptr<RenderObject>> colidedWith; std::vector<std::shared_ptr<RenderObject>> colidedWith;
uint64_t id; uint64_t id;
bool hidden = false;
std::tuple<int,int,int> colider_color = {0x00, 0xFF, 0xFF};
}; };
class Scene { class Scene {
@ -342,6 +333,9 @@ public:
int getHeight() const { int getHeight() const {
return renderer->getHeight(); return renderer->getHeight();
} }
Renderer &getRenderer() {
return *renderer;
}
private: private:
std::vector<std::shared_ptr<RenderObject>> renderObjects; std::vector<std::shared_ptr<RenderObject>> renderObjects;
std::vector<std::shared_ptr<RenderObject>> collisionObjects; std::vector<std::shared_ptr<RenderObject>> collisionObjects;
@ -377,6 +371,10 @@ public:
virtual void render() { virtual void render() {
if(texture != NULL) if(texture != NULL)
SDL_RenderCopy(renderer->getRendererPtr(), texture->getTexturePtr(), NULL, &rect); SDL_RenderCopy(renderer->getRendererPtr(), texture->getTexturePtr(), NULL, &rect);
if(hasCollisions() && renderer->getRenderColiders()) {
for(const auto &col : getCollisions())
col->render(*renderer, colider_color);
}
} }
virtual void move(int ticks) { virtual void move(int ticks) {
auto dimension = renderer->getSmallerSide(); auto dimension = renderer->getSmallerSide();
@ -504,7 +502,36 @@ public:
pixel_w = w_ * w; pixel_w = w_ * w;
pixel_h = h_ * h; pixel_h = h_ * h;
} }
virtual void render(Renderer &renderer, const std::tuple<int,int,int> &color) override {
auto rect = getRect();
// outline with desired color at 50% opacity
SDL_SetRenderDrawColor(renderer.getRendererPtr(), std::get<0>(color), std::get<1>(color), std::get<2>(color), 0x80);
SDL_RenderDrawRect(renderer.getRendererPtr(), &rect);
// fill with desired color at 25% opacity
SDL_SetRenderDrawColor(renderer.getRendererPtr(), std::get<0>(color), std::get<1>(color), std::get<2>(color), 0x40);
SDL_RenderFillRect(renderer.getRendererPtr(), &rect);
}
private: private:
SDL_Rect getRect() {
if( !isInfinite() )
return {leftmost(), topmost(), pixel_w, pixel_h};
SDL_Rect r = {0,0,0,0};
if((r.x = leftmost()) == -1)
r.x = 0;
if((r.y = topmost()) == -1)
r.y = 0;
if(rightmost() == -1)
r.w = std::numeric_limits<int>::max();
else
r.w = pixel_w;
if(bottommost() == -1)
r.h = std::numeric_limits<int>::max();
else
r.h = pixel_h;
return r;
}
double w_; double w_;
double h_; double h_;
int pixel_w; int pixel_w;