Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm making a game, and right now I'm working on the user interface. I'm just wondering when I should be loading my resources.

Should I do it in small pieces at a time?

  • Load resources for part 1 of menu
  • Run part 1 of menu
  • Free resources for part 1 of menu
  • Load resources for part 2 of menu
  • Run part 2 of menu
  • Free resources for part 2 of menu
  • (So on)

Or all at once?

  • Load resources for all parts of menu
  • Run part 1 of menu
  • Run part 2 of menu
  • (So on)
  • Free resources for all of menu

It would seem to me that the pros of the first method are that my code can be much cleaner, as I can isolate the resources to specific parts of my class:

class UI {
    method run() {
         part1();
         part2();
    }
    method part1() {
         // part1's resources can be declared here
    }
    method part2() {
         // part2's resources can be declared here
    }
}

Whereas with the second method, it will probably run faster, but then all my resources have to be global:

class UI {
    // resources are declared here 
    method run() {
        part1();
        part2();
    }
}

I'm using OpenGL, so right now what I'm doing is loading all my textures and audio at once, but I'm allocating and freeing my Vertex Buffer Objects as they're needed. For those of you who have more experience writing games, what method do you think I should use?

share|improve this question
    
are you asking only about the GUI or about all the resources? –  concept3d Nov 18 '13 at 19:22
    
@concept3d Mostly geared toward GUI as performance isn't that relevant with it. –  Forgive Goto Nov 18 '13 at 22:19

3 Answers 3

up vote 7 down vote accepted

I think it depends on several factors:

How big your game is

If your game is small and can fit memory, it's much easier to load everything at once and keep it in memory.

If your game is strctured as levels, load everything you need and keep it in memory (e.g. UI, main character) and load each level resources when you need it.

The nature of the game

Open world game are usually not stored all in memory, the loading/unloading (streaming) of the game can happen while you are traversing the level. But that also depends on the size of the world and the target platform. Keep in mind that loading resources doesn't mean freeing the VBO objects for example, those can re-used and dynamically updated.

Your target platform

Programming on PC is different from consoles and mobiles. The latter usually have limited memory, so it's more likely you need to load resources on demand.

share|improve this answer

Use neither, because both approaches involve loading and freeing GPU resources at runtime, which is slow and will lead to constant thrashing of video RAM.

Instead what you should so is have knowledge of all UI resources needed at startup time, and load them all during program startup. Then when you need to use a resource for drawing, you just use the resource that's already been loaded.

If you can't have that knowledge (and you may consider it desirable to not require your startup routines to have knowledge of your UI requirements) you can load on-demand, which is a technique that some games use (the Quake series, for example). In that case, once a resource has been loaded, you keep it loaded: don't unload it. Then the next time you need it, you can once again just use the already loaded resource instead of having to load it again.

share|improve this answer

You dont want to constantly free things you loaded, once something is loaded keep it in memory, when its needed again, its already loaded in memory. In addition operations like file reading are expensive in themselves. Generally you should only Free something like that when you will never need it again.

For example if I have a Mesh for a Boss monster, I load it in memory during the transition to the room with the boss. It then stays in memory until the player dies and is teleported away, or it's Defeated. During the boss fight however I do not want to Free the mesh because its still in use.

For GUIs its slightly different, since they will almost always be on screen(excluding cutscenes) they should be loaded at the start of the game, and only be freed when exiting or closing the game. Even in cutscenes, leave them in memory and just don't render them that way when the cutscene is over. you dont need to read in all the GUI elements again.

share|improve this answer
    
This of course works when the memory used by your resources is far lower than the memory you have available for storing them. Sadly this is not always true for memory constrained devices such as smartphones. –  Panda Pajama Nov 19 '13 at 0:53
    
Makes sense, in that case though you would need to consider when its ok to free something and when you should keep it. You would also want to free things in the case that the user goes to the home screen or another application and load them again when the app resumes. That being said, you also have to consider that most smartphones even the low end ones come with 1GB memory, a 200MB memory allocation is large but not unheard of. if your supporting older devices with 1GB or less, then obviously this kind of thing matters more, but the resources should also take that into account and be smaller –  JayPC Dec 31 '13 at 4:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.