| [1638] | 1 | /************************************************************************ | 
|---|
 | 2 |     filename:   OgreCEGUITexture.cpp | 
|---|
 | 3 |     created:    11/5/2004 | 
|---|
 | 4 |     author:             Paul D Turner | 
|---|
 | 5 |      | 
|---|
 | 6 |     purpose:    Implementation of Texture using Ogre engine | 
|---|
 | 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 | /************************************************************************* | 
|---|
 | 38 |     Static data definition / initialisation | 
|---|
 | 39 | *************************************************************************/ | 
|---|
 | 40 | uint32 OgreCEGUITexture::d_texturenumber                = 0; | 
|---|
 | 41 |  | 
|---|
 | 42 |  | 
|---|
 | 43 | /************************************************************************* | 
|---|
 | 44 |     Constructor | 
|---|
 | 45 | *************************************************************************/ | 
|---|
 | 46 | OgreCEGUITexture::OgreCEGUITexture(Renderer* owner) : | 
|---|
 | 47 |     Texture(owner) | 
|---|
 | 48 | { | 
|---|
 | 49 |     d_ogre_texture.setNull(); | 
|---|
 | 50 |     d_isLinked = false; | 
|---|
 | 51 | } | 
|---|
 | 52 |  | 
|---|
 | 53 |  | 
|---|
 | 54 | /************************************************************************* | 
|---|
 | 55 |     Destructor | 
|---|
 | 56 | *************************************************************************/ | 
|---|
 | 57 | OgreCEGUITexture::~OgreCEGUITexture(void) | 
|---|
 | 58 | { | 
|---|
 | 59 |     freeOgreTexture(); | 
|---|
 | 60 | } | 
|---|
 | 61 |  | 
|---|
 | 62 |  | 
|---|
 | 63 | /************************************************************************* | 
|---|
 | 64 |     Loads the specified image file into the texture.  The texture is | 
|---|
 | 65 |     resized as required to hold the image.       | 
|---|
 | 66 | *************************************************************************/ | 
|---|
 | 67 | void OgreCEGUITexture::loadFromFile(const String& filename, const String& resourceGroup) | 
|---|
 | 68 | { | 
|---|
 | 69 |     using namespace Ogre; | 
|---|
 | 70 |  | 
|---|
 | 71 |     // unload old ogre texture | 
|---|
 | 72 |     freeOgreTexture(); | 
|---|
 | 73 |  | 
|---|
 | 74 |     // create / load a new ogre texture from the specified image | 
|---|
 | 75 |     try | 
|---|
 | 76 |     { | 
|---|
 | 77 |         TextureManager& textureManager = TextureManager::getSingleton(); | 
|---|
 | 78 |  | 
|---|
 | 79 |         // see if texture already exists | 
|---|
 | 80 |         Ogre::TexturePtr ogreTexture = (Ogre::TexturePtr)textureManager.getByName(filename.c_str()); | 
|---|
 | 81 |  | 
|---|
 | 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 |         { | 
|---|
 | 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 |  | 
|---|
 | 102 |             d_ogre_texture = TextureManager::getSingleton().load(filename.c_str(), orpGroup.c_str(), TEX_TYPE_2D, 0, 1.0f); | 
|---|
 | 103 |             d_isLinked = false; | 
|---|
 | 104 |         } | 
|---|
 | 105 |  | 
|---|
 | 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 |     } | 
|---|
 | 111 |  | 
|---|
 | 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 |     } | 
|---|
 | 123 |  | 
|---|
 | 124 | } | 
|---|
 | 125 |  | 
|---|
 | 126 |  | 
|---|
 | 127 | /************************************************************************* | 
|---|
 | 128 |     Loads (copies) an image in memory into the texture.  The texture is | 
|---|
 | 129 |     resized as required to hold the image.       | 
|---|
 | 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 | { | 
|---|
 | 146 |     using namespace Ogre; | 
|---|
 | 147 |  | 
|---|
 | 148 |     // get rid of old texture | 
|---|
 | 149 |     freeOgreTexture(); | 
|---|
 | 150 |  | 
|---|
 | 151 |     // wrap input buffer with an Ogre DataChunk | 
|---|
 | 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 | 
|---|
 | 163 |     DataStreamPtr odc(new MemoryDataStream(const_cast<void*>(buffPtr), bytesize, false)); | 
|---|
 | 164 | #endif | 
|---|
 | 165 |  | 
|---|
 | 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; | 
|---|
 | 170 |  | 
|---|
 | 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); | 
|---|
 | 173 |  | 
|---|
 | 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 |     } | 
|---|
 | 185 |  | 
|---|
 | 186 | } | 
|---|
 | 187 |  | 
|---|
 | 188 |  | 
|---|
 | 189 | /************************************************************************* | 
|---|
 | 190 |     set the size of the internal Ogre texture.  Previous Ogre texture | 
|---|
 | 191 |     is lost.     | 
|---|
 | 192 | *************************************************************************/ | 
|---|
 | 193 | void OgreCEGUITexture::setOgreTextureSize(uint size) | 
|---|
 | 194 | { | 
|---|
 | 195 |     using namespace Ogre; | 
|---|
 | 196 |  | 
|---|
 | 197 |     // unload any current Ogre::Texture | 
|---|
 | 198 |     freeOgreTexture(); | 
|---|
 | 199 |  | 
|---|
 | 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); | 
|---|
 | 202 |  | 
|---|
 | 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 |     } | 
|---|
 | 214 |  | 
|---|
 | 215 | } | 
|---|
 | 216 |  | 
|---|
 | 217 |  | 
|---|
 | 218 | /************************************************************************* | 
|---|
 | 219 |     safely free Ogre::Texture texture (can be called multiple times with | 
|---|
 | 220 |     no ill effect) | 
|---|
 | 221 | *************************************************************************/ | 
|---|
 | 222 | void OgreCEGUITexture::freeOgreTexture(void) | 
|---|
 | 223 | { | 
|---|
 | 224 |     if ((!d_ogre_texture.isNull()) && !d_isLinked) | 
|---|
 | 225 |     { | 
|---|
 | 226 |         Ogre::TextureManager::getSingleton().remove(d_ogre_texture->getHandle()); | 
|---|
 | 227 |     } | 
|---|
 | 228 |     d_ogre_texture.setNull(); | 
|---|
 | 229 | } | 
|---|
 | 230 |  | 
|---|
 | 231 |  | 
|---|
 | 232 | /************************************************************************* | 
|---|
 | 233 |     return a Ogre::string that contains a unique name.   | 
|---|
 | 234 | *************************************************************************/ | 
|---|
 | 235 | Ogre::String OgreCEGUITexture::getUniqueName(void) | 
|---|
 | 236 | { | 
|---|
 | 237 |     Ogre::String str; | 
|---|
 | 238 |  | 
|---|
 | 239 | #ifdef CEGUI_USEOLDOGRESTRING | 
|---|
 | 240 |     str << "_cegui_ogre_" << d_texturenumber; | 
|---|
 | 241 | #else | 
|---|
 | 242 |     Ogre::StringUtil::StrStreamType strstream; | 
|---|
 | 243 |     strstream << "_cegui_ogre_" << d_texturenumber; | 
|---|
 | 244 |     str = strstream.str(); | 
|---|
 | 245 | #endif | 
|---|
 | 246 |  | 
|---|
 | 247 |     ++d_texturenumber; | 
|---|
 | 248 |  | 
|---|
 | 249 |     return str; | 
|---|
 | 250 | } | 
|---|
 | 251 |  | 
|---|
 | 252 |  | 
|---|
 | 253 | /************************************************************************* | 
|---|
 | 254 |     Set the internal Ogre::Texture object. | 
|---|
 | 255 | *************************************************************************/ | 
|---|
 | 256 | void OgreCEGUITexture::setOgreTexture(Ogre::TexturePtr& texture) | 
|---|
 | 257 | { | 
|---|
 | 258 |     freeOgreTexture(); | 
|---|
 | 259 |  | 
|---|
 | 260 |     d_ogre_texture = texture; | 
|---|
 | 261 |     d_width      = d_ogre_texture->getWidth(); | 
|---|
 | 262 |     d_height = d_ogre_texture->getHeight(); | 
|---|
 | 263 |     d_isLinked = true; | 
|---|
 | 264 | } | 
|---|
 | 265 |  | 
|---|
 | 266 |  | 
|---|
 | 267 | } // End of  CEGUI namespace section | 
|---|
 | 268 |  | 
|---|