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 >
Wrap
C/C++ Source or Header
|
1995-12-06
|
4KB
|
181 lines
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dos.h>
#include "matrix.h"
#include "suflib.h"
#include "parts.h"
#include "files.h"
#include "mecha.h"
#include "log.h"
static const int MAX = 32767;
static MechaFile *top = NULL;
Mechanic *MechaFile::OpenMechanic(char *fname)
{
MechaFile *m;
char str[128];
strcpy(str, mechadir.c_str());
strcat(str, fname);
for (m = top; m != NULL; m = m->next) {
if (strcmpi(m->filename, fname) == 0 || strcmpi(m->filename, str) == 0) {
return m->mecha;
}
}
m = new MechaFile(fname);
if (m->mecha == NULL) {
delete m;
m = new MechaFile(str);
if (m->mecha == NULL) {
delete m;
return NULL;
}
}
m->next = top;
top = m;
return m->mecha;
}
MechaFile::MechaFile(char *fname)
{
Parts *parts;
CameraParts *camera;
strcpy(filename, fname);
if (Parts::FileRead(parts, camera, fname, NULL) == FALSE) {
mecha = NULL;
return;
}
mecha = new Mechanic(parts, filename);
// delete parts;
delete camera;
}
MechaFile::~MechaFile()
{
}
Mechanic::Mechanic(Parts *pa, char *fname)
{
parts = pa;
Parts *p;
int ps = 0, ls = 0, pls = 0, pps = 0;
int usepoly = Object::IsPoly();
filename = fname;
char *fp;
if ((fp = strrchr(fname, '\\')) != NULL) {
strcpy(name, fp+1);
} else {
strcpy(name, fname);
}
polys = polypoints = 0;
for (p = pa; p != NULL; p = p->next) {
ps += p->object->points;
ls += p->object->lines;
pls += p->object->polys;
pps += p->object->polypoints;
}
point_x = new int[ps];
point_y = new int[ps];
point_z = new int[ps];
line_1 = new int[ls];
line_2 = new int[ls];
if (usepoly) {
poly = new int[pls];
polypoint = new int[pps];
}
points = ps;
lines = ls;
polys = pls;
polypoints = pps;
ps = ls = pls = pps = 0;
maxx = maxy = maxz = -MAX;
minx = miny = minz = MAX;
for (p = pa; p != NULL; p = p->next) {
int i;
int inverseflag = FALSE;
p->CalcPoints(Matrix(1)
.move(p->position)
.rotz(p->rotation.z)
.roty(p->rotation.y)
.rotx(p->rotation.x)
.scale(p->scale));
if (p->scale.x < 0) inverseflag = !inverseflag;
if (p->scale.y < 0) inverseflag = !inverseflag;
if (p->scale.z < 0) inverseflag = !inverseflag;
for (i = 0; i < p->object->points; ++i) {
point_x[ps+i] = p->point_x[i];
point_y[ps+i] = p->point_y[i];
point_z[ps+i] = p->point_z[i];
if (maxx < p->point_x[i]) maxx = p->point_x[i];
if (minx > p->point_x[i]) minx = p->point_x[i];
if (maxy < p->point_y[i]) maxy = p->point_y[i];
if (miny > p->point_y[i]) miny = p->point_y[i];
if (maxz < p->point_z[i]) maxz = p->point_z[i];
if (minz > p->point_z[i]) minz = p->point_z[i];
}
for (i = 0; i < p->object->lines; ++i) {
line_1[ls+i] = p->line_1[i] + ps;
line_2[ls+i] = p->line_2[i] + ps;
}
if (usepoly) {
if (!inverseflag) {
for (i = 0; i < p->object->polypoints; ++i) {
if (p->object->polypoint[i] != POLY_SEPARATER) {
polypoint[pps+i] = p->object->polypoint[i] + ps;
} else {
polypoint[pps+i] = POLY_SEPARATER;
}
}
for (i = 0; i < p->object->polys; ++i) {
poly[pls+i] = p->object->poly[i] + pps;
}
pps += p->object->polypoints;
pls += p->object->polys;
} else {
poly[pls++] = pps;
for (i = p->object->polypoints-2;i >= 0; --i) {
if (p->object->polypoint[i] == POLY_SEPARATER) {
polypoint[pps++] = POLY_SEPARATER;
poly[pls++] = pps;
} else {
polypoint[pps++] = p->object->polypoint[i] + ps;
}
}
polypoint[pps++] = POLY_SEPARATER;
}
}
ps += p->object->points;
ls += p->object->lines;
}
#if 0
for (int i = 0; i < polys; ++i) {
for (int j = poly[i]; polypoint[j] != POLY_SEPARATER; ++j) {
logprintf("%d..", polypoint[j]);
}
logprintf("\n");
}
#endif
}
Mechanic::~Mechanic()
{
delete parts;
delete point_x;
delete point_y;
delete point_z;
delete line_1;
delete line_2;
}