Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Animation not displaying

Credits go to Kris Asick for helping out!
This thread is locked; no one can reply to it. rss feed Print
Animation not displaying
CursedTyrant
Member #7,080
April 2006
avatar

Does anyone see a reason why this code:

void CEnemy::Draw()
{
  rectfill(buffer, int(X-W/2), int(Y-H), int(X+W/2), int(Y), makecol(255, 0, 0));
  if (Move) { Move->SetPos(X-W/2, Y-H); Move->DrawFrame(0); }
}

wouldn't work? The rectfill is displayed, so the object exists and works fine (even moves :P), but CAnim *Move isn't displayed for some weird reason.

Here's some other stuff:

1//ENEMY.CPP
2 
3CEnemy::CEnemy(enum EEnemyType nType, float nX, float nY, float nDstX, float nDstY, enum EEnemyMoveType nMoveType)
4:Type(nType), X(nX), Y(nY), DstX(nDstX), DstY(nDstY), MoveType(nMoveType)
5{
6 Speed = 0.0f;
7 W = 64.0f; H = 64.0f;
8
9 if (Type==FIRE_FLAME) { Move = new CAnim(ANIM_ENEMY_FIRE_MOVE, X, Y, 64.0f, 5.0f); }
10 else { Move = 0; }
11}
12 
13//CUBE.CPP
14 
15CCube::CCube(enum ECubeType nType, float nX, float nY, float nW, float nH)
16:Type(nType), X(nX), Y(nY), W(nW), H(nH)
17{
18 if (Type==FIRE) { Destroy = new CAnim(ANIM_FIRECUBE_DESTROY, X, Y, 16.0f, 2.0f); }
19 else { Destroy = 0; }
20
21 Dead = false;
22}
23 
24void CCube::Draw()
25{
26 if (Destroy && !Dead) { Destroy->SetPos(X, Y); Destroy->DrawFrame(0); }
27 else if (Dead) { Destroy->SetPos(X, Y); Destroy->Play(); }
28}

1//ANIM.HPP
2 
3#ifndef ANIM_HPP
4#define ANIM_HPP
5 
6#include "globals.hpp"
7 
8class CAnim
9{
10public:
11 float X, Y, W, H, Size;
12 float Delay, DelayMax;
13 int Frames, CurFrame;
14
15 vector<BITMAP *> &ANIM;
16
17 void SetPos(float nX, float nY);
18
19 void DrawFrame(int Frame);
20 void Play();
21
22 CAnim(vector<BITMAP *> &nANIM, float nX, float nY, float nSize, float nDelayMax);
23 virtual ~CAnim();
24};
25 
26#endif
27 
28//ANIM.CPP
29 
30#include "anim.hpp"
31 
32CAnim::CAnim(vector<BITMAP *> &nANIM, float nX, float nY, float nSize, float nDelayMax)
33:ANIM(nANIM), X(nX), Y(nY), Size(nSize), DelayMax(nDelayMax)
34{
35 Delay = 0.0f;
36 W = 0.0f; H = 0.0f;
37
38 Frames = 0;
39 CurFrame = 0;
40
41 if (&ANIM && !ANIM.empty())
42 {
43 Frames = ANIM.size();
44 W = ANIM.front()->w; H = ANIM.front()->h;
45 }
46}
47 
48CAnim::~CAnim()
49{
50 X = 0; Y = 0;
51 Size = 0;
52 Delay = 0; DelayMax = 0;
53 Frames = 0; CurFrame = 0;
54}
55 
56void CAnim::SetPos(float nX, float nY)
57{
58 X = nX; Y = nY;
59}
60 
61void CAnim::DrawFrame(int Frame)
62{
63 CurFrame = Frame;
64 if (&ANIM[CurFrame]) { masked_blit(ANIM[CurFrame], buffer, 0, 0, int(X), int(Y), int(W), int(H)); }
65}
66void CAnim::Play()
67{
68 if (&ANIM[CurFrame]) { masked_blit(ANIM[CurFrame], buffer, 0, 0, int(X), int(Y), int(W), int(H)); }
69
70 if (Delay>=DelayMax)
71 {
72 CurFrame++; if (CurFrame>=ANIM.size()) { CurFrame = 0; }
73 Delay = 0.0f;
74 }
75 else { Delay += 1.0f; }
76}

If I do this in main:

CAnim *anim = new CAnim(ANIM_ENEMY_FIRE_MOVE, 300, SCREEN_H-160, 64, 5.0f);
...
//game loop

if (anim) { anim->Play(); }

//game loop
...

Note that both enemy.cpp and main.cpp use the same global ANIM_ENEMY_FIRE_MOVE, and while it works fine in main and in cube.cpp, it does not work in enemy.cpp.

It's probably something stupid, but I just can't see it.

---------
Signature.
----
[My Website] | [My YouTube Channel]

Kris Asick
Member #1,424
July 2001

I think you're simply forgetting to call the Play() function. ;)

If that's not it, I'll take a closer look, but that's the only difference I saw at first glance.

--- Kris Asick (Gemini)
--- http://www.pixelships.com

--- Kris Asick (Gemini)
--- http://www.pixelships.com

CursedTyrant
Member #7,080
April 2006
avatar

Quote:

I think you're simply forgetting to call the Play() function.

DrawFrame() does the same as Play(), and they both don't work in enemy.cpp, but work perfectly fine elsewhere. Frame 0 is not empty.

---------
Signature.
----
[My Website] | [My YouTube Channel]

Kris Asick
Member #1,424
July 2001

Try setting up copy constructors for your classes and see if that helps. The only thing I can think of at the moment is that you're losing data because it's not being reallocated properly when you add items to your vectors. (I found out while first using vectors a couple months ago that certain allocations of memory might be lost, especially with Allegro objects, if you don't manually handle them in a copy constructor when using vectors of objects instead of vectors of pointers, but without seeing more code I can only guess if this is the case.)

--- Kris Asick (Gemini)
--- http://www.pixelships.com

--- Kris Asick (Gemini)
--- http://www.pixelships.com

CursedTyrant
Member #7,080
April 2006
avatar

Would this be enough?

---------
Signature.
----
[My Website] | [My YouTube Channel]

Kris Asick
Member #1,424
July 2001

Well, I get an invalid page fault when I try to run your program so something's up, and it's likely related to memory or vector handling.

EDIT: Actually, the crash was my bad. I fixed it and got the program running. I see what you mean now... let's see... continues messing with it

EDIT: You're gonna kick yourself when I tell you what the problem is... ;D

You are loading the enemy before you load the animations the enemy runs off of. As a result, when the enemy sets up its animation, it creates an empty (but still valid) animation object. Since the object exists, the Move object points somewhere, but it doesn't have anything in it because the animation that was supposed to go into it wasn't loaded when the enemy was created!

Move the line in your initialization routine that creates the enemy to after the animation loading and you should be fine. ;)

--- Kris Asick (Gemini)
--- http://www.pixelships.com

--- Kris Asick (Gemini)
--- http://www.pixelships.com

CursedTyrant
Member #7,080
April 2006
avatar

Quote:

EDIT: You're gonna kick yourself when I tell you what the problem is... ;D

Aye... This was a stupid mistake... I couldn't see it tough, and I've tried for a day :P or so. I guess I was looking in the wrong place.

Thanks for your help, I could've never found the problem without it :P

---------
Signature.
----
[My Website] | [My YouTube Channel]

Go to: