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
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
00059
00060
00061
00062
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
00087
00088
00089
00090
00091
00092
00093
00094
00095 class KrTextureManager
00096 {
00097 public:
00098 ~KrTextureManager();
00099 static KrTextureManager* Instance();
00100
00101
00102 KrTexture* CreateTexture( const KrRGBA* data, int width, int height );
00103
00104 static int TextureIndex() { return ( instance ) ? instance->oglTextureCount : 0; }
00105
00106
00107 void OglTextureDeleting( KrOglTexture* goingAway );
00108
00109 private:
00110 KrTextureManager() : oglTextureCount( 0 ) {}
00111
00112 static KrTextureManager* instance;
00113 int oglTextureCount;
00114
00115
00116 };
00117
00118 #endif