Mario: added fps counter

This commit is contained in:
zv0n 2021-05-25 20:21:02 +02:00
parent cc6f3f838b
commit 670d5b7c1b

View File

@ -18,9 +18,11 @@
#include "mario.hpp" #include "mario.hpp"
bool quit = false; bool quit = false;
bool update = false;
std::shared_ptr< Mario > mario = nullptr; std::shared_ptr< Mario > mario = nullptr;
std::shared_ptr< SDLPP::RectangleRender > leftStop = nullptr; std::shared_ptr< SDLPP::RectangleRender > leftStop = nullptr;
std::shared_ptr< SDLPP::Renderer > renderer = nullptr; std::shared_ptr< SDLPP::Renderer > renderer = nullptr;
std::shared_ptr< SDLPP::TextRenderer > fps = nullptr;
void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) { void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
switch ( key ) { switch ( key ) {
@ -42,6 +44,10 @@ void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
case SDLK_r: case SDLK_r:
scene.getRenderer().setRenderColiders( scene.getRenderer().setRenderColiders(
!scene.getRenderer().getRenderColiders() ); !scene.getRenderer().getRenderColiders() );
break;
case SDLK_f:
if(fps)
fps->setHidden(!fps->getHidden());
default: default:
break; break;
} }
@ -93,8 +99,10 @@ void pollEvents( SDLPP::Scene &scene ) {
auto marioPos = mario->getPos(); auto marioPos = mario->getPos();
if ( marioPos.getX() < leftmost ) { if ( marioPos.getX() < leftmost ) {
scene.moveEverything( leftmost - marioPos.getX(), 0 ); scene.moveEverything( leftmost - marioPos.getX(), 0 );
update = true;
} else if ( marioPos.getX() > rightmost ) { } else if ( marioPos.getX() > rightmost ) {
scene.moveEverything( marioPos.getX() - rightmost, 0 ); scene.moveEverything( marioPos.getX() - rightmost, 0 );
update = true;
} }
if ( mario->getPos().getX() < if ( mario->getPos().getX() <
leftStop->getDoubleRect().first.getX() + leftStop->getDoubleRect().first.getX() +
@ -136,6 +144,7 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
( playerX > rightBarrier && rightmostX > width ) * ( playerX > rightBarrier && rightmostX > width ) *
( rightBarrier - playerX ) / width, ( rightBarrier - playerX ) / width,
0 ); 0 );
update = true;
} }
} }
@ -192,6 +201,14 @@ int main() {
loadMap( scene, mario, "test_binary2.bin", renderer ); loadMap( scene, mario, "test_binary2.bin", renderer );
auto font = std::make_shared< SDLPP::Font >( "testfont.ttf", 36 );
fps = std::make_shared<SDLPP::TextRenderer>(0.2, 0, 0.78, 0.1, renderer, font, "0fps", "#FFFFFF", "#000000", 0.1, SDLPP_TEXT_RIGHT);
fps->setAlignment(SDLPP::OBJ_END, SDLPP::OBJ_START);
fps->setId(0);
fps->setPermanent();
fps->setHidden(true);
scene->addObject(fps);
FPSmanager gFPS; FPSmanager gFPS;
SDL_initFramerate( &gFPS ); SDL_initFramerate( &gFPS );
SDL_setFramerate( &gFPS, 60 ); SDL_setFramerate( &gFPS, 60 );
@ -201,15 +218,20 @@ int main() {
std::thread inputThread( doInput, scene ); std::thread inputThread( doInput, scene );
inputThread.detach(); inputThread.detach();
scene->moveEverything( -mario->getDoubleRect().first.getX() + 0.2, 0 ); scene->moveEverything( -mario->getDoubleRect().first.getX() + 0.2, 0 );
update = true;
while ( !quit ) { while ( !quit ) {
SDL_PumpEvents(); SDL_PumpEvents();
SDL_framerateDelay( &gFPS ); SDL_framerateDelay( &gFPS );
mario->setStanding(); mario->setStanding();
if(update) {
scene->updateSizeAndPosition();
update = false;
}
scene->renderScene(); scene->renderScene();
renderer->presentRenderer(); renderer->presentRenderer();
frames++; frames++;
if ( SDL_GetTicks() - base >= 1000 ) { if ( SDL_GetTicks() - base >= 1000 ) {
std::cout << "FPS: " << frames << std::endl; fps->changeText(std::to_string(frames) + " fps");
frames = 0; frames = 0;
base = SDL_GetTicks(); base = SDL_GetTicks();
} }