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

341
main.cpp
View File

@ -12,24 +12,27 @@
bool pause = false; bool pause = false;
int pause_select = 0; int pause_select = 0;
int pause_max = 1; int pause_max = 1;
std::vector<std::shared_ptr<SDLPP::RectangleRender>> pause_options; std::vector< std::shared_ptr< SDLPP::RectangleRender > > pause_options;
std::shared_ptr<SDLPP::Font> font; std::shared_ptr< SDLPP::Font > font;
std::shared_ptr<SDLPP::Scene> active_scene; std::shared_ptr< SDLPP::Scene > active_scene;
std::shared_ptr<SDLPP::Scene> pause_scene; std::shared_ptr< SDLPP::Scene > pause_scene;
void doInput(std::shared_ptr<SDLPP::Scene> scene); void doInput( std::shared_ptr< SDLPP::Scene > scene );
void doInputPause(); 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;
cur_gravity_time = max_gravity_time; cur_gravity_time = max_gravity_time;
} }
void setGravity(bool grav) { void setGravity( bool grav ) {
gravity_enabled = grav; gravity_enabled = grav;
} }
void enableGravity() { void enableGravity() {
@ -51,30 +54,34 @@ public:
void jump() { void jump() {
jumping = true; jumping = true;
} }
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 *
if( grav > max_gravity) ( max_gravity_time - cur_gravity_time ) / max_gravity_time;
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 ) {
// in this time frame, just hover in same place // in this time frame, just hover in same place
jump_ = jumping * grav; jump_ = jumping * grav;
} else if(division < 0.72) { } else if ( division < 0.72 ) {
// fall slowly // fall slowly
jump_ = jumping * jump_speed * division * 0.15; jump_ = jumping * jump_speed * division * 0.15;
if(jump_ > grav) if ( jump_ > grav )
jump_ = grav; jump_ = grav;
} }
if(jump_ < 0 || jump_ > jump_speed) if ( jump_ < 0 || jump_ > jump_speed )
jump_ = 0; jump_ = 0;
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,21 +93,26 @@ 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 {
if(code == DESTROYABLE_DESTROY) if ( code == DESTROYABLE_DESTROY )
startDestruction(); startDestruction();
} }
virtual void custom_move(int ticks) override { virtual void custom_move( int ticks ) override {
if(destruction) { if ( destruction ) {
destruction_countdown -= ticks; destruction_countdown -= ticks;
if(destruction_countdown <= 0) if ( destruction_countdown <= 0 )
destroy(); destroy();
} }
} }
private: private:
void startDestruction() { void startDestruction() {
destruction = true; destruction = true;
@ -109,70 +121,75 @@ private:
int destruction_countdown = 3000; int destruction_countdown = 3000;
}; };
std::shared_ptr<Player> player; 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,
bg->setId(123); "#ebdbb2FF", true );
bg->setPermanent(true); bg->setId( 123 );
scene.addObject(bg); bg->setPermanent( true );
std::shared_ptr<Destroyable> stone; scene.addObject( bg );
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 =
stone->addCollision(SDLPP::Rect(0,0,1,1)); std::make_shared< Destroyable >( posx, 0.5, 0.15, 0.1, r, 1000 );
stone->setColor("#222222FF"); stone->addCollision( SDLPP::Rect( 0, 0, 1, 1 ) );
stone->setId(STONE_ID); stone->setColor( "#222222FF" );
stone->setColiderColor("FF0000"); stone->setId( STONE_ID );
scene.addObject(stone); stone->setColiderColor( "FF0000" );
scene.addObject( stone );
posx += 0.45; posx += 0.45;
} }
auto x = std::make_shared<Player>(0,0,0.2,0.2, r); auto x = std::make_shared< Player >( 0, 0, 0.2, 0.2, r );
x->addCollision(SDLPP::Rect(0.3,0.7,0.05,0.31)); x->addCollision( SDLPP::Rect( 0.3, 0.7, 0.05, 0.31 ) );
x->addCollision(SDLPP::Rect(0.65,0.7,0.05,0.31)); x->addCollision( SDLPP::Rect( 0.65, 0.7, 0.05, 0.31 ) );
x->addCollision(SDLPP::Rect(0.2,0.3,0.6,0.45)); x->addCollision( SDLPP::Rect( 0.2, 0.3, 0.6, 0.45 ) );
x->addCollision(SDLPP::Circle(0.5,0.15,0.3)); x->addCollision( SDLPP::Circle( 0.5, 0.15, 0.3 ) );
x->setColor("E164B7"); x->setColor( "E164B7" );
x->setId(PLAYER_ID); x->setId( PLAYER_ID );
x->setColiderColor("00FF00"); x->setColiderColor( "00FF00" );
scene.addObject(x); scene.addObject( x );
player = x; player = x;
auto z = std::make_shared<SDLPP::RectangleRender>(0,2.5,0,0,r); auto z = std::make_shared< SDLPP::RectangleRender >( 0, 2.5, 0, 0, r );
auto z_col = SDLPP::Rect(-1,0,-1,-1); auto z_col = SDLPP::Rect( -1, 0, -1, -1 );
z_col.setInfinite(); z_col.setInfinite();
z->addCollision(z_col); z->addCollision( z_col );
z->setId(DEATH); z->setId( DEATH );
z->setColiderColor("FF00FF"); z->setColiderColor( "FF00FF" );
scene.addObject(z); scene.addObject( z );
auto y = std::make_shared<SDLPP::TextRenderer>(0, 0, 0.2, 0.1, r); auto y = std::make_shared< SDLPP::TextRenderer >( 0, 0, 0.2, 0.1, r );
y->setText(*font, "DEMO", "#FFFFFF", "#000000", 5); y->setText( *font, "DEMO", "#FFFFFF", "#000000", 5 );
y->setPermanent(true); y->setPermanent( true );
y->setId(123); y->setId( 123 );
scene.addObject(y); scene.addObject( y );
} }
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,
bg->setId(123); "#00000080", true );
bg->setPermanent(true); bg->setId( 123 );
scene.addObject(bg); bg->setPermanent( true );
auto y = std::make_shared<SDLPP::TextRenderer>(0.25, 0.1, 0.5, 0.3, r); scene.addObject( bg );
y->setText(*font, "PAUSED", "#FFFFFF", "#000000", 5); auto y = std::make_shared< SDLPP::TextRenderer >( 0.25, 0.1, 0.5, 0.3, r );
y->setId(0); y->setText( *font, "PAUSED", "#FFFFFF", "#000000", 5 );
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 =
resume->setText(*font, "Resume", "#FFFFFF", "#000000", 5); std::make_shared< SDLPP::TextRenderer >( 0.4, 0.5, 0.2, 0.1, r );
resume->setColor("#FFFFFF40"); resume->setText( *font, "Resume", "#FFFFFF", "#000000", 5 );
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 =
quit->setText(*font, "Quit Game", "#FFFFFF", "#000000", 5); std::make_shared< SDLPP::TextRenderer >( 0.4, 0.7, 0.2, 0.1, r );
quit->setText( *font, "Quit Game", "#FFFFFF", "#000000", 5 );
quit->centerX(); quit->centerX();
scene.addObject(quit); scene.addObject( quit );
pause_options.push_back(quit); pause_options.push_back( quit );
} }
void quitGame() { void quitGame() {
@ -180,25 +197,24 @@ void quitGame() {
quit = true; quit = true;
} }
void handleKeyDown(SDL_Keycode key, SDLPP::Scene &scene) { void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
switch(key) { switch ( key ) {
case SDLK_ESCAPE: { case SDLK_ESCAPE: {
pause = true; pause = true;
pause_scene->updateSizeAndPosition(); pause_scene->updateSizeAndPosition();
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;
case SDLK_d: case SDLK_d:
player->addMovement(1,0); player->addMovement( 1, 0 );
break; break;
case SDLK_SPACE: case SDLK_SPACE:
case SDLK_w: case SDLK_w:
if(!player->isJumping() && !pause) { if ( !player->isJumping() && !pause ) {
player->setLastStand(); player->setLastStand();
player->jump(); player->jump();
} }
@ -206,49 +222,49 @@ 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;
} }
} }
void handleKeyDownPause(SDL_Keycode key) { void handleKeyDownPause( SDL_Keycode key ) {
switch(key) { switch ( key ) {
case SDLK_ESCAPE: { case SDLK_ESCAPE: {
pause = false; pause = false;
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:
pause_options[pause_select]->unsetColor(); pause_options[pause_select]->unsetColor();
pause_select++; pause_select++;
if(pause_select > pause_max) if ( pause_select > pause_max )
pause_select = 0; pause_select = 0;
pause_options[pause_select]->setColor("FFFFFF40"); pause_options[pause_select]->setColor( "FFFFFF40" );
break; break;
case SDLK_w: case SDLK_w:
case SDLK_UP: case SDLK_UP:
pause_options[pause_select]->unsetColor(); pause_options[pause_select]->unsetColor();
pause_select--; pause_select--;
if(pause_select < 0) if ( pause_select < 0 )
pause_select = pause_max; pause_select = pause_max;
pause_options[pause_select]->setColor("FFFFFF40"); pause_options[pause_select]->setColor( "FFFFFF40" );
break; break;
case SDLK_RETURN: case SDLK_RETURN:
switch(pause_select) { switch ( pause_select ) {
case 0:{ case 0: {
pause = false; pause = false;
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:
@ -259,13 +275,13 @@ void handleKeyDownPause(SDL_Keycode key) {
} }
} }
void handleKeyUp(SDL_Keycode key) { void handleKeyUp( SDL_Keycode key ) {
switch(key) { switch ( key ) {
case SDLK_a: case SDLK_a:
player->addMovement(1,0); player->addMovement( 1, 0 );
break; break;
case SDLK_d: case SDLK_d:
player->addMovement(-1,0); player->addMovement( -1, 0 );
break; break;
case SDLK_w: case SDLK_w:
case SDLK_s: case SDLK_s:
@ -274,22 +290,22 @@ void handleKeyUp(SDL_Keycode key) {
} }
} }
void pollEvents(SDLPP::Scene &scene) { void pollEvents( SDLPP::Scene &scene ) {
SDL_Event event; SDL_Event event;
while( SDL_PollEvent( &event ) != 0 ) { while ( SDL_PollEvent( &event ) != 0 ) {
switch(event.type) { switch ( event.type ) {
case SDL_QUIT: case SDL_QUIT:
quitGame(); quitGame();
break; break;
case SDL_KEYDOWN: case SDL_KEYDOWN:
if( !event.key.repeat ) if ( !event.key.repeat )
handleKeyDown(event.key.keysym.sym, scene); handleKeyDown( event.key.keysym.sym, scene );
break; break;
case SDL_KEYUP: case SDL_KEYUP:
handleKeyUp(event.key.keysym.sym); handleKeyUp( event.key.keysym.sym );
break; break;
case SDL_WINDOWEVENT: case SDL_WINDOWEVENT:
if(event.window.event == SDL_WINDOWEVENT_RESIZED) if ( event.window.event == SDL_WINDOWEVENT_RESIZED )
scene.updateSizeAndPosition(); scene.updateSizeAndPosition();
default: default:
break; break;
@ -299,17 +315,17 @@ void pollEvents(SDLPP::Scene &scene) {
void pollEventsPause() { void pollEventsPause() {
SDL_Event event; SDL_Event event;
while( SDL_PollEvent( &event ) != 0 ) { while ( SDL_PollEvent( &event ) != 0 ) {
switch(event.type) { switch ( event.type ) {
case SDL_QUIT: case SDL_QUIT:
quitGame(); quitGame();
break; break;
case SDL_KEYDOWN: case SDL_KEYDOWN:
if( !event.key.repeat ) if ( !event.key.repeat )
handleKeyDownPause(event.key.keysym.sym); handleKeyDownPause( event.key.keysym.sym );
break; break;
case SDL_WINDOWEVENT: case SDL_WINDOWEVENT:
if(event.window.event == SDL_WINDOWEVENT_RESIZED) { if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
active_scene->updateSizeAndPosition(); active_scene->updateSizeAndPosition();
pause_scene->updateSizeAndPosition(); pause_scene->updateSizeAndPosition();
} }
@ -319,93 +335,98 @@ void pollEventsPause() {
} }
} }
void doInput(std::shared_ptr<SDLPP::Scene> scene) { void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
FPSmanager gFPS; FPSmanager gFPS;
SDL_initFramerate(&gFPS); SDL_initFramerate( &gFPS );
SDL_setFramerate(&gFPS, 200); SDL_setFramerate( &gFPS, 200 );
while(!quit && !pause) { while ( !quit && !pause ) {
SDL_framerateDelay(&gFPS); SDL_framerateDelay( &gFPS );
pollEvents(*scene); pollEvents( *scene );
scene->movement(); scene->movement();
bool gravity = true; bool gravity = true;
for( auto &x : scene->getCollisions(*player) ) { for ( auto &x : scene->getCollisions( *player ) ) {
if( x->getId() == STONE_ID ) { if ( x->getId() == STONE_ID ) {
gravity = false; gravity = false;
if(player->isGravityEnabled()) { if ( player->isGravityEnabled() ) {
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 =
player->setPos(newPX, newPY); stoneRect.first.second - playerPos.second.second;
player->setPos( newPX, newPY );
player->setLastStand(); player->setLastStand();
} }
x->specialAction(DESTROYABLE_DESTROY); x->specialAction( DESTROYABLE_DESTROY );
} }
if( x->getId() == DEATH ) { if ( x->getId() == DEATH ) {
std::cout << "Oh no, you died!" << std::endl; std::cout << "Oh no, you died!" << std::endl;
quitGame(); quitGame();
} }
} }
player->setGravity(gravity); player->setGravity( gravity );
auto playerX = player->getRect().x; auto playerX = player->getRect().x;
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 );
} }
} }
void doInputPause() { void doInputPause() {
FPSmanager gFPS; FPSmanager gFPS;
SDL_initFramerate(&gFPS); SDL_initFramerate( &gFPS );
SDL_setFramerate(&gFPS, 200); SDL_setFramerate( &gFPS, 200 );
while(pause) { while ( pause ) {
SDL_framerateDelay(&gFPS); SDL_framerateDelay( &gFPS );
pollEventsPause(); pollEventsPause();
if(!pause) if ( !pause )
break; break;
} }
} }
int main() { int main() {
SDLPP::init(); SDLPP::init();
SDLPP::Window w("Oh yeah, boi!"); SDLPP::Window w( "Oh yeah, boi!" );
auto renderer = std::make_shared<SDLPP::Renderer>(w); auto renderer = std::make_shared< SDLPP::Renderer >( w );
renderer->setBlendMode(SDL_BLENDMODE_BLEND); renderer->setBlendMode( SDL_BLENDMODE_BLEND );
auto main_scene = std::make_shared<SDLPP::Scene>(renderer); auto main_scene = std::make_shared< SDLPP::Scene >( renderer );
active_scene = main_scene; active_scene = main_scene;
font = std::make_shared<SDLPP::Font>("testfont.ttf", 96); font = std::make_shared< SDLPP::Font >( "testfont.ttf", 96 );
addStuff(*main_scene, renderer); addStuff( *main_scene, renderer );
player->setMovementSpeed(0.3); player->setMovementSpeed( 0.3 );
player->enableGravity(); player->enableGravity();
pause_scene = std::make_shared<SDLPP::Scene>(renderer); pause_scene = std::make_shared< SDLPP::Scene >( renderer );
addPause(*pause_scene, renderer); addPause( *pause_scene, renderer );
int base = SDL_GetTicks(); int base = SDL_GetTicks();
int frames = 0; int frames = 0;
FPSmanager gFPS; FPSmanager gFPS;
SDL_initFramerate(&gFPS); SDL_initFramerate( &gFPS );
SDL_setFramerate(&gFPS, 60); SDL_setFramerate( &gFPS, 60 );
std::thread inputThread(doInput, main_scene); std::thread inputThread( doInput, main_scene );
inputThread.detach(); inputThread.detach();
while( !quit ) { while ( !quit ) {
// SDL_framerateDelay(&gFPS); // SDL_framerateDelay(&gFPS);
main_scene->renderScene(); main_scene->renderScene();
if(pause) { if ( pause ) {
pause_scene->renderScene(false); pause_scene->renderScene( false );
} }
main_scene->presentScene(); main_scene->presentScene();
frames++; frames++;
if(SDL_GetTicks() - base >= 1000) { if ( SDL_GetTicks() - base >= 1000 ) {
base = SDL_GetTicks(); base = SDL_GetTicks();
printf("FPS: %d\n", frames); printf( "FPS: %d\n", frames );
frames = 0; frames = 0;
} }
} }

161
sdlpp.cpp
View File

@ -4,118 +4,136 @@
#include <iostream> #include <iostream>
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;
} }
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;
} }
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 ) {
p1.topmost() > p2.bottommost() || p2.topmost() > p1.bottommost()); 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 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;
} }
bool SDLPP::Rect::colidesWith(const SDLPP::CollisionPolygon &other) const { bool SDLPP::Rect::colidesWith( const SDLPP::CollisionPolygon &other ) const {
if(other.isCircle()) { if ( other.isCircle() ) {
return other.colidesWith(*this); return other.colidesWith( *this );
} }
if(other.isInfinite() ) { if ( other.isInfinite() ) {
return infinityIntersection(other, *this); return infinityIntersection( other, *this );
} }
if(isInfinite()) if ( isInfinite() )
return infinityIntersection(*this, other); return infinityIntersection( *this, other );
return intersects(*this, other); return intersects( *this, other );
} }
bool SDLPP::Circle::colidesWith(const SDLPP::CollisionPolygon &other) const { bool SDLPP::Circle::colidesWith( const SDLPP::CollisionPolygon &other ) const {
if(other.isCircle()) { if ( other.isCircle() ) {
int otherRad = (other.rightmost() - other.leftmost())/2; int otherRad = ( other.rightmost() - other.leftmost() ) / 2;
int thisRad = getRadius(); int thisRad = getRadius();
int totalDist = otherRad + thisRad; int totalDist = otherRad + thisRad;
int xdiff = other.leftmost() + otherRad - (leftmost() + thisRad); int xdiff = other.leftmost() + otherRad - ( leftmost() + thisRad );
int ydiff = other.topmost() + otherRad - (topmost() + thisRad); int ydiff = other.topmost() + otherRad - ( topmost() + thisRad );
return (xdiff*xdiff + ydiff*ydiff) <= totalDist * totalDist; return ( xdiff * xdiff + ydiff * ydiff ) <= totalDist * totalDist;
} else if (other.isInfinite()) { } else if ( other.isInfinite() ) {
return infinityIntersection(other, *this); return infinityIntersection( other, *this );
} }
int rad = rad_; int rad = rad_;
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() &&
} else if (other.leftmost() <= centerx && other.rightmost() >= centerx) { other.rightmost() >= leftmost();
return other.topmost() <= bottommost() && other.bottommost() >= topmost(); } else if ( other.leftmost() <= centerx && other.rightmost() >= centerx ) {
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() ) {
pointx = other.rightmost(); pointx = other.rightmost();
} else { } else {
pointx = other.leftmost(); pointx = other.leftmost();
} }
if(centery < other.topmost()) { if ( centery < other.topmost() ) {
pointy = other.topmost(); pointy = other.topmost();
} else { } else {
pointy = other.bottommost(); pointy = other.bottommost();
} }
int distancesquared = (pointx - centerx)*(pointx - centerx) + (pointy - centery)*(pointy-centery); int distancesquared = ( pointx - centerx ) * ( pointx - centerx ) +
return distancesquared <= rad*rad; ( pointy - centery ) * ( pointy - centery );
return distancesquared <= rad * rad;
} }
int SDLPP::hex2num(char c) { int SDLPP::hex2num( char c ) {
if(c <= '9') if ( c <= '9' )
return c - '0'; return c - '0';
switch(c) { switch ( c ) {
case 'a': case 'a':
case 'A': case 'A':
return 10; return 10;
@ -136,33 +154,36 @@ 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] == '#' )
color_ptr++; color_ptr++;
red = hex2num(color_ptr[0])*16 + hex2num(color_ptr[1]); red = hex2num( color_ptr[0] ) * 16 + hex2num( color_ptr[1] );
green = hex2num(color_ptr[2])*16 + hex2num(color_ptr[3]); green = hex2num( color_ptr[2] ) * 16 + hex2num( color_ptr[3] );
blue = hex2num(color_ptr[4])*16 + hex2num(color_ptr[5]); blue = hex2num( color_ptr[4] ) * 16 + hex2num( color_ptr[5] );
if( color_ptr[6] != '\0' ) if ( color_ptr[6] != '\0' )
alpha = hex2num(color_ptr[6])*16 + hex2num(color_ptr[7]); alpha = hex2num( color_ptr[6] ) * 16 + hex2num( color_ptr[7] );
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 );
ret_color.b = std::get<2>(tuple); ret_color.b = std::get< 2 >( tuple );
ret_color.a = std::get<3>(tuple); ret_color.a = std::get< 3 >( tuple );
return ret_color; return ret_color;
} }
SDL_Color SDLPP::getSDLColorHEX(const std::string &color) { SDL_Color SDLPP::getSDLColorHEX( const std::string &color ) {
auto color_tuple = SDLPP::getColorsHEX(color); auto color_tuple = SDLPP::getColorsHEX( 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 >
return {color.r, color.g, color.b, color.a}; SDLPP::getColorsSDLColor( const SDL_Color &color ) {
return { color.r, color.g, color.b, color.a };
} }

828
sdlpp.hpp

File diff suppressed because it is too large Load Diff

View File

@ -14,33 +14,34 @@ int pause_max = 1;
int ticks_till_next = 1500; int ticks_till_next = 1500;
int ticks_till_fall = 500; int ticks_till_fall = 500;
int ticks_till_descend = 50; int ticks_till_descend = 50;
std::vector<std::shared_ptr<SDLPP::RectangleRender>> pause_options; std::vector< std::shared_ptr< SDLPP::RectangleRender > > pause_options;
std::shared_ptr<SDLPP::TextRenderer> score_texture; std::shared_ptr< SDLPP::TextRenderer > score_texture;
std::shared_ptr<SDLPP::Renderer> active_renderer; std::shared_ptr< SDLPP::Renderer > active_renderer;
int score = 0; int score = 0;
bool update_score = false; bool update_score = false;
std::shared_ptr<SDLPP::Font> font; std::shared_ptr< SDLPP::Font > font;
std::shared_ptr<SDLPP::Scene> active_scene; std::shared_ptr< SDLPP::Scene > active_scene;
std::shared_ptr<SDLPP::Scene> pause_scene; 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,
pieces.push_back(piece); int y ) {
pieces_rel_position.push_back({0,0,0,0}); pieces.push_back( piece );
pieces_rel_position.push_back( { 0, 0, 0, 0 } );
// done this way for SPEEEEEEED // done this way for SPEEEEEEED
//left // left
pieces_rel_position.back()[0] = (x<0)*(-1)*x; pieces_rel_position.back()[0] = ( x < 0 ) * ( -1 ) * x;
//right // right
pieces_rel_position.back()[1] = (x>0)*x; pieces_rel_position.back()[1] = ( x > 0 ) * x;
//top // top
pieces_rel_position.back()[2] = (y<0)*(-1)*y; pieces_rel_position.back()[2] = ( y < 0 ) * ( -1 ) * y;
//bottom // bottom
pieces_rel_position.back()[3] = (y>0)*y; pieces_rel_position.back()[3] = ( y > 0 ) * y;
} }
void rotate() { void rotate() {
for(unsigned long i = 0; i < pieces.size(); i++) { for ( unsigned long i = 0; i < pieces.size(); i++ ) {
auto &piece = pieces[i]; auto &piece = pieces[i];
auto &positions = pieces_rel_position[i]; auto &positions = pieces_rel_position[i];
auto position = piece->getPos(); auto position = piece->getPos();
@ -58,22 +59,22 @@ public:
position.first += positions[1] * 0.04; position.first += positions[1] * 0.04;
position.second -= positions[2] * 0.04; position.second -= positions[2] * 0.04;
position.second += positions[3] * 0.04; position.second += positions[3] * 0.04;
piece->setPos(position.first, position.second); piece->setPos( position.first, position.second );
} }
} }
std::vector<std::shared_ptr<SDLPP::RectangleRender>> &getObjects() { std::vector< std::shared_ptr< SDLPP::RectangleRender > > &getObjects() {
return pieces; return pieces;
} }
void setPos(double x, double y) { void setPos( double x, double y ) {
for(unsigned long i = 0; i < pieces.size(); i++) { for ( unsigned long i = 0; i < pieces.size(); i++ ) {
auto &piece = pieces[i]; auto &piece = pieces[i];
auto pos = piece->getPos(); auto pos = piece->getPos();
piece->setPos(x + pos.first - default_x, piece->setPos( x + pos.first - default_x,
y + pos.second - default_y); y + pos.second - default_y );
} }
setDefPos(x, y); setDefPos( x, y );
} }
void setDefPos(double x, double y) { void setDefPos( double x, double y ) {
default_x = x; default_x = x;
default_y = y; default_y = y;
} }
@ -90,199 +91,274 @@ 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;
double default_x; double default_x;
double default_y; double default_y;
bool descend = false; bool descend = false;
}; };
std::vector<std::shared_ptr<SDLPP::RectangleRender>> line_coliders; std::vector< std::shared_ptr< SDLPP::RectangleRender > > line_coliders;
TetrisPiece cur_object; TetrisPiece cur_object;
TetrisPiece next_object; TetrisPiece next_object;
void doInput(std::shared_ptr<SDLPP::Scene> scene); void doInput( std::shared_ptr< SDLPP::Scene > scene );
void doInputPause(); void doInputPause();
bool quit = false; 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,
ret->setOutlineColor(outline); const std::string &outline,
ret->addCollision(SDLPP::Rect(0.1,0.1,0.8,0.8)); std::shared_ptr< SDLPP::Renderer > renderer,
ret->setId(BRICK_ID); 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 );
ret->centerX(); ret->centerX();
scene->addObject(ret); scene->addObject( ret );
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(
retPiece.setDefPos(0.5, 0.16); 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; 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(
retPiece.setDefPos(0.5, 0.16); 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; 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(
retPiece.setDefPos(0.5, 0.16); 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; 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(
retPiece.setDefPos(0.5, 0.16); 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; 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(
retPiece.setDefPos(0.5, 0.16); 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; 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(
retPiece.setDefPos(0.5, 0.16); 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; 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(
retPiece.setDefPos(0.5, 0.16); 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; 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,
bg->setPermanent(true); "#101090FF", true );
scene.addObject(bg); bg->setPermanent( true );
auto left_barrier = std::make_shared<SDLPP::RectangleRender>(0.28,0,0.02,1,r,"#FF000080", true); scene.addObject( bg );
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 =
auto gameover_collision = SDLPP::Rect(-1,0,-1,1); 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_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 >(
auto colider_colider = SDLPP::Rect(-1,0.1,-1,0.8); 0.3, posy, 0.04, 0.04, r );
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 );
colider->setId(COLIDER_ID); colider->setId( COLIDER_ID );
line_coliders.push_back(colider); line_coliders.push_back( colider );
scene.addObject(colider); scene.addObject( colider );
} }
} }
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,
bg->setId(123); "#00000080", true );
bg->setPermanent(true); bg->setId( 123 );
scene.addObject(bg); bg->setPermanent( true );
auto y = std::make_shared<SDLPP::TextRenderer>(0.25, 0.1, 0.5, 0.3, r); scene.addObject( bg );
y->setText(*font, "PAUSED", "#FFFFFF", "#000000", 5); auto y = std::make_shared< SDLPP::TextRenderer >( 0.25, 0.1, 0.5, 0.3, r );
y->setId(0); y->setText( *font, "PAUSED", "#FFFFFF", "#000000", 5 );
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 =
resume->setText(*font, "Resume", "#FFFFFF", "#000000", 5); std::make_shared< SDLPP::TextRenderer >( 0.4, 0.5, 0.2, 0.1, r );
resume->setColor("#FFFFFF40"); resume->setText( *font, "Resume", "#FFFFFF", "#000000", 5 );
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 =
quit->setText(*font, "Quit Game", "#FFFFFF", "#000000", 5); std::make_shared< SDLPP::TextRenderer >( 0.4, 0.7, 0.2, 0.1, r );
quit->setText( *font, "Quit Game", "#FFFFFF", "#000000", 5 );
quit->centerX(); quit->centerX();
scene.addObject(quit); scene.addObject( quit );
pause_options.push_back(quit); pause_options.push_back( quit );
} }
void quitGame() { void quitGame() {
@ -290,58 +366,57 @@ void quitGame() {
quit = true; quit = true;
} }
void handleKeyDown(SDL_Keycode key, SDLPP::Scene &scene) { void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
bool crash = false; bool crash = false;
std::lock_guard<std::mutex> guard(movement_mutex); std::lock_guard< std::mutex > guard( movement_mutex );
switch(key) { switch ( key ) {
case SDLK_ESCAPE: { case SDLK_ESCAPE: {
pause = true; pause = true;
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() ) {
auto pos = x->getPos(); auto pos = x->getPos();
// 0.31 because doubles // 0.31 because doubles
if(pos.first < 0.31) if ( pos.first < 0.31 )
crash = true; crash = true;
x->setPos(pos.first - 0.04, pos.second); x->setPos( pos.first - 0.04, pos.second );
} }
for(auto &x : cur_object.getObjects()) { for ( auto &x : cur_object.getObjects() ) {
auto collisions = scene.getCollisions(*x, {BRICK_ID}); auto collisions = scene.getCollisions( *x, { BRICK_ID } );
if(collisions.size() > 1) if ( collisions.size() > 1 )
crash = true; crash = true;
} }
if(crash) { if ( crash ) {
for(auto &x : cur_object.getObjects()) { for ( auto &x : cur_object.getObjects() ) {
auto pos = x->getPos(); auto pos = x->getPos();
x->setPos(pos.first + 0.04, pos.second); x->setPos( pos.first + 0.04, pos.second );
} }
} }
break; break;
case SDLK_RIGHT: case SDLK_RIGHT:
case SDLK_d: case SDLK_d:
for(auto &x : cur_object.getObjects()) { for ( auto &x : cur_object.getObjects() ) {
auto pos = x->getPos(); auto pos = x->getPos();
// 0.65 because doubles // 0.65 because doubles
if(pos.first > 0.65) { if ( pos.first > 0.65 ) {
crash = true; crash = true;
} }
x->setPos(pos.first + 0.04, pos.second); x->setPos( pos.first + 0.04, pos.second );
} }
for(auto &x : cur_object.getObjects()) { for ( auto &x : cur_object.getObjects() ) {
auto collisions = scene.getCollisions(*x, {BRICK_ID}); auto collisions = scene.getCollisions( *x, { BRICK_ID } );
if(collisions.size() > 1) { if ( collisions.size() > 1 ) {
crash = true; crash = true;
} }
} }
if(crash) { if ( crash ) {
for(auto &x : cur_object.getObjects()) { for ( auto &x : cur_object.getObjects() ) {
auto pos = x->getPos(); auto pos = x->getPos();
x->setPos(pos.first - 0.04, pos.second); x->setPos( pos.first - 0.04, pos.second );
} }
} }
break; break;
@ -354,55 +429,55 @@ 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;
} }
} }
void handleKeyUp(SDL_Keycode key) { void handleKeyUp( SDL_Keycode key ) {
if(key == SDLK_DOWN || key == SDLK_s) { if ( key == SDLK_DOWN || key == SDLK_s ) {
cur_object.stopDescend(); cur_object.stopDescend();
} }
} }
void handleKeyDownPause(SDL_Keycode key) { void handleKeyDownPause( SDL_Keycode key ) {
switch(key) { switch ( key ) {
case SDLK_ESCAPE: { case SDLK_ESCAPE: {
pause = false; pause = false;
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:
pause_options[pause_select]->unsetColor(); pause_options[pause_select]->unsetColor();
pause_select++; pause_select++;
if(pause_select > pause_max) if ( pause_select > pause_max )
pause_select = 0; pause_select = 0;
pause_options[pause_select]->setColor("FFFFFF40"); pause_options[pause_select]->setColor( "FFFFFF40" );
break; break;
case SDLK_w: case SDLK_w:
case SDLK_UP: case SDLK_UP:
pause_options[pause_select]->unsetColor(); pause_options[pause_select]->unsetColor();
pause_select--; pause_select--;
if(pause_select < 0) if ( pause_select < 0 )
pause_select = pause_max; pause_select = pause_max;
pause_options[pause_select]->setColor("FFFFFF40"); pause_options[pause_select]->setColor( "FFFFFF40" );
break; break;
case SDLK_RETURN: case SDLK_RETURN:
switch(pause_select) { switch ( pause_select ) {
case 0:{ case 0: {
pause = false; pause = false;
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:
@ -413,22 +488,22 @@ void handleKeyDownPause(SDL_Keycode key) {
} }
} }
void pollEvents(SDLPP::Scene &scene) { void pollEvents( SDLPP::Scene &scene ) {
SDL_Event event; SDL_Event event;
while( SDL_PollEvent( &event ) != 0 ) { while ( SDL_PollEvent( &event ) != 0 ) {
switch(event.type) { switch ( event.type ) {
case SDL_QUIT: case SDL_QUIT:
quitGame(); quitGame();
break; break;
case SDL_KEYDOWN: case SDL_KEYDOWN:
if( !event.key.repeat ) if ( !event.key.repeat )
handleKeyDown(event.key.keysym.sym, scene); handleKeyDown( event.key.keysym.sym, scene );
break; break;
case SDL_KEYUP: case SDL_KEYUP:
handleKeyUp(event.key.keysym.sym); handleKeyUp( event.key.keysym.sym );
break; break;
case SDL_WINDOWEVENT: case SDL_WINDOWEVENT:
if(event.window.event == SDL_WINDOWEVENT_RESIZED) if ( event.window.event == SDL_WINDOWEVENT_RESIZED )
scene.updateSizeAndPosition(); scene.updateSizeAndPosition();
default: default:
break; break;
@ -438,17 +513,17 @@ void pollEvents(SDLPP::Scene &scene) {
void pollEventsPause() { void pollEventsPause() {
SDL_Event event; SDL_Event event;
while( SDL_PollEvent( &event ) != 0 ) { while ( SDL_PollEvent( &event ) != 0 ) {
switch(event.type) { switch ( event.type ) {
case SDL_QUIT: case SDL_QUIT:
quitGame(); quitGame();
break; break;
case SDL_KEYDOWN: case SDL_KEYDOWN:
if( !event.key.repeat ) if ( !event.key.repeat )
handleKeyDownPause(event.key.keysym.sym); handleKeyDownPause( event.key.keysym.sym );
break; break;
case SDL_WINDOWEVENT: case SDL_WINDOWEVENT:
if(event.window.event == SDL_WINDOWEVENT_RESIZED) { if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
active_scene->updateSizeAndPosition(); active_scene->updateSizeAndPosition();
pause_scene->updateSizeAndPosition(); pause_scene->updateSizeAndPosition();
} }
@ -458,13 +533,13 @@ void pollEventsPause() {
} }
} }
void moveThem(std::shared_ptr<SDLPP::Scene> scene, int ticks) { void moveThem( std::shared_ptr< SDLPP::Scene > scene, int ticks ) {
std::lock_guard<std::mutex> guard(movement_mutex); std::lock_guard< std::mutex > guard( movement_mutex );
ticks_till_fall -= ticks; ticks_till_fall -= ticks;
if(cur_object.isDescending()) if ( cur_object.isDescending() )
ticks_till_descend -= ticks; ticks_till_descend -= ticks;
if(ticks_till_fall > 0) { if ( ticks_till_fall > 0 ) {
if(cur_object.isDescending() && ticks_till_descend <= 0) { if ( cur_object.isDescending() && ticks_till_descend <= 0 ) {
ticks_till_descend = 50; ticks_till_descend = 50;
goto fall; goto fall;
} }
@ -472,29 +547,29 @@ void moveThem(std::shared_ptr<SDLPP::Scene> scene, int ticks) {
} }
ticks_till_fall = 500; ticks_till_fall = 500;
fall: fall:
for(auto &x : cur_object.getObjects()) { for ( auto &x : cur_object.getObjects() ) {
auto pos = x->getPos(); auto pos = x->getPos();
x->setPos(pos.first, pos.second + 0.04); x->setPos( pos.first, pos.second + 0.04 );
} }
bool fell = false; bool fell = false;
for(auto &x : cur_object.getObjects()) { for ( auto &x : cur_object.getObjects() ) {
auto collisions = scene->getCollisions(*x, {BRICK_ID}); auto collisions = scene->getCollisions( *x, { BRICK_ID } );
if(collisions.size() > 1) { if ( collisions.size() > 1 ) {
fell = true; fell = true;
break; break;
} }
if(x->getPos().second >= 1) { if ( x->getPos().second >= 1 ) {
fell = true; fell = true;
break; break;
} }
} }
if(fell) { if ( fell ) {
for(auto &x : cur_object.getObjects()) { for ( auto &x : cur_object.getObjects() ) {
auto pos = x->getPos(); auto pos = x->getPos();
x->setPos(pos.first, pos.second - 0.04); x->setPos( pos.first, pos.second - 0.04 );
} }
for(auto &block : cur_object.getObjects()) { for ( auto &block : cur_object.getObjects() ) {
if(scene->getCollisions(*block, {GAME_OVER}).size() > 0) { if ( scene->getCollisions( *block, { GAME_OVER } ).size() > 0 ) {
std::cout << "You lost" << std::endl; std::cout << "You lost" << std::endl;
quitGame(); quitGame();
} }
@ -503,42 +578,42 @@ fall:
} }
} }
void doInput(std::shared_ptr<SDLPP::Scene> scene) { void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
FPSmanager gFPS; FPSmanager gFPS;
SDL_initFramerate(&gFPS); SDL_initFramerate( &gFPS );
SDL_setFramerate(&gFPS, 200); SDL_setFramerate( &gFPS, 200 );
auto base = SDL_GetTicks(); auto base = SDL_GetTicks();
while(!quit && !pause) { while ( !quit && !pause ) {
base = SDL_GetTicks(); base = SDL_GetTicks();
SDL_framerateDelay(&gFPS); SDL_framerateDelay( &gFPS );
pollEvents(*scene); pollEvents( *scene );
scene->movement(); scene->movement();
if(cur_object.getObjects().size() != 0) { if ( cur_object.getObjects().size() != 0 ) {
moveThem(scene, SDL_GetTicks() - base); moveThem( scene, SDL_GetTicks() - base );
continue; continue;
} }
std::lock_guard<std::mutex> guard(movement_mutex); std::lock_guard< std::mutex > guard( movement_mutex );
for( auto &colider : line_coliders ) { for ( auto &colider : line_coliders ) {
auto collisions = scene->getCollisions(*colider, {BRICK_ID}); auto collisions = scene->getCollisions( *colider, { BRICK_ID } );
while(collisions.size() == 10) { while ( collisions.size() == 10 ) {
score += 10; score += 10;
// updateScore(); // updateScore();
update_score = true; update_score = true;
for(auto &col : collisions) { for ( auto &col : collisions ) {
col->destroy(); col->destroy();
} }
auto colider_y = colider->getPos().second; auto colider_y = colider->getPos().second;
for(auto &elem : scene->getObjects()) { for ( auto &elem : scene->getObjects() ) {
if(elem->getId() != BRICK_ID) if ( elem->getId() != BRICK_ID )
continue; continue;
auto pos = elem->getPos(); auto pos = elem->getPos();
if(pos.second < colider_y) { if ( pos.second < colider_y ) {
elem->setPos(pos.first, pos.second + 0.04); elem->setPos( pos.first, pos.second + 0.04 );
} }
} }
using namespace std::chrono_literals; using namespace std::chrono_literals;
std::this_thread::sleep_for(0.1s); std::this_thread::sleep_for( 0.1s );
collisions = scene->getCollisions(*colider, {BRICK_ID}); collisions = scene->getCollisions( *colider, { BRICK_ID } );
} }
} }
} }
@ -546,67 +621,70 @@ void doInput(std::shared_ptr<SDLPP::Scene> scene) {
void doInputPause() { void doInputPause() {
FPSmanager gFPS; FPSmanager gFPS;
SDL_initFramerate(&gFPS); SDL_initFramerate( &gFPS );
SDL_setFramerate(&gFPS, 200); SDL_setFramerate( &gFPS, 200 );
while(pause) { while ( pause ) {
SDL_framerateDelay(&gFPS); SDL_framerateDelay( &gFPS );
pollEventsPause(); pollEventsPause();
if(!pause) if ( !pause )
break; break;
} }
} }
int main() { int main() {
SDLPP::init(); SDLPP::init();
SDLPP::Window w("Tetris clone!"); SDLPP::Window w( "Tetris clone!" );
auto renderer = std::make_shared<SDLPP::Renderer>(w); auto renderer = std::make_shared< SDLPP::Renderer >( w );
active_renderer = renderer; active_renderer = renderer;
renderer->setBlendMode(SDL_BLENDMODE_BLEND); renderer->setBlendMode( SDL_BLENDMODE_BLEND );
auto main_scene = std::make_shared<SDLPP::Scene>(renderer); auto main_scene = std::make_shared< SDLPP::Scene >( renderer );
active_scene = main_scene; active_scene = main_scene;
font = std::make_shared<SDLPP::Font>("testfont.ttf", 96); font = std::make_shared< SDLPP::Font >( "testfont.ttf", 96 );
addStuff(*main_scene, renderer); addStuff( *main_scene, renderer );
pause_scene = std::make_shared<SDLPP::Scene>(renderer); pause_scene = std::make_shared< SDLPP::Scene >( renderer );
addPause(*pause_scene, renderer); addPause( *pause_scene, renderer );
int base = SDL_GetTicks(); int base = SDL_GetTicks();
int frames = 0; int frames = 0;
std::srand(std::time(nullptr)); std::srand( std::time( nullptr ) );
FPSmanager gFPS; FPSmanager gFPS;
SDL_initFramerate(&gFPS); SDL_initFramerate( &gFPS );
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 )](
next_object.setPos(0.9, 0.5); renderer, main_scene );
while( !quit ) { next_object.setPos( 0.9, 0.5 );
SDL_framerateDelay(&gFPS); while ( !quit ) {
if(cur_object.getObjects().size() != 0) { SDL_framerateDelay( &gFPS );
if ( cur_object.getObjects().size() != 0 ) {
ticks_till_next = 1500; ticks_till_next = 1500;
} else { } else {
ticks_till_next -= SDL_GetTicks() - base; ticks_till_next -= SDL_GetTicks() - base;
if(ticks_till_next <= 0 && cur_object.getObjects().size() == 0) { if ( ticks_till_next <= 0 && cur_object.getObjects().size() == 0 ) {
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 =
next_object.setPos(0.9, 0.5); tetrisFunctions[std::rand() / ( ( RAND_MAX + 1u ) / 7 )](
renderer, main_scene );
next_object.setPos( 0.9, 0.5 );
} }
} }
if(update_score) { if ( update_score ) {
updateScore(); updateScore();
update_score = false; update_score = false;
} }
main_scene->renderScene(); main_scene->renderScene();
if(pause) { if ( pause ) {
pause_scene->renderScene(false); pause_scene->renderScene( false );
} }
main_scene->presentScene(); main_scene->presentScene();
frames++; frames++;
if(SDL_GetTicks() - base >= 1000) { if ( SDL_GetTicks() - base >= 1000 ) {
base = SDL_GetTicks(); base = SDL_GetTicks();
std::cout << "FPS: " << frames << std::endl; std::cout << "FPS: " << frames << std::endl;
frames = 0; frames = 0;