summaryrefslogtreecommitdiff
path: root/lib/gltexture.cc
diff options
context:
space:
mode:
Diffstat (limited to 'lib/gltexture.cc')
-rw-r--r--lib/gltexture.cc93
1 files changed, 71 insertions, 22 deletions
diff --git a/lib/gltexture.cc b/lib/gltexture.cc
index 601d24f..accdb3a 100644
--- a/lib/gltexture.cc
+++ b/lib/gltexture.cc
@@ -6,7 +6,9 @@
#include "engine.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
-#else
+#endif
+#include "gettext.h"
+#ifndef _
#define _(x) x
#endif
@@ -58,41 +60,54 @@ mogltk::texture::texture(Handle * h, bool plane, texture_type ttype) throw (Gene
SDL_Surface * temp;
-#if USE_SDLIMAGE
- if (!(temp = IMG_Load_RW(engine::RWFromHandle(h), 1)))
- throw GeneralException(_("Can't load image from Handle ") + h->GetName());
-#else
- if (!(temp = LoadNTEX(h)))
- throw GeneralException(_("Can't load texture from Handle ") + h->GetName());
-#endif
+ 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!"));
}
- if (!(surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,
+ SDL_PixelFormat f;
+
+ f.palette = 0;
+ f.BitsPerPixel = 32;
+ f.BytesPerPixel = 4;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
- 0xff000000,
- 0x00ff0000,
- 0x0000ff00,
- 0x000000ff
+ 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
- 0x000000ff,
- 0x0000ff00,
- 0x00ff0000,
- 0xff000000
+ 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
- ))) {
- SDL_FreeSurface(temp);
- throw GeneralException(_("Can't create RGB Surface"));
+ 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_BlitSurface(temp, 0, surface, 0);
SDL_FreeSurface(temp);
-
+
#ifdef TRACE_TEXTURES
next = 0;
prev = footer;
@@ -222,6 +237,7 @@ GLuint mogltk::texture::GetHeight() {
}
void mogltk::texture::Unbind(void) {
+ glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
active = 0;
#ifdef DEBUG
@@ -232,3 +248,36 @@ void mogltk::texture::Unbind(void) {
void mogltk::texture::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,
+ 0x000000ff,
+ 0x0000ff00,
+ 0x00ff0000,
+ 0xff000000
+ ))) {
+ throw GeneralException(_("Can't create RGB Surface for LoadNTEX"));
+ }
+
+ h->read(r->pixels, height * width * 4);
+
+ return r;
+}