Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

d3d9texture.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002         filename:       d3d9texture.cpp
00003         created:        17/7/2004
00004         author:         Paul D Turner with D3D 9 Updates by Magnus Österlind
00005         
00006         purpose:        Implements texture class for D3D9.0 system
00007 *************************************************************************/
00008 /*************************************************************************
00009     Crazy Eddie's GUI System (http://www.cegui.org.uk)
00010     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
00011 
00012     This library is free software; you can redistribute it and/or
00013     modify it under the terms of the GNU Lesser General Public
00014     License as published by the Free Software Foundation; either
00015     version 2.1 of the License, or (at your option) any later version.
00016 
00017     This library is distributed in the hope that it will be useful,
00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020     Lesser General Public License for more details.
00021 
00022     You should have received a copy of the GNU Lesser General Public
00023     License along with this library; if not, write to the Free Software
00024     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 *************************************************************************/
00026 #include "renderers/directx9GUIRenderer/d3d9texture.h"
00027 #include "renderers/directx9GUIRenderer/d3d9renderer.h"
00028 #include "CEGUIExceptions.h"
00029 #include "CEGUISystem.h"
00030 
00031 #include <d3dx9.h>
00032 #include <dxerr9.h>
00033 #undef max
00034 
00035 // Start of CEGUI namespace section
00036 namespace CEGUI
00037 {
00038 
00039 /*************************************************************************
00040         Constructor
00041 *************************************************************************/
00042 DirectX9Texture::DirectX9Texture(Renderer* owner) :
00043         Texture(owner)
00044 {
00045         d_d3dtexture = NULL;
00046 
00047         // do this mainly to indicate the lack of a filename.
00048         d_isMemoryTexture = true;
00049 }
00050 
00051 /*************************************************************************
00052         Destructor
00053 *************************************************************************/
00054 DirectX9Texture::~DirectX9Texture(void)
00055 {
00056         freeD3DTexture();
00057 }
00058 
00059 /*************************************************************************
00060         Load texture from file.  Texture is made to be same size as image in
00061         file.
00062 *************************************************************************/
00063 void DirectX9Texture::loadFromFile(const String& filename, const String& resourceGroup)
00064 {
00065         freeD3DTexture();
00066         
00067         // load the file via the resource provider
00068         RawDataContainer texFile;
00069         System::getSingleton().getResourceProvider()->loadRawDataContainer(filename, texFile, resourceGroup);
00070 
00071         D3DXIMAGE_INFO texInfo;
00072         HRESULT hr = D3DXCreateTextureFromFileInMemoryEx(((DirectX9Renderer*)getRenderer())->getDevice(), texFile.getDataPtr(),
00073             static_cast<UINT>(texFile.getSize()), D3DX_DEFAULT, D3DX_DEFAULT, 1, 0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT,
00074             D3DX_DEFAULT, D3DX_DEFAULT, 0, &texInfo, NULL, &d_d3dtexture);
00075         
00076         System::getSingleton().getResourceProvider()->unloadRawDataContainer(texFile);
00077         
00078         if (SUCCEEDED(hr))
00079         {
00080                 d_width         = (ushort)texInfo.Width;
00081                 d_height        = (ushort)texInfo.Height;
00082 
00083                 d_filename = filename;
00084         d_resourceGroup = resourceGroup;
00085                 d_isMemoryTexture = false;
00086         }
00087         else
00088         {
00089                 throw RendererException((utf8*)"Failed to create Texture object from file '" + filename + "'.  Additional Info: " + (const utf8*)DXGetErrorString9(hr));
00090         }
00091 
00092 }
00093 
00094 
00095 /*************************************************************************
00096         Load texture from raw memory.   
00097 *************************************************************************/
00098 void DirectX9Texture::loadFromMemory(const void* buffPtr, uint buffWidth, uint buffHeight)
00099 {
00100         using namespace std;
00101 
00102         // release old texture
00103         freeD3DTexture();
00104 
00105         // calculate square size big enough for whole memory buffer
00106         uint tex_size = ceguimax(buffWidth, buffHeight);
00107 
00108         // create a texture
00109         // TODO: Check resulting pixel format and react appropriately.
00110         HRESULT hr = D3DXCreateTexture(((DirectX9Renderer*)getRenderer())->getDevice(), tex_size, tex_size, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &d_d3dtexture);
00111 
00112         if (FAILED(hr))
00113         {
00114                 throw RendererException((utf8*)"Failed to load texture from memory: D3D Texture creation failed.");
00115         }
00116         else
00117         {
00118                 D3DSURFACE_DESC texdesc;
00119                 d_d3dtexture->GetLevelDesc(0, &texdesc);
00120 
00121                 // store new size;
00122                 d_width         = (ushort)texdesc.Width;
00123                 d_height        = (ushort)texdesc.Height;
00124 
00125                 // lock the D3D texture
00126                 D3DLOCKED_RECT  rect;
00127                 hr = d_d3dtexture->LockRect(0, &rect, NULL, 0);
00128 
00129                 if (FAILED(hr))
00130                 {
00131                         d_d3dtexture->Release();
00132                         d_d3dtexture = NULL;
00133 
00134                         throw RendererException((utf8*)"Failed to load texture from memory: IDirect3DTexture9::LockRect failed.");
00135                 }
00136                 else
00137                 {
00138                         // copy data from buffer into texture
00139                         ulong* dst = (ulong*)rect.pBits;
00140                         ulong* src = (ulong*)buffPtr;
00141 
00142                         for (uint i = 0; i < buffHeight; ++i)
00143                         {
00144                                 for (uint j = 0; j < buffWidth; ++j)
00145                                 {
00146                                         dst[j] = src[j];
00147                                 }
00148 
00149                                 dst += rect.Pitch / sizeof(ulong);
00150                                 src += buffWidth;
00151                         }
00152 
00153                         d_d3dtexture->UnlockRect(0);
00154                 }
00155 
00156         }
00157 
00158 }
00159 
00160 
00161 /*************************************************************************
00162         safely release D3D texture associated with this Texture
00163 *************************************************************************/
00164 void DirectX9Texture::freeD3DTexture(void)
00165 {
00166         if (d_d3dtexture != NULL)
00167         {
00168                 d_d3dtexture->Release();
00169                 d_d3dtexture = NULL;
00170         }
00171 
00172         d_filename.clear();
00173         d_isMemoryTexture = true;
00174 }
00175 
00176 
00177 /*************************************************************************
00178         allocate a D3D texture >= 'size'.  Previous D3D texture is lost
00179 *************************************************************************/
00180 void DirectX9Texture::setD3DTextureSize(uint size)
00181 {
00182         freeD3DTexture();
00183 
00184         HRESULT hr = D3DXCreateTexture(((DirectX9Renderer*)getRenderer())->getDevice(), size, size, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &d_d3dtexture);
00185 
00186         if (FAILED(hr))
00187         {
00188                 throw RendererException((utf8*)"Failed to create texture of specified size: D3D Texture creation failed.");
00189         }
00190         else
00191         {
00192                 D3DSURFACE_DESC texdesc;
00193                 d_d3dtexture->GetLevelDesc(0, &texdesc);
00194 
00195                 // store new size;
00196                 d_width         = (ushort)texdesc.Width;
00197                 d_height        = (ushort)texdesc.Height;
00198         }
00199 
00200 }
00201 
00202 
00203 /*************************************************************************
00204         Direct3D support method that must be called prior to a Reset call
00205         on the Direct3DDevice.
00206 *************************************************************************/
00207 void DirectX9Texture::preD3DReset(void)
00208 {
00209         // textures not based on files are in the managed pool, so we do
00210         // not worry about those.
00211         if (!d_isMemoryTexture)
00212         {
00213                 // release the d3d texture
00214                 if (d_d3dtexture != NULL)
00215                 {
00216                         if (FAILED(d_d3dtexture->Release()))
00217                         {
00218                                 throw RendererException("DirectX9Texture::preD3DReset - failed to release the Direct3DTexture9 object for this texture.");
00219                         }
00220 
00221                         d_d3dtexture = NULL;
00222                 }
00223 
00224         }
00225 
00226 }
00227 
00228 
00229 /*************************************************************************
00230         Direct3D support method that must be called after a Reset call on the
00231         Direct3DDevice.
00232 *************************************************************************/
00233 void DirectX9Texture::postD3DReset(void)
00234 {
00235         // textures not based on files are in the managed pool, so we do
00236         // not worry about those.
00237         if (!d_isMemoryTexture)
00238         {
00239                 // The reason this copy is made is that d_filename will be reset once we
00240                 // call loadFromFile and loading would always fail due to invalid filenames.
00241                 String name(d_filename);
00242 
00243                 // re-load the texture
00244                 loadFromFile(name, d_resourceGroup);
00245         }
00246 
00247 }
00248 
00249 } // End of  CEGUI namespace section

Generated on Wed Sep 7 09:56:35 2005 for Crazy Eddies GUI System by  doxygen 1.4.3