Friday 15 August 2014

opengl - Simulated time in a game loop using c++ -


I am making a 3D game as a hobby of OpenLL and SDL from scratch in the CD and about this area To learn more about programming.

Thinking about the best way to simulate time while running the game. Obviously I have a loop that looks like this:

  void main_loop () {while (! Quit) {handle_events (); DrawScene (); ... SDL_Delay (time_left ()); }}  

I am using SDL Della and Time_th () to maintain the framerate of approximately 33 FPS.

I thought I needed some global variables

  int current_hour = 0; Int current_min = 0; Int num_days = 0; Uint32 prev_ticks = 0;  

Then like a function:

  zero handle_time () {Uint32 current_ticks; Uint32 dticks; Current_ticks = SDL_GetTicks (); Dticks = current_ticks - prev_ticks; // Get the difference from the last time // If the difference is more than 30000 (half minute) the pay scale game is minutes (dticks> = 30000) {prev_ticks = current_ticks; Current_mins ++; If (current_mins = 60) {current_mins = 0; Current_hour ++; } If (current_hour> 23) {current_hour = 0; NUM_DAYS ++; }}}  

and then call the handle_time () function in the main loop.

It compiles and runs (moment to write time in the console using printf) but I am thinking that this is the best way to do this. What are the easy ways or more efficient ways?

I mentioned this before in other game related threads, like always, with the suggestions of Glenn Fidelell Follow

What you want to do is use continuous timestep which you get by depositing the delta of time. If you want 33 updates per second 33, then your constant timestep should be 1/33. You can also call it update frequency by presenting you should reduce the game logic as they are not together. You want the machine to be able to use the short updates frequency as fast as possible while rendering. Here are some sample codes:

  running = true; Unsigned int t_accum = 0, lt = 0, ct = 0; While (running) {while (SDL_PollEvent (and event)) {switch (event.type) {...}} ct = SDL_GetTicks (); T_accum + = ct - lt; Lieutenant = CT; While (t_accum> = timestep) {t + = timestep; / * This is our real time, in millisecond * / T_accum - = timestep; (Std :: vector & gt; unit & gt; :: for iterator en = entities.begin (); en! = Entities.end (); ++ en) {integrated (n, (float) t * 0.001f, Timestep); }} / * This should actually be in a separate thread, a mute x * / std :: vector & lt; Unit & gt; TmpEntities (entities.size ()); For (int i = 0; i & lt; entities.size (+) i) {float alpha = (float) t_accum / (float) timestep; TMPNTI [I] = InterpolateState (Institutions [I] .ST, Alpha, Institutions [IRA] .CRRT, 1.0F - alpha); } Render (tmpEntities); }  

This controls undersmappeling as well as overspelling. If you use integer arithmetic here, then your game physics must be close to 100% deterministic, even if the machine is slow or Be sharp This is the benefit of increasing time in fixed time intervals. The state used for rendering is making a distinction between previous and present states, where the remaining value of time is used in the accumulation process as the interpolation factor. This ensures that rendering is smooth, no matter how big the timestamp is.


No comments:

Post a Comment