Resource Management

The Resource Management system in GLEED2D is designed to provide a robust and flexible way to manage game assets like images, sounds, and fonts.

Core Concepts

  • Resource Manager: This is the central object responsible for loading, managing, and unloading resources. It offers a variety of options for loading, caching, and accessing resources.
  • Resource Types: GLEED2D supports various resource types, including images, sounds, fonts, and custom types. Each type has its own loading and management mechanisms.
  • Resource Handles: These are references to loaded resources, allowing access to the actual data.
  • Resource Groups: Resources can be grouped together, enabling efficient loading and unloading of related assets.
  • Resource Loading: Loading resources can occur in several ways:
    • Explicit Loading: Manually loading resources at runtime using the Resource Manager’s LoadResource() method.
    • Implicit Loading: Loading resources automatically when they are first accessed through a Resource Handle.
    • Group Loading: Loading all resources within a specific group.
  • Resource Unloading: Resources can be unloaded to free up memory.
    • Explicit Unloading: Manually unloading resources at runtime using the Resource Manager’s UnloadResource() method.
    • Group Unloading: Unloading all resources within a specific group.
    • Automatic Unloading: Unloading resources that haven’t been accessed for a specified period.

Implementation

Resource Manager

The Resource Manager class (gleed::ResourceManager) provides the core functions for managing resources.

Code Example:

// Load an image resource
          auto imageHandle = resourceManager.LoadResource<gleed::Image>("my_image.png");
          
          // Access the image data
          if (imageHandle.IsValid()) {
            auto& image = *imageHandle;
            // Use the image data
          }
          
          // Unload the image resource
          resourceManager.UnloadResource(imageHandle);
          

Source:

src/gleed/ResourceMgr.cpp

Resource Types

Each supported resource type has its own class with specific loading and management functions. For example, the gleed::Image class handles loading and accessing image data.

Resource Groups

Resource groups are represented by the gleed::ResourceGroup class, which allows grouping related resources and managing them together.

Source:

src/gleed/ResourceGroup.cpp

Resource Loading Options

The Resource Manager provides different loading methods:

  • Explicit Loading: Use the LoadResource() method with the resource type and filename.
  • Implicit Loading: Accessing the resource using a ResourceHandle will automatically load the resource if it hasn’t been loaded already.
  • Group Loading: Load all resources in a group using LoadGroup().

Source:

src/gleed/ResourceMgr.cpp

Resource Unloading Options

Unloading resources allows reclaiming memory.

  • Explicit Unloading: Use the UnloadResource() method with the ResourceHandle.
  • Group Unloading: Unload all resources in a group using UnloadGroup().
  • Automatic Unloading: Set a time limit for unloading unused resources using SetAutoUnloadTimeout().

Source:

src/gleed/ResourceMgr.cpp

Resource Handles

Resource Handles are references to loaded resources. Use the IsValid() method to check if the handle is valid.

Source:

src/gleed/ResourceHandle.h

Custom Resource Types

GLEED2D supports extending the Resource Manager to handle custom resource types by implementing the gleed::IResource interface.