Games Development 2 Resource Management CO3301 Week 3.

14
Games Development 2 Resource Management CO3301 Week 3

Transcript of Games Development 2 Resource Management CO3301 Week 3.

Page 1: Games Development 2 Resource Management CO3301 Week 3.

Games Development 2Resource Management

CO3301

Week 3

Page 2: Games Development 2 Resource Management CO3301 Week 3.

Today’s LectureToday’s Lecture

1. Resources / Scene set-up

2. Loading Resources

3. Resource Manager

4. Resource Destruction

Page 3: Games Development 2 Resource Management CO3301 Week 3.

Assets / ResourcesAssets / Resources

• A resource or asset is any file that is loaded and used/shared by elements in the game– Meshes, animations, textures, sounds, scripts, etc.

• A typical game use many 1000’s of game assets

• Asset management describes both:– The programming involved in loading and working with asset files– The practical organisation of the enormous number of asset files

created during development

• This lecture is concerned with the first point– Will look at the second later in the module

Page 4: Games Development 2 Resource Management CO3301 Week 3.

Resources at Scene SetupResources at Scene Setup

• Consider creating an entity:– It will require a mesh, textures, some animations, maybe a

behavioural script (see later) and some sound effects– All resources that need to be loaded

• Previously, we loaded such files in SceneSetup• This “hard-coding” of resource loading is inflexible:

– Effectively uses global data to initialise scene– Requires a recompile every time the scene changes

• Improve this with a system to flexibly determine what resources need to be loaded and when

Page 5: Games Development 2 Resource Management CO3301 Week 3.

Resources from TemplatesResources from Templates

• Recall that entities have a template that holds generic data for that entity type

• We could store resource file names here:

Character 1

ID

Position/Rotation

Hit Points

Character 2

ID

Position/Rotation

Hit Points

Character Template

Mesh / File name

Animations / File names

Max Hit Points

Script / File name

• All the template resources could be loaded at one time• …or the resources for each template loaded only when an entity is actually

needed• But certainly don’t need to load anything per-entity

Page 6: Games Development 2 Resource Management CO3301 Week 3.

Level Data/Scripts for ResourcesLevel Data/Scripts for Resources

• Use a level data file / script to drive scene set-up– We will look at XML for this purpose shortly

• Data file provides template data for all entities in the level– Not just those initially present

• At set-up time, parse the level data file and load template resources needed for the level– Template may in turn include further resources, load them also– E.g. the sounds used by the actions of a character

• The level data file could also contain:– The initial level state, esp. starting locations for entities– Other level resources (e.g. background music)

Page 7: Games Development 2 Resource Management CO3301 Week 3.

Example Level DataExample Level Data

• Example setup data file snippet:

EntityTemplates:

BasicWizard(“Wiz1.msh”, “Wizard.anm”, 100) // HP

ArchWizard(“Wiz2.msh”, “Wizard.anm”, 180)

WizardPortal(“Portal.msh”, “”, 1000) // No anims

Entities:

Portal(“WizardPortal”, {100, 0, 200}) // Position

Wizard_Boss(“BasicWizard”, {110, 0 250})

– No wizard entities at first – will be spawned from portal– The “.msh” files will likely include further resources – the mesh’s

textures, which will also be loaded

Page 8: Games Development 2 Resource Management CO3301 Week 3.

Resource Loading IssuesResource Loading Issues

• This system can automatically load all the level resources at set-up time and is fairly flexible– No hard-coding

• But what when the same resource is shared between several entities?– 20 character types, some shared textures / animations– Will load the same resources several times– Need to identify resources that are already loaded

• Also what if an entity isn’t spawned for a long time?– Load on demand, during game, when entity is needed– Need to be careful to avoid excessive game play stutter

Page 9: Games Development 2 Resource Management CO3301 Week 3.

Resource ManagerResource Manager

• A resource manager can be used to handle creation / loading and destruction of resources

• When a resource is requested, it can check to see if it is already loaded– If so, simply return the existing resource

• We can write one for each resource type:– Texture manager, animation manager, sound etc.

• Better to write a more generic resource manager– Using interfaces and inheritance

• This manager can also handle media folders for resources used in the game (c.f. TL-Engine)

Page 10: Games Development 2 Resource Management CO3301 Week 3.

Shared Resources / Resource IDsShared Resources / Resource IDs

• To find if a resource has already been loaded– Search the entire list of loaded resources - maybe slow

• Better to use a hash map for efficiency– Map resource filename to resource pointer– If map contains pointer, then resource already loaded

• We could give each resource a UID, like entities• Such a setup will allow us to store IDs instead of (long)

filename strings in all our templates– Not vital to do this though, more preference

Page 11: Games Development 2 Resource Management CO3301 Week 3.

Resource DestructionResource Destruction

• When should resources be destroyed?

• Destroy all resources at end of a level - simple but:– What about resources shared between levels?– Reduces scope for on-demand loading – not memory efficient– Harder to update resources during play for development /

debugging purposes

• Destroy explicitly – call a function to delete each resource– Better, but how to detect shared resources?– Quite manual

• Or something smarter…

Page 12: Games Development 2 Resource Management CO3301 Week 3.

Reference Counted ResourcesReference Counted Resources

• Each resource keeps track of how many references there are to it– A resource is only deleted once it has no references

• Can be done together with the resource manager:– A resource is requested from the manager

• New resource (not previously loaded) gets reference count of 1

• Previously loaded resource - increase the reference count

– Resources can only be released, by request to the manager. They cannot be explicitly deleted

– Releasing a resource decreases the reference count– When the reference count reaches 0, it is deleted

Page 13: Games Development 2 Resource Management CO3301 Week 3.

Smart PointersSmart Pointers

• Alternatively, can use smart pointers– Effectively a class encapsulating a pointer

• Such a pointer can manage reference counting:– Construction initialises the object’s reference count – Copying the pointer increases the count– If a pointer goes out of scope or is deleted, it decreases the count– The object pointed at released when count reaches 0

• <auto_ptr> is a simple smart pointer in the STL– Not flexible enough in itself, so instead use the following:

• <unique_ptr> and <shared_ptr> are newer STL features– Strongly recommend you research these – widely used

Page 14: Games Development 2 Resource Management CO3301 Week 3.

Reference Counting IssueReference Counting Issue

• There is one significant issue when deleting a resource upon its reference count reaching 0:– Imagine a reference-counted wizard class– We kill all the wizards in a level– Reference count reaches 0, so wizard resources are deleted

• However, maybe the wizards need to respawn– Don’t actually want to delete them yet

• Also this might stutter the game

• Can store a single persistent reference throughout the level if we wish, stopping the deletion– This reference might fit naturally into the level set-up