Sprite Sheets

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andrew Davie

Sprite sheets are animations for a game stored in a single image.
We did NOT use these back in the days when I was a professional developer; all our graphics were done on custom animation tools and output in custom formats.

Useful low-res page of Lemmings Sprites..
SMS

Andrew Davie

I'm at the point where I need a tool to convert the sprites from the sprite sheets into my sprite data format. Data is 4 bytes per line; the first bite is a mask (basically the following 3 bytes OR'd together). I do it this way to shave just a little time off the draw routine, though if memory becomes tight I can dump this byte. The next 3 bytes are effectively the 3 planes for the 8-colour image set of 8 pixels for the line. Consider this "R-G-B" although conceptually it's any 3 colours which, combined, give the 8 colours.
Now I have a sprite sheet; the first thing to do is programatically separate all those frames into individual images, recording their x,y position via filenames.
At some point I'm going to give those frames "names" (such as "WALK1", "WALK2" etc). Those names will be used to access the frames in animation lists in a human-readable format.
The frame-set in the game needs each frame to be entered in a table, in the following format

{ height, centerX, centerY, pointer to data}

The height is because the sprite format is variable-height, so we can optimise things by only having the data we need. Also, we can if we want have super big sprites.  Height comes directly from the frame size, so that's easy. 

centerX and centerY define the "locked" point for a sprite. No matter what frame is displayed, this particular point in any frame will display in the same position on screen. That is, if a lemming is not moving, we can draw ANY frame we want, and the 'centerX' and 'centerY' pixel of that frame will be in the same place onscreen.  I cannot derive these points from the graphics data programatically, so these will need to be entered manually.  But as a starting point, we can assume that the middle of mass at bottom of sprite is where it most likely is, so we'll use that as our default.

That's about it for now.  So, tasks...

1) separate out the individual frames from the sprite sheet.  Mostly done; needs refinement. Note, the top-left pixel position of each culled frame is embedded in each output frame filename, for unique ID. e.g., 'frame_test_4_153.jpg' says that the frame came from the 'test.jpg' image (or whatever extension), its top-left corner is at x=4, y=153 (top left is 0,0).  Also output a template association file (used in step 3), just listing all of the frame names, and default centerX and centerY.  so it will have something like 'FRAME_245, frame_test_4_153, 5, 15' -- where the format is as described in #3.  If there's an existing association file, use that instead of overwriting - just add to it as required.
1a) we may want/need to reallocate colours -- that is, we only have 8, so it might be nice to specify "blue" is colour 3, etc. This may be easier than having a palette reduction on the source image. I'll think about this.
2) Cull the garbage frames, leaving us with a decent 'frame set'
3) create a text file using the names of the frames and associate with a label (e.g., 'WALK1'). The labels will be distinct. Also (optionally) specify a center point.  So, the line will contain multiple lines like this  'WALK1, frame_test_4_153, 4, 15' -- essentially the frame name, the filename of the image, the centerX, centerY
4) write a tool to process this text file and create C-tables and defines to fully specify the frame set.  The C-tables will have a frameset, and individual frame data, the frame names and their indexes into the frameset (i.e, #define FRAME_WALK1 23) or an enum or const int if that works out easier. Same things really.
5) Integrate to makefile and have it automatically build/process the source image if there are any changes.

The eventual requirement:  make a change to a frame in the source sprite sheet (say, 'test.jpg') and have it automatically propogate down into modified source code which ... just works.
So, that's going to keep me busy for a few days.

Andrew Davie

#2
It's now 4 hours later. I haven't been working full-on for 4 hours. Maybe 2.
Anyway, it's 90% done and functional.  The tool takes any number of input image files, automatically segments them into frames, removes duplicates, loads a manifest file to see if the frame has already been given center points and frame name. Then updates the manifest file as required, writes source .c and .h files, and does the colour conversions while it's at it.

I have the gut feeling that thinking carefully beforehand exactly what I wanted/needed, and writing it down in a previous post, made the whole process so much faster. I had a clear goal with clear steps, and went about solving those one by one.

The colours are probably wrong, but for a first pass good enough. But everything else appears to work. Outstanding. I'm impressed, actually!  Here's a first video just running randomly through the 48 available frames that I ran through the processor. This went MUCH more quickly than I expeced.  Couple of hours instead of several days.


Andrew Davie

#3
Here are the sprite sheets I fed it. I split out frames from the original sheet into several just to make it more apparent what the frames were...


Andrew Davie

#4
Here's a single lemming cycling through the frames.
Not bad. There are many more frames to come; this was just a first pass to see if the tool works.
I literally change a pixel in the graphics, or add a new frame... and hit build, and it's there in the game and perfect. Just what I wanted.


Andrew Davie

#5
Now I have the frames, I can start to "name" them. In the manifest file, you can add a name - which is effectively an enum that I can use in the code. If you name a frame in the manifest, that name 'sticks' and is not replaced. So I can slowly go through them and give them meaningful names (such as UMBRELLA_0, UMBRELLA_1, ... etc).  Once I have names I can start to think about meaningful animation sequences and get an animation system going.

Here's the umbrella sequence playing. Quite nice.


Andrew Davie

#6
A binary to end the day.
I found/fixed the sprite glitch that occasionally showed up mid-screen at the top.