Animation in SDL

Heya everyone, got a new problem :slight_smile:

In SDL, how do I go about doing animations? I know that I can obviously load
5 different files for 5 animations of one object, but that gets to be a pain
in the butt, especially after you have 5 animations for walking, jumping,
crouching, shooting, etc… Too many files.

How do I create just 1 .bmp file with ALL the animations in it? Split up in
to blocks of course, then blit according to the frame I am on?

Thanks for any help!
-Bryan Arant

Yes… Just use different source SDL_Rect’s for each frame.

-bill!On Mon, Nov 12, 2001 at 09:56:03PM -0800, Bryan Arant wrote:

Heya everyone, got a new problem :slight_smile:

In SDL, how do I go about doing animations? I know that I can obviously load
5 different files for 5 animations of one object, but that gets to be a pain
in the butt, especially after you have 5 animations for walking, jumping,
crouching, shooting, etc… Too many files.

How do I create just 1 .bmp file with ALL the animations in it? Split up in
to blocks of course, then blit according to the frame I am on?

on 11/12/01 11:56 PM, Bryan Arant at bryan at barant.com wrote:

Heya everyone, got a new problem :slight_smile:

In SDL, how do I go about doing animations? I know that I can obviously load
5 different files for 5 animations of one object, but that gets to be a pain
in the butt, especially after you have 5 animations for walking, jumping,
crouching, shooting, etc… Too many files.

How do I create just 1 .bmp file with ALL the animations in it? Split up in
to blocks of course, then blit according to the frame I am on?

This is how I go about it…

Make one bmp that is a multiple of your sprite’s size. Say your sprite is
50x50 pixels. make a bmp that’s 50 by 400 or 200 by 800, whatever, then you
can divide that file into 50 x 50 rows and/or columns, each being a frame of
animation.

I usually add in an additional pixel of width/height between each region so
I can draw in borders to help me know where each region ends when I’m
drawing the sprites. You have to account for this when blitting.

then when you go to blit, grab one region out of the file with your
SDL_Rect.

SDL_Surface* sprite = SDL_LoadBMP("./sprite.bmp");
//etc

int column, frame_in_column;
//determine these values based on what’s going on in the game

SDL_Rect src, dest;

src.x = 50 * column;
src.y = 50 * frame_in_column;
src.w = 50;
src.h = 50;

dest.x = //somewhere on the screen
dest.y = //somewhere on the screen
dest.w = 50;
dest.h = 50;

SDL_BlitSurface(sprite, &src, screen, &dest);

so if you want the frame that is 2 over and 4 down, column would equal 1 and
frame_in_column would equal 3;

Matt_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com

You could have a look at the Spitfire Engine, used in Kobo Deluxe:

home:		http://olofson.net/skobo/

some snapshots:	http://olofson.net/download/

Or grab the (older) “enginetest” from:

http://olofson.net/mixed.html

(The interesting code is in sprite.c, while the C++ interface to the
whole engine is en gfxengine.C.)

There are various methods of loading images, splitting them up into
individual SDL surfaces in display format, and arranging them in a
container/bank/frame hierarchy.

The later versions (in Kobo Deluxe) also include a modular filter system
for processing of graphics while loading. (Based on a global "pipeline"
of plugins with arguments - throw the plugins in, and then load data as
usual to get it all processed.) This is how filtered scaling, alpha
cleaning w/ contrast + offset, “fuzzy colorkey” etc is done in Kobo
Deluxe.

//David Olofson — Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |-------------------------------------> http://olofson.net -'On Tuesday 13 November 2001 06:56, Bryan Arant wrote:

Heya everyone, got a new problem :slight_smile:

In SDL, how do I go about doing animations? I know that I can obviously
load 5 different files for 5 animations of one object, but that gets to
be a pain in the butt, especially after you have 5 animations for
walking, jumping, crouching, shooting, etc… Too many files.

How do I create just 1 .bmp file with ALL the animations in it? Split
up in to blocks of course, then blit according to the frame I am on?

If you are going to display sequences of such images
in this fashion you should allocate an array of source
SDL_Rect’s and calcuate the source once for each
frame. Then select which

SDL_Rect* frame = new SDL_Rect[number_of_frames_of_animation];

int i = number_of_frames_of_animation;
while (i–)
{
frame[i].x = blah…blah…blah…
…
}

animate = number_of_frames_of_animation;
while (animate–)
{
SDL_BlitSurface(
sprite,
&frame[ animate ],
screen,
&dest );
handle_your_events();
}

I highly recommend populating the frame array in reverse
and counting down to zero in this fashion. The post
decrement handles the translation from humane numbers
to “numbers only a computer programmer could love” :)On Tue, Nov 13, 2001 at 09:31:20AM -0600, Matt Greer wrote:

SDL_Rect src, dest;
src.x = 50 * column;
src.y = 50 * frame_in_column;
src.w = 50;
src.h = 50;

dest.x = //somewhere on the screen
dest.y = //somewhere on the screen
dest.w = 50;
dest.h = 50;

SDL_BlitSurface(sprite, &src, screen, &dest);

Dave Goehrig wrote:

If you are going to display sequences of such images
in this fashion you should allocate an array of source
SDL_Rect’s and calcuate the source once for each
frame. Then select which

having separate surfaces for each image makes RLE blits work better

I highly recommend populating the frame array in reverse
and counting down to zero in this fashion.

there is no reason for that

Sure there is. It is a coding practice which helps reduce
overflow bugs, and make it easier to refactor your code into
recursive routines. It also helps break C programmers of
their incremental bais…On Tue, Nov 13, 2001 at 06:06:02PM +0100, Mattias Engdeg?rd wrote:

I highly recommend populating the frame array in reverse
and counting down to zero in this fashion.

there is no reason for that