source: OGRE/trunk/ogrenew/Samples/Common/CEGUIRenderer/src/OgreCEGUITexture.cpp @ 692

Revision 692, 8.4 KB checked in by mattausch, 19 years ago (diff)

adding ogre 1.2 and dependencies

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 <CEGUI/CEGUISystem.h>
28#include <CEGUI/CEGUIExceptions.h>
29#include "OgreCEGUITexture.h"
30#include "OgreCEGUIRenderer.h"
31
32#include <OgreTextureManager.h>
33
34// Start of CEGUI namespace section
35namespace CEGUI
36{
37/*************************************************************************
38        Static data definition / initialisation
39*************************************************************************/
40uint32 OgreCEGUITexture::d_texturenumber                = 0;
41
42
43/*************************************************************************
44        Constructor
45*************************************************************************/
46OgreCEGUITexture::OgreCEGUITexture(Renderer* owner) :
47        Texture(owner)
48{
49        d_ogre_texture.setNull();
50        d_isLinked = false;
51}
52
53
54/*************************************************************************
55        Destructor
56*************************************************************************/
57OgreCEGUITexture::~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*************************************************************************/
67void 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
132void _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
144void OgreCEGUITexture::loadFromMemory(const void* buffPtr, uint buffWidth, uint buffHeight)
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        // try to create a Ogre::Texture from the input data
167        d_ogre_texture = TextureManager::getSingleton().loadRawData(getUniqueName(), "General", odc, buffWidth, buffHeight, PF_A8R8G8B8, TEX_TYPE_2D, 0, 1.0f);
168
169        // if we got a pointer cache some details
170        if (!d_ogre_texture.isNull())
171        {
172                d_width         = d_ogre_texture->getWidth();
173                d_height        = d_ogre_texture->getHeight();
174        }
175        // no texture from memory so throw.
176        else
177        {
178                throw RendererException((utf8*)"Failed to create Texture object from memory:  Ogre returned a NULL Ogre::Texture pointer.");
179        }
180
181}
182
183
184/*************************************************************************
185        set the size of the internal Ogre texture.  Previous Ogre texture
186        is lost.       
187*************************************************************************/
188void OgreCEGUITexture::setOgreTextureSize(uint size)
189{
190        using namespace Ogre;
191
192        // unload any current Ogre::Texture
193        freeOgreTexture();
194
195        // Try to create an empty texture of the given size
196        d_ogre_texture = TextureManager::getSingleton().createManual(getUniqueName(), "General", TEX_TYPE_2D, size, size, 0, PF_A8R8G8B8, TU_DEFAULT);
197
198        // if we got a pointer cache some details
199        if (!d_ogre_texture.isNull())
200        {
201                d_width         = d_ogre_texture->getWidth();
202                d_height        = d_ogre_texture->getHeight();
203        }
204        // no texture so throw.
205        else
206        {
207                throw RendererException((utf8*)"Failed to create texture of specified size: Ogre::Texture creation failed.");
208        }
209
210}
211
212
213/*************************************************************************
214        safely free Ogre::Texture texture (can be called multiple times with
215        no ill effect)
216*************************************************************************/
217void OgreCEGUITexture::freeOgreTexture(void)
218{
219        if ((!d_ogre_texture.isNull()) && !d_isLinked)
220        {
221                Ogre::TextureManager::getSingleton().remove(d_ogre_texture->getHandle());
222        }
223        d_ogre_texture.setNull();
224}
225
226
227/*************************************************************************
228        return a Ogre::string that contains a unique name.     
229*************************************************************************/
230Ogre::String OgreCEGUITexture::getUniqueName(void)
231{
232        Ogre::String str;
233
234#ifdef CEGUI_USEOLDOGRESTRING
235        str << "_cegui_ogre_" << d_texturenumber;
236#else
237        Ogre::StringUtil::StrStreamType strstream;
238        strstream << "_cegui_ogre_" << d_texturenumber;
239        str = strstream.str();
240#endif
241
242        ++d_texturenumber;
243
244        return str;
245}
246
247
248/*************************************************************************
249        Set the internal Ogre::Texture object.
250*************************************************************************/
251void OgreCEGUITexture::setOgreTexture(Ogre::TexturePtr& texture)
252{
253        freeOgreTexture();
254
255        d_ogre_texture = texture;
256        d_width  = d_ogre_texture->getWidth();
257        d_height = d_ogre_texture->getHeight();
258        d_isLinked = true;
259}
260
261
262} // End of  CEGUI namespace section
263
Note: See TracBrowser for help on using the repository browser.