Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/questsystem3/src/orxonox/gui/OgreCEGUITexture.cpp @ 2383

Last change on this file since 2383 was 2087, checked in by landauf, 16 years ago

merged objecthierarchy branch back to trunk

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