• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
black_darkness

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

4 posts in this topic

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.

 

[CODE]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;
}[/CODE]

0

Share this post


Link to post
Share on other sites
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).
2

Share this post


Link to post
Share on other sites

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
2

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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_
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0