[1853] | 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. |
---|
[1855] | 10 | |
---|
| 11 | ### File Specific: |
---|
[3655] | 12 | main-programmer: Benjamin Grauer |
---|
[1855] | 13 | co-programmer: ... |
---|
[1853] | 14 | */ |
---|
| 15 | |
---|
[3655] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOAD |
---|
[1853] | 17 | |
---|
[3655] | 18 | #include "resource_manager.h" |
---|
[1853] | 19 | |
---|
[3655] | 20 | // different resource Types |
---|
| 21 | #include "objModel.h" |
---|
[3657] | 22 | #include "primitive_model.h" |
---|
[3655] | 23 | #include "texture.h" |
---|
[1853] | 24 | |
---|
[3655] | 25 | // File Handling Includes |
---|
| 26 | #include <sys/types.h> |
---|
| 27 | #include <sys/stat.h> |
---|
| 28 | #include <unistd.h> |
---|
| 29 | |
---|
| 30 | |
---|
[1856] | 31 | using namespace std; |
---|
[1853] | 32 | |
---|
[1856] | 33 | |
---|
[3245] | 34 | /** |
---|
| 35 | \brief standard constructor |
---|
| 36 | */ |
---|
[3655] | 37 | ResourceManager::ResourceManager () |
---|
[3365] | 38 | { |
---|
[3655] | 39 | this->setClassName ("ResourceManager"); |
---|
| 40 | this->dataDir = NULL; |
---|
[3365] | 41 | } |
---|
[1853] | 42 | |
---|
[3655] | 43 | ResourceManager* ResourceManager::getInstance(void) |
---|
| 44 | { |
---|
| 45 | if (!ResourceManager::singletonRef) |
---|
| 46 | ResourceManager::singletonRef = new ResourceManager(); |
---|
| 47 | return ResourceManager::singletonRef; |
---|
| 48 | } |
---|
[1853] | 49 | |
---|
[3655] | 50 | ResourceManager* ResourceManager::singletonRef = NULL; |
---|
| 51 | |
---|
[3245] | 52 | /** |
---|
| 53 | \brief standard deconstructor |
---|
[3655] | 54 | */ |
---|
| 55 | ResourceManager::~ResourceManager (void) |
---|
| 56 | { |
---|
| 57 | ResourceManager::singletonRef = NULL; |
---|
| 58 | } |
---|
[1853] | 59 | |
---|
[3655] | 60 | |
---|
| 61 | |
---|
| 62 | /** |
---|
| 63 | \brief sets the data main directory |
---|
| 64 | \param dataDir the DataDirectory. |
---|
[3245] | 65 | */ |
---|
[3655] | 66 | bool ResourceManager::setDataDir(char* dataDir) |
---|
[3543] | 67 | { |
---|
[3655] | 68 | if (isDir(dataDir)) |
---|
| 69 | { |
---|
| 70 | this->dataDir = new char[strlen(dataDir)+1]; |
---|
| 71 | strcpy(this->dataDir, dataDir); |
---|
| 72 | } |
---|
| 73 | else |
---|
| 74 | { |
---|
| 75 | PRINTF(1)("%s is not a Directory, and can not be the Data Directory\n", dataDir); |
---|
| 76 | } |
---|
[3543] | 77 | } |
---|
[1853] | 78 | |
---|
[3245] | 79 | /** |
---|
[3655] | 80 | \brief loads resources |
---|
| 81 | \param fileName The fileName of the resource to load |
---|
| 82 | \returns a pointer to a desired Resource. |
---|
| 83 | */ |
---|
| 84 | void* ResourceManager::load(const char* fileName) |
---|
| 85 | { |
---|
| 86 | ResourceType tmpType; |
---|
| 87 | if (!strncmp(fileName+(strlen(fileName)-4), ".obj", 4)) |
---|
| 88 | tmpType = OBJ; |
---|
[3245] | 89 | |
---|
[3655] | 90 | return ResourceManager::load(fileName, tmpType); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | /** |
---|
| 94 | \brief loads resources |
---|
| 95 | \param fileName The fileName of the resource to load |
---|
| 96 | \param type The Type of Resource to load (\see ResourceType) |
---|
| 97 | \returns a pointer to a desired Resource. |
---|
[3245] | 98 | */ |
---|
[3655] | 99 | void* ResourceManager::load(const char* fileName, ResourceType type) |
---|
| 100 | { |
---|
| 101 | void* tmpResource = NULL; |
---|
| 102 | char* tmpName = new char[strlen(fileName)+1]; |
---|
| 103 | strcpy(tmpName, fileName); |
---|
| 104 | // Checking for the type of resource \see ResourceType |
---|
| 105 | switch(type) |
---|
| 106 | { |
---|
| 107 | case OBJ: |
---|
| 108 | if(isFile(tmpName)) |
---|
| 109 | tmpResource = new OBJModel(tmpName); |
---|
| 110 | else |
---|
| 111 | { |
---|
| 112 | PRINTF(2)("Sorry, %s does not exist. Loading a cube-Model instead\n", tmpName); |
---|
[3657] | 113 | tmpResource = new PrimitiveModel(CUBE); |
---|
[3655] | 114 | } |
---|
| 115 | break; |
---|
| 116 | case IMAGE: |
---|
| 117 | if(isFile(tmpName)) |
---|
| 118 | { |
---|
| 119 | tmpResource = new Texture(tmpName); |
---|
| 120 | } |
---|
| 121 | else |
---|
| 122 | PRINTF(1)("!!Texture %s not Found!!\n", tmpName); |
---|
| 123 | break; |
---|
| 124 | default: |
---|
| 125 | tmpResource = NULL; |
---|
| 126 | PRINTF(2)("No type found for %s.\n !!This should not happen unless the Type is not supported yet.!!\n", tmpName); |
---|
| 127 | break; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | // checking if the File really exists. |
---|
| 131 | if(!isFile(tmpName)) |
---|
| 132 | { |
---|
| 133 | PRINTF(2)("Sorry, %s is not a regular file.\n", tmpName); |
---|
| 134 | tmpResource = NULL; |
---|
| 135 | } |
---|
| 136 | return tmpResource; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | /** |
---|
| 140 | \brief Checks if it is a Directory |
---|
| 141 | \param directoryName the Directory to check for |
---|
| 142 | \returns true if it is a directory/symlink false otherwise |
---|
| 143 | */ |
---|
| 144 | bool ResourceManager::isDir(const char* directoryName) |
---|
| 145 | { |
---|
| 146 | struct stat status; |
---|
| 147 | stat(directoryName, &status); |
---|
| 148 | if (status.st_mode & (S_IFDIR | S_IFLNK)) |
---|
| 149 | return true; |
---|
| 150 | else |
---|
| 151 | return false; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | /** |
---|
| 155 | \brief Checks if the file is either a Regular file or a Symlink |
---|
| 156 | \param fileName the File to check for |
---|
| 157 | \returns true if it is a regular file/symlink, false otherwise |
---|
| 158 | */ |
---|
| 159 | bool ResourceManager::isFile(const char* fileName) |
---|
| 160 | { |
---|
| 161 | struct stat status; |
---|
| 162 | stat(fileName, &status); |
---|
| 163 | if (status.st_mode & (S_IFREG | S_IFLNK)) |
---|
| 164 | return true; |
---|
| 165 | else |
---|
| 166 | return false; |
---|
| 167 | } |
---|