- Timestamp:
- Jan 29, 2009, 7:12:27 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/buildsystem2/src/ogreceguirenderer/OgreCEGUITexture.cpp
r2569 r2602 1 1 /************************************************************************ 2 3 4 5 6 2 filename: OgreCEGUITexture.cpp 3 created: 11/5/2004 4 author: Paul D Turner 5 6 purpose: Implementation of Texture using Ogre engine 7 7 *************************************************************************/ 8 8 /************************************************************************* … … 36 36 { 37 37 /************************************************************************* 38 38 Static data definition / initialisation 39 39 *************************************************************************/ 40 40 uint32 OgreCEGUITexture::d_texturenumber = 0; … … 42 42 43 43 /************************************************************************* 44 44 Constructor 45 45 *************************************************************************/ 46 46 OgreCEGUITexture::OgreCEGUITexture(Renderer* owner) : 47 48 { 49 50 51 } 52 53 54 /************************************************************************* 55 47 Texture(owner) 48 { 49 d_ogre_texture.setNull(); 50 d_isLinked = false; 51 } 52 53 54 /************************************************************************* 55 Destructor 56 56 *************************************************************************/ 57 57 OgreCEGUITexture::~OgreCEGUITexture(void) 58 58 { 59 60 } 61 62 63 /************************************************************************* 64 65 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 66 *************************************************************************/ 67 67 void OgreCEGUITexture::loadFromFile(const String& filename, const String& resourceGroup) 68 68 { 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 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 91 String orpGroup; 92 92 if (resourceGroup.empty()) … … 100 100 } 101 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 } 125 126 127 /************************************************************************* 128 129 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 130 *************************************************************************/ 131 131 … … 144 144 void OgreCEGUITexture::loadFromMemory(const void* buffPtr, uint buffWidth, uint buffHeight, PixelFormat pixelFormat) 145 145 { 146 147 148 149 150 151 146 using namespace Ogre; 147 148 // get rid of old texture 149 freeOgreTexture(); 150 151 // wrap input buffer with an Ogre DataChunk 152 152 uint32 bytesize = ((buffWidth * sizeof(uint32)) * buffHeight); 153 153 … … 161 161 DataStreamPtr odc(new MemoryDataStream(static_cast<void*>(swappedBuffer), bytesize, false)); 162 162 #else 163 163 DataStreamPtr odc(new MemoryDataStream(const_cast<void*>(buffPtr), bytesize, false)); 164 164 #endif 165 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 } 187 188 189 /************************************************************************* 190 191 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 192 *************************************************************************/ 193 193 void OgreCEGUITexture::setOgreTextureSize(uint size) 194 194 { 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 } 216 217 218 /************************************************************************* 219 220 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 221 *************************************************************************/ 222 222 void OgreCEGUITexture::freeOgreTexture(void) 223 223 { 224 225 226 227 228 229 } 230 231 232 /************************************************************************* 233 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 234 *************************************************************************/ 235 235 Ogre::String OgreCEGUITexture::getUniqueName(void) 236 236 { 237 237 Ogre::String str; 238 238 239 239 #ifdef CEGUI_USEOLDOGRESTRING 240 240 str << "_cegui_ogre_" << d_texturenumber; 241 241 #else 242 243 244 242 Ogre::StringUtil::StrStreamType strstream; 243 strstream << "_cegui_ogre_" << d_texturenumber; 244 str = strstream.str(); 245 245 #endif 246 246 247 248 249 250 } 251 252 253 /************************************************************************* 254 247 ++d_texturenumber; 248 249 return str; 250 } 251 252 253 /************************************************************************* 254 Set the internal Ogre::Texture object. 255 255 *************************************************************************/ 256 256 void OgreCEGUITexture::setOgreTexture(Ogre::TexturePtr& texture) 257 257 { 258 259 260 261 262 263 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 264 } 265 265
Note: See TracChangeset
for help on using the changeset viewer.