Game Engines
Intro + Foundations
Faculty of Mathematics and Physics
Charles University
10th March 2022
Karel Vlachovský (based on Jakub Gemrot’s presentation)
Based on “Game Engine Architectures”
and “Game Design Patterns” books
and various inet resources
Game Engine�Roadmap
Game Engine�Why?
WHY DO YOU NEED�(3rd party) GAME ENGINE?
Game Engine�Why?
Game Engine�Why?
Game Engine�What?
WHAT IS GAME ENGINE?
Game Engine�What?
Game Engine�What?
Game Engine�What?
HOW DO GAME ENGINE DIFFER FROM �A FRAMEWORK OR A MIDDLEWARE?
Game Framework / Middleware�What?
Game Engine�For whom?
WHOSE WORK GAME ENGINE MUST SUPPORT?
Game Engine�For whom?
Game Engine�For whom?
Game Engine�What for?
WHAT FEATURES MUST GAME ENGINE PROVIDE TO GAMES?
Game Engine�What for?
Game Engine�What for?
Game Engine�What for?
Game Engine�What for?
RULE OF THE THUMB
Game Engine�Architecture Overview
GAME ENGINE ARCHITECTURE OVERVIEW
Game Engine�Architecture Overview – Game Specific
Game Engine�Architecture Overview – Game Specific
Game Engine�Architecture Overview – Game Specific
Game Engine�Architecture Overview – Game “Agnostic” Layer
Game Engine�Architecture Overview – Foundations
Game Engine�Architecture Overview – Platform Level
Game Engine�Platform Independence Layer
Game Engine�Platform Independence Layer
Game Engine�Tools <-> Engine Integration
Independent
Built-in
Game Engine�As an Integrator
GAME ENGINE AS AN INTEGRATOR OF WORK
Game Engine�Asset Pipeline
Game Engine�Resource Management
-> Resource Exporter (exports out of DCC)
-> Resource Compiler (adapts for GE)
-> Resource Linker (creates composite packages)
- e.g., character consists of a model, skeleton, rig,
textures and animations
Game Engine�Resource Management
Game Engine�Resource Management
Game Engine�Resource Management
Game Engine�Architecture Overview
A FEW COMPONENTS…
Game Engine�Core
Game Engine�Core
Game Engine�Rendering Engine
Game Engine�Other components
Game Engine�As Soft Real-time System
GAME ENGINE AS A SOFT REAL-TIME SYSTEM
And why you typically do not want to reinvent them…
Game Engine�As Soft Real-time System
GAME ENGINE MESSAGING SYSTEM
How do subsystems communicate?
Game Engine�As Soft Real-time System
http://www.gamasutra.com/blogs/MichaelKissner/20151027/257369/Writing_a_Game_Engine_from_Scratch__Part_1_Messaging.php
Game Engine�As Soft Real-time System
=>
http://www.gamasutra.com/blogs/MichaelKissner/20151027/257369/Writing_a_Game_Engine_from_Scratch__Part_1_Messaging.php
Game Engine�As Soft Real-time System
LOW-LEVEL OPTIMIZATIONS
Game Engine�As Soft Real-time System
Game Engine�As Soft Real-time System
=> You have to profile, always
Game Engine�As Soft Real-time System
Game Engine�As Soft Real-time System
=> accessing a[++i] can be worse than a[i++]
Game Engine�As Soft Real-time System
uint32_t digits10(uint64_t v) {
uint32_t result = 1;
for (;;) {
if (v < 10) return result;
v /= 10U;
result += 1;
}
}
Game Engine�As Soft Real-time System
uint32_t digits10(uint64_t v) {
uint32_t result = 1;
for (;;) {
if (v < 10) return result;
if (v < 100) return result + 1;
if (v < 1000) return result + 2;
if (v < 10000) return result + 3;
// Skip ahead by 4 orders of magnitude
v /= 10000U;
result += 4;
}
}
Game Engine�As Soft Real-time System
Game Engine�Low-level Systems
MEMORY MANAGEMENT
Game Engine�As Soft Real-time System
Game Engine�As Soft Real-time System
[2016]
1ns = 102x => 100 ns = 102x => 10µs = 102x => 1ms
Game Engine�As Soft Real-time System
[2020]
1ns = 102x => 100 ns = 102x => 10µs = 102x => 1ms
Game Engine�Memory Management
=> Avoid dynamic allocation completely
=> Align your data correctly on every level
Game Engine�Memory Management – Avoiding Dyn. Alloc.
Game Engine�Memory Management – Avoiding Dyn. Alloc.
Game Engine�Memory Management – Aligning Data Struct.
struct InefficientPacking
{
U32 mU1; // 32 bits
F32 mF2; // 32 bits
U8 mB3; // 8 bits
I32 mI4; // 32 bits
bool mB5; // 8 bits
char* mP6; // 32 bits
};
Compiler will not
optimize this!
Because of efficient
CPU Read/Write (CPU works
only with properly aligned data).
Game Engine�Memory Management – Aligning Data Struct.
struct InefficientAccess
{
U32 a; // 32 bits
char[512] txt; // 512*8bits
U32 b; // 32 bits
};
Accessing a and b in a single expression => 2 cache misses
https://akkadia.org/drepper/cpumemory.pdf
Game Engine�Memory Management – Aligning Data Struct.
Game Engine�Low-level Systems
STRING HANDLING
Game Engine�String Handling
Game Engine�String Handling
Game Engine�String Handling
Have the best of both worlds!
Define custom types ID and macros for creating and manipulating those IDs, e.g. TEXT(...). And then, depending on the build type (devel vs. ship), include different implementations!
Game Engine�String Handling
=> No plain text ever used within the code
Game Engine�Loops
GAME ENGINE LOOPS
do { play the game }
while (ExitNotRequested())
Game Engine�Loops
Game Engine�Loops
GAME ENGINE LOOPS
SINGLE THREADED
Game Engine�Loops – Simple Pong Example
void main() // Pong
{
initGame();
while (true) // game loop
{
readHumanInterfaceDevices();
if (quitButtonPressed()) break; // exit the game loop
movePaddles();
moveBall();
collideAndBounceBall();
if (ballImpactedSide(LEFT_PLAYER))
{
incremenentScore(RIGHT_PLAYER);
resetBall();
}
else if (ballImpactedSide(RIGHT_PLAYER))
{
incrementScore(LEFT_PLAYER);
resetBall();
}
renderPlayfield();
}
}
Game Engine�Loops – Simple Pong Example
void main() // ENGINE
{
initEngineAndLoadData();
while (true) // game loop
{
inputSystem.Update ();
if (isExit()) break; // exit the game
physics.tick();
gameplay.tick();
rendering.tick();
}
destroyEngine();
}
Game Engine�Loops – Windows Loop
while (true)
{
// Service any and all pending Windows messages.
MSG msg;
while (PeekMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// No more Windows messages to process -- run one
// iteration of our "real" game loop.
RunOneIterationOfGameLoop();
}
Game Engine�Loops – Callback-Driven
while (true)
{
for (each frameListener)
{
frameListener.frameStarted();
}
renderCurrentScene();
for (each frameListener)
{
frameListener.frameEnded();
}
finalizeSceneAndSwapBuffers();
}
Game Engine�Loops – Event-Driven
Game Engine�Loops
GAME ENGINE LOOPS
MULTI-THREADED
Game Engine�Loops
Game Engine�Loops
Game Engine�Loops
Game Engine�Loops
Game Engine�Loops
Game Engine�Loops
Game Engine�Loops
Game Engine�Loops
Game Engine�Loops
Game Engine�Data-oriented design
DATA ORIENTED DESIGN
Source: https://gamedevelopment.tutsplus.com/articles/what-is-data-oriented-game-engine-design--cms-21052
Source: https://www.youtube.com/watch?time_continue=4&v=yy8jQgmhbAU
Game Engine�Data-oriented design
“Object-oriented programming is [...] both anti-modular and anti-parallel by its very nature, and hence unsuitable for a modern CS curriculum.”
Game Engine�Data-oriented design
Looks like a fork&join model, in theory can be jobified and streamlined.
/ Entity-Component-System (ECS)
Game Engine�Thanks you for you attention!
THAT’S IT FOR TODAY!
Game Engine�Thanks you for you attention!