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