@ -70,18 +70,67 @@ struct BoxTest Box2d_setup(b2World &world) {
return box ;
}
int main ( ) {
fmt : : print ( " Setting up a window for you... \n " ) ;
sf : : ContextSettings settings ;
settings . antialiasingLevel = 8 ;
sf : : RenderWindow window ( sf : : VideoMode ( 1920 , 1080 ) , " Simple Game Demo " , sf : : Style : : Default , settings ) ;
void Handle_events ( sf : : RenderWindow & window , BoxTest & box ) {
sf : : Event event ;
// is this a main event loop
while ( window . pollEvent ( event ) ) {
ImGui : : SFML : : ProcessEvent ( window , event ) ;
switch ( event . type ) {
case sf : : Event : : Closed :
fmt : : print ( " Exiting... \n " ) ;
window . close ( ) ;
break ;
case sf : : Event : : KeyPressed :
if ( sf : : Keyboard : : isKeyPressed ( sf : : Keyboard : : Left ) ) {
b2Vec2 force ( - 200 , 1000 ) ;
box . body - > ApplyForceToCenter ( force , true ) ;
} else if ( sf : : Keyboard : : isKeyPressed ( sf : : Keyboard : : Right ) ) {
b2Vec2 force ( 200 , 1000 ) ;
box . body - > ApplyForceToCenter ( force , true ) ;
}
break ;
case sf : : Event : : MouseButtonPressed :
if ( sf : : Mouse : : isButtonPressed ( sf : : Mouse : : Left ) ) {
b2Vec2 force ( - 200 , 1000 ) ;
box . body - > ApplyForceToCenter ( force , true ) ;
box . body - > ApplyTorque ( 100.0f , true ) ;
} else if ( sf : : Mouse : : isButtonPressed ( sf : : Mouse : : Right ) ) {
b2Vec2 force ( 200 , 1000 ) ;
box . body - > ApplyForceToCenter ( force , true ) ;
box . body - > ApplyTorque ( - 100.0f , true ) ;
}
break ;
}
}
}
// window.setFramerateLimit(60);
window . setVerticalSyncEnabled ( true ) ;
sf : : Time Update_entities ( sf : : RenderWindow & window , b2World & world , sf : : Clock & clock , sf : : Clock & deltaClock , sf : : Time & tick , BoxTest & box , sf : : Shape & shape ) {
sf : : Vector2u winSize = window . getSize ( ) ;
float timeStep = 1.0f / 60.0f ;
int velocityIterations = 6 ;
int positionIterations = 2 ;
sf : : Time since = clock . getElapsedTime ( ) ;
sf : : Time nextTick = since - tick > sf : : seconds ( 1 ) ? since : tick ;
ImGui_setup ( window ) ;
world . Step ( timeStep , velocityIterations , positionIterations ) ;
b2Vec2 position = box . body - > GetPosition ( ) ;
float angle = box . body - > GetAngle ( ) ;
shape . setPosition ( position . x * 100.0f , winSize . y - position . y * 100.0f ) ;
shape . setRotation ( angle * 180.0f / M_PI ) ;
ImGui_update ( window , deltaClock , tick ) ;
Window_update ( window , shape ) ;
return nextTick ;
}
sf : : CircleShape Create_player ( sf : : RenderWindow & window ) {
sf : : CircleShape shape ( 50.f , 4 ) ;
sf : : Vector2u winSize = window . getSize ( ) ;
shape . setPosition ( winSize . x / 2 , winSize . y / 2 ) ;
@ -90,82 +139,32 @@ int main() {
shape . setOutlineThickness ( 10.f ) ;
shape . setOutlineColor ( sf : : Color ( 250 , 150 , 100 ) ) ;
return shape ;
}
int main ( ) {
fmt : : print ( " Setting up a window for you... \n " ) ;
sf : : ContextSettings settings ;
settings . antialiasingLevel = 8 ;
sf : : RenderWindow window ( sf : : VideoMode ( 1920 , 1080 ) , " Simple Game Demo " , sf : : Style : : Default , settings ) ;
window . setFramerateLimit ( 60 ) ;
window . setVerticalSyncEnabled ( true ) ;
ImGui_setup ( window ) ;
sf : : Clock deltaClock ;
sf : : Clock clock ;
sf : : Time tick = clock . getElapsedTime ( ) ;
// very cool, c++11 lambdas doing a 1 second slept thread
sf : : Thread thread ( [ ] ( ) {
for ( int i = 0 ; i < 10 ; i + + ) {
fmt : : print ( " I'm a thread. {} \n " , i ) ;
sf : : sleep ( sf : : seconds ( 1 ) ) ;
}
} ) ;
thread . launch ( ) ;
sf : : CircleShape shape = Create_player ( window ) ;
b2Vec2 gravity ( 0.0f , - 10.0f ) ;
b2World world ( gravity ) ;
BoxTest box = Box2d_setup ( world ) ;
float timeStep = 1.0f / 60.0f ;
int velocityIterations = 6 ;
int positionIterations = 2 ;
while ( window . isOpen ( ) ) {
sf : : Event event ;
// is this a main event loop
while ( window . pollEvent ( event ) ) {
ImGui : : SFML : : ProcessEvent ( window , event ) ;
switch ( event . type ) {
case sf : : Event : : Closed :
fmt : : print ( " Exiting... \n " ) ;
window . close ( ) ;
thread . terminate ( ) ;
break ;
case sf : : Event : : KeyPressed :
if ( sf : : Keyboard : : isKeyPressed ( sf : : Keyboard : : Left ) ) {
b2Vec2 force ( - 200 , 1000 ) ;
box . body - > ApplyForceToCenter ( force , true ) ;
} else if ( sf : : Keyboard : : isKeyPressed ( sf : : Keyboard : : Right ) ) {
b2Vec2 force ( 200 , 1000 ) ;
box . body - > ApplyForceToCenter ( force , true ) ;
}
break ;
case sf : : Event : : MouseButtonPressed :
if ( sf : : Mouse : : isButtonPressed ( sf : : Mouse : : Left ) ) {
b2Vec2 force ( - 200 , 1000 ) ;
box . body - > ApplyForceToCenter ( force , true ) ;
box . body - > ApplyTorque ( 100.0f , true ) ;
} else if ( sf : : Mouse : : isButtonPressed ( sf : : Mouse : : Right ) ) {
b2Vec2 force ( 200 , 1000 ) ;
box . body - > ApplyForceToCenter ( force , true ) ;
box . body - > ApplyTorque ( - 100.0f , true ) ;
}
break ;
}
}
// should this move up to the pollEvent loop?
sf : : Time since = clock . getElapsedTime ( ) ;
if ( since - tick > sf : : seconds ( 1 ) ) {
tick = since ;
}
world . Step ( timeStep , velocityIterations , positionIterations ) ;
b2Vec2 position = box . body - > GetPosition ( ) ;
float angle = box . body - > GetAngle ( ) ;
shape . setPosition ( position . x * 100.0f , winSize . y - position . y * 100.0f ) ;
shape . setRotation ( angle * 180.0f / M_PI ) ;
ImGui_update ( window , deltaClock , tick ) ;
Window_update ( window , shape ) ;
Handle_events ( window , box ) ;
// preparing for refactoring this into a class or struct for everything
tick = Update_entities ( window , world , clock , deltaClock , tick , box , shape ) ;
}
ImGui : : SFML : : Shutdown ( ) ;