240 likes | 261 Views
Game Programming Algorithms and Techniques. Chapter 1 Game Programming Overview. Chapter 1 Objectives. Evolution of Video Game Programming A quick discussion about how the role of the game programmer has changed over the years The Game Loop
E N D
Game Programming Algorithms and Techniques Chapter 1 Game Programming Overview
Chapter 1Objectives • Evolution of Video Game Programming • A quick discussion about how the role of the game programmer has changed over the years • The Game Loop • Cover the basic loop that drives all real-time games • Learn about more advanced multithreaded loops • Time and Games • Learn the difference between "real time" and "game time" • Learn how to make logic a function of delta time • Learn how to implement frame limiting • Game Objects • Types of game objects • Integrating game objects into the game loop
Evolution of Game Programming • The earliest games were more hardware than software. • Atari 2600 (1977) was the first successful generalized gaming system. • Console vs. PC development: • PC hardware changes at a more rapid pace, which means games can be more cutting edge. • Console generations are usually every 5-7 years, which means programmers can become more comfortable with their hardware.
Atari Era (1977–1985) Games had to be programmed 100% in assembly. There was no software development kit (SDK) and no debugger. Most games were made by one developer.
NES and SNES Era (1985–1995) • North American video game market crashed in 1983, and it wasn't until the NES release that it recovered. • NES/SNES games were also programmed in assembly. • Most NES titles needed a handful of programmers: • The Legend of Zelda (1986) had three. • SNES increased team size, and programmers started specializing: • Super Mario World (1990) had six. • Chrono Trigger (1995) had nine.
Playstation/PS2 Era (1995–2005) • Playstation/N64 were the first consoles to allow high-level programming in C. • Due to productivity of high-level languages, team sizes initially did not increase: • Grand Theft Auto III (2001) had nine. • By the end of the era, team sizes grew: • Full Spectrum Warrior (2005) had 15, many in highly specialized roles.
Xbox 360, PS3, and Wii Era(2005–2013) • HD consoles caused a split in the industry: • AAA games have huge teams now (often 30–50 or more programmers). • Indie developers have very small teams (1–5, total, is not uncommon). • Digital distribution (Steam, XBLA, PSN, iOS App Store, etc.) have made indie development possible. • More developers now use middleware—libraries that implement solutions to common problems.
The Game Loop • Overall flow control for the entire program: • Keeps looping until the user quits out of the game. • Each iteration of the loop is a frame: • Loop usually iterates several times per second. • Most common intervals are 30 and 60. • 60 FPS (frames per second) means the loop completes 60 iterations in 1 second.
Traditional Game Loop while game is running process inputs update game world generate outputs loop
Traditional Game Loop(Process Inputs) • Any input devices, including: • Joystick • Keyboard • Mouse • For online games, network data • Instant replay playback data • On a mobile device, can include GPS or camera
Traditional Game Loop(Update Game World) Iterate through every active game object in the world and update it. Could include hundreds or thousands of objects. Will cover in more detail later in this presentation.
Traditional Game Loop(Generate Outputs) Most common/expensive is graphics. Audio, including sound effects, music, and dialogue. Controller rumble (force feedback). For online game, network data.
Traditional Game Loop Example:Pac-Man whileplayer.lives > 0 // Process Inputs JoystickDataj = grab raw data from joystick // Update Game World update player.position based on j foreachGhostg in world ifplayer collides with g kill either player or g else update AI for g based on player.position end loop // Pac-Man eats any pellets ... // Generate Outputs draw graphics update audio loop
Multi-Threaded Game Loops • Modern CPUs have multiple cores: • Can run multiple lines of execution (threads) at once. • Games must take advantage of all available cores. • Most current AAA games use very complex multithreaded systems.
Multithreaded "Split" Loop • Graphics (rendering) runs on one thread. • Everything else runs on a second thread. • If graphics takes 30 ms/frame and everything else takes 20 ms/frame: • Traditional loop takes 50 ms/frame. • "Split" loop is as slow as the slowest of the two threads, so 30 ms/frame.
Multithreaded "Split" Loop:Disadvantages The jump is delayed a couple of frames due to input lag. Rendering thread must be one frame "behind" the gameplay thread.
Input Lag Amount of time it takes for an action to be visible on screen. The higher the input lag, the more sluggish the game feels. For some genres (such as fighting games), a high input lag is unacceptable.
Real Time vs. Game Time • Real time: • Amount of time elapsed in the real world. • Game time: • Amount of time elapsed in the game world. • If the game is paused, no game time elapsed. • Game may be slower or faster than real time (for example, bullet time is slower). • Some games may even have game time go in reverse.
Logic as a Function of Delta Time The following code will run differently on different systems: enemy.position.x += 5 At 30 FPS, the enemy would move 150 pixels/sec. At 60 FPS, the enemy would move 300 pixels/sec. Instead, make the speed a function of delta time (the amount of elapsed game time since last frame): enemy.position.x += 150 * deltaTime
Game Loop with Delta Time while game is running realDeltaTime = time since last frame gameDeltaTime = realDeltaTime * gameTimeFactor // Process inputs ... update game world with gameDeltaTime // Render outputs ... loop
Problems with "Unlocked" Frame Rate • Basic physics has wildly different behavior based on frame rate: • Characters may jump further at lower frame rates. • Online games may not work properly with variable frame rates. • Solution: • Use frame limiting, which locks the frame rate to a specific value.
Types of Game Objects • Drawn and Updated • Any character, creature, or otherwise movable object that's also visible in the world • In Super Mario Bros.: Mario, any enemies, all dynamic blocks • Only Drawn (Static Objects) • A building in the background of a level • Only Updated • Camera • Trigger
Basic Game Object Implementation • Create a base GameObject class. • Create two interfaces: • Drawable • Updateable • The three game objects then implement a combination of the interfaces. • Create separate lists of drawable and updateable objects, and add to the lists as appropriate.