Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jan 25, 2006, 2:19:46 PM (18 years ago)
Author:
patrick
Message:

branches: removed spaceshipcontrol branche

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/network/src/util/loading/resource_manager.cc

    r6467 r6693  
    1818#include "resource_manager.h"
    1919
     20#include "substring.h"
    2021#include "debug.h"
    2122
    2223#include <algorithm>
     24#include <assert.h>
    2325
    2426// different resource Types
     
    5052
    5153/**
    52  * standard constructor
     54 * @brief standard constructor
    5355*/
    5456ResourceManager::ResourceManager ()
     
    6668
    6769/**
    68  * standard destructor
     70 * @brief standard destructor
    6971*/
    7072ResourceManager::~ResourceManager ()
     
    7880  // deleting the Directorie Lists
    7981  while (!this->imageDirs.empty())
    80     {
    81       delete[] this->imageDirs.front();
    82       this->imageDirs.pop_front();
    83     }
     82  {
     83    delete[] this->imageDirs.back();
     84    this->imageDirs.pop_back();
     85  }
    8486
    8587  delete[] this->dataDir;
     
    8991
    9092/**
    91  * sets the data main directory
     93 * @brief sets the data main directory
    9294 * @param dataDir the DataDirectory.
    9395 */
     
    122124
    123125/**
    124  * sets the data main directory
     126 * @brief sets the data main directory
    125127 * @param dataDir the DataDirectory.
    126128 *
     
    153155
    154156
    155 
    156 /**
    157  * checks for the DataDirectory, by looking if
     157/**
     158 * @brief checks for the DataDirectory, by looking if
    158159 * @param fileInside is iniside of the given directory.
    159160*/
     
    162163  bool retVal;
    163164  if (!isDir(this->dataDir))
    164     {
    165       PRINTF(1)("%s is not a directory\n", this->dataDir);
    166       return false;
    167     }
     165  {
     166    PRINTF(1)("%s is not a directory\n", this->dataDir);
     167    return false;
     168  }
    168169
    169170  char* testFile = new char[strlen(this->dataDir)+strlen(fileInside)+1];
     
    176177#ifndef NO_TEXTURES
    177178/**
    178  * adds a new Path for Images
     179 * @brief adds a new Path for Images
    179180 * @param imageDir The path to insert
    180181 * @returns true, if the Path was well and injected (or already existent within the list)
     
    201202  // check if the param is a Directory
    202203  if (isDir(newDir))
    203     {
    204       // check if the Directory has been added before
    205       std::list<char*>::const_iterator imageDir;
    206       for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++)
     204  {
     205    // check if the Directory has been added before
     206    std::vector<char*>::const_iterator imageDir;
     207    for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++)
     208    {
     209      if (!strcmp(*imageDir, newDir))
    207210      {
    208          if (!strcmp(*imageDir, newDir))
    209          {
    210            PRINTF(3)("Path %s already loaded\n", newDir);
    211            delete[] newDir;
    212            return true;
    213          }
     211        PRINTF(3)("Path %s already loaded\n", newDir);
     212        delete[] newDir;
     213        return true;
    214214      }
    215       // adding the directory to the List
    216       this->imageDirs.push_back(newDir);
    217       return true;
    218     }
    219   else
    220     {
    221       PRINTF(1)("%s is not a Directory, and can not be added to the Paths of Images\n", newDir);
    222       delete[] newDir;
    223       return false;
    224     }
     215    }
     216    // adding the directory to the List
     217    this->imageDirs.push_back(newDir);
     218    return true;
     219  }
     220  else
     221  {
     222    PRINTF(1)("%s is not a Directory, and can not be added to the Paths of Images\n", newDir);
     223    delete[] newDir;
     224    return false;
     225  }
    225226}
    226227#endif /* NO_TEXTURES */
    227228
    228229/**
    229  * loads resources
     230 * @brief loads resources
    230231 * @param fileName: The fileName of the resource to load
    231232 * @param prio: The ResourcePriority of this resource (will only be increased)
     233 * @param param0: an additional option to parse (see the constuctors for more help)
    232234 * @param param1: an additional option to parse (see the constuctors for more help)
    233235 * @param param2: an additional option to parse (see the constuctors for more help)
    234  * @param param3: an additional option to parse (see the constuctors for more help)
    235236 * @returns a pointer to a desired Resource.
    236237*/
    237 BaseObject* ResourceManager::load(const char* fileName, ResourcePriority prio, void* param1, void* param2, void* param3)
     238BaseObject* ResourceManager::load(const char* fileName, ResourcePriority prio,
     239                                  const MultiType& param0, const MultiType& param1, const MultiType& param2)
    238240{
    239241  if (fileName == NULL)
     
    258260#endif
    259261#define __IF_OK
    260   if (!strncasecmp(fileName+(strlen(fileName)-4), ".wav", 4))
    261     tmpType = WAV;
    262   else if (!strncasecmp(fileName+(strlen(fileName)-4), ".mp3", 4))
    263     tmpType = MP3;
    264   else if (!strncasecmp(fileName+(strlen(fileName)-4), ".ogg", 4))
    265     tmpType = OGG;
     262    if (!strncasecmp(fileName+(strlen(fileName)-4), ".wav", 4))
     263      tmpType = WAV;
     264    else if (!strncasecmp(fileName+(strlen(fileName)-4), ".mp3", 4))
     265      tmpType = MP3;
     266    else if (!strncasecmp(fileName+(strlen(fileName)-4), ".ogg", 4))
     267      tmpType = OGG;
    266268#endif /* NO_AUDIO */
    267269#ifndef NO_TEXT
    268270#ifdef __IF_OK
    269   else
     271    else
    270272#endif
    271273#define __IF_OK
    272  if (!strncasecmp(fileName+(strlen(fileName)-4), ".ttf", 4))
    273     tmpType = TTF;
     274      if (!strncasecmp(fileName+(strlen(fileName)-4), ".ttf", 4))
     275        tmpType = TTF;
    274276#endif /* NO_TEXT */
    275277#ifndef NO_SHADERS
    276278#ifdef __IF_OK
    277   else
     279      else
    278280#endif
    279281#define __IF_OK
    280  if (!strncasecmp(fileName+(strlen(fileName)-5), ".vert", 5))
    281     tmpType = SHADER;
     282        if (!strncasecmp(fileName+(strlen(fileName)-5), ".vert", 5))
     283          tmpType = SHADER;
    282284#endif /* NO_SHADERS */
    283285#ifndef NO_TEXTURES
    284286#ifdef __IF_OK
    285   else
     287        else
    286288#else
    287289  if
    288290#endif
    289    tmpType = IMAGE;
     291          tmpType = IMAGE;
    290292#endif /* NO_TEXTURES */
    291293#undef __IF_OK
    292   return this->load(fileName, tmpType, prio, param1, param2, param3);
    293 }
    294 
    295 /**
    296  * loads resources
     294  return this->load(fileName, tmpType, prio, param0, param1, param2);
     295}
     296
     297/**
     298 * @brief caches a Resource
     299 *
     300 * @see load;
     301 *
     302 * @brief returns true if ok, false otherwise.
     303 * This function loads a Resource without applying it to an Object.
     304 * This is for loading purposes, e.g, when the user is loading a Resource
     305 * during the initialisation instead of at Runtime.
     306 */
     307bool ResourceManager::cache(const char* fileName, ResourceType type, ResourcePriority prio,
     308                            const MultiType& param0, const MultiType& param1, const MultiType& param2)
     309{
     310  assert(fileName != NULL);
     311
     312  // searching if the resource was loaded before.
     313  Resource* tmpResource;
     314  // check if we already loaded this Resource
     315  tmpResource = this->locateResourceByInfo(fileName, type, param0, param1, param2);
     316  // otherwise load it
     317  if (tmpResource == NULL)
     318    tmpResource = this->loadResource(fileName, type, prio, param0, param1, param2);
     319  // return cached pointer.
     320  if (tmpResource != NULL) // if the resource was loaded before.
     321  {
     322    if(tmpResource->prio < prio)
     323      tmpResource->prio = prio;
     324    return true;
     325  }
     326  else
     327    return false;
     328}
     329
     330/**
     331 * tells the ResourceManager to generate a Copy of the Resource.
     332 * @brief resourcePointer: The Pointer to the resource to copy
     333 * @returns the Resource pointed to resourcePointer.
     334 */
     335BaseObject* ResourceManager::copy(BaseObject* resourcePointer)
     336{
     337  Resource* tmp = locateResourceByPointer(resourcePointer);
     338  if (tmp!=NULL)
     339  {
     340    tmp->count++;
     341    return tmp->pointer;
     342  }
     343  else
     344    return NULL;
     345}
     346
     347
     348/**
     349 * @brief loads resources
    297350 * @param fileName: The fileName of the resource to load
    298351 * @param type: The Type of Resource to load.
    299352 * @param prio: The ResourcePriority of this resource (will only be increased)
     353 * @param param0: an additional option to parse (see the constuctors for more help)
    300354 * @param param1: an additional option to parse (see the constuctors for more help)
    301355 * @param param2: an additional option to parse (see the constuctors for more help)
    302  * @param param3: an additional option to parse (see the constuctors for more help)
    303356 * @returns a pointer to a desired Resource.
    304357*/
    305358BaseObject* ResourceManager::load(const char* fileName, ResourceType type, ResourcePriority prio,
    306                             void* param1, void* param2, void* param3)
    307 {
    308   if (fileName == NULL)
     359                                  const MultiType& param0, const MultiType& param1, const MultiType& param2)
     360{
     361  assert(fileName != NULL);
     362
     363  // searching if the resource was loaded before.
     364  Resource* tmpResource;
     365  // check if we already loaded this Resource
     366  tmpResource = this->locateResourceByInfo(fileName, type, param0, param1, param2);
     367  // otherwise load it
     368  if (tmpResource == NULL)
     369  {
     370    tmpResource = this->loadResource(fileName, type, prio, param0, param1, param2);
     371  }
     372  // return cached pointer.
     373  if (tmpResource != NULL) // if the resource was loaded before.
     374  {
     375    tmpResource->count++;
     376    if(tmpResource->prio < prio)
     377      tmpResource->prio = prio;
     378
     379    return tmpResource->pointer;
     380  }
     381  else
    309382    return NULL;
    310 
    311   // searching if the resource was loaded before.
    312   Resource* tmpResource = this->locateResourceByInfo(fileName, type, param1, param2, param3);
    313   if (tmpResource != NULL) // if the resource was loaded before.
    314     {
    315       PRINTF(4)("not loading cached resource %s\n", tmpResource->name);
    316       tmpResource->count++;
    317       if(tmpResource->prio < prio)
    318         tmpResource->prio = prio;
    319     }
    320   else
    321     {
    322       // Setting up the new Resource
    323       tmpResource = new Resource;
    324       tmpResource->count = 1;
    325       tmpResource->type = type;
    326       tmpResource->prio = prio;
    327       tmpResource->pointer = NULL;
    328       tmpResource->name = new char[strlen(fileName)+1];
    329       strcpy(tmpResource->name, fileName);
    330 
    331       // creating the full name. (directoryName + FileName)
    332       char* fullName = ResourceManager::getFullName(fileName);
    333       // Checking for the type of resource \see ResourceType
    334       switch(type)
    335         {
     383}
     384
     385
     386/**
     387 * @brief loads resources for internal purposes
     388 * @param fileName: The fileName of the resource to load
     389 * @param type: The Type of Resource to load.
     390 * @param prio: The ResourcePriority of this resource (will only be increased)
     391 * @param param0: an additional option to parse (see the constuctors for more help)
     392 * @param param1: an additional option to parse (see the constuctors for more help)
     393 * @param param2: an additional option to parse (see the constuctors for more help)
     394 * @returns a pointer to a desired Resource.
     395 */
     396Resource* ResourceManager::loadResource(const char* fileName, ResourceType type, ResourcePriority prio,
     397                                        const MultiType& param0, const MultiType& param1, const MultiType& param2)
     398{
     399  // Setting up the new Resource
     400  Resource* tmpResource = new Resource;
     401  tmpResource->count = 0;
     402  tmpResource->type = type;
     403  tmpResource->prio = prio;
     404  tmpResource->pointer = NULL;
     405  tmpResource->name = new char[strlen(fileName)+1];
     406  strcpy(tmpResource->name, fileName);
     407
     408  // creating the full name. (directoryName + FileName)
     409  char* fullName = ResourceManager::getFullName(fileName);
     410  // Checking for the type of resource \see ResourceType
     411  switch(type)
     412  {
    336413#ifndef NO_MODEL
    337         case OBJ:
    338           if (param1)
    339             tmpResource->modelSize = *(float*)param1;
    340           else
    341             tmpResource->modelSize = 1.0;
    342 
    343           if(ResourceManager::isFile(fullName))
    344             tmpResource->pointer = new OBJModel(fullName, tmpResource->modelSize);
    345           else
    346             {
    347               PRINTF(2)("File %s in %s does not exist. Loading a cube-Model instead\n", fileName, dataDir);
    348               tmpResource->pointer = ResourceManager::load("cube", PRIM, prio, &tmpResource->modelSize);
    349             }
    350           break;
    351         case PRIM:
    352           if (param1)
    353             tmpResource->modelSize = *(float*)param1;
    354           else
    355             tmpResource->modelSize = 1.0;
    356 
    357           if (!strcmp(tmpResource->name, "cube"))
    358             tmpResource->pointer = new PrimitiveModel(PRIM_CUBE, tmpResource->modelSize);
    359           else if (!strcmp(tmpResource->name, "sphere"))
    360             tmpResource->pointer = new PrimitiveModel(PRIM_SPHERE, tmpResource->modelSize);
    361           else if (!strcmp(tmpResource->name, "plane"))
    362             tmpResource->pointer = new PrimitiveModel(PRIM_PLANE, tmpResource->modelSize);
    363           else if (!strcmp(tmpResource->name, "cylinder"))
    364             tmpResource->pointer = new PrimitiveModel(PRIM_CYLINDER, tmpResource->modelSize);
    365           else if (!strcmp(tmpResource->name, "cone"))
    366             tmpResource->pointer = new PrimitiveModel(PRIM_CONE, tmpResource->modelSize);
    367           break;
    368         case MD2:
    369           if(ResourceManager::isFile(fullName))
    370             {
    371               if (param1 != NULL)
    372                 {
    373                   tmpResource->secFileName = new char[strlen((const char*)param1)+1];
    374                   strcpy(tmpResource->secFileName, (const char*) param1);
    375                 }
    376               else
    377                 tmpResource->secFileName = NULL;
    378                 tmpResource->pointer = new MD2Data(fullName, tmpResource->secFileName);
    379 //               tmpResource->pointer = new MD2Model(fullName, tmpResource->secFileName);
    380 
    381             }
    382               break;
     414    case OBJ:
     415      if (param0.getType() != MT_NULL)
     416        tmpResource->param[0] = param0;
     417      else
     418        tmpResource->param[0] = 1.0f;
     419
     420      if(ResourceManager::isFile(fullName))
     421        tmpResource->pointer = new OBJModel(fullName, tmpResource->param[0].getFloat());
     422      else
     423      {
     424        PRINTF(2)("File %s in %s does not exist. Loading a cube-Model instead\n", fileName, dataDir);
     425        tmpResource->pointer = ResourceManager::load("cube", PRIM, prio, tmpResource->param[0].getFloat());
     426      }
     427      break;
     428    case PRIM:
     429      if (param0 != MT_NULL)
     430        tmpResource->param[0] = param0;
     431      else
     432        tmpResource->param[0] = 1.0f;
     433
     434      if (!strcmp(tmpResource->name, "cube"))
     435        tmpResource->pointer = new PrimitiveModel(PRIM_CUBE, tmpResource->param[0].getFloat());
     436      else if (!strcmp(tmpResource->name, "sphere"))
     437        tmpResource->pointer = new PrimitiveModel(PRIM_SPHERE, tmpResource->param[0].getFloat());
     438      else if (!strcmp(tmpResource->name, "plane"))
     439        tmpResource->pointer = new PrimitiveModel(PRIM_PLANE, tmpResource->param[0].getFloat());
     440      else if (!strcmp(tmpResource->name, "cylinder"))
     441        tmpResource->pointer = new PrimitiveModel(PRIM_CYLINDER, tmpResource->param[0].getFloat());
     442      else if (!strcmp(tmpResource->name, "cone"))
     443        tmpResource->pointer = new PrimitiveModel(PRIM_CONE, tmpResource->param[0].getFloat());
     444      break;
     445    case MD2:
     446      if(ResourceManager::isFile(fullName))
     447      {
     448        tmpResource->param[0] = param0;
     449        tmpResource->pointer = new MD2Data(fullName, tmpResource->param[0].getString());
     450        //               tmpResource->pointer = new MD2Model(fullName, tmpResource->secFileName);
     451
     452      }
     453      break;
    383454#endif /* NO_MODEL */
    384455#ifndef NO_TEXT
    385         case TTF:
    386             if (param1 != NULL)
    387               tmpResource->ttfSize = *(unsigned int*)param1;
    388             else
    389               tmpResource->ttfSize = FONT_DEFAULT_RENDER_SIZE;
    390 
    391           if(isFile(fullName))
    392             tmpResource->pointer = new Font(fullName, tmpResource->ttfSize);
    393           else
    394             PRINTF(2)("%s does not exist in %s. Not loading Font\n", fileName, this->dataDir);
    395           break;
     456    case TTF:
     457      if (param0 != MT_NULL)
     458      {
     459        assert(param0.getInt() >= 0);
     460        tmpResource->param[0] = param0;
     461      }
     462      else
     463        tmpResource->param[0] = FONT_DEFAULT_RENDER_SIZE;
     464
     465      if(isFile(fullName))
     466        tmpResource->pointer = new Font(fullName, (unsigned int) tmpResource->param[0].getInt());
     467      else
     468        PRINTF(2)("%s does not exist in %s. Not loading Font\n", fileName, this->dataDir);
     469      break;
    396470#endif /* NO_TEXT */
    397471#ifndef NO_AUDIO
    398         case WAV:
    399           if(isFile(fullName))
    400             tmpResource->pointer = new SoundBuffer(fullName);
    401           break;
    402         case OGG:
    403           if (isFile(fullName))
    404             tmpResource->pointer = new OggPlayer(fullName);
    405           break;
     472    case WAV:
     473      if(isFile(fullName))
     474        tmpResource->pointer = new SoundBuffer(fullName);
     475      break;
     476    case OGG:
     477      if (isFile(fullName))
     478        tmpResource->pointer = new OggPlayer(fullName);
     479      break;
    406480#endif /* NO_AUDIO */
    407481#ifndef NO_TEXTURES
    408         case IMAGE:
    409           if (param1 != NULL)
    410             tmpResource->texTarget = *(GLenum*)param1;
    411           else
    412             tmpResource->texTarget = GL_TEXTURE_2D;
    413           if(isFile(fullName))
    414             {
    415               PRINTF(4)("Image %s resides to %s\n", fileName, fullName);
    416               tmpResource->pointer = new Texture(fullName);
    417             }
    418           else
    419             {
    420               std::list<char*>::iterator imageDir;
    421               for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++)
    422                 {
    423                   char* imgName = new char[strlen(*imageDir)+strlen(fileName)+1];
    424                   sprintf(imgName, "%s%s", *imageDir, fileName);
    425                   if(isFile(imgName))
    426                     {
    427                       PRINTF(4)("Image %s resides to %s\n", fileName, imgName);
    428                       tmpResource->pointer = new Texture(imgName, tmpResource->texTarget);
    429                       delete[] imgName;
    430                       break;
    431                     }
    432                   delete[] imgName;
    433                 }
    434             }
    435           if(!tmpResource)
    436              PRINTF(2)("!!Image %s not Found!!\n", fileName);
    437           break;
     482    case IMAGE:
     483      if (param0 != MT_NULL)
     484        tmpResource->param[0] = param0;
     485      else
     486        tmpResource->param[0] = GL_TEXTURE_2D;
     487      if(isFile(fullName))
     488      {
     489        PRINTF(4)("Image %s resides to %s\n", fileName, fullName);
     490        tmpResource->pointer = new Texture(fullName);
     491      }
     492      else
     493      {
     494        std::vector<char*>::iterator imageDir;
     495        for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++)
     496        {
     497          char* imgName = new char[strlen(*imageDir)+strlen(fileName)+1];
     498          sprintf(imgName, "%s%s", *imageDir, fileName);
     499          if(isFile(imgName))
     500          {
     501            PRINTF(4)("Image %s resides to %s\n", fileName, imgName);
     502            tmpResource->pointer = new Texture(imgName, tmpResource->param[0].getInt());
     503            delete[] imgName;
     504            break;
     505          }
     506          delete[] imgName;
     507        }
     508      }
     509      if(!tmpResource)
     510        PRINTF(2)("!!Image %s not Found!!\n", fileName);
     511      break;
    438512#endif /* NO_TEXTURES */
    439513#ifndef NO_SHADERS
    440           case SHADER:
    441             if(ResourceManager::isFile(fullName))
    442             {
    443               if (param1 != NULL)
    444               {
    445                 char* secFullName = ResourceManager::getFullName((const char*)param1);
    446                 if (ResourceManager::isFile(secFullName))
    447                 {
    448                   tmpResource->secFileName = new char[strlen((const char*)param1)+1];
    449                   strcpy(tmpResource->secFileName, (const char*) param1);
    450                   tmpResource->pointer = new Shader(fullName, secFullName);
    451                 }
    452                 delete[] secFullName;
    453               }
    454               else
    455               {
    456                 tmpResource->secFileName = NULL;
    457                 tmpResource->pointer = new Shader(fullName, NULL);
    458               }
    459             }
    460             break;
     514    case SHADER:
     515      if(ResourceManager::isFile(fullName))
     516      {
     517        if (param0 != MT_NULL)
     518        {
     519          MultiType param = param0; /// HACK
     520          char* secFullName = ResourceManager::getFullName(param.getString());
     521          if (ResourceManager::isFile(secFullName))
     522          {
     523            tmpResource->param[0] = secFullName;
     524            tmpResource->pointer = new Shader(fullName, secFullName);
     525          }
     526          delete[] secFullName;
     527        }
     528        else
     529        {
     530          tmpResource->param[0] = param0;
     531          tmpResource->pointer = new Shader(fullName, NULL);
     532        }
     533      }
     534      break;
    461535#endif /* NO_SHADERS */
    462         default:
    463           tmpResource->pointer = NULL;
    464           PRINTF(1)("No type found for %s.\n   !!This should not happen unless the Type is not supported yet. JUST DO IT!!\n", tmpResource->name);
    465           break;
    466         }
    467       if (tmpResource->pointer != NULL)
    468         this->resourceList.push_back(tmpResource);
    469       delete[] fullName;
    470     }
     536    default:
     537      tmpResource->pointer = NULL;
     538      PRINTF(1)("No type found for %s.\n   !!This should not happen unless the Type is not supported yet. JUST DO IT!!\n", tmpResource->name);
     539      break;
     540  }
    471541  if (tmpResource->pointer != NULL)
    472     return tmpResource->pointer;
    473   else
    474     {
    475       PRINTF(2)("Resource %s could not be loaded\n", fileName);
    476       delete[] tmpResource->name;
    477       delete tmpResource;
    478       return NULL;
    479     }
    480 }
    481 
    482 /**
    483  * unloads a Resource
     542    this->resourceList.push_back(tmpResource);
     543  delete[] fullName;
     544
     545
     546  if (tmpResource->pointer != NULL)
     547    return tmpResource;
     548  else
     549  {
     550    PRINTF(2)("Resource %s could not be loaded\n", fileName);
     551    delete[] tmpResource->name;
     552    delete tmpResource;
     553    return NULL;
     554  }
     555}
     556
     557/**
     558 * @brief unloads a Resource
    484559 * @param pointer: The pointer to free
    485560 * @param prio: the PriorityLevel to unload this resource
    486561 * @returns true if successful (pointer found, and deleted), false otherwise
    487562*/
    488 bool ResourceManager::unload(void* pointer, ResourcePriority prio)
     563bool ResourceManager::unload(BaseObject* pointer, ResourcePriority prio)
    489564{
    490565  if (pointer == NULL)
     
    502577
    503578/**
    504  * unloads a Resource
     579 * @brief unloads a Resource
    505580 * @param resource: The resource to unloade
    506581 * @param prio the PriorityLevel to unload this resource
     
    515590
    516591  if (resource->prio <= prio)
    517     {
    518       if (resource->count == 0)
    519         {
    520           // deleting the Resource
    521           switch(resource->type)
    522             {
     592  {
     593    if (resource->count == 0)
     594    {
     595      // deleting the Resource
     596      switch(resource->type)
     597      {
    523598#ifndef NO_MODEL
    524             case OBJ:
    525             case PRIM:
    526               delete (Model*)resource->pointer;
    527               break;
    528             case MD2:
    529               delete (MD2Data*)resource->pointer;
    530               break;
     599        case OBJ:
     600        case PRIM:
     601          delete (Model*)resource->pointer;
     602          break;
     603        case MD2:
     604          delete (MD2Data*)resource->pointer;
     605          break;
    531606#endif /* NO_MODEL */
    532607#ifndef NO_AUDIO
    533             case WAV:
    534               delete (SoundBuffer*)resource->pointer;
    535               break;
    536             case OGG:
    537               delete (OggPlayer*)resource->pointer;
    538               break;
     608        case WAV:
     609          delete (SoundBuffer*)resource->pointer;
     610          break;
     611        case OGG:
     612          delete (OggPlayer*)resource->pointer;
     613          break;
    539614#endif /* NO_AUDIO */
    540615#ifndef NO_TEXT
    541             case TTF:
    542               delete (Font*)resource->pointer;
    543               break;
     616        case TTF:
     617          delete (Font*)resource->pointer;
     618          break;
    544619#endif /* NO_TEXT */
    545620#ifndef NO_TEXTURES
    546             case IMAGE:
    547               delete (Texture*)resource->pointer;
    548               break;
     621        case IMAGE:
     622          delete (Texture*)resource->pointer;
     623          break;
    549624#endif /* NO_TEXTURES */
    550625#ifndef NO_SHADERS
    551             case SHADER:
    552               delete (Shader*)resource->pointer;
    553               break;
     626        case SHADER:
     627          delete (Shader*)resource->pointer;
     628          break;
    554629#endif /* NO_SHADERS */
    555             default:
    556               PRINTF(2)("NOT YET IMPLEMENTED !!FIX FIX!!\n");
    557               return false;
    558               break;
    559             }
    560           // deleting the List Entry:
    561           PRINTF(4)("Resource %s safely removed.\n", resource->name);
    562           delete[] resource->name;
    563           std::list<Resource*>::iterator resourceIT = std::find(this->resourceList.begin(), this->resourceList.end(), resource);
    564           this->resourceList.erase(resourceIT);
    565           delete resource;
    566         }
    567       else
    568         PRINTF(4)("Resource %s not removed, because there are still %d References to it.\n", resource->name, resource->count);
    569     }
     630        default:
     631          PRINTF(2)("NOT YET IMPLEMENTED !!FIX FIX!!\n");
     632          return false;
     633          break;
     634      }
     635      // deleting the List Entry:
     636      PRINTF(4)("Resource %s safely removed.\n", resource->name);
     637      delete[] resource->name;
     638      std::vector<Resource*>::iterator resourceIT = std::find(this->resourceList.begin(), this->resourceList.end(), resource);
     639      this->resourceList.erase(resourceIT);
     640      delete resource;
     641    }
     642    else
     643      PRINTF(4)("Resource %s not removed, because there are still %d References to it.\n", resource->name, resource->count);
     644  }
    570645  else
    571646    PRINTF(4)("not deleting resource %s because DeleteLevel to high\n", resource->name);
     
    575650
    576651/**
    577  * unloads all alocated Memory of Resources with a pririty lower than prio
     652 * @brief unloads all alocated Memory of Resources with a pririty lower than prio
    578653 * @param prio The priority to delete
    579654*/
    580655bool ResourceManager::unloadAllByPriority(ResourcePriority prio)
    581656{
    582   std::list<Resource*>::iterator resource, pre;
    583   pre = --this->resourceList.end();
    584657  unsigned int removeCount;
    585658  for (unsigned int round = 0; round < 3; round++)
    586659  {
     660    int index = this->resourceList.size() - 1;
    587661    removeCount = 0;
    588     while (pre != resourceList.end())
    589     {
    590       resource = pre;
    591       pre--;
    592         if ((*resource)->prio <= prio)
     662    while (index >= 0)
     663    {
     664      if (this->resourceList[index]->prio <= prio)
     665      {
     666        if (this->resourceList[index]->count == 0)
     667          unload(this->resourceList[index], prio);
     668        else
    593669        {
    594           if ((*resource)->count == 0)
    595             unload((*resource), prio);
    596           else
    597           {
     670          if (round == 3)
    598671            PRINTF(2)("unable to unload %s because there are still %d references to it\n",
    599                      (*resource)->name, (*resource)->count);
    600             removeCount++;
    601           }
     672                      this->resourceList[index]->name, this->resourceList[index]->count);
     673          removeCount++;
    602674        }
    603      }
    604      if (removeCount == 0) break;
    605   }
    606 }
    607 
    608 
    609 /**
    610  * Searches for a Resource by some information
     675      }
     676      index--;
     677    }
     678    if (removeCount == 0) break;
     679  }
     680}
     681
     682
     683/**
     684 * @brief Searches for a Resource by some information
    611685 * @param fileName: The name to look for
    612686 * @param type the Type of resource to locate.
     687 * @param param0: an additional option to parse (see the constuctors for more help)
    613688 * @param param1: an additional option to parse (see the constuctors for more help)
    614689 * @param param2: an additional option to parse (see the constuctors for more help)
    615  * @param param3: an additional option to parse (see the constuctors for more help)
    616690 * @returns a Pointer to the Resource if found, NULL otherwise.
    617691*/
    618692Resource* ResourceManager::locateResourceByInfo(const char* fileName, ResourceType type,
    619                                                 void* param1, void* param2, void* param3) const
    620 {
    621   std::list<Resource*>::const_iterator resource;
     693    const MultiType& param0, const MultiType& param1, const MultiType& param2) const
     694{
     695  std::vector<Resource*>::const_iterator resource;
    622696  for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++)
    623697  {
    624698    if ((*resource)->type == type && !strcmp(fileName, (*resource)->name))
    625         {
    626           bool match = false;
    627 
    628           switch (type)
    629             {
     699    {
     700      bool match = false;
     701      switch (type)
     702      {
    630703#ifndef NO_MODEL
    631             case PRIM:
    632             case OBJ:
    633               if (!param1)
    634                 {
    635                   if ((*resource)->modelSize == 1.0)
    636                     match = true;
    637                 }
    638               else if ((*resource)->modelSize == *(float*)param1)
    639                 match = true;
    640               break;
    641             case MD2:
    642               if (!param1)
    643                 {
    644                   if ((*resource)->secFileName == NULL)
    645                     match = true;
    646                 }
    647               else if (!strcmp((*resource)->secFileName, (const char*)param1))
    648                 match = true;
    649               break;
     704        case PRIM:
     705        case OBJ:
     706          if (param0 == MT_NULL)
     707          {
     708            if ((*resource)->param[0] == 1.0f)
     709              match = true;
     710          }
     711          else if ((*resource)->param[0] == param0.getFloat())
     712            match = true;
     713          break;
     714        case MD2:
     715          if (param0 == MT_NULL)
     716          {
     717            if ((*resource)->param[0] == "")
     718              match = true;
     719          }
     720          else if ((*resource)->param[0] == ((MultiType)param0).getString())
     721            match = true;
     722          break;
    650723#endif /* NO_MODEL */
    651724#ifndef NO_TEXT
    652             case TTF:
    653               if (param1 == NULL)
    654                 {
    655                   if ((*resource)->ttfSize == FONT_DEFAULT_RENDER_SIZE)
    656                     match = true;
    657                 }
    658               else if ((*resource)->ttfSize == *(unsigned int*)param1)
    659                 match = true;
    660               break;
     725        case TTF:
     726          if (param0 == MT_NULL)
     727          {
     728            if ((*resource)->param[0] == FONT_DEFAULT_RENDER_SIZE)
     729              match = true;
     730          }
     731          else if ((*resource)->param[0] == param0.getInt())
     732            match = true;
     733          break;
    661734#endif /* NO_TEXT */
    662735#ifndef NO_SHADERS
    663               case SHADER:
    664                 if (!param1)
    665                 {
    666                   if ((*resource)->secFileName == NULL)
    667                     match = true;
    668                 }
    669                 else if (!strcmp((*resource)->secFileName, (const char*)param1))
    670                   match = true;
     736        case SHADER:
     737          if (param0 == MT_NULL)
     738          {
     739            if ((*resource)->param[0] == "")
     740              match = true;
     741          }
     742          else if ((*resource)->param[0] == ((MultiType)param0).getString())
     743            match = true;
    671744#endif /* NO_SHADERS */
    672745#ifndef NO_TEXTURES
    673               case IMAGE:
    674                 if (!param1)
    675                 {
    676                   if ((*resource)->texTarget == GL_TEXTURE_2D)
    677                     match = true;
    678                 }
    679                 else if ((*resource)->texTarget ==  *(GLenum*)param1)
    680                   match = true;
     746        case IMAGE:
     747          if (param0 == MT_NULL)
     748          {
     749            if ((*resource)->param[0] == GL_TEXTURE_2D)
     750              match = true;
     751          }
     752          else if ((*resource)->param[0] ==  param0.getInt())
     753            match = true;
    681754#endif /* NO_TEXTURES */
    682             default:
    683               match = true;
    684               break;
    685             }
    686           if (match)
    687             {
    688               return (*resource);
    689             }
    690         }
    691     }
     755        default:
     756          match = true;
     757          break;
     758      }
     759      if (match)
     760      {
     761        return (*resource);
     762      }
     763    }
     764  }
    692765  return NULL;
    693766}
    694767
    695768/**
    696  * Searches for a Resource by Pointer
     769 * @brief Searches for a Resource by Pointer
    697770 * @param pointer the Pointer to search for
    698771 * @returns a Pointer to the Resource if found, NULL otherwise.
    699 */
     772 */
    700773Resource* ResourceManager::locateResourceByPointer(const void* pointer) const
    701774{
    702775  //  Resource* enumRes = resourceList->enumerate();
    703   std::list<Resource*>::const_iterator resource;
     776  std::vector<Resource*>::const_iterator resource;
    704777  for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++)
    705778    if (pointer == (*resource)->pointer)
    706         return (*resource);
     779      return (*resource);
    707780  return NULL;
    708781}
    709782
    710 /**
    711  * Checks if it is a Directory
     783char* ResourceManager::toResourcableString(unsigned int i)
     784{
     785  int len = strlen(ResourceManager::ResourceTypeToChar(this->resourceList[i]->type));
     786  len += strlen(this->resourceList[i]->name);
     787  if (this->resourceList[i]->param[0].getString()) len += strlen(this->resourceList[i]->param[0].getString()) +1;
     788  if (this->resourceList[i]->param[1].getString()) len += strlen(this->resourceList[i]->param[1].getString()) +1;
     789  if (this->resourceList[i]->param[2].getString()) len += strlen(this->resourceList[i]->param[2].getString()) +1;
     790  len += 10;
     791  char* tmp = new char[len];
     792  tmp[0] = '\0';
     793  strcat( tmp, ResourceManager::ResourceTypeToChar(this->resourceList[i]->type));
     794  strcat(tmp,",");
     795  strcat (tmp, this->resourceList[i]->name);
     796  if (this->resourceList[i]->param[0].getString() && this->resourceList[i]->param[0].getString() != '\0')
     797  {
     798    strcat(tmp,",");
     799    strcat( tmp, this->resourceList[i]->param[0].getString());
     800  }
     801  if (this->resourceList[i]->param[1].getString() && this->resourceList[i]->param[1].getString() != '\0')
     802  {
     803    strcat(tmp,",");
     804    strcat( tmp, this->resourceList[i]->param[1].getString());
     805  }
     806  if (this->resourceList[i]->param[2].getString() && this->resourceList[i]->param[2].getString() != '\0')
     807  {
     808    strcat(tmp,",");
     809    strcat( tmp, this->resourceList[i]->param[2].getString());
     810  }
     811  return tmp;
     812}
     813
     814/**
     815 * @brief caches a Resource from a ResourceableString created with the toResourcableString-function
     816 * @param resourceableString the String to cache the resource from.
     817 */
     818bool ResourceManager::fromResourceableString(const char* resourceableString)
     819{
     820  SubString splits(resourceableString, ',');
     821  splits.debug();
     822  if (splits.getCount() == 2)
     823    this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]),
     824                RP_LEVEL);
     825  else if (splits.getCount() == 3)
     826    return this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]),
     827                RP_LEVEL, splits[2]);
     828  else if (splits.getCount() == 4)
     829    return this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]),
     830                RP_LEVEL, splits[2], splits[3]);
     831  else if (splits.getCount() == 5)
     832    return this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]),
     833                RP_LEVEL, splits[2], splits[3], splits[4]);
     834}
     835
     836
     837/**
     838 * @brief Checks if it is a Directory
    712839 * @param directoryName the Directory to check for
    713840 * @returns true if it is a directory/symlink false otherwise
     
    724851  if (directoryName[strlen(directoryName)-1] == '/' ||
    725852      directoryName[strlen(directoryName)-1] == '\\')
    726     {
    727       tmpDirName = new char[strlen(directoryName)];
    728       strncpy(tmpDirName, directoryName, strlen(directoryName)-1);
    729       tmpDirName[strlen(directoryName)-1] = '\0';
    730     }
    731   else
    732     {
    733       tmpDirName = new char[strlen(directoryName)+1];
    734       strcpy(tmpDirName, directoryName);
    735     }
     853  {
     854    tmpDirName = new char[strlen(directoryName)];
     855    strncpy(tmpDirName, directoryName, strlen(directoryName)-1);
     856    tmpDirName[strlen(directoryName)-1] = '\0';
     857  }
     858  else
     859  {
     860    tmpDirName = new char[strlen(directoryName)+1];
     861    strcpy(tmpDirName, directoryName);
     862  }
    736863
    737864  if(!stat(tmpDirName, &status))
    738     {
    739       if (status.st_mode & (S_IFDIR
     865  {
     866    if (status.st_mode & (S_IFDIR
    740867#ifndef __WIN32__
    741                             | S_IFLNK
     868                          | S_IFLNK
    742869#endif
    743                             ))
    744         {
    745           delete[] tmpDirName;
    746           return true;
    747         }
    748       else
    749         {
    750           delete[] tmpDirName;
    751           return false;
    752         }
    753     }
     870                         ))
     871    {
     872      delete[] tmpDirName;
     873      return true;
     874    }
     875    else
     876    {
     877      delete[] tmpDirName;
     878      return false;
     879    }
     880  }
    754881  else
    755882  {
     
    760887
    761888/**
    762  * Checks if the file is either a Regular file or a Symlink
     889 * @brief Checks if the file is either a Regular file or a Symlink
    763890 * @param fileName the File to check for
    764891 * @returns true if it is a regular file/symlink, false otherwise
     
    772899  struct stat status;
    773900  if (!stat(tmpFileName, &status))
    774     {
    775       if (status.st_mode & (S_IFREG
     901  {
     902    if (status.st_mode & (S_IFREG
    776903#ifndef __WIN32__
    777                             | S_IFLNK
     904                          | S_IFLNK
    778905#endif
    779                             ))
    780         {
    781           delete[] tmpFileName;
    782           return true;
    783         }
    784       else
    785         {
    786           delete[] tmpFileName;
    787           return false;
    788         }
    789     }
    790   else
     906                         ))
     907    {
     908      delete[] tmpFileName;
     909      return true;
     910    }
     911    else
    791912    {
    792913      delete[] tmpFileName;
    793914      return false;
    794915    }
    795 }
    796 
    797 /**
    798  * touches a File on the disk (thereby creating it)
     916  }
     917  else
     918  {
     919    delete[] tmpFileName;
     920    return false;
     921  }
     922}
     923
     924/**
     925 * @brief touches a File on the disk (thereby creating it)
    799926 * @param fileName The file to touch
    800927*/
     
    806933  FILE* stream;
    807934  if( (stream = fopen (tmpName, "w")) == NULL)
    808     {
    809       PRINTF(1)("could not open %s fro writing\n", fileName);
    810       delete[] tmpName;
    811       return false;
    812     }
     935  {
     936    PRINTF(1)("could not open %s fro writing\n", fileName);
     937    delete[] tmpName;
     938    return false;
     939  }
    813940  fclose(stream);
    814941
     
    817944
    818945/**
    819  * deletes a File from disk
     946 * @brief deletes a File from disk
    820947 * @param fileName the File to delete
    821948*/
     
    840967  char* retName;
    841968  if (!strncmp(name, "~/", 2))
    842     {
    843       char tmpFileName[500];
     969  {
     970    char tmpFileName[500];
    844971#ifdef __WIN32__
    845       strcpy(tmpFileName, getenv("USERPROFILE"));
     972    strcpy(tmpFileName, getenv("USERPROFILE"));
    846973#else
    847       strcpy(tmpFileName, getenv("HOME"));
     974    strcpy(tmpFileName, getenv("HOME"));
    848975#endif
    849       retName = new char[strlen(tmpFileName)+strlen(name)];
    850       sprintf(retName, "%s%s", tmpFileName, name+1);
    851     }
    852   else
    853     {
    854       retName = new char[strlen(name)+1];
    855       strcpy(retName, name);
    856     }
     976    retName = new char[strlen(tmpFileName)+strlen(name)];
     977    sprintf(retName, "%s%s", tmpFileName, name+1);
     978  }
     979  else
     980  {
     981    retName = new char[strlen(name)+1];
     982    strcpy(retName, name);
     983  }
    857984  return retName;
    858985}
     
    8741001    return retName;
    8751002  else
    876     {
    877       delete[] retName;
    878       return NULL;
    879     }
    880 }
    881 
    882 
    883 /**
    884  * checks wether a file is in the DataDir.
     1003  {
     1004    delete[] retName;
     1005    return NULL;
     1006  }
     1007}
     1008
     1009
     1010/**
     1011 * @brief checks wether a file is in the DataDir.
    8851012 * @param fileName the File to check if it is in the Data-Dir structure.
    8861013 * @returns true if the file exists, false otherwise
     
    8931020  bool retVal = false;
    8941021  char* checkFile = new char[strlen(ResourceManager::getInstance()->getDataDir())
    895       + strlen(fileName) + 1];
     1022                             + strlen(fileName) + 1];
    8961023  sprintf(checkFile, "%s%s", ResourceManager::getInstance()->getDataDir(), fileName);
    8971024
     
    9061033
    9071034/**
    908  * outputs debug information about the ResourceManager
    909 */
     1035 * @brief outputs debug information about the ResourceManager
     1036 */
    9101037void ResourceManager::debug() const
    9111038{
     
    9171044  PRINT(0)(" Data-Directory is: %s\n", this->dataDir);
    9181045  PRINT(0)(" List of Image-Directories: ");
    919   std::list<char*>::const_iterator imageDir;
     1046  std::vector<char*>::const_iterator imageDir;
    9201047  for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++)
    921       PRINT(0)("%s ", (*imageDir));
     1048    PRINT(0)("%s ", (*imageDir));
    9221049  PRINT(0)("\n");
    9231050
    9241051  PRINT(0)("List of all stored Resources:\n");
    925   std::list<Resource*>::const_iterator resource;
     1052  std::vector<Resource*>::const_iterator resource;
    9261053  for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++)
    9271054
    928     {
    929       PRINT(0)("-----------------------------------------\n");
    930       PRINT(0)("Name: %s; References: %d; Type: %s ", (*resource)->name, (*resource)->count, ResourceManager::ResourceTypeToChar((*resource)->type));
    931 
    932       PRINT(0)("gets deleted at ");
    933       switch((*resource)->prio)
    934         {
    935         default:
    936         case RP_NO:
    937           PRINT(0)("first posibility (0)\n");
    938           break;
    939         case RP_LEVEL:
    940           PRINT(0)("the end of the Level (1)\n");
    941           break;
    942         case RP_CAMPAIGN:
    943           PRINT(0)("the end of the campaign (2)\n");
    944           break;
    945         case RP_GAME:
    946           PRINT(0)("when leaving the game (3)\n");
    947           break;
    948         }
    949     }
     1055  {
     1056    PRINT(0)("-----------------------------------------\n");
     1057    PRINT(0)("Name: %s; References: %d; Type: %s ", (*resource)->name, (*resource)->count, ResourceManager::ResourceTypeToChar((*resource)->type));
     1058
     1059    PRINT(0)("gets deleted at ");
     1060    switch((*resource)->prio)
     1061    {
     1062      default:
     1063      case RP_NO:
     1064        PRINT(0)("first posibility (0)\n");
     1065        break;
     1066      case RP_LEVEL:
     1067        PRINT(0)("the end of the Level (1)\n");
     1068        break;
     1069      case RP_CAMPAIGN:
     1070        PRINT(0)("the end of the campaign (2)\n");
     1071        break;
     1072      case RP_GAME:
     1073        PRINT(0)("when leaving the game (3)\n");
     1074        break;
     1075    }
     1076  }
    9501077
    9511078
     
    9561083
    9571084/**
    958  * converts a ResourceType into the corresponding String
     1085 * @brief converts a ResourceType into the corresponding String
    9591086 * @param type the ResourceType to translate
    9601087 * @returns the converted String.
     
    9621089const char* ResourceManager::ResourceTypeToChar(ResourceType type)
    9631090{
    964   switch (type)
     1091  return ResourceManager::resourceNames[type];
     1092}
     1093
     1094/**
     1095 * @brief converts a String into a ResourceType (good for loading)
     1096 * @param resourceType the name of the Type
     1097 * @returns the Number of the Type, or 0 (defautl) if not found.
     1098 */
     1099ResourceType ResourceManager::stringToResourceType(const char* resourceType)
     1100{
     1101  assert(resourceType != NULL);
     1102  for (unsigned int i = 0; i < RESOURCE_TYPE_SIZE; i++)
     1103    if (!strcmp(resourceType, ResourceManager::resourceNames[i]))
     1104      return (ResourceType)i;
     1105  return (ResourceType)0;
     1106}
     1107
     1108/**
     1109 * The Names of the ResourceTypes
     1110 */
     1111const char* ResourceManager::resourceNames[] =
    9651112  {
    9661113#ifndef NO_MODEL
    967     case OBJ:
    968       return "ObjectModel";
    969       break;
    970     case PRIM:
    971       return "PrimitiveModel";
    972       break;
    973     case MD2:
    974       return "MD2-Data";
    975       break;
     1114    "ObjectModel",
     1115    "PrimitiveModel",
     1116    "MD2-Data",
     1117#endif
     1118#ifndef NO_TEXT
     1119    "Font",
     1120#endif
     1121#ifndef NO_AUDIO
     1122    "Wav",
     1123    "mp3",
     1124    "ogg",
    9761125#endif
    9771126#ifndef NO_TEXTURES
    978     case IMAGE:
    979       return "ImageFile (Texture)";
    980       break;
    981 #endif
    982 #ifndef NO_AUDIO
    983     case WAV:
    984       return "SoundFile";
    985       break;
    986     case OGG:
    987       return "MusicFile";
    988       break;
    989 #endif
    990 #ifndef NO_TEXT
    991     case TTF:
    992       return "Font (TTF)";
    993       break;
     1127    "Texture",
    9941128#endif
    9951129#ifndef NO_SHADERS
    996     case SHADER:
    997       return "Shader";
    998       break;
     1130    "Shader",
    9991131#endif
    1000     default:
    1001       return "unknown Format";
    1002       break;
    1003   }
    1004 }
     1132
     1133  };
Note: See TracChangeset for help on using the changeset viewer.