From 4fde43181e0c726a1e0de1e26ffe1983774d1ed0 Mon Sep 17 00:00:00 2001 From: pixel Date: Sun, 8 Dec 2002 20:00:50 +0000 Subject: Commit of the day --- include/gltexture.h | 12 ++++++-- lib/glfont.cc | 38 ++++++++++++++++++++++-- lib/gltexture.cc | 84 ++++++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 115 insertions(+), 19 deletions(-) diff --git a/include/gltexture.h b/include/gltexture.h index c18e6ec..7305eb5 100644 --- a/include/gltexture.h +++ b/include/gltexture.h @@ -10,16 +10,22 @@ namespace mogltk { public: texture(int = 256, int = 256, bool = false) throw (GeneralException); virtual ~texture(); - SDL_Surface * GetSurface() throw (GeneralException); - void Generate() throw (GeneralException); - void Bind(bool = true) throw (GeneralException); + SDL_Surface * GetSurface(); + void Generate(); + void Bind(bool = true); GLuint GetWidth(); GLuint GetHeight(); static void Unbind(void); private: GLuint width, height, tex; + bool texture_allocated; SDL_Surface * surface; bool planar; +#ifdef TRACE_TEXTURES + static texture * header; + static texture * footer; + texture * next, * prev; +#endif }; }; diff --git a/lib/glfont.cc b/lib/glfont.cc index 82058c8..833d18d 100644 --- a/lib/glfont.cc +++ b/lib/glfont.cc @@ -4,13 +4,45 @@ Uint8 prescale2[4] = { 0, 85, 170, 255 }, prescale3[8] = { 0, 36, 72, 109, 145, 182, 218, 255 }; +/* + +font file format +================ + +off|siz|description +---+---+------------------------------- + 0 | 2 | Number of entries + 2 | 1 | Flags + 3 | 1 | maxX (maximum width) + 4 | 1 | maxY (maximum height) + 5 | X | char entries +5+X| Y | char map + +Flags: +----- + +0000000R + +R = RGBA (=1) or Alpha (=0) + +ABGR in 1232 format: +ABBGGGRR + +variables comments +================== + +nbcU = number of chars on X by texture +nbcV = number of chars on Y by texture +nbcT = number of char by texture +nbT = number of textures + +*/ + mogltk::font::font(const String & file) { Input ffont(file); int i; -#ifdef HAVE_ZLIB ffont.SetZ(); -#endif ffont.read(&nbentries, 2); ffont.read(&flags, 1); @@ -44,7 +76,7 @@ mogltk::font::font(const String & file) { for (int u = 0; u < maxX; u++) { Uint8 f; ffont.read(&f, 1); - if (flags) { + if (flags & 1) { Uint8 r, g, b, a; r = f & 3; g = (f >> 2) & 7; diff --git a/lib/gltexture.cc b/lib/gltexture.cc index 61ba71c..b96184f 100644 --- a/lib/gltexture.cc +++ b/lib/gltexture.cc @@ -4,7 +4,13 @@ #include #include "gltexture.h" -mogltk::texture::texture(int w, int h, bool plane) throw (GeneralException) : width(w), height(h), planar(plane) { +#ifdef TRACE_TEXTURES +mogltk::texture * mogltk::texture::header = 0; +mogltk::texture * mogltk::texture::footer = 0; +#endif + +mogltk::texture::texture(int w, int h, bool plane) throw (GeneralException) : width(w), height(h), + texture_allocated(false), planar(plane) { if ((BITCOUNT(w) != 1) || (BITCOUNT(h) != 1)) throw GeneralException("Size of the texture not a power of 2!"); @@ -23,26 +29,59 @@ mogltk::texture::texture(int w, int h, bool plane) throw (GeneralException) : wi ))) { throw GeneralException("Can't create RGB Surface"); } + +#ifdef TRACE_TEXTURES + next = 0; + prev = footer; + footer = this; + if (!header) { + header = this; + } + if (prev) { + prev->next = this; + } +#endif } mogltk::texture::~texture() { if (surface) { SDL_FreeSurface(surface); - } else { + } + + if (texture_allocated) { glDeleteTextures(1, &tex); } + +#ifdef TRACE_TEXTURES + if (prev) { + prev->next = next; + } + + if (next) { + next->prev = prev; + } + + if (this == footer) { + footer = prev; + } + + if (this == header) { + header = next; + } +#endif } -SDL_Surface * mogltk::texture::GetSurface() throw (GeneralException) { - if (!surface) - throw GeneralException("Texture already generated"); +SDL_Surface * mogltk::texture::GetSurface() { return surface; } -void mogltk::texture::Generate() throw (GeneralException) { - if (!surface) - throw GeneralException("Texture already generated"); +void mogltk::texture::Generate() { + if (texture_allocated) { + glDeleteTextures(1, &tex); + } + glGenTextures(1, &tex); + if (planar) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); @@ -56,13 +95,12 @@ void mogltk::texture::Generate() throw (GeneralException) { // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels); } - SDL_FreeSurface(surface); - surface = 0; + texture_allocated = true; } -void mogltk::texture::Bind(bool expand) throw (GeneralException) { - if (surface) - throw GeneralException("Texture is not yet generated"); +void mogltk::texture::Bind(bool expand) { + if (!texture_allocated) + Generate(); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, tex); if (expand) { @@ -71,6 +109,26 @@ void mogltk::texture::Bind(bool expand) throw (GeneralException) { glScaled(1 / (double) width, 1 / (double) height, 1); glMatrixMode(GL_MODELVIEW); } + +#ifdef TRACE_TEXTURES + if (header == this) + return; + + if (prev) { + prev->next = next; + } + + if (next) { + next->prev = prev; + } + + if (footer = this) { + footer = prev; + } + + header->prev = this; + header = this; +#endif } GLuint mogltk::texture::GetWidth() { -- cgit v1.2.3