home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / grafik / sega / pvquant / anim / makeanim.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-28  |  4.5 KB  |  192 lines

  1. /*
  2.     This program reads an animation source file (*.ANM), and uses it to
  3.     generate data files for the PoV ray tracer. An initial data file
  4.     must also be given.
  5.  
  6.     Command format:
  7.          MAKEANIM data animation
  8.  
  9.     The animation file has the following format:
  10.     Line 1: name1  [,name2]...
  11.     Line 2: value1 [,value2]...
  12.  
  13.     name1, name2, etc. are the names of items #declared in the initial data
  14.     file, separated by commas. The names must be in the same order as they
  15.     are #defined in the data file.
  16.  
  17.     value1, value2, etc. are the values to be assigned to those #defined
  18.     items, separated by commas.
  19.     Each line represents one screen.
  20.  
  21.     This program reads the initial data file, searching for the lines where
  22.     the items named in the animation file are defined.
  23.  
  24.     Data files are then generated, replacing the lines containing the
  25.     #defines identified with new values.
  26. */
  27.  
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31.  
  32. #define FALSE 0
  33. #define TRUE 1
  34.  
  35. #define MAX_ITEMS 20
  36.  
  37. struct ITEM {
  38.     char        name[256];
  39.     int        line;
  40. } ;
  41.  
  42. struct ITEM item[MAX_ITEMS];
  43. int num_items;
  44.  
  45. FILE *anim_file, *data_file;
  46.  
  47. #ifndef __TURBOC__
  48. void strlwr(char *s)
  49. {
  50.     while (*s) {
  51.         *s = tolower(*s);
  52.         s++;
  53.     }
  54. }
  55. #endif
  56.  
  57. int get_field_names(char *field_buff, struct ITEM *field)
  58. {
  59.     int i;
  60.     char *start = field_buff;
  61.     char *dest;
  62.  
  63.     strlwr(field_buff);
  64.     for (i = 0; i < MAX_ITEMS; i++) {
  65.         dest = field[i].name;
  66.         while (*start == ' ' || *start == '\t' && *start != '\n') start++;
  67.         while (*start != ',' && *start != '\n') {
  68.             if (*start != ' ' && *start != '\t')    *dest++ = *start;
  69.             start++;
  70.         }
  71.         if (*start == '\n') break;
  72.         start++;
  73.     }
  74.     return i+1;
  75. }
  76.  
  77. void split_fields(char *anim_buff, char name[256][MAX_ITEMS], int num_items)
  78. {
  79.     int i;
  80.     char *end;
  81.  
  82.     for (i = 0; i < num_items; i++) {
  83.         end = strpbrk(anim_buff, ",\n");
  84.         *end = '\0';
  85.         strcpy(name[i], anim_buff);
  86.         anim_buff = end + 1;
  87.     }
  88. }
  89.  
  90. void scan_data_file(struct ITEM *field, int num_items)
  91. {
  92.     char data_buff[256];
  93.     char *name;
  94.     char *end;
  95.     int line, i;
  96.  
  97.     for (line = 0; !feof(data_file); line++) {
  98.         fgets(data_buff, sizeof(data_buff), data_file);
  99.         strlwr(data_buff);
  100.         if (strncmp("#declare", data_buff, 8) != 0) continue;
  101.         name = data_buff+8;
  102.         while (*name == ' ' || *name == '\t') name++;
  103.         end = name;
  104.         while (*end != ' ' && *end != '\t') end++;
  105.         *end = '\0';
  106.         for (i = 0; i < num_items; i++)
  107.             if (strcmp(name, field[i].name) == 0) {
  108.                 field[i].line = line;
  109.                 break;
  110.             }
  111.     }
  112. }
  113.  
  114. void generate_file(char *fname, struct ITEM *field, int num_fields, char *anim_line)
  115. {
  116.     int line, i;
  117.     FILE *out;
  118.     static char value[256][MAX_ITEMS];
  119.     char data_buff[256];
  120.  
  121.     fseek(data_file, 0L, SEEK_SET);
  122.     if ((out = fopen(fname, "wt")) == NULL) {
  123.         printf("Couldn't open %s\n", fname);
  124.         exit(1);
  125.     }
  126.  
  127.     line = 0;
  128.     split_fields(anim_line, value, num_fields);
  129.     for (i = 0; i < num_fields; i++) {
  130.         for ( ; line < field[i].line; line++) {
  131.             fgets(data_buff, sizeof(data_buff), data_file);
  132.             fputs(data_buff, out);
  133.         }
  134.         fgets(data_buff, sizeof(data_buff), data_file);
  135.         fprintf(out, "#declare %s = %s\n", field[i].name, value[i]);
  136.         line++;
  137.     }
  138.     fgets(data_buff, sizeof(data_buff), data_file);
  139.     while (!feof(data_file)) {
  140.         fputs(data_buff, out);
  141.         fgets(data_buff, sizeof(data_buff), data_file);
  142.     }
  143.     fclose(out);
  144. }
  145.  
  146. void cdecl main(int argc, char *argv[])
  147. {
  148.     char fname[256];
  149.     char anim_line[256];
  150.     int file_no;
  151.  
  152.     printf("MAKEANIM v1.00 -- Create a series of PVRay data files.\n");
  153.     printf("By F van der Hulst. Copyright 1991\n\n");
  154.  
  155.     if (argc != 3) {
  156.         printf("2 Filenames must be specified\n");
  157.         exit(1);
  158.     }
  159.  
  160.     strcpy(fname, argv[1]);
  161.     strcat(fname, ".dat");
  162.     if ((data_file = fopen(fname, "rt")) == NULL) {
  163.         printf("Couldn't open %s\n", fname);
  164.         exit(1);
  165.     }
  166.  
  167.     strcpy(fname, argv[2]);
  168.     strcat(fname, ".anm");
  169.     if ((anim_file = fopen(fname, "rt")) == NULL) {
  170.         printf("Couldn't open %s\n", fname);
  171.         exit(1);
  172.     }
  173.  
  174.     fgets(anim_line, sizeof(anim_line), anim_file);
  175.     num_items = get_field_names(anim_line, item);
  176.     if (num_items == 0) {
  177.         printf("No items specified\n");
  178.         exit(1);
  179.     }
  180.  
  181.     printf("%12s  %s", " ", anim_line);
  182.     scan_data_file(item, num_items);
  183.  
  184.     fgets(anim_line, sizeof(anim_line), anim_file);
  185.     for (file_no = 0; !feof(anim_file); file_no++) {
  186.         sprintf(fname, "%4s_%d.dat", argv[2], file_no);
  187.         printf("%12s: %s", fname, anim_line);
  188.         generate_file(fname, item, num_items, anim_line);
  189.         fgets(anim_line, sizeof(anim_line), anim_file);
  190.     }
  191. }
  192.