Problem with spritesheet/cells

Started by
4 comments, last by LorenzoGatti 12 years, 2 months ago
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;
}


structure of spritesheet

class g_spritesheet
{

unsigned int w;
unsigned int h;
GLubyte *data;
bool loaded;
public:
unsigned int gridsize;
g_spritesheet(){loaded=0;};
g_spritesheet(char*,GLuint);
g_spritesheet(char*);
void load (char*,GLuint);
GLuint getsprite(unsigned short,unsigned short);
GLuint getsprite(unsigned short,unsigned short,unsigned short,unsigned short);
};


structure of texture/sprite

class g_texture
{
GLuint ID;
GLuint tex;
int w, h;
public:
bool loaded;
g_texture()
{
tex=0; w=0; h=1; loaded=0;
}
g_texture(char*);
void load(char*);
void load(g_spritesheet,GLuint,GLuint);
void draw(int,int); //int x, int y
};


any help appreciated, i know the spritesheet loadss a working image, the problem is when i try to take a sprite from the spritesheet
Advertisement
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);

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, data);
return tex;
}
[/source]
Odd, now it seems as though my spritesheet is losing its data somehow between the time it's loaded and it attempts to take a cell from it

this is what i'm using to load data into the spritesheet

void g_spritesheet::load(char* filename,GLuint cellsize)
{
gridsize=cellsize;
loaded=0;
ILuint iltex;
ilGenImages(1, &iltex);
ilBindImage(iltex);
bool success=0;
success=ilLoadImage(filename);
if (success)
{
success = ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
if (!success)
{
printf("error converting: ");
printf((char*)iluErrorString( ilGetError()));
printf("\n");
}
else
{
loaded=1;
}
}
else
{
printf("error: ");
printf((char*)iluErrorString( ilGetError()));
printf("\n");
}
if (loaded)
{
w=ilGetInteger(IL_IMAGE_WIDTH);
h=ilGetInteger(IL_IMAGE_HEIGHT);
data=(GLubyte*) malloc((w*h)*4);
data=ilGetData();
//printf((char*)data);
ilDeleteImage(iltex);
}
}

and from debugging, it shows it does load data, but when it tries to use it in the getsprite function, it only has one character in it.
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

JwOfx.jpg
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++;
}
}

printf((char*)data);
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;
}


help is appreciated
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.

Omae Wa Mou Shindeiru

This topic is closed to new replies.

Advertisement