Presentation is loading. Please wait.

Presentation is loading. Please wait.

3D Game Development Time and game loop Jernej Vičič.

Similar presentations


Presentation on theme: "3D Game Development Time and game loop Jernej Vičič."— Presentation transcript:

1 3D Game Development Time and game loop Jernej Vičič

2 Overview (chapters in book: 7.1–7.5)
rendering loop; game loop; architectural styles; abstract time plans; time and measuring. 3D Game Development, Jernej Vičič

3 Game loop a computer game is a real-time computer application,
we use different types of time: real time (clock on the wall), time of the game (simulated time), local timetables (sound, animation time, ...), processor cycles (function time). game loop defines how these times are combined, this is how individual components are synchronized. 3D Game Development, Jernej Vičič

4 Game loop most of the components use local times,
usually only three tasks run in parallel: HID input (interaction with the player), game logic (player status, world state, story), feedback (rendering, sound, HID output). technology restrictions: 1-4 (8) processors (maybe some more), limited memory, limited speed. 3D Game Development, Jernej Vičič

5 Game loop 3D Game Development, Jernej Vičič

6 Game loop - http://www.notebookcheck.net/
3D Game Development, Jernej Vičič

7 Game loop – game logic game logic – analogy to business logic,
recipe for updating: player status update: detect the player's input (HID), update the status of the player (observe the limits of the world); 3D Game Development, Jernej Vičič

8 Game loop – logika igre refresh world data: static elements:
optimized by selecting the area of interest logic, elements controlled by the game logic (dynamic elements): sorted by relevance (LOD), update the status. elements managed by AI (complex behavior): aware of the internal state, decision making and enforcement of rules. 3D Game Development, Jernej Vičič

9 Rendering loop The illusion of movement is achieved with the high frequency of the rendering loop. while (!quit) { // fix the camera view according to input or path updateCamera(); // update scene graph (position/orientation of objects) updateSceneGraph(); // Render the scene in “Back Buffer” renderScene(); // swap “Back Buffer” and Front Bufferjem swapBuffers(); } 3D Game Development, Jernej Vičič

10 Game loop – real-time constraints
we have to display graphics with at least 30 FPS (motion illusion), the frequency of other systems may vary: AI (~ 10), input (~ 40), sound (~ 50), stereovision (~ 60), physics (~ 100), haptic feedback (~ 3k), some need synchronization (physics and graphics), engine services all these subsystems: game loop calls the components in the right time. 3D Game Development, Jernej Vičič

11 Game loop – possible implementations
coupled approach: update/render in one loop update render 3D Game Development, Jernej Vičič

12 Game loop – coupled approach
primer (Pong): int main () { initGame(); // init while (true) { // game loop readHumanInterfaceDevices(); if (quitButtonPressed()) break; // end game loop movePaddles(); moveBall(); if (scored()) { updateScore(); resetBall(); } renderScore(); // render new state renderPaddles(); renderBall(); return 0; 3D Game Development, Jernej Vičič

13 Game loop – coupled approach
advantages: both routines have the same priority, logic and presentation are united, shortcomings: the change in the complexity of one of the routines affects the another, we have no control over how often a routineis executed. 3D Game Development, Jernej Vičič

14 Game loop – possible implementations
two threads with different frequencies: Engine update render 3D Game Development, Jernej Vičič

15 Game loop – združen pristop
primer (Pong): Engine.cpp initGame(); // Set up initial configuration startUpdater(60); // Start the update loop (60 Hz) startRenderer(30); // Start the rendering loop (30 Hz) Update.cpp while (true) { // zanka Timer(60); readHumanInterfaceDevices(); if (quitButtonPressed()) exit(0); movePaddles(); moveBall(); if (scored()) { updateScore(); resetBall(); } Render.cpp while (true) { // zanka Timer(30); renderScore(); renderPaddles(); renderBall(); } 3D Game Development, Jernej Vičič

16 Game loop – dve niti z ločenima frekvencama
pros: the rendering and update loops are performed at different frame rates, cons: not all the hardware is suitable for the implementation of threads (single-processor, timing problems), problems with synchronization (two threads access the same data). 3D Game Development, Jernej Vičič

17 Game loop – possible implementations
one thread, open loop: render clock upodobitev 3D Game Development, Jernej Vičič

18 Game loop – one thread, open loop
example (Pong): int main () { initGame(); float lastCall = getTime(); // internal computer clock while (true) { // Game loop if (getTime()-lastCall > 1/FREQ) {// timer readHumanInterfaceDevices(); if (quitButtonPressed()) break; movePaddles(); moveBall(); if (scored()) { updateScore(); resetBall(); } lastCall = getTime(); } // using MAX frequency for render renderScore(); renderPaddles(); renderBall(); return 0; 3D Game Development, Jernej Vičič

19 Game loop – one thread, open loop
advantages: better control than one thread and simpler implementation than multiple threads (no synchronization), weaknesses: assumes that tick hours lasts 0 time, alt-tab destroys synchronization, does not allow higher frequency nests. 3D Game Development, Jernej Vičič

20 Game loop – possible implementations
one thread, open loop, rendering and update is frequency dependent: Engine clock clock update update render 3D Game Development, Jernej Vičič

21 Game loop – one loop, open loop
primer (Pong): int main () { HID.setFrequency(20); Paddles.setFrequency(10); Ball.setFrequency(10); while (true) { // Zanka igre HID.update(); if (quitButtonPressed()) break; Paddles.update(); Ball.update(); if (scored()) { updateScore(); resetBall(); } lastCall = getTime(); // render frequency “as fast as possible” Score.render(); Paddles.render(); Ball.render(); return 0; 3D Game Development, Jernej Vičič

22 Game loop – one thread, open loop
Advantages: allows separate frequencies for each entity in the game, the same mechanism can also be used for rendering, generic auto registration. Shortcomings: manually determine the frequency for each entity, we need an entry point for each update (the number can be high). 3D Game Development, Jernej Vičič

23 Game loop what we do if the time between two updates is much greater than the desired frequency: nothing (the game runs slower) (we are screwed), the update of the logic of the game should still take place in real time: ’visual gaps’ (poor man’s choice). 3D Game Development, Jernej Vičič

24 Game loop solution: speed up the update (if we can/know how)
reduce the frequency (if it can be done - if it does not disturb), we use LoD for game logic (we are not dealing with all entities). 3D Game Development, Jernej Vičič

25 Game loop solution: speed up rendering: graphic LoD,
reduce the resolution, reduce the number of effects, automatically with real-time profilers (detect and reduce accuracy - engine). 3D Game Development, Jernej Vičič

26 Threads and synchronization
A piece of code is thread-safe if it only manipulates shared data structures in a manner that guarantees safe execution by multiple threads at the same time. a challenging task to ensure consistency, not all libraries and engines are thread-safe. Razvoj iger, Jernej Vičič

27 Threads and synchronization
Which subsystem has thread control? Input Control (HiD), main engine, game logic, component that creates threads, ? 3D Game Development, Jernej Vičič

28 Threads and synchronization
this is where we should go through paralle programming basics, let us assume that we already know the basics, still, have a look at: critical section, semaphore, mutex, conditional variables. 3D Game Development, Jernej Vičič


Download ppt "3D Game Development Time and game loop Jernej Vičič."

Similar presentations


Ads by Google