1010#include <stdlib.h>
1111#include <string.h>
1212
13+ static const double NANOSECONDS_PER_SECOND = 1000000000.0 ;
14+ static const double FPS_CALCULATION_THRESHOLD = 1.0 ; // seconds
15+ static const double FLOATING_POINT_EPSILON = 1e-12 ;
16+
1317struct TimeManager
1418{
15- size_t physicsHz ;
16- double physicsTimeStep ;
17- double maxFrameTime ;
18- size_t maxPhysicsSteps ;
19-
19+ // Hot path variables (accessed every frame) - group together
2020 double accumulator ;
21- HighResTimeT lastTime ;
22-
23- bool firstFrame ;
21+ double physicsTimeStep ;
2422 double timeScale ;
23+ HighResTimeT lastTime ;
24+ HighResTimeT (* now )(void );
25+ double maxFrameTime ;
2526
26- double timeScaleBeforePause ;
27-
28- // Debug Stats
27+ // Configuration (accessed less frequently)
28+ size_t physicsHz ;
29+ size_t maxPhysicsSteps ;
2930 size_t physicsStepsThisFrame ;
30- double averageFps ;
3131
32- // Average
32+ // Statistics (accessed even less frequently)
33+ double averageFps ;
3334 double fpsAccumulator ;
3435 size_t fpsFrameCount ;
36+ double timeScaleBeforePause ;
3537
36- // Time source
37- HighResTimeT ( * now )( void ) ;
38+ // Flags (pack together at the end)
39+ bool firstFrame ;
3840};
3941
4042static inline double Clamp (const double x , const double min , const double max )
@@ -47,7 +49,7 @@ void UpdateFpsStats(TimeManager* tm, const double frameTime)
4749 tm -> fpsAccumulator += frameTime ;
4850 tm -> fpsFrameCount ++ ;
4951
50- if (tm -> fpsAccumulator >= 1.0 )
52+ if (tm -> fpsAccumulator >= FPS_CALCULATION_THRESHOLD )
5153 {
5254 tm -> averageFps = (double )tm -> fpsFrameCount / tm -> fpsAccumulator ;
5355 tm -> fpsAccumulator = 0.0 ;
@@ -124,14 +126,14 @@ FrameTimingData TmBeginFrame(TimeManager* tm)
124126 assert (tm -> physicsTimeStep > 0.0 && "physicsTimeStep must be > 0" );
125127
126128 const HighResTimeT currentTime = tm -> now ();
127- const double deltaTime = fmax ((double )(currentTime .nanoseconds - tm -> lastTime .nanoseconds ) / 1000000000.0 ,
129+ const double deltaTime = fmax ((double )(currentTime .nanoseconds - tm -> lastTime .nanoseconds ) / NANOSECONDS_PER_SECOND ,
128130 DBL_EPSILON );
129131 const double cappedDeltaTime = fmin (deltaTime , tm -> maxFrameTime );
130132 const double scaledFrameTime = cappedDeltaTime * tm -> timeScale ;
131133 tm -> lastTime = currentTime ;
132134 tm -> accumulator += scaledFrameTime ;
133135
134- const double stepsD = floor ((tm -> accumulator + 1e-12 ) / tm -> physicsTimeStep );
136+ const double stepsD = floor ((tm -> accumulator + FLOATING_POINT_EPSILON ) / tm -> physicsTimeStep );
135137 const bool lagging = stepsD > (double )tm -> maxPhysicsSteps ;
136138 const size_t steps = lagging ? tm -> maxPhysicsSteps : (size_t )stepsD ;
137139 tm -> physicsStepsThisFrame = steps ;
0 commit comments