Advertisement

Animation in engine

Started by June 12, 2002 03:46 PM
1 comment, last by JinJo 22 years, 3 months ago
Im doing a simple 2d game engine in directx7 and im using directdraw and was wondering what would be a good way to do a general, reusable function/class to use for animation? Ive thought of for each object that needs animated i could have a variable for image height and width and a variable holding the objects bitmap which has a few different pictures that can be used as frames. the object also has an array or linked list(not ever done lists but they sound useful), that holds each frame (dont know if this is bad for memory) and a variable for current frame. Then when i want to animate the object i just pass the objects position and current frame to a function that draws this to the screen. This doesnt sound like something that can be reused though and just sounds a bit too simple. Is this ok or does anyone have a better idea?
What exactly is the array of? DirectDraw surfaces? frame indices? Plain ol'' bitmap data?

I''m not sure how you load your bitmaps, but the method I use is loading an entire template of bitmaps - one big bitmap with all the frames of animation drawn in table form.
_ _ _ _ _
|_|_|_|_|_| <-- in each box would be drawn a frame of animation
|_|_|_|_|_|
|_|_|_|_|_|

I load this large bitmap onto one surface. To get frame rectangles, I use this function:


  RECT GetSourceRect(int frame,  //frame index in table                   int frameswide,  //frames wide the template is                  int height, int width)  //dimensions of each frame{   RECT source;   source.top = (floor(frame/frameswide) * height) + floor(frames/frameswide);   source.bottom = source.top + height;   source.left = (frame%frameswide * width) + frame%frameswide;   source.right = source.left + width;   return source;}  


For source.top and source.left, the statement added at the end of the line accounts for the width of the white line border seperating frames in the bitmap template (1).
Advertisement
JinJo,

If I understand you correctly, your way would work reasonably well.

If you create a List, and each node of the list contains a frame of the animation, then you could, theoretically, animate the object by simply moving the iterator up the list each frame and returning the image array from each node to be drawn. This would take very near the same amount of memory as loading all of the frames into memory via a single bitmap.

The problems you MIGHT run into are when you want to transition from one animation to another in the middle of an animation... such as when you''re sprite is walking and somone hits the jump button.

You will have to either always start at the beginning of an animation sequence, or you''ll have to waste the time of iterating through the list until you get at the frame you want to start the animation with.

With an array as detailed in the post before, once you figure out which frame you want to start at, say, frame 5,you just jump to that memory address...with a list, you have to iterate through 4 other possible frames to get to the one you want...wastes a bit of time that could add up with long animations.

Best Regards,
Jeromy "Maverick" Walsh
------------------------
"The question isn''t how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" -Boondock Saints
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints

This topic is closed to new replies.

Advertisement