Some basic formatting

This commit is contained in:
zvon 2020-08-23 09:50:05 +02:00
parent 097fab4329
commit a995ba3b85
4 changed files with 1277 additions and 999 deletions

View File

@ -23,7 +23,10 @@ void doInputPause();
class Player : public SDLPP::RectangleRender {
public:
Player(double x, double y, double w, double h, std::shared_ptr<SDLPP::Renderer> &r, double _max_gravity = 1.0, uint32_t _max_gravity_time = 1000) : SDLPP::RectangleRender(x,y,w,h,r) {
Player( double x, double y, double w, double h,
std::shared_ptr< SDLPP::Renderer > &r, double _max_gravity = 1.0,
uint32_t _max_gravity_time = 1000 )
: SDLPP::RectangleRender( x, y, w, h, r ) {
max_gravity = _max_gravity;
jump_speed = 2.2 * max_gravity;
max_gravity_time = _max_gravity_time;
@ -54,11 +57,14 @@ public:
virtual void custom_move( int ticks ) override {
auto time_portion = ( static_cast< double >( ticks ) / 1000 );
cur_gravity_time -= ticks;
auto grav = gravity_enabled * max_gravity * (max_gravity_time - cur_gravity_time)/max_gravity_time;
auto grav = gravity_enabled * max_gravity *
( max_gravity_time - cur_gravity_time ) / max_gravity_time;
if ( grav > max_gravity )
grav = max_gravity;
// percentage of how close we are to maximum velocity of gravity (0 = we've reached max velocity, 1 = we've just started accelerating)
double division = static_cast<double>(cur_gravity_time)/static_cast<double>(max_gravity_time);
// percentage of how close we are to maximum velocity of gravity (0 =
// we've reached max velocity, 1 = we've just started accelerating)
double division = static_cast< double >( cur_gravity_time ) /
static_cast< double >( max_gravity_time );
// current jump speed
auto jump_ = jumping * jump_speed * division;
if ( division < 0.75 && division >= 0.72 ) {
@ -75,6 +81,7 @@ public:
og_y += grav * time_portion;
og_y -= jump_ * time_portion;
}
private:
double max_gravity = 1.0;
double jump_speed = 2.0;
@ -86,8 +93,12 @@ private:
class Destroyable : public SDLPP::RectangleRender {
public:
Destroyable(double x, double y, double w, double h, std::shared_ptr<SDLPP::Renderer> &r) : SDLPP::RectangleRender(x,y,w,h,r) {}
Destroyable(double x, double y, double w, double h, std::shared_ptr<SDLPP::Renderer> &r, int destruction_time) : SDLPP::RectangleRender(x,y,w,h,r) {
Destroyable( double x, double y, double w, double h,
std::shared_ptr< SDLPP::Renderer > &r )
: SDLPP::RectangleRender( x, y, w, h, r ) {}
Destroyable( double x, double y, double w, double h,
std::shared_ptr< SDLPP::Renderer > &r, int destruction_time )
: SDLPP::RectangleRender( x, y, w, h, r ) {
destruction_countdown = destruction_time;
}
virtual void specialAction( int code ) override {
@ -101,6 +112,7 @@ public:
destroy();
}
}
private:
void startDestruction() {
destruction = true;
@ -113,14 +125,16 @@ std::shared_ptr<Player> player;
bool quit = false;
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);
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;
double posx = 0;
while ( posx < 3 ) {
stone = std::make_shared<Destroyable>(posx,0.5,0.15,0.1,r, 1000);
stone =
std::make_shared< Destroyable >( posx, 0.5, 0.15, 0.1, r, 1000 );
stone->addCollision( SDLPP::Rect( 0, 0, 1, 1 ) );
stone->setColor( "#222222FF" );
stone->setId( STONE_ID );
@ -153,7 +167,8 @@ void addStuff(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) {
}
void addPause( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer > &r ) {
auto bg = std::make_shared<SDLPP::RectangleRender>(0,0,10,10,r,"#00000080", true);
auto bg = std::make_shared< SDLPP::RectangleRender >( 0, 0, 10, 10, r,
"#00000080", true );
bg->setId( 123 );
bg->setPermanent( true );
scene.addObject( bg );
@ -162,13 +177,15 @@ void addPause(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) {
y->setId( 0 );
y->centerX();
scene.addObject( y );
auto resume = std::make_shared<SDLPP::TextRenderer>(0.4, 0.5, 0.2, 0.1, r);
auto resume =
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.5, 0.2, 0.1, r );
resume->setText( *font, "Resume", "#FFFFFF", "#000000", 5 );
resume->setColor( "#FFFFFF40" );
resume->centerX();
scene.addObject( resume );
pause_options.push_back( resume );
auto quit = std::make_shared<SDLPP::TextRenderer>(0.4, 0.7, 0.2, 0.1, r);
auto quit =
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.7, 0.2, 0.1, r );
quit->setText( *font, "Quit Game", "#FFFFFF", "#000000", 5 );
quit->centerX();
scene.addObject( quit );
@ -188,8 +205,7 @@ void handleKeyDown(SDL_Keycode key, SDLPP::Scene &scene) {
player->resetMovementX();
std::thread pauseThread( doInputPause );
pauseThread.detach();
}
break;
} break;
case SDLK_a:
player->addMovement( -1, 0 );
break;
@ -206,7 +222,8 @@ void handleKeyDown(SDL_Keycode key, SDLPP::Scene &scene) {
case SDLK_s:
break;
case SDLK_r:
scene.getRenderer().setRenderColiders(!scene.getRenderer().getRenderColiders());
scene.getRenderer().setRenderColiders(
!scene.getRenderer().getRenderColiders() );
default:
break;
}
@ -219,10 +236,10 @@ void handleKeyDownPause(SDL_Keycode key) {
active_scene->setPrevTicks( SDL_GetTicks() );
std::thread inputThread( doInput, active_scene );
inputThread.detach();
}
break;
} break;
case SDLK_r:
active_scene->getRenderer().setRenderColiders(!active_scene->getRenderer().getRenderColiders());
active_scene->getRenderer().setRenderColiders(
!active_scene->getRenderer().getRenderColiders() );
break;
case SDLK_s:
case SDLK_DOWN:
@ -247,8 +264,7 @@ void handleKeyDownPause(SDL_Keycode key) {
active_scene->setPrevTicks( SDL_GetTicks() );
std::thread inputThread( doInput, active_scene );
inputThread.detach();
}
break;
} break;
case 1:
quitGame();
default:
@ -335,7 +351,8 @@ void doInput(std::shared_ptr<SDLPP::Scene> scene) {
auto stoneRect = x->getDoubleRect();
auto playerPos = player->getDoubleRect();
auto newPX = playerPos.first.first;
auto newPY = stoneRect.first.second - playerPos.second.second;
auto newPY =
stoneRect.first.second - playerPos.second.second;
player->setPos( newPX, newPY );
player->setLastStand();
}
@ -351,12 +368,16 @@ void doInput(std::shared_ptr<SDLPP::Scene> scene) {
auto width = scene->getWidth();
auto rightBarrier = width * 0.7;
auto leftBarrier = width * 0.3;
auto rightmostX = scene->rightmost()->getRect().x + scene->rightmost()->getRect().w;
auto rightmostX =
scene->rightmost()->getRect().x + scene->rightmost()->getRect().w;
auto leftmostX = scene->leftmost()->getRect().x;
scene->moveEverything(
(playerX > rightBarrier && rightmostX > width) * (rightBarrier - playerX)/width, 0);
scene->moveEverything(
(playerX < leftBarrier && leftmostX < 0) * (leftBarrier - playerX)/width, 0);
( playerX > rightBarrier && rightmostX > width ) *
( rightBarrier - playerX ) / width,
0 );
scene->moveEverything( ( playerX < leftBarrier && leftmostX < 0 ) *
( leftBarrier - playerX ) / width,
0 );
}
}

View File

@ -5,15 +5,18 @@
bool SDLPP::init() {
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError()
<< std::endl;
return false;
}
if ( IMG_Init( IMG_INIT_PNG ) != IMG_INIT_PNG ) {
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;
}
if ( TTF_Init() == -1 ) {
std::cerr << "SDL_ttf could not initialize! SDL_ttf Error: " << TTF_GetError() << std::endl;
std::cerr << "SDL_ttf could not initialize! SDL_ttf Error: "
<< TTF_GetError() << std::endl;
return false;
}
return true;
@ -21,11 +24,13 @@ bool SDLPP::init() {
bool SDLPP::init( uint32_t SDL_OPTIONS ) {
if ( SDL_Init( SDL_OPTIONS ) < 0 ) {
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError()
<< std::endl;
return false;
}
if ( IMG_Init( IMG_INIT_PNG ) != IMG_INIT_PNG ) {
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 true;
@ -33,35 +38,45 @@ bool SDLPP::init(uint32_t SDL_OPTIONS) {
bool SDLPP::init( uint32_t SDL_OPTIONS, int IMAGE_OPTIONS ) {
if ( SDL_Init( SDL_OPTIONS ) < 0 ) {
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError()
<< std::endl;
return false;
}
if ( IMG_Init( IMAGE_OPTIONS ) != IMAGE_OPTIONS ) {
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 true;
}
void SDLPP::CircleRender::render() {
std::cout << "I'm a circle, look at me go!" << std::endl << "My dimensions are: [" << x_ << ", " << y_ << "], radius: " << rad_ << std::endl;
std::cout << "I'm a circle, look at me go!" << std::endl
<< "My dimensions are: [" << x_ << ", " << y_
<< "], radius: " << rad_ << std::endl;
}
// only rectangles for now
bool intersects(const SDLPP::CollisionPolygon &p1, const SDLPP::CollisionPolygon &p2) {
return !(p1.rightmost() < p2.leftmost() || p2.rightmost() < p1.leftmost() ||
bool intersects( const SDLPP::CollisionPolygon &p1,
const SDLPP::CollisionPolygon &p2 ) {
return !(
p1.rightmost() < p2.leftmost() || p2.rightmost() < p1.leftmost() ||
p1.topmost() > p2.bottommost() || p2.topmost() > p1.bottommost() );
}
bool infinityIntersection(const SDLPP::CollisionPolygon &infinite, const SDLPP::CollisionPolygon &other) {
bool infinityIntersection( const SDLPP::CollisionPolygon &infinite,
const SDLPP::CollisionPolygon &other ) {
int ileft = infinite.leftmost();
int iright = infinite.rightmost();
int itop = infinite.topmost();
int ibottom = infinite.bottommost();
bool ret = ileft != -1 && ileft <= other.rightmost() && ileft >= other.leftmost();
ret |= iright != -1 && iright >= other.leftmost() && iright <= other.rightmost();
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 |= ibottom != -1 && ibottom >= other.topmost() &&
ibottom <= other.bottommost();
ret |= ileft == -1 && iright == -1 && itop == -1 && ibottom == -1;
return ret;
}
@ -93,9 +108,11 @@ bool SDLPP::Circle::colidesWith(const SDLPP::CollisionPolygon &other) const {
int centerx = getX();
int centery = getY();
if ( other.topmost() <= centery && other.bottommost() >= centery ) {
return other.leftmost() <= rightmost() && other.rightmost() >= leftmost();
return other.leftmost() <= rightmost() &&
other.rightmost() >= leftmost();
} else if ( other.leftmost() <= centerx && other.rightmost() >= centerx ) {
return other.topmost() <= bottommost() && other.bottommost() >= topmost();
return other.topmost() <= bottommost() &&
other.bottommost() >= topmost();
}
int pointx = 0, pointy = 0;
if ( centerx > other.rightmost() ) {
@ -108,7 +125,8 @@ bool SDLPP::Circle::colidesWith(const SDLPP::CollisionPolygon &other) const {
} else {
pointy = other.bottommost();
}
int distancesquared = (pointx - centerx)*(pointx - centerx) + (pointy - centery)*(pointy-centery);
int distancesquared = ( pointx - centerx ) * ( pointx - centerx ) +
( pointy - centery ) * ( pointy - centery );
return distancesquared <= rad * rad;
}
@ -136,7 +154,8 @@ int SDLPP::hex2num(char c) {
}
}
std::tuple<int, 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, alpha = 255;
const char *color_ptr = color.c_str();
if ( color_ptr[0] == '#' )
@ -149,7 +168,8 @@ std::tuple<int, int, int, int> SDLPP::getColorsHEX(const std::string &color) {
return { red, green, blue, alpha };
}
SDL_Color SDLPP::getSDLColorTuple(const std::tuple<int, int, int, int> &tuple) {
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 );
@ -163,6 +183,7 @@ SDL_Color SDLPP::getSDLColorHEX(const std::string &color) {
return getSDLColorTuple( color_tuple );
}
std::tuple<int, int, int, int> SDLPP::getColorsSDLColor(const SDL_Color &color) {
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

@ -30,16 +30,22 @@ SDL_Color getSDLColorTuple(const std::tuple<int, int, int, int> &tuple);
class Window {
public:
Window() : Window("SDL Window", 640, 480, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED) {
}
Window(const std::string &window_name) : Window(window_name, 640, 480, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED) {
}
Window(const std::string &window_name, uint32_t width, uint32_t height) : Window(window_name, width, height, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED) {}
Window()
: Window( "SDL Window", 640, 480, SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED ) {}
Window( const std::string &window_name )
: Window( window_name, 640, 480, SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED ) {}
Window( const std::string &window_name, uint32_t width, uint32_t height )
: Window( window_name, width, height, SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED ) {}
Window( const std::string &window_name, uint32_t width, uint32_t height,
uint32_t posx, uint32_t posy ) {
window = SDL_CreateWindow(window_name.c_str(), posx, posy, width, height, SDL_WINDOW_SHOWN);
window = SDL_CreateWindow( window_name.c_str(), posx, posy, width,
height, SDL_WINDOW_SHOWN );
if ( window == NULL ) {
std::cerr << "SDL could not create a window! SDL_Error: " << SDL_GetError() << std::endl;
std::cerr << "SDL could not create a window! SDL_Error: "
<< SDL_GetError() << std::endl;
throw "Couldn't create window";
}
}
@ -49,6 +55,7 @@ public:
SDL_Window *getWindowPtr() {
return window;
}
private:
SDL_Window *window = NULL;
};
@ -57,9 +64,11 @@ class Renderer {
public:
Renderer() = delete;
Renderer( Window &window ) {
renderer = SDL_CreateRenderer(window.getWindowPtr(), -1, SDL_RENDERER_ACCELERATED);
renderer = SDL_CreateRenderer( window.getWindowPtr(), -1,
SDL_RENDERER_ACCELERATED );
if ( renderer == NULL ) {
std::cerr << "SDL could not create a renderer! SDL_Error: " << SDL_GetError();
std::cerr << "SDL could not create a renderer! SDL_Error: "
<< SDL_GetError();
throw "Couldn't create renderer";
}
SDL_SetRenderDrawColor( renderer, 0xFF, 0xFF, 0xFF, 0xFF );
@ -83,11 +92,13 @@ public:
}
int getSmallerSide() const {
auto dimensions = getDimensions();
return dimensions.first < dimensions.second ? dimensions.first : dimensions.second;
return dimensions.first < dimensions.second ? dimensions.first
: dimensions.second;
}
int getLargerSide() const {
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 );
@ -98,6 +109,7 @@ public:
bool getRenderColiders() {
return render_coliders;
}
private:
SDL_Renderer *renderer = NULL;
bool render_coliders = false;
@ -109,7 +121,8 @@ public:
Font( const std::string &font, int size ) {
font_ptr = TTF_OpenFont( font.c_str(), size );
if ( font_ptr == NULL ) {
std::cerr << "Unable to load font '" << font << "': TTF Error: " << TTF_GetError() << std::endl;
std::cerr << "Unable to load font '" << font
<< "': TTF Error: " << TTF_GetError() << std::endl;
throw "TTF_OpenFont error";
}
}
@ -134,6 +147,7 @@ public:
void setHinting( int hinting ) {
TTF_SetFontHinting( font_ptr, hinting );
}
private:
TTF_Font *font_ptr;
};
@ -141,36 +155,51 @@ private:
class Texture {
public:
Texture() = delete;
Texture(std::shared_ptr<Renderer> &renderer, const std::string &img_path) : Texture(renderer, img_path, "") {}
Texture(std::shared_ptr<Renderer> &renderer, const std::string &img_path, const std::string &color_key) {
Texture( std::shared_ptr< Renderer > &renderer,
const std::string &img_path )
: Texture( renderer, img_path, "" ) {}
Texture( std::shared_ptr< Renderer > &renderer, const std::string &img_path,
const std::string &color_key ) {
SDL_Surface *surface = IMG_Load( img_path.c_str() );
if ( surface == NULL ) {
std::cerr << "Unable to load image '" << img_path << "': IMG Error: " << IMG_GetError() << std::endl;
std::cerr << "Unable to load image '" << img_path
<< "': IMG Error: " << IMG_GetError() << std::endl;
throw "IMG_Load error";
}
if ( !color_key.empty() ) {
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 ) ) );
}
setTextureFromSurface( renderer, surface );
}
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) {
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 ) {
if ( outline_size != -1 ) {
font.setOutline( outline_size );
}
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) );
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;
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) );
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;
std::cerr << "Unable to render text '" << text
<< "': TTF Error: " << TTF_GetError() << std::endl;
throw "TTF_RenderUTF8_Shaded error";
}
if ( og_outline != 0 ) {
@ -190,11 +219,15 @@ public:
SDL_Texture *getTexturePtr() {
return texture;
}
private:
void setTextureFromSurface(std::shared_ptr<Renderer> &renderer, SDL_Surface *surface) {
texture = SDL_CreateTextureFromSurface(renderer->getRendererPtr(), surface);
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;
std::cerr << "Unable to create texture from surface! SDL Error: "
<< SDL_GetError() << std::endl;
throw "Texture error";
}
SDL_FreeSurface( surface );
@ -213,8 +246,12 @@ public:
virtual ~CollisionPolygon() {}
virtual bool colidesWith( const CollisionPolygon &other ) const = 0;
virtual bool isCircle() const = 0;
virtual bool isInfinite() const { return infinite; }
virtual void setInfinite() { infinite = true; }
virtual bool isInfinite() const {
return infinite;
}
virtual void setInfinite() {
infinite = true;
}
virtual int topmost() const = 0;
virtual int bottommost() const = 0;
virtual int leftmost() const = 0;
@ -223,7 +260,8 @@ public:
position_x = original_x * w + x;
position_y = original_y * h + y;
}
virtual void render(Renderer &renderer, const std::tuple<int,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 {
return position_x;
@ -237,6 +275,7 @@ public:
void setOutlineColor( const std::string &color ) {
sdl_outline = getSDLColorHEX( color );
}
protected:
double original_x;
double original_y;
@ -263,11 +302,14 @@ public:
virtual int collisionWidth() = 0;
virtual int collisionHeight() = 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 std::pair< double, double > getPos() = 0;
bool colidesWith( const RenderObject &other ) const {
if(!hasCollisions() || !other.hasCollisions() || getHidden() || other.getHidden()) {
if ( !hasCollisions() || !other.hasCollisions() || getHidden() ||
other.getHidden() ) {
return false;
}
for ( const auto &x : collisions ) {
@ -278,15 +320,17 @@ public:
}
return false;
}
template<class T>
void addCollision(const T &p) {
template < class T > void addCollision( const T &p ) {
collisions.push_back( std::make_shared< T >( p ) );
collisions.back()->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight());
collisions.back()->updateCollision( collisionPushX(), collisionPushY(),
collisionWidth(),
collisionHeight() );
}
bool hasCollisions() const {
return !collisions.empty();
}
const std::vector<std::shared_ptr<CollisionPolygon>> &getCollisions() const {
const std::vector< std::shared_ptr< CollisionPolygon > > &
getCollisions() const {
return collisions;
}
virtual void setTexture( std::shared_ptr< Texture > &t ) {
@ -295,8 +339,12 @@ public:
virtual void setTexture( const std::string &img_path ) {
texture = std::make_shared< Texture >( renderer, img_path );
}
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 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;
@ -362,6 +410,7 @@ public:
return permanent;
}
virtual void centerX() = 0;
protected:
std::vector< std::shared_ptr< CollisionPolygon > > collisions;
std::shared_ptr< Texture > texture;
@ -376,6 +425,7 @@ protected:
std::tuple< int, int, int, int > colider_color = { 0x00, 0xFF, 0xFF, 0xFF };
uint64_t scene_id;
bool permanent = false;
private:
void setSceneID( int id ) {
scene_id = id;
@ -386,7 +436,8 @@ private:
class Scene {
public:
Scene( std::shared_ptr< Renderer > &r ) : renderer( r ) {
SDL_SetRenderDrawColor(renderer->getRendererPtr(), 0xFF, 0xFF, 0xFF, 0xFF);
SDL_SetRenderDrawColor( renderer->getRendererPtr(), 0xFF, 0xFF, 0xFF,
0xFF );
prev_ticks = SDL_GetTicks();
}
void addObject( const std::shared_ptr< RenderObject > &obj ) {
@ -406,7 +457,8 @@ public:
if ( rect.first.first < leftmost_rect.first.first )
leftmost_obj = obj;
auto rightmost_rect = rightmost_obj->getDoubleRect();
if(rect.first.first + rect.second.first > rightmost_rect.first.first + rightmost_rect.second.first)
if ( rect.first.first + rect.second.first >
rightmost_rect.first.first + rightmost_rect.second.first )
rightmost_obj = obj;
}
render_mutex.unlock();
@ -427,7 +479,8 @@ public:
prev_ticks = now_ticks;
render_mutex.unlock();
}
std::vector<std::shared_ptr<RenderObject>> getCollisions(RenderObject &r) {
std::vector< std::shared_ptr< RenderObject > >
getCollisions( RenderObject &r ) {
if ( r.getHidden() )
return {};
std::vector< std::shared_ptr< RenderObject > > ret{};
@ -438,12 +491,15 @@ public:
}
return ret;
}
std::vector<std::shared_ptr<RenderObject>> getCollisions( RenderObject &r, const std::unordered_set<int> &objectIDs ) {
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)) {
if ( objectIDs.find( x->getId() ) != objectIDs.end() &&
x->colidesWith( r ) ) {
ret.push_back( x );
}
}
@ -455,7 +511,8 @@ public:
if ( clear_scene )
SDL_RenderClear( renderer->getRendererPtr() );
if ( background && background->getTexturePtr() )
SDL_RenderCopy(renderer->getRendererPtr(), background->getTexturePtr(), NULL, NULL);
SDL_RenderCopy( renderer->getRendererPtr(),
background->getTexturePtr(), NULL, NULL );
for ( const auto &x : renderObjects ) {
x->render();
}
@ -476,7 +533,9 @@ public:
for ( auto &x : renderObjects ) {
x->updateSizeAndPosition();
for ( auto &col : x->getCollisions() ) {
col->updateCollision(x->collisionPushX(), x->collisionPushY(), x->collisionWidth(), x->collisionHeight());
col->updateCollision( x->collisionPushX(), x->collisionPushY(),
x->collisionWidth(),
x->collisionHeight() );
}
}
render_mutex.unlock();
@ -513,6 +572,7 @@ public:
void setPrevTicks( int ticks ) {
prev_ticks = ticks;
}
private:
void checkKilled() {
render_mutex.lock();
@ -547,18 +607,24 @@ public:
}
virtual ~Rect() {}
virtual bool colidesWith( const CollisionPolygon &other ) const override;
virtual bool isCircle() const override { return false; }
virtual bool isCircle() const override {
return false;
}
virtual int topmost() const override {
return (!isInfinite() || original_y != -1) * getY() + isInfinite() * -1;
return ( !isInfinite() || original_y != -1 ) * getY() +
isInfinite() * -1;
}
virtual int bottommost() const override {
return (!isInfinite() || h_ != -1) * (getY() + pixel_h) + isInfinite() * -1;
return ( !isInfinite() || h_ != -1 ) * ( getY() + pixel_h ) +
isInfinite() * -1;
};
virtual int leftmost() const override {
return (!isInfinite() || original_x != -1) * getX() + isInfinite() * -1;
return ( !isInfinite() || original_x != -1 ) * getX() +
isInfinite() * -1;
}
virtual int rightmost() const override {
return (!isInfinite() || w_ != -1) * (getX() + pixel_w) + isInfinite() * -1;
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;
@ -566,22 +632,31 @@ public:
pixel_w = w_ * w;
pixel_h = h_ * h;
}
virtual void render(Renderer &renderer, const std::tuple<int,int,int,int> &color) override {
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_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_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_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_SetRenderDrawColor( renderer.getRendererPtr(), sdl_outline.r,
sdl_outline.g, sdl_outline.b, sdl_outline.a );
SDL_RenderDrawRect( renderer.getRendererPtr(), &rect );
}
private:
SDL_Rect getRect() {
if ( !isInfinite() )
@ -616,18 +691,31 @@ public:
}
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 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()};
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_;
@ -635,20 +723,35 @@ public:
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(), 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_);
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()};
std::vector< int > rect = { leftmost(), topmost(), rightmost(),
bottommost() };
auto center_x = getX();
auto center_y = getY();
auto radsq = rad_ * rad_;
@ -656,13 +759,23 @@ public:
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);
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);
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:
int getRadius() const {
return rad_;
@ -675,17 +788,25 @@ class RectangleRender : public RenderObject {
public:
RectangleRender() = delete;
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 ) {
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) {
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 );
}
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) {
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 ) {
if ( !is_polygon ) {
setTexture( img_or_color );
} else {
@ -695,14 +816,16 @@ public:
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->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->updateCollision( collisionPushX(), collisionPushY(),
collisionWidth(), collisionHeight() );
}
polygon->setOutlineColor( color );
}
@ -712,9 +835,11 @@ public:
if ( polygon )
polygon->render( *renderer );
if ( texture != NULL )
SDL_RenderCopy(renderer->getRendererPtr(), texture->getTexturePtr(), NULL, &rect);
SDL_RenderCopy( renderer->getRendererPtr(),
texture->getTexturePtr(), NULL, &rect );
}
if(hasCollisions() && renderer->getRenderColiders() && !getHidden()) {
if ( hasCollisions() && renderer->getRenderColiders() &&
!getHidden() ) {
for ( const auto &col : getCollisions() )
col->render( *renderer, colider_color );
}
@ -722,8 +847,12 @@ public:
virtual void move( int ticks ) override {
if ( permanent )
return;
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 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 );
if ( std::isnan( addx ) || std::isnan( addy ) )
return;
@ -735,7 +864,9 @@ public:
updateSizeAndPosition();
}
virtual void custom_move( int /*UNUSED*/ ) override {}
virtual std::pair<std::pair<double,double>,std::pair<double,double>> getDoubleRect() 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 ) override {
@ -778,9 +909,11 @@ public:
rect.w = w_ * dimension;
rect.h = h_ * dimension;
if ( polygon )
polygon->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight());
polygon->updateCollision( collisionPushX(), collisionPushY(),
collisionWidth(), collisionHeight() );
for ( auto &x : collisions ) {
x->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight());
x->updateCollision( collisionPushX(), collisionPushY(),
collisionWidth(), collisionHeight() );
}
}
virtual SDL_Rect getRect() override {
@ -790,6 +923,7 @@ public:
centerx = true;
updateSizeAndPosition();
}
protected:
void updateXY() {
if ( !centerx ) {
@ -800,7 +934,8 @@ protected:
auto width = renderer->getWidth();
auto height = renderer->getHeight();
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 + static_cast< double >( multiplier - 1 ) / 2;
} else {
x_ = og_x;
@ -822,12 +957,22 @@ protected:
class TextRenderer : public RectangleRender {
public:
TextRenderer() = delete;
TextRenderer( double x, double y, double w, double h, std::shared_ptr<Renderer> &r ) : RectangleRender(x, y, w, h, r) {}
TextRenderer( double x, double y, double w, double h, std::shared_ptr<Renderer> &r, Font &font, const std::string &text, const std::string &color = "FFFFFF", const std::string &outline_color = "000000", int outline_size = -1, int flags = SDLPP_TEXT_CENTER ) : RectangleRender(x, y, w, h, r) {
TextRenderer( double x, double y, double w, double h,
std::shared_ptr< Renderer > &r )
: RectangleRender( x, y, w, h, r ) {}
TextRenderer( double x, double y, double w, double h,
std::shared_ptr< Renderer > &r, Font &font,
const std::string &text, const std::string &color = "FFFFFF",
const std::string &outline_color = "000000",
int outline_size = -1, int flags = SDLPP_TEXT_CENTER )
: RectangleRender( x, y, w, h, r ) {
position_flags = flags;
setText( font, text, color, outline_color, outline_size );
}
void setText(Font &font, const std::string &text, const std::string &color = "FFFFFF", const std::string &outline_color = "000000", int outline_size = -1) {
void setText( Font &font, const std::string &text,
const std::string &color = "FFFFFF",
const std::string &outline_color = "000000",
int outline_size = -1 ) {
setTexture( font, text, color, outline_color, outline_size );
updateDstRect();
}
@ -840,9 +985,11 @@ public:
if ( polygon )
polygon->render( *renderer );
if ( texture != NULL )
SDL_RenderCopy(renderer->getRendererPtr(), texture->getTexturePtr(), NULL, &dst_rect);
SDL_RenderCopy( renderer->getRendererPtr(),
texture->getTexturePtr(), NULL, &dst_rect );
}
if(hasCollisions() && renderer->getRenderColiders() && !getHidden()) {
if ( hasCollisions() && renderer->getRenderColiders() &&
!getHidden() ) {
for ( const auto &col : getCollisions() )
col->render( *renderer, colider_color );
}
@ -851,18 +998,22 @@ public:
RectangleRender::updateSizeAndPosition();
updateDstRect();
}
private:
void updateDstRect() {
if ( !texture )
return;
int text_width{}, text_height{};
SDL_QueryTexture(texture->getTexturePtr(), NULL, NULL, &text_width, &text_height);
SDL_QueryTexture( texture->getTexturePtr(), NULL, NULL, &text_width,
&text_height );
if ( text_width < rect.w && text_height < rect.h ) {
dst_rect.w = text_width;
dst_rect.h = text_height;
} else {
double x_div = static_cast<double>(text_width)/static_cast<double>(rect.w);
double y_div = static_cast<double>(text_height)/static_cast<double>(rect.h);
double x_div = static_cast< double >( text_width ) /
static_cast< double >( rect.w );
double y_div = static_cast< double >( text_height ) /
static_cast< double >( rect.h );
if ( x_div > y_div ) {
dst_rect.w = text_width / x_div;
dst_rect.h = text_height / x_div;
@ -871,14 +1022,16 @@ private:
dst_rect.h = text_height / y_div;
}
}
if(!(position_flags & SDLPP_TEXT_LEFT || position_flags & SDLPP_TEXT_RIGHT)) {
if ( !( position_flags & SDLPP_TEXT_LEFT ||
position_flags & SDLPP_TEXT_RIGHT ) ) {
dst_rect.x = rect.x + ( rect.w - dst_rect.w ) / 2;
} else if ( position_flags & SDLPP_TEXT_LEFT ) {
dst_rect.x = rect.x;
} else if ( position_flags & SDLPP_TEXT_RIGHT ) {
dst_rect.x = rect.x + rect.w - dst_rect.w;
}
if(!(position_flags & SDLPP_TEXT_TOP || position_flags & SDLPP_TEXT_BOTTOM)) {
if ( !( position_flags & SDLPP_TEXT_TOP ||
position_flags & SDLPP_TEXT_BOTTOM ) ) {
dst_rect.y = rect.y + ( rect.h - dst_rect.h ) / 2;
} else if ( position_flags & SDLPP_TEXT_TOP ) {
dst_rect.y = rect.y;
@ -894,15 +1047,20 @@ class CircleRender : public RenderObject {
public:
CircleRender() = delete;
virtual ~CircleRender(){};
CircleRender(int x, int y, int rad, std::shared_ptr<Renderer> &r) : RenderObject(r) {
CircleRender( int x, int y, int rad, std::shared_ptr< Renderer > &r )
: RenderObject( r ) {
x_ = x;
y_ = y;
rad_ = rad;
}
CircleRender(int x, int y, int rad, std::shared_ptr<Renderer> &r, std::shared_ptr<Texture> &t) : CircleRender(x,y,rad,r) {
CircleRender( int x, int y, int rad, std::shared_ptr< Renderer > &r,
std::shared_ptr< Texture > &t )
: CircleRender( x, y, rad, r ) {
setTexture( t );
}
CircleRender(int x, int y, int rad, std::shared_ptr<Renderer> &r, const std::string &img_path) : CircleRender(x,y,rad,r) {
CircleRender( int x, int y, int rad, std::shared_ptr< Renderer > &r,
const std::string &img_path )
: CircleRender( x, y, rad, r ) {
auto texture = std::make_shared< Texture >( r, img_path );
setTexture( texture );
}
@ -919,6 +1077,7 @@ public:
virtual int collisionPushY() {
return y_;
}
private:
int x_;
int y_;
@ -929,8 +1088,7 @@ bool init();
bool init( uint32_t SDL_OPTIONS );
bool init( uint32_t SDL_OPTIONS, int IMAGE_OPTIONS );
template<class T>
void testPolymorphism(T &obj);
template < class T > void testPolymorphism( T &obj );
} // end of namespace SDLPP

View File

@ -26,7 +26,8 @@ std::shared_ptr<SDLPP::Scene> pause_scene;
class TetrisPiece {
public:
void addPiece(std::shared_ptr<SDLPP::RectangleRender> piece, int x, int y) {
void addPiece( std::shared_ptr< SDLPP::RectangleRender > piece, int x,
int y ) {
pieces.push_back( piece );
pieces_rel_position.push_back( { 0, 0, 0, 0 } );
// done this way for SPEEEEEEED
@ -90,6 +91,7 @@ public:
bool isDescending() {
return descend;
}
private:
std::vector< std::vector< int > > pieces_rel_position;
std::vector< std::shared_ptr< SDLPP::RectangleRender > > pieces;
@ -108,8 +110,13 @@ bool quit = false;
std::mutex movement_mutex;
std::shared_ptr<SDLPP::RectangleRender> createTetrisBlock(double x, double y, const std::string &color, const std::string &outline, std::shared_ptr<SDLPP::Renderer> renderer, std::shared_ptr<SDLPP::Scene> scene) {
auto ret = std::make_shared<SDLPP::RectangleRender>(x, y, 0.04, 0.04, renderer, color, true);
std::shared_ptr< SDLPP::RectangleRender >
createTetrisBlock( double x, double y, const std::string &color,
const std::string &outline,
std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Scene > scene ) {
auto ret = std::make_shared< SDLPP::RectangleRender >(
x, y, 0.04, 0.04, renderer, color, true );
ret->setOutlineColor( outline );
ret->addCollision( SDLPP::Rect( 0.1, 0.1, 0.8, 0.8 ) );
ret->setId( BRICK_ID );
@ -118,137 +125,202 @@ std::shared_ptr<SDLPP::RectangleRender> createTetrisBlock(double x, double y, co
return ret;
}
TetrisPiece tetrisBrick(std::shared_ptr<SDLPP::Renderer> renderer, std::shared_ptr<SDLPP::Scene> scene) {
TetrisPiece tetrisBrick( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Scene > scene ) {
TetrisPiece retPiece{};
auto color = "#FF0000";
auto outline = "#AA0000";
retPiece.addPiece(createTetrisBlock(0.46, 0.16, color, outline, renderer, scene), 0, 0);
retPiece.addPiece(createTetrisBlock(0.5, 0.16, color, outline, renderer, scene), 0, 0);
retPiece.addPiece(createTetrisBlock(0.46, 0.20, color, outline, renderer, scene), 0, 0);
retPiece.addPiece(createTetrisBlock(0.5, 0.20, color, outline, renderer, scene), 0, 0);
retPiece.addPiece(
createTetrisBlock( 0.46, 0.16, color, outline, renderer, scene ), 0,
0 );
retPiece.addPiece(
createTetrisBlock( 0.5, 0.16, color, outline, renderer, scene ), 0, 0 );
retPiece.addPiece(
createTetrisBlock( 0.46, 0.20, color, outline, renderer, scene ), 0,
0 );
retPiece.addPiece(
createTetrisBlock( 0.5, 0.20, color, outline, renderer, scene ), 0, 0 );
retPiece.setDefPos( 0.5, 0.16 );
return retPiece;
}
TetrisPiece tetrisT(std::shared_ptr<SDLPP::Renderer> renderer, std::shared_ptr<SDLPP::Scene> scene) {
TetrisPiece tetrisT( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Scene > scene ) {
TetrisPiece retPiece{};
auto color = "#00FF00";
auto outline = "#00AA00";
retPiece.addPiece(createTetrisBlock(0.46, 0.20, color, outline, renderer, scene), -1, 0);
retPiece.addPiece(createTetrisBlock(0.5, 0.20, color, outline, renderer, scene), 0, 0);
retPiece.addPiece(createTetrisBlock(0.5, 0.16, color, outline, renderer, scene), 0, -1);
retPiece.addPiece(createTetrisBlock(0.54, 0.20, color, outline, renderer, scene), 1, 0);
retPiece.addPiece(
createTetrisBlock( 0.46, 0.20, color, outline, renderer, scene ), -1,
0 );
retPiece.addPiece(
createTetrisBlock( 0.5, 0.20, color, outline, renderer, scene ), 0, 0 );
retPiece.addPiece(
createTetrisBlock( 0.5, 0.16, color, outline, renderer, scene ), 0,
-1 );
retPiece.addPiece(
createTetrisBlock( 0.54, 0.20, color, outline, renderer, scene ), 1,
0 );
retPiece.setDefPos( 0.5, 0.16 );
return retPiece;
}
TetrisPiece tetrisLRight(std::shared_ptr<SDLPP::Renderer> renderer, std::shared_ptr<SDLPP::Scene> scene) {
TetrisPiece tetrisLRight( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Scene > scene ) {
TetrisPiece retPiece{};
auto color = "#0000FF";
auto outline = "#0000AA";
retPiece.addPiece(createTetrisBlock(0.46, 0.20, color, outline, renderer, scene), -2, 0);
retPiece.addPiece(createTetrisBlock(0.5, 0.20, color, outline, renderer, scene), -1, 0);
retPiece.addPiece(createTetrisBlock(0.54, 0.20, color, outline, renderer, scene), 0, 0);
retPiece.addPiece(createTetrisBlock(0.54, 0.16, color, outline, renderer, scene), 0, -1);
retPiece.addPiece(
createTetrisBlock( 0.46, 0.20, color, outline, renderer, scene ), -2,
0 );
retPiece.addPiece(
createTetrisBlock( 0.5, 0.20, color, outline, renderer, scene ), -1,
0 );
retPiece.addPiece(
createTetrisBlock( 0.54, 0.20, color, outline, renderer, scene ), 0,
0 );
retPiece.addPiece(
createTetrisBlock( 0.54, 0.16, color, outline, renderer, scene ), 0,
-1 );
retPiece.setDefPos( 0.5, 0.16 );
return retPiece;
}
TetrisPiece tetrisZRight(std::shared_ptr<SDLPP::Renderer> renderer, std::shared_ptr<SDLPP::Scene> scene) {
TetrisPiece tetrisZRight( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Scene > scene ) {
TetrisPiece retPiece{};
auto color = "#FF00FF";
auto outline = "#AA00AA";
retPiece.addPiece(createTetrisBlock(0.46, 0.20, color, outline, renderer, scene), -1, 0);
retPiece.addPiece(createTetrisBlock(0.5, 0.20, color, outline, renderer, scene), 0, 0);
retPiece.addPiece(createTetrisBlock(0.5, 0.16, color, outline, renderer, scene), 0, -1);
retPiece.addPiece(createTetrisBlock(0.54, 0.16, color, outline, renderer, scene), 1, -1);
retPiece.addPiece(
createTetrisBlock( 0.46, 0.20, color, outline, renderer, scene ), -1,
0 );
retPiece.addPiece(
createTetrisBlock( 0.5, 0.20, color, outline, renderer, scene ), 0, 0 );
retPiece.addPiece(
createTetrisBlock( 0.5, 0.16, color, outline, renderer, scene ), 0,
-1 );
retPiece.addPiece(
createTetrisBlock( 0.54, 0.16, color, outline, renderer, scene ), 1,
-1 );
retPiece.setDefPos( 0.5, 0.16 );
return retPiece;
}
TetrisPiece tetrisLine(std::shared_ptr<SDLPP::Renderer> renderer, std::shared_ptr<SDLPP::Scene> scene) {
TetrisPiece tetrisLine( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Scene > scene ) {
TetrisPiece retPiece{};
auto color = "#FFFF00";
auto outline = "#AAAA00";
retPiece.addPiece(createTetrisBlock(0.42, 0.16, color, outline, renderer, scene), -1, 0);
retPiece.addPiece(createTetrisBlock(0.46, 0.16, color, outline, renderer, scene), 0, 0);
retPiece.addPiece(createTetrisBlock(0.5, 0.16, color, outline, renderer, scene), 1, 0);
retPiece.addPiece(createTetrisBlock(0.54, 0.16, color, outline, renderer, scene), 2, 0);
retPiece.addPiece(
createTetrisBlock( 0.42, 0.16, color, outline, renderer, scene ), -1,
0 );
retPiece.addPiece(
createTetrisBlock( 0.46, 0.16, color, outline, renderer, scene ), 0,
0 );
retPiece.addPiece(
createTetrisBlock( 0.5, 0.16, color, outline, renderer, scene ), 1, 0 );
retPiece.addPiece(
createTetrisBlock( 0.54, 0.16, color, outline, renderer, scene ), 2,
0 );
retPiece.setDefPos( 0.5, 0.16 );
return retPiece;
}
TetrisPiece tetrisLLeft(std::shared_ptr<SDLPP::Renderer> renderer, std::shared_ptr<SDLPP::Scene> scene) {
TetrisPiece tetrisLLeft( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Scene > scene ) {
TetrisPiece retPiece{};
auto color = "#00FFFF";
auto outline = "#00AAAA";
retPiece.addPiece(createTetrisBlock(0.46, 0.16, color, outline, renderer, scene), 0, -1);
retPiece.addPiece(createTetrisBlock(0.46, 0.20, color, outline, renderer, scene), 0, 0);
retPiece.addPiece(createTetrisBlock(0.5, 0.20, color, outline, renderer, scene), 1, 0);
retPiece.addPiece(createTetrisBlock(0.54, 0.20, color, outline, renderer, scene), 2, 0);
retPiece.addPiece(
createTetrisBlock( 0.46, 0.16, color, outline, renderer, scene ), 0,
-1 );
retPiece.addPiece(
createTetrisBlock( 0.46, 0.20, color, outline, renderer, scene ), 0,
0 );
retPiece.addPiece(
createTetrisBlock( 0.5, 0.20, color, outline, renderer, scene ), 1, 0 );
retPiece.addPiece(
createTetrisBlock( 0.54, 0.20, color, outline, renderer, scene ), 2,
0 );
retPiece.setDefPos( 0.5, 0.16 );
return retPiece;
}
TetrisPiece tetrisZLeft(std::shared_ptr<SDLPP::Renderer> renderer, std::shared_ptr<SDLPP::Scene> scene) {
TetrisPiece tetrisZLeft( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Scene > scene ) {
TetrisPiece retPiece{};
auto color = "#FFFFFF";
auto outline = "#AAAAAA";
retPiece.addPiece(createTetrisBlock(0.46, 0.16, color, outline, renderer, scene), -1, 0);
retPiece.addPiece(createTetrisBlock(0.5, 0.16, color, outline, renderer, scene), 0, 0);
retPiece.addPiece(createTetrisBlock(0.5, 0.20, color, outline, renderer, scene), 0, 1);
retPiece.addPiece(createTetrisBlock(0.54, 0.20, color, outline, renderer, scene), 1, 1);
retPiece.addPiece(
createTetrisBlock( 0.46, 0.16, color, outline, renderer, scene ), -1,
0 );
retPiece.addPiece(
createTetrisBlock( 0.5, 0.16, color, outline, renderer, scene ), 0, 0 );
retPiece.addPiece(
createTetrisBlock( 0.5, 0.20, color, outline, renderer, scene ), 0, 1 );
retPiece.addPiece(
createTetrisBlock( 0.54, 0.20, color, outline, renderer, scene ), 1,
1 );
retPiece.setDefPos( 0.5, 0.16 );
return retPiece;
}
std::vector<TetrisPiece (*)(std::shared_ptr<SDLPP::Renderer>, std::shared_ptr<SDLPP::Scene>)> tetrisFunctions = {
tetrisBrick,
tetrisT,
tetrisLRight,
tetrisZRight,
tetrisLine,
tetrisLLeft,
tetrisZLeft,
std::vector< TetrisPiece ( * )( std::shared_ptr< SDLPP::Renderer >,
std::shared_ptr< SDLPP::Scene > ) >
tetrisFunctions = {
tetrisBrick, tetrisT, tetrisLRight, tetrisZRight,
tetrisLine, tetrisLLeft, tetrisZLeft,
};
void addStuff( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer > &r ) {
auto bg = std::make_shared<SDLPP::RectangleRender>(0,0,10,10,r,"#101090FF", true);
auto bg = std::make_shared< SDLPP::RectangleRender >( 0, 0, 10, 10, r,
"#101090FF", true );
bg->setPermanent( true );
scene.addObject( bg );
auto left_barrier = std::make_shared<SDLPP::RectangleRender>(0.28,0,0.02,1,r,"#FF000080", true);
auto left_barrier = std::make_shared< SDLPP::RectangleRender >(
0.28, 0, 0.02, 1, r, "#FF000080", true );
left_barrier->centerX();
scene.addObject( left_barrier );
auto right_barrier = std::make_shared<SDLPP::RectangleRender>(0.7,0,0.02,1,r,"#FF000080", true);
auto right_barrier = std::make_shared< SDLPP::RectangleRender >(
0.7, 0, 0.02, 1, r, "#FF000080", true );
right_barrier->centerX();
scene.addObject( right_barrier );
auto bottom_barrier = std::make_shared<SDLPP::RectangleRender>(0.28,1,0.44,0.02,r,"#FF000080", true);
auto bottom_barrier = std::make_shared< SDLPP::RectangleRender >(
0.28, 1, 0.44, 0.02, r, "#FF000080", true );
bottom_barrier->centerX();
scene.addObject( bottom_barrier );
auto tetris = std::make_shared<SDLPP::TextRenderer>(0.4, 0, 0.2, 0.1, r, *font, "TETRIS", "FFFFFF", "000000", 5);
auto tetris = std::make_shared< SDLPP::TextRenderer >(
0.4, 0, 0.2, 0.1, r, *font, "TETRIS", "FFFFFF", "000000", 5 );
tetris->centerX();
scene.addObject( tetris );
auto next = std::make_shared<SDLPP::TextRenderer>(0.8, 0.35, 0.2, 0.1, r, *font, "NEXT", "FFFFFF", "000000", 5, SDLPP_TEXT_CENTER);
auto next = std::make_shared< SDLPP::TextRenderer >(
0.8, 0.35, 0.2, 0.1, r, *font, "NEXT", "FFFFFF", "000000", 5,
SDLPP_TEXT_CENTER );
next->centerX();
scene.addObject( next );
double posy = 1;
auto gameover = std::make_shared<SDLPP::RectangleRender>(0.5,0,0,0.195, r);
auto gameover =
std::make_shared< SDLPP::RectangleRender >( 0.5, 0, 0, 0.195, r );
auto gameover_collision = SDLPP::Rect( -1, 0, -1, 1 );
gameover_collision.setInfinite();
gameover->addCollision( gameover_collision );
gameover->setId( GAME_OVER );
gameover->setColiderColor( "FF0000" );
scene.addObject( gameover );
auto score_text = std::make_shared<SDLPP::TextRenderer>(0.8, 0.1, 0.2, 0.1, r, *font, "SCORE", "#FFFFFF", "#000000", 5, SDLPP_TEXT_CENTER);
auto score_text = std::make_shared< SDLPP::TextRenderer >(
0.8, 0.1, 0.2, 0.1, r, *font, "SCORE", "#FFFFFF", "#000000", 5,
SDLPP_TEXT_CENTER );
score_text->centerX();
scene.addObject( score_text );
score_texture = std::make_shared<SDLPP::TextRenderer>(0.8, 0.2, 0.2, 0.1, r, *font, std::to_string(score), "FFFFFF", "000000", 5, SDLPP_TEXT_TOP);
score_texture = std::make_shared< SDLPP::TextRenderer >(
0.8, 0.2, 0.2, 0.1, r, *font, std::to_string( score ), "FFFFFF",
"000000", 5, SDLPP_TEXT_TOP );
score_texture->centerX();
scene.addObject( score_texture );
for ( int i = 0; i < 20; i++ ) {
posy -= 0.04;
auto colider = std::make_shared<SDLPP::RectangleRender>(0.3, posy, 0.04, 0.04, r);
auto colider = std::make_shared< SDLPP::RectangleRender >(
0.3, posy, 0.04, 0.04, r );
auto colider_colider = SDLPP::Rect( -1, 0.1, -1, 0.8 );
colider_colider.setInfinite();
colider->addCollision( colider_colider );
@ -259,11 +331,13 @@ void addStuff(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) {
}
void updateScore() {
score_texture->setText(*font, std::to_string(score), "#FFFFFF", "#000000", 5);
score_texture->setText( *font, std::to_string( score ), "#FFFFFF",
"#000000", 5 );
}
void addPause( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer > &r ) {
auto bg = std::make_shared<SDLPP::RectangleRender>(0,0,10,10,r,"#00000080", true);
auto bg = std::make_shared< SDLPP::RectangleRender >( 0, 0, 10, 10, r,
"#00000080", true );
bg->setId( 123 );
bg->setPermanent( true );
scene.addObject( bg );
@ -272,13 +346,15 @@ void addPause(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) {
y->setId( 0 );
y->centerX();
scene.addObject( y );
auto resume = std::make_shared<SDLPP::TextRenderer>(0.4, 0.5, 0.2, 0.1, r);
auto resume =
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.5, 0.2, 0.1, r );
resume->setText( *font, "Resume", "#FFFFFF", "#000000", 5 );
resume->setColor( "#FFFFFF40" );
resume->centerX();
scene.addObject( resume );
pause_options.push_back( resume );
auto quit = std::make_shared<SDLPP::TextRenderer>(0.4, 0.7, 0.2, 0.1, r);
auto quit =
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.7, 0.2, 0.1, r );
quit->setText( *font, "Quit Game", "#FFFFFF", "#000000", 5 );
quit->centerX();
scene.addObject( quit );
@ -299,8 +375,7 @@ void handleKeyDown(SDL_Keycode key, SDLPP::Scene &scene) {
pause_scene->updateSizeAndPosition();
std::thread pauseThread( doInputPause );
pauseThread.detach();
}
break;
} break;
case SDLK_LEFT:
case SDLK_a:
for ( auto &x : cur_object.getObjects() ) {
@ -354,7 +429,8 @@ void handleKeyDown(SDL_Keycode key, SDLPP::Scene &scene) {
cur_object.rotate();
break;
case SDLK_r:
scene.getRenderer().setRenderColiders(!scene.getRenderer().getRenderColiders());
scene.getRenderer().setRenderColiders(
!scene.getRenderer().getRenderColiders() );
default:
break;
}
@ -373,10 +449,10 @@ void handleKeyDownPause(SDL_Keycode key) {
active_scene->setPrevTicks( SDL_GetTicks() );
std::thread inputThread( doInput, active_scene );
inputThread.detach();
}
break;
} break;
case SDLK_r:
active_scene->getRenderer().setRenderColiders(!active_scene->getRenderer().getRenderColiders());
active_scene->getRenderer().setRenderColiders(
!active_scene->getRenderer().getRenderColiders() );
break;
case SDLK_s:
case SDLK_DOWN:
@ -401,8 +477,7 @@ void handleKeyDownPause(SDL_Keycode key) {
active_scene->setPrevTicks( SDL_GetTicks() );
std::thread inputThread( doInput, active_scene );
inputThread.detach();
}
break;
} break;
case 1:
quitGame();
default:
@ -579,7 +654,8 @@ int main() {
SDL_setFramerate( &gFPS, 60 );
std::thread inputThread( doInput, main_scene );
inputThread.detach();
next_object = tetrisFunctions[std::rand()/((RAND_MAX + 1u)/7)](renderer, main_scene);
next_object = tetrisFunctions[std::rand() / ( ( RAND_MAX + 1u ) / 7 )](
renderer, main_scene );
next_object.setPos( 0.9, 0.5 );
while ( !quit ) {
SDL_framerateDelay( &gFPS );
@ -591,7 +667,9 @@ int main() {
std::lock_guard< std::mutex > guard( movement_mutex );
cur_object = next_object;
cur_object.setPos( 0.5, 0.16 );
next_object = tetrisFunctions[std::rand()/((RAND_MAX + 1u)/7)](renderer, main_scene);
next_object =
tetrisFunctions[std::rand() / ( ( RAND_MAX + 1u ) / 7 )](
renderer, main_scene );
next_object.setPos( 0.9, 0.5 );
}
}