| [1638] | 1 | /************************************************************************ | 
|---|
| [2602] | 2 |         filename:       OgreCEGUITexture.cpp | 
|---|
 | 3 |         created:        11/5/2004 | 
|---|
 | 4 |         author:         Paul D Turner | 
|---|
 | 5 |          | 
|---|
 | 6 |         purpose:        Implementation of Texture using Ogre engine | 
|---|
| [1638] | 7 | *************************************************************************/ | 
|---|
 | 8 | /************************************************************************* | 
|---|
 | 9 |     Crazy Eddie's GUI System (http://www.cegui.org.uk) | 
|---|
 | 10 |     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk) | 
|---|
 | 11 |  | 
|---|
 | 12 |     This library is free software; you can redistribute it and/or | 
|---|
 | 13 |     modify it under the terms of the GNU Lesser General Public | 
|---|
 | 14 |     License as published by the Free Software Foundation; either | 
|---|
 | 15 |     version 2.1 of the License, or (at your option) any later version. | 
|---|
 | 16 |  | 
|---|
 | 17 |     This library is distributed in the hope that it will be useful, | 
|---|
 | 18 |     but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
 | 19 |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|---|
 | 20 |     Lesser General Public License for more details. | 
|---|
 | 21 |  | 
|---|
 | 22 |     You should have received a copy of the GNU Lesser General Public | 
|---|
 | 23 |     License along with this library; if not, write to the Free Software | 
|---|
 | 24 |     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA | 
|---|
 | 25 | *************************************************************************/ | 
|---|
 | 26 |  | 
|---|
 | 27 | #include <CEGUISystem.h> | 
|---|
 | 28 | #include <CEGUIExceptions.h> | 
|---|
 | 29 | #include "OgreCEGUITexture.h" | 
|---|
 | 30 | #include "OgreCEGUIRenderer.h" | 
|---|
 | 31 |  | 
|---|
 | 32 | #include <OgreTextureManager.h> | 
|---|
 | 33 |  | 
|---|
 | 34 | // Start of CEGUI namespace section | 
|---|
 | 35 | namespace CEGUI | 
|---|
 | 36 | { | 
|---|
 | 37 | /************************************************************************* | 
|---|
| [2602] | 38 |         Static data definition / initialisation | 
|---|
| [1638] | 39 | *************************************************************************/ | 
|---|
 | 40 | uint32 OgreCEGUITexture::d_texturenumber                = 0; | 
|---|
 | 41 |  | 
|---|
 | 42 |  | 
|---|
 | 43 | /************************************************************************* | 
|---|
| [2602] | 44 |         Constructor | 
|---|
| [1638] | 45 | *************************************************************************/ | 
|---|
 | 46 | OgreCEGUITexture::OgreCEGUITexture(Renderer* owner) : | 
|---|
| [2602] | 47 |         Texture(owner) | 
|---|
| [1638] | 48 | { | 
|---|
| [2602] | 49 |         d_ogre_texture.setNull(); | 
|---|
 | 50 |         d_isLinked = false; | 
|---|
| [1638] | 51 | } | 
|---|
 | 52 |  | 
|---|
 | 53 |  | 
|---|
 | 54 | /************************************************************************* | 
|---|
| [2602] | 55 |         Destructor | 
|---|
| [1638] | 56 | *************************************************************************/ | 
|---|
 | 57 | OgreCEGUITexture::~OgreCEGUITexture(void) | 
|---|
 | 58 | { | 
|---|
| [2602] | 59 |         freeOgreTexture(); | 
|---|
| [1638] | 60 | } | 
|---|
 | 61 |  | 
|---|
 | 62 |  | 
|---|
 | 63 | /************************************************************************* | 
|---|
| [2602] | 64 |         Loads the specified image file into the texture.  The texture is | 
|---|
 | 65 |         resized as required to hold the image.   | 
|---|
| [1638] | 66 | *************************************************************************/ | 
|---|
 | 67 | void OgreCEGUITexture::loadFromFile(const String& filename, const String& resourceGroup) | 
|---|
 | 68 | { | 
|---|
| [2602] | 69 |         using namespace Ogre; | 
|---|
| [1638] | 70 |  | 
|---|
| [2602] | 71 |         // unload old ogre texture | 
|---|
 | 72 |         freeOgreTexture(); | 
|---|
| [1638] | 73 |  | 
|---|
| [2602] | 74 |         // create / load a new ogre texture from the specified image | 
|---|
 | 75 |         try | 
|---|
 | 76 |         { | 
|---|
 | 77 |                 TextureManager& textureManager = TextureManager::getSingleton(); | 
|---|
| [1638] | 78 |  | 
|---|
| [2602] | 79 |                 // see if texture already exists | 
|---|
 | 80 |                 Ogre::TexturePtr ogreTexture = (Ogre::TexturePtr)textureManager.getByName(filename.c_str()); | 
|---|
| [1638] | 81 |  | 
|---|
| [2602] | 82 |                 if (!ogreTexture.isNull()) | 
|---|
 | 83 |                 { | 
|---|
 | 84 |                         // texture already exists, so create a 'linked' texture (ensures texture is not destroyed twice) | 
|---|
 | 85 |                         d_ogre_texture = ogreTexture; | 
|---|
 | 86 |                         d_isLinked = true; | 
|---|
 | 87 |                 } | 
|---|
 | 88 |                 // texture does not already exist, so load it in | 
|---|
 | 89 |                 else | 
|---|
 | 90 |                 { | 
|---|
| [1638] | 91 |             String orpGroup; | 
|---|
 | 92 |             if (resourceGroup.empty()) | 
|---|
 | 93 |             { | 
|---|
 | 94 |                 const String& defGrp = CEGUI::System::getSingleton().getResourceProvider()->getDefaultResourceGroup(); | 
|---|
 | 95 |                 orpGroup = defGrp.empty() ? Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME.c_str() : defGrp; | 
|---|
 | 96 |             } | 
|---|
 | 97 |             else | 
|---|
 | 98 |             { | 
|---|
 | 99 |                 orpGroup = resourceGroup; | 
|---|
 | 100 |             } | 
|---|
 | 101 |  | 
|---|
| [2602] | 102 |                         d_ogre_texture = TextureManager::getSingleton().load(filename.c_str(), orpGroup.c_str(), TEX_TYPE_2D, 0, 1.0f); | 
|---|
 | 103 |                         d_isLinked = false; | 
|---|
 | 104 |                 } | 
|---|
| [1638] | 105 |  | 
|---|
| [2602] | 106 |         } | 
|---|
 | 107 |         catch(Ogre::Exception e) | 
|---|
 | 108 |         { | 
|---|
 | 109 |                 throw RendererException((utf8*)"Failed to create Texture object from file '" + filename + "'. Additional Information:\n" + e.getFullDescription().c_str()); | 
|---|
 | 110 |         } | 
|---|
| [1638] | 111 |  | 
|---|
| [2602] | 112 |         // if we got a pointer cache some details | 
|---|
 | 113 |         if (!d_ogre_texture.isNull()) | 
|---|
 | 114 |         { | 
|---|
 | 115 |                 d_width         = d_ogre_texture->getWidth(); | 
|---|
 | 116 |                 d_height        = d_ogre_texture->getHeight(); | 
|---|
 | 117 |         } | 
|---|
 | 118 |         // no texture from image so throw. | 
|---|
 | 119 |         else | 
|---|
 | 120 |         { | 
|---|
 | 121 |                 throw RendererException((utf8*)"Failed to create Texture object from file '" + filename + "'.  Ogre returned a NULL pointer."); | 
|---|
 | 122 |         } | 
|---|
| [1638] | 123 |  | 
|---|
 | 124 | } | 
|---|
 | 125 |  | 
|---|
 | 126 |  | 
|---|
 | 127 | /************************************************************************* | 
|---|
| [2602] | 128 |         Loads (copies) an image in memory into the texture.  The texture is | 
|---|
 | 129 |         resized as required to hold the image.   | 
|---|
| [1638] | 130 | *************************************************************************/ | 
|---|
 | 131 |  | 
|---|
 | 132 | void _byteSwap(unsigned char* b, int n) | 
|---|
 | 133 | { | 
|---|
 | 134 |     register int i = 0; | 
|---|
 | 135 |     register int j = n-1; | 
|---|
 | 136 |     while (i<j) | 
|---|
 | 137 |     { | 
|---|
 | 138 |         std::swap(b[i], b[j]); | 
|---|
 | 139 |         i++, j--; | 
|---|
 | 140 |     } | 
|---|
 | 141 | } | 
|---|
 | 142 | #define byteSwap(x) _byteSwap((unsigned char*) &x,sizeof(x)) | 
|---|
 | 143 |  | 
|---|
 | 144 | void OgreCEGUITexture::loadFromMemory(const void* buffPtr, uint buffWidth, uint buffHeight, PixelFormat pixelFormat) | 
|---|
 | 145 | { | 
|---|
| [2602] | 146 |         using namespace Ogre; | 
|---|
| [1638] | 147 |  | 
|---|
| [2602] | 148 |         // get rid of old texture | 
|---|
 | 149 |         freeOgreTexture(); | 
|---|
| [1638] | 150 |  | 
|---|
| [2602] | 151 |         // wrap input buffer with an Ogre DataChunk | 
|---|
| [1638] | 152 |     uint32 bytesize = ((buffWidth * sizeof(uint32)) * buffHeight); | 
|---|
 | 153 |  | 
|---|
 | 154 | #if OGRE_ENDIAN == OGRE_ENDIAN_BIG | 
|---|
 | 155 |     uint32* swappedBuffer = new uint32[bytesize/4]; | 
|---|
 | 156 |     memcpy(swappedBuffer, buffPtr, bytesize); | 
|---|
 | 157 |  | 
|---|
 | 158 |     for (int i=0; i < bytesize/4; i++) | 
|---|
 | 159 |         byteSwap(swappedBuffer[i]); | 
|---|
 | 160 |  | 
|---|
 | 161 |     DataStreamPtr odc(new MemoryDataStream(static_cast<void*>(swappedBuffer), bytesize, false)); | 
|---|
 | 162 | #else | 
|---|
| [2602] | 163 |         DataStreamPtr odc(new MemoryDataStream(const_cast<void*>(buffPtr), bytesize, false)); | 
|---|
| [1638] | 164 | #endif | 
|---|
 | 165 |  | 
|---|
| [2602] | 166 |         // get pixel type for the target texture - the elements here might look wrong, but is just | 
|---|
 | 167 |         // differences in definition (at the core level, between GL and D3D). | 
|---|
 | 168 |         Ogre::PixelFormat targetFmt = | 
|---|
 | 169 |                 (pixelFormat == PF_RGBA) ? Ogre::PF_A8R8G8B8 : Ogre::PF_R8G8B8; | 
|---|
| [1638] | 170 |  | 
|---|
| [2602] | 171 |         // try to create a Ogre::Texture from the input data | 
|---|
 | 172 |         d_ogre_texture = TextureManager::getSingleton().loadRawData(getUniqueName(), "General", odc, buffWidth, buffHeight, targetFmt , TEX_TYPE_2D, 0, 1.0f); | 
|---|
| [1638] | 173 |  | 
|---|
| [2602] | 174 |         // if we got a pointer cache some details | 
|---|
 | 175 |         if (!d_ogre_texture.isNull()) | 
|---|
 | 176 |         { | 
|---|
 | 177 |                 d_width         = d_ogre_texture->getWidth(); | 
|---|
 | 178 |                 d_height        = d_ogre_texture->getHeight(); | 
|---|
 | 179 |         } | 
|---|
 | 180 |         // no texture from memory so throw. | 
|---|
 | 181 |         else | 
|---|
 | 182 |         { | 
|---|
 | 183 |                 throw RendererException((utf8*)"Failed to create Texture object from memory:  Ogre returned a NULL Ogre::Texture pointer."); | 
|---|
 | 184 |         } | 
|---|
| [1638] | 185 |  | 
|---|
 | 186 | } | 
|---|
 | 187 |  | 
|---|
 | 188 |  | 
|---|
 | 189 | /************************************************************************* | 
|---|
| [2602] | 190 |         set the size of the internal Ogre texture.  Previous Ogre texture | 
|---|
 | 191 |         is lost.         | 
|---|
| [1638] | 192 | *************************************************************************/ | 
|---|
 | 193 | void OgreCEGUITexture::setOgreTextureSize(uint size) | 
|---|
 | 194 | { | 
|---|
| [2602] | 195 |         using namespace Ogre; | 
|---|
| [1638] | 196 |  | 
|---|
| [2602] | 197 |         // unload any current Ogre::Texture | 
|---|
 | 198 |         freeOgreTexture(); | 
|---|
| [1638] | 199 |  | 
|---|
| [2602] | 200 |         // Try to create an empty texture of the given size | 
|---|
 | 201 |         d_ogre_texture = TextureManager::getSingleton().createManual(getUniqueName(), "General", TEX_TYPE_2D, size, size, 0, PF_A8R8G8B8, TU_DEFAULT); | 
|---|
| [1638] | 202 |  | 
|---|
| [2602] | 203 |         // if we got a pointer cache some details | 
|---|
 | 204 |         if (!d_ogre_texture.isNull()) | 
|---|
 | 205 |         { | 
|---|
 | 206 |                 d_width         = d_ogre_texture->getWidth(); | 
|---|
 | 207 |                 d_height        = d_ogre_texture->getHeight(); | 
|---|
 | 208 |         } | 
|---|
 | 209 |         // no texture so throw. | 
|---|
 | 210 |         else | 
|---|
 | 211 |         { | 
|---|
 | 212 |                 throw RendererException((utf8*)"Failed to create texture of specified size: Ogre::Texture creation failed."); | 
|---|
 | 213 |         } | 
|---|
| [1638] | 214 |  | 
|---|
 | 215 | } | 
|---|
 | 216 |  | 
|---|
 | 217 |  | 
|---|
 | 218 | /************************************************************************* | 
|---|
| [2602] | 219 |         safely free Ogre::Texture texture (can be called multiple times with | 
|---|
 | 220 |         no ill effect) | 
|---|
| [1638] | 221 | *************************************************************************/ | 
|---|
 | 222 | void OgreCEGUITexture::freeOgreTexture(void) | 
|---|
 | 223 | { | 
|---|
| [2602] | 224 |         if ((!d_ogre_texture.isNull()) && !d_isLinked) | 
|---|
 | 225 |         { | 
|---|
 | 226 |                 Ogre::TextureManager::getSingleton().remove(d_ogre_texture->getHandle()); | 
|---|
 | 227 |         } | 
|---|
 | 228 |         d_ogre_texture.setNull(); | 
|---|
| [1638] | 229 | } | 
|---|
 | 230 |  | 
|---|
 | 231 |  | 
|---|
 | 232 | /************************************************************************* | 
|---|
| [2602] | 233 |         return a Ogre::string that contains a unique name.       | 
|---|
| [1638] | 234 | *************************************************************************/ | 
|---|
 | 235 | Ogre::String OgreCEGUITexture::getUniqueName(void) | 
|---|
 | 236 | { | 
|---|
| [2602] | 237 |         Ogre::String str; | 
|---|
| [1638] | 238 |  | 
|---|
 | 239 | #ifdef CEGUI_USEOLDOGRESTRING | 
|---|
| [2602] | 240 |         str << "_cegui_ogre_" << d_texturenumber; | 
|---|
| [1638] | 241 | #else | 
|---|
| [2602] | 242 |         Ogre::StringUtil::StrStreamType strstream; | 
|---|
 | 243 |         strstream << "_cegui_ogre_" << d_texturenumber; | 
|---|
 | 244 |         str = strstream.str(); | 
|---|
| [1638] | 245 | #endif | 
|---|
 | 246 |  | 
|---|
| [2602] | 247 |         ++d_texturenumber; | 
|---|
| [1638] | 248 |  | 
|---|
| [2602] | 249 |         return str; | 
|---|
| [1638] | 250 | } | 
|---|
 | 251 |  | 
|---|
 | 252 |  | 
|---|
 | 253 | /************************************************************************* | 
|---|
| [2602] | 254 |         Set the internal Ogre::Texture object. | 
|---|
| [1638] | 255 | *************************************************************************/ | 
|---|
 | 256 | void OgreCEGUITexture::setOgreTexture(Ogre::TexturePtr& texture) | 
|---|
 | 257 | { | 
|---|
| [2602] | 258 |         freeOgreTexture(); | 
|---|
| [1638] | 259 |  | 
|---|
| [2602] | 260 |         d_ogre_texture = texture; | 
|---|
 | 261 |         d_width  = d_ogre_texture->getWidth(); | 
|---|
 | 262 |         d_height = d_ogre_texture->getHeight(); | 
|---|
 | 263 |         d_isLinked = true; | 
|---|
| [1638] | 264 | } | 
|---|
 | 265 |  | 
|---|
 | 266 |  | 
|---|
 | 267 | } // End of  CEGUI namespace section | 
|---|
 | 268 |  | 
|---|