瀏覽代碼

Get rid of the ugly FpsDeltaTime structure.

rexim 1 周之前
父節點
當前提交
d7486354f1
共有 2 個文件被更改,包括 17 次插入39 次删除
  1. 7 0
      common.c
  2. 10 39
      main.c

+ 7 - 0
common.c

@@ -112,6 +112,13 @@ void state_update(State *state, float dt)
     if (!state->paused) {
         switch (state->mode) {
         case MODE_ASCENDING: {
+            // TODOOOOO: display_time should not depend on `dt` AT ALL!
+            //
+            // Capture some sort of timestamp from the start of the application, and depending on the mode
+            // display the time relative to the start accordingly. That way the timer is alway accurate
+            // regardless of the FPS.
+            //
+            // Maybe even wiggle animation should not depend on the `dt`.
             state->displayed_time += dt;
         } break;
         case MODE_COUNTDOWN: {

+ 10 - 39
main.c

@@ -115,41 +115,6 @@ void render_penger_at(SDL_Renderer *renderer, SDL_Texture *penger, float time, i
 }
 #endif
 
-typedef struct {
-    Uint32 frame_delay;
-    float dt;
-    Uint64 last_time;
-} FpsDeltaTime;
-
-FpsDeltaTime make_fpsdeltatime(const Uint32 fps_cap)
-{
-    return (FpsDeltaTime){
-        .frame_delay=(1000 / fps_cap),
-        .dt=0.0f,
-        .last_time=SDL_GetPerformanceCounter(),
-    };
-}
-
-void frame_start(FpsDeltaTime *fpsdt)
-{
-    const Uint64 now = SDL_GetPerformanceCounter();
-    const Uint64 elapsed = now - fpsdt->last_time;
-    fpsdt->dt = ((float)elapsed)  / ((float)SDL_GetPerformanceFrequency());
-    // printf("FPS: %f | dt %f\n", 1.0 / fpsdt->dt, fpsdt->dt);
-    fpsdt->last_time = now;
-}
-
-void frame_end(FpsDeltaTime *fpsdt)
-{
-    const Uint64 now = SDL_GetPerformanceCounter();
-    const Uint64 elapsed = now - fpsdt->last_time;
-    const Uint32 cap_frame_end = (Uint32) ((((float)elapsed) * 1000.0f) / ((float)SDL_GetPerformanceFrequency()));
-
-    if (cap_frame_end < fpsdt->frame_delay) {
-        SDL_Delay((fpsdt->frame_delay - cap_frame_end) );
-    }
-}
-
 int main(int argc, char **argv)
 {
     State state = {0};
@@ -185,9 +150,12 @@ int main(int argc, char **argv)
         secc(SDL_SetTextureColorMod(digits, MAIN_COLOR_R, MAIN_COLOR_G, MAIN_COLOR_B));
     }
 
-    FpsDeltaTime fps_dt = make_fpsdeltatime(FPS);
+    uint64_t last_time = SDL_GetPerformanceCounter();
     while (!state.quit) {
-        frame_start(&fps_dt);
+        uint64_t now = SDL_GetPerformanceCounter();
+        float dt = (float)(now - last_time)/SDL_GetPerformanceFrequency();
+        last_time = now;
+
         // INPUT BEGIN //////////////////////////////
         SDL_Event event = {0};
         while (SDL_PollEvent(&event)) {
@@ -313,10 +281,13 @@ int main(int argc, char **argv)
         // RENDER END //////////////////////////////
 
         // UPDATE BEGIN //////////////////////////////
-        state_update(&state, fps_dt.dt);
+        state_update(&state, dt);
         // UPDATE END //////////////////////////////
 
-        frame_end(&fps_dt);
+        now = SDL_GetPerformanceCounter();
+        Uint32 frame_time = ((now - last_time)*1000.0f)/SDL_GetPerformanceFrequency();
+        Uint32 frame_cap = 1000/FPS;
+        if (frame_time < frame_cap) SDL_Delay(frame_cap - frame_time);
     }
 
     SDL_Quit();