Originally Posted by
Perspective
Post the texture gen and render code. Are the texture dimensions a power of 2?
Yep they're 32x32.
The reason the archive is so big is because of the IntelliSense database MSVC builds. I think.
It'd be easier if I just post up the entire classes:
CTextureCache: The idea was to keep a static std::vector containing the textures for use anywhere since only ~20 textures are ever going to be used.
Code:
// CTextureCache.h
#pragma once
#include "stdafx.h"
const ILstring textureNames[] = { "water.jpg",
"grass.jpg"
};
class CTextureCache {
static std::vector<GLuint> texture;
const unsigned NUM_TEXTURES;
const unsigned LIM_WIDTH;
const unsigned LIM_HEIGHT;
ILuint currentTexID;
public:
CTextureCache ();
~CTextureCache ();
void Init ();
void Load ();
GLuint GetByID (char id);
};
// CTextureCache.cpp
#include "StdAfx.h"
#include ".\texturecache.h"
std::vector<GLuint> CTextureCache::texture;
CTextureCache::CTextureCache ()
:NUM_TEXTURES (2),
LIM_WIDTH (32),
LIM_HEIGHT (32)
{
}
CTextureCache::~CTextureCache ()
{
}
// Init
//
void CTextureCache::Init ()
{
for (unsigned i = 0; i < NUM_TEXTURES; i++)
texture.push_back (0);
}
// Load
//
void CTextureCache::Load ()
{
unsigned i;
for (i = 0; i < NUM_TEXTURES; i++) {
ilGenImages (NUM_TEXTURES, ¤tTexID);
ilBindImage (currentTexID);
if (! ilLoadImage (textureNames[i]))
reportErr (true, "Unable to load texture '%s'", textureNames[i]);
if ((ilGetInteger (IL_IMAGE_WIDTH) != LIM_WIDTH) ||
(ilGetInteger (IL_IMAGE_HEIGHT) != LIM_HEIGHT))
{
reportErr (true, "'%s', Image dimensions out of bounds [%d, %d]",
textureNames[i],
ilGetInteger (IL_IMAGE_WIDTH),
ilGetInteger (IL_IMAGE_HEIGHT));
}
ilConvertImage (IL_RGBA, IL_UNSIGNED_BYTE);
glGenTextures (1, &texture[i]);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D (GL_TEXTURE_2D, 0, ilGetInteger (IL_IMAGE_BPP), ilGetInteger (IL_IMAGE_WIDTH),
ilGetInteger (IL_IMAGE_HEIGHT), 0, ilGetInteger (IL_IMAGE_FORMAT),
GL_UNSIGNED_BYTE, ilGetData ());
ilDeleteImages (1, ¤tTexID);
}
}
// GetByID
//
GLuint CTextureCache::GetByID (char id)
{
switch (id) {
case 'w':
return texture[0];
case 'g':
return texture[1];
default:
reportErrBox (true, "Invalid texture ID [%c]", id);
}
return 0;
}
CTile:
Code:
// CTile.h
#pragma once
class CTile {
GLuint texture;
public:
CTile (void);
~CTile (void);
void SetTexture (char id);
void Draw ();
// Debug
GLuint Texture () const
{
return texture;
}
};
// CTile.cpp
#include "StdAfx.h"
#include ".\tile.h"
#include "TextureCache.h"
CTile::CTile (void)
{
}
CTile::~CTile (void)
{
}
// SetTexture
// Gets specified texture from the texture cache
void CTile::SetTexture (char id)
{
CTextureCache* tcache = new CTextureCache ();
texture = tcache->GetByID (id);
delete tcache;
}
// Draw
//
void CTile::Draw ()
{
glBindTexture (GL_TEXTURE_2D, texture);
glEnable (GL_TEXTURE_2D);
glBegin (GL_QUADS);
glTexCoord2i (1, 0);
glVertex2i (32, 0);
glTexCoord2i (1, 1);
glVertex2i (32, 32);
glTexCoord2i (0, 1);
glVertex2i (0, 32);
glTexCoord2i (0, 0);
glVertex2i (0, 0);
glEnd ();
glDisable (GL_TEXTURE_2D);
}