Thread: Sharing resources between game states

  1. #1
    Registered User
    Join Date
    Mar 2009

    Sharing resources between game states

    What is the best way to share resources between game states? To the make the game engine flexible, the resources should be able to be loaded on demand and unloaded only when no states no longer need them. This also includes functions that will be shared by the states.

    Would it be ugly to have one set of functions, shared_init, shared_cleanup in a base class, and then inherit that class and specify custom init/cleanup functions for resources that only the derived state uses? The shared_init/shared_cleanup functions would then need reference counting in order not to cleanup when there are still states around that use the resources.

  2. #2
    Registered User
    Join Date
    Nov 2005
    You could make a resource manager class that counts references to the resources loaded.

    So, pseudo example

    texture* myTex;
    resMgr->GetResource(myTex,  "C:\\Test.png");
    This would load the the texture from the disk ( if the resource does not yet exist, else it would simply increment a reference count )
    Then return a state code ( success, error, file_not_found, etc ), and create a reference to the resource.

    This would decrement the texture reference, and null the myTex pointer.

    This would only free/remove resources with a 0 reference count.

    For this to be safe, you will need to implement copy functionality for the resource classes to handle the reference count to insure a valid count at all times. Or you can disable copying, but this may hinder some things you may want to do.

    Some of the information I have provided may be inaccurate, as I am running purely off memory. If you would like, I can formulate a short code example to demonstrate what I am proposing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. Need book to program game into multiplayer...
    By edomingox in forum Game Programming
    Replies: 3
    Last Post: 10-02-2008, 09:26 AM
  3. So you want to be a game programmer?
    By dxfoo in forum Game Programming
    Replies: 23
    Last Post: 09-26-2006, 08:38 AM
  4. Try my game
    By LuckY in forum A Brief History of
    Replies: 14
    Last Post: 09-15-2004, 11:58 AM
  5. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM