Advertisement

Sprites, and good ways to implement them.

Started by June 08, 2005 04:18 PM
19 comments, last by MrLeap 19 years, 8 months ago
here is a break down of the arguements and what they mean:


LoadTextureColorKey(const char *filename, unsigned char key[], unsigned int& id)

const char *filename
you can pass the file name like this "sprite.bmp" or a variable array of type char that contains the file name

unsigned char key[]
a three value array for the transparent color: red, green, blue
you can create an array like this and pass it as Key:
char Key[3] = {255,0,255};

unsigned int& id
this is where the texture is stored so that you can use it with glBindTexture to draw it and should be a variable declared as UINT

so here is what your function call and declarations would look like:

UINT SpriteTextures[2];
char Key[3] = {255,0,255};

LoadTextureColorKey("sprite.bmp", Key, SpriteTextures[0]);
When the world can't make you smile, program one that can
RedDragon0509 gave a perfect explanation of the function arguments :)

-DavidR-
In this world gone mad, we won't spank the monkey; the monkey will spank us.
Advertisement
Thank you Red dragon. (at this rate, my readme is going to be larger than my exe XD)

Got it to compile with no errors, but it doesn't ..uhm, make all the pixels that are 255,0,255 go away, i'm trying to track down the problem right now. It does however place the texturemapped polygon! So atleast part of the function is being recognized :D
Yeah I can't get that to work :(
I know it's due to my incompatence though. Maybe it's a problem with the texture files i'm using again. I would assume my 24 bit bmp pictures are a-o-k though, as they're DISPLAYED as-per-the function, the color just isn't being.. uh.. "keyed" and "apha channel'd" :p I hate to put the burden of my failure on all of you, but if you're not as clervoyent as DavidR, then my source is hosted here:

http://joshleap.dnsalias.com/game/


On a completely unrelated note: I'm trying to do collision detection. I'm running off of the openGL tiling tut on this website ;). I can't come up with a way to do collision detection with the things I know already. What I need to do is find a way to check the position of my sprite that I dont want noclipping with the x+y coordinates of my '1' entries in my tile map array. It's probably really simple, I just can't figure out how. The array doesn't store the location of the blocks, it just is run through a nested for loop. Anyone want to point me in the right direction?
I looked at your code, and one thing I noticed is that you're loading your texture every single frame. Just load it one time in InitGL(). Also, you're loading your pirate image (the one you want color-keyed) in load_gl_textures() (which isnt using color keying).

int load_gl_textures(){  AUX_RGBImageRec *TextureImage[2];  memset(TextureImage,0,sizeof(void *)*1);  TextureImage[0] = loadbmp("data/tile0.bmp");  TextureImage[1] = loadbmp("data/tile1.bmp");  // LoadTextureColorKey takes care of generating and binding,  // as well as memory deallocation.  LoadTextureColorKey("data/pirate.bmp", key, texture[2]);  glGenTextures(2, &texture[0]); // We are generating two textures  glBindTexture(GL_TEXTURE_2D, texture[0]);  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, TextureImage[0]->sizeX,               TextureImage[0]->sizeY, 0, GL_RGB,               GL_UNSIGNED_BYTE, TextureImage[0]->data);  glBindTexture(GL_TEXTURE_2D, texture[1]);  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, TextureImage[1]->sizeX,               TextureImage[1]->sizeY, 0, GL_RGB,               GL_UNSIGNED_BYTE, TextureImage[1]->data);   // notice I changed this to 2   for (int t = 0; t < 2; t++)  {    if (TextureImage[t])    {      if (TextureImage[t]->data)      {        free(TextureImage[t]->data);      }      free(TextureImage[t]);    }  }  return(1);}


updated piratedraw function:
void piratedraw(GLvoid){	glBindTexture(GL_TEXTURE_2D, texture[2]);		glBegin(GL_QUADS);	// Front Face	glTexCoord2f(0.0f,0.0f); glVertex3f(-0.5f, -0.5f,  1.0f);        // Bottom Left Of The Texture and Quad	glTexCoord2f(1.0f,0.0f); glVertex3f( 0.5f, -0.5f,  1.0f);        // Bottom Right Of The Texture and Quad	glTexCoord2f(1.0f,1.0f); glVertex3f( 0.5f,  0.5f,  1.0f);        // Top Right Of The Texture and Quad	glTexCoord2f(0.0f,1.0f); glVertex3f(-0.5f,  0.5f,  1.0f);	glEnd();}


Hope that helps!

-DavidR-
In this world gone mad, we won't spank the monkey; the monkey will spank us.
DavidR, thank you again :D,

Apparently, my incompetance knows no bounds.


This is boggling my mind, colorkeying still isn't doing it's thing :P I'm beginning to think it's my texture, so i've placed a copy of it in the same directory you got my code from last time ;) along with a copy of the code modified as per your help. (http://joshleap.dnsalias.com/game/)

Again, my appreciation for ALL of the help I'm getting is boundless.
Advertisement
the 255,0,255 color isnt transparent because you need to add: glEnable(GL_BLEND);
to the InitGL function
When the world can't make you smile, program one that can
lol every post gets me so excited. This has got to work some time, I mean.. Seriously :)

(blend is now enabled, but blend is not uhh.. happening yet ;))

sorry forgot that you must also add(in InitGL):
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
When the world can't make you smile, program one that can
Yeah, I forgot to mention to enable blending ;)
Before you draw your color-keyed bitmap, you need to enable blending and set the blending equation.

glEnable(GL_BLEND);glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);// Drawing function hereglDisable(GL_BLEND);

MrLeap, just think of us helping you as a way to improve your programming skills faster than if you sat around by yourself struggling to learn this material :)

-DavidR-
In this world gone mad, we won't spank the monkey; the monkey will spank us.

This topic is closed to new replies.

Advertisement