Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jan 29, 2009, 7:12:27 PM (16 years ago)
Author:
rgrieder
Message:
  • Added unified diff file with our changes for OgreCEGUIRenderer
  • Minimised our changes
File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/buildsystem2/src/ogreceguirenderer/OgreCEGUITexture.cpp

    r2569 r2602  
    11/************************************************************************
    2     filename:   OgreCEGUITexture.cpp
    3     created:    11/5/2004
    4     author:             Paul D Turner
    5    
    6     purpose:    Implementation of Texture using Ogre engine
     2        filename:       OgreCEGUITexture.cpp
     3        created:        11/5/2004
     4        author:         Paul D Turner
     5       
     6        purpose:        Implementation of Texture using Ogre engine
    77*************************************************************************/
    88/*************************************************************************
     
    3636{
    3737/*************************************************************************
    38     Static data definition / initialisation
     38        Static data definition / initialisation
    3939*************************************************************************/
    4040uint32 OgreCEGUITexture::d_texturenumber                = 0;
     
    4242
    4343/*************************************************************************
    44     Constructor
     44        Constructor
    4545*************************************************************************/
    4646OgreCEGUITexture::OgreCEGUITexture(Renderer* owner) :
    47     Texture(owner)
    48 {
    49     d_ogre_texture.setNull();
    50     d_isLinked = false;
    51 }
    52 
    53 
    54 /*************************************************************************
    55     Destructor
     47        Texture(owner)
     48{
     49        d_ogre_texture.setNull();
     50        d_isLinked = false;
     51}
     52
     53
     54/*************************************************************************
     55        Destructor
    5656*************************************************************************/
    5757OgreCEGUITexture::~OgreCEGUITexture(void)
    5858{
    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.     
     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. 
    6666*************************************************************************/
    6767void OgreCEGUITexture::loadFromFile(const String& filename, const String& resourceGroup)
    6868{
    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         {
     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                {
    9191            String orpGroup;
    9292            if (resourceGroup.empty())
     
    100100            }
    101101
    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.     
     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. 
    130130*************************************************************************/
    131131
     
    144144void OgreCEGUITexture::loadFromMemory(const void* buffPtr, uint buffWidth, uint buffHeight, PixelFormat pixelFormat)
    145145{
    146     using namespace Ogre;
    147 
    148     // get rid of old texture
    149     freeOgreTexture();
    150 
    151     // wrap input buffer with an Ogre DataChunk
     146        using namespace Ogre;
     147
     148        // get rid of old texture
     149        freeOgreTexture();
     150
     151        // wrap input buffer with an Ogre DataChunk
    152152    uint32 bytesize = ((buffWidth * sizeof(uint32)) * buffHeight);
    153153
     
    161161    DataStreamPtr odc(new MemoryDataStream(static_cast<void*>(swappedBuffer), bytesize, false));
    162162#else
    163     DataStreamPtr odc(new MemoryDataStream(const_cast<void*>(buffPtr), bytesize, false));
     163        DataStreamPtr odc(new MemoryDataStream(const_cast<void*>(buffPtr), bytesize, false));
    164164#endif
    165165
    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.   
     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.       
    192192*************************************************************************/
    193193void OgreCEGUITexture::setOgreTextureSize(uint size)
    194194{
    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)
     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)
    221221*************************************************************************/
    222222void OgreCEGUITexture::freeOgreTexture(void)
    223223{
    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. 
     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.     
    234234*************************************************************************/
    235235Ogre::String OgreCEGUITexture::getUniqueName(void)
    236236{
    237     Ogre::String str;
     237        Ogre::String str;
    238238
    239239#ifdef CEGUI_USEOLDOGRESTRING
    240     str << "_cegui_ogre_" << d_texturenumber;
     240        str << "_cegui_ogre_" << d_texturenumber;
    241241#else
    242     Ogre::StringUtil::StrStreamType strstream;
    243     strstream << "_cegui_ogre_" << d_texturenumber;
    244     str = strstream.str();
     242        Ogre::StringUtil::StrStreamType strstream;
     243        strstream << "_cegui_ogre_" << d_texturenumber;
     244        str = strstream.str();
    245245#endif
    246246
    247     ++d_texturenumber;
    248 
    249     return str;
    250 }
    251 
    252 
    253 /*************************************************************************
    254     Set the internal Ogre::Texture object.
     247        ++d_texturenumber;
     248
     249        return str;
     250}
     251
     252
     253/*************************************************************************
     254        Set the internal Ogre::Texture object.
    255255*************************************************************************/
    256256void OgreCEGUITexture::setOgreTexture(Ogre::TexturePtr& texture)
    257257{
    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;
     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;
    264264}
    265265
Note: See TracChangeset for help on using the changeset viewer.