Jump to content
  • Advertisement
Sign in to follow this  
lukesmith123

Resource Loading

This topic is 2508 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was just wondering if somebody could explain the theory behind resource loading to me.

The thing that I'm confused about is whether all of the resources should be loaded at the start of the game or on a per level basis.

for example some resources might be used in all levels whereas others might be used only in certain levels.

Do we need to keep track of resources to determine whether they need to be unloaded before the next level?

thanks,

Share this post


Link to post
Share on other sites
Advertisement
That will depend on your game and target hardware. In general, for level based games, you load general purpose assets at startup (ui textures and such), then when a level is selected you load the resources for it. A reference counting system is used, and when you are about to switch levels, increase the reference of all resources used by the new level, decrease the reference of all resources used in the previous level, and delete all resources with a reference of 0.
You can also start preloading resources asynchronously near the end of the level, to reduce wait time between levels (loading screens).

Share this post


Link to post
Share on other sites

I was just wondering if somebody could explain the theory behind resource loading to me.


Disks are slow. Ram is fast. Slow access to resources causes choppy gameplay or missing textures/sounds/etc. So textures/sounds/etc are loaded into ram so that the faster access avoids the issues.


The thing that I'm confused about is whether all of the resources should be loaded at the start of the game or on a per level basis.


Whatever is appropriate for your scenario.

Share this post


Link to post
Share on other sites
that's great, thanks for the replies,

turch, I've been learning and beginning to implement reference counting scoped pointers, is this the kind of thing you're talking about?

Share this post


Link to post
Share on other sites

turch, I've been learning and beginning to implement reference counting scoped pointers, is this the kind of thing you're talking about?


True reference counting pointers are pretty hard to get right, and useful for several things, but for inter-level resource management you can go for something much simpler.

The point of ref pointers is to simplify ref counting (don't have to manually addRef / release) and reduce bugs. When switching levels, you're only going to have one place to addRef and one place to release.

Just keep an int for each resource, and when loading a level go through each resource it needs and increase the ref, then go through each resource the previous level needed and decrease the ref.

Its also useful to distinguish between unregistered / registered / loaded resources. Unregistered means the resource manager doesn't know anything about the resource. Registered / unloaded means the manager knows basic stuff about the resource like the path and size, but its not in memory yet. I'll let you figure out what loaded means :)

If you use the resource path (relative path from root game directory) as its id (hashed into an unsigned int for fast comparisons), increasing the ref count for a resource that the manager doesn't know about will automatically register it.

Share this post


Link to post
Share on other sites
What I usually do is have a class which caches resources on load -- I avoid the singleton approach that many (I daresay "most") "resource managers" you can find online use, and it's templated on a resource base type (so, for instance, I can have my texture cache support file-based and procedural textures). Resources are specified as being in one of three categories -- permanent, temporary, or instantaneous -- which basically means don't-delete-me-unless-its-explicit, delete-me-if-memory-is-low, and delete-me-as-soon-as-I'm-disused. Also, because its not based on a singleton, I can create new resource managers on a per-state/per-stage basis -- the design is not to load only one instance app-wide, but one instance within each cache (this is more flexible, if you want one instance app-wide, just make one cache). I create one to hold UI elements and other things that are used consistantly throughout the app, and others on a per-stage basis -- when the stage is over, I just delete the stage's cache and all the resources go along with it. I can also "clone" a resource, such that there are two distinct instances, in case I want to alter copies of it programmatically.

This might be overkill for some -- certainly you can simplify the code by not allowing derived resource types, etc -- but my implementation weighs in at around just 400 lines of (well-engineered, I humbly submit) C++ code, so its pretty tight. It's reference counted using shared_ptr from std:: (std::tr1 at the time) along with other std:: types and containers.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!