Main Page   Class Hierarchy   Compound List   File List   Compound Members  

ogltexture.h

00001 #ifndef KR_OPENGL_TEXTURE_INCLUDED
00002 #define KR_OPENGL_TEXTURE_INCLUDED
00003 
00004 #ifdef KYRA_SUPPORT_OPENGL
00005         #include "SDL_opengl.h"
00006         #define KYRA_GLuint GLuint
00007 #else
00008         #define KYRA_GLuint int
00009 #endif
00010 #include "krmath.h"
00011 #include "color.h"
00012 #include "../util/gldynarray.h"
00013 
00014 /*      A wrapper to the actual OpenGL texture.
00015 */
00016 class KrOglTexture
00017 {
00018         friend class KrTexture;
00019 
00020   public:
00021         KrOglTexture(   const KrRGBA*, int width, int height,
00022                                         KrTRect<float>* textCoords,
00023                                         KrRect* pixelCoords );
00024 
00025         ~KrOglTexture();
00026 
00027         void AddRef()           { ++refCount; }
00028         void RemoveRef()        {       --refCount;
00029                                                         if ( refCount == 0 )
00030                                                                 delete this;
00031                                                 }
00032 
00033         KYRA_GLuint     Id()                                    { return textureId; }
00034         bool    Scale()                                 { return scaled; }
00035 
00036 
00037   private:
00038         void SetTexture(        const KrRGBA* pixels,
00039                                                 const KrRect& pixelCoords );
00040 
00041         void SetScaledTexture(  const KrRGBA* pixels, int w, int h );
00042 
00043 
00044         void    FillRow( U8* target, const KrRGBA* source, int width );
00045         void    CreateTexture(  U8* target, int targetW, int targetH, int targetScan,
00046                                                         const KrRGBA* source, int sourceW, int sourceH );
00047 
00048         void    CreateScaledTexture(    U8* target, int targetW, int targetH, int targetScan,
00049                                                                         const KrRGBA* source, int sourceW, int sourceH );
00050 
00051         int             refCount;
00052         KYRA_GLuint  textureId;
00053         bool    canSetTexture;
00054         bool    scaled;
00055 };
00056 
00057 
00058 /*      A texture is presented to the Kyra engine.
00059         It has (opelGL) bounds, an id number, and may or may not be scaled.
00060         Scaled textures occur when a texture that is requested is too large,
00061         either because a given OpenGL system doesn't support it,
00062         or because KrEngine::MaxOglTextureSize() has restricted memory usage.
00063 */
00064 class KrTexture
00065 {
00066   public:
00067         KrTexture(      KrOglTexture* oglTexture,
00068                                 const KrTRect<float>& bounds,
00069                                 const KrRect& pixelBounds );
00070 
00071         ~KrTexture();
00072 
00073         const KrTRect<float>& Bounds()  { return bounds; }
00074         KYRA_GLuint Id()                                                { return oglTexture->Id(); }
00075         bool   Scale()                                  { return oglTexture->Scale(); }
00076 
00077         void SetTexture( const KrRGBA* image, int width, int height );
00078 
00079   private:
00080         KrOglTexture* oglTexture;
00081         KrTRect<float>  bounds;
00082         KrRect                  pixelBounds;
00083 };
00084 
00085 
00086 /*      Singleton class to manage textures.
00087         Conceptually, the TextureManager returns a pointer to newly created
00088         Texture. The Texture is a wrapper to an OglTexture, which describes
00089         the actually memory held by openGL. Multiple Textures could wrap the
00090         same OglTexture.
00091 
00092         At this point, there is a one to one relationship from the Texture
00093         to the OglTexture, so some functionality is unused.
00094 */
00095 class KrTextureManager
00096 {
00097   public:
00098         ~KrTextureManager();
00099         static KrTextureManager* Instance();
00100 
00101         // Call this to allocate and return a new texture, set up with 'data'
00102         KrTexture* CreateTexture( const KrRGBA* data, int width, int height );
00103 
00104         static int TextureIndex()               { return ( instance ) ? instance->oglTextureCount : 0; }
00105 
00106         // When the ogl texture deletes, it warns the manager. Called by destructor.
00107         void OglTextureDeleting( KrOglTexture* goingAway );
00108 
00109   private:
00110         KrTextureManager() : oglTextureCount( 0 )               {}
00111 
00112         static KrTextureManager* instance;
00113         int oglTextureCount;
00114 
00115 //      GlDynArray< KrOglTexture* > texArray;
00116 };
00117 
00118 #endif

Generated on Mon Sep 15 12:01:11 2003 for Kyra by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001