home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 March / VPR9703A.ISO / VPR_DATA / DOGA / SOURCES / MEDIT.LZH / MECHA.CPP < prev    next >
C/C++ Source or Header  |  1995-12-06  |  4KB  |  181 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <dos.h>
  5. #include "matrix.h"
  6. #include "suflib.h"
  7. #include "parts.h"
  8. #include "files.h"
  9. #include "mecha.h"
  10.  
  11. #include "log.h"
  12.  
  13. static const int MAX = 32767;
  14.  
  15. static MechaFile *top = NULL;
  16.  
  17. Mechanic *MechaFile::OpenMechanic(char *fname)
  18. {
  19.     MechaFile *m;
  20.     char str[128];
  21.     strcpy(str, mechadir.c_str());
  22.     strcat(str, fname);
  23.     for (m = top; m != NULL; m = m->next) {
  24.         if (strcmpi(m->filename, fname) == 0 || strcmpi(m->filename, str) == 0) {
  25.             return m->mecha;
  26.         }
  27.     }
  28.     m = new MechaFile(fname);
  29.     if (m->mecha == NULL) {
  30.         delete m;
  31.         m = new MechaFile(str);
  32.         if (m->mecha == NULL) {
  33.             delete m;
  34.             return NULL;
  35.         }
  36.     }
  37.     m->next = top;
  38.     top = m;
  39.     return m->mecha;
  40. }
  41.  
  42. MechaFile::MechaFile(char *fname)
  43. {
  44.     Parts *parts;
  45.     CameraParts *camera;
  46.  
  47.     strcpy(filename, fname);
  48.     if (Parts::FileRead(parts, camera, fname, NULL) == FALSE) {
  49.         mecha = NULL;
  50.         return;
  51.     }
  52.  
  53.     mecha = new Mechanic(parts, filename);
  54.  
  55. //    delete parts;
  56.     delete camera;
  57. }
  58.  
  59. MechaFile::~MechaFile()
  60. {
  61. }
  62.  
  63. Mechanic::Mechanic(Parts *pa, char *fname)
  64. {
  65.     parts = pa;
  66.     Parts *p;
  67.     int ps = 0, ls = 0, pls = 0, pps = 0;
  68.     int usepoly = Object::IsPoly();
  69.  
  70.     filename = fname;
  71.     char *fp;
  72.     if ((fp = strrchr(fname, '\\')) != NULL) {
  73.         strcpy(name, fp+1);
  74.     } else {
  75.         strcpy(name, fname);
  76.     }
  77.  
  78.     polys = polypoints = 0;
  79.     for (p = pa; p != NULL; p = p->next) {
  80.         ps += p->object->points;
  81.         ls += p->object->lines;
  82.         pls += p->object->polys;
  83.         pps += p->object->polypoints;
  84.     }
  85.     point_x = new int[ps];
  86.     point_y = new int[ps];
  87.     point_z = new int[ps];
  88.     line_1 = new int[ls];
  89.     line_2 = new int[ls];
  90.     if (usepoly) {
  91.         poly = new int[pls];
  92.         polypoint = new int[pps];
  93.     }
  94.     points = ps;
  95.     lines = ls;
  96.     polys = pls;
  97.     polypoints = pps;
  98.     ps = ls = pls = pps = 0;
  99.     maxx = maxy = maxz = -MAX;
  100.     minx = miny = minz =  MAX;
  101.     for (p = pa; p != NULL; p = p->next) {
  102.         int i;
  103.         int inverseflag = FALSE;
  104.         p->CalcPoints(Matrix(1)
  105.                     .move(p->position)
  106.                     .rotz(p->rotation.z)
  107.                     .roty(p->rotation.y)
  108.                     .rotx(p->rotation.x)
  109.                     .scale(p->scale));
  110.         if (p->scale.x < 0) inverseflag = !inverseflag;
  111.         if (p->scale.y < 0) inverseflag = !inverseflag;
  112.         if (p->scale.z < 0) inverseflag = !inverseflag;
  113.  
  114.         for (i = 0; i < p->object->points; ++i) {
  115.             point_x[ps+i] = p->point_x[i];
  116.             point_y[ps+i] = p->point_y[i];
  117.             point_z[ps+i] = p->point_z[i];
  118.             if (maxx < p->point_x[i]) maxx = p->point_x[i];
  119.             if (minx > p->point_x[i]) minx = p->point_x[i];
  120.             if (maxy < p->point_y[i]) maxy = p->point_y[i];
  121.             if (miny > p->point_y[i]) miny = p->point_y[i];
  122.             if (maxz < p->point_z[i]) maxz = p->point_z[i];
  123.             if (minz > p->point_z[i]) minz = p->point_z[i];
  124.         }
  125.         for (i = 0; i < p->object->lines; ++i) {
  126.             line_1[ls+i] = p->line_1[i] + ps;
  127.             line_2[ls+i] = p->line_2[i] + ps;
  128.         }
  129.         if (usepoly) {
  130.             if (!inverseflag) {
  131.                 for (i = 0; i < p->object->polypoints; ++i) {
  132.                     if (p->object->polypoint[i] != POLY_SEPARATER) {
  133.                         polypoint[pps+i] = p->object->polypoint[i] + ps;
  134.                     } else {
  135.                         polypoint[pps+i] = POLY_SEPARATER;
  136.                     }
  137.                 }
  138.                 for (i = 0; i < p->object->polys; ++i) {
  139.                     poly[pls+i] = p->object->poly[i] + pps;
  140.                 }
  141.                 pps += p->object->polypoints;
  142.                 pls += p->object->polys;
  143.             } else {
  144.                 poly[pls++] = pps;
  145.                 for (i = p->object->polypoints-2;i >= 0; --i) {
  146.                     if (p->object->polypoint[i] == POLY_SEPARATER) {
  147.                         polypoint[pps++] = POLY_SEPARATER;
  148.                         poly[pls++] = pps;
  149.                     } else {
  150.                         polypoint[pps++] = p->object->polypoint[i] + ps;
  151.                     }
  152.                 }
  153.                 polypoint[pps++] = POLY_SEPARATER;
  154.             }
  155.         }
  156.         ps += p->object->points;
  157.         ls += p->object->lines;
  158.     }
  159. #if 0
  160.     for (int i = 0; i < polys; ++i) {
  161.         for (int j = poly[i]; polypoint[j] != POLY_SEPARATER; ++j) {
  162.             logprintf("%d..", polypoint[j]);
  163.         }
  164.         logprintf("\n");
  165.     }
  166. #endif
  167.  
  168. }
  169.  
  170. Mechanic::~Mechanic()
  171. {
  172.     delete parts;
  173.     delete point_x;
  174.     delete point_y;
  175.     delete point_z;
  176.     delete line_1;
  177.     delete line_2;
  178. }
  179.  
  180.  
  181.