afResourceManager.cpp
资源名称:AirForce.rar [点击查看]
上传用户:kaiguan
上传日期:2007-10-28
资源大小:1074k
文件大小:4k
源码类别:
其他游戏
开发平台:
Visual C++
- #include "stdafx.h"
- #include "d3dx9tex.h"
- #include "afApplication.h"
- #include "afResourceManager.h"
- #include "afLog.h"
- #include "afSettings.h"
- #include "afTexture.h"
- #include <direct.h>
- static LPDIRECT3DDEVICE9 pd3dDevice = NULL;
- static afTexture* textures[afResourceManager::MAX_TEXTURES];
- static afPtrList<afImage> imageList;
- afTexture* getLoadedTexture(int nID);
- afTexture* getLoadedTexture(const afString& nName);
- int getLoadedTextureID(const afString& nName);
- void afResourceManager::init()
- {
- for(int i=0; i<MAX_TEXTURES; i++)
- textures[i] = NULL;
- pd3dDevice =g_pApp->m_pd3dDevice;
- if(pd3dDevice==NULL)
- afLog::error("afResourceManager::init failed: d3d device not available");
- }
- void afResourceManager::deinit()
- {
- for(int i=0;i<MAX_TEXTURES;i++)
- {
- SAFE_DELETE(textures[i]);
- }
- for(i=0;i<imageList.getSize();i++)
- {
- afImage* p=imageList[i];
- SAFE_DELETE(p);
- }
- }
- afImage* afResourceManager::getRawImage(const afString& nFileName, int nWidth, int nHeight, afImage::FORMAT nFormat)
- {
- int size = nWidth * nHeight * afImage::getPixelSize(nFormat), read;
- FILE* fp;
- unsigned char* data;
- afImage* img;
- afString fullName;
- fullName = afSettings::tmpFullPath(nFileName, ".raw");
- if(size==0)
- {
- afLog::error("can not load raw image '%s': illegal image properties", nFileName);
- return NULL;
- }
- fp=fopen(fullName, "rb");
- if(fp==NULL)
- {
- afLog::error("can not load raw image '%s': file not found", nFileName);
- return NULL;
- }
- data = new unsigned char[size];
- read = fread(data, 1, size, fp);
- if(read!=size)
- {
- afLog::error("loading raw image '%s' with %d x %d pixels (%d bytes) failed: file not large enough", nFileName, nWidth, nHeight, size);
- fclose(fp);
- return NULL;
- }
- img = new afImage(nWidth, nHeight, nFormat, nFileName, data, false);
- imageList.addTail(img);
- fclose(fp);
- afLog::info("raw image '%s' (%dx%d %dbits) loaded", nFileName, nWidth, nHeight, 8*afImage::getPixelSize(nFormat));
- return img;
- }
- afTexture* afResourceManager::getTexture(const afString& nFileName, afImage::FORMAT nFormat)
- {
- afString fName0, fName1, fName2, fName3;
- int i;
- if(pd3dDevice==NULL)
- {
- afLog::error("can not load texture '%s': d3d device not set yet",nFileName);
- return NULL;
- }
- afTexture* tex = getLoadedTexture(nFileName);
- if(tex!=NULL) // already loaded ???
- return tex;
- for(i=0; i<MAX_TEXTURES; i++)
- if(textures[i] == NULL)
- {
- LPDIRECT3DTEXTURE9 tex = NULL;
- D3DXIMAGE_INFO imgInfo;
- fName0 = afSettings::tmpFullPath(nFileName, ".dds");
- fName1 = afSettings::tmpFullPath(nFileName, ".tga");
- fName2 = afSettings::tmpFullPath(nFileName, ".jpg");
- fName3 = afSettings::tmpFullPath(nFileName, ".bmp");
- D3DFORMAT fmt = afImage::getD3DFormat(nFormat);
- if(FAILED(D3DXCreateTextureFromFileEx(pd3dDevice,fName0,D3DX_DEFAULT,D3DX_DEFAULT,D3DX_DEFAULT,0,fmt,D3DPOOL_MANAGED,D3DX_DEFAULT,D3DX_DEFAULT,0,&imgInfo,NULL,&tex)))
- if(FAILED(D3DXCreateTextureFromFileEx(pd3dDevice,fName1,D3DX_DEFAULT,D3DX_DEFAULT,D3DX_DEFAULT,0,fmt,D3DPOOL_MANAGED,D3DX_DEFAULT,D3DX_DEFAULT,0,&imgInfo,NULL,&tex)))
- if(FAILED(D3DXCreateTextureFromFileEx(pd3dDevice,fName2,D3DX_DEFAULT,D3DX_DEFAULT,D3DX_DEFAULT,0,fmt,D3DPOOL_MANAGED,D3DX_DEFAULT,D3DX_DEFAULT,0,&imgInfo,NULL,&tex)))
- if(FAILED(D3DXCreateTextureFromFileEx(pd3dDevice,fName3,D3DX_DEFAULT,D3DX_DEFAULT,D3DX_DEFAULT,0,fmt,D3DPOOL_MANAGED,D3DX_DEFAULT,D3DX_DEFAULT,0,&imgInfo,NULL,&tex)))
- {
- afLog::error("texture '%s' not found: using 'texture_not_found'", nFileName);
- return NULL;
- }
- textures[i] = new afTexture(nFileName, tex, imgInfo.Width, imgInfo.Height,
- (afImage::FORMAT)imgInfo.Format);
- afLog::info("texture '%s' (%dx%d %s) loaded", nFileName, imgInfo.Width, imgInfo.Height, afImage::getFormatString((afImage::FORMAT)imgInfo.Format));
- return textures[i];
- }
- afLog::error("can not load texture '%s': too many textures in memory (%d)", nFileName, MAX_TEXTURES);
- return NULL;
- }
- afTexture* getLoadedTexture(int nID)
- {
- if(nID>=afResourceManager::MAX_TEXTURES || nID<0)
- return NULL;
- return textures[nID];
- }
- afTexture* getLoadedTexture(const afString& nName)
- {
- return getLoadedTexture(getLoadedTextureID(nName));
- }
- int getLoadedTextureID(const afString& nName)
- {
- for(int i=0; i<afResourceManager::MAX_TEXTURES; i++)
- if(textures[i]!=NULL && textures[i]->getName()==nName)
- return i;
- return -1;
- }