Add text and rendering of color rectangles

This commit is contained in:
zvon 2020-08-22 13:17:56 +02:00
parent 618786f885
commit 329ef7f40f
4 changed files with 283 additions and 148 deletions

View File

@ -1,7 +1,7 @@
CXX ?= g++ CXX ?= g++
CFLAGS ?= -O2 -Wall -Wextra -g CFLAGS ?= -O2 -Wall -Wextra -g
PREFIX ?= /usr/local/bin PREFIX ?= /usr/local/bin
LDFLAGS ?= -lSDL2 -lSDL2_image -lSDL2_gfx -pthread LDFLAGS ?= -lSDL2 -lSDL2_image -lSDL2_gfx -lSDL2_ttf -pthread
.PHONY: default .PHONY: default
default: demo default: demo

View File

@ -10,6 +10,7 @@
#define DESTROYABLE_DESTROY 0x00000001 #define DESTROYABLE_DESTROY 0x00000001
bool pause = false; bool pause = false;
std::shared_ptr<SDLPP::Font> font;
class Player : public SDLPP::RectangleRender { class Player : public SDLPP::RectangleRender {
public: public:
@ -125,6 +126,10 @@ std::vector<std::shared_ptr<SDLPP::Texture>> bgtextures;
bool quit = false; bool quit = false;
void addStuff(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) { void addStuff(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) {
auto bg = std::make_shared<SDLPP::RectangleRender>(0,0,10,10,r,"#ebdbb2FF", true);
bg->setId(123);
bg->setPermanent(true);
scene.addObject(bg);
std::shared_ptr<Destroyable> stone; std::shared_ptr<Destroyable> stone;
double posx = 0; double posx = 0;
while(posx < 3) { while(posx < 3) {
@ -153,6 +158,10 @@ void addStuff(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) {
z->setId(DEATH); z->setId(DEATH);
z->setColiderColor("FF00FF"); z->setColiderColor("FF00FF");
scene.addObject(z); scene.addObject(z);
auto y = std::make_shared<SDLPP::RectangleRender>(0, 0, 0.2, 0.1, r);
y->setTexture(*font, "THIS IS A TEST", "#FFFFFF", "#000000", 5);
y->setId(0);
scene.addObject(y);
} }
void quitGame() { void quitGame() {
@ -162,20 +171,10 @@ void quitGame() {
void handleKeyDown(SDL_Keycode key, SDLPP::Scene &scene) { void handleKeyDown(SDL_Keycode key, SDLPP::Scene &scene) {
switch(key) { switch(key) {
case SDLK_UP:
scene.setBackground(bgtextures[0]);
break;
case SDLK_DOWN:
scene.setBackground(bgtextures[1]);
break;
case SDLK_RIGHT:
scene.setBackground(bgtextures[2]);
break;
case SDLK_LEFT:
scene.setBackground(bgtextures[3]);
break;
case SDLK_ESCAPE: case SDLK_ESCAPE:
quitGame(); if(pause)
scene.setPrevTicks(SDL_GetTicks());
pause = !pause;
break; break;
case SDLK_a: case SDLK_a:
player->addMovement(-1,0); player->addMovement(-1,0);
@ -183,6 +182,7 @@ void handleKeyDown(SDL_Keycode key, SDLPP::Scene &scene) {
case SDLK_d: case SDLK_d:
player->addMovement(1,0); player->addMovement(1,0);
break; break;
case SDLK_SPACE:
case SDLK_w: case SDLK_w:
if(!player->isJumping() && !pause) { if(!player->isJumping() && !pause) {
player->setLastStand(); player->setLastStand();
@ -193,14 +193,7 @@ void handleKeyDown(SDL_Keycode key, SDLPP::Scene &scene) {
break; break;
case SDLK_r: case SDLK_r:
scene.getRenderer().setRenderColiders(!scene.getRenderer().getRenderColiders()); scene.getRenderer().setRenderColiders(!scene.getRenderer().getRenderColiders());
break;
case SDLK_SPACE:
if(pause)
scene.setPrevTicks(SDL_GetTicks());
pause = !pause;
break;
default: default:
scene.setBackground(bgtextures[4]);
break; break;
} }
} }
@ -290,14 +283,14 @@ 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); renderer->setBlendMode(SDL_BLENDMODE_BLEND);
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"));
bgtextures.push_back(std::make_shared<SDLPP::Texture>(renderer, "3.png")); bgtextures.push_back(std::make_shared<SDLPP::Texture>(renderer, "3.png"));
bgtextures.push_back(std::make_shared<SDLPP::Texture>(renderer, "4.png")); bgtextures.push_back(std::make_shared<SDLPP::Texture>(renderer, "4.png"));
bgtextures.push_back(std::make_shared<SDLPP::Texture>(renderer, "test.bmp")); bgtextures.push_back(std::make_shared<SDLPP::Texture>(renderer, "test.bmp"));
main_scene->setBackground(bgtextures[0]); font = std::make_shared<SDLPP::Font>("testfont.ttf", 24);
addStuff(*main_scene, renderer); addStuff(*main_scene, renderer);
player->setMovementSpeed(0.3); player->setMovementSpeed(0.3);
player->enableGravity(); player->enableGravity();

View File

@ -12,6 +12,10 @@ bool SDLPP::init() {
std::cerr << "SDL_image could not initialize! SDL_image Error: " << IMG_GetError() << std::endl; std::cerr << "SDL_image could not initialize! SDL_image Error: " << IMG_GetError() << std::endl;
return false; return false;
} }
if( TTF_Init() == -1 ) {
std::cerr << "SDL_ttf could not initialize! SDL_ttf Error: " << TTF_GetError() << std::endl;
return false;
}
return true; return true;
} }
@ -132,13 +136,33 @@ int SDLPP::hex2num(char c) {
} }
} }
std::tuple<int, int, int> SDLPP::getColorsHEX(const std::string &color) { std::tuple<int, int, int, int> SDLPP::getColorsHEX(const std::string &color) {
int red = 0, green = 0, blue = 0; int red = 0, green = 0, blue = 0, alpha = 255;
const char *color_ptr = color.c_str(); const char *color_ptr = color.c_str();
if(color_ptr[0] == '#') if(color_ptr[0] == '#')
color_ptr++; color_ptr++;
red = hex2num(color_ptr[0])*16 + hex2num(color_ptr[1]); red = hex2num(color_ptr[0])*16 + hex2num(color_ptr[1]);
green = hex2num(color_ptr[2])*16 + hex2num(color_ptr[3]); green = hex2num(color_ptr[2])*16 + hex2num(color_ptr[3]);
blue = hex2num(color_ptr[4])*16 + hex2num(color_ptr[5]); blue = hex2num(color_ptr[4])*16 + hex2num(color_ptr[5]);
return {red, green, blue}; if( color_ptr[6] != '\0' )
alpha = hex2num(color_ptr[6])*16 + hex2num(color_ptr[7]);
return {red, green, blue, alpha};
}
SDL_Color SDLPP::getSDLColorTuple(const std::tuple<int, int, int, int> &tuple) {
SDL_Color ret_color{};
ret_color.r = std::get<0>(tuple);
ret_color.g = std::get<1>(tuple);
ret_color.b = std::get<2>(tuple);
ret_color.a = std::get<3>(tuple);
return ret_color;
}
SDL_Color SDLPP::getSDLColorHEX(const std::string &color) {
auto color_tuple = SDLPP::getColorsHEX(color);
return getSDLColorTuple(color_tuple);
}
std::tuple<int, int, int, int> SDLPP::getColorsSDLColor(const SDL_Color &color) {
return {color.r, color.g, color.b, color.a};
} }

362
sdlpp.hpp
View File

@ -3,6 +3,7 @@
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_image.h> #include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <algorithm> #include <algorithm>
#include <cmath> #include <cmath>
#include <iostream> #include <iostream>
@ -15,7 +16,10 @@ namespace SDLPP {
int hex2num(char c); int hex2num(char c);
std::tuple<int, int, int> getColorsHEX(const std::string &color); std::tuple<int, int, int, int> getColorsHEX(const std::string &color);
SDL_Color getSDLColorHEX(const std::string &color);
std::tuple<int, int, int, int> getColorsSDLColor(const SDL_Color &color);
SDL_Color getSDLColorTuple(const std::tuple<int, int, int, int> &tuple);
class Window { class Window {
public: public:
@ -92,6 +96,37 @@ private:
bool render_coliders = false; bool render_coliders = false;
}; };
class Font {
public:
Font() = delete;
Font(const std::string &font, int size) {
font_ptr = TTF_OpenFont(font.c_str(), size);
}
~Font() {
TTF_CloseFont( font_ptr );
}
const TTF_Font *getFont() const {
return font_ptr;
}
TTF_Font *getFont() {
return font_ptr;
}
void setOutline(int size) {
TTF_SetFontOutline(font_ptr, size);
}
int getOutline() {
return TTF_GetFontOutline(font_ptr);
}
void setStyle(int style) {
TTF_SetFontStyle(font_ptr, style);
}
void setHinting(int hinting) {
TTF_SetFontHinting(font_ptr, hinting);
}
private:
TTF_Font *font_ptr;
};
class Texture { class Texture {
public: public:
Texture() = delete; Texture() = delete;
@ -106,12 +141,37 @@ public:
auto colors = getColorsHEX(color_key); auto colors = getColorsHEX(color_key);
SDL_SetColorKey(surface, SDL_TRUE, SDL_MapRGB(surface->format, std::get<0>(colors), std::get<1>(colors), std::get<2>(colors))); SDL_SetColorKey(surface, SDL_TRUE, SDL_MapRGB(surface->format, std::get<0>(colors), std::get<1>(colors), std::get<2>(colors)));
} }
texture = SDL_CreateTextureFromSurface(renderer->getRendererPtr(), surface); setTextureFromSurface(renderer, surface);
if( texture == NULL ) { }
std::cerr << "Unable to create texture from '" << img_path << "'! SDL Error: " << SDL_GetError() << std::endl; Texture(std::shared_ptr<Renderer> &renderer, Font &font, const std::string &text, const std::string &color = "FFFFFF", const std::string &outline_color = "000000", const int outline_size = -1) {
throw "Texture error"; if(outline_size != -1) {
font.setOutline(outline_size);
} }
SDL_FreeSurface(surface); int og_outline = 0;
SDL_Surface *bg_surface = NULL;
if((og_outline = font.getOutline()) != 0) {
bg_surface = TTF_RenderUTF8_Blended( font.getFont(), text.c_str(), getSDLColorHEX(outline_color) );
if( bg_surface == NULL ) {
std::cerr << "Unable to render text '" << text << "': TTF Error: " << TTF_GetError() << std::endl;
throw "TTF_RenderUTF8_Shaded error";
}
font.setOutline(0);
}
SDL_Surface *surface = TTF_RenderUTF8_Blended( font.getFont(), text.c_str(), getSDLColorHEX(color) );
if( surface == NULL ) {
std::cerr << "Unable to render text '" << text << "': TTF Error: " << TTF_GetError() << std::endl;
throw "TTF_RenderUTF8_Shaded error";
}
if(og_outline != 0) {
SDL_Rect rect = {og_outline, og_outline, surface->w, surface->h};
SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
SDL_BlitSurface(surface, NULL, bg_surface, &rect);
SDL_FreeSurface(surface);
surface = bg_surface;
bg_surface = NULL;
font.setOutline(og_outline);
}
setTextureFromSurface(renderer, surface);
} }
~Texture() { ~Texture() {
SDL_DestroyTexture(texture); SDL_DestroyTexture(texture);
@ -120,6 +180,14 @@ public:
return texture; return texture;
} }
private: private:
void setTextureFromSurface(std::shared_ptr<Renderer> &renderer, SDL_Surface *surface) {
texture = SDL_CreateTextureFromSurface(renderer->getRendererPtr(), surface);
if( texture == NULL ) {
std::cerr << "Unable to create texture from surface! SDL Error: " << SDL_GetError() << std::endl;
throw "Texture error";
}
SDL_FreeSurface(surface);
}
SDL_Texture *texture = NULL; SDL_Texture *texture = NULL;
}; };
@ -144,19 +212,24 @@ 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; virtual void render(Renderer &renderer, const std::tuple<int,int,int,int> &color) = 0;
virtual void render(Renderer &renderer) = 0;
int getX() const { int getX() const {
return position_x; return position_x;
} }
int getY() const { int getY() const {
return position_y; return position_y;
} }
void setColor(const std::string &color) {
sdl_color = 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;
}; };
class Scene; class Scene;
@ -206,6 +279,9 @@ public:
void setTexture(const std::string &img_path) { 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) {
texture = std::make_shared<Texture>(renderer, font, text, color, outline_color, outline_size);
}
// per second, relative to window width // per second, relative to window width
void setMovementSpeed(double speed) { void setMovementSpeed(double speed) {
movementSpeed = speed; movementSpeed = speed;
@ -248,18 +324,26 @@ public:
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;
void setPermanent(bool perm) {
permanent = perm;
}
bool getPermanent() const {
return permanent;
}
protected: protected:
std::vector<std::shared_ptr<CollisionPolygon>> collisions; std::vector<std::shared_ptr<CollisionPolygon>> collisions;
std::shared_ptr<Texture> texture; std::shared_ptr<Texture> texture;
std::shared_ptr<Renderer> renderer; std::shared_ptr<Renderer> renderer;
std::shared_ptr<CollisionPolygon> polygon;
double movementSpeed; double movementSpeed;
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; bool hidden = false;
bool kill = false; bool kill = false;
std::tuple<int,int,int> colider_color = {0x00, 0xFF, 0xFF}; std::tuple<int,int,int,int> colider_color = {0x00, 0xFF, 0xFF, 0xFF};
uint64_t scene_id; uint64_t scene_id;
bool permanent = false;
private: private:
void setSceneID(int id) { void setSceneID(int id) {
scene_id = id; scene_id = id;
@ -323,7 +407,8 @@ public:
checkKilled(); checkKilled();
render_mutex.lock(); render_mutex.lock();
SDL_RenderClear(renderer->getRendererPtr()); SDL_RenderClear(renderer->getRendererPtr());
SDL_RenderCopy(renderer->getRendererPtr(), background->getTexturePtr(), NULL, NULL); if(background && background->getTexturePtr())
SDL_RenderCopy(renderer->getRendererPtr(), background->getTexturePtr(), NULL, NULL);
for( const auto &x : renderObjects ) { for( const auto &x : renderObjects ) {
x->render(); x->render();
} }
@ -405,6 +490,133 @@ private:
std::mutex render_mutex; std::mutex render_mutex;
}; };
class Rect : public CollisionPolygon {
public:
Rect(double x, double y, double w, double h) : CollisionPolygon(x, y) {
w_ = w;
h_ = h;
}
virtual ~Rect() {}
virtual bool colidesWith(const CollisionPolygon &other) const override;
virtual bool isCircle() const override { return false; }
virtual int topmost() const override {
return (!isInfinite() || original_y != -1) * getY() + isInfinite() * -1;
}
virtual int bottommost() const override {
return (!isInfinite() || h_ != -1) * (getY() + pixel_h) + isInfinite() * -1;
};
virtual int leftmost() const override {
return (!isInfinite() || original_x != -1) * getX() + isInfinite() * -1;
}
virtual int rightmost() const override {
return (!isInfinite() || w_ != -1) * (getX() + pixel_w) + isInfinite() * -1;
}
virtual void updateCollision(int x, int y, int w, int h) override {
position_x = original_x * w + x;
position_y = original_y * h + y;
pixel_w = w_ * w;
pixel_h = h_ * h;
}
virtual void render(Renderer &renderer, const std::tuple<int,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);
}
virtual void render(Renderer &renderer) override {
auto rect = getRect();
SDL_SetRenderDrawColor(renderer.getRendererPtr(), sdl_color.r, sdl_color.g, sdl_color.b, sdl_color.a);
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<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 h_;
int pixel_w;
int pixel_h;
};
class Circle : public CollisionPolygon {
public:
Circle(double x, double y, double rad) : CollisionPolygon(x, y) {
original_rad = rad;
}
virtual ~Circle() {}
virtual bool colidesWith(const CollisionPolygon &other) const override;
virtual bool isCircle() const override { return true; }
virtual int topmost() const override { return getY() - rad_; }
virtual int bottommost() const override { return getY() + rad_; };
virtual int leftmost() const override { return getX() - rad_; }
virtual int rightmost() const override { return getX() + rad_; }
virtual void updateCollision(int x, int y, int w, int h) override {
position_x = original_x * w + x;
position_y = original_y * h + y;
rad_ = original_rad * w;
}
virtual void render(Renderer &renderer, const std::tuple<int,int,int,int> &color) override {
std::vector<int> rect = {leftmost(), topmost(), rightmost(), bottommost()};
auto center_x = getX();
auto center_y = getY();
auto radsq = rad_ * rad_;
for(int i = rect[0]; i <= rect[2]; i++) {
auto xdiff = center_x - i;
auto xdist = xdiff * xdiff;
auto allowed_rad = sqrt(radsq - xdist);
SDL_SetRenderDrawColor(renderer.getRendererPtr(), std::get<0>(color), std::get<1>(color), std::get<2>(color), 0x40);
SDL_RenderDrawLine(renderer.getRendererPtr(), i, center_y - allowed_rad, i, center_y + allowed_rad);
SDL_SetRenderDrawColor(renderer.getRendererPtr(), std::get<0>(color), std::get<1>(color), std::get<2>(color), 0x80);
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);
}
SDL_SetRenderDrawColor(renderer.getRendererPtr(), 0xFF, 0, 0, 0xFF);
SDL_RenderDrawLine(renderer.getRendererPtr(), center_x, center_y, center_x + rad_, center_y);
SDL_RenderDrawLine(renderer.getRendererPtr(), center_x, center_y, center_x, center_y + rad_);
SDL_RenderDrawLine(renderer.getRendererPtr(), center_x, center_y, center_x - rad_, center_y);
SDL_RenderDrawLine(renderer.getRendererPtr(), center_x, center_y, center_x, center_y - rad_);
}
virtual void render(Renderer &renderer) override {
std::vector<int> rect = {leftmost(), topmost(), rightmost(), bottommost()};
auto center_x = getX();
auto center_y = getY();
auto radsq = rad_ * rad_;
for(int i = rect[0]; i <= rect[2]; i++) {
auto xdiff = center_x - i;
auto xdist = xdiff * xdiff;
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);
}
}
private:
int getRadius() const {
return rad_;
}
double original_rad;
int rad_;
};
class RectangleRender : public RenderObject { class RectangleRender : public RenderObject {
public: public:
RectangleRender() = delete; RectangleRender() = delete;
@ -420,12 +632,18 @@ public:
w_ = w; w_ = w;
h_ = h; h_ = h;
} }
RectangleRender(int x, int y, int w, int 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);
} }
RectangleRender(int x, int y, int w, int h, std::shared_ptr<Renderer> &r, const std::string &img_path) : RectangleRender(x,y,w,h,r) { 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) {
auto texture = std::make_shared<Texture>(r, img_path); if(!is_polygon) {
setTexture(texture); auto texture = std::make_shared<Texture>(r, img_or_color);
setTexture(texture);
} else {
polygon = std::make_shared<Rect>(0,0,1,1);
polygon->setColor(img_or_color);
polygon->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight());
}
} }
virtual void specialAction(int /*UNUSED*/) {}; virtual void specialAction(int /*UNUSED*/) {};
virtual void render() { virtual void render() {
@ -435,8 +653,13 @@ public:
for(const auto &col : getCollisions()) for(const auto &col : getCollisions())
col->render(*renderer, colider_color); col->render(*renderer, colider_color);
} }
if(polygon) {
polygon->render(*renderer);
}
} }
virtual void move(int ticks) { virtual void move(int ticks) {
if(permanent)
return;
auto dimension = renderer->getSmallerSide(); 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);
@ -447,6 +670,8 @@ public:
for( auto &x : collisions ) { for( auto &x : collisions ) {
x->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight()); x->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight());
} }
if(polygon)
polygon->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight());
} }
virtual std::pair<std::pair<double,double>,std::pair<double,double>> getDoubleRect() { virtual std::pair<std::pair<double,double>,std::pair<double,double>> getDoubleRect() {
return {{x_,y_}, {w_,h_}}; return {{x_,y_}, {w_,h_}};
@ -488,6 +713,8 @@ public:
rect.y = y_ * dimension; rect.y = y_ * dimension;
rect.w = w_ * dimension; rect.w = w_ * dimension;
rect.h = h_ * dimension; rect.h = h_ * dimension;
if(polygon)
polygon->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight());
} }
virtual SDL_Rect getRect() { virtual SDL_Rect getRect() {
return rect; return rect;
@ -535,115 +762,6 @@ private:
int rad_; int rad_;
}; };
class Rect : public CollisionPolygon {
public:
Rect(double x, double y, double w, double h) : CollisionPolygon(x, y) {
w_ = w;
h_ = h;
}
virtual ~Rect() {}
virtual bool colidesWith(const CollisionPolygon &other) const override;
virtual bool isCircle() const override { return false; }
virtual int topmost() const override {
return (!isInfinite() || original_y != -1) * getY() + isInfinite() * -1;
}
virtual int bottommost() const override {
return (!isInfinite() || h_ != -1) * (getY() + pixel_h) + isInfinite() * -1;
};
virtual int leftmost() const override {
return (!isInfinite() || original_x != -1) * getX() + isInfinite() * -1;
}
virtual int rightmost() const override {
return (!isInfinite() || w_ != -1) * (getX() + pixel_w) + isInfinite() * -1;
}
virtual void updateCollision(int x, int y, int w, int h) override {
position_x = original_x * w + x;
position_y = original_y * h + y;
pixel_w = w_ * w;
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:
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 h_;
int pixel_w;
int pixel_h;
};
class Circle : public CollisionPolygon {
public:
Circle(double x, double y, double rad) : CollisionPolygon(x, y) {
original_rad = rad;
}
virtual ~Circle() {}
virtual bool colidesWith(const CollisionPolygon &other) const override;
virtual bool isCircle() const override { return true; }
virtual int topmost() const override { return getY() - rad_; }
virtual int bottommost() const override { return getY() + rad_; };
virtual int leftmost() const override { return getX() - rad_; }
virtual int rightmost() const override { return getX() + rad_; }
virtual void updateCollision(int x, int y, int w, int h) override {
position_x = original_x * w + x;
position_y = original_y * h + y;
rad_ = original_rad * w;
}
virtual void render(Renderer &renderer, const std::tuple<int,int,int> &color) override {
std::vector<int> rect = {leftmost(), topmost(), rightmost(), bottommost()};
auto center_x = getX();
auto center_y = getY();
auto radsq = rad_ * rad_;
for(int i = rect[0]; i <= rect[2]; i++) {
auto xdiff = center_x - i;
auto xdist = xdiff * xdiff;
auto allowed_rad = sqrt(radsq - xdist);
SDL_SetRenderDrawColor(renderer.getRendererPtr(), std::get<0>(color), std::get<1>(color), std::get<2>(color), 0x40);
SDL_RenderDrawLine(renderer.getRendererPtr(), i, center_y - allowed_rad, i, center_y + allowed_rad);
SDL_SetRenderDrawColor(renderer.getRendererPtr(), std::get<0>(color), std::get<1>(color), std::get<2>(color), 0x80);
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);
}
SDL_SetRenderDrawColor(renderer.getRendererPtr(), 0xFF, 0, 0, 0xFF);
SDL_RenderDrawLine(renderer.getRendererPtr(), center_x, center_y, center_x + rad_, center_y);
SDL_RenderDrawLine(renderer.getRendererPtr(), center_x, center_y, center_x, center_y + rad_);
SDL_RenderDrawLine(renderer.getRendererPtr(), center_x, center_y, center_x - rad_, center_y);
SDL_RenderDrawLine(renderer.getRendererPtr(), center_x, center_y, center_x, center_y - rad_);
}
private:
int getRadius() const {
return rad_;
}
double original_rad;
int rad_;
};
bool init(); bool init();
bool init(uint32_t SDL_OPTIONS); bool init(uint32_t SDL_OPTIONS);
bool init(uint32_t SDL_OPTIONS, int IMAGE_OPTIONS); bool init(uint32_t SDL_OPTIONS, int IMAGE_OPTIONS);