sdl question

Started by
10 comments, last by nullsquared 18 years, 3 months ago
how do you display an animated image in SDL?
Advertisement
The same way you do it with any API: You display one image, then another that is a little different, then another one that is a little different still. (This is called page flipping).

To see how to display an image:
SDL doc
AFAIK there is no pre-build function for that, bu you can easily roll your own animation system.
The easiest way is to have your animation in separate images, load them into an array of surfaces and index into this array. The current frame would be identical to the array index.
In your game loop you would use a timer to decide when to switch to the next frame
(e.g. when to increment the index).
Once the index reaches the number of animation frames loaded into the array, you can either reset it to zero (loop), stop incrementing it (one-shot animation) or
invert the sign of the frame increment (ping-pong loop).

Once you have this working, you can put individual frames into a single surface (e.g. evenly spaced both horizontally and vertically) and index into the surface by calculating the source rectangle of the crequested frame within the surface.

// sample animation info structurestruct AnimationInfo {   int frameWidth;    // frame width in pixels   int frameHeight;   // frame height in pixels   int numFrames;     // number of frames in image   int currentFrame;  // currently displayed frame   int delayInTicks;  // number of ticks until frame skip   int currentTick;   // current tick number (a tick could be anything from ms to minutes - it's up to you to define it)   int LoopCount;     // loop count, 0 -> no loop, 1 -> repeat one time, -1 -> endless loop   int LoopIncrement; // loop increment, 1 -> forward, -1 -> backwards, 0 -> stop   SDL_Surface * frameData; // frame data};// calculate frame offset in surface from given frame numbervoid GetFrameRect( int frame, const AnimationInfo * animation, SDL_Rect * frameRect ) {     frameRect->x = (frame * animation->frameWidth) % animation->frameData->w;     frameRect->y = (frame * animation->frameWidth) / animation->frameData->w;     frameRect->w = animation->frameWidth;     frameRect->h = animation->frameHeight;}


HTH,
Pat.
I forogot to mention, with out them being in a animated format, there are over 150 of them (I have about 14 images I need to motion)

EDIT: or I could learn opengl
Quote:Original post by game mercenary
EDIT: or I could learn opengl

It's absolutely the same with OpenGL. There is no other way than animating the images yourself by displaying one after another.
Please be a little more specific about what you already have/know and what you need to know to implement this (other than how to call the SDL functions - this can be found in the docs and tutorials).

Quote:Original post by game mercenary
I forogot to mention, with out them being in a animated format, there are over 150 of them (I have about 14 images I need to motion)
You could have every frame held in one large image and then when you're rendering the animation, just loop through one by one displaying each frame as you go through the loop.
Rob Loach [Website] [Projects] [Contact]
collision detection owuldnt work if I did that, I have a different system (I will explain it when I have time)
Here is an idea. What if you created a seperate thread for each animation you create? They would all be able to update automaticaly if you had an internal timer going on each thread.
HxRender | Cornerstone SDL TutorialsCurrently picking on: Hedos, Programmer One
You dont really want to do that =)
Quote:Original post by Simian Man
The same way you do it with any API: You display one image, then another that is a little different, then another one that is a little different still. (This is called page flipping).


No, this is called animating. Page flipping is the method by which you prevent display flicker. First you draw the next screen of video in a section of memory that is not visible (the back buffer). Then once you're done, you either brute-force copy the back buffer to front buffer, or you instruct the display hardware to swap the back buffer and the front buffer. This is much faster than a brute-force copy because no data is being moved. This method is called page flipping.

Quote:Original post by PnP Bios
Here is an idea. What if you created a seperate thread for each animation you create? They would all be able to update automaticaly if you had an internal timer going on each thread.


No! Here's what I do (python):

# All time values are floating point values representing seconds or fractions thereofclass Frame:    def __init__(self, surface, duration):        self.surface = surface        self.duration = durationclass Animation:    def __init__(self):        self.frames = []        self.current_frame = None        self.frame_start_time        self.next_frame_time    def play(self, current_time):        self.current_frame = 0        f = self.frames[self.current_frame]        self.frame_start_time = current_time        self.next_frame_time = self.frame_start_time + f.duration        self.update(current_time)    def update(self, current_time):        if current_time >= self.next_frame_time:            self.current_frame += 1        if self.current_frame >= len(self.frames):            print "Animation done!"            self.current_frame = self.frames[len(self.frames) - 1]        else:            f = self.frames[self.current_frame]            self.frame_start_time = self.next_frame_time            self.next_frame_time += f.duration

This topic is closed to new replies.

Advertisement