summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/font.h1
-rw-r--r--include/glfont.h3
-rw-r--r--include/glshape.h6
-rw-r--r--include/glsprite.h45
-rw-r--r--include/gltexture.h29
-rw-r--r--include/sprite.h4
-rw-r--r--include/texture.h22
-rw-r--r--lib/Makefile.am2
-rw-r--r--lib/font.cc6
-rw-r--r--lib/glfont.cc6
-rw-r--r--lib/glshape.cc13
-rw-r--r--lib/glsprite.cc146
-rw-r--r--lib/gltexture.cc214
-rw-r--r--lib/texture.cc194
14 files changed, 444 insertions, 247 deletions
diff --git a/include/font.h b/include/font.h
index a6f4a16..fcc2e03 100644
--- a/include/font.h
+++ b/include/font.h
@@ -36,6 +36,7 @@ namespace mogltk {
int cx, cy, ox;
ColorP textcolor;
int shadow, wspace;
+ virtual texture * alloctexture();
};
extern font * SystemFont;
};
diff --git a/include/glfont.h b/include/glfont.h
index b8663d7..1931467 100644
--- a/include/glfont.h
+++ b/include/glfont.h
@@ -10,6 +10,9 @@ namespace mogltk {
glfont(Handle *);
virtual ~glfont();
void drawentry(Uint16, int, int, glColorP = WHITE);
+
+ protected:
+ virtual gltexture * alloctexture();
private:
void Bind(int);
diff --git a/include/glshape.h b/include/glshape.h
index 81439ad..465f60b 100644
--- a/include/glshape.h
+++ b/include/glshape.h
@@ -11,10 +11,10 @@ namespace mogltk {
class glshape : public shape {
public:
virtual void box(int x1, int y1, int x2, int y2, glColorP = WHITE);
- virtual void box3d(int x1, int y1, int x2, int y2, glColorP = DOS_WHITE, ColorP = DOS_HIGH_WHITE, ColorP = DOS_GRAY, int = 2, bool = false);
+ virtual void box3d(int x1, int y1, int x2, int y2, glColorP = DOS_WHITE, glColorP = DOS_HIGH_WHITE, glColorP = DOS_GRAY, int = 2, bool = false);
virtual void box(int x1, int y1, int x2, int y2, glColorP, glColorP, glColorP, glColorP);
- virtual void tbox(texture *, int x1, int y1, int x2, int y2, int tx1, int ty1, int tx2, int ty2, ColorP = WHITE);
- virtual void tbox(texture *, int x1, int y1, int x2, int y2, glColorP, glColorP, glColorP, glColorP, int tx1, int ty1, int tx2, int ty2);
+ virtual void tbox(gltexture *, int x1, int y1, int x2, int y2, int tx1, int ty1, int tx2, int ty2, glColorP = WHITE);
+ virtual void tbox(gltexture *, int x1, int y1, int x2, int y2, glColorP, glColorP, glColorP, glColorP, int tx1, int ty1, int tx2, int ty2);
private:
bool Enter(bool);
virtual bool Enter();
diff --git a/include/glsprite.h b/include/glsprite.h
new file mode 100644
index 0000000..3f481c5
--- /dev/null
+++ b/include/glsprite.h
@@ -0,0 +1,45 @@
+#ifndef __GLSPRITE_H__
+#define __GLSPRITE_H__
+
+#include <vector>
+#include <Exceptions.h>
+#include <Handle.h>
+#include <glcolor.h>
+#include "gltexture.h"
+#include "sprite.h"
+
+namespace mogltk {
+ class glSprite : public Sprite {
+ public:
+ glSprite(Handle *, int, int);
+ glSprite(Uint8 *, int, int);
+ virtual ~glSprite();
+ virtual void draw(int, int, ColorP = WHITE);
+ private:
+ class TexList : public Base {
+ public:
+ TexList(int);
+ virtual ~TexList();
+ glSprite * sprheader;
+ const texture * GetTex() const;
+ texture * GetTex();
+ static const TexList * GetHead();
+ const TexList * GetNext() const;
+ TexList * GetNext();
+ void Bind() const;
+ private:
+ texture * tex;
+ static TexList * header;
+ TexList * next, * prev;
+ };
+ glSprite * next, * prev;
+ TexList * tlist;
+ int sx, sy, posx, posy;
+ void alloc();
+ bool canfit(int, int, int, int) const;
+ bool intersect(int, int, int, int) const;
+ };
+
+};
+
+#endif
diff --git a/include/gltexture.h b/include/gltexture.h
index 4f5e162..a5137db 100644
--- a/include/gltexture.h
+++ b/include/gltexture.h
@@ -6,33 +6,22 @@
#include <Handle.h>
#include <Exceptions.h>
#include <generic.h>
+#include <texture.h>
namespace mogltk {
- class texture : public Base {
+ class gltexture : public texture {
public:
- texture(int = 256, int = 256, bool = false) throw (GeneralException);
- texture(Handle *, bool = false) throw (GeneralException);
- virtual ~texture();
- SDL_Surface * GetSurface();
- Uint32 * GetPixels();
- static SDL_Surface * LoadNTEX(Handle * h) throw (GeneralException) ;
+ gltexture(int = 256, int = 256, bool = false) throw (GeneralException);
+ gltexture(Handle *, bool = false) throw (GeneralException);
+ virtual ~gltexture();
void Generate();
+ void Taint();
void Bind(bool = true);
- GLuint GetWidth();
- GLuint GetHeight();
static void Unbind(void);
- void Taint(void);
private:
- GLuint width, height, tex;
- bool texture_allocated;
- SDL_Surface * surface;
- bool planar, tainted;
-#ifdef TRACE_TEXTURES
- static texture * header;
- static texture * footer;
- texture * next, * prev;
-#endif
- static texture * active;
+ GLuint tex;
+ bool texture_allocated, planar, tainted;
+ static gltexture * active;
};
};
diff --git a/include/sprite.h b/include/sprite.h
index 16565d0..aed711e 100644
--- a/include/sprite.h
+++ b/include/sprite.h
@@ -4,8 +4,8 @@
#include <vector>
#include <Exceptions.h>
#include <Handle.h>
-#include <glcolor.h>
-#include "gltexture.h"
+#include <mcolor.h>
+#include <texture.h>
namespace mogltk {
class Sprite : public Base {
diff --git a/include/texture.h b/include/texture.h
index 4f5e162..904eeae 100644
--- a/include/texture.h
+++ b/include/texture.h
@@ -1,8 +1,7 @@
-#ifndef __GLTEXTURE_H__
-#define __GLTEXTURE_H__
+#ifndef __TEXTURE_H__
+#define __TEXTURE_H__
#include <SDL.h>
-#include <SDL_opengl.h>
#include <Handle.h>
#include <Exceptions.h>
#include <generic.h>
@@ -10,29 +9,22 @@
namespace mogltk {
class texture : public Base {
public:
- texture(int = 256, int = 256, bool = false) throw (GeneralException);
- texture(Handle *, bool = false) throw (GeneralException);
+ texture(int = 256, int = 256) throw (GeneralException);
+ texture(Handle *) throw (GeneralException);
virtual ~texture();
SDL_Surface * GetSurface();
Uint32 * GetPixels();
static SDL_Surface * LoadNTEX(Handle * h) throw (GeneralException) ;
- void Generate();
- void Bind(bool = true);
- GLuint GetWidth();
- GLuint GetHeight();
- static void Unbind(void);
- void Taint(void);
+ Uint32 GetWidth();
+ Uint32 GetHeight();
private:
- GLuint width, height, tex;
- bool texture_allocated;
+ Uint32 width, height;
SDL_Surface * surface;
- bool planar, tainted;
#ifdef TRACE_TEXTURES
static texture * header;
static texture * footer;
texture * next, * prev;
#endif
- static texture * active;
};
};
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 <SDL.h>
+#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 <sys/types.h>
+#include <SDL.h>
+#include <generic.h>
+#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;
+}