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: ... |
---|
14 | */ |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOAD |
---|
17 | |
---|
18 | #include "resource_manager.h" |
---|
19 | |
---|
20 | // different resource Types |
---|
21 | #include "objModel.h" |
---|
22 | #include "primitive_model.h" |
---|
23 | #include "texture.h" |
---|
24 | |
---|
25 | // File Handling Includes |
---|
26 | #include <sys/types.h> |
---|
27 | #include <sys/stat.h> |
---|
28 | #include <unistd.h> |
---|
29 | |
---|
30 | |
---|
31 | using namespace std; |
---|
32 | |
---|
33 | |
---|
34 | /** |
---|
35 | \brief standard constructor |
---|
36 | */ |
---|
37 | ResourceManager::ResourceManager () |
---|
38 | { |
---|
39 | this->setClassName ("ResourceManager"); |
---|
40 | this->dataDir = NULL; |
---|
41 | } |
---|
42 | |
---|
43 | ResourceManager* ResourceManager::getInstance(void) |
---|
44 | { |
---|
45 | if (!ResourceManager::singletonRef) |
---|
46 | ResourceManager::singletonRef = new ResourceManager(); |
---|
47 | return ResourceManager::singletonRef; |
---|
48 | } |
---|
49 | |
---|
50 | ResourceManager* ResourceManager::singletonRef = NULL; |
---|
51 | |
---|
52 | /** |
---|
53 | \brief standard deconstructor |
---|
54 | */ |
---|
55 | ResourceManager::~ResourceManager (void) |
---|
56 | { |
---|
57 | ResourceManager::singletonRef = NULL; |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | |
---|
62 | /** |
---|
63 | \brief sets the data main directory |
---|
64 | \param dataDir the DataDirectory. |
---|
65 | */ |
---|
66 | bool ResourceManager::setDataDir(char* dataDir) |
---|
67 | { |
---|
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 | } |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
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; |
---|
89 | |
---|
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. |
---|
98 | */ |
---|
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); |
---|
113 | tmpResource = new PrimitiveModel(CUBE); |
---|
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 | } |
---|