Archived

This topic is now archived and is closed to further replies.

2d DX game engine

This topic is 5624 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey everyone, I am currently creating a 2d graphics engine in DirectX 7. The way I currently have it set up is like this: I have a class called cObject which holds the info for every game object. Within the class is information like x,y coords, velocity, current_frame, etc. The problem I am having is with using different animations. I want to store the animations in arrays like so: anim_shoot[]={1,2,3,2,1}; etc... How would I go about setting the internal animation by loading these external arrays? Any tips on how you would achieve something like this are appreciated - Jesse Barksdale

Share this post


Link to post
Share on other sites
With a timer like QueryPerformanceCounter() or TimeGetTime
There''s a good DX 7 book which tells you how to animate your sprites

Author is Robert Dunlop (from MS).

Share this post


Link to post
Share on other sites

Quick outline:


  
class OBJECT
{
public:

...

OBJECT* lpNext;

...

}

#define NUM_OBJECTS 2;
OBJECT myObject[NUM_OBJECTS];
OBJECT* lpObject[NUM_OBJECTS];

for (int i = 0; i < NUM_OBJECTS; i++)
lpObject[i] = &myObject[i];

lpObject[1]->lpNext = lpObject[0];

...

if (currentTicks >= targetTicks)
{
lpObject = lpObject->lpNext;
}

...



Basically, just create your object, set your pointer, and if it''s time to change frames, just advance the pointer to the next frame in the animation.

That''s kind of quick and dirty, but it''s the best I can do on short notice.




DracosX:

Master of the General Protection Fault

Share this post


Link to post
Share on other sites
I've actually developed something like this for use in my games. Here's the header file to my classes:


    
enum ANIM_TYP {A_LOOP,A_BOUNCE,A_ONCE};

class CAnim
{
public:
int m_StartFrame;
int m_EndFrame;
ANIM_TYP m_AnimTyp;
int m_CurrFrame;
CAnim* m_Next;
CAnim* m_Prev;
int m_AnimNum;
};

class CAnimSet
{
public:
CAnimSet();
~CAnimSet();

CAnim* m_Animations;
int m_CurrAnimNum;
CAnim* m_HeadAnim;

BOOL AddAnim(CAnim Anim);
BOOL SetAnim(int Num);
int StepAnim();
};


Currently I don't have a method to easily setup the CAnim classes to be added to CAnimSet. But I'm working on a custom file type to go along with an editor to create the files while looking at the sprites.

Always remember, you''re unique. Just like everyone else.

[edited by - Greven on July 23, 2002 12:35:21 AM]

Share this post


Link to post
Share on other sites
I built a simple Animation structure that holds various information about a given animation, its name, number of frames, then an array of frame information. Each frame contained its number in the file (by rect), its rect, and its delay.

So for any given object, there would be an array of aninmations, and in each animation, there would be an arrary of frames. I would get the current frame from the list of animations, do my testing, like delay and so on, move to the next frame, and draw. I''m assuming this is a very standard way of doing things.

-----------------------------
kevin@mayday-anime.com
http://games.mayday-anime.com

Share this post


Link to post
Share on other sites
I assume the following things (since u said ur using DX7)
1. You have a single direct draw surface containing all the animation frames for a particular animation set.
2. Each frame in an animation is a RECT into this surface
3. You have a high resolution timer code already written (using either QueryPerformanceTimer or timeGetTime, NOT WM_TIMER).


Create an array (dynamic array, if loaded from a file) of RECT representing the animation frame information in a particular master SpriteSurface. Set the m_StartFrame and m_EndFrame to the index into these array. Update the m_CurrFrame according to the animation update rules (a simple Update function is given below). Draw the sprite using m_CurrFrame as an index to the RECT array and draw from the master spritesurface.

Also add these parameters to the CAnim class for a realtime animation speed.


  
class CAnim
{
public:
int m_StartFrame;
int m_EndFrame;
ANIM_TYP m_AnimTyp;
int m_CurrFrame;
CAnim* m_Next;
CAnim* m_Prev;
int m_AnimNum;

// Added members

float m_AnimFPS;
float m_FPSAcc;
void Update(float delta_t)
{
m_FPSAcc += delta_t;
if(m_FPSAcc >= m_AnimFPS)
{ // Time to change the frame

m_CurrFrame++;
if(m_CurrFrame > m_EndFrame)
{
// Do the necessary handling for diff.

// types of animation.

// right now assume, LOOP

m_CurrFrame = m_StartFrame;
}
m_FPSAcc = 0.0f;
}
}
};

// Some code

....
// Rendering

void Scene::Update()
{
float delta_t;
// Some code

...

m_pTimer->Update();
delta_t = m_pTimer->GetFrameTime();

// Some more code

...

m_pAnim[i]->Update(delta_t);

// Rest of the code

...
}


The member m_AnimFPS is the animation speed, i.e how long a particular frame should be played. This can be calculated directly from the frames per second of the animation. If an animation has to be played at 10 FPS and the animation has ''N'' frames, the m_AnimFPS = 1.0f / 10;
The member m_FPSAcc basically tracks the time elapsed for the current frame. If the time elapsed is greater than the time duration allowed for a frame, the frame is incremented (or decremented in case of BOUNCY).

OK! the name ''m_AnimFPS'' is not very creative, i''m sleepy, couldn''t think of any other one ...


hope this helps...if you already know this, then i probably did not understand ur query...maybe you can be more specific...


Share this post


Link to post
Share on other sites