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

View File

@ -5,15 +5,18 @@
bool SDLPP::init() { bool SDLPP::init() {
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ) { 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; return false;
} }
if ( IMG_Init( IMG_INIT_PNG ) != IMG_INIT_PNG ) { 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 false;
} }
if ( TTF_Init() == -1 ) { 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 false;
} }
return true; return true;
@ -21,11 +24,13 @@ bool SDLPP::init() {
bool SDLPP::init( uint32_t SDL_OPTIONS ) { bool SDLPP::init( uint32_t SDL_OPTIONS ) {
if ( SDL_Init( SDL_OPTIONS ) < 0 ) { 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; return false;
} }
if ( IMG_Init( IMG_INIT_PNG ) != IMG_INIT_PNG ) { 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 false;
} }
return true; return true;
@ -33,35 +38,45 @@ bool SDLPP::init(uint32_t SDL_OPTIONS) {
bool SDLPP::init( uint32_t SDL_OPTIONS, int IMAGE_OPTIONS ) { bool SDLPP::init( uint32_t SDL_OPTIONS, int IMAGE_OPTIONS ) {
if ( SDL_Init( SDL_OPTIONS ) < 0 ) { 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; return false;
} }
if ( IMG_Init( IMAGE_OPTIONS ) != IMAGE_OPTIONS ) { 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 false;
} }
return true; return true;
} }
void SDLPP::CircleRender::render() { 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 // only rectangles for now
bool intersects(const SDLPP::CollisionPolygon &p1, const SDLPP::CollisionPolygon &p2) { bool intersects( const SDLPP::CollisionPolygon &p1,
return !(p1.rightmost() < p2.leftmost() || p2.rightmost() < p1.leftmost() || const SDLPP::CollisionPolygon &p2 ) {
return !(
p1.rightmost() < p2.leftmost() || p2.rightmost() < p1.leftmost() ||
p1.topmost() > p2.bottommost() || p2.topmost() > p1.bottommost() ); 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 ileft = infinite.leftmost();
int iright = infinite.rightmost(); int iright = infinite.rightmost();
int itop = infinite.topmost(); int itop = infinite.topmost();
int ibottom = infinite.bottommost(); int ibottom = infinite.bottommost();
bool ret = ileft != -1 && ileft <= other.rightmost() && ileft >= other.leftmost(); bool ret =
ret |= iright != -1 && iright >= other.leftmost() && iright <= other.rightmost(); 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 |= 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; ret |= ileft == -1 && iright == -1 && itop == -1 && ibottom == -1;
return ret; return ret;
} }
@ -93,9 +108,11 @@ bool SDLPP::Circle::colidesWith(const SDLPP::CollisionPolygon &other) const {
int centerx = getX(); int centerx = getX();
int centery = getY(); int centery = getY();
if ( other.topmost() <= centery && other.bottommost() >= centery ) { 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 ) { } 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; int pointx = 0, pointy = 0;
if ( centerx > other.rightmost() ) { if ( centerx > other.rightmost() ) {
@ -108,7 +125,8 @@ bool SDLPP::Circle::colidesWith(const SDLPP::CollisionPolygon &other) const {
} else { } else {
pointy = other.bottommost(); 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; 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; 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] == '#' )
@ -149,7 +168,8 @@ std::tuple<int, int, int, int> SDLPP::getColorsHEX(const std::string &color) {
return { red, green, blue, alpha }; 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{}; SDL_Color ret_color{};
ret_color.r = std::get< 0 >( tuple ); ret_color.r = std::get< 0 >( tuple );
ret_color.g = std::get< 1 >( tuple ); ret_color.g = std::get< 1 >( tuple );
@ -163,6 +183,7 @@ SDL_Color SDLPP::getSDLColorHEX(const std::string &color) {
return getSDLColorTuple( color_tuple ); 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 }; 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 { 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,
Window(const std::string &window_name) : Window(window_name, 640, 480, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED) { SDL_WINDOWPOS_UNDEFINED ) {}
} Window( const std::string &window_name )
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_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, Window( const std::string &window_name, uint32_t width, uint32_t height,
uint32_t posx, uint32_t posy ) { 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 ) { 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"; throw "Couldn't create window";
} }
} }
@ -49,6 +55,7 @@ public:
SDL_Window *getWindowPtr() { SDL_Window *getWindowPtr() {
return window; return window;
} }
private: private:
SDL_Window *window = NULL; SDL_Window *window = NULL;
}; };
@ -57,9 +64,11 @@ class Renderer {
public: public:
Renderer() = delete; Renderer() = delete;
Renderer( Window &window ) { Renderer( Window &window ) {
renderer = SDL_CreateRenderer(window.getWindowPtr(), -1, SDL_RENDERER_ACCELERATED); renderer = SDL_CreateRenderer( window.getWindowPtr(), -1,
SDL_RENDERER_ACCELERATED );
if ( renderer == NULL ) { 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"; throw "Couldn't create renderer";
} }
SDL_SetRenderDrawColor( renderer, 0xFF, 0xFF, 0xFF, 0xFF ); SDL_SetRenderDrawColor( renderer, 0xFF, 0xFF, 0xFF, 0xFF );
@ -83,11 +92,13 @@ public:
} }
int getSmallerSide() const { int getSmallerSide() const {
auto dimensions = getDimensions(); auto dimensions = getDimensions();
return dimensions.first < dimensions.second ? dimensions.first : dimensions.second; return dimensions.first < dimensions.second ? dimensions.first
: dimensions.second;
} }
int getLargerSide() const { int getLargerSide() const {
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 ) { void setBlendMode( SDL_BlendMode blendMode ) {
SDL_SetRenderDrawBlendMode( renderer, blendMode ); SDL_SetRenderDrawBlendMode( renderer, blendMode );
@ -98,6 +109,7 @@ public:
bool getRenderColiders() { bool getRenderColiders() {
return render_coliders; return render_coliders;
} }
private: private:
SDL_Renderer *renderer = NULL; SDL_Renderer *renderer = NULL;
bool render_coliders = false; bool render_coliders = false;
@ -109,7 +121,8 @@ public:
Font( const std::string &font, int size ) { Font( const std::string &font, int size ) {
font_ptr = TTF_OpenFont( font.c_str(), size ); font_ptr = TTF_OpenFont( font.c_str(), size );
if ( font_ptr == NULL ) { 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"; throw "TTF_OpenFont error";
} }
} }
@ -134,6 +147,7 @@ public:
void setHinting( int hinting ) { void setHinting( int hinting ) {
TTF_SetFontHinting( font_ptr, hinting ); TTF_SetFontHinting( font_ptr, hinting );
} }
private: private:
TTF_Font *font_ptr; TTF_Font *font_ptr;
}; };
@ -141,36 +155,51 @@ private:
class Texture { class Texture {
public: public:
Texture() = delete; Texture() = delete;
Texture(std::shared_ptr<Renderer> &renderer, const std::string &img_path) : Texture(renderer, img_path, "") {} Texture( std::shared_ptr< Renderer > &renderer,
Texture(std::shared_ptr<Renderer> &renderer, const std::string &img_path, const std::string &color_key) { 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() ); SDL_Surface *surface = IMG_Load( img_path.c_str() );
if ( surface == NULL ) { 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"; throw "IMG_Load error";
} }
if ( !color_key.empty() ) { if ( !color_key.empty() ) {
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 ) ) );
} }
setTextureFromSurface( renderer, surface ); 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 ) { if ( outline_size != -1 ) {
font.setOutline( outline_size ); font.setOutline( outline_size );
} }
int og_outline = 0; int og_outline = 0;
SDL_Surface *bg_surface = NULL; SDL_Surface *bg_surface = NULL;
if ( ( og_outline = font.getOutline() ) != 0 ) { 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 ) { 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"; throw "TTF_RenderUTF8_Shaded error";
} }
font.setOutline( 0 ); 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 ) { 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"; throw "TTF_RenderUTF8_Shaded error";
} }
if ( og_outline != 0 ) { if ( og_outline != 0 ) {
@ -190,11 +219,15 @@ public:
SDL_Texture *getTexturePtr() { SDL_Texture *getTexturePtr() {
return texture; return texture;
} }
private: private:
void setTextureFromSurface(std::shared_ptr<Renderer> &renderer, SDL_Surface *surface) { void setTextureFromSurface( std::shared_ptr< Renderer > &renderer,
texture = SDL_CreateTextureFromSurface(renderer->getRendererPtr(), surface); SDL_Surface *surface ) {
texture =
SDL_CreateTextureFromSurface( renderer->getRendererPtr(), surface );
if ( texture == NULL ) { 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"; throw "Texture error";
} }
SDL_FreeSurface( surface ); SDL_FreeSurface( surface );
@ -213,8 +246,12 @@ public:
virtual ~CollisionPolygon() {} virtual ~CollisionPolygon() {}
virtual bool colidesWith( const CollisionPolygon &other ) const = 0; virtual bool colidesWith( const CollisionPolygon &other ) const = 0;
virtual bool isCircle() const = 0; virtual bool isCircle() const = 0;
virtual bool isInfinite() const { return infinite; } virtual bool isInfinite() const {
virtual void setInfinite() { infinite = true; } return infinite;
}
virtual void setInfinite() {
infinite = true;
}
virtual int topmost() const = 0; virtual int topmost() const = 0;
virtual int bottommost() const = 0; virtual int bottommost() const = 0;
virtual int leftmost() const = 0; virtual int leftmost() const = 0;
@ -223,7 +260,8 @@ 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,int> &color) = 0; virtual void render( Renderer &renderer,
const std::tuple< int, int, int, int > &color ) = 0;
virtual void render( Renderer &renderer ) = 0; virtual void render( Renderer &renderer ) = 0;
int getX() const { int getX() const {
return position_x; return position_x;
@ -237,6 +275,7 @@ public:
void setOutlineColor( const std::string &color ) { void setOutlineColor( const std::string &color ) {
sdl_outline = getSDLColorHEX( color ); sdl_outline = getSDLColorHEX( color );
} }
protected: protected:
double original_x; double original_x;
double original_y; double original_y;
@ -263,11 +302,14 @@ public:
virtual int collisionWidth() = 0; virtual int collisionWidth() = 0;
virtual int collisionHeight() = 0; virtual int collisionHeight() = 0;
virtual void specialAction( int code ) = 0; virtual void specialAction( int code ) = 0;
virtual std::pair<std::pair<double,double>,std::pair<double,double>> getDoubleRect() = 0; virtual std::pair< std::pair< double, double >,
std::pair< double, double > >
getDoubleRect() = 0;
virtual void setPos( double x, double y ) = 0; virtual void setPos( double x, double y ) = 0;
virtual std::pair< double, double > getPos() = 0; virtual std::pair< double, double > getPos() = 0;
bool colidesWith( const RenderObject &other ) const { bool colidesWith( const RenderObject &other ) const {
if(!hasCollisions() || !other.hasCollisions() || getHidden() || other.getHidden()) { if ( !hasCollisions() || !other.hasCollisions() || getHidden() ||
other.getHidden() ) {
return false; return false;
} }
for ( const auto &x : collisions ) { for ( const auto &x : collisions ) {
@ -278,15 +320,17 @@ public:
} }
return false; return false;
} }
template<class T> template < class T > void addCollision( const T &p ) {
void addCollision(const T &p) {
collisions.push_back( std::make_shared< 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 { bool hasCollisions() const {
return !collisions.empty(); return !collisions.empty();
} }
const std::vector<std::shared_ptr<CollisionPolygon>> &getCollisions() const { const std::vector< std::shared_ptr< CollisionPolygon > > &
getCollisions() const {
return collisions; return collisions;
} }
virtual void setTexture( std::shared_ptr< Texture > &t ) { virtual void setTexture( std::shared_ptr< Texture > &t ) {
@ -295,8 +339,12 @@ public:
virtual void setTexture( const std::string &img_path ) { virtual void setTexture( const std::string &img_path ) {
texture = std::make_shared< Texture >( renderer, img_path ); texture = std::make_shared< Texture >( renderer, img_path );
} }
virtual void setTexture(Font &font, const std::string &text, const std::string &color = "FFFFFF", const std::string &outline_color = "000000", int outline_size = -1) { virtual void setTexture( Font &font, const std::string &text,
texture = std::make_shared<Texture>(renderer, font, text, color, outline_color, outline_size); 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 setColor( const std::string &color ) = 0;
virtual void setOutlineColor( const std::string &color ) = 0; virtual void setOutlineColor( const std::string &color ) = 0;
@ -362,6 +410,7 @@ public:
return permanent; return permanent;
} }
virtual void centerX() = 0; virtual void centerX() = 0;
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;
@ -376,6 +425,7 @@ protected:
std::tuple< int, int, int, int > colider_color = { 0x00, 0xFF, 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; bool permanent = false;
private: private:
void setSceneID( int id ) { void setSceneID( int id ) {
scene_id = id; scene_id = id;
@ -386,7 +436,8 @@ private:
class Scene { class Scene {
public: public:
Scene( std::shared_ptr< Renderer > &r ) : renderer( r ) { 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(); prev_ticks = SDL_GetTicks();
} }
void addObject( const std::shared_ptr< RenderObject > &obj ) { void addObject( const std::shared_ptr< RenderObject > &obj ) {
@ -406,7 +457,8 @@ public:
if ( rect.first.first < leftmost_rect.first.first ) if ( rect.first.first < leftmost_rect.first.first )
leftmost_obj = obj; leftmost_obj = obj;
auto rightmost_rect = rightmost_obj->getDoubleRect(); 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; rightmost_obj = obj;
} }
render_mutex.unlock(); render_mutex.unlock();
@ -427,7 +479,8 @@ public:
prev_ticks = now_ticks; prev_ticks = now_ticks;
render_mutex.unlock(); render_mutex.unlock();
} }
std::vector<std::shared_ptr<RenderObject>> getCollisions(RenderObject &r) { std::vector< std::shared_ptr< RenderObject > >
getCollisions( RenderObject &r ) {
if ( r.getHidden() ) if ( r.getHidden() )
return {}; return {};
std::vector< std::shared_ptr< RenderObject > > ret{}; std::vector< std::shared_ptr< RenderObject > > ret{};
@ -438,12 +491,15 @@ public:
} }
return ret; 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() ) if ( r.getHidden() )
return {}; return {};
std::vector< std::shared_ptr< RenderObject > > ret{}; std::vector< std::shared_ptr< RenderObject > > ret{};
for ( const auto &x : collisionObjects ) { 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 ); ret.push_back( x );
} }
} }
@ -455,7 +511,8 @@ public:
if ( clear_scene ) if ( clear_scene )
SDL_RenderClear( renderer->getRendererPtr() ); SDL_RenderClear( renderer->getRendererPtr() );
if ( background && background->getTexturePtr() ) 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 ) { for ( const auto &x : renderObjects ) {
x->render(); x->render();
} }
@ -476,7 +533,9 @@ public:
for ( auto &x : renderObjects ) { for ( auto &x : renderObjects ) {
x->updateSizeAndPosition(); x->updateSizeAndPosition();
for ( auto &col : x->getCollisions() ) { 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(); render_mutex.unlock();
@ -513,6 +572,7 @@ public:
void setPrevTicks( int ticks ) { void setPrevTicks( int ticks ) {
prev_ticks = ticks; prev_ticks = ticks;
} }
private: private:
void checkKilled() { void checkKilled() {
render_mutex.lock(); render_mutex.lock();
@ -547,18 +607,24 @@ public:
} }
virtual ~Rect() {} virtual ~Rect() {}
virtual bool colidesWith( const CollisionPolygon &other ) const override; 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 { 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 { 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 { 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 { 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 { virtual void updateCollision( int x, int y, int w, int h ) override {
position_x = original_x * w + x; position_x = original_x * w + x;
@ -566,22 +632,31 @@ 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,int> &color) override { virtual void
render( Renderer &renderer,
const std::tuple< int, int, int, int > &color ) override {
auto rect = getRect(); auto rect = getRect();
// outline with desired color at 50% opacity // 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 ); SDL_RenderDrawRect( renderer.getRendererPtr(), &rect );
// fill with desired color at 25% opacity // 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 ); SDL_RenderFillRect( renderer.getRendererPtr(), &rect );
} }
virtual void render( Renderer &renderer ) override { virtual void render( Renderer &renderer ) override {
auto rect = getRect(); auto rect = getRect();
SDL_SetRenderDrawColor(renderer.getRendererPtr(), sdl_color.r, sdl_color.g, sdl_color.b, sdl_color.a); SDL_SetRenderDrawColor( renderer.getRendererPtr(), sdl_color.r,
sdl_color.g, sdl_color.b, sdl_color.a );
SDL_RenderFillRect( renderer.getRendererPtr(), &rect ); SDL_RenderFillRect( renderer.getRendererPtr(), &rect );
SDL_SetRenderDrawColor(renderer.getRendererPtr(), sdl_outline.r, sdl_outline.g, sdl_outline.b, sdl_outline.a); SDL_SetRenderDrawColor( renderer.getRendererPtr(), sdl_outline.r,
sdl_outline.g, sdl_outline.b, sdl_outline.a );
SDL_RenderDrawRect( renderer.getRendererPtr(), &rect ); SDL_RenderDrawRect( renderer.getRendererPtr(), &rect );
} }
private: private:
SDL_Rect getRect() { SDL_Rect getRect() {
if ( !isInfinite() ) if ( !isInfinite() )
@ -616,18 +691,31 @@ public:
} }
virtual ~Circle() {} virtual ~Circle() {}
virtual bool colidesWith( const CollisionPolygon &other ) const override; virtual bool colidesWith( const CollisionPolygon &other ) const override;
virtual bool isCircle() const override { return true; } virtual bool isCircle() const override {
virtual int topmost() const override { return getY() - rad_; } return true;
virtual int bottommost() const override { return getY() + rad_; }; }
virtual int leftmost() const override { return getX() - rad_; } virtual int topmost() const override {
virtual int rightmost() const override { return getX() + rad_; } 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 { virtual void updateCollision( int x, int y, int w, int h ) override {
position_x = original_x * w + x; position_x = original_x * w + x;
position_y = original_y * h + y; position_y = original_y * h + y;
rad_ = original_rad * w; rad_ = original_rad * w;
} }
virtual void render(Renderer &renderer, const std::tuple<int,int,int,int> &color) override { virtual void
std::vector<int> rect = {leftmost(), topmost(), rightmost(), bottommost()}; 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_x = getX();
auto center_y = getY(); auto center_y = getY();
auto radsq = rad_ * rad_; auto radsq = rad_ * rad_;
@ -635,20 +723,35 @@ public:
auto xdiff = center_x - i; auto xdiff = center_x - i;
auto xdist = xdiff * xdiff; auto xdist = xdiff * xdiff;
auto allowed_rad = sqrt( radsq - xdist ); auto allowed_rad = sqrt( radsq - xdist );
SDL_SetRenderDrawColor(renderer.getRendererPtr(), std::get<0>(color), std::get<1>(color), std::get<2>(color), 0x40); SDL_SetRenderDrawColor(
SDL_RenderDrawLine(renderer.getRendererPtr(), i, center_y - allowed_rad, i, center_y + allowed_rad); renderer.getRendererPtr(), std::get< 0 >( color ),
SDL_SetRenderDrawColor(renderer.getRendererPtr(), std::get<0>(color), std::get<1>(color), std::get<2>(color), 0x80); std::get< 1 >( color ), std::get< 2 >( color ), 0x40 );
SDL_RenderDrawLine(renderer.getRendererPtr(), i, center_y - allowed_rad, i, center_y - allowed_rad + 2); SDL_RenderDrawLine( renderer.getRendererPtr(), i,
SDL_RenderDrawLine(renderer.getRendererPtr(), i, center_y + allowed_rad, i, center_y + allowed_rad - 2); 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_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,
SDL_RenderDrawLine(renderer.getRendererPtr(), center_x, center_y, center_x, center_y + rad_); center_x + rad_, center_y );
SDL_RenderDrawLine(renderer.getRendererPtr(), center_x, center_y, center_x - rad_, center_y); SDL_RenderDrawLine( renderer.getRendererPtr(), center_x, center_y,
SDL_RenderDrawLine(renderer.getRendererPtr(), center_x, center_y, center_x, center_y - rad_); 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 { 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_x = getX();
auto center_y = getY(); auto center_y = getY();
auto radsq = rad_ * rad_; auto radsq = rad_ * rad_;
@ -656,13 +759,23 @@ public:
auto xdiff = center_x - i; auto xdiff = center_x - i;
auto xdist = xdiff * xdiff; auto xdist = xdiff * xdiff;
auto allowed_rad = sqrt( radsq - xdist ); auto allowed_rad = sqrt( radsq - xdist );
SDL_SetRenderDrawColor(renderer.getRendererPtr(), sdl_color.r, sdl_color.g, sdl_color.b, sdl_color.a); SDL_SetRenderDrawColor( renderer.getRendererPtr(), sdl_color.r,
SDL_RenderDrawLine(renderer.getRendererPtr(), i, center_y - allowed_rad, i, center_y + allowed_rad); sdl_color.g, sdl_color.b, sdl_color.a );
SDL_SetRenderDrawColor(renderer.getRendererPtr(), sdl_outline.r, sdl_outline.g, sdl_outline.b, sdl_outline.a); SDL_RenderDrawLine( renderer.getRendererPtr(), i,
SDL_RenderDrawLine(renderer.getRendererPtr(), i, center_y - allowed_rad, i, center_y - allowed_rad + 2); center_y - allowed_rad, i,
SDL_RenderDrawLine(renderer.getRendererPtr(), i, center_y + allowed_rad, i, center_y + allowed_rad - 2); center_y + allowed_rad );
SDL_SetRenderDrawColor( renderer.getRendererPtr(), sdl_outline.r,
sdl_outline.g, sdl_outline.b,
sdl_outline.a );
SDL_RenderDrawLine( renderer.getRendererPtr(), i,
center_y - allowed_rad, i,
center_y - allowed_rad + 2 );
SDL_RenderDrawLine( renderer.getRendererPtr(), i,
center_y + allowed_rad, i,
center_y + allowed_rad - 2 );
} }
} }
private: private:
int getRadius() const { int getRadius() const {
return rad_; return rad_;
@ -675,17 +788,25 @@ class RectangleRender : public RenderObject {
public: public:
RectangleRender() = delete; RectangleRender() = delete;
virtual ~RectangleRender(){}; virtual ~RectangleRender(){};
RectangleRender(double x, double y, double w, double h, std::shared_ptr<Renderer> &r) : RenderObject(r) { RectangleRender( double x, double y, double w, double h,
std::shared_ptr< Renderer > &r )
: RenderObject( r ) {
og_x = x_ = x; og_x = x_ = x;
og_y = y_ = y; og_y = y_ = y;
og_w = w_ = w; og_w = w_ = w;
og_h = h_ = h; og_h = h_ = h;
updateSizeAndPosition(); updateSizeAndPosition();
} }
RectangleRender(double x, double y, double w, double h, std::shared_ptr<Renderer> &r, std::shared_ptr<Texture> &t) : RectangleRender(x, y, w, h, r) { RectangleRender( double x, double y, double w, double h,
std::shared_ptr< Renderer > &r,
std::shared_ptr< Texture > &t )
: RectangleRender( x, y, w, h, r ) {
setTexture( t ); setTexture( t );
} }
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 ) { if ( !is_polygon ) {
setTexture( img_or_color ); setTexture( img_or_color );
} else { } else {
@ -695,14 +816,16 @@ public:
virtual void setColor( const std::string &color ) override { virtual void setColor( const std::string &color ) override {
if ( !polygon ) { if ( !polygon ) {
polygon = std::make_shared< Rect >( 0, 0, 1, 1 ); polygon = std::make_shared< Rect >( 0, 0, 1, 1 );
polygon->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight()); polygon->updateCollision( collisionPushX(), collisionPushY(),
collisionWidth(), collisionHeight() );
} }
polygon->setColor( color ); polygon->setColor( color );
} }
virtual void setOutlineColor( const std::string &color ) override { virtual void setOutlineColor( const std::string &color ) override {
if ( !polygon ) { if ( !polygon ) {
polygon = std::make_shared< Rect >( 0, 0, 1, 1 ); polygon = std::make_shared< Rect >( 0, 0, 1, 1 );
polygon->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight()); polygon->updateCollision( collisionPushX(), collisionPushY(),
collisionWidth(), collisionHeight() );
} }
polygon->setOutlineColor( color ); polygon->setOutlineColor( color );
} }
@ -712,9 +835,11 @@ public:
if ( polygon ) if ( polygon )
polygon->render( *renderer ); polygon->render( *renderer );
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() && !getHidden()) { if ( hasCollisions() && renderer->getRenderColiders() &&
!getHidden() ) {
for ( const auto &col : getCollisions() ) for ( const auto &col : getCollisions() )
col->render( *renderer, colider_color ); col->render( *renderer, colider_color );
} }
@ -722,8 +847,12 @@ public:
virtual void move( int ticks ) override { virtual void move( int ticks ) override {
if ( permanent ) if ( permanent )
return; return;
auto addx = static_cast<double>(movementSpeed * movementDirection.first)*(static_cast<double>(ticks)/1000); auto addx =
auto addy = static_cast<double>(movementSpeed * movementDirection.second)*(static_cast<double>(ticks)/1000); 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 ) ) if ( std::isnan( addx ) || std::isnan( addy ) )
return; return;
@ -735,7 +864,9 @@ public:
updateSizeAndPosition(); updateSizeAndPosition();
} }
virtual void custom_move( int /*UNUSED*/ ) override {} 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 } }; return { { og_x, og_y }, { og_w, og_h } };
} }
virtual void setPos( double x, double y ) override { virtual void setPos( double x, double y ) override {
@ -778,9 +909,11 @@ public:
rect.w = w_ * dimension; rect.w = w_ * dimension;
rect.h = h_ * dimension; rect.h = h_ * dimension;
if ( polygon ) if ( polygon )
polygon->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight()); polygon->updateCollision( collisionPushX(), collisionPushY(),
collisionWidth(), collisionHeight() );
for ( auto &x : collisions ) { for ( auto &x : collisions ) {
x->updateCollision(collisionPushX(), collisionPushY(), collisionWidth(), collisionHeight()); x->updateCollision( collisionPushX(), collisionPushY(),
collisionWidth(), collisionHeight() );
} }
} }
virtual SDL_Rect getRect() override { virtual SDL_Rect getRect() override {
@ -790,6 +923,7 @@ public:
centerx = true; centerx = true;
updateSizeAndPosition(); updateSizeAndPosition();
} }
protected: protected:
void updateXY() { void updateXY() {
if ( !centerx ) { if ( !centerx ) {
@ -800,7 +934,8 @@ protected:
auto width = renderer->getWidth(); auto width = renderer->getWidth();
auto height = renderer->getHeight(); auto height = renderer->getHeight();
if ( width > height ) { if ( width > height ) {
auto multiplier = static_cast<double>(width)/static_cast<double>(height); auto multiplier = static_cast< double >( width ) /
static_cast< double >( height );
x_ = og_x + static_cast< double >( multiplier - 1 ) / 2; x_ = og_x + static_cast< double >( multiplier - 1 ) / 2;
} else { } else {
x_ = og_x; x_ = og_x;
@ -822,12 +957,22 @@ protected:
class TextRenderer : public RectangleRender { class TextRenderer : public RectangleRender {
public: public:
TextRenderer() = delete; 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,
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) { 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; position_flags = flags;
setText( font, text, color, outline_color, outline_size ); 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 ); setTexture( font, text, color, outline_color, outline_size );
updateDstRect(); updateDstRect();
} }
@ -840,9 +985,11 @@ public:
if ( polygon ) if ( polygon )
polygon->render( *renderer ); polygon->render( *renderer );
if ( texture != NULL ) 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() ) for ( const auto &col : getCollisions() )
col->render( *renderer, colider_color ); col->render( *renderer, colider_color );
} }
@ -851,18 +998,22 @@ public:
RectangleRender::updateSizeAndPosition(); RectangleRender::updateSizeAndPosition();
updateDstRect(); updateDstRect();
} }
private: private:
void updateDstRect() { void updateDstRect() {
if ( !texture ) if ( !texture )
return; return;
int text_width{}, text_height{}; 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 ) { if ( text_width < rect.w && text_height < rect.h ) {
dst_rect.w = text_width; dst_rect.w = text_width;
dst_rect.h = text_height; dst_rect.h = text_height;
} else { } else {
double x_div = static_cast<double>(text_width)/static_cast<double>(rect.w); double x_div = static_cast< double >( text_width ) /
double y_div = static_cast<double>(text_height)/static_cast<double>(rect.h); static_cast< double >( rect.w );
double y_div = static_cast< double >( text_height ) /
static_cast< double >( rect.h );
if ( x_div > y_div ) { if ( x_div > y_div ) {
dst_rect.w = text_width / x_div; dst_rect.w = text_width / x_div;
dst_rect.h = text_height / x_div; dst_rect.h = text_height / x_div;
@ -871,14 +1022,16 @@ private:
dst_rect.h = text_height / y_div; 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; dst_rect.x = rect.x + ( rect.w - dst_rect.w ) / 2;
} else if ( position_flags & SDLPP_TEXT_LEFT ) { } else if ( position_flags & SDLPP_TEXT_LEFT ) {
dst_rect.x = rect.x; dst_rect.x = rect.x;
} else if ( position_flags & SDLPP_TEXT_RIGHT ) { } else if ( position_flags & SDLPP_TEXT_RIGHT ) {
dst_rect.x = rect.x + rect.w - dst_rect.w; 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; dst_rect.y = rect.y + ( rect.h - dst_rect.h ) / 2;
} else if ( position_flags & SDLPP_TEXT_TOP ) { } else if ( position_flags & SDLPP_TEXT_TOP ) {
dst_rect.y = rect.y; dst_rect.y = rect.y;
@ -894,15 +1047,20 @@ class CircleRender : public RenderObject {
public: public:
CircleRender() = delete; CircleRender() = delete;
virtual ~CircleRender(){}; 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; x_ = x;
y_ = y; y_ = y;
rad_ = rad; 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 ); 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 ); auto texture = std::make_shared< Texture >( r, img_path );
setTexture( texture ); setTexture( texture );
} }
@ -919,6 +1077,7 @@ public:
virtual int collisionPushY() { virtual int collisionPushY() {
return y_; return y_;
} }
private: private:
int x_; int x_;
int y_; int y_;
@ -929,8 +1088,7 @@ 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 );
template<class T> template < class T > void testPolymorphism( T &obj );
void testPolymorphism(T &obj);
} // end of namespace SDLPP } // end of namespace SDLPP

View File

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