#define _POSIX_C_SOURCE 200809L // for strnlen #include #include char s[4] = "0000"; char f = 'A'; unsigned fi = 0; unsigned sp = 1; unsigned max = 0; unsigned sk = 1; unsigned mdl = 0; // recursion is fun int advance_sprite( int idx ); int advance_sprite( int idx ) { if ( idx < 0 ) return 0; // we reached the limit, bail out if ( s[idx] == '9' ) s[idx] = 'A'; // from 9 to A else if ( s[idx] == 'Z' ) // at limit { // advance the previous char if ( !advance_sprite(idx-1) ) return 0; // no more, bail out s[idx] = '0'; // restart } else s[idx]++; // just increment return 1; } int advance_frame( void ) { printf("\tFrameIndex %.4s %c %d %d\n",s,f,mdl,fi); fi+=sk; // advance frame in model f++; // advance frame in sprite if ( f > 'Z' ) // note that [,\ and ] are not used. this is personal preference, really { // we need to change the sprite name if ( !advance_sprite(3) ) return 0; // can't advance further f = 'A'; // restart } return 1; } int main( int argc, char **argv ) { if ( argc < 5 ) { fprintf(stderr,"usage: pframes [frame step] [model index] [first sprite frame]\n" "\n" " - sprite name: must be 4 characters.\n" " - start frame: frame offset within the model to start from.\n" " - max frames: limit on frames advanced.\n" " - frame step: how many frames to move each step (default:1, must be higher than zero).\n" " - model index: which model to assign (default: 0).\n" " - first sprite frame: which sprite frame to start from (default: A).\n" "\n" " Frames advance from A to Z, wrapping around.\n" " Each wrap, the fourth char of the sprite name will be cycled, from 0 to 9, then A to Z.\n" " After ###Z is reached, the third character will by cycled instead.\n" " And so on for the second, then the first...\n" " If you somehow run out of sprite names, you need to be more reasonable. (^^;\n"); return 1; } strncpy(s,argv[1],4); if ( strnlen(s,4) < 4 ) { fprintf(stderr,"fatal: sprite name must be 4 characters.\n"); return 2; } sscanf(argv[2],"%u",&fi); sscanf(argv[3],"%u",&max); if ( max == 0 ) { fprintf(stderr,"fatal: max frames must be higher than zero.\n"); return 4; } if ( argc > 4 ) sscanf(argv[4],"%u",&sk); if ( sk == 0 ) { fprintf(stderr,"fatal: frame step must be higher than zero.\n"); return 8; } if ( argc > 5 ) sscanf(argv[5],"%u",&mdl); if ( argc > 6 ) sscanf(argv[6],"%c",&f); if ( (f < 'A') || (f > 'Z') ) { fprintf(stderr,"fatal: first sprite frame must be between A and Z (inclusive).\n"); return 16; } for ( unsigned i=0; i