From be0486797260377246c1ea1229cca27c19c64ad2 Mon Sep 17 00:00:00 2001 From: pixel Date: Wed, 26 Mar 2003 17:19:23 +0000 Subject: bleh --- lib/Makefile.am | 2 +- lib/font.cc | 6 +- lib/glfont.cc | 6 +- lib/glshape.cc | 13 ++-- lib/glsprite.cc | 146 +++++++++++++++++++++++++++++++++++++ lib/gltexture.cc | 214 +++++-------------------------------------------------- lib/texture.cc | 194 +++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 374 insertions(+), 207 deletions(-) create mode 100644 lib/glsprite.cc create mode 100644 lib/texture.cc (limited to 'lib') diff --git a/lib/Makefile.am b/lib/Makefile.am index bbb603c..c1bc885 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -6,4 +6,4 @@ INCLUDES = -I.. -I../include -I$(includedir) lib_LTLIBRARIES = libmogltk.la libmogltk_la_SOURCES = engine.cc glbase.cc glcolor.cc glfont.cc gltexture.cc \ -glshape.cc glwidgets.cc sprite.cc base.cc font.cc shape.cc mcolor.cc +glshape.cc glwidgets.cc sprite.cc base.cc font.cc shape.cc mcolor.cc texture.cc diff --git a/lib/font.cc b/lib/font.cc index 7cc57f6..204e60d 100644 --- a/lib/font.cc +++ b/lib/font.cc @@ -104,7 +104,7 @@ mogltk::font::font(Handle * ffont) : textcolor(255, 255, 255, 255), shadow(0), w fonttex = (texture **) malloc(nbT * sizeof(texture *)); for (i = 0; i < nbT; i++) { - fonttex[i] = new texture(256, 256, true); + fonttex[i] = alloctexture(); } sizes = (Uint8 *) malloc(nbentries * sizeof(Uint8)); @@ -315,4 +315,6 @@ int mogltk::font::singletextsize(const String & s) const { mogltk::font * mogltk::SystemFont; - +mogltk::texture * mogltk::font::alloctexture() { + return new mogltk::texture(256, 256); +} diff --git a/lib/glfont.cc b/lib/glfont.cc index 66e36c4..38f5c78 100644 --- a/lib/glfont.cc +++ b/lib/glfont.cc @@ -51,5 +51,9 @@ void mogltk::glfont::drawentry(Uint16 entry, int x, int y, glColorP c) { } void mogltk::glfont::Bind(int index) { - fonttex[index]->Bind(); + ((mogltk::gltexture *) fonttex[index])->Bind(); +} + +mogltk::gltexture * mogltk::glfont::alloctexture() { + return new mogltk::gltexture(256, 256, true); } diff --git a/lib/glshape.cc b/lib/glshape.cc index 7b93dfb..831be32 100644 --- a/lib/glshape.cc +++ b/lib/glshape.cc @@ -3,6 +3,7 @@ #include "glshape.h" #include "gltexture.h" #include "glfont.h" +#include "engine.h" #define ENTER bool was2d = Enter(true) #define ENTERT bool was2d = Enter(false) @@ -35,7 +36,7 @@ void mogltk::glshape::box(int x1, int y1, int x2, int y2, glColorP c1, glColorP LEAVE; } -void mogltk::glshape::tbox(mogltk::texture * t, int x1, int y1, int x2, int y2, int tx1, int ty1, int tx2, int ty2, glColorP c) { +void mogltk::glshape::tbox(mogltk::gltexture * t, int x1, int y1, int x2, int y2, int tx1, int ty1, int tx2, int ty2, glColorP c) { ENTERT; c.Bind(); @@ -50,7 +51,7 @@ void mogltk::glshape::tbox(mogltk::texture * t, int x1, int y1, int x2, int y2, LEAVE; } -void mogltk::glshape::tbox(mogltk::texture * t, int x1, int y1, int x2, int y2, glColorP c1, glColorP c2, glColorP c3, glColorP c4, int tx1, int ty1, int tx2, int ty2) { +void mogltk::glshape::tbox(mogltk::gltexture * t, int x1, int y1, int x2, int y2, glColorP c1, glColorP c2, glColorP c3, glColorP c4, int tx1, int ty1, int tx2, int ty2) { ENTERT; t->Bind(); @@ -105,12 +106,12 @@ void mogltk::glshape::box3d(int x1, int y1, int x2, int y2, glColorP face, glCol } bool mogltk::glshape::Enter(bool unbind) { - bool was2D = mogltk::glbase::is2D(); + bool was2D = mogltk::engine::glbase_o->is2D(); if (!was2D) - mogltk::glbase::Enter2DMode(); + mogltk::engine::glbase_o->Enter2DMode(); - if (unbind) mogltk::texture::Unbind(); + if (unbind) mogltk::gltexture::Unbind(); return was2D; } @@ -121,5 +122,5 @@ bool mogltk::glshape::Enter() { void mogltk::glshape::Leave(bool was2D) { if (!was2D) - mogltk::glbase::Leave2DMode(); + mogltk::engine::glbase_o->Leave2DMode(); } diff --git a/lib/glsprite.cc b/lib/glsprite.cc new file mode 100644 index 0000000..282eefe --- /dev/null +++ b/lib/glsprite.cc @@ -0,0 +1,146 @@ +#include +#include "glbase.h" +#include "sprite.h" + +#define TEXSIZE 256 + +mogltk::Sprite::TexList * mogltk::Sprite::TexList::header = 0; + +mogltk::Sprite::Sprite(Handle * h, int asx, int asy) : sx(asx), sy(asy) { + alloc(); + for (int y = 0; y < sy; y++) { + h->read(tlist->GetTex()->GetPixels() + TEXSIZE * y + posx, sx * 4); + } +} + +mogltk::Sprite::Sprite(Uint8 * p, int asx, int asy) : sx(asx), sy(asy) { + alloc(); + for (int y = 0; y < sy; y++) { + bcopy(p, tlist->GetTex()->GetPixels() + TEXSIZE * y + posx, sx * 4); + p += sx * 4; + } +} + +mogltk::Sprite::~Sprite() { + if (prev) + prev->next = next; + else + tlist->sprheader = next; + + if (next) + next->prev = prev; + + if (!tlist->sprheader) { + delete tlist; + } +} + +void mogltk::Sprite::alloc() { + /* FIXMEEEEEEEEEEEEE */ + bool found = false; + TexList * p; + + for (p = tlist; p && !found; p = p->GetNext()) { + for (posy = 0; (posy < (TEXSIZE - sy)) && !found; posy++) { + for (posx = 0; (posx < (TEXSIZE - sx)) && !found; posx++) { + if (p->sprheader->canfit(posx, posy, posx + sx, posy + sy)) { + found = true; + } + } + } + } + + if (!found) { +#ifdef DEBUG + printm(M_INFO, "Allocating a new texture for sprites\n"); +#endif + posx = posy = 0; + p = new TexList(TEXSIZE); + } + + tlist = p; +} + +mogltk::Sprite::TexList::TexList(int size) { + tex = new texture(size, size, true); + + if (header) + header->next->prev = this; + prev = 0; + next = header; + header = this; +} + +mogltk::Sprite::TexList::~TexList() { + delete tex; + + if (prev) + prev->next = next; + else + header = next; + + if (next) + next->prev = prev; +} + +const mogltk::texture * mogltk::Sprite::TexList::GetTex() const { + return tex; +} + +mogltk::texture * mogltk::Sprite::TexList::GetTex() { + return tex; +} + +const mogltk::Sprite::TexList * mogltk::Sprite::TexList::GetHead() { + return header; +} + +const mogltk::Sprite::TexList * mogltk::Sprite::TexList::GetNext() const { + return next; +} + +mogltk::Sprite::TexList * mogltk::Sprite::TexList::GetNext() { + return next; +} + +void mogltk::Sprite::TexList::Bind() const { + tex->Bind(); +} + +bool mogltk::Sprite::intersect(int x1, int y1, int x2, int y2) const { + int sx1 = posx, sy1 = posy, sx2 = posx + sx, sy2 = posy + sy; + + return !((sx1 >= x2) || (sx2 <= x1) || (sy1 >= y1) || (sy2 <= y2)); +} + +bool mogltk::Sprite::canfit(int x1, int y1, int x2, int y2) const { + if (!intersect(x1, y1, x2, y2)) + return true; + else + if (next) + return next->canfit(x1, y1, x2, y2); + else + return false; +} + +void mogltk::Sprite::draw(int dx, int dy, ColorP c) { + bool was2D; + + was2D = mogltk::glbase::is2D(); + + if (!was2D) + mogltk::glbase::Enter2DMode(); + + c.Bind(); + + tlist->Bind(); + glBegin(GL_TRIANGLE_STRIP); + glTexCoord2i(posx , posy ); glVertex2i(dx , dy ); + glTexCoord2i(posx + sx - 1, posy ); glVertex2i(dx + sx - 1, dy ); + glTexCoord2i(posx , posy + sy - 1); glVertex2i(dx , dy + sy - 1); + glTexCoord2i(posx + sx - 1, posy + sy - 1); glVertex2i(dx + sx - 1, dy + sy - 1); + glEnd(); + + if (!was2D) + mogltk::glbase::Leave2DMode(); +} diff --git a/lib/gltexture.cc b/lib/gltexture.cc index c0a940f..ff79112 100644 --- a/lib/gltexture.cc +++ b/lib/gltexture.cc @@ -8,163 +8,31 @@ #include "config.h" #endif #include "gettext.h" -#ifndef _ -#define _(x) x -#endif - -#define DEBUG 1 - -#ifdef TRACE_TEXTURES -mogltk::texture * mogltk::texture::header = 0; -mogltk::texture * mogltk::texture::footer = 0; -#endif -mogltk::texture * mogltk::texture::active = 0; +mogltk::gltexture * mogltk::gltexture::active = 0; -mogltk::texture::texture(int w, int h, bool plane) throw (GeneralException) : width(w), height(h), - texture_allocated(false), planar(plane), tainted(true) { - if ((!ISPOT(w)) || (!ISPOT(h))) - throw GeneralException(_("Size of the texture not a power of 2!")); - - if (!(surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, -#if SDL_BYTEORDER == SDL_BIG_ENDIAN - 0xff000000, - 0x00ff0000, - 0x0000ff00, - 0x000000ff -#else - 0x000000ff, - 0x0000ff00, - 0x00ff0000, - 0xff000000 -#endif - ))) { - 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::gltexture::gltexture(int w, int h, bool plane) throw (GeneralException) : + texture(w, h), texture_allocated(false), planar(plane), tainted(true) { } -mogltk::texture::texture(Handle * h, bool plane) throw (GeneralException) : - texture_allocated(false), planar(plane), tainted(true) { - - SDL_Surface * temp; - - temp = LoadNTEX(h); - width = temp->w; - height = temp->h; - -#ifdef DEBUG - printm(M_INFO, "Creating texture from file: size %ix%i\n", height, width); -#endif - - if ((!ISPOT(width)) || (!ISPOT(height))) { - SDL_FreeSurface(temp); - throw GeneralException(_("Size of the texture not a power of 2!")); - } - - SDL_PixelFormat f; - - f.palette = 0; - f.BitsPerPixel = 32; - f.BytesPerPixel = 4; -#if SDL_BYTEORDER == SDL_BIG_ENDIAN - f.Amask = 0x000000ff; - f.Bmask = 0x0000ff00; - f.Gmask = 0x00ff0000; - f.Rmask = 0xff000000; - f.Ashift = 0; - f.Bshift = 8; - f.Gshift = 16; - f.Rshift = 24; -#else - f.Rmask = 0x000000ff; - f.Gmask = 0x0000ff00; - f.Bmask = 0x00ff0000; - f.Amask = 0xff000000; - f.Rshift = 0; - f.Gshift = 8; - f.Bshift = 16; - f.Ashift = 24; -#endif - f.Rloss = 0; - f.Gloss = 0; - f.Bloss = 0; - f.Aloss = 0; - - if (!(surface = SDL_ConvertSurface(temp, &f, 0))) { - throw GeneralException("Could not convert texture to OpenGL format"); - } - - SDL_FreeSurface(temp); - -#ifdef TRACE_TEXTURES - next = 0; - prev = footer; - footer = this; - if (!header) { - header = this; - } - if (prev) { - prev->next = this; - } -#endif +mogltk::gltexture::gltexture(Handle * h, bool plane) throw (GeneralException) : + texture(h), texture_allocated(false), planar(plane), tainted(true) { } -mogltk::texture::~texture() { - if (surface) { - SDL_FreeSurface(surface); - } - +mogltk::gltexture::~gltexture() { 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 -} - -Uint32 * mogltk::texture::GetPixels() { - return (Uint32 *) surface->pixels; } -SDL_Surface * mogltk::texture::GetSurface() { - return surface; -} - -void mogltk::texture::Generate() { +void mogltk::gltexture::Generate() { if (texture_allocated) { glDeleteTextures(1, &tex); } glGenTextures(1, &tex); #ifdef DEBUG - printm(M_INFO, _("Generated texture index: %i\n"), tex); + printm(M_INFO, _("Generated gltexture index: %i\n"), tex); #endif glBindTexture(GL_TEXTURE_2D, tex); @@ -172,7 +40,7 @@ void mogltk::texture::Generate() { #if 0 if (planar) { #ifdef DEBUG - printm(M_INFO, _("Generating planar texture: %i\n"), tex); + printm(M_INFO, _("Generating planar gltexture: %i\n"), tex); #endif glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); @@ -180,13 +48,13 @@ void mogltk::texture::Generate() { } else { #endif #ifdef DEBUG - printm(M_INFO, _("Generating 3D texture: %i\n"), tex); + printm(M_INFO, _("Generating 3D gltexture: %i\n"), tex); #endif glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, width, height, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels); + gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, GetWidth(), GetHeight(), GL_RGBA, GL_UNSIGNED_BYTE, GetPixels()); // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels); #if 0 } @@ -196,20 +64,20 @@ void mogltk::texture::Generate() { tainted = false; } -void mogltk::texture::Bind(bool expand) { +void mogltk::gltexture::Bind(bool expand) { if ((!texture_allocated) || tainted) Generate(); glEnable(GL_TEXTURE_2D); if (active == this) return; #ifdef DEBUG - printm(M_INFO, _("Binding texture index %i.\n"), tex); + printm(M_INFO, _("Binding gltexture index %i.\n"), tex); #endif glBindTexture(GL_TEXTURE_2D, tex); if (expand) { glMatrixMode(GL_TEXTURE); glLoadIdentity(); - glScaled(1 / (double) width, 1 / (double) height, 1); + glScaled(1 / (double) GetWidth(), 1 / (double) GetHeight(), 1); glMatrixMode(GL_MODELVIEW); } @@ -236,65 +104,17 @@ void mogltk::texture::Bind(bool expand) { #endif } -GLuint mogltk::texture::GetWidth() { - return width; -} - -GLuint mogltk::texture::GetHeight() { - return height; -} - -void mogltk::texture::Unbind(void) { +void mogltk::gltexture::Unbind(void) { if (active) { glBindTexture(GL_TEXTURE_2D, 0); glDisable(GL_TEXTURE_2D); active = 0; #ifdef DEBUG - printm(M_INFO, _("Unbinding texture.\n")); + printm(M_INFO, _("Unbinding gltexture.\n")); #endif } } -void mogltk::texture::Taint(void) { +void mogltk::gltexture::Taint(void) { tainted = true; } - -#ifdef WORDS_BIGENDIAN -#define NTEX_SIGNATURE 0x4e544558 -#else -#define NTEX_SIGNATURE 0x5845544e -#endif - -SDL_Surface * mogltk::texture::LoadNTEX(Handle * h) throw (GeneralException) { - SDL_Surface * r; - char buffer[4]; - Uint16 height, width; - - h->read(buffer, 4); - buffer[4] = 0; - if (*((Uint32 *) buffer) != NTEX_SIGNATURE) - throw GeneralException("Texture file " + h->GetName() + " corrupted"); - - height = h->readU16(); - width = h->readU16(); - - if (!(r = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, -#if SDL_BYTEORDER == SDL_BIG_ENDIAN - 0xff000000, - 0x00ff0000, - 0x0000ff00, - 0x000000ff -#else - 0x000000ff, - 0x0000ff00, - 0x00ff0000, - 0xff000000 -#endif - ))) { - throw GeneralException(_("Can't create RGB Surface for LoadNTEX")); - } - - h->read(r->pixels, height * width * 4); - - return r; -} diff --git a/lib/texture.cc b/lib/texture.cc new file mode 100644 index 0000000..36297ac --- /dev/null +++ b/lib/texture.cc @@ -0,0 +1,194 @@ +#include +#include +#include +#include "texture.h" +#include "engine.h" +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif +#include "gettext.h" + +#ifdef TRACE_TEXTURES +mogltk::texture * mogltk::texture::header = 0; +mogltk::texture * mogltk::texture::footer = 0; +#endif + +mogltk::texture * mogltk::texture::active = 0; + +mogltk::texture::texture(int w, int h) throw (GeneralException) : width(w), height(h), + texture_allocated(false), planar(plane), tainted(true) { + if ((!ISPOT(w)) || (!ISPOT(h))) + throw GeneralException(_("Size of the texture not a power of 2!")); + + if (!(surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + 0xff000000, + 0x00ff0000, + 0x0000ff00, + 0x000000ff +#else + 0x000000ff, + 0x0000ff00, + 0x00ff0000, + 0xff000000 +#endif + ))) { + 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(Handle * h) throw (GeneralException) { + SDL_Surface * temp; + + temp = LoadNTEX(h); + width = temp->w; + height = temp->h; + +#ifdef DEBUG + printm(M_INFO, "Creating texture from file: size %ix%i\n", height, width); +#endif + + if ((!ISPOT(width)) || (!ISPOT(height))) { + SDL_FreeSurface(temp); + throw GeneralException(_("Size of the texture not a power of 2!")); + } + + SDL_PixelFormat f; + + f.palette = 0; + f.BitsPerPixel = 32; + f.BytesPerPixel = 4; +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + f.Amask = 0x000000ff; + f.Bmask = 0x0000ff00; + f.Gmask = 0x00ff0000; + f.Rmask = 0xff000000; + f.Ashift = 0; + f.Bshift = 8; + f.Gshift = 16; + f.Rshift = 24; +#else + f.Rmask = 0x000000ff; + f.Gmask = 0x0000ff00; + f.Bmask = 0x00ff0000; + f.Amask = 0xff000000; + f.Rshift = 0; + f.Gshift = 8; + f.Bshift = 16; + f.Ashift = 24; +#endif + f.Rloss = 0; + f.Gloss = 0; + f.Bloss = 0; + f.Aloss = 0; + + if (!(surface = SDL_ConvertSurface(temp, &f, 0))) { + throw GeneralException("Could not convert texture to OpenGL format"); + } + + SDL_FreeSurface(temp); + +#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); + } + +#ifdef TRACE_TEXTURES + if (prev) { + prev->next = next; + } + + if (next) { + next->prev = prev; + } + + if (this == footer) { + footer = prev; + } + + if (this == header) { + header = next; + } +#endif +} + +Uint32 * mogltk::texture::GetPixels() { + return (Uint32 *) surface->pixels; +} + +SDL_Surface * mogltk::texture::GetSurface() { + return surface; +} + +Uint32 mogltk::texture::GetWidth() { + return width; +} + +Uint32 mogltk::texture::GetHeight() { + return height; +} + +#ifdef WORDS_BIGENDIAN +#define NTEX_SIGNATURE 0x4e544558 +#else +#define NTEX_SIGNATURE 0x5845544e +#endif + +SDL_Surface * mogltk::texture::LoadNTEX(Handle * h) throw (GeneralException) { + SDL_Surface * r; + char buffer[4]; + Uint16 height, width; + + h->read(buffer, 4); + buffer[4] = 0; + if (*((Uint32 *) buffer) != NTEX_SIGNATURE) + throw GeneralException("Texture file " + h->GetName() + " corrupted"); + + height = h->readU16(); + width = h->readU16(); + + if (!(r = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + 0xff000000, + 0x00ff0000, + 0x0000ff00, + 0x000000ff +#else + 0x000000ff, + 0x0000ff00, + 0x00ff0000, + 0xff000000 +#endif + ))) { + throw GeneralException(_("Can't create RGB Surface for LoadNTEX")); + } + + h->read(r->pixels, height * width * 4); + + return r; +} -- cgit v1.2.3