home *** CD-ROM | disk | FTP | other *** search
/ World of Graphics / WOGRAPH.BIN / 729.SOURCE.ZIP / 3DSANI.C < prev    next >
C/C++ Source or Header  |  1993-04-24  |  6KB  |  285 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <values.h>
  6.  
  7. char filename[80];
  8. char vuename[80];
  9. char tplname[80];
  10. char outname[80];
  11. int  fstart;
  12. int  fend;
  13. int  fstep;
  14.  
  15. void process_args (int argc, char *argv[]);
  16. void read_vue (char *fname, int *start, int *end);
  17. void add_ext (char *fname, char *ext, int force);
  18. void abortmsg (char *msg, int exit_code);
  19. char *after (char *str, char *target);
  20. char lookahead (FILE *f);
  21. char upcase (char ch);
  22.  
  23.  
  24. int main (int argc, char *argv[])
  25. {
  26.     FILE *tpl, *out;
  27.     char fstring[] = "%03d";
  28.     char ch;
  29.     int  frame, digits, i;
  30.  
  31.     /* Handle the command line args */
  32.     process_args (argc, argv);
  33.  
  34.     /* Get the start/end frame numbers from the .vue file */
  35.     read_vue (vuename, &fstart, &fend);
  36.  
  37.     if (fstart > fend)
  38.     abortmsg ("No frames generated.", 1);
  39.  
  40.     digits = (fend < 1) ? 1 : (floor(log10(fend)) + 1);
  41.  
  42.     out = fopen (outname, "w");
  43.     if (out == NULL) {
  44.     printf ("Unable to open output file '%s'", outname);
  45.     exit(1);
  46.     }
  47.  
  48.     printf ("Output to: %s\n", outname);
  49.  
  50.     for (frame = fstart; frame <= fend; frame = frame + fstep) {
  51.     tpl = fopen (tplname, "r");
  52.  
  53.     if (tpl == NULL) {
  54.         printf ("Unable to open template file '%s'\n", tplname);
  55.         exit(1);
  56.     }
  57.  
  58.     while (1) {
  59.         ch = getc (tpl);
  60.  
  61.         if (feof(tpl))
  62.         break;
  63.  
  64.         if (ch == '$') {
  65.         if (lookahead(tpl) == '#' && (strlen(filename) + digits > 8)) {
  66.             for (i = 0; i < 8 - digits; i++)
  67.             putc (filename[i], out);
  68.         }
  69.         else
  70.             fprintf (out, "%s", filename);
  71.         }
  72.         else if (ch == '#') {
  73.         fstring[2] = '0' + digits;
  74.         fprintf (out, fstring, frame);
  75.         }
  76.         else
  77.         putc (ch, out);
  78.     }
  79.  
  80.     fclose (tpl);
  81.     }
  82.  
  83.     fclose (out);
  84.  
  85.     return 0;
  86. }
  87.  
  88.  
  89.  
  90. void process_args (int argc, char *argv[])
  91. {
  92.     int i;
  93.  
  94.     printf("\n\n3DS2POV Animation Utility\n");
  95.     printf ("\n");
  96.  
  97.     if (argc < 2) {
  98.     printf ("Usage: 3dsani inputfile [options]\n\n");
  99.     printf ("Options: -a<filename> - Specifies name of the 3ds animation (.vue) file\n");
  100.     printf ("         -t<filename> - Specifies name of the template (.tpl) file\n");
  101.     printf ("         -o<filename> - Specifies name of the output (.bat) file\n");
  102.     printf ("         -snnn        - Start animation at frame nnn\n");
  103.     printf ("         -ennn        - End animation at frame nnn\n");
  104.     printf ("         -pnnn        - Render every nnn'th frane\n");
  105.     printf ("\nex. 3dsani ruby -aruby.vue -tani.tpl -oruby.bat -s0 -e10\n\n");
  106.     exit(1);
  107.     }
  108.  
  109.     strcpy (filename, "");
  110.     strcpy (vuename, "");
  111.     strcpy (tplname, "");
  112.     strcpy (outname, "");
  113.     fstart = -1;
  114.     fend   = -1;
  115.     fstep  =  1;
  116.  
  117.     for (i = 1; i < argc; i++) {
  118.     if (argv[i][0] == '-' || argv[i][0] == '/') {
  119.         switch (upcase(argv[i][1])) {
  120.         case 'A': strcpy (vuename, &argv[i][2]);
  121.               break;
  122.  
  123.         case 'T': strcpy (tplname, &argv[i][2]);
  124.               break;
  125.  
  126.         case 'O': strcpy (outname, &argv[i][2]);
  127.               break;
  128.  
  129.         case 'S': if (argv[i][2] != '\0')
  130.                   fstart = atoi (&argv[i][2]);
  131.               break;
  132.  
  133.         case 'E': if (argv[i][2] != '\0')
  134.                   fend = atoi (&argv[i][2]);
  135.               break;
  136.  
  137.         case 'P': if (argv[i][2] != '\0') {
  138.                   fstep = atoi (&argv[i][2]);
  139.  
  140.                   if (fstep < 1)
  141.                   fstep = 1;
  142.               }
  143.               break;
  144.  
  145.         default : printf ("\nInvalid option -%c\n", argv[i][1]);
  146.               exit (1);
  147.         }
  148.     }
  149.     else if (strlen (filename) == 0) {
  150.         strcpy (filename, argv[i]);
  151.         add_ext (filename, "", 1);
  152.     }
  153.     else
  154.         abortmsg ("Too many file names specified.", 1);
  155.     }
  156.  
  157.     if (strlen (filename) == 0)
  158.     abortmsg ("No file name specified", 1);
  159.  
  160.     if (strlen(vuename) == 0) {
  161.     strcpy (vuename, filename);
  162.     add_ext (vuename, "vue", 1);
  163.     }
  164.  
  165.     if (strlen (tplname) == 0) {
  166.     strcpy (tplname, filename);
  167.     add_ext (tplname, "tpl", 1);
  168.     }
  169.  
  170.     if (strlen (outname) == 0) {
  171.     strcpy (outname, filename);
  172.     add_ext (outname, "bat", 1);
  173.     }
  174.  
  175.     add_ext (vuename, "vue", 0);
  176.     add_ext (tplname, "tpl", 0);
  177.     add_ext (outname, "bat", 0);
  178. }
  179.  
  180. /* Read the start/end frame numbers from the .vue file */
  181. void read_vue (char *fname, int *start, int *end)
  182. {
  183.     FILE *f;
  184.     char string[256];
  185.     int  fmin, fmax, frame;
  186.  
  187.     f = fopen (fname, "r");
  188.  
  189.     if (f != NULL) {
  190.     fmin = +MAXINT;
  191.     fmax = -MAXINT;
  192.  
  193.     while (fgets (string, 256, f) != NULL) {
  194.         if (strstr (string, "frame") == string) {
  195.         frame = atoi (after (string, "frame"));
  196.  
  197.         if (frame < fmin)  fmin = frame;
  198.         if (frame > fmax)  fmax = frame;
  199.         }
  200.     }
  201.  
  202.     if (*start < 0 && fmin != +MAXINT)
  203.         *start = fmin;
  204.  
  205.     if (*end < 0 && fmax != -MAXINT)
  206.         *end = fmax;
  207.  
  208.     fclose(f);
  209.     }
  210.  
  211.     if (*start < 0)
  212.     abortmsg ("No start frame specified", 1);
  213.  
  214.     if (*end < 0)
  215.     abortmsg ("No end frame specified", 1);
  216.  
  217.     printf ("Generating frames %d through %d", *start, *end);
  218.  
  219.     if (fstep > 1)
  220.     printf (" (every %d frames)", fstep);
  221.  
  222.     printf ("\n");
  223. }
  224.  
  225.  
  226. void add_ext (char *fname, char *ext, int force)
  227. {
  228.     int i;
  229.  
  230.     for (i = 0; i < strlen(fname); i++)
  231.     if (fname[i] == '.') break;
  232.  
  233.     if (fname[i] == '\0' || force) {
  234.     if (strlen(ext) > 0)
  235.         fname[i++] = '.';
  236.  
  237.     strcpy (&fname[i], ext);
  238.     }
  239. }
  240.  
  241.  
  242. void abortmsg (char *msg, int exit_code)
  243. {
  244.     printf ("\n%s\n", msg);
  245.     exit (exit_code);
  246. }
  247.  
  248.  
  249. char *after (char *str, char *target)
  250. {
  251.     static char result[256];
  252.     char   *search;
  253.  
  254.     search = strstr (str, target);
  255.  
  256.     if (search == NULL)
  257.     strncpy (result, "", 256);
  258.     else
  259.     strncpy (result, search + strlen(target), 256);
  260.  
  261.     result[255] = '\0';
  262.  
  263.     return result;
  264. }
  265.  
  266. /* Peek at the next character in the file */
  267. char lookahead (FILE *f)
  268. {
  269.     char ch;
  270.  
  271.     ch = getc (f);
  272.     ungetc (ch, f);
  273.  
  274.     return ch;
  275. }
  276.  
  277.  
  278. char upcase (char c)
  279. {
  280.     if (c >= 'a' && c <= 'z')
  281.     c = c - 'a' + 'A';
  282.  
  283.     return c;
  284. }
  285.