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

491
main.cpp
View File

@ -12,24 +12,27 @@
bool pause = false;
int pause_select = 0;
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::Scene> active_scene;
std::shared_ptr<SDLPP::Scene> pause_scene;
std::shared_ptr< SDLPP::Font > font;
std::shared_ptr< SDLPP::Scene > active_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();
class Player : public SDLPP::RectangleRender {
public:
Player(double x, double y, double w, double h, std::shared_ptr<SDLPP::Renderer> &r, double _max_gravity = 1.0, uint32_t _max_gravity_time = 1000) : SDLPP::RectangleRender(x,y,w,h,r) {
Player( double x, double y, double w, double h,
std::shared_ptr< SDLPP::Renderer > &r, double _max_gravity = 1.0,
uint32_t _max_gravity_time = 1000 )
: SDLPP::RectangleRender( x, y, w, h, r ) {
max_gravity = _max_gravity;
jump_speed = 2.2*max_gravity;
jump_speed = 2.2 * max_gravity;
max_gravity_time = _max_gravity_time;
cur_gravity_time = max_gravity_time;
}
void setGravity(bool grav) {
void setGravity( bool grav ) {
gravity_enabled = grav;
}
void enableGravity() {
@ -51,30 +54,34 @@ public:
void jump() {
jumping = true;
}
virtual void custom_move(int ticks) override {
auto time_portion = (static_cast<double>(ticks)/1000);
virtual void custom_move( int ticks ) override {
auto time_portion = ( static_cast< double >( ticks ) / 1000 );
cur_gravity_time -= ticks;
auto grav = gravity_enabled * max_gravity * (max_gravity_time - cur_gravity_time)/max_gravity_time;
if( grav > max_gravity)
auto grav = gravity_enabled * max_gravity *
( max_gravity_time - cur_gravity_time ) / max_gravity_time;
if ( grav > max_gravity )
grav = max_gravity;
// percentage of how close we are to maximum velocity of gravity (0 = we've reached max velocity, 1 = we've just started accelerating)
double division = static_cast<double>(cur_gravity_time)/static_cast<double>(max_gravity_time);
// percentage of how close we are to maximum velocity of gravity (0 =
// we've reached max velocity, 1 = we've just started accelerating)
double division = static_cast< double >( cur_gravity_time ) /
static_cast< double >( max_gravity_time );
// current jump speed
auto jump_ = jumping * jump_speed * division;
if(division < 0.75 && division >= 0.72) {
if ( division < 0.75 && division >= 0.72 ) {
// in this time frame, just hover in same place
jump_ = jumping * grav;
} else if(division < 0.72) {
} else if ( division < 0.72 ) {
// fall slowly
jump_ = jumping * jump_speed * division * 0.15;
if(jump_ > grav)
if ( jump_ > grav )
jump_ = grav;
}
if(jump_ < 0 || jump_ > jump_speed)
if ( jump_ < 0 || jump_ > jump_speed )
jump_ = 0;
og_y += grav * time_portion;
og_y -= jump_ * time_portion;
}
private:
double max_gravity = 1.0;
double jump_speed = 2.0;
@ -86,21 +93,26 @@ private:
class Destroyable : public SDLPP::RectangleRender {
public:
Destroyable(double x, double y, double w, double h, std::shared_ptr<SDLPP::Renderer> &r) : SDLPP::RectangleRender(x,y,w,h,r) {}
Destroyable(double x, double y, double w, double h, std::shared_ptr<SDLPP::Renderer> &r, int destruction_time) : SDLPP::RectangleRender(x,y,w,h,r) {
Destroyable( double x, double y, double w, double h,
std::shared_ptr< SDLPP::Renderer > &r )
: SDLPP::RectangleRender( x, y, w, h, r ) {}
Destroyable( double x, double y, double w, double h,
std::shared_ptr< SDLPP::Renderer > &r, int destruction_time )
: SDLPP::RectangleRender( x, y, w, h, r ) {
destruction_countdown = destruction_time;
}
virtual void specialAction(int code) override {
if(code == DESTROYABLE_DESTROY)
virtual void specialAction( int code ) override {
if ( code == DESTROYABLE_DESTROY )
startDestruction();
}
virtual void custom_move(int ticks) override {
if(destruction) {
virtual void custom_move( int ticks ) override {
if ( destruction ) {
destruction_countdown -= ticks;
if(destruction_countdown <= 0)
if ( destruction_countdown <= 0 )
destroy();
}
}
private:
void startDestruction() {
destruction = true;
@ -109,70 +121,75 @@ private:
int destruction_countdown = 3000;
};
std::shared_ptr<Player> player;
std::shared_ptr< Player > player;
bool quit = false;
void addStuff(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) {
auto bg = std::make_shared<SDLPP::RectangleRender>(0,0,10,10,r,"#ebdbb2FF", true);
bg->setId(123);
bg->setPermanent(true);
scene.addObject(bg);
std::shared_ptr<Destroyable> stone;
void addStuff( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer > &r ) {
auto bg = std::make_shared< SDLPP::RectangleRender >( 0, 0, 10, 10, r,
"#ebdbb2FF", true );
bg->setId( 123 );
bg->setPermanent( true );
scene.addObject( bg );
std::shared_ptr< Destroyable > stone;
double posx = 0;
while(posx < 3) {
stone = std::make_shared<Destroyable>(posx,0.5,0.15,0.1,r, 1000);
stone->addCollision(SDLPP::Rect(0,0,1,1));
stone->setColor("#222222FF");
stone->setId(STONE_ID);
stone->setColiderColor("FF0000");
scene.addObject(stone);
while ( posx < 3 ) {
stone =
std::make_shared< Destroyable >( posx, 0.5, 0.15, 0.1, r, 1000 );
stone->addCollision( SDLPP::Rect( 0, 0, 1, 1 ) );
stone->setColor( "#222222FF" );
stone->setId( STONE_ID );
stone->setColiderColor( "FF0000" );
scene.addObject( stone );
posx += 0.45;
}
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.65,0.7,0.05,0.31));
x->addCollision(SDLPP::Rect(0.2,0.3,0.6,0.45));
x->addCollision(SDLPP::Circle(0.5,0.15,0.3));
x->setColor("E164B7");
x->setId(PLAYER_ID);
x->setColiderColor("00FF00");
scene.addObject(x);
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.65, 0.7, 0.05, 0.31 ) );
x->addCollision( SDLPP::Rect( 0.2, 0.3, 0.6, 0.45 ) );
x->addCollision( SDLPP::Circle( 0.5, 0.15, 0.3 ) );
x->setColor( "E164B7" );
x->setId( PLAYER_ID );
x->setColiderColor( "00FF00" );
scene.addObject( x );
player = x;
auto z = std::make_shared<SDLPP::RectangleRender>(0,2.5,0,0,r);
auto z_col = SDLPP::Rect(-1,0,-1,-1);
auto z = std::make_shared< SDLPP::RectangleRender >( 0, 2.5, 0, 0, r );
auto z_col = SDLPP::Rect( -1, 0, -1, -1 );
z_col.setInfinite();
z->addCollision(z_col);
z->setId(DEATH);
z->setColiderColor("FF00FF");
scene.addObject(z);
auto y = std::make_shared<SDLPP::TextRenderer>(0, 0, 0.2, 0.1, r);
y->setText(*font, "DEMO", "#FFFFFF", "#000000", 5);
y->setPermanent(true);
y->setId(123);
scene.addObject(y);
z->addCollision( z_col );
z->setId( DEATH );
z->setColiderColor( "FF00FF" );
scene.addObject( z );
auto y = std::make_shared< SDLPP::TextRenderer >( 0, 0, 0.2, 0.1, r );
y->setText( *font, "DEMO", "#FFFFFF", "#000000", 5 );
y->setPermanent( true );
y->setId( 123 );
scene.addObject( y );
}
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);
bg->setId(123);
bg->setPermanent(true);
scene.addObject(bg);
auto y = std::make_shared<SDLPP::TextRenderer>(0.25, 0.1, 0.5, 0.3, r);
y->setText(*font, "PAUSED", "#FFFFFF", "#000000", 5);
y->setId(0);
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 );
bg->setId( 123 );
bg->setPermanent( true );
scene.addObject( bg );
auto y = std::make_shared< SDLPP::TextRenderer >( 0.25, 0.1, 0.5, 0.3, r );
y->setText( *font, "PAUSED", "#FFFFFF", "#000000", 5 );
y->setId( 0 );
y->centerX();
scene.addObject(y);
auto resume = std::make_shared<SDLPP::TextRenderer>(0.4, 0.5, 0.2, 0.1, r);
resume->setText(*font, "Resume", "#FFFFFF", "#000000", 5);
resume->setColor("#FFFFFF40");
scene.addObject( y );
auto resume =
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.5, 0.2, 0.1, r );
resume->setText( *font, "Resume", "#FFFFFF", "#000000", 5 );
resume->setColor( "#FFFFFF40" );
resume->centerX();
scene.addObject(resume);
pause_options.push_back(resume);
auto quit = std::make_shared<SDLPP::TextRenderer>(0.4, 0.7, 0.2, 0.1, r);
quit->setText(*font, "Quit Game", "#FFFFFF", "#000000", 5);
scene.addObject( resume );
pause_options.push_back( resume );
auto quit =
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.7, 0.2, 0.1, r );
quit->setText( *font, "Quit Game", "#FFFFFF", "#000000", 5 );
quit->centerX();
scene.addObject(quit);
pause_options.push_back(quit);
scene.addObject( quit );
pause_options.push_back( quit );
}
void quitGame() {
@ -180,232 +197,236 @@ void quitGame() {
quit = true;
}
void handleKeyDown(SDL_Keycode key, SDLPP::Scene &scene) {
switch(key) {
case SDLK_ESCAPE: {
pause = true;
pause_scene->updateSizeAndPosition();
player->resetMovementX();
std::thread pauseThread(doInputPause);
pauseThread.detach();
}
break;
case SDLK_a:
player->addMovement(-1,0);
break;
case SDLK_d:
player->addMovement(1,0);
break;
case SDLK_SPACE:
case SDLK_w:
if(!player->isJumping() && !pause) {
player->setLastStand();
player->jump();
}
break;
case SDLK_s:
break;
case SDLK_r:
scene.getRenderer().setRenderColiders(!scene.getRenderer().getRenderColiders());
default:
break;
void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
switch ( key ) {
case SDLK_ESCAPE: {
pause = true;
pause_scene->updateSizeAndPosition();
player->resetMovementX();
std::thread pauseThread( doInputPause );
pauseThread.detach();
} break;
case SDLK_a:
player->addMovement( -1, 0 );
break;
case SDLK_d:
player->addMovement( 1, 0 );
break;
case SDLK_SPACE:
case SDLK_w:
if ( !player->isJumping() && !pause ) {
player->setLastStand();
player->jump();
}
break;
case SDLK_s:
break;
case SDLK_r:
scene.getRenderer().setRenderColiders(
!scene.getRenderer().getRenderColiders() );
default:
break;
}
}
void handleKeyDownPause(SDL_Keycode key) {
switch(key) {
case SDLK_ESCAPE: {
pause = false;
active_scene->setPrevTicks(SDL_GetTicks());
std::thread inputThread(doInput, active_scene);
inputThread.detach();
}
break;
case SDLK_r:
active_scene->getRenderer().setRenderColiders(!active_scene->getRenderer().getRenderColiders());
break;
case SDLK_s:
case SDLK_DOWN:
pause_options[pause_select]->unsetColor();
pause_select++;
if(pause_select > pause_max)
pause_select = 0;
pause_options[pause_select]->setColor("FFFFFF40");
break;
case SDLK_w:
case SDLK_UP:
pause_options[pause_select]->unsetColor();
pause_select--;
if(pause_select < 0)
pause_select = pause_max;
pause_options[pause_select]->setColor("FFFFFF40");
break;
case SDLK_RETURN:
switch(pause_select) {
case 0:{
pause = false;
active_scene->setPrevTicks(SDL_GetTicks());
std::thread inputThread(doInput, active_scene);
inputThread.detach();
}
break;
case 1:
quitGame();
default:
break;
}
void handleKeyDownPause( SDL_Keycode key ) {
switch ( key ) {
case SDLK_ESCAPE: {
pause = false;
active_scene->setPrevTicks( SDL_GetTicks() );
std::thread inputThread( doInput, active_scene );
inputThread.detach();
} break;
case SDLK_r:
active_scene->getRenderer().setRenderColiders(
!active_scene->getRenderer().getRenderColiders() );
break;
case SDLK_s:
case SDLK_DOWN:
pause_options[pause_select]->unsetColor();
pause_select++;
if ( pause_select > pause_max )
pause_select = 0;
pause_options[pause_select]->setColor( "FFFFFF40" );
break;
case SDLK_w:
case SDLK_UP:
pause_options[pause_select]->unsetColor();
pause_select--;
if ( pause_select < 0 )
pause_select = pause_max;
pause_options[pause_select]->setColor( "FFFFFF40" );
break;
case SDLK_RETURN:
switch ( pause_select ) {
case 0: {
pause = false;
active_scene->setPrevTicks( SDL_GetTicks() );
std::thread inputThread( doInput, active_scene );
inputThread.detach();
} break;
case 1:
quitGame();
default:
break;
}
default:
break;
}
}
void handleKeyUp(SDL_Keycode key) {
switch(key) {
case SDLK_a:
player->addMovement(1,0);
break;
case SDLK_d:
player->addMovement(-1,0);
break;
case SDLK_w:
case SDLK_s:
default:
break;
void handleKeyUp( SDL_Keycode key ) {
switch ( key ) {
case SDLK_a:
player->addMovement( 1, 0 );
break;
case SDLK_d:
player->addMovement( -1, 0 );
break;
case SDLK_w:
case SDLK_s:
default:
break;
}
}
void pollEvents(SDLPP::Scene &scene) {
void pollEvents( SDLPP::Scene &scene ) {
SDL_Event event;
while( SDL_PollEvent( &event ) != 0 ) {
switch(event.type) {
case SDL_QUIT:
quitGame();
break;
case SDL_KEYDOWN:
if( !event.key.repeat )
handleKeyDown(event.key.keysym.sym, scene);
break;
case SDL_KEYUP:
handleKeyUp(event.key.keysym.sym);
break;
case SDL_WINDOWEVENT:
if(event.window.event == SDL_WINDOWEVENT_RESIZED)
scene.updateSizeAndPosition();
default:
break;
while ( SDL_PollEvent( &event ) != 0 ) {
switch ( event.type ) {
case SDL_QUIT:
quitGame();
break;
case SDL_KEYDOWN:
if ( !event.key.repeat )
handleKeyDown( event.key.keysym.sym, scene );
break;
case SDL_KEYUP:
handleKeyUp( event.key.keysym.sym );
break;
case SDL_WINDOWEVENT:
if ( event.window.event == SDL_WINDOWEVENT_RESIZED )
scene.updateSizeAndPosition();
default:
break;
}
}
}
void pollEventsPause() {
SDL_Event event;
while( SDL_PollEvent( &event ) != 0 ) {
switch(event.type) {
case SDL_QUIT:
quitGame();
break;
case SDL_KEYDOWN:
if( !event.key.repeat )
handleKeyDownPause(event.key.keysym.sym);
break;
case SDL_WINDOWEVENT:
if(event.window.event == SDL_WINDOWEVENT_RESIZED) {
active_scene->updateSizeAndPosition();
pause_scene->updateSizeAndPosition();
}
default:
break;
while ( SDL_PollEvent( &event ) != 0 ) {
switch ( event.type ) {
case SDL_QUIT:
quitGame();
break;
case SDL_KEYDOWN:
if ( !event.key.repeat )
handleKeyDownPause( event.key.keysym.sym );
break;
case SDL_WINDOWEVENT:
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
active_scene->updateSizeAndPosition();
pause_scene->updateSizeAndPosition();
}
default:
break;
}
}
}
void doInput(std::shared_ptr<SDLPP::Scene> scene) {
void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
FPSmanager gFPS;
SDL_initFramerate(&gFPS);
SDL_setFramerate(&gFPS, 200);
while(!quit && !pause) {
SDL_framerateDelay(&gFPS);
pollEvents(*scene);
SDL_initFramerate( &gFPS );
SDL_setFramerate( &gFPS, 200 );
while ( !quit && !pause ) {
SDL_framerateDelay( &gFPS );
pollEvents( *scene );
scene->movement();
bool gravity = true;
for( auto &x : scene->getCollisions(*player) ) {
if( x->getId() == STONE_ID ) {
for ( auto &x : scene->getCollisions( *player ) ) {
if ( x->getId() == STONE_ID ) {
gravity = false;
if(player->isGravityEnabled()) {
if ( player->isGravityEnabled() ) {
auto stoneRect = x->getDoubleRect();
auto playerPos = player->getDoubleRect();
auto newPX = playerPos.first.first;
auto newPY = stoneRect.first.second - playerPos.second.second;
player->setPos(newPX, newPY);
auto newPY =
stoneRect.first.second - playerPos.second.second;
player->setPos( newPX, newPY );
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;
quitGame();
}
}
player->setGravity(gravity);
player->setGravity( gravity );
auto playerX = player->getRect().x;
auto width = scene->getWidth();
auto rightBarrier = width * 0.7;
auto leftBarrier = width * 0.3;
auto rightmostX = scene->rightmost()->getRect().x + scene->rightmost()->getRect().w;
auto rightmostX =
scene->rightmost()->getRect().x + scene->rightmost()->getRect().w;
auto leftmostX = scene->leftmost()->getRect().x;
scene->moveEverything(
(playerX > rightBarrier && rightmostX > width) * (rightBarrier - playerX)/width, 0);
scene->moveEverything(
(playerX < leftBarrier && leftmostX < 0) * (leftBarrier - playerX)/width, 0);
( playerX > rightBarrier && rightmostX > width ) *
( rightBarrier - playerX ) / width,
0 );
scene->moveEverything( ( playerX < leftBarrier && leftmostX < 0 ) *
( leftBarrier - playerX ) / width,
0 );
}
}
void doInputPause() {
FPSmanager gFPS;
SDL_initFramerate(&gFPS);
SDL_setFramerate(&gFPS, 200);
while(pause) {
SDL_framerateDelay(&gFPS);
SDL_initFramerate( &gFPS );
SDL_setFramerate( &gFPS, 200 );
while ( pause ) {
SDL_framerateDelay( &gFPS );
pollEventsPause();
if(!pause)
if ( !pause )
break;
}
}
int main() {
SDLPP::init();
SDLPP::Window w("Oh yeah, boi!");
auto renderer = std::make_shared<SDLPP::Renderer>(w);
renderer->setBlendMode(SDL_BLENDMODE_BLEND);
auto main_scene = std::make_shared<SDLPP::Scene>(renderer);
SDLPP::Window w( "Oh yeah, boi!" );
auto renderer = std::make_shared< SDLPP::Renderer >( w );
renderer->setBlendMode( SDL_BLENDMODE_BLEND );
auto main_scene = std::make_shared< SDLPP::Scene >( renderer );
active_scene = main_scene;
font = std::make_shared<SDLPP::Font>("testfont.ttf", 96);
addStuff(*main_scene, renderer);
player->setMovementSpeed(0.3);
font = std::make_shared< SDLPP::Font >( "testfont.ttf", 96 );
addStuff( *main_scene, renderer );
player->setMovementSpeed( 0.3 );
player->enableGravity();
pause_scene = std::make_shared<SDLPP::Scene>(renderer);
addPause(*pause_scene, renderer);
pause_scene = std::make_shared< SDLPP::Scene >( renderer );
addPause( *pause_scene, renderer );
int base = SDL_GetTicks();
int frames = 0;
FPSmanager gFPS;
SDL_initFramerate(&gFPS);
SDL_setFramerate(&gFPS, 60);
std::thread inputThread(doInput, main_scene);
SDL_initFramerate( &gFPS );
SDL_setFramerate( &gFPS, 60 );
std::thread inputThread( doInput, main_scene );
inputThread.detach();
while( !quit ) {
// SDL_framerateDelay(&gFPS);
while ( !quit ) {
// SDL_framerateDelay(&gFPS);
main_scene->renderScene();
if(pause) {
pause_scene->renderScene(false);
if ( pause ) {
pause_scene->renderScene( false );
}
main_scene->presentScene();
frames++;
if(SDL_GetTicks() - base >= 1000) {
if ( SDL_GetTicks() - base >= 1000 ) {
base = SDL_GetTicks();
printf("FPS: %d\n", frames);
printf( "FPS: %d\n", frames );
frames = 0;
}
}

195
sdlpp.cpp
View File

@ -4,165 +4,186 @@
#include <iostream>
bool SDLPP::init() {
if( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError()
<< std::endl;
return false;
}
if( IMG_Init( IMG_INIT_PNG ) != IMG_INIT_PNG ) {
std::cerr << "SDL_image could not initialize! SDL_image Error: " << IMG_GetError() << std::endl;
if ( IMG_Init( IMG_INIT_PNG ) != IMG_INIT_PNG ) {
std::cerr << "SDL_image could not initialize! SDL_image Error: "
<< IMG_GetError() << std::endl;
return false;
}
if( TTF_Init() == -1 ) {
std::cerr << "SDL_ttf could not initialize! SDL_ttf Error: " << TTF_GetError() << std::endl;
if ( TTF_Init() == -1 ) {
std::cerr << "SDL_ttf could not initialize! SDL_ttf Error: "
<< TTF_GetError() << std::endl;
return false;
}
return true;
}
bool SDLPP::init(uint32_t SDL_OPTIONS) {
if( SDL_Init(SDL_OPTIONS) < 0 ) {
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
bool SDLPP::init( uint32_t SDL_OPTIONS ) {
if ( SDL_Init( SDL_OPTIONS ) < 0 ) {
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError()
<< std::endl;
return false;
}
if( IMG_Init( IMG_INIT_PNG ) != IMG_INIT_PNG ) {
std::cerr << "SDL_image could not initialize! SDL_image Error: " << IMG_GetError() << std::endl;
if ( IMG_Init( IMG_INIT_PNG ) != IMG_INIT_PNG ) {
std::cerr << "SDL_image could not initialize! SDL_image Error: "
<< IMG_GetError() << std::endl;
return false;
}
return true;
}
bool SDLPP::init(uint32_t SDL_OPTIONS, int IMAGE_OPTIONS) {
if( SDL_Init(SDL_OPTIONS) < 0 ) {
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
bool SDLPP::init( uint32_t SDL_OPTIONS, int IMAGE_OPTIONS ) {
if ( SDL_Init( SDL_OPTIONS ) < 0 ) {
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError()
<< std::endl;
return false;
}
if( IMG_Init( IMAGE_OPTIONS ) != IMAGE_OPTIONS ) {
std::cerr << "SDL_image could not initialize! SDL_image Error: " << IMG_GetError() << std::endl;
if ( IMG_Init( IMAGE_OPTIONS ) != IMAGE_OPTIONS ) {
std::cerr << "SDL_image could not initialize! SDL_image Error: "
<< IMG_GetError() << std::endl;
return false;
}
return true;
}
void SDLPP::CircleRender::render() {
std::cout << "I'm a circle, look at me go!" << std::endl << "My dimensions are: [" << x_ << ", " << y_ << "], radius: " << rad_ << std::endl;
std::cout << "I'm a circle, look at me go!" << std::endl
<< "My dimensions are: [" << x_ << ", " << y_
<< "], radius: " << rad_ << std::endl;
}
// only rectangles for now
bool intersects(const SDLPP::CollisionPolygon &p1, const SDLPP::CollisionPolygon &p2) {
return !(p1.rightmost() < p2.leftmost() || p2.rightmost() < p1.leftmost() ||
p1.topmost() > p2.bottommost() || p2.topmost() > p1.bottommost());
bool intersects( const SDLPP::CollisionPolygon &p1,
const SDLPP::CollisionPolygon &p2 ) {
return !(
p1.rightmost() < p2.leftmost() || p2.rightmost() < p1.leftmost() ||
p1.topmost() > p2.bottommost() || p2.topmost() > p1.bottommost() );
}
bool infinityIntersection(const SDLPP::CollisionPolygon &infinite, const SDLPP::CollisionPolygon &other) {
bool infinityIntersection( const SDLPP::CollisionPolygon &infinite,
const SDLPP::CollisionPolygon &other ) {
int ileft = infinite.leftmost();
int iright = infinite.rightmost();
int itop = infinite.topmost();
int ibottom = infinite.bottommost();
bool ret = ileft != -1 && ileft <= other.rightmost() && ileft >= other.leftmost();
ret |= iright != -1 && iright >= other.leftmost() && iright <= other.rightmost();
bool ret =
ileft != -1 && ileft <= other.rightmost() && ileft >= other.leftmost();
ret |= iright != -1 && iright >= other.leftmost() &&
iright <= other.rightmost();
ret |= itop != -1 && itop <= other.bottommost() && itop >= other.topmost();
ret |= ibottom != -1 && ibottom >= other.topmost() && ibottom <= other.bottommost();
ret |= ibottom != -1 && ibottom >= other.topmost() &&
ibottom <= other.bottommost();
ret |= ileft == -1 && iright == -1 && itop == -1 && ibottom == -1;
return ret;
}
bool SDLPP::Rect::colidesWith(const SDLPP::CollisionPolygon &other) const {
if(other.isCircle()) {
return other.colidesWith(*this);
bool SDLPP::Rect::colidesWith( const SDLPP::CollisionPolygon &other ) const {
if ( other.isCircle() ) {
return other.colidesWith( *this );
}
if(other.isInfinite() ) {
return infinityIntersection(other, *this);
if ( other.isInfinite() ) {
return infinityIntersection( other, *this );
}
if(isInfinite())
return infinityIntersection(*this, other);
return intersects(*this, other);
if ( isInfinite() )
return infinityIntersection( *this, other );
return intersects( *this, other );
}
bool SDLPP::Circle::colidesWith(const SDLPP::CollisionPolygon &other) const {
if(other.isCircle()) {
int otherRad = (other.rightmost() - other.leftmost())/2;
bool SDLPP::Circle::colidesWith( const SDLPP::CollisionPolygon &other ) const {
if ( other.isCircle() ) {
int otherRad = ( other.rightmost() - other.leftmost() ) / 2;
int thisRad = getRadius();
int totalDist = otherRad + thisRad;
int xdiff = other.leftmost() + otherRad - (leftmost() + thisRad);
int ydiff = other.topmost() + otherRad - (topmost() + thisRad);
return (xdiff*xdiff + ydiff*ydiff) <= totalDist * totalDist;
} else if (other.isInfinite()) {
return infinityIntersection(other, *this);
int xdiff = other.leftmost() + otherRad - ( leftmost() + thisRad );
int ydiff = other.topmost() + otherRad - ( topmost() + thisRad );
return ( xdiff * xdiff + ydiff * ydiff ) <= totalDist * totalDist;
} else if ( other.isInfinite() ) {
return infinityIntersection( other, *this );
}
int rad = rad_;
int centerx = getX();
int centery = getY();
if(other.topmost() <= centery && other.bottommost() >= centery) {
return other.leftmost() <= rightmost() && other.rightmost() >= leftmost();
} else if (other.leftmost() <= centerx && other.rightmost() >= centerx) {
return other.topmost() <= bottommost() && other.bottommost() >= topmost();
if ( other.topmost() <= centery && other.bottommost() >= centery ) {
return other.leftmost() <= rightmost() &&
other.rightmost() >= leftmost();
} else if ( other.leftmost() <= centerx && other.rightmost() >= centerx ) {
return other.topmost() <= bottommost() &&
other.bottommost() >= topmost();
}
int pointx=0, pointy=0;
if(centerx > other.rightmost()) {
int pointx = 0, pointy = 0;
if ( centerx > other.rightmost() ) {
pointx = other.rightmost();
} else {
pointx = other.leftmost();
}
if(centery < other.topmost()) {
if ( centery < other.topmost() ) {
pointy = other.topmost();
} else {
pointy = other.bottommost();
}
int distancesquared = (pointx - centerx)*(pointx - centerx) + (pointy - centery)*(pointy-centery);
return distancesquared <= rad*rad;
int distancesquared = ( pointx - centerx ) * ( pointx - centerx ) +
( pointy - centery ) * ( pointy - centery );
return distancesquared <= rad * rad;
}
int SDLPP::hex2num(char c) {
if(c <= '9')
int SDLPP::hex2num( char c ) {
if ( c <= '9' )
return c - '0';
switch(c) {
case 'a':
case 'A':
return 10;
case 'b':
case 'B':
return 11;
case 'c':
case 'C':
return 12;
case 'd':
case 'D':
return 13;
case 'e':
case 'E':
return 14;
default:
return 15;
switch ( c ) {
case 'a':
case 'A':
return 10;
case 'b':
case 'B':
return 11;
case 'c':
case 'C':
return 12;
case 'd':
case 'D':
return 13;
case 'e':
case 'E':
return 14;
default:
return 15;
}
}
std::tuple<int, int, int, int> SDLPP::getColorsHEX(const std::string &color) {
std::tuple< int, int, int, int >
SDLPP::getColorsHEX( const std::string &color ) {
int red = 0, green = 0, blue = 0, alpha = 255;
const char *color_ptr = color.c_str();
if(color_ptr[0] == '#')
if ( color_ptr[0] == '#' )
color_ptr++;
red = hex2num(color_ptr[0])*16 + hex2num(color_ptr[1]);
green = hex2num(color_ptr[2])*16 + hex2num(color_ptr[3]);
blue = hex2num(color_ptr[4])*16 + hex2num(color_ptr[5]);
if( color_ptr[6] != '\0' )
alpha = hex2num(color_ptr[6])*16 + hex2num(color_ptr[7]);
return {red, green, blue, alpha};
red = hex2num( color_ptr[0] ) * 16 + hex2num( color_ptr[1] );
green = hex2num( color_ptr[2] ) * 16 + hex2num( color_ptr[3] );
blue = hex2num( color_ptr[4] ) * 16 + hex2num( color_ptr[5] );
if ( color_ptr[6] != '\0' )
alpha = hex2num( color_ptr[6] ) * 16 + hex2num( color_ptr[7] );
return { red, green, blue, alpha };
}
SDL_Color SDLPP::getSDLColorTuple(const std::tuple<int, int, int, int> &tuple) {
SDL_Color
SDLPP::getSDLColorTuple( const std::tuple< int, int, int, int > &tuple ) {
SDL_Color ret_color{};
ret_color.r = std::get<0>(tuple);
ret_color.g = std::get<1>(tuple);
ret_color.b = std::get<2>(tuple);
ret_color.a = std::get<3>(tuple);
ret_color.r = std::get< 0 >( tuple );
ret_color.g = std::get< 1 >( tuple );
ret_color.b = std::get< 2 >( tuple );
ret_color.a = std::get< 3 >( tuple );
return ret_color;
}
SDL_Color SDLPP::getSDLColorHEX(const std::string &color) {
auto color_tuple = SDLPP::getColorsHEX(color);
return getSDLColorTuple(color_tuple);
SDL_Color SDLPP::getSDLColorHEX( const std::string &color ) {
auto color_tuple = SDLPP::getColorsHEX( color );
return getSDLColorTuple( color_tuple );
}
std::tuple<int, int, int, int> SDLPP::getColorsSDLColor(const SDL_Color &color) {
return {color.r, color.g, color.b, color.a};
std::tuple< int, int, int, int >
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_fall = 500;
int ticks_till_descend = 50;
std::vector<std::shared_ptr<SDLPP::RectangleRender>> pause_options;
std::shared_ptr<SDLPP::TextRenderer> score_texture;
std::shared_ptr<SDLPP::Renderer> active_renderer;
std::vector< std::shared_ptr< SDLPP::RectangleRender > > pause_options;
std::shared_ptr< SDLPP::TextRenderer > score_texture;
std::shared_ptr< SDLPP::Renderer > active_renderer;
int score = 0;
bool update_score = false;
std::shared_ptr<SDLPP::Font> font;
std::shared_ptr<SDLPP::Scene> active_scene;
std::shared_ptr<SDLPP::Scene> pause_scene;
std::shared_ptr< SDLPP::Font > font;
std::shared_ptr< SDLPP::Scene > active_scene;
std::shared_ptr< SDLPP::Scene > pause_scene;
class TetrisPiece {
public:
void addPiece(std::shared_ptr<SDLPP::RectangleRender> piece, int x, int y) {
pieces.push_back(piece);
pieces_rel_position.push_back({0,0,0,0});
void addPiece( std::shared_ptr< SDLPP::RectangleRender > piece, int x,
int y ) {
pieces.push_back( piece );
pieces_rel_position.push_back( { 0, 0, 0, 0 } );
// done this way for SPEEEEEEED
//left
pieces_rel_position.back()[0] = (x<0)*(-1)*x;
//right
pieces_rel_position.back()[1] = (x>0)*x;
//top
pieces_rel_position.back()[2] = (y<0)*(-1)*y;
//bottom
pieces_rel_position.back()[3] = (y>0)*y;
// left
pieces_rel_position.back()[0] = ( x < 0 ) * ( -1 ) * x;
// right
pieces_rel_position.back()[1] = ( x > 0 ) * x;
// top
pieces_rel_position.back()[2] = ( y < 0 ) * ( -1 ) * y;
// bottom
pieces_rel_position.back()[3] = ( y > 0 ) * y;
}
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 &positions = pieces_rel_position[i];
auto position = piece->getPos();
@ -58,22 +59,22 @@ public:
position.first += positions[1] * 0.04;
position.second -= positions[2] * 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;
}
void setPos(double x, double y) {
for(unsigned long i = 0; i < pieces.size(); i++) {
void setPos( double x, double y ) {
for ( unsigned long i = 0; i < pieces.size(); i++ ) {
auto &piece = pieces[i];
auto pos = piece->getPos();
piece->setPos(x + pos.first - default_x,
y + pos.second - default_y);
piece->setPos( x + pos.first - default_x,
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_y = y;
}
@ -90,199 +91,274 @@ public:
bool isDescending() {
return descend;
}
private:
std::vector<std::vector<int>> pieces_rel_position;
std::vector<std::shared_ptr<SDLPP::RectangleRender>> pieces;
std::vector< std::vector< int > > pieces_rel_position;
std::vector< std::shared_ptr< SDLPP::RectangleRender > > pieces;
double default_x;
double default_y;
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 next_object;
void doInput(std::shared_ptr<SDLPP::Scene> scene);
void doInput( std::shared_ptr< SDLPP::Scene > scene );
void doInputPause();
bool quit = false;
std::mutex movement_mutex;
std::shared_ptr<SDLPP::RectangleRender> createTetrisBlock(double x, double y, const std::string &color, const std::string &outline, std::shared_ptr<SDLPP::Renderer> renderer, std::shared_ptr<SDLPP::Scene> scene) {
auto ret = std::make_shared<SDLPP::RectangleRender>(x, y, 0.04, 0.04, renderer, color, true);
ret->setOutlineColor(outline);
ret->addCollision(SDLPP::Rect(0.1,0.1,0.8,0.8));
ret->setId(BRICK_ID);
std::shared_ptr< SDLPP::RectangleRender >
createTetrisBlock( double x, double y, const std::string &color,
const std::string &outline,
std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Scene > scene ) {
auto ret = std::make_shared< SDLPP::RectangleRender >(
x, y, 0.04, 0.04, renderer, color, true );
ret->setOutlineColor( outline );
ret->addCollision( SDLPP::Rect( 0.1, 0.1, 0.8, 0.8 ) );
ret->setId( BRICK_ID );
ret->centerX();
scene->addObject(ret);
scene->addObject( 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{};
auto color = "#FF0000";
auto outline = "#AA0000";
retPiece.addPiece(createTetrisBlock(0.46, 0.16, color, outline, renderer, scene), 0, 0);
retPiece.addPiece(createTetrisBlock(0.5, 0.16, color, outline, renderer, scene), 0, 0);
retPiece.addPiece(createTetrisBlock(0.46, 0.20, color, outline, renderer, scene), 0, 0);
retPiece.addPiece(createTetrisBlock(0.5, 0.20, color, outline, renderer, scene), 0, 0);
retPiece.setDefPos(0.5, 0.16);
retPiece.addPiece(
createTetrisBlock( 0.46, 0.16, color, outline, renderer, scene ), 0,
0 );
retPiece.addPiece(
createTetrisBlock( 0.5, 0.16, color, outline, renderer, scene ), 0, 0 );
retPiece.addPiece(
createTetrisBlock( 0.46, 0.20, color, outline, renderer, scene ), 0,
0 );
retPiece.addPiece(
createTetrisBlock( 0.5, 0.20, color, outline, renderer, scene ), 0, 0 );
retPiece.setDefPos( 0.5, 0.16 );
return retPiece;
}
TetrisPiece tetrisT(std::shared_ptr<SDLPP::Renderer> renderer, std::shared_ptr<SDLPP::Scene> scene) {
TetrisPiece tetrisT( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Scene > scene ) {
TetrisPiece retPiece{};
auto color = "#00FF00";
auto outline = "#00AA00";
retPiece.addPiece(createTetrisBlock(0.46, 0.20, color, outline, renderer, scene), -1, 0);
retPiece.addPiece(createTetrisBlock(0.5, 0.20, color, outline, renderer, scene), 0, 0);
retPiece.addPiece(createTetrisBlock(0.5, 0.16, color, outline, renderer, scene), 0, -1);
retPiece.addPiece(createTetrisBlock(0.54, 0.20, color, outline, renderer, scene), 1, 0);
retPiece.setDefPos(0.5, 0.16);
retPiece.addPiece(
createTetrisBlock( 0.46, 0.20, color, outline, renderer, scene ), -1,
0 );
retPiece.addPiece(
createTetrisBlock( 0.5, 0.20, color, outline, renderer, scene ), 0, 0 );
retPiece.addPiece(
createTetrisBlock( 0.5, 0.16, color, outline, renderer, scene ), 0,
-1 );
retPiece.addPiece(
createTetrisBlock( 0.54, 0.20, color, outline, renderer, scene ), 1,
0 );
retPiece.setDefPos( 0.5, 0.16 );
return retPiece;
}
TetrisPiece tetrisLRight(std::shared_ptr<SDLPP::Renderer> renderer, std::shared_ptr<SDLPP::Scene> scene) {
TetrisPiece tetrisLRight( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Scene > scene ) {
TetrisPiece retPiece{};
auto color = "#0000FF";
auto outline = "#0000AA";
retPiece.addPiece(createTetrisBlock(0.46, 0.20, color, outline, renderer, scene), -2, 0);
retPiece.addPiece(createTetrisBlock(0.5, 0.20, color, outline, renderer, scene), -1, 0);
retPiece.addPiece(createTetrisBlock(0.54, 0.20, color, outline, renderer, scene), 0, 0);
retPiece.addPiece(createTetrisBlock(0.54, 0.16, color, outline, renderer, scene), 0, -1);
retPiece.setDefPos(0.5, 0.16);
retPiece.addPiece(
createTetrisBlock( 0.46, 0.20, color, outline, renderer, scene ), -2,
0 );
retPiece.addPiece(
createTetrisBlock( 0.5, 0.20, color, outline, renderer, scene ), -1,
0 );
retPiece.addPiece(
createTetrisBlock( 0.54, 0.20, color, outline, renderer, scene ), 0,
0 );
retPiece.addPiece(
createTetrisBlock( 0.54, 0.16, color, outline, renderer, scene ), 0,
-1 );
retPiece.setDefPos( 0.5, 0.16 );
return retPiece;
}
TetrisPiece tetrisZRight(std::shared_ptr<SDLPP::Renderer> renderer, std::shared_ptr<SDLPP::Scene> scene) {
TetrisPiece tetrisZRight( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Scene > scene ) {
TetrisPiece retPiece{};
auto color = "#FF00FF";
auto outline = "#AA00AA";
retPiece.addPiece(createTetrisBlock(0.46, 0.20, color, outline, renderer, scene), -1, 0);
retPiece.addPiece(createTetrisBlock(0.5, 0.20, color, outline, renderer, scene), 0, 0);
retPiece.addPiece(createTetrisBlock(0.5, 0.16, color, outline, renderer, scene), 0, -1);
retPiece.addPiece(createTetrisBlock(0.54, 0.16, color, outline, renderer, scene), 1, -1);
retPiece.setDefPos(0.5, 0.16);
retPiece.addPiece(
createTetrisBlock( 0.46, 0.20, color, outline, renderer, scene ), -1,
0 );
retPiece.addPiece(
createTetrisBlock( 0.5, 0.20, color, outline, renderer, scene ), 0, 0 );
retPiece.addPiece(
createTetrisBlock( 0.5, 0.16, color, outline, renderer, scene ), 0,
-1 );
retPiece.addPiece(
createTetrisBlock( 0.54, 0.16, color, outline, renderer, scene ), 1,
-1 );
retPiece.setDefPos( 0.5, 0.16 );
return retPiece;
}
TetrisPiece tetrisLine(std::shared_ptr<SDLPP::Renderer> renderer, std::shared_ptr<SDLPP::Scene> scene) {
TetrisPiece tetrisLine( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Scene > scene ) {
TetrisPiece retPiece{};
auto color = "#FFFF00";
auto outline = "#AAAA00";
retPiece.addPiece(createTetrisBlock(0.42, 0.16, color, outline, renderer, scene), -1, 0);
retPiece.addPiece(createTetrisBlock(0.46, 0.16, color, outline, renderer, scene), 0, 0);
retPiece.addPiece(createTetrisBlock(0.5, 0.16, color, outline, renderer, scene), 1, 0);
retPiece.addPiece(createTetrisBlock(0.54, 0.16, color, outline, renderer, scene), 2, 0);
retPiece.setDefPos(0.5, 0.16);
retPiece.addPiece(
createTetrisBlock( 0.42, 0.16, color, outline, renderer, scene ), -1,
0 );
retPiece.addPiece(
createTetrisBlock( 0.46, 0.16, color, outline, renderer, scene ), 0,
0 );
retPiece.addPiece(
createTetrisBlock( 0.5, 0.16, color, outline, renderer, scene ), 1, 0 );
retPiece.addPiece(
createTetrisBlock( 0.54, 0.16, color, outline, renderer, scene ), 2,
0 );
retPiece.setDefPos( 0.5, 0.16 );
return retPiece;
}
TetrisPiece tetrisLLeft(std::shared_ptr<SDLPP::Renderer> renderer, std::shared_ptr<SDLPP::Scene> scene) {
TetrisPiece tetrisLLeft( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Scene > scene ) {
TetrisPiece retPiece{};
auto color = "#00FFFF";
auto outline = "#00AAAA";
retPiece.addPiece(createTetrisBlock(0.46, 0.16, color, outline, renderer, scene), 0, -1);
retPiece.addPiece(createTetrisBlock(0.46, 0.20, color, outline, renderer, scene), 0, 0);
retPiece.addPiece(createTetrisBlock(0.5, 0.20, color, outline, renderer, scene), 1, 0);
retPiece.addPiece(createTetrisBlock(0.54, 0.20, color, outline, renderer, scene), 2, 0);
retPiece.setDefPos(0.5, 0.16);
retPiece.addPiece(
createTetrisBlock( 0.46, 0.16, color, outline, renderer, scene ), 0,
-1 );
retPiece.addPiece(
createTetrisBlock( 0.46, 0.20, color, outline, renderer, scene ), 0,
0 );
retPiece.addPiece(
createTetrisBlock( 0.5, 0.20, color, outline, renderer, scene ), 1, 0 );
retPiece.addPiece(
createTetrisBlock( 0.54, 0.20, color, outline, renderer, scene ), 2,
0 );
retPiece.setDefPos( 0.5, 0.16 );
return retPiece;
}
TetrisPiece tetrisZLeft(std::shared_ptr<SDLPP::Renderer> renderer, std::shared_ptr<SDLPP::Scene> scene) {
TetrisPiece tetrisZLeft( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Scene > scene ) {
TetrisPiece retPiece{};
auto color = "#FFFFFF";
auto outline = "#AAAAAA";
retPiece.addPiece(createTetrisBlock(0.46, 0.16, color, outline, renderer, scene), -1, 0);
retPiece.addPiece(createTetrisBlock(0.5, 0.16, color, outline, renderer, scene), 0, 0);
retPiece.addPiece(createTetrisBlock(0.5, 0.20, color, outline, renderer, scene), 0, 1);
retPiece.addPiece(createTetrisBlock(0.54, 0.20, color, outline, renderer, scene), 1, 1);
retPiece.setDefPos(0.5, 0.16);
retPiece.addPiece(
createTetrisBlock( 0.46, 0.16, color, outline, renderer, scene ), -1,
0 );
retPiece.addPiece(
createTetrisBlock( 0.5, 0.16, color, outline, renderer, scene ), 0, 0 );
retPiece.addPiece(
createTetrisBlock( 0.5, 0.20, color, outline, renderer, scene ), 0, 1 );
retPiece.addPiece(
createTetrisBlock( 0.54, 0.20, color, outline, renderer, scene ), 1,
1 );
retPiece.setDefPos( 0.5, 0.16 );
return retPiece;
}
std::vector<TetrisPiece (*)(std::shared_ptr<SDLPP::Renderer>, std::shared_ptr<SDLPP::Scene>)> tetrisFunctions = {
tetrisBrick,
tetrisT,
tetrisLRight,
tetrisZRight,
tetrisLine,
tetrisLLeft,
tetrisZLeft,
};
std::vector< TetrisPiece ( * )( std::shared_ptr< SDLPP::Renderer >,
std::shared_ptr< SDLPP::Scene > ) >
tetrisFunctions = {
tetrisBrick, tetrisT, tetrisLRight, tetrisZRight,
tetrisLine, tetrisLLeft, tetrisZLeft,
};
void addStuff(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) {
auto bg = std::make_shared<SDLPP::RectangleRender>(0,0,10,10,r,"#101090FF", true);
bg->setPermanent(true);
scene.addObject(bg);
auto left_barrier = std::make_shared<SDLPP::RectangleRender>(0.28,0,0.02,1,r,"#FF000080", true);
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 );
bg->setPermanent( true );
scene.addObject( bg );
auto left_barrier = std::make_shared< SDLPP::RectangleRender >(
0.28, 0, 0.02, 1, r, "#FF000080", true );
left_barrier->centerX();
scene.addObject(left_barrier);
auto right_barrier = std::make_shared<SDLPP::RectangleRender>(0.7,0,0.02,1,r,"#FF000080", true);
scene.addObject( left_barrier );
auto right_barrier = std::make_shared< SDLPP::RectangleRender >(
0.7, 0, 0.02, 1, r, "#FF000080", true );
right_barrier->centerX();
scene.addObject(right_barrier);
auto bottom_barrier = std::make_shared<SDLPP::RectangleRender>(0.28,1,0.44,0.02,r,"#FF000080", true);
scene.addObject( right_barrier );
auto bottom_barrier = std::make_shared< SDLPP::RectangleRender >(
0.28, 1, 0.44, 0.02, r, "#FF000080", true );
bottom_barrier->centerX();
scene.addObject(bottom_barrier);
auto tetris = std::make_shared<SDLPP::TextRenderer>(0.4, 0, 0.2, 0.1, r, *font, "TETRIS", "FFFFFF", "000000", 5);
scene.addObject( bottom_barrier );
auto tetris = std::make_shared< SDLPP::TextRenderer >(
0.4, 0, 0.2, 0.1, r, *font, "TETRIS", "FFFFFF", "000000", 5 );
tetris->centerX();
scene.addObject(tetris);
auto next = std::make_shared<SDLPP::TextRenderer>(0.8, 0.35, 0.2, 0.1, r, *font, "NEXT", "FFFFFF", "000000", 5, SDLPP_TEXT_CENTER);
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 );
next->centerX();
scene.addObject(next);
scene.addObject( next );
double posy = 1;
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 =
std::make_shared< SDLPP::RectangleRender >( 0.5, 0, 0, 0.195, r );
auto gameover_collision = SDLPP::Rect( -1, 0, -1, 1 );
gameover_collision.setInfinite();
gameover->addCollision(gameover_collision);
gameover->setId(GAME_OVER);
gameover->setColiderColor("FF0000");
scene.addObject(gameover);
auto score_text = std::make_shared<SDLPP::TextRenderer>(0.8, 0.1, 0.2, 0.1, r, *font, "SCORE", "#FFFFFF", "#000000", 5, SDLPP_TEXT_CENTER);
gameover->addCollision( gameover_collision );
gameover->setId( GAME_OVER );
gameover->setColiderColor( "FF0000" );
scene.addObject( gameover );
auto score_text = std::make_shared< SDLPP::TextRenderer >(
0.8, 0.1, 0.2, 0.1, r, *font, "SCORE", "#FFFFFF", "#000000", 5,
SDLPP_TEXT_CENTER );
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();
scene.addObject(score_texture);
for(int i = 0; i < 20; i++) {
scene.addObject( score_texture );
for ( int i = 0; i < 20; i++ ) {
posy -= 0.04;
auto colider = std::make_shared<SDLPP::RectangleRender>(0.3, posy, 0.04, 0.04, r);
auto colider_colider = SDLPP::Rect(-1,0.1,-1,0.8);
auto colider = std::make_shared< SDLPP::RectangleRender >(
0.3, posy, 0.04, 0.04, r );
auto colider_colider = SDLPP::Rect( -1, 0.1, -1, 0.8 );
colider_colider.setInfinite();
colider->addCollision(colider_colider);
colider->setId(COLIDER_ID);
line_coliders.push_back(colider);
scene.addObject(colider);
colider->addCollision( colider_colider );
colider->setId( COLIDER_ID );
line_coliders.push_back( colider );
scene.addObject( colider );
}
}
void updateScore() {
score_texture->setText(*font, std::to_string(score), "#FFFFFF", "#000000", 5);
score_texture->setText( *font, std::to_string( score ), "#FFFFFF",
"#000000", 5 );
}
void addPause(SDLPP::Scene &scene, std::shared_ptr<SDLPP::Renderer> &r) {
auto bg = std::make_shared<SDLPP::RectangleRender>(0,0,10,10,r,"#00000080", true);
bg->setId(123);
bg->setPermanent(true);
scene.addObject(bg);
auto y = std::make_shared<SDLPP::TextRenderer>(0.25, 0.1, 0.5, 0.3, r);
y->setText(*font, "PAUSED", "#FFFFFF", "#000000", 5);
y->setId(0);
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 );
bg->setId( 123 );
bg->setPermanent( true );
scene.addObject( bg );
auto y = std::make_shared< SDLPP::TextRenderer >( 0.25, 0.1, 0.5, 0.3, r );
y->setText( *font, "PAUSED", "#FFFFFF", "#000000", 5 );
y->setId( 0 );
y->centerX();
scene.addObject(y);
auto resume = std::make_shared<SDLPP::TextRenderer>(0.4, 0.5, 0.2, 0.1, r);
resume->setText(*font, "Resume", "#FFFFFF", "#000000", 5);
resume->setColor("#FFFFFF40");
scene.addObject( y );
auto resume =
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.5, 0.2, 0.1, r );
resume->setText( *font, "Resume", "#FFFFFF", "#000000", 5 );
resume->setColor( "#FFFFFF40" );
resume->centerX();
scene.addObject(resume);
pause_options.push_back(resume);
auto quit = std::make_shared<SDLPP::TextRenderer>(0.4, 0.7, 0.2, 0.1, r);
quit->setText(*font, "Quit Game", "#FFFFFF", "#000000", 5);
scene.addObject( resume );
pause_options.push_back( resume );
auto quit =
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.7, 0.2, 0.1, r );
quit->setText( *font, "Quit Game", "#FFFFFF", "#000000", 5 );
quit->centerX();
scene.addObject(quit);
pause_options.push_back(quit);
scene.addObject( quit );
pause_options.push_back( quit );
}
void quitGame() {
@ -290,181 +366,180 @@ void quitGame() {
quit = true;
}
void handleKeyDown(SDL_Keycode key, SDLPP::Scene &scene) {
void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
bool crash = false;
std::lock_guard<std::mutex> guard(movement_mutex);
switch(key) {
case SDLK_ESCAPE: {
pause = true;
pause_scene->updateSizeAndPosition();
std::thread pauseThread(doInputPause);
pauseThread.detach();
}
break;
case SDLK_LEFT:
case SDLK_a:
for(auto &x : cur_object.getObjects()) {
std::lock_guard< std::mutex > guard( movement_mutex );
switch ( key ) {
case SDLK_ESCAPE: {
pause = true;
pause_scene->updateSizeAndPosition();
std::thread pauseThread( doInputPause );
pauseThread.detach();
} break;
case SDLK_LEFT:
case SDLK_a:
for ( auto &x : cur_object.getObjects() ) {
auto pos = x->getPos();
// 0.31 because doubles
if ( pos.first < 0.31 )
crash = true;
x->setPos( pos.first - 0.04, pos.second );
}
for ( auto &x : cur_object.getObjects() ) {
auto collisions = scene.getCollisions( *x, { BRICK_ID } );
if ( collisions.size() > 1 )
crash = true;
}
if ( crash ) {
for ( auto &x : cur_object.getObjects() ) {
auto pos = x->getPos();
// 0.31 because doubles
if(pos.first < 0.31)
crash = true;
x->setPos(pos.first - 0.04, pos.second);
x->setPos( pos.first + 0.04, pos.second );
}
for(auto &x : cur_object.getObjects()) {
auto collisions = scene.getCollisions(*x, {BRICK_ID});
if(collisions.size() > 1)
crash = true;
}
break;
case SDLK_RIGHT:
case SDLK_d:
for ( auto &x : cur_object.getObjects() ) {
auto pos = x->getPos();
// 0.65 because doubles
if ( pos.first > 0.65 ) {
crash = true;
}
if(crash) {
for(auto &x : cur_object.getObjects()) {
auto pos = x->getPos();
x->setPos(pos.first + 0.04, pos.second);
}
x->setPos( pos.first + 0.04, pos.second );
}
for ( auto &x : cur_object.getObjects() ) {
auto collisions = scene.getCollisions( *x, { BRICK_ID } );
if ( collisions.size() > 1 ) {
crash = true;
}
break;
case SDLK_RIGHT:
case SDLK_d:
for(auto &x : cur_object.getObjects()) {
}
if ( crash ) {
for ( auto &x : cur_object.getObjects() ) {
auto pos = x->getPos();
// 0.65 because doubles
if(pos.first > 0.65) {
crash = true;
}
x->setPos(pos.first + 0.04, pos.second);
x->setPos( pos.first - 0.04, pos.second );
}
for(auto &x : cur_object.getObjects()) {
auto collisions = scene.getCollisions(*x, {BRICK_ID});
if(collisions.size() > 1) {
crash = true;
}
}
if(crash) {
for(auto &x : cur_object.getObjects()) {
auto pos = x->getPos();
x->setPos(pos.first - 0.04, pos.second);
}
}
break;
case SDLK_DOWN:
case SDLK_s:
cur_object.startDescend();
break;
case SDLK_UP:
case SDLK_w:
cur_object.rotate();
break;
case SDLK_r:
scene.getRenderer().setRenderColiders(!scene.getRenderer().getRenderColiders());
default:
break;
}
break;
case SDLK_DOWN:
case SDLK_s:
cur_object.startDescend();
break;
case SDLK_UP:
case SDLK_w:
cur_object.rotate();
break;
case SDLK_r:
scene.getRenderer().setRenderColiders(
!scene.getRenderer().getRenderColiders() );
default:
break;
}
}
void handleKeyUp(SDL_Keycode key) {
if(key == SDLK_DOWN || key == SDLK_s) {
void handleKeyUp( SDL_Keycode key ) {
if ( key == SDLK_DOWN || key == SDLK_s ) {
cur_object.stopDescend();
}
}
void handleKeyDownPause(SDL_Keycode key) {
switch(key) {
case SDLK_ESCAPE: {
pause = false;
active_scene->setPrevTicks(SDL_GetTicks());
std::thread inputThread(doInput, active_scene);
inputThread.detach();
}
break;
case SDLK_r:
active_scene->getRenderer().setRenderColiders(!active_scene->getRenderer().getRenderColiders());
break;
case SDLK_s:
case SDLK_DOWN:
pause_options[pause_select]->unsetColor();
pause_select++;
if(pause_select > pause_max)
pause_select = 0;
pause_options[pause_select]->setColor("FFFFFF40");
break;
case SDLK_w:
case SDLK_UP:
pause_options[pause_select]->unsetColor();
pause_select--;
if(pause_select < 0)
pause_select = pause_max;
pause_options[pause_select]->setColor("FFFFFF40");
break;
case SDLK_RETURN:
switch(pause_select) {
case 0:{
pause = false;
active_scene->setPrevTicks(SDL_GetTicks());
std::thread inputThread(doInput, active_scene);
inputThread.detach();
}
break;
case 1:
quitGame();
default:
break;
}
void handleKeyDownPause( SDL_Keycode key ) {
switch ( key ) {
case SDLK_ESCAPE: {
pause = false;
active_scene->setPrevTicks( SDL_GetTicks() );
std::thread inputThread( doInput, active_scene );
inputThread.detach();
} break;
case SDLK_r:
active_scene->getRenderer().setRenderColiders(
!active_scene->getRenderer().getRenderColiders() );
break;
case SDLK_s:
case SDLK_DOWN:
pause_options[pause_select]->unsetColor();
pause_select++;
if ( pause_select > pause_max )
pause_select = 0;
pause_options[pause_select]->setColor( "FFFFFF40" );
break;
case SDLK_w:
case SDLK_UP:
pause_options[pause_select]->unsetColor();
pause_select--;
if ( pause_select < 0 )
pause_select = pause_max;
pause_options[pause_select]->setColor( "FFFFFF40" );
break;
case SDLK_RETURN:
switch ( pause_select ) {
case 0: {
pause = false;
active_scene->setPrevTicks( SDL_GetTicks() );
std::thread inputThread( doInput, active_scene );
inputThread.detach();
} break;
case 1:
quitGame();
default:
break;
}
default:
break;
}
}
void pollEvents(SDLPP::Scene &scene) {
void pollEvents( SDLPP::Scene &scene ) {
SDL_Event event;
while( SDL_PollEvent( &event ) != 0 ) {
switch(event.type) {
case SDL_QUIT:
quitGame();
break;
case SDL_KEYDOWN:
if( !event.key.repeat )
handleKeyDown(event.key.keysym.sym, scene);
break;
case SDL_KEYUP:
handleKeyUp(event.key.keysym.sym);
break;
case SDL_WINDOWEVENT:
if(event.window.event == SDL_WINDOWEVENT_RESIZED)
scene.updateSizeAndPosition();
default:
break;
while ( SDL_PollEvent( &event ) != 0 ) {
switch ( event.type ) {
case SDL_QUIT:
quitGame();
break;
case SDL_KEYDOWN:
if ( !event.key.repeat )
handleKeyDown( event.key.keysym.sym, scene );
break;
case SDL_KEYUP:
handleKeyUp( event.key.keysym.sym );
break;
case SDL_WINDOWEVENT:
if ( event.window.event == SDL_WINDOWEVENT_RESIZED )
scene.updateSizeAndPosition();
default:
break;
}
}
}
void pollEventsPause() {
SDL_Event event;
while( SDL_PollEvent( &event ) != 0 ) {
switch(event.type) {
case SDL_QUIT:
quitGame();
break;
case SDL_KEYDOWN:
if( !event.key.repeat )
handleKeyDownPause(event.key.keysym.sym);
break;
case SDL_WINDOWEVENT:
if(event.window.event == SDL_WINDOWEVENT_RESIZED) {
active_scene->updateSizeAndPosition();
pause_scene->updateSizeAndPosition();
}
default:
break;
while ( SDL_PollEvent( &event ) != 0 ) {
switch ( event.type ) {
case SDL_QUIT:
quitGame();
break;
case SDL_KEYDOWN:
if ( !event.key.repeat )
handleKeyDownPause( event.key.keysym.sym );
break;
case SDL_WINDOWEVENT:
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
active_scene->updateSizeAndPosition();
pause_scene->updateSizeAndPosition();
}
default:
break;
}
}
}
void moveThem(std::shared_ptr<SDLPP::Scene> scene, int ticks) {
std::lock_guard<std::mutex> guard(movement_mutex);
void moveThem( std::shared_ptr< SDLPP::Scene > scene, int ticks ) {
std::lock_guard< std::mutex > guard( movement_mutex );
ticks_till_fall -= ticks;
if(cur_object.isDescending())
if ( cur_object.isDescending() )
ticks_till_descend -= ticks;
if(ticks_till_fall > 0) {
if(cur_object.isDescending() && ticks_till_descend <= 0) {
if ( ticks_till_fall > 0 ) {
if ( cur_object.isDescending() && ticks_till_descend <= 0 ) {
ticks_till_descend = 50;
goto fall;
}
@ -472,29 +547,29 @@ void moveThem(std::shared_ptr<SDLPP::Scene> scene, int ticks) {
}
ticks_till_fall = 500;
fall:
for(auto &x : cur_object.getObjects()) {
for ( auto &x : cur_object.getObjects() ) {
auto pos = x->getPos();
x->setPos(pos.first, pos.second + 0.04);
x->setPos( pos.first, pos.second + 0.04 );
}
bool fell = false;
for(auto &x : cur_object.getObjects()) {
auto collisions = scene->getCollisions(*x, {BRICK_ID});
if(collisions.size() > 1) {
for ( auto &x : cur_object.getObjects() ) {
auto collisions = scene->getCollisions( *x, { BRICK_ID } );
if ( collisions.size() > 1 ) {
fell = true;
break;
}
if(x->getPos().second >= 1) {
if ( x->getPos().second >= 1 ) {
fell = true;
break;
}
}
if(fell) {
for(auto &x : cur_object.getObjects()) {
if ( fell ) {
for ( auto &x : cur_object.getObjects() ) {
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()) {
if(scene->getCollisions(*block, {GAME_OVER}).size() > 0) {
for ( auto &block : cur_object.getObjects() ) {
if ( scene->getCollisions( *block, { GAME_OVER } ).size() > 0 ) {
std::cout << "You lost" << std::endl;
quitGame();
}
@ -503,42 +578,42 @@ fall:
}
}
void doInput(std::shared_ptr<SDLPP::Scene> scene) {
void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
FPSmanager gFPS;
SDL_initFramerate(&gFPS);
SDL_setFramerate(&gFPS, 200);
SDL_initFramerate( &gFPS );
SDL_setFramerate( &gFPS, 200 );
auto base = SDL_GetTicks();
while(!quit && !pause) {
while ( !quit && !pause ) {
base = SDL_GetTicks();
SDL_framerateDelay(&gFPS);
pollEvents(*scene);
SDL_framerateDelay( &gFPS );
pollEvents( *scene );
scene->movement();
if(cur_object.getObjects().size() != 0) {
moveThem(scene, SDL_GetTicks() - base);
if ( cur_object.getObjects().size() != 0 ) {
moveThem( scene, SDL_GetTicks() - base );
continue;
}
std::lock_guard<std::mutex> guard(movement_mutex);
for( auto &colider : line_coliders ) {
auto collisions = scene->getCollisions(*colider, {BRICK_ID});
while(collisions.size() == 10) {
std::lock_guard< std::mutex > guard( movement_mutex );
for ( auto &colider : line_coliders ) {
auto collisions = scene->getCollisions( *colider, { BRICK_ID } );
while ( collisions.size() == 10 ) {
score += 10;
// updateScore();
// updateScore();
update_score = true;
for(auto &col : collisions) {
for ( auto &col : collisions ) {
col->destroy();
}
auto colider_y = colider->getPos().second;
for(auto &elem : scene->getObjects()) {
if(elem->getId() != BRICK_ID)
for ( auto &elem : scene->getObjects() ) {
if ( elem->getId() != BRICK_ID )
continue;
auto pos = elem->getPos();
if(pos.second < colider_y) {
elem->setPos(pos.first, pos.second + 0.04);
if ( pos.second < colider_y ) {
elem->setPos( pos.first, pos.second + 0.04 );
}
}
using namespace std::chrono_literals;
std::this_thread::sleep_for(0.1s);
collisions = scene->getCollisions(*colider, {BRICK_ID});
std::this_thread::sleep_for( 0.1s );
collisions = scene->getCollisions( *colider, { BRICK_ID } );
}
}
}
@ -546,67 +621,70 @@ void doInput(std::shared_ptr<SDLPP::Scene> scene) {
void doInputPause() {
FPSmanager gFPS;
SDL_initFramerate(&gFPS);
SDL_setFramerate(&gFPS, 200);
while(pause) {
SDL_framerateDelay(&gFPS);
SDL_initFramerate( &gFPS );
SDL_setFramerate( &gFPS, 200 );
while ( pause ) {
SDL_framerateDelay( &gFPS );
pollEventsPause();
if(!pause)
if ( !pause )
break;
}
}
int main() {
SDLPP::init();
SDLPP::Window w("Tetris clone!");
auto renderer = std::make_shared<SDLPP::Renderer>(w);
SDLPP::Window w( "Tetris clone!" );
auto renderer = std::make_shared< SDLPP::Renderer >( w );
active_renderer = renderer;
renderer->setBlendMode(SDL_BLENDMODE_BLEND);
auto main_scene = std::make_shared<SDLPP::Scene>(renderer);
renderer->setBlendMode( SDL_BLENDMODE_BLEND );
auto main_scene = std::make_shared< SDLPP::Scene >( renderer );
active_scene = main_scene;
font = std::make_shared<SDLPP::Font>("testfont.ttf", 96);
addStuff(*main_scene, renderer);
font = std::make_shared< SDLPP::Font >( "testfont.ttf", 96 );
addStuff( *main_scene, renderer );
pause_scene = std::make_shared<SDLPP::Scene>(renderer);
addPause(*pause_scene, renderer);
pause_scene = std::make_shared< SDLPP::Scene >( renderer );
addPause( *pause_scene, renderer );
int base = SDL_GetTicks();
int frames = 0;
std::srand(std::time(nullptr));
std::srand( std::time( nullptr ) );
FPSmanager gFPS;
SDL_initFramerate(&gFPS);
SDL_setFramerate(&gFPS, 60);
std::thread inputThread(doInput, main_scene);
SDL_initFramerate( &gFPS );
SDL_setFramerate( &gFPS, 60 );
std::thread inputThread( doInput, main_scene );
inputThread.detach();
next_object = tetrisFunctions[std::rand()/((RAND_MAX + 1u)/7)](renderer, main_scene);
next_object.setPos(0.9, 0.5);
while( !quit ) {
SDL_framerateDelay(&gFPS);
if(cur_object.getObjects().size() != 0) {
next_object = tetrisFunctions[std::rand() / ( ( RAND_MAX + 1u ) / 7 )](
renderer, main_scene );
next_object.setPos( 0.9, 0.5 );
while ( !quit ) {
SDL_framerateDelay( &gFPS );
if ( cur_object.getObjects().size() != 0 ) {
ticks_till_next = 1500;
} else {
ticks_till_next -= SDL_GetTicks() - base;
if(ticks_till_next <= 0 && cur_object.getObjects().size() == 0) {
std::lock_guard<std::mutex> guard(movement_mutex);
if ( ticks_till_next <= 0 && cur_object.getObjects().size() == 0 ) {
std::lock_guard< std::mutex > guard( movement_mutex );
cur_object = next_object;
cur_object.setPos(0.5, 0.16);
next_object = tetrisFunctions[std::rand()/((RAND_MAX + 1u)/7)](renderer, main_scene);
next_object.setPos(0.9, 0.5);
cur_object.setPos( 0.5, 0.16 );
next_object =
tetrisFunctions[std::rand() / ( ( RAND_MAX + 1u ) / 7 )](
renderer, main_scene );
next_object.setPos( 0.9, 0.5 );
}
}
if(update_score) {
if ( update_score ) {
updateScore();
update_score = false;
}
main_scene->renderScene();
if(pause) {
pause_scene->renderScene(false);
if ( pause ) {
pause_scene->renderScene( false );
}
main_scene->presentScene();
frames++;
if(SDL_GetTicks() - base >= 1000) {
if ( SDL_GetTicks() - base >= 1000 ) {
base = SDL_GetTicks();
std::cout << "FPS: " << frames << std::endl;
frames = 0;