Help - Search - Members - Calendar
Full Version: C Animation
Pixel2Life Forum > Help Section > Desktop Programming
Dragolux
Hey all,

I've been trying to make a program that animates from an array in C, and I can't get it to work. The idea is to have a separate header file that stores all of the frames of the animation. (all of the frames are just text - ascii) Then in the main program, there is a function called playForward that plays the animation forward.

Here's the main file...

CODE
             #include <stdio.h>
             #include "frames.h"
            
             void playForward( char *frames );
            
             int main( void ) {
                 playForward( *bounce );
                
                 getchar();
                
                 return 0;
             }
            
             void playForward( char *frames ) {
                  int i = 0;  /* outer counter */
                  int j = 0;  /* inner counter */
                  int k = 0;  /* double inner counter */
                  
                  for( i = 0; i < frameNum * frameHeight; i++ ) {
                       printf( "%s\n", &frames[ i ] );
                  }
                  
                  /*for( i = 1; i <= frameNum; i++ ) {
                       for( j = 0; j < frameHeight; j++ ) {
                            //for( k = 1; k <= frameLength; k++ ) {
                                 printf( "%s\n", &frames[ j ] );
                            //}
                            //printf( "\n" );
                       }
                       printf( "\n" );
                  }*/
             }


...and here's the header file (frames.h)...

CODE
             int frameNum = 5;
             int frameHeight = 3;
             int frameLength = 5;
            
             char *bounce[] = {
                  "*****",
                  "*    ",
                  "*****",
                  
                  "*****",
                  " *   ",
                  "*****",
                  
                  "*****",
                  "  *  ",
                  "*****",
                  
                  "*****",
                  "   * ",
                  "*****",
                  
                  "*****",
                  "    *",
                  "*****",
                  };


As you can see, the animation is very simple, and I just want to figure out how to make it work.

This is the output I get when I run the program:

CODE
           *****
           ****
           ***
           **
           *
          
           *
          
          
          
          
            *
           *


But I want it to print this:

CODE
*****
       *
       *****
  
         *****
      *
         *****
  
         *****
       *
         *****
  
         *****
        *
         *****
  
         *****
         *
         *****


(or something like that - this forum messes up the spacing in the code dry.gif )

Does anyone know what the problem could be?

Thanks!

*bump*
rc69
Don't bump, especially when you're still in the top 10...

My C is a bit rusty, but this should theoretically work. Before you try to change anything though, my mixing of pointer/array syntax was intentional, so though i make typos quite frequently, that wasn't one of them bigwink.gif

CODE
#include <stdio.h>

int frameNum = 5;

char *bounce = {
     "*****\n*    \n*****",
     "*****\n *   \n*****",
     "*****\n  *  \n*****",
     "*****\n   * \n*****",
     "*****\n    *\n*****"
};

void playForward(char *frames){
    int x;
    for(x=0; x < frameNum; x++){
        printf("%s\n", frames[x]);
    }
}

int main() {
    playForward(bounce);

    getchar();

    return 0;
}
Dragolux
Sorry about bumping - I didn't know what else to do (hey, it worked, didn't it? bigwink.gif ).

Anyway, thanks for the code, I'll check it out and see how it works.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.