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

texture.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002         filename:       texture.cpp
00003         created:        10/4/2004
00004         author:         Paul D Turner
00005         
00006         purpose:        Implements texture class for D3D8.1 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/directx81GUIRenderer/texture.h"
00027 #include "renderers/directx81GUIRenderer/renderer.h"
00028 #include "CEGUIExceptions.h"
00029 #include "CEGUISystem.h"
00030 
00031 #include <d3dx8.h>
00032 #include <dxerr8.h>
00033 
00034 // remove Microsoft idiocy
00035 #undef max
00036 #undef min
00037 
00038 
00039 // Start of CEGUI namespace section
00040 namespace CEGUI
00041 {
00042 
00043 /*************************************************************************
00044         Constructor
00045 *************************************************************************/
00046 DirectX81Texture::DirectX81Texture(Renderer* owner) :
00047         Texture(owner)
00048 {
00049         d_d3dtexture = NULL;
00050 
00051         // do this mainly to indicate the lack of a filename.
00052         d_isMemoryTexture = true;
00053 }
00054 
00055 /*************************************************************************
00056         Destructor
00057 *************************************************************************/
00058 DirectX81Texture::~DirectX81Texture(void)
00059 {
00060         freeD3DTexture();
00061 }
00062 
00063 /*************************************************************************
00064         Load texture from file.  Texture is made to be same size as image in
00065         file.
00066 *************************************************************************/
00067 void DirectX81Texture::loadFromFile(const String& filename, const String& resourceGroup)
00068 {
00069         freeD3DTexture();
00070 
00071         // load the file via the resource provider
00072         RawDataContainer texFile;
00073         System::getSingleton().getResourceProvider()->loadRawDataContainer(filename, texFile, resourceGroup);
00074         
00075         D3DXIMAGE_INFO texInfo;
00076         HRESULT hr = D3DXCreateTextureFromFileInMemoryEx(((DirectX81Renderer*)getRenderer())->getDevice(), texFile.getDataPtr(),
00077             static_cast<UINT>(texFile.getSize()), D3DX_DEFAULT, D3DX_DEFAULT, 1, 0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT,
00078             D3DX_DEFAULT, D3DX_DEFAULT, 0, &texInfo, NULL, &d_d3dtexture);
00079 
00080         System::getSingleton().getResourceProvider()->unloadRawDataContainer(texFile);
00081 
00082         if (SUCCEEDED(hr))
00083         {
00084                 d_width         = static_cast<ushort>(texInfo.Width);
00085                 d_height        = static_cast<ushort>(texInfo.Height);
00086 
00087                 d_filename = filename;
00088         d_resourceGroup = resourceGroup;
00089                 d_isMemoryTexture = false;
00090         }
00091         else
00092         {
00093                 throw RendererException((utf8*)"Failed to create Texture object from file '" + filename + "'.  Additional Info: " + (const utf8*)DXGetErrorString8(hr));
00094         }
00095 
00096 }
00097 
00098 
00099 /*************************************************************************
00100         Load texture from raw memory.   
00101 *************************************************************************/
00102 void DirectX81Texture::loadFromMemory(const void* buffPtr, uint buffWidth, uint buffHeight)
00103 {
00104         using namespace std;
00105 
00106         // release old texture
00107         freeD3DTexture();
00108 
00109         // calculate square size big enough for whole memory buffer
00110         uint tex_size = ceguimax(buffWidth, buffHeight);
00111 
00112         // create a texture
00113         // TODO: Check resulting pixel format and react appropriately.
00114         HRESULT hr = D3DXCreateTexture(((DirectX81Renderer*)getRenderer())->getDevice(), tex_size, tex_size, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &d_d3dtexture);
00115 
00116         if (FAILED(hr))
00117         {
00118                 throw RendererException((utf8*)"Failed to load texture from memory: D3D Texture creation failed.");
00119         }
00120         else
00121         {
00122                 D3DSURFACE_DESC texdesc;
00123                 d_d3dtexture->GetLevelDesc(0, &texdesc);
00124 
00125                 // store new size;
00126                 d_width         = static_cast<ushort>(texdesc.Width);
00127                 d_height        = static_cast<ushort>(texdesc.Height);
00128 
00129                 // lock the D3D texture
00130                 D3DLOCKED_RECT  rect;
00131                 hr = d_d3dtexture->LockRect(0, &rect, NULL, 0);
00132 
00133                 if (FAILED(hr))
00134                 {
00135                         d_d3dtexture->Release();
00136                         d_d3dtexture = NULL;
00137 
00138                         throw RendererException((utf8*)"Failed to load texture from memory: IDirect3DTexture8::LockRect failed.");
00139                 }
00140                 else
00141                 {
00142                         // copy data from buffer into texture
00143                         ulong* dst = (ulong*)rect.pBits;
00144                         ulong* src = (ulong*)buffPtr;
00145 
00146                         for (uint i = 0; i < buffHeight; ++i)
00147                         {
00148                                 for (uint j = 0; j < buffWidth; ++j)
00149                                 {
00150                                         dst[j] = src[j];
00151                                 }
00152 
00153                                 dst += rect.Pitch / sizeof(ulong);
00154                                 src += buffWidth;
00155                         }
00156 
00157                         d_d3dtexture->UnlockRect(0);
00158                 }
00159 
00160         }
00161 
00162 }
00163 
00164 
00165 /*************************************************************************
00166         safely release D3D texture associated with this Texture
00167 *************************************************************************/
00168 void DirectX81Texture::freeD3DTexture(void)
00169 {
00170         if (d_d3dtexture != NULL)
00171         {
00172                 d_d3dtexture->Release();
00173                 d_d3dtexture = NULL;
00174         }
00175 
00176         d_filename.clear();
00177         d_isMemoryTexture = true;
00178 }
00179 
00180 
00181 /*************************************************************************
00182         allocate a D3D texture >= 'size'.  Previous D3D texture is lost
00183 *************************************************************************/
00184 void DirectX81Texture::setD3DTextureSize(uint size)
00185 {
00186         freeD3DTexture();
00187 
00188         HRESULT hr = D3DXCreateTexture(((DirectX81Renderer*)getRenderer())->getDevice(), size, size, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &d_d3dtexture);
00189 
00190         if (FAILED(hr))
00191         {
00192                 throw RendererException((utf8*)"Failed to create texture of specified size: D3D Texture creation failed.");
00193         }
00194         else
00195         {
00196                 D3DSURFACE_DESC texdesc;
00197                 d_d3dtexture->GetLevelDesc(0, &texdesc);
00198 
00199                 // store new size;
00200                 d_width         = static_cast<ushort>(texdesc.Width);
00201                 d_height        = static_cast<ushort>(texdesc.Height);
00202         }
00203 
00204 }
00205 
00206 
00207 /*************************************************************************
00208         Direct3D support method that must be called prior to a Reset call
00209         on the Direct3DDevice.
00210 *************************************************************************/
00211 void DirectX81Texture::preD3DReset(void)
00212 {
00213         // textures not based on files are in the managed pool, so we do
00214         // not worry about those.
00215         if (!d_isMemoryTexture)
00216         {
00217                 // release the d3d texture
00218                 if (d_d3dtexture != NULL)
00219                 {
00220                         if (FAILED(d_d3dtexture->Release()))
00221                         {
00222                                 throw RendererException("DirectX81Texture::preD3DReset - failed to release the Direct3DTexture8 object for this texture.");
00223                         }
00224 
00225                         d_d3dtexture = NULL;
00226                 }
00227 
00228         }
00229 
00230 }
00231 
00232 
00233 /*************************************************************************
00234         Direct3D support method that must be called after a Reset call on the
00235         Direct3DDevice.
00236 *************************************************************************/
00237 void DirectX81Texture::postD3DReset(void)
00238 {
00239         // textures not based on files are in the managed pool, so we do
00240         // not worry about those.
00241         if (!d_isMemoryTexture)
00242         {
00243                 // The reason this copy is made is that d_filename will be reset once we
00244                 // call loadFromFile and loading would always fail due to invalid filenames.
00245                 String name(d_filename);
00246 
00247                 // re-load the texture
00248                 loadFromFile(name, d_resourceGroup);
00249         }
00250 
00251 }
00252 
00253 } // End of  CEGUI namespace section

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