Thread: Busy animation?

  1. #1
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273

    Question Busy animation?

    Hi,

    I've written a program which takes a while to complete what it has to do, going through a loop several billion times (You'd think it'd be speedy on a P4, but nooooooooo... ), and I've tried adding a "busy" animation so I know it hasn't crashed. The way I tried to do it was:-
    Code:
    char chBusy[4] = { 0x2F, 0x2D, 0x5C, 0x7C }; // "/", "-", "\", "|"
    int iBusyIndex = 0;
    
    (inside loop)
    putchar(0x08); // backspace to remove previous character
    putchar(chBusy[iBusyIndex++]);
    if (iBusyIndex = 4)
    	iBusyIndex = 0;
    else
    	iBusyIndex++;
    However, this doesn't produce the desired effect. I can't actually see the animation (perhaps it changes too quickly?) and the cursor hops back and forth like mad. What's the proper way, please?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    An example, courtesy of Prelude:
    Code:
    #include <stdio.h>
    #include <time.h>
    
    void sleep ( long milli )
    {
      clock_t end, current = clock();
      for( end = current + milli; current < end; current = clock() );
    }
    
    int main ( void )
    {
      int x = 0;
      const char *barRotate = "|\\-/";
      printf ( "Loading configuration files... " );
      for ( ; ; ) {
        if ( x > 3 ) x = 0;
        printf ( "%c\b", barRotate[x++] );
        sleep ( 200 );
      }
      return 0;
    }
    You probably won't want the sleep in your program, I only include it here to show you a complete working example.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Whoops, looks like I don't know all the printf flags, that's a much better way of doing it, thanx

  4. #4
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    here..

    Code:
    void showwhirl()
    {
        int i, j;
        char whirl[] = {'|','/','-','\\\'};
    
        for (i=0;i<=2; i++)
        {
            for (j=0; j<=3; j++)
            {
                printf("%c", whirl[j]);
                delay(25);
                printf("\b");
            }
        }
    
    }

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. 3D animation (difficult?)
    By maes in forum Game Programming
    Replies: 3
    Last Post: 09-08-2003, 10:44 PM
  4. ascii animation
    By jstn in forum C Programming
    Replies: 3
    Last Post: 05-14-2002, 07:16 PM
  5. Trouble with animation using Sleep
    By QuestionC in forum Windows Programming
    Replies: 6
    Last Post: 02-06-2002, 11:08 AM