00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
00036 namespace CEGUI
00037 {
00038
00039
00040
00041
00042 DirectX9Texture::DirectX9Texture(Renderer* owner) :
00043 Texture(owner)
00044 {
00045 d_d3dtexture = NULL;
00046
00047
00048 d_isMemoryTexture = true;
00049 }
00050
00051
00052
00053
00054 DirectX9Texture::~DirectX9Texture(void)
00055 {
00056 freeD3DTexture();
00057 }
00058
00059
00060
00061
00062
00063 void DirectX9Texture::loadFromFile(const String& filename, const String& resourceGroup)
00064 {
00065 freeD3DTexture();
00066
00067
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(), texFile.getSize(),
00073 D3DX_DEFAULT, D3DX_DEFAULT, 1, 0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT,
00074 0, &texInfo, NULL, &d_d3dtexture);
00075
00076 if (SUCCEEDED(hr))
00077 {
00078 d_width = (ushort)texInfo.Width;
00079 d_height = (ushort)texInfo.Height;
00080
00081 d_filename = filename;
00082 d_resourceGroup = resourceGroup;
00083 d_isMemoryTexture = false;
00084 }
00085 else
00086 {
00087 throw RendererException((utf8*)"Failed to create Texture object from file '" + filename + "'. Additional Info: " + (const utf8*)DXGetErrorString9(hr));
00088 }
00089
00090 }
00091
00092
00093
00094
00095
00096 void DirectX9Texture::loadFromMemory(const void* buffPtr, uint buffWidth, uint buffHeight)
00097 {
00098 using namespace std;
00099
00100
00101 freeD3DTexture();
00102
00103
00104 uint tex_size = ceguimax(buffWidth, buffHeight);
00105
00106
00107
00108 HRESULT hr = D3DXCreateTexture(((DirectX9Renderer*)getRenderer())->getDevice(), tex_size, tex_size, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &d_d3dtexture);
00109
00110 if (FAILED(hr))
00111 {
00112 throw RendererException((utf8*)"Failed to load texture from memory: D3D Texture creation failed.");
00113 }
00114 else
00115 {
00116 D3DSURFACE_DESC texdesc;
00117 d_d3dtexture->GetLevelDesc(0, &texdesc);
00118
00119
00120 d_width = (ushort)texdesc.Width;
00121 d_height = (ushort)texdesc.Height;
00122
00123
00124 D3DLOCKED_RECT rect;
00125 hr = d_d3dtexture->LockRect(0, &rect, NULL, 0);
00126
00127 if (FAILED(hr))
00128 {
00129 d_d3dtexture->Release();
00130 d_d3dtexture = NULL;
00131
00132 throw RendererException((utf8*)"Failed to load texture from memory: IDirect3DTexture9::LockRect failed.");
00133 }
00134 else
00135 {
00136
00137 ulong* dst = (ulong*)rect.pBits;
00138 ulong* src = (ulong*)buffPtr;
00139
00140 for (uint i = 0; i < buffHeight; ++i)
00141 {
00142 for (uint j = 0; j < buffWidth; ++j)
00143 {
00144 dst[j] = src[j];
00145 }
00146
00147 dst += rect.Pitch / sizeof(ulong);
00148 src += buffWidth;
00149 }
00150
00151 d_d3dtexture->UnlockRect(0);
00152 }
00153
00154 }
00155
00156 }
00157
00158
00159
00160
00161
00162 void DirectX9Texture::freeD3DTexture(void)
00163 {
00164 if (d_d3dtexture != NULL)
00165 {
00166 d_d3dtexture->Release();
00167 d_d3dtexture = NULL;
00168 }
00169
00170 d_filename.clear();
00171 d_isMemoryTexture = true;
00172 }
00173
00174
00175
00176
00177
00178 void DirectX9Texture::setD3DTextureSize(uint size)
00179 {
00180 freeD3DTexture();
00181
00182 HRESULT hr = D3DXCreateTexture(((DirectX9Renderer*)getRenderer())->getDevice(), size, size, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &d_d3dtexture);
00183
00184 if (FAILED(hr))
00185 {
00186 throw RendererException((utf8*)"Failed to create texture of specified size: D3D Texture creation failed.");
00187 }
00188 else
00189 {
00190 D3DSURFACE_DESC texdesc;
00191 d_d3dtexture->GetLevelDesc(0, &texdesc);
00192
00193
00194 d_width = (ushort)texdesc.Width;
00195 d_height = (ushort)texdesc.Height;
00196 }
00197
00198 }
00199
00200
00201
00202
00203
00204
00205 void DirectX9Texture::preD3DReset(void)
00206 {
00207
00208
00209 if (!d_isMemoryTexture)
00210 {
00211
00212 if (d_d3dtexture != NULL)
00213 {
00214 if (FAILED(d_d3dtexture->Release()))
00215 {
00216 throw RendererException("DirectX9Texture::preD3DReset - failed to release the Direct3DTexture9 object for this texture.");
00217 }
00218
00219 d_d3dtexture = NULL;
00220 }
00221
00222 }
00223
00224 }
00225
00226
00227
00228
00229
00230
00231 void DirectX9Texture::postD3DReset(void)
00232 {
00233
00234
00235 if (!d_isMemoryTexture)
00236 {
00237
00238
00239 String name(d_filename);
00240
00241
00242 loadFromFile(name, d_resourceGroup);
00243 }
00244
00245 }
00246
00247 }