Jernej Vičič Roger Mailer Razvoj iger Jernej Vičič Overview chapter 14 startup and shutdown of subsystems memory management containers strings configuration ID: 789938
Download The PPT/PDF document "Support systems 3D Game Development" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Support systems
3D Game Development
Jernej Vičič
Slide2Roger Mailer
Slide3Razvoj iger, Jernej Vičič
Overview (chapter:
14
)
startup
and shutdown of subsystems
,
memory
management
,
containers,
strings,
configuration
of the engine
.
Slide4Start-up and shut-down
Engines are complex ”beasts",at the first startup, some subsystems are set up and configured
,
order
is important because of
dependencies,usually we run them in an order and shut down in the reverse order.4
Slide5Singleton
A common pattern for the engine components is the creation of a controller using a singleton
class
RenderManager
{ public: RenderManager (){ //start up the manager } ~RenderManager (){ //shut down the manager
} };
//singleton instance
static
RenderManager
gRenderManager
5
Slide6Singleton
6
In
software engineering
, the
singleton pattern is a software design pattern that restricts the instantiation of a class to one object.
This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects. The term comes from the
mathematical concept of a singleton
.
„
wikipedia
“
Slide7Initialisation and singleton
C++ makes static objects just before the main method is started,the
order of creation of static objects is arbitrary
,
such
a method is not suitable for order control.7
Slide8Singleton control
one trick to control the order is that the static variable declared in the method is not initialized at startup
class
RenderManager
{ public: static RenderManager& get(){
static RenderManager
sSingleton
;
return
sSingleton
;
}
RenderManager
(){
VideoManager
::get();
TextureManager::get(); } ~RenderManager (){ //shut down the manager } };for startup this method is OK, shutdown cannot be controlled.
8
Slide9Do it directly
we make a startup and shutdown method in all the managers,easy,understandable,easy
to debug and support
,
you
can use singletons where main method creates objects with the new operator.9
Slide10Memory Management
the capability of an engine does not depend solely on our algorithms, it depends also on memory usage,malloc() and new are slow
,
the way the memory is accessed and
memory fragmentation greatly affect cache performance
.10
Slide11Dynamic Allocation
Heap allocation is slow because it must be general,it allows requirements from 1 byte to 1GB,
causes
a lot of additional work (overhead
),
slow also because of context switching,first switch from user to kernel space,do the work,switch back to user space,we can not completely avoid dynamic allocation, but we can prepare our own alocators that know the usage patterns and the size of our data.11
Slide12Stack based allocator
the easiest way is to use an already allocated block of memory and use it as a stack,when a game level is loaded, it is added to the
stack
when it
ends,
just return the stack to the pointer backwards, while in the next allocation we just overwrite the old level,the order is important, otherwise we can delete useful information,rollback markers are often used.12
Slide13Stacks
13
Slide14Double ended
stackdouble ended stack,
add bigg blocks on one side and small on the other.
14
Slide15Pool allocator
allocate large number of blocks of fixed size - a pool of 1000 4X4 matrices,when we need a new matrix, we pick it up from the pool
,
return to the pool after usage
it
,the pool can be implemented as a linked list.15
Slide16Alligned allocation
the problem with the "pool" memory is that each variable must take into account the allignment,the allocator must return aligned memory, otherwise
we have problems
, we
can easily correct this if we are aware of this
:16
Slide17Single and double frames
we often need to allocate memory in every frame,this can be solved with a single frame buffer cache for all frames,we only
allocate
once and release when the entire rendering is complete
,
very fast and easy, we must be aware of what we are doing,potentially dangerous for memory leak,it may happen that we write in this buffer before the previous frame has “consumed" the content – overwrite useful content.17
Slide18Single and double frames
we often need to allocate memory in every frame,a double frame buffer is often a better choice for multi-core setup,we alocate memory in frame i
for the
use in i + 1 frame
.
18
Slide19Fragmentation
dynamic allocation can cause fragmented memory,it slows down the memory copy,
we
can run out of large pieces of memory
,
pools and stack allocators solve this problem.19
Slide20Fragmentation
if we need random
allocation/deallocation,
we may need to defragment the memory
:
20
Slide21Shift
21
Slide22Defrag
defragmentation can take place through multiple frames,we simply fold things together slowly,
moving
the memory is not easy
,
we need to fix all the pointers,we often use handles,the handle points to a fixed location in memory that contains the right pointer on the object,basically it's a pointer to the pointer.22
Slide23Cache
avoid caching errors:small pieces of data,continious
in memory
,
access
them sequentially (do not skip),we need to be aware that the instructions are also in the cache.23
Slide24I-Cache
compiler and linker take care of the code representation in memory,we can help:the
function code is continuous in memory
,
functions
are stored in the basic order,functions in the same file are almost always continuous in memory.24
Slide25Optimisation
high-performance code should be short,do not use function calls in performance-critical sections,if
you need to call
a
function, place it as close as possible to the caller, never to another file
,inline functions can inflate the code.25
Slide26Containers
Data structures:ArrayDynamic Array
Linked List
Queue
Deque
TreeBinary Search TreeBinary heapPriority QueueDictionarySetGraphDirected acyclic graphmany of them can be found in STL (Standard Template Library)26
Slide27Container operations
InsertRemoveSequential access (iteration)Random accessFindSort
different containers have different time complexities for individual operations
.
27
Slide28Iterators
STL has an iterator class, that is similar to Java Iterator (it is the other way around actually),easyer than pointer manipulation,
void
processList
(
std::list<int>& container){ std::list<int>::iterator pBegin = container.begin(); std::list<int>::iterator pEnd = container.end();
std::list<int>::iterator p;
for (p =
pBegin
; p !=
pEnd
; p++)
{
int
element = *p;
}
}
28
Slide29Personal containers
many reasons:complete control (algorithms, memory usage,...),optimisation possibility - for HW and for own algorithms
,
special
functionality (for a specific purpose
),no dependence on external factors (licenses, errors can be solved),control of parallel data structures - complete control over parallel access.29
Slide30Standard Template Library
pros:rich functionality,robust implementations on different platforms,standard with most C ++ compilers
,
cons:
steep learning curve,
often slower than proprietary data structures,”eats" a lot of memory,uses dynamic allocation,performance depends on the compiler.30
Slide31STL usage
be aware of performance and memory features,avoid large use of STLs in critical parts of the program,do not use when you are on the border with memory
,
use
STL if you are doing a multiplatform game
.31
Slide32Boost
STL extensionpros:allows things that are not in the STL,offers some solutions to STL
issues,
good
documentation
,cons:most of the basic classes are templates, we get large .lib files,there is no guarantees if you encounter a bug ...,Boost License - not limiting.32
Slide33Loki
napisal Andrei Alexandrescu,močno, težko za uporabo,težko prenosljivo, uporablja zahtevne trike v prevajalnikih,
dobra knjiga:
Modern C++ Design
by Alexandrescu33
Slide34Nizi - strings
nizi so ZELO pomembni v igrah,niso enostavni za ravnanje,kaj če moramo niz povečat,
problemi z lokalizacijo,
različni kodni nabori,
različne dolžine prevodov,
različni display layouts.34
Slide35Razredi za nize
prinašajo overhead,copy konstruktorji pri klicu funkcij,dinamično
alociranje
spomina,
ponavadi boljše kar fixed sized wchar_t arrays.35
Slide36Unikatni IDji
objekti v igri morajo biti identificirani,nizi se zdijo kot idealna izbira,primerjave so drage,GUID (
števila
)
so veliko hitrejša za primerjavo,
težje pa si jih MI zapomnimo.36
Slide37Hashed String IDs
lahko uporabimo zgoščevalne funkcije hash functions za mapiranje nizov v števila,
boljše iz obeh svetov,
možne kolizije,
dobra zgoščevalna funkcija reši ta problem,
uint32 za vrednosti nam nudi 4 milijarde možnosti,včasih so klicani tudi string id.37
Slide38Ideje za implementacijo
zgoščevanje med delom je počasno,to lahko opravimo offline na izvorni kodi,poišči klice funkcije in zamenjaj z zgoščenim številom,
lahko pa uporabimo statično spremenljivko,
ali pa uporabimo
intern
thestring ids:niz damo v intern bazen in dobimo unikaten ID,če vprašamo bazen po ID dobimo naš niz.38
Slide39Lokalizacija
planirajte lokalizacijo že od začetka,ASCII kode ne omogočajo lokalizacije,razmišljajte izklučno
Unicode
!
39
Slide40Unikod I.
Unikod (Unicode oz. ISO 10646)
1991 – Unicode Consortium
:
http://www.unicode.org/
definira univerzalni nabor znakovvsebuje 30 svetovnih abeced, ki pokrivajo več sto jezikov, definiranih približno 40.000 znakov…arabščina, sanskrt, kitajščina, japonščina, korješčina,…tudi zgodovinske pisave, ločila, matematični simboli, naglasna znamenja,..Unikod razdeli znake v „bloke“ npr. Basic Latin, Latin-1 Supplement, Latin Extended-A, Latin Extended-B, IPA Extensions, Combining Diacritical Marks, Greek, Cyrillic, …
Slide41Slovene letters
Slide42Unikod definicije za IPA
Slide43Unicode II.
ISO 8859, Windows-CP,... used
only
256
codes (characters) and needed only one byte,Unicode can store many more characters1 character ≠ 1 byte, solution?
There are many Unicode standard notations:UTF-32
1
character
–
4 bytes
UTF-16
1
character
–
2
bytes
(
for „basic“ characters)UTF-8changable length: 1-6 bytes for a character,ASCII characters are written in 1 byte (compatibility),slovenian/czech characters are coded in 2 bytes.
Slide44char and wchar_t
standard C/C++ defines two data types for characters,char
is used for old ANSI
and WCS strings,
wchar_t
should be used for all rest:8, 16, or 32 bits,if we want a totaly platform independent code, we must define proprietary data types for characters.44
Slide45Unicode in Windowsi
v Windowsih je wchar_t
samo
UTF-16
char
za ANSI kodiranje,Windows API definira tri množice character/string funkcijobstajajo pretvorbe wcstombs().ANSIWCSMBCS
strcmp()wcscmp()
_
mbscmp
()
strcpy
()
wcscpy
()
_
mbscpy
()
strlen
()
wcslen()
_
mbstrlen()45
Slide46Drugi lokalizacijski problemi
prevesti moramo več kot samo nizeaudio clips,
teksture
–
če imajo angleške besede,
simboli – z menjavo jezikov lahko spremenijo pomen,lokalizacijska podatkovna baza,potrebujemo način za prevedbo id-jev v nize v izbranem jeziku,format je vaša izbira, od csv do prave bazeIdEnglishFrenchp1score
“Player 1 Score”“Grade Joueur 1”
p2score
“Player 2
Score”
“Grade
Joueur
2”
p1wins
“Player one wins!”
“
Joueur
un
gagne
!”
p2wins
“Player two wins!”“Joueur deux gagne!”46
Slide47Konfiguracija engina
večina enginov potrebuje zmožnost snemanja in nalaganja konfiguracijskih datotek,več načinov:
besedilne datoteke,
kompresirane
binarne datoteke,
windows registry,command line options,spremenljivke okolja,uporabniški profili (online).