Okay, I have re-coded this using static member variables... but I am getting the exact same result. Below is my code and output.
Code:
//texture.cpp
#include "texture.h"
#include "iostream"
void texture::initList()
{
glGenTextures( 256, textureBank);
}
GLuint texture::textureBank[256] = {};
bool texture::textureFree[256] = {true};
int texture::numTextures = 0;
bool texture::listInitialized = false;
void texture::drawTexture(int x, int y)
{
y = y - imageRect.y;
// Bind the texture to which subsequent calls refer to
glBindTexture( GL_TEXTURE_2D, textureBank[glTexture] );
glColor3f(1,1,1);
glBegin( GL_QUADS );
//Bottom-left vertex (corner)
glTexCoord2i( 0, 0 );
glVertex3f( x, (y + textureRect.h), 0.0f );
//Bottom-right vertex (corner)
glTexCoord2i( 1, 0 );
glVertex3f( (x + textureRect.w), (y + textureRect.h), 0.f );
//Top-right vertex (corner)
glTexCoord2i( 1, 1 );
glVertex3f( (x + textureRect.w), y, 0.f );
//Top-left vertex (corner)
glTexCoord2i( 0, 1 );
glVertex3f( x, y, 0.f );
glEnd();
}
int texture::getTextureIndex()
{
return glTexture;
}
texture::texture()
{
if(listInitialized == false)
{
initList();
}
}
texture::~texture()
{
numTextures--;
textureFree[glTexture] = true;
}
SDL_Rect texture::getImageRect()
{
return imageRect;
}
texture::texture(SDL_Surface *rawSurface)
{
if(listInitialized == false)
{
initList();
}
for(int a = 0; a < 256; a++)
{
std::cout << "texture # " << a << " status is " << textureFree[a];
if(textureFree[a] == true)
{
glTexture = a;
numTextures++;
textureFree[a] = false;
std::cout << " true, taking, numtextures: " << numTextures << ".\n";
break;
}
}
SDL_Surface *surface;
textureRect.w = nextPowerOfTwo(rawSurface->w);
textureRect.h = nextPowerOfTwo(rawSurface->h);
imageRect.w = rawSurface->w;
imageRect.h = rawSurface->h;
imageRect.x = 0;
imageRect.y = textureRect.h - imageRect.h;
surface = SDL_CreateRGBSurface(SDL_SRCALPHA, textureRect.w, textureRect.h, 32, 0, 0, 0, 0);
blitFast(rawSurface, surface, 0, 0);
GLint nOfColors = surface->format->BytesPerPixel;
GLenum texture_format;
if (nOfColors == 4) // contains an alpha channel
{
if (surface->format->Rmask == 0x000000ff)
texture_format = GL_RGBA;
else
texture_format = GL_BGRA;
} else if (nOfColors == 3) // no alpha channel
{
if (surface->format->Rmask == 0x000000ff)
texture_format = GL_RGB;
else
texture_format = GL_BGR;
}
glBindTexture(GL_TEXTURE_2D, textureBank[glTexture]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, surface->w, surface->h, 0,texture_format, GL_UNSIGNED_BYTE, surface->pixels);
if(surface)
{SDL_FreeSurface( surface );}
}
Code:
//texture.h
#pragma once
#include "SDL/SDL.h"
#include "SDL/SDL_opengl.h"
#include "gfxUtilities.h"
class texture
{
private:
int glTexture;
SDL_Rect imageRect;
SDL_Rect textureRect;
void initList();
static bool listInitialized;
public:
static GLuint textureBank[256];
static bool textureFree[256];
static int numTextures;
texture();
~texture();
texture(SDL_Surface *rawSurface);
int getTextureIndex();
void drawTexture(int x, int y);
SDL_Rect getImageRect();
};