Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update frame only when necessary and fix idle animation speed #26

Merged
merged 1 commit into from
Nov 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion content/setting/animation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Nodes:
AnimationNode:
name: idle
sprite: idle.png
framerate: 10
framerate: 5
loop: true
GrabNode:
name: grab
Expand Down
2 changes: 1 addition & 1 deletion content/setting/setting.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Game:
FPS: 10 # min 1
FPS: 60 # min 1
Scale: 4 # min 1
RandomSeed: -1 # -1: use random seed
Physic:
Expand Down
51 changes: 30 additions & 21 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,8 @@ struct GameData
Vec2i windowPos = {0.f, 0.f};
Vec2i petPosLimit = {0, 0};

bool shouldUpdateFrame = true;

// Resources
std::unique_ptr<Framebuffer> pFramebuffer = nullptr;

Expand Down Expand Up @@ -1182,44 +1184,50 @@ class SpriteAnimator
protected:
SpriteSheet* pSheet = nullptr;
float timer;
float maxTimer;
bool loop;
bool isEnd;
int frameRate;

int indexCurrentAnimSprite;

public:
void play(SpriteSheet& inSheet, bool inLoop, int inFrameRate)
void play(GameData& data, SpriteSheet& inSheet, bool inLoop, int inFrameRate)
{
pSheet = &inSheet;
loop = inLoop;
frameRate = inFrameRate;
indexCurrentAnimSprite = 0;
timer = 0.f;
maxTimer = pSheet->getTileCount() / (float)frameRate;
isEnd = false;
data.shouldUpdateFrame = true;
}

void update(double deltaTime)
void update(GameData& data, double deltaTime)
{
if (!isDone())
{
timer += deltaTime;
indexCurrentAnimSprite = timer * frameRate;

if (indexCurrentAnimSprite >= pSheet->getTileCount())
while (timer >= maxTimer)
{
if (loop)
{

indexCurrentAnimSprite = 0;
timer -= pSheet->getTileCount() / (float)frameRate;
timer -= maxTimer;
}
else
{
indexCurrentAnimSprite = pSheet->getTileCount() - 1;
timer = 0.f;
isEnd = true;
}
}

if (indexCurrentAnimSprite != (int)(timer * frameRate))
{
data.shouldUpdateFrame = true;
indexCurrentAnimSprite = timer * frameRate;
}
}
}

Expand Down Expand Up @@ -1343,13 +1351,13 @@ class AnimationNode : public StateMachine::Node
void onEnter(GameData& blackBoard) override
{
StateMachine::Node::onEnter(blackBoard);
spriteAnimator.play(spriteSheets, loop, frameRate);
spriteAnimator.play(blackBoard, spriteSheets, loop, frameRate);
}

void onUpdate(GameData& blackBoard, double dt) override
{
StateMachine::Node::onUpdate(blackBoard, dt);
spriteAnimator.update(dt);
spriteAnimator.update(blackBoard, dt);
}

void onExit(GameData& blackBoard) override
Expand Down Expand Up @@ -1979,19 +1987,23 @@ class Game
const std::function<void(double)> unlimitedUpdateDebugCollision{[&](double deltaTime) {
processInput(datas.window);

physicSystem.update(deltaTime);

// poll for and process events
glfwPollEvents();
}};
const std::function<void(double)> limitedUpdate{[&](double deltaTime) {
// render
initDrawContext();
pet.update(deltaTime);

pet.draw();
if (datas.shouldUpdateFrame)
{
// render
initDrawContext();

// swap front and back buffers
glfwSwapBuffers(datas.window);
pet.draw();

// swap front and back buffers
glfwSwapBuffers(datas.window);
datas.shouldUpdateFrame = false;
}
}};

const std::function<void(double)> limitedUpdateDebugCollision{[&](double deltaTime) {
Expand Down Expand Up @@ -2021,16 +2033,13 @@ class Game
mainLoop.emplaceTimer(
[&]() {
physicSystem.update(1.f / datas.physicFrameRate);

pet.update(1.f / datas.physicFrameRate);
},
1.f / datas.physicFrameRate, true);

mainLoop.start();
while (!glfwWindowShouldClose(datas.window))
{
mainLoop.update(datas.debugEdgeDetection ? unlimitedUpdateDebugCollision : unlimitedUpdate,
datas.debugEdgeDetection ? limitedUpdateDebugCollision : limitedUpdate);
mainLoop.update(unlimitedUpdate, datas.debugEdgeDetection ? limitedUpdateDebugCollision : limitedUpdate);
}
}
};
Expand Down