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"
bool quit = false;
bool update = false;
std::shared_ptr< Mario > mario = nullptr;
std::shared_ptr< SDLPP::RectangleRender > leftStop = nullptr;
std::shared_ptr< SDLPP::Renderer > renderer = nullptr;
std::shared_ptr< SDLPP::TextRenderer > fps = nullptr;
void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
switch ( key ) {
@ -42,6 +44,10 @@ void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
case SDLK_r:
scene.getRenderer().setRenderColiders(
!scene.getRenderer().getRenderColiders() );
break;
case SDLK_f:
if(fps)
fps->setHidden(!fps->getHidden());
default:
break;
}
@ -93,8 +99,10 @@ void pollEvents( SDLPP::Scene &scene ) {
auto marioPos = mario->getPos();
if ( marioPos.getX() < leftmost ) {
scene.moveEverything( leftmost - marioPos.getX(), 0 );
update = true;
} else if ( marioPos.getX() > rightmost ) {
scene.moveEverything( marioPos.getX() - rightmost, 0 );
update = true;
}
if ( mario->getPos().getX() <
leftStop->getDoubleRect().first.getX() +
@ -136,6 +144,7 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
( playerX > rightBarrier && rightmostX > width ) *
( rightBarrier - playerX ) / width,
0 );
update = true;
}
}
@ -192,6 +201,14 @@ int main() {
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;
SDL_initFramerate( &gFPS );
SDL_setFramerate( &gFPS, 60 );
@ -201,15 +218,20 @@ int main() {
std::thread inputThread( doInput, scene );
inputThread.detach();
scene->moveEverything( -mario->getDoubleRect().first.getX() + 0.2, 0 );
update = true;
while ( !quit ) {
SDL_PumpEvents();
SDL_framerateDelay( &gFPS );
mario->setStanding();
if(update) {
scene->updateSizeAndPosition();
update = false;
}
scene->renderScene();
renderer->presentRenderer();
frames++;
if ( SDL_GetTicks() - base >= 1000 ) {
std::cout << "FPS: " << frames << std::endl;
fps->changeText(std::to_string(frames) + " fps");
frames = 0;
base = SDL_GetTicks();
}