diff --git a/main.cpp b/main.cpp index 45b2ff1..3757e9e 100644 --- a/main.cpp +++ b/main.cpp @@ -80,6 +80,7 @@ void addStuff(SDLPP::Scene &scene, std::shared_ptr &r) { stone->addCollision(SDLPP::Rect(0,0,1,1)); stone->setTexture("stone.png"); stone->setId(STONE_ID); + stone->setColiderColor("FF0000"); scene.addObject(stone); posx += 0.45; } @@ -87,6 +88,7 @@ void addStuff(SDLPP::Scene &scene, std::shared_ptr &r) { x->addCollision(SDLPP::Rect(0,0,1,1)); x->setTexture("5.png"); x->setId(PLAYER_ID); + x->setColiderColor("00FF00"); scene.addObject(x); player = x; auto z = std::make_shared(0,2.5,0,0,r); @@ -94,6 +96,7 @@ void addStuff(SDLPP::Scene &scene, std::shared_ptr &r) { z_col.setInfinite(); z->addCollision(z_col); z->setId(DEATH); + z->setColiderColor("FF00FF"); scene.addObject(z); } @@ -133,6 +136,9 @@ void handleKeyDown(SDL_Keycode key, SDLPP::Scene &scene) { break; case SDLK_s: break; + case SDLK_r: + scene.getRenderer().setRenderColiders(!scene.getRenderer().getRenderColiders()); + break; default: scene.setBackground(bgtextures[4]); break; @@ -222,6 +228,7 @@ int main() { SDLPP::init(); SDLPP::Window w("Oh yeah, boi!"); auto renderer = std::make_shared(w); + renderer->setBlendMode(SDL_BLENDMODE_ADD); auto main_scene = std::make_shared(renderer); bgtextures.push_back(std::make_shared(renderer, "1.bmp")); bgtextures.push_back(std::make_shared(renderer, "2.bmp")); diff --git a/sdlpp.cpp b/sdlpp.cpp index 0e844bd..35e0eed 100644 --- a/sdlpp.cpp +++ b/sdlpp.cpp @@ -107,3 +107,38 @@ bool SDLPP::Circle::colidesWith(const SDLPP::CollisionPolygon &other) const { int distancesquared = (pointx - centerx)*(pointx - centerx) + (pointy - centery)*(pointy-centery); 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 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}; +} diff --git a/sdlpp.hpp b/sdlpp.hpp index 08e787c..e1e8a1e 100644 --- a/sdlpp.hpp +++ b/sdlpp.hpp @@ -10,6 +10,10 @@ namespace SDLPP { +int hex2num(char c); + +std::tuple getColorsHEX(const std::string &color); + class Window { public: Window() : Window("SDL Window", 640, 480, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED) { @@ -71,8 +75,18 @@ public: auto dimensions = getDimensions(); 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: SDL_Renderer *renderer = NULL; + bool render_coliders = false; }; class Texture { @@ -103,41 +117,6 @@ public: return texture; } 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 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; }; @@ -162,6 +141,7 @@ public: position_x = original_x * w + x; position_y = original_y * h + y; } + virtual void render(Renderer &renderer, const std::tuple &color) = 0; int getX() const { return position_x; } @@ -243,6 +223,15 @@ public: uint64_t getId() { 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 updateSizeAndPosition() = 0; virtual SDL_Rect getRect() = 0; @@ -254,6 +243,8 @@ protected: std::pair movementDirection; std::vector> colidedWith; uint64_t id; + bool hidden = false; + std::tuple colider_color = {0x00, 0xFF, 0xFF}; }; class Scene { @@ -342,6 +333,9 @@ public: int getHeight() const { return renderer->getHeight(); } + Renderer &getRenderer() { + return *renderer; + } private: std::vector> renderObjects; std::vector> collisionObjects; @@ -377,6 +371,10 @@ public: virtual void render() { if(texture != NULL) 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) { auto dimension = renderer->getSmallerSide(); @@ -504,7 +502,36 @@ public: pixel_w = w_ * w; pixel_h = h_ * h; } + virtual void render(Renderer &renderer, const std::tuple &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: + 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::max(); + else + r.w = pixel_w; + if(bottommost() == -1) + r.h = std::numeric_limits::max(); + else + r.h = pixel_h; + return r; + } + double w_; double h_; int pixel_w;