so i'm currently writing some scripts to handle images, anyway, this script is supposed to load a cell from a larger image (a spritesheet)
anyway, there's a problem in it, it's only loading a white square, and i was hoping someone could check through my code
the script to get the sprite from the spritesheet
GLuint g_spritesheet::getsprite(unsigned short x,unsigned short y)
{
GLubyte* dater;
dater=(GLubyte*) malloc((gridsize*gridsize)*4);
if (x>0 && y>0 && (x+1) < (w/gridsize) && (y+1) < (h/gridsize)) //if within the thingo
{
int i;
for (i = 0; i<((gridsize*gridsize)*4);i++)
{
short xadd,yadd;
xadd=i-(((int) floor((float) i/gridsize)*gridsize)*4);
yadd=(int) floor((float) i/gridsize)*w;
int res=(((((y*gridsize)-1)*w)*4)+x*gridsize)+xadd+yadd;
dater=data[res];
}
}
GLuint tex;
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1,&tex);
glBindTexture(GL_TEXTURE_2D,tex);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, 4, gridsize, gridsize, 0,GL_RGBA, GL_UNSIGNED_BYTE, dater);
free(dater);
return tex;
}
A typical cause of that symptom is that you try to load the textures before the rendering context is created.
As a tip though, you can get rid of all that sprite extraction code and temporary storage just to create a texture from a sprite within the sprite sheet by taking advantage of the unpack parameters. Just make sure you're restoring the unpack parameters after loading all all sprites from the sheet (or just restore them on a per-sprite basis).
[source]
GLuint g_spritesheet::getsprite(unsigned short x,unsigned short y)
{
glPixelStorei(GL_UNPACK_SKIP_PIXELS, x*gridsize);
glPixelStorei(GL_UNPACK_SKIP_ROWS, y*gridsize);
glPixelStorei(GL_UNPACK_ROW_LENGTH, w);
When taking the image data from ilGetData, you need to copy the data pointed to by the pointer, not copy the pointer value. What you do is allocate a memory block for the image, reset the pointer to point to the image, and delete the image. The allocated memory is lost, and the pointer is pointing to a deleted image.
so i have a new problem now, getting a sprite from a spritesheet causes some corruption
the blue square in first image represents the section that is being loaded (not the whole square, but the first 48x48 chunk of it)
the second image shows what is being drawn afterwards, as you can see there is some corruption at the top of the image
here is the new code that i'm using
GLuint g_spritesheet::getsprite(unsigned short x,unsigned short y)
{
GLubyte* dater;
dater=(GLubyte*) malloc((gridsize*gridsize)*4);
if (x>0 && y>0 && (x+1) < (w/gridsize) && (y+1) < (h/gridsize)) //if within the thingo
{
int i =0;
while (i<((gridsize*gridsize)))
{
int cel=(w*((y*gridsize)+(floor((float)i/gridsize))))+(x*gridsize)+(i % gridsize);
char e = 0;
while (e<=3)
{
dater[(i*4)+e]=data[(cel*4)+e];
e+=1;
}
dater=data[cel];
i++;
}
}
I'd run nested loops over the appropriate range of x and y coordinates and range-check the minimum and maximum values instead of doing bizarre tricks with a single index that starts from 0.