1 | #include "dxstdafx.h"
|
---|
2 | #include "ResourceManager.h"
|
---|
3 | #include "GameManager.h"
|
---|
4 |
|
---|
5 | ResourceManager::ResourceManager(void)
|
---|
6 | {
|
---|
7 | }
|
---|
8 |
|
---|
9 | ResourceManager::~ResourceManager(void)
|
---|
10 | {
|
---|
11 | this->cleanUpResources(true);
|
---|
12 | }
|
---|
13 |
|
---|
14 | void ResourceManager::cleanUpResources(bool endOfGame) {
|
---|
15 | //Delete the stuff
|
---|
16 | for (UINT i = 0; i < this->resourceVector.size(); i++) {
|
---|
17 | //delete mesh
|
---|
18 | if(resourceVector[i].myMesh!=NULL) {
|
---|
19 | SAFE_RELEASE(resourceVector[i].myMesh);
|
---|
20 | }
|
---|
21 |
|
---|
22 | // delete adjacencyInfo
|
---|
23 | delete [] resourceVector[i].adjacencyInfo;
|
---|
24 | // delete materials
|
---|
25 | resourceVector[i].Materials.clear();
|
---|
26 |
|
---|
27 | // release textures
|
---|
28 | for(UINT j = 0; j < resourceVector[i].Textures.size(); j++) {
|
---|
29 | if (resourceVector[i].Textures[j] != 0) {
|
---|
30 | SAFE_RELEASE(resourceVector[i].Textures[j]);
|
---|
31 | }
|
---|
32 | }
|
---|
33 | }
|
---|
34 |
|
---|
35 | resourceVector.clear();
|
---|
36 |
|
---|
37 | std::vector<textureResources>::iterator it;
|
---|
38 | for(it=this->textureVector.begin();it!=this->textureVector.end();it++) {
|
---|
39 | if((!it->alwaysNeeded) || endOfGame) {
|
---|
40 | SAFE_RELEASE(it->texture);
|
---|
41 | textureVector.erase(it);
|
---|
42 | it--;
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | for(UINT i=0;i<this->soundVector.size();i++) {
|
---|
47 | FSOUND_Sample_Free(this->soundVector.at(i).soundSample);
|
---|
48 | }
|
---|
49 | this->soundVector.clear();
|
---|
50 | if(endOfGame) {
|
---|
51 | textureVector.clear();
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | void ResourceManager::freeUsedResources(SPTR<Node> node)
|
---|
56 | {
|
---|
57 | }
|
---|
58 |
|
---|
59 | void ResourceManager::freeUsedResources(SPTR<Renderer> renderer)
|
---|
60 | {
|
---|
61 | }
|
---|
62 |
|
---|
63 | void ResourceManager::setGameManager(GameManager *manager) {
|
---|
64 | this->gameManager = manager;
|
---|
65 | this->gameManager->printToConsole("ResourceManager initialised!");
|
---|
66 | }
|
---|
67 |
|
---|
68 | bool ResourceManager::loadXModel(std::string filename, Object3d *myObject) {
|
---|
69 | bool success = true;
|
---|
70 | bool alreadyLoaded = false;
|
---|
71 | modelResources *loadedResources = NULL;
|
---|
72 |
|
---|
73 | //check if file has been loaded already
|
---|
74 | for (UINT i = 0; i < this->resourceVector.size(); i++) {
|
---|
75 | if(filename == this->resourceVector[i].filename) {
|
---|
76 | alreadyLoaded = true;
|
---|
77 | loadedResources = &resourceVector[i];
|
---|
78 | break;
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | if(!alreadyLoaded) {
|
---|
83 | modelResources newModel;
|
---|
84 | wstring ws( filename.begin(), filename.end() );
|
---|
85 |
|
---|
86 | LPD3DXBUFFER pAdjacencyBuffer = NULL;
|
---|
87 | LPD3DXBUFFER pD3DXMtrlBuffer = NULL;
|
---|
88 | LPD3DXBUFFER ppEffectInstances = NULL;
|
---|
89 |
|
---|
90 | HRESULT hr = D3DXLoadMeshFromX(ws.c_str(),
|
---|
91 | D3DXMESH_MANAGED,
|
---|
92 | DXUTGetD3DDevice(),
|
---|
93 | &pAdjacencyBuffer,
|
---|
94 | &pD3DXMtrlBuffer,
|
---|
95 | &ppEffectInstances,
|
---|
96 | &newModel.NumMaterials,
|
---|
97 | &newModel.myMesh);
|
---|
98 |
|
---|
99 | if(hr != S_OK) {
|
---|
100 | success = false;
|
---|
101 | pAdjacencyBuffer = NULL;
|
---|
102 | pD3DXMtrlBuffer = NULL;
|
---|
103 | ppEffectInstances = NULL;
|
---|
104 | MessageBox(NULL, ws.c_str(), L"Error loading .x file!", MB_ICONERROR);
|
---|
105 | return success;
|
---|
106 | } else {
|
---|
107 | //set filename:
|
---|
108 | newModel.filename = filename;
|
---|
109 |
|
---|
110 | //calculate & update adjacency info:
|
---|
111 | newModel.adjacencyInfo = new DWORD[pAdjacencyBuffer->GetBufferSize()];
|
---|
112 | memcpy (newModel.adjacencyInfo, (DWORD*)pAdjacencyBuffer->GetBufferPointer(), pAdjacencyBuffer->GetBufferSize());
|
---|
113 |
|
---|
114 | //load materials & textures:
|
---|
115 | if(pD3DXMtrlBuffer != 0 && newModel.NumMaterials != 0)
|
---|
116 | {
|
---|
117 | D3DXMATERIAL* mtrls = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();
|
---|
118 |
|
---|
119 | for(UINT i = 0; i < newModel.NumMaterials; i++)
|
---|
120 | {
|
---|
121 | // the MatD3D property doesn't have an ambient value set
|
---|
122 | // when its loaded, so set it now:
|
---|
123 | mtrls[i].MatD3D.Ambient = mtrls[i].MatD3D.Diffuse;
|
---|
124 |
|
---|
125 | // save the ith material
|
---|
126 | newModel.Materials.push_back(mtrls[i].MatD3D);
|
---|
127 |
|
---|
128 | // check if the ith material has an associative texture
|
---|
129 | if(mtrls[i].pTextureFilename != 0)
|
---|
130 | {
|
---|
131 | // yes, load the texture for the ith subset
|
---|
132 | //this->myScene->manager->printToConsole(mtrls[i].pTextureFilename);
|
---|
133 | string temp = mtrls[i].pTextureFilename;
|
---|
134 | wstring texStr(temp.begin(), temp.end());
|
---|
135 |
|
---|
136 | // Change the current directory to the mesh's directory so we can
|
---|
137 | // find the textures.
|
---|
138 | WCHAR* pLastSlash = wcsrchr(ws.c_str(), L'/');
|
---|
139 | if( pLastSlash )
|
---|
140 | *(pLastSlash + 1) = 0;
|
---|
141 |
|
---|
142 | //save old dir
|
---|
143 | WCHAR strCWD[MAX_PATH];
|
---|
144 | GetCurrentDirectory(MAX_PATH, strCWD);
|
---|
145 |
|
---|
146 | //set new dir
|
---|
147 | SetCurrentDirectory(ws.c_str());
|
---|
148 |
|
---|
149 | IDirect3DTexture9* tex = 0;
|
---|
150 | D3DXCreateTextureFromFile(
|
---|
151 | DXUTGetD3DDevice(),
|
---|
152 | texStr.c_str(),
|
---|
153 | &tex);
|
---|
154 |
|
---|
155 | // save the loaded texture
|
---|
156 | newModel.Textures.push_back(tex);
|
---|
157 |
|
---|
158 | // restore old dir
|
---|
159 | SetCurrentDirectory(strCWD);
|
---|
160 | }
|
---|
161 | else
|
---|
162 | {
|
---|
163 | // no texture for the ith subset
|
---|
164 | newModel.Textures.push_back( 0 );
|
---|
165 | }
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | //Calculate Bounding Box
|
---|
170 | byte* verPointer;
|
---|
171 | DWORD numVertices = newModel.myMesh->GetNumVertices();
|
---|
172 | DWORD stride = newModel.myMesh->GetNumBytesPerVertex();
|
---|
173 | newModel.myMesh->LockVertexBuffer(0, (void**)&verPointer);
|
---|
174 |
|
---|
175 | D3DXVECTOR3 pMin;
|
---|
176 | D3DXVECTOR3 pMax;
|
---|
177 |
|
---|
178 | hr = D3DXComputeBoundingBox((D3DXVECTOR3 *) verPointer, numVertices, stride, &pMin, &pMax);
|
---|
179 |
|
---|
180 | if(hr!=S_OK) {
|
---|
181 | int i=0;
|
---|
182 | i++;
|
---|
183 | } else {
|
---|
184 | newModel.minBBox.setXYZ(pMin.x, pMin.y, pMin.z);
|
---|
185 | newModel.maxBBox.setXYZ(pMax.x, pMax.y, pMax.z);
|
---|
186 | }
|
---|
187 | newModel.myMesh->UnlockVertexBuffer();
|
---|
188 |
|
---|
189 | // Release Buffers
|
---|
190 | pAdjacencyBuffer->Release();
|
---|
191 | pD3DXMtrlBuffer->Release();
|
---|
192 | ppEffectInstances->Release();
|
---|
193 |
|
---|
194 | //store information in resourceManager:
|
---|
195 | this->resourceVector.push_back(newModel);
|
---|
196 |
|
---|
197 | //set current model resource
|
---|
198 | loadedResources = &this->resourceVector[resourceVector.size()-1];
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | //now fill in the object3d with the model resources:
|
---|
203 | //set mesh
|
---|
204 | myObject->setMesh(&(*loadedResources).myMesh);
|
---|
205 | //set number of materials
|
---|
206 | myObject->g_dwNumMaterials = (*loadedResources).NumMaterials;
|
---|
207 | //set adjacency info
|
---|
208 | myObject->adjacencyInfo = (*loadedResources).adjacencyInfo;
|
---|
209 | //set materials & textures:
|
---|
210 | myObject->Materials.resize((*loadedResources).Materials.size());
|
---|
211 | myObject->Textures.resize((*loadedResources).Textures.size());
|
---|
212 | for (UINT i = 0; i < (*loadedResources).NumMaterials; i++) {
|
---|
213 | myObject->Materials[i] = (*loadedResources).Materials[i];
|
---|
214 | myObject->Textures[i] = (*loadedResources).Textures[i];
|
---|
215 | }
|
---|
216 |
|
---|
217 | //Set local BoundingBox
|
---|
218 | myObject->minAABBox = (*loadedResources).minBBox;
|
---|
219 | myObject->maxAABBox = (*loadedResources).maxBBox;
|
---|
220 | myObject->setXFilename(filename);
|
---|
221 | myObject->isManagedByResourceManager = true;
|
---|
222 | myObject->setModelLoaded(true);
|
---|
223 |
|
---|
224 | return success;
|
---|
225 | }
|
---|
226 |
|
---|
227 | IDirect3DTexture9* ResourceManager::loadTexture(std::string filename, bool alwayNeeded) {
|
---|
228 | bool alreadyLoaded = false;
|
---|
229 | textureResources *loadedResources;
|
---|
230 |
|
---|
231 | //check if file has been loaded already
|
---|
232 | for (UINT i = 0; i < this->textureVector.size(); i++) {
|
---|
233 | if(filename == this->textureVector[i].filename) {
|
---|
234 | alreadyLoaded = true;
|
---|
235 | loadedResources = &textureVector[i];
|
---|
236 | break;
|
---|
237 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | if(!alreadyLoaded) {
|
---|
241 | //creating new structure
|
---|
242 | textureResources newTex;
|
---|
243 |
|
---|
244 | wstring texStr(filename.begin(), filename.end());
|
---|
245 |
|
---|
246 | //creating texture
|
---|
247 | IDirect3DTexture9* tex = 0;
|
---|
248 | D3DXCreateTextureFromFile(
|
---|
249 | DXUTGetD3DDevice(),
|
---|
250 | texStr.c_str(),
|
---|
251 | &tex);
|
---|
252 |
|
---|
253 | //setting texture
|
---|
254 | newTex.texture = tex;
|
---|
255 |
|
---|
256 | //setting filename
|
---|
257 | newTex.filename = filename;
|
---|
258 |
|
---|
259 | newTex.alwaysNeeded = true;
|
---|
260 |
|
---|
261 | //saving texture
|
---|
262 | this->textureVector.push_back(newTex);
|
---|
263 |
|
---|
264 | //set to current texture
|
---|
265 | loadedResources = &this->textureVector[textureVector.size()-1];
|
---|
266 | }
|
---|
267 |
|
---|
268 | return loadedResources->texture;
|
---|
269 | }
|
---|
270 |
|
---|
271 | FSOUND_SAMPLE* ResourceManager::loadSound(std::string filename, bool loadForLoop)
|
---|
272 | {
|
---|
273 | bool alreadyLoaded = false;
|
---|
274 | soundResources *loadedResources;
|
---|
275 |
|
---|
276 | //check if file has been loaded already
|
---|
277 | for (UINT i = 0; i < this->soundVector.size(); i++) {
|
---|
278 | if(filename == this->soundVector[i].filename) {
|
---|
279 | alreadyLoaded = true;
|
---|
280 | loadedResources = &soundVector[i];
|
---|
281 | break;
|
---|
282 | }
|
---|
283 | }
|
---|
284 | wstring texStr(filename.begin(), filename.end());
|
---|
285 | if(!alreadyLoaded) {
|
---|
286 | soundResources soundRes;
|
---|
287 | if(loadForLoop) {
|
---|
288 | soundRes.soundSample = FSOUND_Sample_Load(FSOUND_FREE, filename.c_str(), FSOUND_HW3D | FSOUND_LOOP_NORMAL, 0, 0);
|
---|
289 | } else {
|
---|
290 | soundRes.soundSample = FSOUND_Sample_Load(FSOUND_FREE, filename.c_str(), FSOUND_HW3D | FSOUND_NORMAL, 0, 0);
|
---|
291 | }
|
---|
292 | if(!soundRes.soundSample) {
|
---|
293 | MessageBox(NULL, texStr.c_str(), L"Error loading soundfile!", MB_ICONERROR);
|
---|
294 | return NULL;
|
---|
295 | } else {
|
---|
296 | soundRes.filename = filename;
|
---|
297 | soundRes.loadedForLoop = loadForLoop;
|
---|
298 | this->soundVector.push_back(soundRes);
|
---|
299 | loadedResources = &this->soundVector.at(this->soundVector.size()-1);
|
---|
300 | }
|
---|
301 | }
|
---|
302 | return loadedResources->soundSample;
|
---|
303 | }
|
---|
304 |
|
---|
305 | void ResourceManager::OnLostDevice()
|
---|
306 | {
|
---|
307 | this->gameManager->printToConsole("OnLostDevice in RESOURCEMANAGER!");
|
---|
308 | }
|
---|
309 |
|
---|
310 | void ResourceManager::OnDestroyDevice()
|
---|
311 | {
|
---|
312 | this->gameManager->printToConsole("OnDestroyDevice in RESOURCEMANAGER!");
|
---|
313 | //Delete the stuff
|
---|
314 | for (UINT i = 0; i < this->resourceVector.size(); i++) {
|
---|
315 | //delete mesh
|
---|
316 | if(resourceVector[i].myMesh!=NULL) {
|
---|
317 | SAFE_RELEASE(resourceVector[i].myMesh);
|
---|
318 | }
|
---|
319 |
|
---|
320 | // delete adjacencyInfo
|
---|
321 | delete [] resourceVector[i].adjacencyInfo;
|
---|
322 | // delete materials
|
---|
323 | resourceVector[i].Materials.clear();
|
---|
324 |
|
---|
325 | // release textures
|
---|
326 | for(UINT j = 0; j < resourceVector[i].Textures.size(); j++) {
|
---|
327 | if (resourceVector[i].Textures[j] != 0) {
|
---|
328 | SAFE_RELEASE(resourceVector[i].Textures[j]);
|
---|
329 | resourceVector[i].Textures[j] = 0;
|
---|
330 | }
|
---|
331 | }
|
---|
332 | }
|
---|
333 |
|
---|
334 | for (UINT i = 0; i < this->textureVector.size(); i++) {
|
---|
335 | SAFE_RELEASE(textureVector[i].texture);
|
---|
336 | }
|
---|
337 |
|
---|
338 | resourceVector.clear();
|
---|
339 | textureVector.clear();
|
---|
340 |
|
---|
341 | this->gameManager->printToConsole("OnDestroyDevice in RESOURCEMANAGER FINISHED!");
|
---|
342 | } |
---|