summaryrefslogtreecommitdiff
path: root/lib/texture.cc
diff options
context:
space:
mode:
authorpixel <pixel>2004-07-04 11:56:18 +0000
committerpixel <pixel>2004-07-04 11:56:18 +0000
commit5cc874802b0b8c4462e7e873654e6daa54be00de (patch)
treed97bfbfe26998b1ee9cf01d830ba446e61094a86 /lib/texture.cc
parentbf452e7f6a3fa0e41964fc8e2c57e1e577cd1682 (diff)
SOL Demo
Diffstat (limited to 'lib/texture.cc')
-rw-r--r--lib/texture.cc50
1 files changed, 45 insertions, 5 deletions
diff --git a/lib/texture.cc b/lib/texture.cc
index a8036bc..42c8f3f 100644
--- a/lib/texture.cc
+++ b/lib/texture.cc
@@ -17,7 +17,7 @@ mogltk::texture * mogltk::texture::footer = 0;
mogltk::texture * mogltk::texture::active = 0;
mogltk::texture::texture(int w, int h, bool plane) throw (GeneralException) : width(w), height(h),
- texture_allocated(false), planar(plane), tainted(true) {
+ texture_allocated(false), planar(plane), tainted(true), taintable(true) {
if ((!ISPOT(w)) || (!ISPOT(h)))
throw GeneralException(_("Size of the texture not a power of 2!"));
@@ -51,7 +51,7 @@ mogltk::texture::texture(int w, int h, bool plane) throw (GeneralException) : wi
}
mogltk::texture::texture(Handle * h, bool plane) throw (GeneralException) :
- texture_allocated(false), planar(plane), tainted(true) {
+ texture_allocated(false), planar(plane), tainted(true), taintable(true) {
SDL_Surface * temp;
@@ -114,6 +114,34 @@ mogltk::texture::texture(Handle * h, bool plane) throw (GeneralException) :
}
}
+inline static unsigned int nextpower(unsigned int n) {
+ unsigned int i;
+
+ if (!n)
+ return n;
+
+ if (ISPOT(n))
+ return n;
+
+ for (i = 31; i >= 0; i--) {
+ if ((n >> i) & 1) {
+ return 1 << (i + 1);
+ }
+ }
+}
+
+mogltk::texture::texture(int x, int y, int w, int h) throw (GeneralException) : width(nextpower(w)), height(nextpower(h)),
+ texture_allocated(true), planar(false), tainted(false), taintable(false) {
+ glGenTextures(1, &tex);
+ glBindTexture(GL_TEXTURE_2D, tex);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ 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);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
+ glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, x, y, w, h);
+}
+
mogltk::texture::~texture() {
if (surface) {
SDL_FreeSurface(surface);
@@ -141,7 +169,10 @@ mogltk::texture::~texture() {
}
Uint32 * mogltk::texture::GetPixels() {
- return (Uint32 *) surface->pixels;
+ if (surface)
+ return (Uint32 *) surface->pixels;
+ else
+ return 0;
}
SDL_Surface * mogltk::texture::GetSurface() {
@@ -149,7 +180,10 @@ SDL_Surface * mogltk::texture::GetSurface() {
}
SDL_PixelFormat * mogltk::texture::GetFormat() {
- return surface->format;
+ if (surface)
+ return surface->format;
+ else
+ return 0;
}
void mogltk::texture::Generate() {
@@ -245,7 +279,8 @@ void mogltk::texture::Unbind(void) {
}
void mogltk::texture::Taint(void) {
- tainted = true;
+ if (taintable)
+ tainted = true;
}
void mogltk::texture::Taintall(void) {
@@ -298,3 +333,8 @@ SDL_Surface * mogltk::texture::LoadNTEX(Handle * h) throw (GeneralException) {
return r;
}
+
+void mogltk::texture::DumpBMP(const String & n) {
+ if (surface)
+ SDL_SaveBMP(surface, n.to_charp());
+}