Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5328 was 5324, checked in by bensch, 19 years ago

orxonox/trunk: better algorithm for loading the Shader through the ResourceManager
if a fragment-Shader was supplied, but the file did not exist, the Shader gets rejected

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