Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3660 in orxonox.OLD for orxonox


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

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

Location:
orxonox/trunk/src
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • orxonox/trunk/src/lib/graphics/importer/texture.cc

    r3655 r3660  
    2323
    2424#include "graphics_engine.h"
    25 
    26 // headers only for PathList
    27 #include <unistd.h>
    28 #include <sys/types.h>
    29 #include <sys/stat.h>
    30 #include <stdlib.h>
    31 #include <fstream>
    32 
    33 /**
    34    \brief creates a ned PathList.
    35    
    36    It is a good idea to use this as an initial List,
    37    because if you give on a name the Path will not be checked for its existence.
    38 */
    39 PathList::PathList()
    40 {
    41   this->pathName = NULL;
    42   this->next = NULL;
    43 }
    44 
    45 /**
    46    \brief Creates a new PathList with a Name.
    47    \param pName the Name of The Path.
    48 
    49    This function just adds the Path without checking if it exists.
    50 */
    51 PathList::PathList(char* pName)
    52 {
    53   this->pathName = new char [strlen(pName)+1];
    54   strcpy (this->pathName, pName);
    55   this->next = NULL;
    56 }
    57 
    58 /**
    59    \brief destroys a PathList
    60 
    61    It does this by deleting the Name and then delete its preceding PathList.
    62 */
    63 PathList::~PathList()
    64 {
    65   if (this->pathName)
    66     delete []this->pathName;
    67   if (this->next)
    68     delete this->next;
    69 }
    70 
    71 PathList* PathList::firstPath = NULL;
    72 
    73 /**
    74    \returns A Pointer to the first Path of the Pathlist
    75 */
    76 PathList* PathList::getInstance(void)
    77 {
    78   if (firstPath)
    79     return firstPath;
    80   firstPath = new PathList();
    81 }
    82 /**
    83    \brief Adds a new Pathlist Element.
    84    \param pName
    85    
    86    Adding a Path automatically checks if the Path exists,
    87    and if it does not it will not add it to the List.
    88 */
    89 void PathList::addPath (char* pName)
    90 {
    91   if (pName[0] == '\0')
    92     {
    93       PRINTF(2)("not Adding empty Path to the List.\n");
    94       return;
    95     }
    96   char* tmpPName = new char[strlen(pName)];
    97   strncpy(tmpPName, pName, strlen(pName)-1);
    98   tmpPName[strlen(pName)-1] = '\0';
    99   if (access (tmpPName, F_OK) == 0)
    100     {
    101       struct stat status;
    102       stat(tmpPName, &status);
    103       if (status.st_mode & S_IFDIR)
    104         {
    105           PRINTF(4)("Adding Path %s to the PathList.\n", pName);
    106           PathList* tmpPathList = this;
    107           while (tmpPathList->next)
    108             tmpPathList = tmpPathList->next;
    109           tmpPathList->next = new PathList(pName);
    110         }
    111       else
    112         PRINTF(2)("You tried to add non-folder %s to a PathList.\n", tmpPName);
    113     }
    114   else
    115       PRINTF(2)("You tried to add non-existing folder %s to a PathList.\n", tmpPName);
    116   delete []tmpPName;
    117 }
    118 
    119 
    12025
    12126/**
     
    15459  if (this->texture)
    15560    glDeleteTextures(1, &this->texture);
    156 }
    157 
    158 /**
    159    \brief Searches for a Texture inside one of the defined Paths
    160    \param texName The name of the texture o search for.
    161    \returns pathName+texName if texName was found in the pathList. NULL if the Texture is not found.
    162 */
    163 char* Texture::searchTextureInPaths(const char* texName) const
    164 {
    165   char* tmpName = NULL;
    166   PathList* pList = PathList::getInstance();
    167   while (pList)
    168     {
    169       if (pList->pathName)
    170         {
    171           tmpName = new char [strlen(pList->pathName)+strlen(texName)+1];
    172           strcpy(tmpName, pList->pathName);
    173         }
    174       else
    175         {
    176           tmpName = new char [strlen(texName)+1];
    177           tmpName[0]='\0';
    178         }
    179       strcat(tmpName, texName);
    180       if (access (tmpName, F_OK) == 0)
    181         return tmpName;
    182      
    183       if (tmpName)
    184         delete []tmpName;
    185       tmpName = NULL;
    186       pList = pList->next;
    187     }
    188   return NULL;
    18961}
    19062
     
    231103  if (GraphicsEngine::texturesEnabled)
    232104    {
    233       char* imgNameWithPath = searchTextureInPaths(imageName);
    234       if (imgNameWithPath)
    235         {
    236           this->map=IMG_Load(imgNameWithPath);
     105      if (imageName)
     106        {
     107          this->map=IMG_Load(imageName);
    237108          if(!map)
    238109            {
     
    249120            pImage->format = GL_RGBA;
    250121         
    251           if( !IMG_isPNG(SDL_RWFromFile(imgNameWithPath, "rb")) && !IMG_isJPG(SDL_RWFromFile(imgNameWithPath, "rb")))
     122          if( !IMG_isPNG(SDL_RWFromFile(imageName, "rb")) && !IMG_isJPG(SDL_RWFromFile(imageName, "rb")))
    252123            for (int i=0;i<map->h * map->w *3;i+=3)
    253124              {
     
    268139      else
    269140        {
    270           PRINTF(2)("Image not Found: %s\n", imgNameWithPath);
     141          PRINTF(2)("Image not Found: %s\n", imageName);
    271142          return false;
    272143        }
     
    285156  if (GraphicsEngine::texturesEnabled)
    286157    {
    287       char* imgNameWithPath = searchTextureInPaths(imageName);
    288       if (imgNameWithPath)
    289         {
    290           if (!strncmp(imgNameWithPath+strlen(imgNameWithPath)-4, ".bmp", 4))
     158      if (imageName)
     159        {
     160          if (!strncmp(imageName+strlen(imageName)-4, ".bmp", 4))
    291161            {
    292162              PRINTF(4)("Requested bmp-image. Trying to Import.\n");
    293               return this->loadBMP(imgNameWithPath);
     163              return this->loadBMP(imageName);
    294164            }
    295165         
    296           else if (!strncmp(imgNameWithPath+strlen(imgNameWithPath)-4, ".jpg", 4) || !strncmp(imgNameWithPath+strlen(imgNameWithPath)-5, ".jpg", 5))
     166          else if (!strncmp(imageName+strlen(imageName)-4, ".jpg", 4) || !strncmp(imageName+strlen(imageName)-5, ".jpg", 5))
    297167            {
    298168              PRINTF(4)("Requested jpeg-image. Trying to Import\n");
    299               return this->loadJPG(imgNameWithPath);
    300             }
    301           else if (!strncmp(imgNameWithPath+strlen(imgNameWithPath)-4, ".tga", 4))
     169              return this->loadJPG(imageName);
     170            }
     171          else if (!strncmp(imageName+strlen(imageName)-4, ".tga", 4))
    302172            {
    303173              PRINTF(4)("Requested tga-image. Trying to Import\n");
    304               return this->loadTGA(imgNameWithPath);
    305             }
    306           else if (!strncmp(imgNameWithPath+strlen(imgNameWithPath)-4, ".png", 4))
     174              return this->loadTGA(imageName);
     175            }
     176          else if (!strncmp(imageName+strlen(imageName)-4, ".png", 4))
    307177            {
    308178              PRINTF(4)("Requested png-image. Trying to Import\n");
    309               return this->loadPNG(imgNameWithPath);
     179              return this->loadPNG(imageName);
    310180            }
    311181          else
    312182            {
    313               PRINTF(2)("Requested Image was not recognized in its type. (Maybe a type-Cast-error.)\n FileName: %s", imgNameWithPath);
     183              PRINTF(2)("Requested Image was not recognized in its type. (Maybe a type-Cast-error.)\n FileName: %s", imageName);
    314184              return false;
    315185            }
     
    317187      else
    318188        {
    319           PRINTF(2)("Image not Found: %s\n", imgNameWithPath);
     189          PRINTF(2)("Image not Found: %s\n", imageName);
    320190          return false;
    321191        }
  • orxonox/trunk/src/lib/graphics/importer/texture.h

    r3655 r3660  
    2727#endif /* HAVE_PNG_H */
    2828#endif /* HAVE_SDL_SDL_IMAGE_H */
    29 
    30 
    31 
    32 //! Class to handle lists of paths.
    33 /**
    34    \todo Ability to return Paths by itself.
    35 
    36    It is simple to use, and good, for all PathList you want.
    37    just create a new Pathlist, and add Paths.
    38 */
    39 class PathList
    40 {
    41  private:
    42   PathList();
    43   static PathList* firstPath; //!< A static Pointer to the first PathList in the List.
    44  public:
    45   PathList(char* pName);
    46   ~PathList();
    47   static PathList* getInstance(void);
    48  
    49   void addPath (char* pName);
    50   char* pathName;              //!< The Name of the current Path.
    51   PathList* next;              //!< Pointer to the next Pathlist.
    52 };
    5329
    5430//! A Class, that reads in Textures from different fileformats.
  • orxonox/trunk/src/lib/util/resource_manager.cc

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

    r3658 r3660  
    1717//template<class T> class tList;
    1818#include "list.h"                //! \todo do this by forward definition (ask Patrick)
     19
     20//! An eumerator for different fileTypes the resourceManager supports \todo WAV, MP3, OGG support
    1921enum ResourceType {OBJ, PRIM, WAV, MP3, OGG, IMAGE};
     22//! An enumerator for different UNLOAD-types.
     23/**
     24   RP_NO: will be unloaded on request
     25   RP_LEVEL: will be unloaded at the end of a Level
     26   RP_CAMPAIGN: will be unloaded at the end of a Campaign
     27   RP_GAME: will be unloaded at the end of the whole Game (when closing orxonox)
     28*/
     29enum ResourcePriority {RP_NO = 0, RP_LEVEL = 1, RP_CAMPAIGN = 2, RP_GAME = 3};
    2030
     31//! A Struct that keeps track about A resource its name its Type, and so on
    2132struct Resource
    2233{
     
    2536  char* name;                //!< Name of the Resource.
    2637  ResourceType type;         //!< ResourceType of this Resource.
     38  ResourcePriority prio;     //!< The Priority of this resource. (This will only be increased)
    2739  int count;                 //!< How many times this Resource has been loaded.
    2840};
     
    4658  static bool setDataDir(char* dataDir);
    4759  static bool addImageDir(char* imageDir);
    48   static void* load(const char* fileName);
    49   static void* load(const char* fileName, ResourceType type);
    50   static bool unload(void* pointer);
     60  static void* load(const char* fileName, ResourcePriority prio = RP_NO);
     61  static void* load(const char* fileName, ResourceType type, ResourcePriority prio = RP_NO);
     62  static bool unload(void* pointer, ResourcePriority prio = RP_NO);
     63  static bool unload(Resource* resource, ResourcePriority = RP_NO);
     64  static bool unloadAllByPriority(ResourcePriority prio);
    5165
    5266 private:
     
    6175  static Resource* locateResourceByPointer(const void* pointer);
    6276 
    63 
    6477  static bool isDir(const char* directory);
    6578  static bool isFile(const char* directory); 
  • orxonox/trunk/src/orxonox.cc

    r3658 r3660  
    2222   main-programmer: Patrick Boenzli
    2323   co-programmer: Christian Meyer
     24   co-programmer: Benjamin Grauer: injected ResourceManager/GraphicsEngine
    2425*/
    2526
     
    5657  if( resources != NULL) delete resources;
    5758  delete GraphicsEngine::getInstance(); // deleting the Graphics
     59  delete ResourceManager::getInstance(); // deletes the Resource Manager
    5860}
    5961
     
    329331  orx->start();
    330332 
    331   //delete orx;
     333  //  delete orx;
    332334 
    333335}
  • orxonox/trunk/src/orxonox.h

    r3655 r3660  
    2626  static Orxonox* singletonRef;
    2727  Orxonox ();
    28   virtual ~Orxonox ();
    2928
    3029  char configfilename[256];   //!< Filename of the configuration-file.
     
    5958 public:
    6059  static Orxonox* getInstance ();
     60  virtual ~Orxonox ();
     61
    6162  void start();
    6263  void quitGame();
  • orxonox/trunk/src/story_entities/world.cc

    r3654 r3660  
    145145  cn->unbind(this->localPlayer);
    146146  cn->reset();
     147
     148  ResourceManager::unloadAllByPriority(RP_LEVEL);
    147149
    148150  delete WorldInterface::getInstance();
  • orxonox/trunk/src/world_entities/player.cc

    r3658 r3660  
    4545     the player.cc for debug also
    4646  */
    47   this->model = (Model*)ResourceManager::load("models/reaplow.obj", OBJ);
     47  this->model = (Model*)ResourceManager::load("models/reaplow.obj", OBJ, RP_CAMPAIGN);
    4848  travelSpeed = 15.0;
    4949  velocity = new Vector();
Note: See TracChangeset for help on using the changeset viewer.