1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-28 08:48:48 +02:00

Fix hero movement animation

This commit is contained in:
Andrii Danylchenko 2022-06-05 16:20:01 +03:00 committed by Andrii Danylchenko
parent 31c9d6e28d
commit 1128abc593
3 changed files with 44 additions and 35 deletions

View File

@ -348,8 +348,16 @@ void CPlayerInterface::heroMoved(const TryMoveHero & details, bool verbose)
initMovement(details, hero, hp);
auto waitFrame = [&]()
{
int frameNumber = GH.mainFPSmng->getFrameNumber();
auto unlockPim = vstd::makeUnlockGuard(*pim);
while(frameNumber == GH.mainFPSmng->getFrameNumber())
SDL_Delay(5);
};
//first initializing done
GH.mainFPSmng->framerateDelay(); // after first move
//main moving
for(int i = 1; i < 32; i += 2 * speed)
@ -363,8 +371,7 @@ void CPlayerInterface::heroMoved(const TryMoveHero & details, bool verbose)
//evil returns here ...
//todo: get rid of it
auto unlockPim = vstd::makeUnlockGuard(*pim); //let frame to be rendered
GH.mainFPSmng->framerateDelay(); //for animation purposes
waitFrame(); //for animation purposes
}
//main moving done

View File

@ -473,10 +473,10 @@ void CGuiHandler::renderFrame()
SDL_RenderPresent(mainRenderer);
disposed.clear();
}
mainFPSmng->framerateDelay(); // holds a constant FPS
}
}
CGuiHandler::CGuiHandler()
@ -610,7 +610,9 @@ void CFramerateManager::init()
void CFramerateManager::framerateDelay()
{
ui32 currentTicks = SDL_GetTicks();
timeElapsed = currentTicks - lastticks;
accumulatedFrames++;
// FPS is higher than it should be, then wait some time
if(timeElapsed < rateticks)
@ -618,21 +620,20 @@ void CFramerateManager::framerateDelay()
SDL_Delay((Uint32)ceil(this->rateticks) - timeElapsed);
}
accumulatedTime += timeElapsed;
accumulatedFrames++;
if(accumulatedFrames >= 100)
{
//about 2 second should be passed
fps = static_cast<int>(ceil(1000.0 / (accumulatedTime/accumulatedFrames)));
accumulatedTime = 0;
accumulatedFrames = 0;
}
currentTicks = SDL_GetTicks();
// recalculate timeElapsed for external calls via getElapsed()
// limit it to 1000 ms to avoid breaking animation in case of huge lag (e.g. triggered breakpoint)
timeElapsed = std::min<ui32>(currentTicks - lastticks, 1000);
lastticks = SDL_GetTicks();
accumulatedTime += timeElapsed;
if(accumulatedFrames >= 100)
{
//about 2 second should be passed
fps = static_cast<int>(ceil(1000.0 / (accumulatedTime / accumulatedFrames)));
accumulatedTime = 0;
accumulatedFrames = 0;
}
}

View File

@ -51,6 +51,7 @@ public:
void init(); // needs to be called directly before the main game loop to reset the internal timer
void framerateDelay(); // needs to be called every game update cycle
ui32 getElapsedMilliseconds() const {return this->timeElapsed;}
ui32 getFrameNumber() const { return accumulatedFrames; }
};
// Handles GUI logic and drawing