Page 1 of 1

Loading up a PNG in SDL->OpenGL

Posted: Thu Dec 20, 2007 7:46 pm
by JS Lemming
I keep getting a black box of annoyance. I've done this before in SDL but is has been ages and I can't find my code.

I was browsing the internets for answers and found this little function that is supposed to do it:

Code: Select all

GLuint loadTexture(const char* file)
{
   SDL_Surface* surface = IMG_Load(file);
   GLuint texture;
   glPixelStorei(GL_UNPACK_ALIGNMENT,4);
   glGenTextures(1, &texture);
   glBindTexture(GL_TEXTURE_2D, texture);
   SDL_PixelFormat *format = surface->format;
   if (format->Amask)
   {
      gluBuild2DMipmaps(GL_TEXTURE_2D, 4,
         surface->w, surface->h, GL_RGBA,GL_UNSIGNED_BYTE, surface->pixels);
   }
   else
   {
      gluBuild2DMipmaps(GL_TEXTURE_2D, 3,
         surface->w, surface->h, GL_RGB, GL_UNSIGNED_BYTE, surface->pixels);
   }
   SDL_FreeSurface(surface);
   return texture;
}
But... it looks iffy for some reason. Hey Gyro, I assume you're loading pngs for your game. Mind if I peek at the png loading texture code you're using?

Posted: Thu Dec 20, 2007 8:38 pm
by Falco Girgis
Oh my god, it was such a pain in the ass, dude. There's like no documentation.

I used lib glpng from here: http://www.fifi.org/doc/libglpng-dev/glpng.html.

In all of my screwing around, it is by far the easiest to use.

Code: Select all

 id = pngBind("/pc/filename.png", PNG_NOMIPMAP, PNG_ALPHA, &info, GL_CLAMP, GL_NEAREST, GL_NEAREST)
That's what I use to load the texture. id is now the texture ID of the PNG.

Here's my drawing code:

Code: Select all

void SpriteSheet::DrawPNG(float x, float y, float z, float w, float h, int uv) {
	glLoadIdentity();

	glEnable(GL_ALPHA_TEST);
	glAlphaFunc(GL_GREATER, 0.0);
	glEnable( GL_TEXTURE_2D );
    glTranslatef(x, y, z);
	glBindTexture(GL_TEXTURE_2D, id);
    glBegin(GL_QUADS);

        glTexCoord2f(Coord[uv].u1, Coord[uv].v1); glVertex3f(0, 0, z);
        glTexCoord2f(Coord[uv].u2, Coord[uv].v1); glVertex3f(sp_w, 0, z);
        glTexCoord2f(Coord[uv].u2, Coord[uv].v2); glVertex3f(sp_w, sp_h, z);
        glTexCoord2f(Coord[uv].u1, Coord[uv].v2); glVertex3f(0, sp_h, z);

    glEnd();
}
That should load and draw a PNG with enabled transparency. If you need anything else, or if it doesn't work, just let me know. It was a complete pain in the ASS to get to work. If you want, I can post my PNG spritesheet splitting/rendering class.

Posted: Thu Dec 20, 2007 11:29 pm
by JS Lemming
Dear god it works. Thanks Gyro, you saved my ass an ass load of time.

I'm gonna end up having to write some crazy custom class for my sprites cause they're going to be animated skeletons with textured skin parts but it'd still be koo to see how you went about yours.

Posted: Sat Dec 22, 2007 2:01 am
by Falco Girgis
Yeah man, no problem. I'll give you anything I have. We're still "The Chaos Team," haha.

Really? What are you up to nowadays? The main point of interest in my sprite sheet code is how I made it sorta cross platform. Is this like an OGL only project?

Posted: Sat Dec 22, 2007 1:33 pm
by JS Lemming
Oh my old comrade and I were reminiscing the other day about Chainsaw Rally 5 and I had a sudden urge to give it another try. We came up with some new idears for it to make it better and such. We also decided for it to be PC only. Even though deep down I still secretly love the Dreamcast and all that it represents... however, game input is incredibly important to me. And I really hate the DC's controller. If for some impossible reason the Wii ever became practically brewable I'd mount that thing and hump it dry... but that won't happen.

This Chainsaw Rally 5 thing still has the same old problems.... mainly, my friend has very little computer skills so I will have to do the entire thing myself (and we all know how that ends up). Including art, like this old mock up I did long ago for a hotel hall:

Image

He doesn't have photoshop or gimp know-how to make things. So that's that.

Posted: Sat Dec 22, 2007 4:47 pm
by Falco Girgis
DAMN. That looks great. 0_o

Posted: Sat Dec 22, 2007 6:42 pm
by Arce
Looks kewl. Is movement like Golden Axe?

Posted: Sat Dec 22, 2007 10:48 pm
by JS Lemming
I've never played Golden Ax but from the looks of it, yeah, kinda.