2011-10-25 05:03:10 +00:00
//
// KRTextureManager.cpp
2012-03-15 20:09:01 +00:00
// KREngine
2011-10-25 05:03:10 +00:00
//
2012-03-15 20:09:01 +00:00
// Copyright 2012 Kearwood Gilbert. All rights reserved.
2011-10-25 06:16:47 +00:00
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are those of the
// authors and should not be interpreted as representing official policies, either expressed
// or implied, of Kearwood Gilbert.
2011-10-25 05:03:10 +00:00
//
# include "KRTextureManager.h"
2012-09-11 06:45:02 +00:00
# include "KRContext.h"
2011-10-25 05:03:10 +00:00
# include <string.h>
2012-08-17 01:04:49 +00:00
KRTextureManager : : KRTextureManager ( KRContext & context ) : KRContextObject ( context ) {
2012-09-11 04:32:04 +00:00
m_textureMemUsed = 0 ;
2012-09-11 03:06:35 +00:00
for ( int iTexture = 0 ; iTexture < KRENGINE_MAX_TEXTURE_UNITS ; iTexture + + ) {
m_activeTextures [ iTexture ] = NULL ;
}
2011-10-25 05:03:10 +00:00
}
KRTextureManager : : ~ KRTextureManager ( ) {
for ( map < std : : string , KRTexture * > : : iterator itr = m_textures . begin ( ) ; itr ! = m_textures . end ( ) ; + + itr ) {
delete ( * itr ) . second ;
}
}
2012-04-12 00:43:53 +00:00
# if TARGET_OS_IPHONE
2012-09-11 03:06:35 +00:00
KRTexture * KRTextureManager : : loadTexture ( const char * szName , KRDataBlock * data ) {
KRTexture * pTexture = new KRTexture ( data , this ) ;
2011-10-25 05:03:10 +00:00
std : : string lowerName = szName ;
std : : transform ( lowerName . begin ( ) , lowerName . end ( ) ,
lowerName . begin ( ) , : : tolower ) ;
m_textures [ lowerName ] = pTexture ;
return pTexture ;
}
2012-04-12 00:43:53 +00:00
# endif
2011-10-25 05:03:10 +00:00
KRTexture * KRTextureManager : : getTexture ( const char * szName ) {
std : : string lowerName = szName ;
std : : transform ( lowerName . begin ( ) , lowerName . end ( ) ,
lowerName . begin ( ) , : : tolower ) ;
map < std : : string , KRTexture * > : : iterator itr = m_textures . find ( lowerName ) ;
if ( itr = = m_textures . end ( ) ) {
// Not found
return NULL ;
} else {
return ( * itr ) . second ;
}
2012-09-11 03:06:35 +00:00
}
void KRTextureManager : : selectTexture ( int iTextureUnit , KRTexture * pTexture ) {
if ( m_activeTextures [ iTextureUnit ] ! = pTexture ) {
glActiveTexture ( GL_TEXTURE0 + iTextureUnit ) ;
if ( pTexture ! = NULL ) {
m_textureCache . erase ( pTexture ) ; // Ensure that the texture will not be deleted while it is bound to a texture unit, and return it to the top of the texture cache when it is released
2012-09-11 04:32:04 +00:00
glBindTexture ( GL_TEXTURE_2D , pTexture - > getHandle ( m_textureMemUsed ) ) ;
2012-09-11 03:06:35 +00:00
// TODO - These texture parameters should be assigned by the material or texture parameters
glTexParameterf ( GL_TEXTURE_2D , GL_TEXTURE_MAX_ANISOTROPY_EXT , 1.0f ) ;
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_WRAP_S , GL_REPEAT ) ;
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_WRAP_T , GL_REPEAT ) ;
} else {
glBindTexture ( GL_TEXTURE_2D , 0 ) ;
}
if ( m_activeTextures [ iTextureUnit ] ! = NULL ) {
KRTexture * unloadedTexture = m_activeTextures [ iTextureUnit ] ;
2012-09-11 04:32:04 +00:00
m_activeTextures [ iTextureUnit ] = NULL ;
2012-09-11 03:06:35 +00:00
bool bActive = false ;
for ( int iTexture = 0 ; iTexture < KRENGINE_MAX_TEXTURE_UNITS ; iTexture + + ) {
if ( m_activeTextures [ iTexture ] = = unloadedTexture ) {
bActive = true ;
}
}
if ( ! bActive ) {
// Only return a texture to the cache when the last texture unit referencing it is re-assigned to a different texture
if ( m_textureCache . find ( unloadedTexture ) = = m_textureCache . end ( ) ) {
m_textureCache . insert ( unloadedTexture ) ;
2012-09-11 04:32:04 +00:00
while ( m_textureCache . size ( ) > KRENGINE_MAX_TEXTURE_HANDLES | | m_textureMemUsed > KRENGINE_MAX_TEXTURE_MEM ) {
2012-09-11 03:06:35 +00:00
// Keep texture size within limits
KRTexture * droppedTexture = ( * m_textureCache . begin ( ) ) ;
2012-09-11 04:32:04 +00:00
droppedTexture - > releaseHandle ( m_textureMemUsed ) ;
2012-09-11 03:06:35 +00:00
m_textureCache . erase ( droppedTexture ) ;
fprintf ( stderr , " Texture Swapping... \n " ) ;
}
}
}
}
m_activeTextures [ iTextureUnit ] = pTexture ;
}
}
2012-09-11 04:32:04 +00:00
long KRTextureManager : : getMemUsed ( ) {
return m_textureMemUsed ;
}