home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / DEVEL2.ZIP / SEGIO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-22  |  5.0 KB  |  177 lines

  1. /* Segment file i/o */
  2.  
  3. /* Written by Bernie Roehl, March 1992 */
  4.  
  5. /* The format of a segment file is simple, and very C-like.  Each segment
  6.    is bounded by { and }, and contains any combination of attributes and
  7.    additional segments (which are children of the segment they appear in).
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <ctype.h>
  12. #include <string.h>
  13. #include "rend386.h"
  14. #include "plg.h"
  15.  
  16. int readseg_err = 0;
  17.  
  18. #define match(a, b) (!strnicmp((a), (b), strlen(b)))
  19.  
  20. static OBJLIST *curr_objlist = NULL;
  21.  
  22. static char *boundstypes[] = { "NONE", "SPHERE", "BOX", "CSG", NULL };
  23.  
  24. void set_readseg_objlist(OBJLIST *olist)
  25.     {
  26.     curr_objlist = olist;
  27.     }
  28.  
  29. static process_attribute(char *buff, SEGMENT *seg)
  30.     {
  31.     char filename[100];
  32.     float sx = 1, sy = 1, sz = 1;
  33.     long tx = 0, ty = 0, tz = 0;
  34.     FILE *in;
  35.     while (isspace(*buff)) ++buff;
  36.     if (match(buff, "plgfile")) {
  37.         OBJECT *obj;
  38.         sscanf(buff, "plgfile = %s scale %f,%f,%f shift %ld,%ld,%ld", filename, &sx, &sy, &sz, &tx, &ty, &tz);
  39.         set_loadplg_scale(sx, sy, sz);
  40.         set_loadplg_offset(tx, ty, tz);
  41.         seg_set_load_info(seg, filename, sx, sy, sz, tx, ty, tz);
  42.         if ((in = fopen(filename, "r")) == NULL) {
  43.             readseg_err = -1;
  44.             return -1;
  45.             }
  46.         if ((obj = load_plg(in)) == NULL) {
  47.             readseg_err = -2;
  48.             fclose(in);
  49.             return -2;
  50.             }
  51.         seg_setrep(seg, obj);
  52.         set_object_owner(obj, seg);
  53.         if (curr_objlist) add_to_objlist(curr_objlist, obj);
  54.         fclose(in);
  55.         return 0;
  56.         }
  57.     else if (match(buff, "pos")) {
  58.         long tx, ty, tz;
  59.         sscanf(buff, "pos = %ld,%ld,%ld", &tx, &ty, &tz);
  60.         abs_move_segment(seg, tx, ty, tz);
  61.         }
  62.     else if (match(buff, "rot")) {
  63.         float rx, ry, rz;
  64.         sscanf(buff, "rot = %f,%f,%f", &rx, &ry, &rz);
  65.         abs_rot_segment(seg, (long) (rx * 65536L), (long) (ry * 65536L), (long) (rz * 65536L));
  66.         }
  67.     else if (match(buff, "name")) {
  68.         char *p;
  69.         if ((p = strchr(buff, '=')) == NULL) {
  70.             readseg_err = -3;
  71.             return -3;
  72.             }
  73.         do ++p; while (isspace(*p));
  74.         seg_setname(p);
  75.         }
  76.     else if (match(buff, "boundstype")) {
  77.         char *p;
  78.         int i;
  79.         if ((p = strchr(buff, '=')) == NULL) {
  80.             readseg_err = -3;
  81.             return -3;
  82.             }
  83.         do ++p; while (isspace(*p));
  84.         for (i = 0; boundstypes[i]; ++i)
  85.             if (match(p, boundstypes[i]))
  86.                 break;
  87.         if (boundstypes[i] == NULL) i = 0;
  88.         set_seg_boundtype(seg, (unsigned char) i);
  89.         }
  90.     else if (match(buff, "boundsorig")) {
  91.         long x = 0, y = 0, z = 0;
  92.         sscanf(buff, "boundsorig = %ld,%ld,%ld", &x, &y, &z);
  93.         set_seg_boundorig(seg, x, y, z);        
  94.         }
  95.     else if (match(buff, "boundslimits")) {
  96.         long x = 0, y = 0, z = 0;
  97.         sscanf(buff, "boundslimits = %ld,%ld,%ld", &x, &y, &z);
  98.         set_seg_boundlimits(seg, x, y, z);        
  99.         }
  100.     else if (match(buff, "hotspot")) {
  101.         long x = 0, y = 0, z = 0;
  102.         sscanf(buff, "hotspot = %ld,%ld,%ld", &x, &y, &z);
  103.         add_hotspot(seg, x, y, z);
  104.         }
  105.     /* ignore anything we don't understand */
  106.     return 0;
  107.     }
  108.  
  109. SEGMENT *readseg(FILE *in, SEGMENT *parent)
  110.     {
  111.     SEGMENT *seg;
  112.     char buff[256];
  113.     int c, i = 0;
  114.     if ((seg = new_seg(parent)) == NULL) return NULL;
  115.     while ((c = getc(in)) != EOF) {
  116.         switch (c) {
  117.             case '{':
  118.                 readseg(in, seg);
  119.                 break;
  120.             case '}':
  121.                 return seg;
  122.             case ';':
  123.                 buff[i] = '\0';
  124.                 process_attribute(buff, seg);
  125.                 i = 0;
  126.                 break;
  127.             default:
  128.                 if (i < sizeof(buff)-1) buff[i++] = c;
  129.                 break;
  130.             }
  131.         }
  132.     return seg;
  133.     }
  134.  
  135. writeseg(FILE *out, SEGMENT *s, int level)
  136.     {
  137.     SEGMENT *p;
  138.     void *q;
  139.     long tx, ty, tz, rx, ry, rz;
  140.     float frx, fry, frz;
  141.     char *name;
  142.     unsigned char btype;
  143.     int i;
  144.     for (i = 0; i < level; ++i) fprintf(out, "\t");
  145.     fprintf(out, "{\n");
  146.     if ((name = seg_getname(s)) != NULL) {
  147.         for (i = 0; i < level; ++i) fprintf(out, "\t");
  148.         fprintf(out, "name = %s;\n", name);
  149.         }
  150.     seg_getposition(s, &tx, &ty, &tz, &rx, &ry, &rz);
  151.     for (i = 0; i < level; ++i) fprintf(out, "\t");
  152.     fprintf(out, "pos = %ld,%ld,%ld;\n", tx, ty, tz);
  153.     for (i = 0; i < level; ++i) fprintf(out, "\t");
  154.     fprintf(out, "rot = %f,%f,%f;\n", ((float) rx) / 65536L, ((float) ry) / 65536L, ((float) rz) / 65536L);
  155.     btype = get_seg_boundinfo(s, &tx, &ty, &tz, &rx, &ry, &rz);
  156.     if (btype) {
  157.         for (i = 0; i < level; ++i) fprintf(out, "\t");
  158.         fprintf(out, "boundstype = %s;\n", boundstypes[btype]);
  159.         for (i = 0; i < level; ++i) fprintf(out, "\t");
  160.         fprintf(out, "boundsorig = %ld,%ld,%ld;\n", tx, ty, tz);
  161.         for (i = 0; i < level; ++i) fprintf(out, "\t");
  162.         fprintf(out, "boundslimits = %ld,%ld,%ld;\n", rx, ry, rz);
  163.         }
  164.     for (q = first_hotspot(s, &tx, &ty, &tz); q; q = next_hotspot(q, &tx, &ty, &tz)) {
  165.         for (i = 0; i < level; ++i) fprintf(out, "\t");
  166.         fprintf(out, "hotspot = %ld,%ld,%ld;\n", tx, ty, tz);
  167.         }
  168.     for (p = child_segment(s); p; p = sibling_segment(p))
  169.         writeseg(out, p, level+1);
  170.     name = seg_get_load_info(s, &frx, &fry, &frz, &tx, &ty, &tz);
  171.     for (i = 0; i < level; ++i) fprintf(out, "\t");
  172.     fprintf(out, "plgfile = %s scale %f,%f,%f shift %ld,%ld,%ld;\n", name, frx, fry, frz, tx, ty, tz);
  173.     for (i = 0; i < level; ++i) fprintf(out, "\t");
  174.     fprintf(out, "}\n");
  175.     return 0;
  176.     }
  177.