Search code examples
c++openglsdltexturessprite-sheet

Sprite Sheet With OpenGL and SDL


I been working in a new game, and finally reached the point where I started to code the motion of my main character but I have a doubt about how do that.

Previously, I make two games in Allegro, so the spritesheets are kind of easy to implement, because I establish the frame and position on the image, and save every frame in a different bitmap, but I know that do that with OpenGL it's not neccesary and cost a little bit more.

So, I been thinking in how save my spritesheet and used in my program and I have only one idea.

I loaded the image and transformed in a texture, in my function that help me animate I simply grab a portion of the texture to draw instead of store every single texture in my program.

This is the best way to do that?

Thanks beforehand for the help.


Solution

  • You're on the right track.

    Things to consider:

    • leave enough dead space around each sprite so that the video card does not blend in texels from adjacent sprites at small scales.
    • set texture min/mag filtering appropriately. GL_NEAREST is OK if you're going for the blocky look.
    • if you want to be fancy and save some texture memory, there's no reason that the sprites have to be laid out in a regular grid. Smaller sprites can be packed closer in the texture.
    • if your sprites are being rendered from 3D models, you could output normal & displacement maps from the model into another texture, then combine them in a fragment shader for some awesome lighting and self-shadowing.