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