2021-03-12 21:33:46 +00:00
|
|
|
#include "sdlpp/sdlpp.hpp"
|
2020-07-26 20:37:20 +00:00
|
|
|
#include <thread>
|
|
|
|
#include <chrono>
|
|
|
|
#include <SDL2/SDL2_framerate.h>
|
|
|
|
|
|
|
|
#define PLAYER_ID 0x00000001
|
2020-07-27 18:05:59 +00:00
|
|
|
#define STONE_ID 0x00000002
|
2020-07-28 16:13:38 +00:00
|
|
|
#define DEATH 0x00000003
|
2020-07-26 20:37:20 +00:00
|
|
|
|
2020-08-21 19:40:15 +00:00
|
|
|
#define DESTROYABLE_DESTROY 0x00000001
|
|
|
|
|
2020-08-21 16:54:42 +00:00
|
|
|
bool pause = false;
|
2020-08-22 12:29:39 +00:00
|
|
|
int pause_select = 0;
|
|
|
|
int pause_max = 1;
|
2020-08-23 07:50:05 +00:00
|
|
|
std::vector< std::shared_ptr< SDLPP::RectangleRender > > pause_options;
|
2020-08-22 12:29:39 +00:00
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
std::shared_ptr< SDLPP::Font > font;
|
|
|
|
std::shared_ptr< SDLPP::Scene > active_scene;
|
|
|
|
std::shared_ptr< SDLPP::Scene > pause_scene;
|
2020-08-22 12:13:28 +00:00
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
void doInput( std::shared_ptr< SDLPP::Scene > scene );
|
2020-08-22 12:13:28 +00:00
|
|
|
void doInputPause();
|
2020-08-21 16:54:42 +00:00
|
|
|
|
2020-07-27 18:05:59 +00:00
|
|
|
class Player : public SDLPP::RectangleRender {
|
|
|
|
public:
|
2020-08-23 07:50:05 +00:00
|
|
|
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 ) {
|
2020-07-27 18:05:59 +00:00
|
|
|
max_gravity = _max_gravity;
|
2020-08-23 07:50:05 +00:00
|
|
|
jump_speed = 2.2 * max_gravity;
|
2020-07-27 18:05:59 +00:00
|
|
|
max_gravity_time = _max_gravity_time;
|
|
|
|
cur_gravity_time = max_gravity_time;
|
|
|
|
}
|
2020-08-23 07:50:05 +00:00
|
|
|
void setGravity( bool grav ) {
|
2020-07-27 18:05:59 +00:00
|
|
|
gravity_enabled = grav;
|
|
|
|
}
|
|
|
|
void enableGravity() {
|
|
|
|
gravity_enabled = true;
|
|
|
|
}
|
|
|
|
void disableGravity() {
|
|
|
|
gravity_enabled = false;
|
|
|
|
}
|
|
|
|
bool isGravityEnabled() {
|
|
|
|
return gravity_enabled;
|
|
|
|
}
|
|
|
|
bool isJumping() {
|
|
|
|
return jumping;
|
|
|
|
}
|
|
|
|
void setLastStand() {
|
|
|
|
cur_gravity_time = max_gravity_time;
|
|
|
|
jumping = false;
|
|
|
|
}
|
|
|
|
void jump() {
|
|
|
|
jumping = true;
|
|
|
|
}
|
2020-08-23 07:50:05 +00:00
|
|
|
virtual void custom_move( int ticks ) override {
|
|
|
|
auto time_portion = ( static_cast< double >( ticks ) / 1000 );
|
2020-07-27 18:05:59 +00:00
|
|
|
cur_gravity_time -= ticks;
|
2020-08-23 07:50:05 +00:00
|
|
|
auto grav = gravity_enabled * max_gravity *
|
|
|
|
( max_gravity_time - cur_gravity_time ) / max_gravity_time;
|
|
|
|
if ( grav > max_gravity )
|
2020-07-27 18:05:59 +00:00
|
|
|
grav = max_gravity;
|
2020-08-23 07:50:05 +00:00
|
|
|
// 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 );
|
2020-08-21 17:04:58 +00:00
|
|
|
// current jump speed
|
|
|
|
auto jump_ = jumping * jump_speed * division;
|
2020-08-23 07:50:05 +00:00
|
|
|
if ( division < 0.75 && division >= 0.72 ) {
|
2020-08-21 17:04:58 +00:00
|
|
|
// in this time frame, just hover in same place
|
|
|
|
jump_ = jumping * grav;
|
2020-08-23 07:50:05 +00:00
|
|
|
} else if ( division < 0.72 ) {
|
2020-08-21 17:04:58 +00:00
|
|
|
// fall slowly
|
|
|
|
jump_ = jumping * jump_speed * division * 0.15;
|
2020-08-23 07:50:05 +00:00
|
|
|
if ( jump_ > grav )
|
2020-08-21 17:04:58 +00:00
|
|
|
jump_ = grav;
|
|
|
|
}
|
2020-08-23 07:50:05 +00:00
|
|
|
if ( jump_ < 0 || jump_ > jump_speed )
|
2020-07-27 18:05:59 +00:00
|
|
|
jump_ = 0;
|
2021-03-12 21:33:46 +00:00
|
|
|
double addition = grav * time_portion - jump_ * time_portion;
|
|
|
|
original += {0, addition};
|
2020-07-27 18:05:59 +00:00
|
|
|
}
|
2020-08-23 07:50:05 +00:00
|
|
|
|
2020-07-27 18:05:59 +00:00
|
|
|
private:
|
|
|
|
double max_gravity = 1.0;
|
|
|
|
double jump_speed = 2.0;
|
|
|
|
uint32_t max_gravity_time = 1000;
|
|
|
|
uint32_t cur_gravity_time = 1000;
|
|
|
|
bool gravity_enabled = false;
|
|
|
|
bool jumping = false;
|
|
|
|
};
|
|
|
|
|
2020-08-21 19:40:15 +00:00
|
|
|
class Destroyable : public SDLPP::RectangleRender {
|
|
|
|
public:
|
2020-08-23 07:50:05 +00:00
|
|
|
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 ) {
|
2020-08-21 19:40:15 +00:00
|
|
|
destruction_countdown = destruction_time;
|
|
|
|
}
|
2020-08-23 07:50:05 +00:00
|
|
|
virtual void specialAction( int code ) override {
|
|
|
|
if ( code == DESTROYABLE_DESTROY )
|
2020-08-21 19:40:15 +00:00
|
|
|
startDestruction();
|
|
|
|
}
|
2020-08-23 07:50:05 +00:00
|
|
|
virtual void custom_move( int ticks ) override {
|
|
|
|
if ( destruction ) {
|
2020-08-21 19:40:15 +00:00
|
|
|
destruction_countdown -= ticks;
|
2020-08-23 07:50:05 +00:00
|
|
|
if ( destruction_countdown <= 0 )
|
2020-08-21 19:40:15 +00:00
|
|
|
destroy();
|
|
|
|
}
|
|
|
|
}
|
2020-08-23 07:50:05 +00:00
|
|
|
|
2020-08-21 19:40:15 +00:00
|
|
|
private:
|
|
|
|
void startDestruction() {
|
|
|
|
destruction = true;
|
|
|
|
}
|
|
|
|
bool destruction = false;
|
|
|
|
int destruction_countdown = 3000;
|
|
|
|
};
|
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
std::shared_ptr< Player > player;
|
2020-07-26 20:37:20 +00:00
|
|
|
bool quit = false;
|
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
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;
|
2020-07-27 18:05:59 +00:00
|
|
|
double posx = 0;
|
2020-08-23 07:50:05 +00:00
|
|
|
while ( posx < 3 ) {
|
|
|
|
stone =
|
|
|
|
std::make_shared< Destroyable >( posx, 0.5, 0.15, 0.1, r, 1000 );
|
2021-03-12 21:33:46 +00:00
|
|
|
stone->addCollision( SDLPP::RectColider( 0, 0, 1, 1 ) );
|
2020-08-23 07:50:05 +00:00
|
|
|
stone->setColor( "#222222FF" );
|
|
|
|
stone->setId( STONE_ID );
|
|
|
|
stone->setColiderColor( "FF0000" );
|
|
|
|
scene.addObject( stone );
|
2020-07-28 16:13:38 +00:00
|
|
|
posx += 0.45;
|
2020-07-27 18:05:59 +00:00
|
|
|
}
|
2020-08-23 07:50:05 +00:00
|
|
|
auto x = std::make_shared< Player >( 0, 0, 0.2, 0.2, r );
|
2021-03-12 21:33:46 +00:00
|
|
|
x->addCollision( SDLPP::RectColider( 0.3, 0.7, 0.05, 0.31 ) );
|
|
|
|
x->addCollision( SDLPP::RectColider( 0.65, 0.7, 0.05, 0.31 ) );
|
|
|
|
x->addCollision( SDLPP::RectColider( 0.2, 0.3, 0.6, 0.45 ) );
|
|
|
|
x->addCollision( SDLPP::CircleColider( 0.5, 0.15, 0.3 ) );
|
2020-08-23 07:50:05 +00:00
|
|
|
x->setColor( "E164B7" );
|
|
|
|
x->setId( PLAYER_ID );
|
|
|
|
x->setColiderColor( "00FF00" );
|
|
|
|
scene.addObject( x );
|
2020-07-26 20:37:20 +00:00
|
|
|
player = x;
|
2020-08-23 07:50:05 +00:00
|
|
|
auto z = std::make_shared< SDLPP::RectangleRender >( 0, 2.5, 0, 0, r );
|
2021-03-12 21:33:46 +00:00
|
|
|
auto z_col = SDLPP::RectColider( -1, 0, -1, -1 );
|
2020-07-28 17:35:31 +00:00
|
|
|
z_col.setInfinite();
|
2020-08-23 07:50:05 +00:00
|
|
|
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 );
|
2021-03-12 21:33:46 +00:00
|
|
|
y->setText( font, "DEMO", "#FFFFFF", "#000000", 5 );
|
2020-08-23 07:50:05 +00:00
|
|
|
y->setPermanent( true );
|
|
|
|
y->setId( 123 );
|
|
|
|
scene.addObject( y );
|
2020-07-26 20:37:20 +00:00
|
|
|
}
|
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
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 );
|
2021-03-12 21:33:46 +00:00
|
|
|
y->setText( font, "PAUSED", "#FFFFFF", "#000000", 5 );
|
2020-08-23 07:50:05 +00:00
|
|
|
y->setId( 0 );
|
2020-08-22 12:13:28 +00:00
|
|
|
y->centerX();
|
2020-08-23 07:50:05 +00:00
|
|
|
scene.addObject( y );
|
|
|
|
auto resume =
|
|
|
|
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.5, 0.2, 0.1, r );
|
2021-03-12 21:33:46 +00:00
|
|
|
resume->setText( font, "Resume", "#FFFFFF", "#000000", 5 );
|
2020-08-23 07:50:05 +00:00
|
|
|
resume->setColor( "#FFFFFF40" );
|
2020-08-22 12:29:39 +00:00
|
|
|
resume->centerX();
|
2020-08-23 07:50:05 +00:00
|
|
|
scene.addObject( resume );
|
|
|
|
pause_options.push_back( resume );
|
|
|
|
auto quit =
|
|
|
|
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.7, 0.2, 0.1, r );
|
2021-03-12 21:33:46 +00:00
|
|
|
quit->setText( font, "Quit Game", "#FFFFFF", "#000000", 5 );
|
2020-08-22 12:29:39 +00:00
|
|
|
quit->centerX();
|
2020-08-23 07:50:05 +00:00
|
|
|
scene.addObject( quit );
|
|
|
|
pause_options.push_back( quit );
|
2020-08-22 12:13:28 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 20:37:20 +00:00
|
|
|
void quitGame() {
|
|
|
|
std::cout << "Quitting!" << std::endl;
|
|
|
|
quit = true;
|
|
|
|
}
|
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
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;
|
2020-07-26 20:37:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
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();
|
2020-08-22 12:13:28 +00:00
|
|
|
default:
|
|
|
|
break;
|
2020-08-23 07:50:05 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
2020-08-22 12:13:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
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;
|
2020-07-26 20:37:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
void pollEvents( SDLPP::Scene &scene ) {
|
2020-07-26 20:37:20 +00:00
|
|
|
SDL_Event event;
|
2020-08-23 07:50:05 +00:00
|
|
|
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;
|
2020-07-26 20:37:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-22 12:13:28 +00:00
|
|
|
void pollEventsPause() {
|
|
|
|
SDL_Event event;
|
2020-08-23 07:50:05 +00:00
|
|
|
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;
|
2020-08-22 12:13:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
|
2020-07-26 20:37:20 +00:00
|
|
|
FPSmanager gFPS;
|
2020-08-23 07:50:05 +00:00
|
|
|
SDL_initFramerate( &gFPS );
|
|
|
|
SDL_setFramerate( &gFPS, 200 );
|
|
|
|
while ( !quit && !pause ) {
|
|
|
|
SDL_framerateDelay( &gFPS );
|
|
|
|
pollEvents( *scene );
|
2020-07-26 20:37:20 +00:00
|
|
|
scene->movement();
|
2020-07-27 18:05:59 +00:00
|
|
|
bool gravity = true;
|
2020-08-23 07:50:05 +00:00
|
|
|
for ( auto &x : scene->getCollisions( *player ) ) {
|
|
|
|
if ( x->getId() == STONE_ID ) {
|
2020-07-27 18:05:59 +00:00
|
|
|
gravity = false;
|
2020-08-23 07:50:05 +00:00
|
|
|
if ( player->isGravityEnabled() ) {
|
2020-08-21 17:49:42 +00:00
|
|
|
auto stoneRect = x->getDoubleRect();
|
|
|
|
auto playerPos = player->getDoubleRect();
|
|
|
|
auto newPX = playerPos.first.first;
|
2020-08-23 07:50:05 +00:00
|
|
|
auto newPY =
|
|
|
|
stoneRect.first.second - playerPos.second.second;
|
|
|
|
player->setPos( newPX, newPY );
|
2020-07-27 18:05:59 +00:00
|
|
|
player->setLastStand();
|
2020-08-21 17:49:42 +00:00
|
|
|
}
|
2020-08-23 07:50:05 +00:00
|
|
|
x->specialAction( DESTROYABLE_DESTROY );
|
2020-07-26 20:37:20 +00:00
|
|
|
}
|
2020-08-23 07:50:05 +00:00
|
|
|
if ( x->getId() == DEATH ) {
|
2020-07-28 16:13:38 +00:00
|
|
|
std::cout << "Oh no, you died!" << std::endl;
|
|
|
|
quitGame();
|
|
|
|
}
|
2020-07-26 20:37:20 +00:00
|
|
|
}
|
2020-08-23 07:50:05 +00:00
|
|
|
player->setGravity( gravity );
|
2020-07-28 17:35:31 +00:00
|
|
|
auto playerX = player->getRect().x;
|
|
|
|
auto width = scene->getWidth();
|
|
|
|
auto rightBarrier = width * 0.7;
|
|
|
|
auto leftBarrier = width * 0.3;
|
2020-08-23 07:50:05 +00:00
|
|
|
auto rightmostX =
|
|
|
|
scene->rightmost()->getRect().x + scene->rightmost()->getRect().w;
|
2020-07-28 17:35:31 +00:00
|
|
|
auto leftmostX = scene->leftmost()->getRect().x;
|
|
|
|
scene->moveEverything(
|
2020-08-23 07:50:05 +00:00
|
|
|
( playerX > rightBarrier && rightmostX > width ) *
|
|
|
|
( rightBarrier - playerX ) / width,
|
|
|
|
0 );
|
|
|
|
scene->moveEverything( ( playerX < leftBarrier && leftmostX < 0 ) *
|
|
|
|
( leftBarrier - playerX ) / width,
|
|
|
|
0 );
|
2020-07-26 20:37:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-22 12:13:28 +00:00
|
|
|
void doInputPause() {
|
|
|
|
FPSmanager gFPS;
|
2020-08-23 07:50:05 +00:00
|
|
|
SDL_initFramerate( &gFPS );
|
|
|
|
SDL_setFramerate( &gFPS, 200 );
|
|
|
|
while ( pause ) {
|
|
|
|
SDL_framerateDelay( &gFPS );
|
2020-08-22 12:13:28 +00:00
|
|
|
pollEventsPause();
|
2020-08-23 07:50:05 +00:00
|
|
|
if ( !pause )
|
2020-08-22 12:13:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-26 20:37:20 +00:00
|
|
|
int main() {
|
|
|
|
SDLPP::init();
|
2020-08-23 07:50:05 +00:00
|
|
|
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 );
|
2020-08-22 12:13:28 +00:00
|
|
|
active_scene = main_scene;
|
2020-08-23 07:50:05 +00:00
|
|
|
font = std::make_shared< SDLPP::Font >( "testfont.ttf", 96 );
|
|
|
|
addStuff( *main_scene, renderer );
|
|
|
|
player->setMovementSpeed( 0.3 );
|
2020-07-27 18:05:59 +00:00
|
|
|
player->enableGravity();
|
2020-07-26 20:37:20 +00:00
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
pause_scene = std::make_shared< SDLPP::Scene >( renderer );
|
|
|
|
addPause( *pause_scene, renderer );
|
2020-08-22 12:13:28 +00:00
|
|
|
|
2020-07-28 16:13:38 +00:00
|
|
|
int base = SDL_GetTicks();
|
|
|
|
int frames = 0;
|
|
|
|
|
2020-07-26 20:37:20 +00:00
|
|
|
FPSmanager gFPS;
|
2020-08-23 07:50:05 +00:00
|
|
|
SDL_initFramerate( &gFPS );
|
|
|
|
SDL_setFramerate( &gFPS, 60 );
|
|
|
|
std::thread inputThread( doInput, main_scene );
|
2020-08-22 12:13:28 +00:00
|
|
|
inputThread.detach();
|
2020-08-23 07:50:05 +00:00
|
|
|
while ( !quit ) {
|
|
|
|
// SDL_framerateDelay(&gFPS);
|
2020-07-26 20:37:20 +00:00
|
|
|
main_scene->renderScene();
|
2020-08-23 07:50:05 +00:00
|
|
|
if ( pause ) {
|
|
|
|
pause_scene->renderScene( false );
|
2020-08-22 12:13:28 +00:00
|
|
|
}
|
2020-07-26 20:37:20 +00:00
|
|
|
main_scene->presentScene();
|
2020-07-28 16:13:38 +00:00
|
|
|
frames++;
|
2020-08-23 07:50:05 +00:00
|
|
|
if ( SDL_GetTicks() - base >= 1000 ) {
|
2020-07-28 16:13:38 +00:00
|
|
|
base = SDL_GetTicks();
|
2020-08-23 07:50:05 +00:00
|
|
|
printf( "FPS: %d\n", frames );
|
2020-07-28 16:13:38 +00:00
|
|
|
frames = 0;
|
|
|
|
}
|
2020-07-26 20:37:20 +00:00
|
|
|
}
|
|
|
|
}
|