Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6639 was 6467, checked in by bensch, 19 years ago

orxonox/trunk: Texture loading with GL_TEXTURE_* in ResourceManager and Material

File size: 28.3 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
[3655]18#include "resource_manager.h"
[1853]19
[4642]20#include "debug.h"
21
[6222]22#include <algorithm>
23
[3655]24// different resource Types
[4534]25#ifndef NO_MODEL
[3655]26#include "objModel.h"
[3657]27#include "primitive_model.h"
[4462]28#include "md2Model.h"
[4534]29#endif /* NO_MODEL */
30#ifndef NO_TEXTURES
[3655]31#include "texture.h"
[4534]32#endif /* NO_TEXTURES */
33#ifndef NO_TEXT
[5344]34#include "font.h"
[4534]35#endif /* NO_TEXT */
36#ifndef NO_AUDIO
[5930]37#include "sound_buffer.h"
[4961]38#include "ogg_player.h"
[4534]39#endif /* NO_AUDIO */
[5323]40#ifndef NO_SHADERS
41#include "shader.h"
42#endif /* NO_SHADERS */
[1853]43
[3655]44// File Handling Includes
45#include <sys/types.h>
46#include <sys/stat.h>
47#include <unistd.h>
48
[1856]49using namespace std;
[1853]50
[3245]51/**
[4836]52 *  standard constructor
[3245]53*/
[4597]54ResourceManager::ResourceManager ()
[3365]55{
[4597]56  this->setClassID(CL_RESOURCE_MANAGER, "ResourceManager");
57  this->setName("ResourceManager");
58
[5423]59  this->dataDir = new char[3];
60  strcpy(this->dataDir, "./");
[5480]61  this->tryDataDir("./data");
[3365]62}
[1853]63
[3658]64//! Singleton Reference to the ResourceManager
[3655]65ResourceManager* ResourceManager::singletonRef = NULL;
66
[3245]67/**
[4836]68 *  standard destructor
[3655]69*/
[4746]70ResourceManager::~ResourceManager ()
[3655]71{
[3660]72  // deleting the Resources-List
[3672]73  this->unloadAllByPriority(RP_GAME);
[5303]74
[6222]75  if (!this->resourceList.empty())
76    PRINTF(1)("Not removed all Resources, since there are still %d resources registered\n", this->resourceList.size());
[5303]77
[3660]78  // deleting the Directorie Lists
[6222]79  while (!this->imageDirs.empty())
[3660]80    {
[6222]81      delete[] this->imageDirs.front();
82      this->imageDirs.pop_front();
[3660]83    }
[3672]84
[5211]85  delete[] this->dataDir;
86
[3655]87  ResourceManager::singletonRef = NULL;
88}
[1853]89
[3655]90/**
[4836]91 *  sets the data main directory
92 * @param dataDir the DataDirectory.
[5480]93 */
[3883]94bool ResourceManager::setDataDir(const char* dataDir)
[3543]95{
[4341]96  char* realDir = ResourceManager::homeDirCheck(dataDir);
97  if (isDir(realDir))
[5480]98  {
99    delete[] this->dataDir;
100    if (dataDir[strlen(dataDir)-1] == '/' || dataDir[strlen(dataDir)-1] == '\\')
[3655]101    {
[5480]102      this->dataDir = new char[strlen(realDir)+1];
103      strcpy(this->dataDir, realDir);
[3655]104    }
[5480]105    else
106    {
107      this->dataDir = new char[strlen(realDir)+2];
108      strcpy(this->dataDir, realDir);
109      this->dataDir[strlen(realDir)] = '/';
110      this->dataDir[strlen(realDir)+1] = '\0';
111    }
112    delete[] realDir;
113    return true;
114  }
[3655]115  else
[5480]116  {
117    PRINTF(1)("%s is not a Directory, and can not be the Data Directory, leaving as %s \n", realDir, this->dataDir);
118    delete[] realDir;
119    return false;
120  }
121}
122
123/**
124 * sets the data main directory
125 * @param dataDir the DataDirectory.
126 *
127 * this is essentially the same as setDataDir, but it ommits the error-message
128 */
129bool ResourceManager::tryDataDir(const char* dataDir)
130{
131  char* realDir = ResourceManager::homeDirCheck(dataDir);
132  if (isDir(realDir))
133  {
134    delete[] this->dataDir;
135    if (dataDir[strlen(dataDir)-1] == '/' || dataDir[strlen(dataDir)-1] == '\\')
[3655]136    {
[5480]137      this->dataDir = new char[strlen(realDir)+1];
138      strcpy(this->dataDir, realDir);
[3655]139    }
[5480]140    else
141    {
142      this->dataDir = new char[strlen(realDir)+2];
143      strcpy(this->dataDir, realDir);
144      this->dataDir[strlen(realDir)] = '/';
145      this->dataDir[strlen(realDir)+1] = '\0';
146    }
147    delete[] realDir;
148    return true;
149  }
[5483]150  delete[] realDir;
[5482]151  return false;
[3543]152}
[1853]153
[5480]154
155
[3660]156/**
[5480]157 * checks for the DataDirectory, by looking if
158 * @param fileInside is iniside of the given directory.
[4091]159*/
[5480]160bool ResourceManager::verifyDataDir(const char* fileInside)
[4091]161{
162  bool retVal;
163  if (!isDir(this->dataDir))
164    {
165      PRINTF(1)("%s is not a directory\n", this->dataDir);
166      return false;
167    }
[4597]168
[4091]169  char* testFile = new char[strlen(this->dataDir)+strlen(fileInside)+1];
170  sprintf(testFile, "%s%s", this->dataDir, fileInside);
171  retVal = isFile(testFile);
[5208]172  delete[] testFile;
[4091]173  return retVal;
174}
175
[4653]176#ifndef NO_TEXTURES
[4091]177/**
[4836]178 *  adds a new Path for Images
179 * @param imageDir The path to insert
180 * @returns true, if the Path was well and injected (or already existent within the list)
[3660]181   false otherwise
182*/
[4370]183bool ResourceManager::addImageDir(const char* imageDir)
[3658]184{
[5335]185  if (imageDir == NULL)
186    return false;
187
188  char* newDir;
189  if (imageDir[strlen(imageDir)-1] == '/' || imageDir[strlen(imageDir)-1] == '\\')
190  {
191    newDir = new char[strlen(imageDir)+1];
192    strcpy(newDir, imageDir);
193  }
194  else
195  {
196    newDir = new char[strlen(imageDir)+2];
197    strcpy(newDir, imageDir);
198    newDir[strlen(imageDir)] = '/';
199    newDir[strlen(imageDir)+1] = '\0';
200  }
[3660]201  // check if the param is a Directory
[5335]202  if (isDir(newDir))
[3658]203    {
[3660]204      // check if the Directory has been added before
[6222]205      std::list<char*>::const_iterator imageDir;
206      for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++)
207      {
208         if (!strcmp(*imageDir, newDir))
209         {
210           PRINTF(3)("Path %s already loaded\n", newDir);
211           delete[] newDir;
212           return true;
213         }
214      }
[3660]215      // adding the directory to the List
[6222]216      this->imageDirs.push_back(newDir);
[3660]217      return true;
[3658]218    }
219  else
220    {
[5335]221      PRINTF(1)("%s is not a Directory, and can not be added to the Paths of Images\n", newDir);
222      delete[] newDir;
[3660]223      return false;
[3658]224    }
225}
[4534]226#endif /* NO_TEXTURES */
[3658]227
[3245]228/**
[4836]229 *  loads resources
230 * @param fileName: The fileName of the resource to load
231 * @param prio: The ResourcePriority of this resource (will only be increased)
232 * @param param1: an additional option to parse (see the constuctors for more help)
233 * @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)
235 * @returns a pointer to a desired Resource.
[3655]236*/
[5304]237BaseObject* ResourceManager::load(const char* fileName, ResourcePriority prio, void* param1, void* param2, void* param3)
[3655]238{
[5366]239  if (fileName == NULL)
240    return NULL;
[3655]241  ResourceType tmpType;
[4534]242#ifndef NO_MODEL
[4637]243#define __IF_OK
[5323]244  if (!strncasecmp(fileName+(strlen(fileName)-4), ".obj", 4))
[3655]245    tmpType = OBJ;
[4534]246  else if (!strncmp(fileName+(strlen(fileName)-4), ".md2", 4))
[4462]247    tmpType = MD2;
[5323]248  else if (!strcasecmp(fileName, "cube") ||
249           !strcasecmp(fileName, "sphere") ||
250           !strcasecmp(fileName, "plane") ||
251           !strcasecmp(fileName, "cylinder") ||
252           !strcasecmp(fileName, "cone"))
[4534]253    tmpType = PRIM;
254#endif /* NO_MODEL */
255#ifndef NO_AUDIO
[4637]256#ifdef __IF_OK
257  else
258#endif
259#define __IF_OK
[5323]260  if (!strncasecmp(fileName+(strlen(fileName)-4), ".wav", 4))
[3658]261    tmpType = WAV;
[5323]262  else if (!strncasecmp(fileName+(strlen(fileName)-4), ".mp3", 4))
[3658]263    tmpType = MP3;
[5323]264  else if (!strncasecmp(fileName+(strlen(fileName)-4), ".ogg", 4))
[3658]265    tmpType = OGG;
[4534]266#endif /* NO_AUDIO */
267#ifndef NO_TEXT
[4637]268#ifdef __IF_OK
269  else
270#endif
271#define __IF_OK
[5323]272 if (!strncasecmp(fileName+(strlen(fileName)-4), ".ttf", 4))
[3790]273    tmpType = TTF;
[4534]274#endif /* NO_TEXT */
[5323]275#ifndef NO_SHADERS
276#ifdef __IF_OK
277  else
278#endif
279#define __IF_OK
280 if (!strncasecmp(fileName+(strlen(fileName)-5), ".vert", 5))
281    tmpType = SHADER;
282#endif /* NO_SHADERS */
[4534]283#ifndef NO_TEXTURES
[4637]284#ifdef __IF_OK
[4597]285  else
[4637]286#else
287  if
288#endif
289   tmpType = IMAGE;
[4534]290#endif /* NO_TEXTURES */
[4653]291#undef __IF_OK
[3790]292  return this->load(fileName, tmpType, prio, param1, param2, param3);
[3655]293}
294
295/**
[4961]296 * loads resources
[4836]297 * @param fileName: The fileName of the resource to load
[5306]298 * @param type: The Type of Resource to load.
[4836]299 * @param prio: The ResourcePriority of this resource (will only be increased)
300 * @param param1: an additional option to parse (see the constuctors for more help)
301 * @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)
303 * @returns a pointer to a desired Resource.
[3245]304*/
[5304]305BaseObject* ResourceManager::load(const char* fileName, ResourceType type, ResourcePriority prio,
[4597]306                            void* param1, void* param2, void* param3)
[3655]307{
[5366]308  if (fileName == NULL)
309    return NULL;
310
[3658]311  // searching if the resource was loaded before.
[5306]312  Resource* tmpResource = this->locateResourceByInfo(fileName, type, param1, param2, param3);
313  if (tmpResource != NULL) // if the resource was loaded before.
[3655]314    {
[3677]315      PRINTF(4)("not loading cached resource %s\n", tmpResource->name);
316      tmpResource->count++;
317      if(tmpResource->prio < prio)
[4597]318        tmpResource->prio = prio;
[3677]319    }
320  else
321    {
[3658]322      // Setting up the new Resource
323      tmpResource = new Resource;
324      tmpResource->count = 1;
325      tmpResource->type = type;
[3660]326      tmpResource->prio = prio;
[4356]327      tmpResource->pointer = NULL;
[3658]328      tmpResource->name = new char[strlen(fileName)+1];
329      strcpy(tmpResource->name, fileName);
330
331      // creating the full name. (directoryName + FileName)
[4462]332      char* fullName = ResourceManager::getFullName(fileName);
[3658]333      // Checking for the type of resource \see ResourceType
334      switch(type)
[4597]335        {
[4534]336#ifndef NO_MODEL
[4597]337        case OBJ:
338          if (param1)
339            tmpResource->modelSize = *(float*)param1;
340          else
341            tmpResource->modelSize = 1.0;
[3790]342
[4597]343          if(ResourceManager::isFile(fullName))
344            tmpResource->pointer = new OBJModel(fullName, tmpResource->modelSize);
345          else
346            {
[5366]347              PRINTF(2)("File %s in %s does not exist. Loading a cube-Model instead\n", fileName, dataDir);
[4597]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;
[3790]356
[4597]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            {
[5306]371              if (param1 != NULL)
[4597]372                {
[5323]373                  tmpResource->secFileName = new char[strlen((const char*)param1)+1];
374                  strcpy(tmpResource->secFileName, (const char*) param1);
[4597]375                }
376              else
[5323]377                tmpResource->secFileName = NULL;
[6222]378                tmpResource->pointer = new MD2Data(fullName, tmpResource->secFileName);
379//               tmpResource->pointer = new MD2Model(fullName, tmpResource->secFileName);
380
[4597]381            }
382              break;
[4534]383#endif /* NO_MODEL */
384#ifndef NO_TEXT
[4597]385        case TTF:
[5307]386            if (param1 != NULL)
[5306]387              tmpResource->ttfSize = *(unsigned int*)param1;
[4597]388            else
[5372]389              tmpResource->ttfSize = FONT_DEFAULT_RENDER_SIZE;
[4597]390
391          if(isFile(fullName))
[5306]392            tmpResource->pointer = new Font(fullName, tmpResource->ttfSize);
[4597]393          else
[5346]394            PRINTF(2)("%s does not exist in %s. Not loading Font\n", fileName, this->dataDir);
[4597]395          break;
[4534]396#endif /* NO_TEXT */
397#ifndef NO_AUDIO
[4597]398        case WAV:
399          if(isFile(fullName))
400            tmpResource->pointer = new SoundBuffer(fullName);
401          break;
[4961]402        case OGG:
403          if (isFile(fullName))
404            tmpResource->pointer = new OggPlayer(fullName);
405          break;
[4534]406#endif /* NO_AUDIO */
407#ifndef NO_TEXTURES
[4597]408        case IMAGE:
[6467]409          if (param1 != NULL)
410            tmpResource->texTarget = *(GLenum*)param1;
411          else
412            tmpResource->texTarget = GL_TEXTURE_2D;
[4597]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            {
[6222]420              std::list<char*>::iterator imageDir;
421              for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++)
[4597]422                {
[6222]423                  char* imgName = new char[strlen(*imageDir)+strlen(fileName)+1];
424                  sprintf(imgName, "%s%s", *imageDir, fileName);
[4597]425                  if(isFile(imgName))
426                    {
427                      PRINTF(4)("Image %s resides to %s\n", fileName, imgName);
[6467]428                      tmpResource->pointer = new Texture(imgName, tmpResource->texTarget);
[5211]429                      delete[] imgName;
[4597]430                      break;
431                    }
[5211]432                  delete[] imgName;
[4597]433                }
434            }
435          if(!tmpResource)
436             PRINTF(2)("!!Image %s not Found!!\n", fileName);
437          break;
[4534]438#endif /* NO_TEXTURES */
[5323]439#ifndef NO_SHADERS
440          case SHADER:
441            if(ResourceManager::isFile(fullName))
442            {
443              if (param1 != NULL)
444              {
[5324]445                char* secFullName = ResourceManager::getFullName((const char*)param1);
[5323]446                if (ResourceManager::isFile(secFullName))
447                {
448                  tmpResource->secFileName = new char[strlen((const char*)param1)+1];
449                  strcpy(tmpResource->secFileName, (const char*) param1);
[5324]450                  tmpResource->pointer = new Shader(fullName, secFullName);
[5323]451                }
[5334]452                delete[] secFullName;
[5323]453              }
454              else
[5324]455              {
[5323]456                tmpResource->secFileName = NULL;
[5324]457                tmpResource->pointer = new Shader(fullName, NULL);
458              }
[5323]459            }
460            break;
461#endif /* NO_SHADERS */
[4597]462        default:
463          tmpResource->pointer = NULL;
[5306]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);
[4597]465          break;
466        }
[5216]467      if (tmpResource->pointer != NULL)
[6222]468        this->resourceList.push_back(tmpResource);
[5211]469      delete[] fullName;
[3655]470    }
[5216]471  if (tmpResource->pointer != NULL)
[3790]472    return tmpResource->pointer;
[4597]473  else
[3790]474    {
475      PRINTF(2)("Resource %s could not be loaded\n", fileName);
[5208]476      delete[] tmpResource->name;
[3790]477      delete tmpResource;
478      return NULL;
479    }
[3658]480}
481
482/**
[4961]483 * unloads a Resource
[4836]484 * @param pointer: The pointer to free
485 * @param prio: the PriorityLevel to unload this resource
486 * @returns true if successful (pointer found, and deleted), false otherwise
[3658]487*/
[3660]488bool ResourceManager::unload(void* pointer, ResourcePriority prio)
[3658]489{
[5366]490  if (pointer == NULL)
491    return false;
[3658]492  // if pointer is existent. and only one resource of this type exists.
[3672]493  Resource* tmpResource = this->locateResourceByPointer(pointer);
[5366]494  if (tmpResource != NULL)
495    return unload(tmpResource, prio);
[3660]496  else
[5366]497  {
498    PRINTF(2)("Resource not Found %p\n", pointer);
499    return false;
500  }
[3660]501}
502
[4465]503/**
[4961]504 * unloads a Resource
[4836]505 * @param resource: The resource to unloade
506 * @param prio the PriorityLevel to unload this resource
[5308]507 * @returns true on success, false otherwise.
[4465]508*/
[3660]509bool ResourceManager::unload(Resource* resource, ResourcePriority prio)
510{
[5306]511  if (resource == NULL)
512    return false;
[3665]513  if (resource->count > 0)
514    resource->count--;
[5306]515
[3660]516  if (resource->prio <= prio)
[3658]517    {
[5308]518      if (resource->count == 0)
[4597]519        {
520          // deleting the Resource
521          switch(resource->type)
522            {
[4534]523#ifndef NO_MODEL
[4597]524            case OBJ:
525            case PRIM:
526              delete (Model*)resource->pointer;
527              break;
528            case MD2:
529              delete (MD2Data*)resource->pointer;
530              break;
[4534]531#endif /* NO_MODEL */
532#ifndef NO_AUDIO
[4597]533            case WAV:
534              delete (SoundBuffer*)resource->pointer;
535              break;
[4961]536            case OGG:
537              delete (OggPlayer*)resource->pointer;
538              break;
[4534]539#endif /* NO_AUDIO */
540#ifndef NO_TEXT
[4597]541            case TTF:
542              delete (Font*)resource->pointer;
543              break;
[4534]544#endif /* NO_TEXT */
545#ifndef NO_TEXTURES
[4597]546            case IMAGE:
547              delete (Texture*)resource->pointer;
548              break;
[4534]549#endif /* NO_TEXTURES */
[5323]550#ifndef NO_SHADERS
551            case SHADER:
552              delete (Shader*)resource->pointer;
553              break;
554#endif /* NO_SHADERS */
[4597]555            default:
[4653]556              PRINTF(2)("NOT YET IMPLEMENTED !!FIX FIX!!\n");
[4597]557              return false;
558              break;
559            }
560          // deleting the List Entry:
561          PRINTF(4)("Resource %s safely removed.\n", resource->name);
[5208]562          delete[] resource->name;
[6222]563          std::list<Resource*>::iterator resourceIT = std::find(this->resourceList.begin(), this->resourceList.end(), resource);
564          this->resourceList.erase(resourceIT);
[5302]565          delete resource;
[4597]566        }
[3660]567      else
[4597]568        PRINTF(4)("Resource %s not removed, because there are still %d References to it.\n", resource->name, resource->count);
[3658]569    }
570  else
[3660]571    PRINTF(4)("not deleting resource %s because DeleteLevel to high\n", resource->name);
[3658]572  return true;
[3655]573}
574
[3660]575
[3655]576/**
[5308]577 * unloads all alocated Memory of Resources with a pririty lower than prio
[4836]578 * @param prio The priority to delete
[3660]579*/
580bool ResourceManager::unloadAllByPriority(ResourcePriority prio)
581{
[6222]582  std::list<Resource*>::iterator resource, pre;
583  pre = --this->resourceList.end();
584  unsigned int removeCount;
585  for (unsigned int round = 0; round < 3; round++)
586  {
587    removeCount = 0;
588    while (pre != resourceList.end())
[3660]589    {
[6222]590      resource = pre;
591      pre--;
592        if ((*resource)->prio <= prio)
593        {
594          if ((*resource)->count == 0)
595            unload((*resource), prio);
596          else
597          {
598            PRINTF(2)("unable to unload %s because there are still %d references to it\n",
599                     (*resource)->name, (*resource)->count);
600            removeCount++;
601          }
602        }
603     }
604     if (removeCount == 0) break;
605  }
[3660]606}
607
[5994]608
[3660]609/**
[4961]610 * Searches for a Resource by some information
[4836]611 * @param fileName: The name to look for
612 * @param type the Type of resource to locate.
613 * @param param1: an additional option to parse (see the constuctors for more help)
614 * @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)
616 * @returns a Pointer to the Resource if found, NULL otherwise.
[3658]617*/
[4462]618Resource* ResourceManager::locateResourceByInfo(const char* fileName, ResourceType type,
[5994]619                                                void* param1, void* param2, void* param3) const
[3658]620{
[6222]621  std::list<Resource*>::const_iterator resource;
622  for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++)
623  {
624    if ((*resource)->type == type && !strcmp(fileName, (*resource)->name))
[4597]625        {
626          bool match = false;
[4462]627
[4597]628          switch (type)
629            {
[4534]630#ifndef NO_MODEL
[4597]631            case PRIM:
632            case OBJ:
633              if (!param1)
634                {
[6222]635                  if ((*resource)->modelSize == 1.0)
[4597]636                    match = true;
637                }
[6222]638              else if ((*resource)->modelSize == *(float*)param1)
[4597]639                match = true;
640              break;
641            case MD2:
642              if (!param1)
643                {
[6222]644                  if ((*resource)->secFileName == NULL)
[4597]645                    match = true;
646                }
[6222]647              else if (!strcmp((*resource)->secFileName, (const char*)param1))
[4597]648                match = true;
649              break;
[4534]650#endif /* NO_MODEL */
651#ifndef NO_TEXT
[4597]652            case TTF:
[5307]653              if (param1 == NULL)
[4597]654                {
[6222]655                  if ((*resource)->ttfSize == FONT_DEFAULT_RENDER_SIZE)
[5307]656                    match = true;
[4597]657                }
[6222]658              else if ((*resource)->ttfSize == *(unsigned int*)param1)
[5307]659                match = true;
[4597]660              break;
[4534]661#endif /* NO_TEXT */
[5323]662#ifndef NO_SHADERS
663              case SHADER:
664                if (!param1)
665                {
[6222]666                  if ((*resource)->secFileName == NULL)
[5323]667                    match = true;
668                }
[6222]669                else if (!strcmp((*resource)->secFileName, (const char*)param1))
[5323]670                  match = true;
671#endif /* NO_SHADERS */
[6467]672#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;
681#endif /* NO_TEXTURES */
[4597]682            default:
683              match = true;
684              break;
685            }
686          if (match)
687            {
[6222]688              return (*resource);
[4597]689            }
690        }
[3658]691    }
692  return NULL;
693}
694
695/**
[4961]696 * Searches for a Resource by Pointer
[4836]697 * @param pointer the Pointer to search for
698 * @returns a Pointer to the Resource if found, NULL otherwise.
[3658]699*/
[5994]700Resource* ResourceManager::locateResourceByPointer(const void* pointer) const
[3658]701{
[3667]702  //  Resource* enumRes = resourceList->enumerate();
[6222]703  std::list<Resource*>::const_iterator resource;
704  for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++)
705    if (pointer == (*resource)->pointer)
706        return (*resource);
[3658]707  return NULL;
708}
709
710/**
[4961]711 * Checks if it is a Directory
[4836]712 * @param directoryName the Directory to check for
713 * @returns true if it is a directory/symlink false otherwise
[3655]714*/
715bool ResourceManager::isDir(const char* directoryName)
716{
[4462]717  if (directoryName == NULL)
718    return false;
719
[3883]720  char* tmpDirName = NULL;
[3655]721  struct stat status;
[3883]722
723  // checking for the termination of the string given. If there is a "/" at the end cut it away
[5113]724  if (directoryName[strlen(directoryName)-1] == '/' ||
725      directoryName[strlen(directoryName)-1] == '\\')
[3883]726    {
[5335]727      tmpDirName = new char[strlen(directoryName)];
[3883]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    }
736
[4032]737  if(!stat(tmpDirName, &status))
738    {
739      if (status.st_mode & (S_IFDIR
[3790]740#ifndef __WIN32__
[4597]741                            | S_IFLNK
[3790]742#endif
[4597]743                            ))
744        {
[5113]745          delete[] tmpDirName;
[4597]746          return true;
747        }
[4032]748      else
[4597]749        {
[5208]750          delete[] tmpDirName;
[4597]751          return false;
752        }
[3883]753    }
[3658]754  else
[5211]755  {
756    delete[] tmpDirName;
[4032]757    return false;
[5211]758  }
[3655]759}
760
761/**
[4961]762 * Checks if the file is either a Regular file or a Symlink
[4836]763 * @param fileName the File to check for
764 * @returns true if it is a regular file/symlink, false otherwise
[3655]765*/
766bool ResourceManager::isFile(const char* fileName)
767{
[4462]768  if (fileName == NULL)
769    return false;
[4032]770  char* tmpFileName = ResourceManager::homeDirCheck(fileName);
771  // actually checks the File
[3655]772  struct stat status;
[4032]773  if (!stat(tmpFileName, &status))
774    {
[4597]775      if (status.st_mode & (S_IFREG
[3790]776#ifndef __WIN32__
[4597]777                            | S_IFLNK
[3790]778#endif
[4597]779                            ))
780        {
[5208]781          delete[] tmpFileName;
[4597]782          return true;
783        }
[4032]784      else
[4597]785        {
[5208]786          delete[] tmpFileName;
[4597]787          return false;
788        }
[4032]789    }
[4597]790  else
[4032]791    {
[5208]792      delete[] tmpFileName;
[4032]793      return false;
794    }
795}
796
[4166]797/**
[4961]798 * touches a File on the disk (thereby creating it)
[4836]799 * @param fileName The file to touch
[4166]800*/
[4032]801bool ResourceManager::touchFile(const char* fileName)
802{
803  char* tmpName = ResourceManager::homeDirCheck(fileName);
[4462]804  if (tmpName == NULL)
805    return false;
[4032]806  FILE* stream;
807  if( (stream = fopen (tmpName, "w")) == NULL)
808    {
809      PRINTF(1)("could not open %s fro writing\n", fileName);
[5208]810      delete[] tmpName;
[4032]811      return false;
812    }
[4033]813  fclose(stream);
[4597]814
[5208]815  delete[] tmpName;
[4032]816}
817
[4166]818/**
[4961]819 * deletes a File from disk
[4836]820 * @param fileName the File to delete
[4166]821*/
[4032]822bool ResourceManager::deleteFile(const char* fileName)
823{
[4462]824  if (fileName == NULL)
825    return false;
[4032]826  char* tmpName = ResourceManager::homeDirCheck(fileName);
827  unlink(tmpName);
[5208]828  delete[] tmpName;
[4032]829}
830
[4597]831/**
[4961]832 * @param name the Name of the file to check
833 * @returns The name of the file, including the HomeDir
834 * IMPORTANT: this has to be deleted from the outside
835 */
[4032]836char* ResourceManager::homeDirCheck(const char* name)
837{
[4462]838  if (name == NULL)
839    return NULL;
[4032]840  char* retName;
841  if (!strncmp(name, "~/", 2))
842    {
843      char tmpFileName[500];
844#ifdef __WIN32__
845      strcpy(tmpFileName, getenv("USERPROFILE"));
846#else
847      strcpy(tmpFileName, getenv("HOME"));
848#endif
849      retName = new char[strlen(tmpFileName)+strlen(name)];
850      sprintf(retName, "%s%s", tmpFileName, name+1);
851    }
[3655]852  else
[4032]853    {
854      retName = new char[strlen(name)+1];
855      strcpy(retName, name);
856    }
857  return retName;
[3655]858}
[3676]859
[4597]860/**
[4961]861 * @param fileName the Name of the File to check
862 * @returns The full name of the file, including the DataDir, and NULL if the file does not exist
[5219]863 * !!IMPORTANT: this has to be deleted from the outside!!
[4166]864*/
865char* ResourceManager::getFullName(const char* fileName)
866{
[5335]867  if (fileName == NULL || ResourceManager::getInstance()->getDataDir() == NULL)
[4462]868    return NULL;
869
[4216]870  char* retName = new char[strlen(ResourceManager::getInstance()->getDataDir())
[4597]871                           + strlen(fileName) + 1];
[4166]872  sprintf(retName, "%s%s", ResourceManager::getInstance()->getDataDir(), fileName);
[4462]873  if (ResourceManager::isFile(retName) || ResourceManager::isDir(retName))
[4167]874    return retName;
875  else
876    {
[5208]877      delete[] retName;
[4167]878      return NULL;
879    }
[4166]880}
[4032]881
882
[3676]883/**
[5335]884 * checks wether a file is in the DataDir.
885 * @param fileName the File to check if it is in the Data-Dir structure.
886 * @returns true if the file exists, false otherwise
887 */
888bool ResourceManager::isInDataDir(const char* fileName)
889{
890  if (fileName == NULL || ResourceManager::getInstance()->getDataDir() == NULL)
891    return false;
892
893  bool retVal = false;
894  char* checkFile = new char[strlen(ResourceManager::getInstance()->getDataDir())
895      + strlen(fileName) + 1];
896  sprintf(checkFile, "%s%s", ResourceManager::getInstance()->getDataDir(), fileName);
897
898  if (ResourceManager::isFile(checkFile) || ResourceManager::isDir(checkFile))
899    retVal = true;
900  else
901    retVal = false;
902  delete[] checkFile;
903  return retVal;
904}
905
906
907/**
[4961]908 * outputs debug information about the ResourceManager
[3676]909*/
[4746]910void ResourceManager::debug() const
[3676]911{
912  PRINT(0)("=RM===================================\n");
913  PRINT(0)("= RESOURCE-MANAGER DEBUG INFORMATION =\n");
914  PRINT(0)("======================================\n");
915  // if it is not initialized
916  PRINT(0)(" Reference is: %p\n", ResourceManager::singletonRef);
917  PRINT(0)(" Data-Directory is: %s\n", this->dataDir);
918  PRINT(0)(" List of Image-Directories: ");
[6222]919  std::list<char*>::const_iterator imageDir;
920  for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++)
921      PRINT(0)("%s ", (*imageDir));
[3676]922  PRINT(0)("\n");
923
924  PRINT(0)("List of all stored Resources:\n");
[6222]925  std::list<Resource*>::const_iterator resource;
926  for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++)
927
[3676]928    {
929      PRINT(0)("-----------------------------------------\n");
[6222]930      PRINT(0)("Name: %s; References: %d; Type: %s ", (*resource)->name, (*resource)->count, ResourceManager::ResourceTypeToChar((*resource)->type));
[5306]931
[3676]932      PRINT(0)("gets deleted at ");
[6222]933      switch((*resource)->prio)
[4597]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        }
[3676]949    }
950
951
952
953  PRINT(0)("==================================RM==\n");
954}
[5306]955
956
957/**
958 * converts a ResourceType into the corresponding String
959 * @param type the ResourceType to translate
960 * @returns the converted String.
961 */
962const char* ResourceManager::ResourceTypeToChar(ResourceType type)
963{
964  switch (type)
965  {
966#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;
976#endif
977#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;
994#endif
[5323]995#ifndef NO_SHADERS
996    case SHADER:
997      return "Shader";
998      break;
999#endif
[5306]1000    default:
1001      return "unknown Format";
1002      break;
1003  }
1004}
Note: See TracBrowser for help on using the repository browser.