Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/util/loading/resource_manager.cc @ 9854

Last change on this file since 9854 was 9854, checked in by bensch, 18 years ago

more nice comments, and also updated the KeepLevel loading (if you want to load a Resource to a KeepLevel just append it at loadtime:
eg.:
Texture tex = ResourceTexture(orxonox.png, GL_TEXTURE_2D, GameEnd);
where GameEnd is the KeepLevel as defined in orxonox.cc→initResources()

File size: 8.2 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#include "debug.h"
20
21#include <algorithm>
22#include <cassert>
23
24
25namespace Resources
26{
27  /// Definition of the ResourceManager's ObjectList.
28  ObjectListDefinition(ResourceManager);
29  //! Singleton Reference to the ResourceManager
30  ResourceManager* ResourceManager::_singletonRef = NULL;
31
32
33  /**
34   * @brief standard constructor
35  */
36  ResourceManager::ResourceManager ()
37  : _defaultKeepLevel(0)
38  {
39    this->registerObject(this, ResourceManager::_objectList);
40    this->setName("ResourceManager");
41    this->_mainGlobalPath = Directory("./");
42  }
43
44
45  /**
46   * @brief standard destructor
47  */
48  ResourceManager::~ResourceManager ()
49  {
50    ResourceManager::_singletonRef = NULL;
51  }
52
53  /**
54   * @brief Registers a new Type to the ResourceManager.
55   * @param type the Type to register.
56   */
57  void ResourceManager::registerType(Resources::Type* type)
58  {
59    this->_resourceTypes.push_back(type);
60    PRINTF(5)("ResourceType '%s' added\n", type->storedClassName().c_str());
61  }
62
63  /**
64   * @brief Unregisters a new Type to the ResourceManager.
65   * @param type the Type to unregister.
66   */
67  void ResourceManager::unregisterType(Resources::Type* type)
68  {
69    std::vector<Resources::Type*>::iterator it = std::find (this->_resourceTypes.begin(), this->_resourceTypes.end(), type);
70    if (it != this->_resourceTypes.end())
71    {
72      this->_resourceTypes.erase(it);
73      PRINTF(5)("ResourceType '%s' removed\n", type->storedClassName().c_str());
74    }
75  }
76
77  /**
78   * @brief Sets the main Global path (the main path Resources are searched for)
79   * @param directory the directory to set.
80   * @see Resource::locateFile
81   */
82  void ResourceManager::setMainGlobalPath(const Directory& directory)
83  {
84    this->_mainGlobalPath = directory;
85    this->_mainGlobalPath.open();
86  }
87
88  /**
89   * @brief add a Global search path. (global paths besided the main path.)
90   * @param directory a directory to add.
91   */
92  void ResourceManager::addGlobalPath(const Directory& directory)
93  {
94    std::vector<Directory>::const_iterator it = std::find(this->_globalPaths.begin(), this->_globalPaths.end(), directory);
95    if (it == this->_globalPaths.end())
96      this->_globalPaths.push_back(directory);
97  }
98
99  /**
100   * @brief add a ResourcePath to a Type's Paths.
101   * @param resourceName the Type's name of Resource to add the path to.
102   * @param pathName pathName the Name of the path to add.
103   * @return true on success. (if a path was added (no duplicate, and resourceName existed).
104   */
105  bool ResourceManager::addResourcePath(const std::string& resourceName, const std::string& pathName)
106  {
107    std::vector<Resources::Type*>::iterator it;
108    for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it)
109      if (*(*it) == resourceName)
110        return (*it)->addResourcePath(pathName);
111    PRINTF(2)("ResourcePath %s could not be added to the ResourceType %s\n", pathName.c_str(), resourceName.c_str());
112    return false;
113  }
114
115  /**
116   * @brief add a ResourcePath to a Type's SubPaths.
117   * @param resourceName the Type's name of Resource to add the subpath to.
118   * @param pathName pathName the Name of the path to add.
119   * @return true on success. (if a path was added (no duplicate, and resourceName existed).
120   */
121  bool ResourceManager::addResourceSubPath(const std::string& resourceName, const std::string& pathName)
122  {
123    std::vector<Resources::Type*>::iterator it;
124    for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it)
125      if (*(*it) == resourceName)
126        return (*it)->addResourceSubPath(pathName);
127    PRINTF(2)("ResourceSubPath %s could not be added to the ResourceType %s\n", pathName.c_str(), resourceName.c_str());
128    return false;
129  }
130
131  /**
132   * @brief checks wether a File is inside of the MainPath.
133   * @param fileInside the file to check
134   * @return true if the file is inside.
135   */
136  bool ResourceManager::checkFileInMainPath(const File& fileInside)
137  {
138    return (this->_mainGlobalPath + fileInside).exists();
139  }
140
141  /**
142   * @brief prepends the fileName by the MainGlobalPath (same as mainGlobalPath + '/' + fileName).
143   * @param fileName The FileName to prepend
144   * @returns the prepended file-name
145   */
146  std::string ResourceManager::prependAbsoluteMainPath(const std::string& fileName)
147  {
148    return (this->_mainGlobalPath + File(fileName)).name();
149  }
150
151  /**
152   * @brief add a KeepLevelName (this function just counts upwards).
153   * @param keepLevelName the Name of the KeepLevel to set.
154   * @returns the Level the Name was set to.
155   */
156  unsigned int ResourceManager::addKeepLevelName(const std::string& keepLevelName)
157  {
158    this->_keepLevelNames.push_back(keepLevelName);
159    return _keepLevelNames.size()-1;
160  }
161
162  /**
163   * @param keepLevelName the Name of the KeepLevel.
164   * @returns the ID of the KeepLevel named keepLevelName
165   */
166  unsigned int ResourceManager::getKeepLevelID(const std::string& keepLevelName) const
167  {
168    for (unsigned int i = 0; i < this->_keepLevelNames.size(); ++i)
169      if (this->_keepLevelNames[i] == keepLevelName)
170        return i;
171
172    PRINTF(2)("KeepLevel '%s' not found. Using 0 instead\n", keepLevelName.c_str());
173    return 0;
174  }
175
176  /**
177   * @param keepLevelID the ID to check.
178   * @return the name of the KeepLevel.
179   */
180  const std::string& ResourceManager::getKeepLevelName(unsigned int keepLevelID) const
181  {
182    assert(keepLevelID < this->_keepLevelNames.size());
183    return this->_keepLevelNames[keepLevelID];
184  }
185
186
187  /**
188   * @brief loads a Resource from a TypeName and a loadString.
189   * @param resourceTypeName The Name of the Type to what to load a Resource from.
190   * @param loadString the loadString to load in the Type.
191   */
192  void ResourceManager::loadFromLoadString(const std::string& resourceTypeName, const std::string& loadString, const KeepLevel& keepLevel)
193  {
194    std::vector<Resources::Type*>::const_iterator it;
195    for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it)
196    {
197      if (*(*it) == resourceTypeName)
198      {
199        (*it)->createFromString(loadString, keepLevel);
200        /// TODO check if the resource was allocated!!
201        return ;
202      }
203    }
204    return ;
205  }
206
207  /**
208   * @brief unloads all Resources below a certain threshhold.
209   * @param keepLevel the KeepLevel below which to erase.
210   *
211   * @not Resources will only be erased, if th keepLevel is below, and the resources are not
212   * referenced anymore.
213   */
214  void ResourceManager::unloadAllBelowKeepLevel(const Resources::KeepLevel& keepLevel)
215  {
216    std::vector<Resources::Type*>::const_iterator it;
217    for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it)
218    {
219      (*it)->unloadAllBelowKeepLevel(keepLevel);
220    }
221  }
222
223
224  /**
225   * @brief outputs debug information about the ResourceManager
226   */
227  void ResourceManager::debug() const
228  {
229    PRINT(0)("/==RM================================\\\n");
230    PRINT(0)("| RESOURCE-MANAGER DEBUG INFORMATION |\n");
231    PRINT(0)("\\====================================/\n");
232    PRINT(0)(" MainGlobal search path is %s\n", this->_mainGlobalPath.name().c_str());
233    if(!this->_globalPaths.empty())
234    {
235      PRINT(0)(" Additional Global search Paths are: ");
236      for (unsigned int i = 0; i < this->_globalPaths.size(); ++i)
237        PRINT(0)("'%s' ", this->_globalPaths[i].name().c_str());
238      PRINT(0)("\n");
239    }
240    PRINT(0)(" Listing %d Types: \n", this->_resourceTypes.size());
241    std::vector<Resources::Type*>::const_iterator it;
242    for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it)
243    {
244      (*it)->debug();
245      if (it != --this->_resourceTypes.end())
246        PRINT(0)(" ------------------------------------\n ");
247    }
248
249    PRINT(0)("KeepLevels are: ");
250    for (unsigned int i = 0; i < this->_keepLevelNames.size(); ++i)
251      PRINT(0)("%d:'%s'  ", i, this->_keepLevelNames[i].c_str());
252    PRINT(0)("\n");
253    PRINT(0)("=================================RM==/\n");
254  }
255}
Note: See TracBrowser for help on using the repository browser.