Thread: Trouble with animation using Sleep

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    752

    Trouble with animation using Sleep

    Okay, this is a function which is supposed to read through a list of transformations, and step through each one, with a delay of pauseTime. TransformPolygon() handles the transformations.

    Actually drawing the polygon is done in WM_PAINT, and I haven't had any other problems with using InvalidateRect like this to do so.

    When I test this, what happens is I have a delay (the delay is the number of transformations x pauseTime). At the end of the delay, the polygon appears where it should at the end of the animation. However, I don't see any of the intermediate steps of the animation, just the end.

    I would really appreciate any insight on how to make this work. I am using DevC.
    Code:
    void PlayMoves (int pauseTime, HWND hWnd)
    {
     int i;
    
     for (i = 0; i < MoveIndex; i++)
     {
      TransformPolygon();
    
      InvalidateRect (hWnd, NULL, TRUE);
    
      Sleep(pauseTime);
     }
    return;
    }
    Callou collei we'll code the way
    Of prime numbers and pings!

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Perhaps creating a 'DrawVertex' fn may help. Your code is calling your TransformPolygon() fn which, presumably, draws an entire polygon conformation. Then is 'sleeps' and then loops back to drawing an entire polygon conformation.

    What, I imagine, you should try is drawing a single line of your polygon, then sleep, then draw another line, then sleep...etc

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    You could use GetTickCount() and a while loop. This would keep the progam running and allow the screen updates.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    I figured out the problem. I tried using GetTickCount, but pretty much the same thing was happening. Apparantly, when using the GDI for animation like this, you need to use WindowUpdate(hWnd) to force the window to redraw itself. InvalidateRect() just tells the window to update itself ... once it's not doing anything else.
    Callou collei we'll code the way
    Of prime numbers and pings!

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>>
    InvalidateRect() just tells the window to update itself ... once it's not doing anything else.
    <<<

    InvalidateRect() flags an area of the screen object that is now invalid and needs to be redrawn. The redraw occurs when the next WM_PAINT message is processed. You are in a tight loop, therefore the WM_PAINT is not received, therefore your accumulated invalidated rectangles are not processed.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  6. #6
    Barjor
    Guest
    Mm I think I will sneak in my question about Sleep() here. It is possible to do

    Magic(something)
    {
    Sleep(Infinite)
    }

    This will cause the function to wait forever. Would it be possible to break this when something happens, like a mouse event?

    Thanks
    ~Barjor

  7. #7
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    If you are considering doing this, then you have a design fault! Sleep() is a blocking call, if you can specify an infinite wait, (I've never tried that, can't think of any use for it), then I would suggest your thread will be blocked infinitely.

    What are you trying to do??? Sounds intruiging!!!!
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Threads in MFC
    By VirtualAce in forum Windows Programming
    Replies: 4
    Last Post: 12-28-2005, 06:03 PM
  2. Animation class not working
    By VirtualAce in forum Game Programming
    Replies: 5
    Last Post: 03-02-2005, 06:48 AM
  3. Problem with Sleep() #$@^#$%^
    By intruder in forum C++ Programming
    Replies: 8
    Last Post: 10-11-2004, 06:46 AM
  4. Sleep is overrated...
    By Polymorphic OOP in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 01-24-2003, 12:40 PM
  5. Mouse + Animation Trouble
    By destroyerBEACON in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 05-17-2002, 06:54 AM