Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Mar 29, 2005, 11:50:51 AM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: now ResourceManager has the ability to unload resources by priority

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orxonox/trunk/src/lib/util/resource_manager.cc

    r3658 r3660  
    4343}
    4444
     45/**
     46   \returns the Instance to this ResourceManager
     47*/
    4548ResourceManager* ResourceManager::getInstance(void)
    4649{
     
    6063
    6164/**
    62    \brief standard deconstructor
     65   \brief standard destructor
    6366*/
    6467ResourceManager::~ResourceManager (void)
    6568{
    66 
     69  // deleting the Resources-List
     70  unloadAllByPriority(RP_GAME);
    6771  delete resourceList;
    6872  resourceList = NULL;
     73  // deleting the Directorie Lists
     74  char* tmpDir = imageDirs->enumerate();
     75  while(tmpDir)
     76    {
     77      delete []tmpDir;
     78      tmpDir = imageDirs->nextElement();
     79    }
     80
     81  delete imageDirs;
     82  imageDirs = NULL;
    6983  ResourceManager::singletonRef = NULL;
    7084}
     
    87101}
    88102
     103/**
     104   \brief adds a new Path for Images
     105   \param imageDir The path to insert
     106   \returns true, if the Path was well and injected (or already existent within the list)
     107   false otherwise
     108*/
    89109bool ResourceManager::addImageDir(char* imageDir)
    90110{
     111  // check if the param is a Directory
    91112  if (isDir(imageDir))
    92113    {
    93       char* tmpDir  = new char[strlen(imageDir)+1];
     114      // check if the Directory has been added before
     115      char* tmpDir = imageDirs->enumerate();
     116      while(tmpDir)
     117        {
     118          if (!strcmp(tmpDir, imageDir))
     119            {
     120              PRINTF(4)("Path %s already loaded\n", imageDir);
     121              return true;
     122            }
     123          tmpDir = imageDirs->nextElement();
     124        }
     125      // adding the directory to the List
     126      tmpDir  = new char[strlen(imageDir)+1];
    94127      strcpy(tmpDir, imageDir);
    95128      imageDirs->add(tmpDir);
     129      return true;
    96130    }
    97131  else
    98132    {
    99133      PRINTF(1)("%s is not a Directory, and can not be added to the Paths of Images\n", dataDir);
     134      return false;
    100135    }
    101136}
     
    104139   \brief loads resources
    105140   \param fileName The fileName of the resource to load
     141   \param prio The ResourcePriority of this resource (will only be increased)
    106142   \returns a pointer to a desired Resource.
    107143*/
    108 void* ResourceManager::load(const char* fileName)
     144void* ResourceManager::load(const char* fileName, ResourcePriority prio)
    109145{
    110146  ResourceType tmpType;
     
    126162    tmpType = IMAGE;
    127163
    128   return ResourceManager::load(fileName, tmpType);
     164  return ResourceManager::load(fileName, tmpType, prio);
    129165}
    130166
     
    133169   \param fileName The fileName of the resource to load
    134170   \param type The Type of Resource to load (\see ResourceType)
     171   \param prio The ResourcePriority of this resource (will only be increased)
    135172   \returns a pointer to a desired Resource.
    136173*/
    137 void* ResourceManager::load(const char* fileName, ResourceType type)
     174void* ResourceManager::load(const char* fileName, ResourceType type, ResourcePriority prio)
    138175{
    139176  // searching if the resource was loaded before.
    140177  Resource* tmpResource = ResourceManager::locateResourceByName(fileName);
    141   char* tmpDir;
    142178  if (!tmpResource) // if the resource was not loaded before.
    143179    {
     180      char* tmpDir;
    144181      // Setting up the new Resource
    145182      tmpResource = new Resource;
    146183      tmpResource->count = 1;
    147184      tmpResource->type = type;
     185      tmpResource->prio = prio;
    148186      tmpResource->name = new char[strlen(fileName)+1];
    149187      strcpy(tmpResource->name, fileName);
     
    212250      resourceList->add(tmpResource);
    213251      delete []fullName;
    214 
    215252    }
    216253  else
    217254    {
    218255      tmpResource->count++;
     256      if(tmpResource->prio < prio)
     257        tmpResource->prio = prio;
    219258    }
    220259
     
    228267   
    229268*/
    230 bool ResourceManager::unload(void* pointer)
     269bool ResourceManager::unload(void* pointer, ResourcePriority prio)
    231270{
    232271  // if pointer is existent. and only one resource of this type exists.
    233   Resource* tmpResource = ResourceManager::locateResourceByPointer(pointer);
     272  Resource* tmpResource =ResourceManager::locateResourceByPointer(pointer);
    234273  if (!tmpResource)
    235274    {
     
    237276      return false;
    238277    }
    239   tmpResource->count--;
    240   if (tmpResource->count <= 0)
    241     {
    242       // deleting the Resource
    243       switch(tmpResource->type)
     278  else
     279    unload(tmpResource, prio);
     280}
     281
     282bool ResourceManager::unload(Resource* resource, ResourcePriority prio)
     283{
     284  resource->count--;
     285  if (resource->prio <= prio)
     286    {
     287      if (resource->count <= 0)
    244288        {
    245         case OBJ:
    246         case PRIM:
    247           delete (Model*)tmpResource->pointer;
    248           break;
    249         case IMAGE:
    250           delete (Texture*)tmpResource->pointer;
    251           break;
    252         default:
    253           PRINTF(1)("NOT YET IMPLEMENTED FIX FIX\n");
    254           return false;
    255           break;
     289          // deleting the Resource
     290          switch(resource->type)
     291            {
     292            case OBJ:
     293            case PRIM:
     294              delete (Model*)resource->pointer;
     295              break;
     296            case IMAGE:
     297              delete (Texture*)resource->pointer;
     298              break;
     299            default:
     300              PRINTF(1)("NOT YET IMPLEMENTED !!FIX FIX!!\n");
     301              return false;
     302              break;
     303            }
     304          // deleting the List Entry:
     305          PRINTF(4)("Resource %s safely removed.\n", resource->name);
     306          delete []resource->name;
     307          resourceList->remove(resource);
    256308        }
    257       // deleting the List Entry:
    258       PRINTF(4)("Resource %s Safely removed.\n", tmpResource->name);
    259       delete []tmpResource->name;
    260       resourceList->remove(tmpResource);
    261     }
    262   else
    263     PRINTF(4)("Resource not removed, because there are still %d References to it.\n", tmpResource->count);
     309      else
     310        PRINTF(4)("Resource %s not removed, because there are still %d References to it.\n", resource->name, resource->count);
     311    }
     312  else
     313    PRINTF(4)("not deleting resource %s because DeleteLevel to high\n", resource->name);
    264314  return true;
     315}
     316
     317
     318/**
     319   \brief unloads all alocated Memory of Resources with a pririty lower than prio
     320   \param prio The priority to delete
     321*/
     322bool ResourceManager::unloadAllByPriority(ResourcePriority prio)
     323{
     324  Resource* enumRes = resourceList->enumerate();
     325  while (enumRes)
     326    {
     327      if (enumRes->prio <= prio)
     328        unload(enumRes, prio);
     329      enumRes = resourceList->nextElement();
     330    }
    265331}
    266332
Note: See TracChangeset for help on using the changeset viewer.