Thread: Animation in SDL

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    Animation in SDL

    I'm doing it like this: I've got a sprite sheet with 6 frames of a dude walking. I take an SDL_Rect and use it "cut" the frames out of the image and display them one after the other. Couldn't sound simpler and when I got it working and saw how ridiculously quickly he was animating I thought "that's ok, I'll fix it in a minute".

    6 hours later and I've still got it wanked.

    For the life of me I can't figure it out. Each object has a HandleInput() function which takes one arg, the time difference between the last frame and the current one. I'm using this value to move but I've got no idea how to use it to get my animation to sync (or whatever).

    Here's my HandleInput for the said sprite:

    Code:
    void TL_Vamp::HandleInput(int dT)
    {
        _sendableMessage._id = M_MOVE;
        _sendableMessage._arg1 = MOVE_LEFT;
        _sendableMessage._arg2 = dT;
    
        if (InputHandler::GetSingleton().KeyStillDown(SDLK_d)) {
            _currentFrame++;
            _srcRect.x += 55;
    
            if (_currentFrame >= 6) {
                _currentFrame = 0;
                _srcRect.x = 0;
            }
    
            if (canMoveForward) {
                _dstRect.x += dT;
            }
    
            if (_dstRect.x > (GameSystem::GetScreenWidth() / 2) - 55) {
                GameSystem::FireMessage(&_sendableMessage, GameSystem::GetObjectByName(std::string("TL_Background")));
                canMoveForward = false;
            }
    
            canMoveBack = true;
        }
    
        if (InputHandler::GetSingleton().KeyStillDown(SDLK_a)) {
            if (canMoveBack) _dstRect.x -= 2;
            if (_dstRect.x < 20) canMoveBack = false;
    
            canMoveForward = true;
        }
    }
    Just ignore everything else for now.
    Everything I find online is about as clear as mud to me and my humble -Z in mathematics.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Maybe, this will help? Here

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Nope I've been reading that over and over all day, I can't see how he's doing it.
    Explain it to me like you're talking to a 12 year old
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Can't you do something like:
    Code:
    if (dT>=250) //in ms?
      _currentFrame++; //change frame every ~1/4 second
    Maybe I don't understand the problem
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    I've output dT every frame and it's always 13. Here's how I've calculated it:

    Code:
    Uint32 lastTime = 0, thisTime = SDL_GetTicks(), dT = 0;
    
    // Once each frame
    lastTime = thisTime;
    thisTime = SDL_GetTicks();
    dT = thisTime - lastTime;
    EDIT: I'm a turd.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Dump the SDL ticks and use GetTickCount() from Win32

  7. #7
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Done.
    Now what?


    I hate animation.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  8. #8
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    There's no point in exchanging SDL_GetTicks() for GetTickCount().

    SDL_GetTicks() will automatically decide which is the best to use: GetTickCount(), QueryPerformanceTimer() or timeGetTime().

  9. #9
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Oh ok.
    But how do I actually use this dT to... get animations to... work....

    ?

    God damn I sound like a retard, I know. It's probably obvious to everyone else (hence the plethora of replies focused on the thing that is the least of my worries) but I've spent more time on this than I had thought.

    Bugger it. Maybe game programming isn't my thing. Well it is - it's more interesting than programming other things but getting stuck on a problem like this is so kick you in the crotch spit on your neck painful.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  10. #10
    Registered User
    Join Date
    Jun 2006
    Posts
    9
    I'm not sure, I'm definetly not a pro but maybe it would help to add somewhere in the code a sleep function, maybe for 1000ms?
    I haven't used c++ so much but it works fine in my animations in Java.
    Like this;

    Code:
    while(){
    sleep(1000);
    }
    Not sure if it works

  11. #11
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Code:
    void TL_Vamp::HandleInput(int dT)
    {
        _sendableMessage._id = M_MOVE;
        _sendableMessage._arg1 = MOVE_LEFT;
        _sendableMessage._arg2 = dT;
    
        if (InputHandler::GetSingleton().KeyStillDown(SDLK_d)  ) {
            _currentFrame++;
            _srcRect.x += 55;
            // I've been calling SDL_Delay(1000) here but ...
    
            if (_currentFrame >= 6) {
                _currentFrame = 0;
                _srcRect.x = 0;
            }
    
            if (canMoveForward) {
                _dstRect.x += dT;
            }
    
            if (_dstRect.x > (GameSystem::GetScreenWidth() / 2) - 55) {
                GameSystem::FireMessage(&_sendableMessage, GameSystem::GetObjectByName(std::string("TL_Background")));
                canMoveForward = false;
            }
    
            canMoveBack = true;
        }
    
        if (InputHandler::GetSingleton().KeyStillDown(SDLK_a)  ) {
            if (canMoveBack) _dstRect.x -= 2;
            if (_dstRect.x < 20) canMoveBack = false;
    
            canMoveForward = true;
        }
    }
    ... it slows the whole damn game down and makes it all jittery.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  12. #12
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I suppose you could keep a static variable in your function:
    Code:
    void TL_Vamp::HandleInput(int dT)
    {
       static int timeElapsed = 0;
       timeElapsed += dT;
      //...
      if (timeElapsed>=250)
     {
      _currentFrame++;
      timeElapsed = 0;
      }
      //...
    }
    Which is what I really meant before...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  13. #13
    Ecologist
    Join Date
    Aug 2001
    Location
    Utah.
    Posts
    1,291
    Quote Originally Posted by ahluka
    Oh ok.
    But how do I actually use this dT to... get animations to... work....
    .
    I don't really have a lot of animation in my game, but here's
    how I handled the one thing I do have
    Code:
    void cl_BoyTorso::Update()
    {
    	const float UpdateInterval = 0.20f; //update 5times / second
    	static double SecondsSinceLastUpdate = 0;
    
    	if(SecondsSinceLastUpdate >= UpdateInterval)
    	{
    		//unimportant code that decides the CurrentFrame
    
    		SecondsSinceLastUpdate = 0;
    		UpdateState = CurrentFrame;
    	}
    	else
    	{
    		SecondsSinceLastUpdate += cl_Clock::Global().GetDeltaTime();
    	}
    }
    Just add keep adding dt to a variable until it's above the
    interval you want between frames.

    //edit: Which is pretty much exactly what Jawib said while I
    was replying.
    Last edited by Cheeze-It; 06-04-2006 at 10:20 AM.
    Staying away from General.

  14. #14
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Thanks guys





    *manly hugs and kisses*

    EDIT: Collision detection is the next thing I'm not looking forward to. I know how to do it just not when to do it. I guess the best idea would be to define an SDL_Rect the same dimensions as the screen, then check when an enemy is inside it. If he is then also check for his collisions (about the only thing they'll collide with is the player). Would that be "ok" for a side scroller?

    Don't tell me I have to do all this BSP-type crapola...
    Last edited by cboard_member; 06-04-2006 at 11:22 AM.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  15. #15
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >Don't tell me I have to do all this BSP-type crapola...

    You only need that if its too computationally expensive to check for a collision between everything and everything else. In a simple 2D side scroller haveing a bounding box around collidables and checking for one box intersecting with another should be just fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems compiling this SDL app
    By Rider in forum C++ Programming
    Replies: 3
    Last Post: 03-27-2007, 12:22 PM
  2. Animation class not working
    By VirtualAce in forum Game Programming
    Replies: 5
    Last Post: 03-02-2005, 06:48 AM
  3. SDL and MinGW Studio
    By Vicious in forum Tech Board
    Replies: 0
    Last Post: 07-30-2004, 09:59 PM
  4. SDL and Windows
    By nickname_changed in forum Windows Programming
    Replies: 14
    Last Post: 10-24-2003, 12:19 AM
  5. sdl in c++
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-07-2002, 07:46 AM