Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/util/loading/resource_manager.cc @ 7917

Last change on this file since 7917 was 7676, checked in by bensch, 19 years ago

some POD's

File size: 26.4 KB
RevLine 
[4597]1/*
[1853]2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
[1855]10
11   ### File Specific:
[3655]12   main-programmer: Benjamin Grauer
[3672]13   co-programmer: Patrick Boenzli
[1853]14*/
15
[3655]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOAD
[1853]17
[7193]18#include "util/loading/resource_manager.h"
[7661]19#include "file.h"
[6648]20#include "substring.h"
[4642]21#include "debug.h"
22
[6222]23#include <algorithm>
[6655]24#include <assert.h>
[6222]25
[3655]26// different resource Types
[4534]27#ifndef NO_MODEL
[3655]28#include "objModel.h"
[3657]29#include "primitive_model.h"
[4462]30#include "md2Model.h"
[4534]31#endif /* NO_MODEL */
32#ifndef NO_TEXTURES
[3655]33#include "texture.h"
[4534]34#endif /* NO_TEXTURES */
35#ifndef NO_TEXT
[5344]36#include "font.h"
[4534]37#endif /* NO_TEXT */
38#ifndef NO_AUDIO
[5930]39#include "sound_buffer.h"
[4961]40#include "ogg_player.h"
[4534]41#endif /* NO_AUDIO */
[5323]42#ifndef NO_SHADERS
43#include "shader.h"
44#endif /* NO_SHADERS */
[1853]45
[3655]46// File Handling Includes
47#include <sys/types.h>
48#include <sys/stat.h>
49#include <unistd.h>
50
[1856]51using namespace std;
[1853]52
[3245]53/**
[6647]54 * @brief standard constructor
[3245]55*/
[4597]56ResourceManager::ResourceManager ()
[3365]57{
[4597]58  this->setClassID(CL_RESOURCE_MANAGER, "ResourceManager");
59  this->setName("ResourceManager");
60
[7221]61  this->dataDir = "./";
[5480]62  this->tryDataDir("./data");
[3365]63}
[1853]64
[3658]65//! Singleton Reference to the ResourceManager
[3655]66ResourceManager* ResourceManager::singletonRef = NULL;
67
[3245]68/**
[6647]69 * @brief standard destructor
[3655]70*/
[4746]71ResourceManager::~ResourceManager ()
[3655]72{
[3660]73  // deleting the Resources-List
[3672]74  this->unloadAllByPriority(RP_GAME);
[5303]75
[6222]76  if (!this->resourceList.empty())
77    PRINTF(1)("Not removed all Resources, since there are still %d resources registered\n", this->resourceList.size());
[5303]78
[3655]79  ResourceManager::singletonRef = NULL;
80}
[1853]81
[3655]82/**
[6647]83 * @brief sets the data main directory
[4836]84 * @param dataDir the DataDirectory.
[5480]85 */
[7221]86bool ResourceManager::setDataDir(const std::string& dataDir)
[3543]87{
[7661]88  File dataDirectory(dataDir);
89  if (dataDirectory.isDirectory())
[5480]90  {
[7661]91    this->dataDir = dataDirectory.name();
92
93    if (dataDir[dataDir.size()-1] != '/' && dataDir[dataDir.size()-1] != '\\')
[3655]94    {
[7221]95      this->dataDir += '/';
[5480]96    }
97    return true;
98  }
[3655]99  else
[5480]100  {
[7661]101    PRINTF(1)("%s is not a Directory, and can not be the Data Directory, leaving as %s \n", dataDir.c_str(), this->dataDir.c_str());
[5480]102    return false;
103  }
104}
105
106/**
[6647]107 * @brief sets the data main directory
[5480]108 * @param dataDir the DataDirectory.
109 *
110 * this is essentially the same as setDataDir, but it ommits the error-message
111 */
[7221]112bool ResourceManager::tryDataDir(const std::string& dataDir)
[5480]113{
[7661]114  File dataDirectory(dataDir);
115  if (dataDirectory.isDirectory())
[5480]116  {
[7661]117    this->dataDir = dataDirectory.name();
118
119    if (dataDir[dataDir.size()-1] != '/' && dataDir[dataDir.size()-1] != '\\')
[3655]120    {
[7221]121      this->dataDir += '/';
[5480]122    }
123    return true;
124  }
[5482]125  return false;
[3543]126}
[1853]127
[5480]128
[3660]129/**
[6647]130 * @brief checks for the DataDirectory, by looking if
[5480]131 * @param fileInside is iniside of the given directory.
[4091]132*/
[7221]133bool ResourceManager::verifyDataDir(const std::string& fileInside)
[4091]134{
[7661]135  File dataDirectory(this->dataDir);
136  if (!dataDirectory.isDirectory())
[6640]137  {
[7661]138    PRINTF(1)("'%s' is not a directory\n", this->dataDir.c_str());
[6640]139    return false;
140  }
[4597]141
[7661]142  File testFile(this->dataDir + fileInside);
143  return testFile.isFile();
[4091]144}
145
[4653]146#ifndef NO_TEXTURES
[4091]147/**
[6647]148 * @brief adds a new Path for Images
[4836]149 * @param imageDir The path to insert
150 * @returns true, if the Path was well and injected (or already existent within the list)
[3660]151   false otherwise
152*/
[7221]153bool ResourceManager::addImageDir(const std::string& imageDir)
[3658]154{
[7661]155  std::string newDir = imageDir;
156  if (imageDir[imageDir.size()-1] != '/' && imageDir[imageDir.size()-1] != '\\')
[5335]157  {
[7221]158    newDir += '/';
[5335]159  }
[3660]160  // check if the param is a Directory
[7661]161  if (File(newDir).isDirectory())
[6640]162  {
163    // check if the Directory has been added before
[7221]164    std::vector<std::string>::const_iterator imageDir;
[6640]165    for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++)
[3658]166    {
[7221]167      if (*imageDir == newDir)
[6222]168      {
[7221]169        PRINTF(3)("Path %s already loaded\n", newDir.c_str());
[6640]170        return true;
[6222]171      }
[3658]172    }
[6640]173    // adding the directory to the List
174    this->imageDirs.push_back(newDir);
175    return true;
176  }
[3658]177  else
[6640]178  {
[7221]179    PRINTF(1)("%s is not a Directory, and can not be added to the Paths of Images\n", newDir.c_str());
[6640]180    return false;
181  }
[3658]182}
[4534]183#endif /* NO_TEXTURES */
[3658]184
[3245]185/**
[6647]186 * @brief loads resources
[4836]187 * @param fileName: The fileName of the resource to load
188 * @param prio: The ResourcePriority of this resource (will only be increased)
[6645]189 * @param param0: an additional option to parse (see the constuctors for more help)
[4836]190 * @param param1: an additional option to parse (see the constuctors for more help)
191 * @param param2: an additional option to parse (see the constuctors for more help)
192 * @returns a pointer to a desired Resource.
[3655]193*/
[7221]194BaseObject* ResourceManager::load(const std::string& fileName, ResourcePriority prio,
[6648]195                                  const MultiType& param0, const MultiType& param1, const MultiType& param2)
[3655]196{
197  ResourceType tmpType;
[4534]198#ifndef NO_MODEL
[4637]199#define __IF_OK
[7221]200  if (!strncasecmp(fileName.c_str()+(fileName.size()-4), ".obj", 4))
[3655]201    tmpType = OBJ;
[7221]202  else if (!strncmp(fileName.c_str()+(fileName.size()-4), ".md2", 4))
[4462]203    tmpType = MD2;
[7221]204  else if (!strcasecmp(fileName.c_str(), "cube") ||
205            !strcasecmp(fileName.c_str(), "sphere") ||
206            !strcasecmp(fileName.c_str(), "plane") ||
207            !strcasecmp(fileName.c_str(), "cylinder") ||
208            !strcasecmp(fileName.c_str(), "cone"))
[4534]209    tmpType = PRIM;
210#endif /* NO_MODEL */
211#ifndef NO_AUDIO
[4637]212#ifdef __IF_OK
213  else
214#endif
215#define __IF_OK
[7221]216    if (!strncasecmp(fileName.c_str()+(fileName.size()-4), ".wav", 4))
[6640]217      tmpType = WAV;
[7221]218    else if (!strncasecmp(fileName.c_str()+(fileName.size()-4), ".mp3", 4))
[6640]219      tmpType = MP3;
[7221]220    else if (!strncasecmp(fileName.c_str()+(fileName.size()-4), ".ogg", 4))
[6640]221      tmpType = OGG;
[4534]222#endif /* NO_AUDIO */
223#ifndef NO_TEXT
[4637]224#ifdef __IF_OK
[6640]225    else
[4637]226#endif
227#define __IF_OK
[7221]228      if (!strncasecmp(fileName.c_str()+(fileName.size()-4), ".ttf", 4))
[6640]229        tmpType = TTF;
[4534]230#endif /* NO_TEXT */
[5323]231#ifndef NO_SHADERS
232#ifdef __IF_OK
[6640]233      else
[5323]234#endif
235#define __IF_OK
[7221]236        if (!strncasecmp(fileName.c_str()+(fileName.size()-5), ".vert", 5))
[6640]237          tmpType = SHADER;
[5323]238#endif /* NO_SHADERS */
[4534]239#ifndef NO_TEXTURES
[4637]240#ifdef __IF_OK
[6640]241        else
[4637]242#else
243  if
244#endif
[6640]245          tmpType = IMAGE;
[4534]246#endif /* NO_TEXTURES */
[4653]247#undef __IF_OK
[6645]248  return this->load(fileName, tmpType, prio, param0, param1, param2);
[3655]249}
250
251/**
[6647]252 * @brief caches a Resource
[6641]253 *
254 * @see load;
255 *
[6650]256 * @brief returns true if ok, false otherwise.
[6641]257 * This function loads a Resource without applying it to an Object.
258 * This is for loading purposes, e.g, when the user is loading a Resource
259 * during the initialisation instead of at Runtime.
260 */
[7221]261bool ResourceManager::cache(const std::string& fileName, ResourceType type, ResourcePriority prio,
[6645]262                            const MultiType& param0, const MultiType& param1, const MultiType& param2)
[6641]263{
264  // searching if the resource was loaded before.
265  Resource* tmpResource;
266  // check if we already loaded this Resource
[6645]267  tmpResource = this->locateResourceByInfo(fileName, type, param0, param1, param2);
[6641]268  // otherwise load it
269  if (tmpResource == NULL)
[6645]270    tmpResource = this->loadResource(fileName, type, prio, param0, param1, param2);
[6641]271  // return cached pointer.
272  if (tmpResource != NULL) // if the resource was loaded before.
[6650]273  {
[6641]274    if(tmpResource->prio < prio)
275      tmpResource->prio = prio;
[6650]276    return true;
277  }
278  else
279    return false;
[6641]280}
281
[6651]282/**
283 * tells the ResourceManager to generate a Copy of the Resource.
284 * @brief resourcePointer: The Pointer to the resource to copy
285 * @returns the Resource pointed to resourcePointer.
286 */
287BaseObject* ResourceManager::copy(BaseObject* resourcePointer)
288{
289  Resource* tmp = locateResourceByPointer(resourcePointer);
290  if (tmp!=NULL)
291  {
292    tmp->count++;
293    return tmp->pointer;
294  }
295  else
296    return NULL;
297}
[6641]298
[6651]299
[6641]300/**
[6647]301 * @brief loads resources
[4836]302 * @param fileName: The fileName of the resource to load
[5306]303 * @param type: The Type of Resource to load.
[4836]304 * @param prio: The ResourcePriority of this resource (will only be increased)
[6645]305 * @param param0: an additional option to parse (see the constuctors for more help)
[4836]306 * @param param1: an additional option to parse (see the constuctors for more help)
307 * @param param2: an additional option to parse (see the constuctors for more help)
308 * @returns a pointer to a desired Resource.
[3245]309*/
[7221]310BaseObject* ResourceManager::load(const std::string& fileName, ResourceType type, ResourcePriority prio,
[6645]311                                  const MultiType& param0, const MultiType& param1, const MultiType& param2)
[3655]312{
[5366]313
[3658]314  // searching if the resource was loaded before.
[6640]315  Resource* tmpResource;
316  // check if we already loaded this Resource
[6645]317  tmpResource = this->locateResourceByInfo(fileName, type, param0, param1, param2);
[6640]318  // otherwise load it
319  if (tmpResource == NULL)
[6648]320  {
[6645]321    tmpResource = this->loadResource(fileName, type, prio, param0, param1, param2);
[6648]322  }
[6640]323  // return cached pointer.
[5306]324  if (tmpResource != NULL) // if the resource was loaded before.
[6640]325  {
326    tmpResource->count++;
327    if(tmpResource->prio < prio)
328      tmpResource->prio = prio;
[6648]329
[6640]330    return tmpResource->pointer;
331  }
[3677]332  else
[6640]333    return NULL;
334}
[3658]335
[6640]336
337/**
[6647]338 * @brief loads resources for internal purposes
[6640]339 * @param fileName: The fileName of the resource to load
340 * @param type: The Type of Resource to load.
341 * @param prio: The ResourcePriority of this resource (will only be increased)
[6645]342 * @param param0: an additional option to parse (see the constuctors for more help)
[6640]343 * @param param1: an additional option to parse (see the constuctors for more help)
344 * @param param2: an additional option to parse (see the constuctors for more help)
345 * @returns a pointer to a desired Resource.
346 */
[7221]347Resource* ResourceManager::loadResource(const std::string& fileName, ResourceType type, ResourcePriority prio,
[6645]348                                        const MultiType& param0, const MultiType& param1, const MultiType& param2)
[6640]349{
350  // Setting up the new Resource
351  Resource* tmpResource = new Resource;
352  tmpResource->count = 0;
353  tmpResource->type = type;
354  tmpResource->prio = prio;
355  tmpResource->pointer = NULL;
[7221]356  tmpResource->name = fileName;
[6640]357
358  // creating the full name. (directoryName + FileName)
[7221]359  std::string fullName = ResourceManager::getFullName(fileName);
[6640]360  // Checking for the type of resource \see ResourceType
361  switch(type)
362  {
[4534]363#ifndef NO_MODEL
[6648]364    case OBJ:
365      if (param0.getType() != MT_NULL)
366        tmpResource->param[0] = param0;
367      else
368        tmpResource->param[0] = 1.0f;
[3790]369
[7661]370      if(File(fullName).isFile())
[6648]371        tmpResource->pointer = new OBJModel(fullName, tmpResource->param[0].getFloat());
372      else
373      {
[7221]374        PRINTF(2)("File %s in %s does not exist. Loading a cube-Model instead\n", fileName.c_str(), dataDir.c_str());
[6648]375        tmpResource->pointer = ResourceManager::load("cube", PRIM, prio, tmpResource->param[0].getFloat());
376      }
377      break;
378    case PRIM:
379      if (param0 != MT_NULL)
380        tmpResource->param[0] = param0;
381      else
382        tmpResource->param[0] = 1.0f;
[3790]383
[7221]384      if (tmpResource->name == "cube")
[6648]385        tmpResource->pointer = new PrimitiveModel(PRIM_CUBE, tmpResource->param[0].getFloat());
[7221]386      else if (tmpResource->name == "sphere")
[6648]387        tmpResource->pointer = new PrimitiveModel(PRIM_SPHERE, tmpResource->param[0].getFloat());
[7221]388      else if (tmpResource->name == "plane")
[6648]389        tmpResource->pointer = new PrimitiveModel(PRIM_PLANE, tmpResource->param[0].getFloat());
[7221]390      else if (tmpResource->name == "cylinder")
[6648]391        tmpResource->pointer = new PrimitiveModel(PRIM_CYLINDER, tmpResource->param[0].getFloat());
[7221]392      else if (tmpResource->name == "cone")
[6648]393        tmpResource->pointer = new PrimitiveModel(PRIM_CONE, tmpResource->param[0].getFloat());
394      break;
395    case MD2:
[7661]396      if(File(fullName).isFile())
[6648]397      {
398        tmpResource->param[0] = param0;
[7059]399        tmpResource->param[1] = param1;
[7199]400        tmpResource->pointer = new MD2Data(fullName, tmpResource->param[0].getCString(), tmpResource->param[1].getFloat());
[6648]401      }
402      break;
[4534]403#endif /* NO_MODEL */
404#ifndef NO_TEXT
[6648]405    case TTF:
406      if (param0 != MT_NULL)
407      {
408        assert(param0.getInt() >= 0);
409        tmpResource->param[0] = param0;
410      }
411      else
412        tmpResource->param[0] = FONT_DEFAULT_RENDER_SIZE;
[4597]413
[7661]414      if(File(fullName).isFile())
[6648]415        tmpResource->pointer = new Font(fullName, (unsigned int) tmpResource->param[0].getInt());
416      else
[7221]417        PRINTF(2)("%s does not exist in %s. Not loading Font\n", fileName.c_str(), this->dataDir.c_str());
[6648]418      break;
[4534]419#endif /* NO_TEXT */
420#ifndef NO_AUDIO
[6648]421    case WAV:
[7661]422      if(File(fullName).isFile())
[7460]423        tmpResource->pointer = new OrxSound::SoundBuffer(fullName);
[6648]424      break;
425    case OGG:
[7661]426      if (File(fullName).isFile())
[7460]427        tmpResource->pointer = new OrxSound::OggPlayer(fullName);
[6648]428      break;
[4534]429#endif /* NO_AUDIO */
430#ifndef NO_TEXTURES
[6648]431    case IMAGE:
432      if (param0 != MT_NULL)
433        tmpResource->param[0] = param0;
434      else
435        tmpResource->param[0] = GL_TEXTURE_2D;
[7661]436      if(File(fullName).isFile())
[6640]437      {
[7676]438        PRINTF(4)("Image %s resides to %s\n", fileName.c_str(), fullName.c_str());
[6859]439        tmpResource->pointer = new Texture(fullName, tmpResource->param[0].getInt());
[6648]440      }
441      else
442      {
[7221]443        std::vector<std::string>::iterator imageDir;
[6648]444        for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++)
[6640]445        {
[7221]446          std::string imgName = *imageDir + fileName;
[7661]447          if(File(imgName).isFile())
[6648]448          {
[7676]449            PRINTF(4)("Image %s resides to %s\n", fileName.c_str(), imgName.c_str());
[6648]450            tmpResource->pointer = new Texture(imgName, tmpResource->param[0].getInt());
451            break;
452          }
[6640]453        }
454      }
[6648]455      if(!tmpResource)
[7221]456        PRINTF(2)("!!Image %s not Found!!\n", fileName.c_str());
[6648]457      break;
[4534]458#endif /* NO_TEXTURES */
[5323]459#ifndef NO_SHADERS
[6648]460    case SHADER:
[7661]461      if(File(fullName).isFile())
[6640]462      {
[6648]463        if (param0 != MT_NULL)
[6640]464        {
[6648]465          MultiType param = param0; /// HACK
[7221]466          std::string secFullName = ResourceManager::getFullName(param.getCString());
[7661]467          if (File(secFullName).isFile())
[6648]468          {
469            tmpResource->param[0] = secFullName;
470            tmpResource->pointer = new Shader(fullName, secFullName);
471          }
[4597]472        }
[6648]473        else
474        {
475          tmpResource->param[0] = param0;
[7221]476          tmpResource->pointer = new Shader(fullName, "");
[6648]477        }
[6640]478      }
[6648]479      break;
[6640]480#endif /* NO_SHADERS */
[6648]481    default:
482      tmpResource->pointer = NULL;
[7221]483      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.c_str());
[6648]484      break;
[6640]485  }
[5216]486  if (tmpResource->pointer != NULL)
[6640]487    this->resourceList.push_back(tmpResource);
488
489  if (tmpResource->pointer != NULL)
490    return tmpResource;
[4597]491  else
[6640]492  {
[7221]493    PRINTF(2)("Resource %s could not be loaded\n", fileName.c_str());
[6640]494    delete tmpResource;
495    return NULL;
496  }
[3658]497}
498
499/**
[6647]500 * @brief unloads a Resource
[4836]501 * @param pointer: The pointer to free
502 * @param prio: the PriorityLevel to unload this resource
503 * @returns true if successful (pointer found, and deleted), false otherwise
[3658]504*/
[6651]505bool ResourceManager::unload(BaseObject* pointer, ResourcePriority prio)
[3658]506{
[5366]507  if (pointer == NULL)
508    return false;
[3658]509  // if pointer is existent. and only one resource of this type exists.
[3672]510  Resource* tmpResource = this->locateResourceByPointer(pointer);
[5366]511  if (tmpResource != NULL)
512    return unload(tmpResource, prio);
[3660]513  else
[5366]514  {
515    PRINTF(2)("Resource not Found %p\n", pointer);
516    return false;
517  }
[3660]518}
519
[4465]520/**
[6647]521 * @brief unloads a Resource
[4836]522 * @param resource: The resource to unloade
523 * @param prio the PriorityLevel to unload this resource
[5308]524 * @returns true on success, false otherwise.
[4465]525*/
[3660]526bool ResourceManager::unload(Resource* resource, ResourcePriority prio)
527{
[5306]528  if (resource == NULL)
529    return false;
[3665]530  if (resource->count > 0)
531    resource->count--;
[5306]532
[3660]533  if (resource->prio <= prio)
[6640]534  {
535    if (resource->count == 0)
[3658]536    {
[7193]537      delete resource->pointer;
[6640]538      // deleting the List Entry:
[7221]539      PRINTF(4)("Resource %s safely removed.\n", resource->name.c_str());
[6642]540      std::vector<Resource*>::iterator resourceIT = std::find(this->resourceList.begin(), this->resourceList.end(), resource);
[6640]541      this->resourceList.erase(resourceIT);
542      delete resource;
[3658]543    }
[6640]544    else
[7221]545      PRINTF(4)("Resource %s not removed, because there are still %d References to it.\n", resource->name.c_str(), resource->count);
[6640]546  }
[3658]547  else
[7221]548    PRINTF(4)("not deleting resource %s because DeleteLevel to high\n", resource->name.c_str());
[3658]549  return true;
[3655]550}
551
[3660]552
[3655]553/**
[6647]554 * @brief unloads all alocated Memory of Resources with a pririty lower than prio
[4836]555 * @param prio The priority to delete
[3660]556*/
557bool ResourceManager::unloadAllByPriority(ResourcePriority prio)
558{
[6222]559  unsigned int removeCount;
560  for (unsigned int round = 0; round < 3; round++)
561  {
[6642]562    int index = this->resourceList.size() - 1;
[6222]563    removeCount = 0;
[6642]564    while (index >= 0)
[3660]565    {
[6642]566      if (this->resourceList[index]->prio <= prio)
[6640]567      {
[6642]568        if (this->resourceList[index]->count == 0)
569          unload(this->resourceList[index], prio);
[6640]570        else
[6222]571        {
[6664]572          if (round == 3)
573            PRINTF(2)("unable to unload %s because there are still %d references to it\n",
[7221]574                      this->resourceList[index]->name.c_str(), this->resourceList[index]->count);
[6640]575          removeCount++;
[6222]576        }
[6640]577      }
[6642]578      index--;
[6640]579    }
580    if (removeCount == 0) break;
[6222]581  }
[3660]582}
583
[5994]584
[3660]585/**
[6647]586 * @brief Searches for a Resource by some information
[4836]587 * @param fileName: The name to look for
588 * @param type the Type of resource to locate.
[6645]589 * @param param0: an additional option to parse (see the constuctors for more help)
[4836]590 * @param param1: an additional option to parse (see the constuctors for more help)
591 * @param param2: an additional option to parse (see the constuctors for more help)
592 * @returns a Pointer to the Resource if found, NULL otherwise.
[3658]593*/
[7221]594Resource* ResourceManager::locateResourceByInfo(const std::string& fileName, ResourceType type,
[6648]595    const MultiType& param0, const MultiType& param1, const MultiType& param2) const
[3658]596{
[6642]597  std::vector<Resource*>::const_iterator resource;
[6222]598  for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++)
599  {
[7221]600    if ((*resource)->type == type && fileName == (*resource)->name)
[6640]601    {
602      bool match = false;
603      switch (type)
604      {
[4534]605#ifndef NO_MODEL
[6648]606        case PRIM:
607        case OBJ:
608          if (param0 == MT_NULL)
609          {
610            if ((*resource)->param[0] == 1.0f)
611              match = true;
612          }
613          else if ((*resource)->param[0] == param0.getFloat())
[6640]614            match = true;
[6648]615          break;
616        case MD2:
[7059]617          if (param0 == MT_NULL && ((*resource)->param[0] == "") && param1 == MT_NULL && ((*resource)->param[0] == 1.0f))
[6648]618              match = true;
[7059]619          else if ((*resource)->param[0] == ((MultiType)param0).getString() && (*resource)->param[1] == ((MultiType)param1).getFloat())
[6640]620            match = true;
[6648]621          break;
[4534]622#endif /* NO_MODEL */
623#ifndef NO_TEXT
[6648]624        case TTF:
625          if (param0 == MT_NULL)
626          {
627            if ((*resource)->param[0] == FONT_DEFAULT_RENDER_SIZE)
628              match = true;
629          }
630          else if ((*resource)->param[0] == param0.getInt())
[6640]631            match = true;
[6648]632          break;
[4534]633#endif /* NO_TEXT */
[5323]634#ifndef NO_SHADERS
[6648]635        case SHADER:
636          if (param0 == MT_NULL)
637          {
638            if ((*resource)->param[0] == "")
639              match = true;
640          }
641          else if ((*resource)->param[0] == ((MultiType)param0).getString())
[6640]642            match = true;
[6859]643          break;
[5323]644#endif /* NO_SHADERS */
[6467]645#ifndef NO_TEXTURES
[6648]646        case IMAGE:
647          if (param0 == MT_NULL)
648          {
649            if ((*resource)->param[0] == GL_TEXTURE_2D)
650              match = true;
651          }
652          else if ((*resource)->param[0] ==  param0.getInt())
[6640]653            match = true;
[6859]654          break;
[6648]655#endif /* NO_TEXTURES */
656        default:
[6640]657          match = true;
[6648]658          break;
[6640]659      }
660      if (match)
661      {
662        return (*resource);
663      }
[3658]664    }
[6640]665  }
[3658]666  return NULL;
667}
668
669/**
[6647]670 * @brief Searches for a Resource by Pointer
[4836]671 * @param pointer the Pointer to search for
672 * @returns a Pointer to the Resource if found, NULL otherwise.
[6647]673 */
[5994]674Resource* ResourceManager::locateResourceByPointer(const void* pointer) const
[3658]675{
[3667]676  //  Resource* enumRes = resourceList->enumerate();
[6642]677  std::vector<Resource*>::const_iterator resource;
[6222]678  for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++)
679    if (pointer == (*resource)->pointer)
[6640]680      return (*resource);
[3658]681  return NULL;
682}
683
[7221]684std::string ResourceManager::toResourcableString(unsigned int i)
[6648]685{
[7221]686/*  int len = strlen(ResourceManager::ResourceTypeToChar(this->resourceList[i]->type));
687  len += this->resourceList[i]->name.size();
[7199]688  if (this->resourceList[i]->param[0].getCString()) len += strlen(this->resourceList[i]->param[0].getCString()) +1;
689  if (this->resourceList[i]->param[1].getCString()) len += strlen(this->resourceList[i]->param[1].getCString()) +1;
690  if (this->resourceList[i]->param[2].getCString()) len += strlen(this->resourceList[i]->param[2].getCString()) +1;
[6648]691  len += 10;
[7221]692  std::string tmp = new char[len];
[6648]693  tmp[0] = '\0';
[7221]694  strcat(tmp, ResourceManager::ResourceTypeToChar(this->resourceList[i]->type));
[6648]695  strcat(tmp,",");
696  strcat (tmp, this->resourceList[i]->name);
[7199]697  if (this->resourceList[i]->param[0].getCString() && this->resourceList[i]->param[0].getCString() != '\0')
[6648]698  {
699    strcat(tmp,",");
[7199]700    strcat( tmp, this->resourceList[i]->param[0].getCString());
[6648]701  }
[7199]702  if (this->resourceList[i]->param[1].getCString() && this->resourceList[i]->param[1].getCString() != '\0')
[6648]703  {
704    strcat(tmp,",");
[7199]705    strcat( tmp, this->resourceList[i]->param[1].getCString());
[6648]706  }
[7199]707  if (this->resourceList[i]->param[2].getCString() && this->resourceList[i]->param[2].getCString() != '\0')
[6648]708  {
709    strcat(tmp,",");
[7199]710    strcat( tmp, this->resourceList[i]->param[2].getCString());
[6648]711  }
[7221]712  return tmp;*/
[6648]713}
714
[3658]715/**
[6648]716 * @brief caches a Resource from a ResourceableString created with the toResourcableString-function
717 * @param resourceableString the String to cache the resource from.
718 */
[7221]719bool ResourceManager::fromResourceableString(const std::string& resourceableString)
[6648]720{
[7221]721/*  SubString splits(resourceableString, ',');
[6648]722  splits.debug();
723  if (splits.getCount() == 2)
724    this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]),
725                RP_LEVEL);
726  else if (splits.getCount() == 3)
[6650]727    return this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]),
[6648]728                RP_LEVEL, splits[2]);
729  else if (splits.getCount() == 4)
[6650]730    return this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]),
[6648]731                RP_LEVEL, splits[2], splits[3]);
732  else if (splits.getCount() == 5)
[6650]733    return this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]),
[7221]734                RP_LEVEL, splits[2], splits[3], splits[4]);*/
[6648]735}
736
737
738/**
[4961]739 * @param fileName the Name of the File to check
740 * @returns The full name of the file, including the DataDir, and NULL if the file does not exist
[5219]741 * !!IMPORTANT: this has to be deleted from the outside!!
[4166]742*/
[7221]743std::string ResourceManager::getFullName(const std::string& fileName)
[4166]744{
[7221]745  if (fileName.empty() || ResourceManager::getInstance()->getDataDir().empty())
746    return "";
[4462]747
[7221]748  std::string retName = ResourceManager::getInstance()->getDataDir() +fileName;
[7661]749  if (File(retName).isFile() || File(retName).isDirectory())
[4167]750    return retName;
751  else
[7221]752    return "";
[4166]753}
[4032]754
755
[3676]756/**
[6647]757 * @brief checks wether a file is in the DataDir.
[5335]758 * @param fileName the File to check if it is in the Data-Dir structure.
759 * @returns true if the file exists, false otherwise
760 */
[7221]761bool ResourceManager::isInDataDir(const std::string& fileName)
[5335]762{
[7221]763  if (fileName.empty() || ResourceManager::getInstance()->getDataDir().empty())
[5335]764    return false;
765
766  bool retVal = false;
[7221]767  std::string checkFile = ResourceManager::getInstance()->getDataDir() + fileName;
[5335]768
[7661]769  if (File(checkFile).exists())
[5335]770    retVal = true;
771  else
772    retVal = false;
773  return retVal;
774}
775
776
777/**
[6647]778 * @brief outputs debug information about the ResourceManager
779 */
[4746]780void ResourceManager::debug() const
[3676]781{
782  PRINT(0)("=RM===================================\n");
783  PRINT(0)("= RESOURCE-MANAGER DEBUG INFORMATION =\n");
784  PRINT(0)("======================================\n");
785  // if it is not initialized
786  PRINT(0)(" Reference is: %p\n", ResourceManager::singletonRef);
[7221]787  PRINT(0)(" Data-Directory is: %s\n", this->dataDir.c_str());
[3676]788  PRINT(0)(" List of Image-Directories: ");
[7221]789  std::vector<std::string>::const_iterator imageDir;
[6222]790  for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++)
[7221]791    PRINT(0)("%s ", (*imageDir).c_str());
[3676]792  PRINT(0)("\n");
793
794  PRINT(0)("List of all stored Resources:\n");
[6642]795  std::vector<Resource*>::const_iterator resource;
[6222]796  for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++)
797
[6640]798  {
799    PRINT(0)("-----------------------------------------\n");
[7221]800    PRINT(0)("Name: %s; References: %d; Type: %s ", (*resource)->name.c_str(), (*resource)->count, ResourceManager::ResourceTypeToChar((*resource)->type));
[6640]801
802    PRINT(0)("gets deleted at ");
803    switch((*resource)->prio)
[3676]804    {
[6648]805      default:
806      case RP_NO:
807        PRINT(0)("first posibility (0)\n");
808        break;
809      case RP_LEVEL:
810        PRINT(0)("the end of the Level (1)\n");
811        break;
812      case RP_CAMPAIGN:
813        PRINT(0)("the end of the campaign (2)\n");
814        break;
815      case RP_GAME:
816        PRINT(0)("when leaving the game (3)\n");
817        break;
[3676]818    }
[6640]819  }
[3676]820
821
822
823  PRINT(0)("==================================RM==\n");
824}
[5306]825
826
827/**
[6647]828 * @brief converts a ResourceType into the corresponding String
[5306]829 * @param type the ResourceType to translate
830 * @returns the converted String.
831 */
832const char* ResourceManager::ResourceTypeToChar(ResourceType type)
833{
[6646]834  return ResourceManager::resourceNames[type];
835}
836
837/**
838 * @brief converts a String into a ResourceType (good for loading)
839 * @param resourceType the name of the Type
840 * @returns the Number of the Type, or 0 (defautl) if not found.
841 */
[7225]842ResourceType ResourceManager::stringToResourceType(const std::string& resourceType)
[6646]843{
844  for (unsigned int i = 0; i < RESOURCE_TYPE_SIZE; i++)
[7225]845    if (resourceType == ResourceManager::resourceNames[i])
[6646]846      return (ResourceType)i;
847  return (ResourceType)0;
848}
849
850/**
851 * The Names of the ResourceTypes
852 */
853const char* ResourceManager::resourceNames[] =
[6648]854  {
[5306]855#ifndef NO_MODEL
[6648]856    "ObjectModel",
857    "PrimitiveModel",
858    "MD2-Data",
[5306]859#endif
[6648]860#ifndef NO_TEXT
861    "Font",
[5306]862#endif
863#ifndef NO_AUDIO
[6648]864    "Wav",
865    "mp3",
866    "ogg",
[5306]867#endif
[6648]868#ifndef NO_TEXTURES
869    "Texture",
[5306]870#endif
[5323]871#ifndef NO_SHADERS
[6648]872    "Shader",
[5323]873#endif
[6648]874
875  };
Note: See TracBrowser for help on using the repository browser.