100 likes | 224 Views
UW Extension Certificate Program in Game Development 2 nd quarter: Advanced Graphics. Game program main loop. Goals. Understand the structure of the game’s main loop Know how the other components affect rendering. The game’s main loop. Three things to do, periodically: Gather inputs
E N D
UW ExtensionCertificate Program inGame Development 2nd quarter:Advanced Graphics Game program main loop
Goals • Understand the structure of the game’s main loop • Know how the other components affect rendering
The game’s main loop • Three things to do, periodically: • Gather inputs • Mouse, keyboard, game controller, network • Run the game simulation • Physics, AI, scripting, logic • Return (render) results • Graphics, audio, force-feedback, network Gather inputs Game simulation Execution Data Render results
The game simulation • The key, central part that makes gameplay work • If it doesn’t run, there’s no game • Inputs and results are as loosely tied to it as possible • Multiple kinds: • Constant vs. variable rate • Deterministic vs. non-deterministic • Main graphics-related concern: • Ensure none of the rendering affects the simulation • Isolate… by all means necessary
Non-gameplaysimulation tasks • For instance, physics is part of the simulation • But not all the physics! • Grenade bouncing off walls belongs in the simulation • Sparks bouncing off walls don’t belong in the simulation • Eye candy only • Also, sometimes rendering uses some simulation code • Local simulation, meant to predict or extrapolate • Used to reduce perceived latency
The input • Some input is used for the simulation • Shooting or jumping buttons • Some input is used only for rendering • Mouse cursor or 1st person camera orientation • Latency and granularity are two key concepts here • Low sampling rate: low granularity and higher latency • Latency also affected by the structure of the code
Latency • Time it takes for the player to see the reaction to her actions • It is a factor of many sequential events • Some can be budgeted • Time it takes for the CPU or the GPU to render • Some can’t be controlled • Video frame rate • Human beings can perceive latencies of 100 ms or more • Some can detect much smaller latencies Player presses button 8.33 ms Half the input sample rate Game reads button press 8.33 ms Simulation CPU budget Game simulation finishes 8.33 ms Render CPU budget Game sends frame to GPU 33.33 ms Half the max GPU queue GPU renders frame 8.33 ms Half the video frame rate Frame becomes visible
The rendering • Must aim for a constant, guaranteed framerate • At the screen’s refresh rate • Budgeted for both: CPU and GPU • Must be able to throttle back • If frames take too long to render or simulate • Keep all animation running at the intended speed • Less framerate: more choppiness, not slower animation
Main loop – Constant-step simulation startTime=GetTime(); while(true) { ProcessWindowsmessages(keepithappy) Readsimulationinputs time=GetTime(); while(time–startTime>frameTime) { startTime+=frameTime; Runsimulation } Renderframe((time–startTime)/frameTime) }
Main loop – Variable-step simulation startTime=GetTime(); while(true) { ProcessWindowsmessages(keepithappy) Readsimulationinputs time=GetTime(); Runsimulation(time–startTime) startTime=time; Renderframe }