Loading up a PNG in SDL->OpenGL

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Loading up a PNG in SDL->OpenGL

Post 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?
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Post 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.
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Post 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.
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Post 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?
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Post 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.
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Post by Falco Girgis »

DAMN. That looks great. 0_o
User avatar
Arce
Jealous Self-Righteous Prick
Jealous Self-Righteous Prick
Posts: 2153
Joined: Mon Jul 10, 2006 9:29 pm

Post by Arce »

Looks kewl. Is movement like Golden Axe?
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Post by JS Lemming »

I've never played Golden Ax but from the looks of it, yeah, kinda.
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
Post Reply