#include "dxstdafx.h" #include "ResourceManager.h" #include "GameManager.h" ResourceManager::ResourceManager(void) { } ResourceManager::~ResourceManager(void) { this->cleanUpResources(true); } void ResourceManager::cleanUpResources(bool endOfGame) { //Delete the stuff for (UINT i = 0; i < this->resourceVector.size(); i++) { //delete mesh if(resourceVector[i].myMesh!=NULL) { SAFE_RELEASE(resourceVector[i].myMesh); } // delete adjacencyInfo delete [] resourceVector[i].adjacencyInfo; // delete materials resourceVector[i].Materials.clear(); // release textures for(UINT j = 0; j < resourceVector[i].Textures.size(); j++) { if (resourceVector[i].Textures[j] != 0) { SAFE_RELEASE(resourceVector[i].Textures[j]); } } } resourceVector.clear(); std::vector::iterator it; for(it=this->textureVector.begin();it!=this->textureVector.end();it++) { if((!it->alwaysNeeded) || endOfGame) { SAFE_RELEASE(it->texture); textureVector.erase(it); it--; } } for(UINT i=0;isoundVector.size();i++) { FSOUND_Sample_Free(this->soundVector.at(i).soundSample); } this->soundVector.clear(); if(endOfGame) { textureVector.clear(); } } void ResourceManager::freeUsedResources(SPTR node) { } void ResourceManager::freeUsedResources(SPTR renderer) { } void ResourceManager::setGameManager(GameManager *manager) { this->gameManager = manager; this->gameManager->printToConsole("ResourceManager initialised!"); } bool ResourceManager::loadXModel(std::string filename, Object3d *myObject) { bool success = true; bool alreadyLoaded = false; modelResources *loadedResources = NULL; //check if file has been loaded already for (UINT i = 0; i < this->resourceVector.size(); i++) { if(filename == this->resourceVector[i].filename) { alreadyLoaded = true; loadedResources = &resourceVector[i]; break; } } if(!alreadyLoaded) { modelResources newModel; wstring ws( filename.begin(), filename.end() ); LPD3DXBUFFER pAdjacencyBuffer = NULL; LPD3DXBUFFER pD3DXMtrlBuffer = NULL; LPD3DXBUFFER ppEffectInstances = NULL; HRESULT hr = D3DXLoadMeshFromX(ws.c_str(), D3DXMESH_MANAGED, DXUTGetD3DDevice(), &pAdjacencyBuffer, &pD3DXMtrlBuffer, &ppEffectInstances, &newModel.NumMaterials, &newModel.myMesh); if(hr != S_OK) { success = false; pAdjacencyBuffer = NULL; pD3DXMtrlBuffer = NULL; ppEffectInstances = NULL; MessageBox(NULL, ws.c_str(), L"Error loading .x file!", MB_ICONERROR); return success; } else { //set filename: newModel.filename = filename; //calculate & update adjacency info: newModel.adjacencyInfo = new DWORD[pAdjacencyBuffer->GetBufferSize()]; memcpy (newModel.adjacencyInfo, (DWORD*)pAdjacencyBuffer->GetBufferPointer(), pAdjacencyBuffer->GetBufferSize()); //load materials & textures: if(pD3DXMtrlBuffer != 0 && newModel.NumMaterials != 0) { D3DXMATERIAL* mtrls = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer(); for(UINT i = 0; i < newModel.NumMaterials; i++) { // the MatD3D property doesn't have an ambient value set // when its loaded, so set it now: mtrls[i].MatD3D.Ambient = mtrls[i].MatD3D.Diffuse; // save the ith material newModel.Materials.push_back(mtrls[i].MatD3D); // check if the ith material has an associative texture if(mtrls[i].pTextureFilename != 0) { // yes, load the texture for the ith subset //this->myScene->manager->printToConsole(mtrls[i].pTextureFilename); string temp = mtrls[i].pTextureFilename; wstring texStr(temp.begin(), temp.end()); // Change the current directory to the mesh's directory so we can // find the textures. WCHAR* pLastSlash = wcsrchr(ws.c_str(), L'/'); if( pLastSlash ) *(pLastSlash + 1) = 0; //save old dir WCHAR strCWD[MAX_PATH]; GetCurrentDirectory(MAX_PATH, strCWD); //set new dir SetCurrentDirectory(ws.c_str()); IDirect3DTexture9* tex = 0; D3DXCreateTextureFromFile( DXUTGetD3DDevice(), texStr.c_str(), &tex); // save the loaded texture newModel.Textures.push_back(tex); // restore old dir SetCurrentDirectory(strCWD); } else { // no texture for the ith subset newModel.Textures.push_back( 0 ); } } } //Calculate Bounding Box byte* verPointer; DWORD numVertices = newModel.myMesh->GetNumVertices(); DWORD stride = newModel.myMesh->GetNumBytesPerVertex(); newModel.myMesh->LockVertexBuffer(0, (void**)&verPointer); D3DXVECTOR3 pMin; D3DXVECTOR3 pMax; hr = D3DXComputeBoundingBox((D3DXVECTOR3 *) verPointer, numVertices, stride, &pMin, &pMax); if(hr!=S_OK) { int i=0; i++; } else { newModel.minBBox.setXYZ(pMin.x, pMin.y, pMin.z); newModel.maxBBox.setXYZ(pMax.x, pMax.y, pMax.z); } newModel.myMesh->UnlockVertexBuffer(); // Release Buffers pAdjacencyBuffer->Release(); pD3DXMtrlBuffer->Release(); ppEffectInstances->Release(); //store information in resourceManager: this->resourceVector.push_back(newModel); //set current model resource loadedResources = &this->resourceVector[resourceVector.size()-1]; } } //now fill in the object3d with the model resources: //set mesh myObject->setMesh(&(*loadedResources).myMesh); //set number of materials myObject->g_dwNumMaterials = (*loadedResources).NumMaterials; //set adjacency info myObject->adjacencyInfo = (*loadedResources).adjacencyInfo; //set materials & textures: myObject->Materials.resize((*loadedResources).Materials.size()); myObject->Textures.resize((*loadedResources).Textures.size()); for (UINT i = 0; i < (*loadedResources).NumMaterials; i++) { myObject->Materials[i] = (*loadedResources).Materials[i]; myObject->Textures[i] = (*loadedResources).Textures[i]; } //Set local BoundingBox myObject->minAABBox = (*loadedResources).minBBox; myObject->maxAABBox = (*loadedResources).maxBBox; myObject->setXFilename(filename); myObject->isManagedByResourceManager = true; myObject->setModelLoaded(true); return success; } IDirect3DTexture9* ResourceManager::loadTexture(std::string filename, bool alwayNeeded) { bool alreadyLoaded = false; textureResources *loadedResources; //check if file has been loaded already for (UINT i = 0; i < this->textureVector.size(); i++) { if(filename == this->textureVector[i].filename) { alreadyLoaded = true; loadedResources = &textureVector[i]; break; } } if(!alreadyLoaded) { //creating new structure textureResources newTex; wstring texStr(filename.begin(), filename.end()); //creating texture IDirect3DTexture9* tex = 0; D3DXCreateTextureFromFile( DXUTGetD3DDevice(), texStr.c_str(), &tex); //setting texture newTex.texture = tex; //setting filename newTex.filename = filename; newTex.alwaysNeeded = true; //saving texture this->textureVector.push_back(newTex); //set to current texture loadedResources = &this->textureVector[textureVector.size()-1]; } return loadedResources->texture; } FSOUND_SAMPLE* ResourceManager::loadSound(std::string filename, bool loadForLoop) { bool alreadyLoaded = false; soundResources *loadedResources; //check if file has been loaded already for (UINT i = 0; i < this->soundVector.size(); i++) { if(filename == this->soundVector[i].filename) { alreadyLoaded = true; loadedResources = &soundVector[i]; break; } } wstring texStr(filename.begin(), filename.end()); if(!alreadyLoaded) { soundResources soundRes; if(loadForLoop) { soundRes.soundSample = FSOUND_Sample_Load(FSOUND_FREE, filename.c_str(), FSOUND_HW3D | FSOUND_LOOP_NORMAL, 0, 0); } else { soundRes.soundSample = FSOUND_Sample_Load(FSOUND_FREE, filename.c_str(), FSOUND_HW3D | FSOUND_NORMAL, 0, 0); } if(!soundRes.soundSample) { MessageBox(NULL, texStr.c_str(), L"Error loading soundfile!", MB_ICONERROR); return NULL; } else { soundRes.filename = filename; soundRes.loadedForLoop = loadForLoop; this->soundVector.push_back(soundRes); loadedResources = &this->soundVector.at(this->soundVector.size()-1); } } return loadedResources->soundSample; } void ResourceManager::OnLostDevice() { this->gameManager->printToConsole("OnLostDevice in RESOURCEMANAGER!"); } void ResourceManager::OnDestroyDevice() { this->gameManager->printToConsole("OnDestroyDevice in RESOURCEMANAGER!"); //Delete the stuff for (UINT i = 0; i < this->resourceVector.size(); i++) { //delete mesh if(resourceVector[i].myMesh!=NULL) { SAFE_RELEASE(resourceVector[i].myMesh); } // delete adjacencyInfo delete [] resourceVector[i].adjacencyInfo; // delete materials resourceVector[i].Materials.clear(); // release textures for(UINT j = 0; j < resourceVector[i].Textures.size(); j++) { if (resourceVector[i].Textures[j] != 0) { SAFE_RELEASE(resourceVector[i].Textures[j]); resourceVector[i].Textures[j] = 0; } } } for (UINT i = 0; i < this->textureVector.size(); i++) { SAFE_RELEASE(textureVector[i].texture); } resourceVector.clear(); textureVector.clear(); this->gameManager->printToConsole("OnDestroyDevice in RESOURCEMANAGER FINISHED!"); }