Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/release2012/src/external/ogreceguirenderer/OgreCEGUITexture.cpp @ 11888

Last change on this file since 11888 was 8367, checked in by rgrieder, 14 years ago

by Adi: Fix for GCC 4.6. The actual problem is in CEGUIString.h, but we cannot really fix that ;)

  • Property svn:eol-style set to native
File size: 8.5 KB
Line 
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// Workaround for GCC 4.6
28#include <cstddef>
29
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
38namespace CEGUI
39{
40/*************************************************************************
41        Static data definition / initialisation
42*************************************************************************/
43uint32 OgreCEGUITexture::d_texturenumber                = 0;
44
45
46/*************************************************************************
47        Constructor
48*************************************************************************/
49OgreCEGUITexture::OgreCEGUITexture(Renderer* owner) :
50        Texture(owner)
51{
52        d_ogre_texture.setNull();
53        d_isLinked = false;
54}
55
56
57/*************************************************************************
58        Destructor
59*************************************************************************/
60OgreCEGUITexture::~OgreCEGUITexture(void)
61{
62        freeOgreTexture();
63}
64
65
66/*************************************************************************
67        Loads the specified image file into the texture.  The texture is
68        resized as required to hold the image. 
69*************************************************************************/
70void OgreCEGUITexture::loadFromFile(const String& filename, const String& resourceGroup)
71{
72        using namespace Ogre;
73
74        // unload old ogre texture
75        freeOgreTexture();
76
77        // create / load a new ogre texture from the specified image
78        try
79        {
80                TextureManager& textureManager = TextureManager::getSingleton();
81
82                // see if texture already exists
83                Ogre::TexturePtr ogreTexture = (Ogre::TexturePtr)textureManager.getByName(filename.c_str());
84
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                {
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
105                        d_ogre_texture = TextureManager::getSingleton().load(filename.c_str(), orpGroup.c_str(), TEX_TYPE_2D, 0, 1.0f);
106                        d_isLinked = false;
107                }
108
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        }
114
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        }
126
127}
128
129
130/*************************************************************************
131        Loads (copies) an image in memory into the texture.  The texture is
132        resized as required to hold the image. 
133*************************************************************************/
134
135void _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
147void OgreCEGUITexture::loadFromMemory(const void* buffPtr, uint buffWidth, uint buffHeight, PixelFormat pixelFormat)
148{
149        using namespace Ogre;
150
151        // get rid of old texture
152        freeOgreTexture();
153
154        // wrap input buffer with an Ogre DataChunk
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
166        DataStreamPtr odc(new MemoryDataStream(const_cast<void*>(buffPtr), bytesize, false));
167#endif
168
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;
173
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);
176
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        }
188
189}
190
191
192/*************************************************************************
193        set the size of the internal Ogre texture.  Previous Ogre texture
194        is lost.       
195*************************************************************************/
196void OgreCEGUITexture::setOgreTextureSize(uint size)
197{
198        using namespace Ogre;
199
200        // unload any current Ogre::Texture
201        freeOgreTexture();
202
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);
205
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        }
217
218}
219
220
221/*************************************************************************
222        safely free Ogre::Texture texture (can be called multiple times with
223        no ill effect)
224*************************************************************************/
225void OgreCEGUITexture::freeOgreTexture(void)
226{
227        if ((!d_ogre_texture.isNull()) && !d_isLinked)
228        {
229                Ogre::TextureManager::getSingleton().remove(d_ogre_texture->getHandle());
230        }
231        d_ogre_texture.setNull();
232}
233
234
235/*************************************************************************
236        return a Ogre::string that contains a unique name.     
237*************************************************************************/
238Ogre::String OgreCEGUITexture::getUniqueName(void)
239{
240        Ogre::String str;
241
242#ifdef CEGUI_USEOLDOGRESTRING
243        str << "_cegui_ogre_" << d_texturenumber;
244#else
245        Ogre::StringUtil::StrStreamType strstream;
246        strstream << "_cegui_ogre_" << d_texturenumber;
247        str = strstream.str();
248#endif
249
250        ++d_texturenumber;
251
252        return str;
253}
254
255
256/*************************************************************************
257        Set the internal Ogre::Texture object.
258*************************************************************************/
259void OgreCEGUITexture::setOgreTexture(Ogre::TexturePtr& texture)
260{
261        freeOgreTexture();
262
263        d_ogre_texture = texture;
264        d_width  = d_ogre_texture->getWidth();
265        d_height = d_ogre_texture->getHeight();
266        d_isLinked = true;
267}
268
269
270} // End of  CEGUI namespace section
271
Note: See TracBrowser for help on using the repository browser.