Jump to content

  • Log In with Google      Sign In   
  • Create Account

How do I create a function to make animating easier and more readable?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 black_darkness   Members   -  Reputation: 280

Like
0Likes
Like

Posted 23 December 2012 - 03:38 PM

Hello. I am having a problem, I need to animate a lot of things but even after providing only 2 animations (Walking, and swinging a hoe) my code is getting hard to maintain with variables all over the place.

 

If you would like to see the animation here is a release of the game. https://www.dropbox.com/sh/cu1rgxprt2pdo48/hDGkZe3cAL

press right control button to swing hoe.

 

Entity move_hero (Entity hero, ALLEGRO_BITMAP* tileset,vector<vector<Tile>> mv,int height,int width) {
    //constants
    //tw means tile width.
    int tw = 16;
    //al_draw_bitmap_region needs tileset, source x, source y, size of x, size of y, where to display on x axis, and where to display on y axis.
    if(hero.move_animation==false) {
        if(hero.is_swing_hoe == true) {
            switch (hero.facing) {
            case 'u':
                if(hero.sta_frame<=9) {
                    al_draw_bitmap_region(tileset, 0*tw, 17*tw, tw, tw, 15*tw, 15*tw, 0);
                    al_draw_bitmap_region(tileset, 0*tw, 16*tw, tw, tw, 15*tw, 14*tw, 0);
                }
                else if(hero.sta_frame<=19) {
                    al_draw_bitmap_region(tileset, 1*tw, 17*tw, tw, tw, 15*tw, 15*tw, 0);
                    al_draw_bitmap_region(tileset, 1*tw, 16*tw, tw, tw, 15*tw, 14*tw, 0);
                }
                else if(hero.sta_frame<=29) {
                    al_draw_bitmap_region(tileset, 2*tw, 17*tw, tw, tw, 15*tw, 15*tw, 0);
                    al_draw_bitmap_region(tileset, 2*tw, 16*tw, tw, tw, 15*tw, 14*tw, 0);
                }
                else if (hero.sta_frame <= 30) {
                    al_draw_bitmap_region(tileset, 2*tw, 17*tw, tw, tw, 15*tw, 15*tw, 0);
                    al_draw_bitmap_region(tileset, 2*tw, 16*tw, tw, tw, 15*tw, 14*tw, 0);
                    hero.is_swing_hoe = false;
                }
                break;
            case 'd' :
                if(hero.sta_frame<=9) {
                    al_draw_bitmap_region(tileset, 0*tw, 13*tw, tw, tw, 15*tw, 14*tw, 0);
                    al_draw_bitmap_region(tileset, 0*tw, 14*tw, tw, tw, 15*tw, 15*tw, 0);
                }
                else if(hero.sta_frame<=19) {
                    al_draw_bitmap_region(tileset, 1*tw, 13*tw, tw, tw, 15*tw, 14*tw, 0);
                    al_draw_bitmap_region(tileset, 1*tw, 14*tw, tw, tw, 15*tw, 15*tw, 0);
                }
                else if(hero.sta_frame<=29) {
                    al_draw_bitmap_region(tileset, 2*tw, 13*tw, tw, tw, 15*tw, 14*tw, 0);
                    al_draw_bitmap_region(tileset, 2*tw, 14*tw, tw, tw, 15*tw, 15*tw, 0);
                    al_draw_bitmap_region(tileset, 2*tw, 15*tw, tw, tw, 15*tw, tw * tw, 0);
                }
                else if (hero.sta_frame <= 30) {
                    al_draw_bitmap_region(tileset, 2*tw, 13*tw, tw, tw, 15*tw, 14*tw, 0);
                    al_draw_bitmap_region(tileset, 2*tw, 14*tw, tw, tw, 15*tw, 15*tw, 0);
                    al_draw_bitmap_region(tileset, 2*tw, 15*tw, tw, tw, 15*tw, tw * tw, 0);
                    hero.is_swing_hoe = false;
                }
                break;
            case 'l':
                if(hero.sta_frame<=9) {
                    al_draw_bitmap_region(tileset, 0*tw, 18*tw, tw, tw, 15*tw, 14*tw, 0);
                    al_draw_bitmap_region(tileset, 0*tw, 19*tw, tw, tw, 15*tw, 15*tw, 0);
                }
                else if(hero.sta_frame<=19) {
                    al_draw_bitmap_region(tileset, 1*tw, 18*tw, tw, tw, 14*tw, 14*tw, 0);
                    al_draw_bitmap_region(tileset, 1*tw, 19*tw, tw, tw, 14*tw, 15*tw, 0);
                    al_draw_bitmap_region(tileset, 2*tw, 18*tw, tw, tw, 15*tw, 14*tw, 0);
                    al_draw_bitmap_region(tileset, 2*tw, 19*tw, tw, tw, 15*tw, 15*tw, 0);
                }
                else if(hero.sta_frame<=29) {
                    al_draw_bitmap_region(tileset, 3*tw, 19*tw, tw, tw, 14*tw, 15*tw, 0);
                    al_draw_bitmap_region(tileset, 4*tw, 19*tw, tw, tw, 15*tw, 15*tw, 0);
                }
                else if (hero.sta_frame <= 30) {
                    al_draw_bitmap_region(tileset, 3*tw, 19*tw, tw, tw, 14*tw, 15*tw, 0);
                    al_draw_bitmap_region(tileset, 4*tw, 19*tw, tw, tw, 15*tw, 15*tw, 0);
                    hero.is_swing_hoe = false;
                }
                break;
            case 'r':
                if(hero.sta_frame<=9) {
                    al_draw_bitmap_region(tileset, 0*tw, 20*tw, tw, tw, 15*tw, 14*tw, 0);
                    al_draw_bitmap_region(tileset, 0*tw, 21*tw, tw, tw, 15*tw, 15*tw, 0);
                }
                else if(hero.sta_frame<=19) {
                    al_draw_bitmap_region(tileset, 1*tw, 20*tw, tw, tw, 15*tw, 14*tw, 0);
                    al_draw_bitmap_region(tileset, 1*tw, 21*tw, tw, tw, 15*tw, 15*tw, 0);
                    al_draw_bitmap_region(tileset, 2*tw, 20*tw, tw, tw, 16* tw, 14*tw, 0);
                    al_draw_bitmap_region(tileset, 2*tw, 21*tw, tw, tw, 16* tw, 15*tw, 0);
                }
                else if(hero.sta_frame<=29) {
                    al_draw_bitmap_region(tileset, 3*tw, 21*tw, tw, tw, 15*tw, 15*tw, 0);
                    al_draw_bitmap_region(tileset, 4*tw, 21*tw, tw, tw, 16*tw, 15*tw, 0);
                }
                else if (hero.sta_frame <= 30) {
                    al_draw_bitmap_region(tileset, 3*tw, 21*tw, tw, tw, 15*tw, 15*tw, 0);
                    al_draw_bitmap_region(tileset, 4*tw, 21*tw, tw, tw, 16*tw, 15*tw, 0);
                    hero.is_swing_hoe = false;
                }
                break;
                break;
            }
            hero.sta_frame++;
        }
    }
    if(hero.is_swing_hoe == false) {
        switch (hero.facing) {
        case 'u':
            if(hero.move_animation==true) {
                for(int col=0;col<height;col++) {
                    for(int row=0;row<width;row++) {
                        al_draw_bitmap_region(tileset, mv[col][row].sx * tw, mv[col][row].sy * tw, tw, tw, (row*tw - hero.wloc*tw)+15*tw, (col*tw - hero.hloc*tw)+15*tw+hero.frame, 0);
                    }
                }    
                if (hero.frame<= 7) {
                    al_draw_bitmap_region(tileset, 1*tw, 3 * tw, tw, tw, 15*tw, 15*tw, 0);
                }
                else if (hero.frame < 15) {
                    al_draw_bitmap_region(tileset, 2*tw, 3 * tw, tw, tw, 15*tw, 15*tw, 0);
                }
                else if(hero.frame == 15) {
                    al_draw_bitmap_region(tileset, 2*tw, 3 * tw, tw, tw, 15*tw, 15*tw, 0);
                    hero.move_animation = false;
                    hero.hloc-=1;
                }
                hero.frame++;
            }
            else {
                al_draw_bitmap_region(tileset, 0*tw, 3 * tw, tw, tw, 15*tw, 15*tw, 0);
            }
            break;
        case 'd':
            if(hero.move_animation==true) {
                for(int col=0;col<height;col++) {
                    for(int row=0;row<width;row++) {
                        al_draw_bitmap_region(tileset, mv[col][row].sx * tw, mv[col][row].sy * tw, tw, tw, (row*tw - hero.wloc*tw)+15*tw, (col*tw - hero.hloc*tw)+15*tw-hero.frame, 0);
                    }
                }    
                if(hero.frame <=7) {
                    al_draw_bitmap_region(tileset, 1*tw, 2*tw, tw, tw, 15*tw, 15*tw, 0);
                }
                else if(hero.frame <= 14) {
                    al_draw_bitmap_region(tileset, 2*tw, 2*tw, tw, tw, 15*tw, 15*tw, 0);
                }
                else if(hero.frame==15) {
                    al_draw_bitmap_region(tileset, 2*tw, 2*tw, tw, tw, 15*tw, 15*tw, 0);
                    hero.move_animation = false;
                    hero.hloc+=1;
                }
                hero.frame++;
            }
            else {
                al_draw_bitmap_region(tileset, 0*tw, 2*tw, tw, tw, 15*tw, 15*tw, 0);
            }
            break;
        case 'l':
            if(hero.move_animation==true) {
                for(int col=0;col<height;col++) {
                    for(int row=0;row<width;row++) {
                        al_draw_bitmap_region(tileset, mv[col][row].sx * tw, mv[col][row].sy * tw, tw, tw, (row*tw - hero.wloc*tw)+15*tw+hero.frame, (col*tw - hero.hloc*tw)+15*tw, 0);
                    }
                }    
                if(hero.frame<=7) {
                    al_draw_bitmap_region(tileset, 1*tw, 4 * tw, tw, tw, 15*tw, 15*tw, 0);
                }
                else if(hero.frame<=14) {
                    al_draw_bitmap_region(tileset, 2*tw, 4 * tw, tw, tw, 15*tw, 15*tw, 0);
                }
                else if(hero.frame==15) {
                    al_draw_bitmap_region(tileset, 2*tw, 4 * tw, tw, tw, 15*tw, 15*tw, 0);
                    hero.move_animation = false;
                    hero.wloc-=1;
                }
                hero.frame++;
            }
            else {
                al_draw_bitmap_region(tileset, 0*tw, 4 * tw, tw, tw, 15*tw, 15*tw, 0);
            }
            break;
        case 'r':
            if(hero.move_animation==true) {
                for(int col=0;col<height;col++) {
                    for(int row=0;row<width;row++) {
                        al_draw_bitmap_region(tileset, mv[col][row].sx * tw, mv[col][row].sy * tw, tw, tw, (row*tw - hero.wloc*tw)+15*tw-hero.frame, (col*tw - hero.hloc*tw)+15*tw, 0);
                    }
                }    
                if(hero.frame<=7) {
                    al_draw_bitmap_region(tileset, 1*tw, 5 * tw, tw, tw, 15*tw, 15*tw, 0);
                }
                else if(hero.frame<=14) {
                    al_draw_bitmap_region(tileset, 2*tw, 5 * tw, tw, tw, 15*tw, 15*tw, 0);
                }
                else if(hero.frame==15) {
                    al_draw_bitmap_region(tileset, 2*tw, 5 * tw, tw, tw, 15*tw, 15*tw, 0);
                    hero.move_animation = false;
                    hero.wloc+=1;
                }
                hero.frame++;
            }
            else {
                al_draw_bitmap_region(tileset, 0*tw, 5 * tw, tw, tw, 15*tw, 15*tw, 0);
            }
            break;
        }
    }
    return hero;
}



Sponsor:

#2 Paradigm Shifter   Crossbones+   -  Reputation: 5433

Like
2Likes
Like

Posted 23 December 2012 - 04:12 PM

Yeah, don't do that.<br /><br />You need to separate the moving logic from the drawing logic, and the animation logic from the moving logic as well. Drawing should be done once a frame when everything has moved. Animations shouldn't cause movement unless the movement is encoded in the animation data (and not the game code - it needs to be data).
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#3 DeafTV   Members   -  Reputation: 1236

Like
2Likes
Like

Posted 23 December 2012 - 04:32 PM

I think what will benefit you most is to keep logic and rendering separate.

So some psuedo code of what I mean is:

int main(){
	
	//...

	while(running){
		//...
	
		Update();
		Draw();
	}
	
	//...
}

 

 

You want logic/character updates to be handled and then update the character based on what has happened.  Then the character should be drawn to the screen based on that.  Hope that makes sense because that is one of the things that I really struggled with at first.  Nice game so far too smile.png

 

Edit:

 

Another thing I thought that might help you is to decide what each "system" of the game does.  Graphics/Rendering functions probably don't need to know about the character or any of its members, but the character may need to know about these functions to draw to the screen.

 

This is the same for what paradigm said about movement and animation.  Movement needs to know about animation but animation doesn't need to know about movement.  This should help you structure your game a bit better.


Edited by DeafTV, 23 December 2012 - 06:21 PM.

Fly Safe 7o


#4 black_darkness   Members   -  Reputation: 280

Like
0Likes
Like

Posted 23 December 2012 - 07:13 PM

I think what will benefit you most is to keep logic and rendering separate.

So some psuedo code of what I mean is:
int main(){
	
	//...

	while(running){
		//...
	
		Update();
		Draw();
	}
	
	//...
}

 

 

You want logic/character updates to be handled and then update the character based on what has happened.  Then the character should be drawn to the screen based on that.  Hope that makes sense because that is one of the things that I really struggled with at first.  Nice game so far too smile.png

 

Edit:

 

Another thing I thought that might help you is to decide what each "system" of the game does.  Graphics/Rendering functions probably don't need to know about the character or any of its members, but the character may need to know about these functions to draw to the screen.

 

This is the same for what paradigm said about movement and animation.  Movement needs to know about animation but animation doesn't need to know about movement.  This should help you structure your game a bit better.

I am attempting to structure my game by creating functions right now. I managed to cut the source code in 1/5. (Alot of animation errors but I will fix them later.(I have a backup just in case for refferal or a restart.))  I will have to think on what you said in this thread. Thanks for the responses. I am going to have to be more organized from now on. I have wasted too much time messing with garbled source code. To be honest I have no idea how I would go about  doing logical operations aka animation without drawing each frame every time. I don't really get it.


Edited by black_darkness, 23 December 2012 - 07:16 PM.


#5 Karsten_   Members   -  Reputation: 1655

Like
0Likes
Like

Posted 24 December 2012 - 09:20 AM

Just remember that you dont need to (and probably shouldnt) draw the player etc... at the same time you update it's frame.

The following pseudo code might make some sense.


class Player
{
  int currentFrame;
 
  void update()
  {
    // set the frame in here
    currentFrame++;
  }
 
  void draw()
  {
    if(currentFrame == 0)
    {
      // draw this picture
    }
    else if(currentFrame == 1)
    {
      // draw this other picture
    }
    // etc...
  }
}

So in your game, the update function of all your ingame entities should be called during the timer tick etc...
Then in the draw function, you should call draw() on all your ingame entities.

This then separates logic (the increasing of the animation frame etc...) with the drawing (The drawing of the correct picture depending on the frame).

Edited by Karsten_, 24 December 2012 - 09:21 AM.

Mutiny - Open-source C++ Unity re-implementation.
Defile of Eden 2 - FreeBSD and OpenBSD binaries of our latest game.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS