Index: orxonox/trunk/src/lib/util/resource_manager.cc
===================================================================
--- orxonox/trunk/src/lib/util/resource_manager.cc	(revision 3658)
+++ orxonox/trunk/src/lib/util/resource_manager.cc	(revision 3660)
@@ -43,4 +43,7 @@
 }
 
+/**
+   \returns the Instance to this ResourceManager
+*/
 ResourceManager* ResourceManager::getInstance(void)
 {
@@ -60,11 +63,22 @@
 
 /**
-   \brief standard deconstructor
+   \brief standard destructor
 */
 ResourceManager::~ResourceManager (void) 
 {
-
+  // deleting the Resources-List
+  unloadAllByPriority(RP_GAME);
   delete resourceList;
   resourceList = NULL;
+  // deleting the Directorie Lists
+  char* tmpDir = imageDirs->enumerate();
+  while(tmpDir)
+    {
+      delete []tmpDir;
+      tmpDir = imageDirs->nextElement();
+    }
+
+  delete imageDirs;
+  imageDirs = NULL;
   ResourceManager::singletonRef = NULL;
 }
@@ -87,15 +101,36 @@
 }
 
+/**
+   \brief adds a new Path for Images
+   \param imageDir The path to insert
+   \returns true, if the Path was well and injected (or already existent within the list)
+   false otherwise
+*/
 bool ResourceManager::addImageDir(char* imageDir)
 {
+  // check if the param is a Directory
   if (isDir(imageDir))
     {
-      char* tmpDir  = new char[strlen(imageDir)+1];
+      // check if the Directory has been added before
+      char* tmpDir = imageDirs->enumerate();
+      while(tmpDir)
+	{
+	  if (!strcmp(tmpDir, imageDir))
+	    {
+	      PRINTF(4)("Path %s already loaded\n", imageDir);
+	      return true;
+	    }
+	  tmpDir = imageDirs->nextElement();
+	}
+      // adding the directory to the List
+      tmpDir  = new char[strlen(imageDir)+1];
       strcpy(tmpDir, imageDir);
       imageDirs->add(tmpDir);
+      return true;
     }
   else
     {
       PRINTF(1)("%s is not a Directory, and can not be added to the Paths of Images\n", dataDir);
+      return false;
     }
 }
@@ -104,7 +139,8 @@
    \brief loads resources
    \param fileName The fileName of the resource to load
+   \param prio The ResourcePriority of this resource (will only be increased)
    \returns a pointer to a desired Resource.
 */
-void* ResourceManager::load(const char* fileName)
+void* ResourceManager::load(const char* fileName, ResourcePriority prio)
 {
   ResourceType tmpType;
@@ -126,5 +162,5 @@
     tmpType = IMAGE;
 
-  return ResourceManager::load(fileName, tmpType);
+  return ResourceManager::load(fileName, tmpType, prio);
 }
 
@@ -133,17 +169,19 @@
    \param fileName The fileName of the resource to load
    \param type The Type of Resource to load (\see ResourceType)
+   \param prio The ResourcePriority of this resource (will only be increased)
    \returns a pointer to a desired Resource.
 */
-void* ResourceManager::load(const char* fileName, ResourceType type)
+void* ResourceManager::load(const char* fileName, ResourceType type, ResourcePriority prio)
 {
   // searching if the resource was loaded before.
   Resource* tmpResource = ResourceManager::locateResourceByName(fileName);
-  char* tmpDir;
   if (!tmpResource) // if the resource was not loaded before.
     {
+      char* tmpDir;
       // Setting up the new Resource
       tmpResource = new Resource;
       tmpResource->count = 1;
       tmpResource->type = type;
+      tmpResource->prio = prio;
       tmpResource->name = new char[strlen(fileName)+1];
       strcpy(tmpResource->name, fileName);
@@ -212,9 +250,10 @@
       resourceList->add(tmpResource);
       delete []fullName;
-
     }
   else
     {
       tmpResource->count++;
+      if(tmpResource->prio < prio)
+	tmpResource->prio = prio;
     }
 
@@ -228,8 +267,8 @@
    
 */
-bool ResourceManager::unload(void* pointer)
+bool ResourceManager::unload(void* pointer, ResourcePriority prio)
 {
   // if pointer is existent. and only one resource of this type exists.
-  Resource* tmpResource = ResourceManager::locateResourceByPointer(pointer);
+  Resource* tmpResource =ResourceManager::locateResourceByPointer(pointer);
   if (!tmpResource)
     {
@@ -237,30 +276,57 @@
       return false;
     }
-  tmpResource->count--;
-  if (tmpResource->count <= 0)
-    {
-      // deleting the Resource
-      switch(tmpResource->type)
+  else
+    unload(tmpResource, prio);
+}
+
+bool ResourceManager::unload(Resource* resource, ResourcePriority prio)
+{
+  resource->count--;
+  if (resource->prio <= prio)
+    {
+      if (resource->count <= 0)
 	{
-	case OBJ:
-	case PRIM:
-	  delete (Model*)tmpResource->pointer;
-	  break;
-	case IMAGE:
-	  delete (Texture*)tmpResource->pointer;
-	  break;
-	default:
-	  PRINTF(1)("NOT YET IMPLEMENTED FIX FIX\n");
-	  return false;
-	  break;
+	  // deleting the Resource
+	  switch(resource->type)
+	    {
+	    case OBJ:
+	    case PRIM:
+	      delete (Model*)resource->pointer;
+	      break;
+	    case IMAGE:
+	      delete (Texture*)resource->pointer;
+	      break;
+	    default:
+	      PRINTF(1)("NOT YET IMPLEMENTED !!FIX FIX!!\n");
+	      return false;
+	      break;
+	    }
+	  // deleting the List Entry:
+	  PRINTF(4)("Resource %s safely removed.\n", resource->name);
+	  delete []resource->name;
+	  resourceList->remove(resource);
 	}
-      // deleting the List Entry:
-      PRINTF(4)("Resource %s Safely removed.\n", tmpResource->name);
-      delete []tmpResource->name;
-      resourceList->remove(tmpResource);
-    }
-  else
-    PRINTF(4)("Resource not removed, because there are still %d References to it.\n", tmpResource->count);
+      else
+	PRINTF(4)("Resource %s not removed, because there are still %d References to it.\n", resource->name, resource->count);
+    }
+  else
+    PRINTF(4)("not deleting resource %s because DeleteLevel to high\n", resource->name);
   return true;
+}
+
+
+/**
+   \brief unloads all alocated Memory of Resources with a pririty lower than prio
+   \param prio The priority to delete
+*/
+bool ResourceManager::unloadAllByPriority(ResourcePriority prio)
+{
+  Resource* enumRes = resourceList->enumerate();
+  while (enumRes)
+    {
+      if (enumRes->prio <= prio)
+	unload(enumRes, prio);
+      enumRes = resourceList->nextElement();
+    }
 }
 
Index: orxonox/trunk/src/lib/util/resource_manager.h
===================================================================
--- orxonox/trunk/src/lib/util/resource_manager.h	(revision 3658)
+++ orxonox/trunk/src/lib/util/resource_manager.h	(revision 3660)
@@ -17,6 +17,17 @@
 //template<class T> class tList;
 #include "list.h"                //! \todo do this by forward definition (ask Patrick)
+
+//! An eumerator for different fileTypes the resourceManager supports \todo WAV, MP3, OGG support
 enum ResourceType {OBJ, PRIM, WAV, MP3, OGG, IMAGE};
+//! An enumerator for different UNLOAD-types. 
+/**
+   RP_NO: will be unloaded on request
+   RP_LEVEL: will be unloaded at the end of a Level
+   RP_CAMPAIGN: will be unloaded at the end of a Campaign
+   RP_GAME: will be unloaded at the end of the whole Game (when closing orxonox)
+*/
+enum ResourcePriority {RP_NO = 0, RP_LEVEL = 1, RP_CAMPAIGN = 2, RP_GAME = 3};
 
+//! A Struct that keeps track about A resource its name its Type, and so on
 struct Resource
 {
@@ -25,4 +36,5 @@
   char* name;                //!< Name of the Resource.
   ResourceType type;         //!< ResourceType of this Resource.
+  ResourcePriority prio;     //!< The Priority of this resource. (This will only be increased)
   int count;                 //!< How many times this Resource has been loaded.
 };
@@ -46,7 +58,9 @@
   static bool setDataDir(char* dataDir);
   static bool addImageDir(char* imageDir);
-  static void* load(const char* fileName);
-  static void* load(const char* fileName, ResourceType type);
-  static bool unload(void* pointer);
+  static void* load(const char* fileName, ResourcePriority prio = RP_NO);
+  static void* load(const char* fileName, ResourceType type, ResourcePriority prio = RP_NO);
+  static bool unload(void* pointer, ResourcePriority prio = RP_NO);
+  static bool unload(Resource* resource, ResourcePriority = RP_NO);
+  static bool unloadAllByPriority(ResourcePriority prio);
 
  private:
@@ -61,5 +75,4 @@
   static Resource* locateResourceByPointer(const void* pointer);
   
-
   static bool isDir(const char* directory);
   static bool isFile(const char* directory);  
