Category : C Source Code
Archive   : CADSRC.ZIP
Filename : T-17.C

 
Output of file : T-17.C contained in archive : CADSRC.ZIP
/*__________________________________________________________________

t-17.c

FUNCTION: Demonstrates real-time animation techniques in
a flight simulator format.

Compatibility: Supports VGA/EGA graphics adapters and monitors
using 640x200, 16 color mode.

NOTE: You need the *.BGI files that come with Turbo-C for this to run.
___________________________________________________________________

INCLUDE FILES */

#include
#include
#include
#include
#include

/*________________________________________________________________
DECLARATIONS */

void graphics_setup(void);void keyboard(void);void quit_pgm(void);
void notice(float x, float y);void calc_3D(void);void window(void);
void clip_2D(void); void yaw_change(void);void translation(void);
void crash(void);void draw_horizon(void);void horiz3D(void);
void corner(void);void window(void);void grid(void);
void draw_grid(void);void clip_3D(void);void window_terrain(void);
void draw_line(void);

int t1=1,t2=1, p=0,p1=1,p2=1, g=7,solid_clr=0;
int C0=0,C1=1,C2=2,C3=3,C4=4,C5=5,C6=6,C7=7,C8=8,C9=9,C10=10,
C11=11,C12=12,C13=13,C14=14,C15=15,mode_flag=0;

float x=0,y=0,z=0;

float x1=0,x2=0,x3=0,x4=0,x5=0,x6=0,x7=0,x8=0,x9=0,x10=0,x11=0,
x12=0,x13=0,x14=0,x15=0,x16=0;
float y1=0,y2=0,y3=0,y4=0,y5=0,y6=0,y7=0,y8=0,y9=0,y10=0,y11=0,
y12=0,y13=0,y14=0,y15=0,y16=0;
float z1=0,z2=0,z3=0,z4=0,z5=0,z6=0,z7=0,z8=0,z9=0,z10=0,z11=0,
z12=0,z13=0,z14=0,z15=0,z16=0;

float L=0;
float sx=0,sy=0;
float xa=0,ya=0,za=0;
float xb=0,yb=0,zb=0;
float xc=0,yc=0,zc=0;
float sxa=0,sya=0,sxb=0,syb=0;
float sxs=0.0,sys=0.0;
float temp_swap=0.0;
float d=620;

double r1=6.28319;
double r2=6.28319;
double r3=6.28319;
float r1a=0,r2a=0,r3a=0;
double sr1=0,sr2=0,sr3=0;
double cr1=0,cr2=0,cr3=0;
float mx=24,my=-7.7,mz=-88;
float m=1,m1=0,mx1=0,my1=0,mz1=0;
int maxx=639,minx=0,maxy=199,miny=0;
float c=0;
int crash_flag=0;
float rx=0,ry=0;
float screen_x=639,screen_y=199;

/*___________________________________________________________

MAIN ROUTINE */

main()
{
graphics_setup();

rx=screen_x/799;ry=screen_y/599;


RESTART:
setactivepage(p);setvisualpage(1-p);p=1-p;

ANIMATE:
cleardevice();
keyboard();
r2=r2+r2a;r3=r3+r3a;
if (r2>6.28319) r2=r2-6.28319;
if (r2<=0) r2=r2+6.28319;
if (r3>6.28319) r3=r3-6.28319;
if (r3<=0) r3=r3+6.28319;
yaw_change();
r1=r1+r1a;
if (r1>6.28319) r1=r1-6.28319;
if (r1<=0) r1=r1+6.28319;
sr1=sin(r1);sr2=sin(r2);sr3=sin(r3);
cr1=cos(r1);cr2=cos(r2);cr3=cos(r3);
translation();
if (my>0) crash();
if (crash_flag==1)
{
crash_flag=0;goto RESTART;
};
g=C1;draw_horizon();
corner();
grid();
g=C2;draw_grid();
setcolor(C7);circle(319,99,30);
moveto(319,86);lineto(319,112);moveto(290,99);lineto(348,99);
outtextxy(208,0,"Using Turbo C for Flight Simulation ");
setactivepage(p);setvisualpage(1-p);p=1-p;
goto ANIMATE;
}

/*_________________________________________________________________

SUBROUTINE: yaw change

calculates new rla yaw change factor based on
r2 roll angle.
*/

void yaw_change(void)
{
if (r2>=0)
{
if (r2<=1.57079)
{
r1a=(r2/0.017453)*0.00349;return;
};
};

if (r2<=6.28319)
{
if (r2>=4.71239)
{
r1a=((6.28319-r2)/0.017453)*(-0.00349);return;
};
};

if (r2>1.57079)
{
if (r2<=3.14159)
{
r1a=((3.14159-r2)/0.017453)*(0.00349);return;
};
};


if (r2>3.14159)
{
if (r2<4.71239)
{
r1a=((r2-3.14159)/0.017453)*(-0.00349);return;
};
};
return;
}


/*_________________________________________________________________

FUNCTION: movement routine

calculates translation factors
that control movement of viewport over landmarks.
dependent on r1 (yaw) and r3 (pitch).
*/
void translation(void)
{
m1=cr3*m;
my1=(-1)*sr3*m;
if (r3>0)
{
if (r3<=1.57079) my1=cr3*my1;
};
if (r3>1.57079)
{
if (r3<3.14159) my1=(-1)*cr3*my1;
};

mx1=(-1)*sr1*m1;mz1=cr1*m1;
mx=mx+mx1;my=my+my1;mz=mz+mz1;
return;
}

/*_________________________________________________________________

FUNCTION: crash scenario

handles a ground crash.
after a pause user starts over.
*/
void crash(void)
{
setactivepage(0);cleardevice();setvisualpage(0);
setcolor(C12);
moveto(280,80);outtext("C R A S H !");
setcolor(C7);
for (t1=1;t1<=5;t1++)
{
for (t2=1;t2<=30000;t2++);
};
p1=1;g=C7;p=0;
r1=6.28319;r2=6.28319;r3=6.28319;
r1a=0;r2a=0;r3a=0;
mx=24;my=-7.7;mz=-78;m=1;
crash_flag=1;
return;
}

/*_________________________________________________________________

FUNCTION: draws the horizon
*/
void draw_horizon(void)
{
if (r3>1.57079)
{
if (r3<4.71239) goto INVERTED;
};
x=-8000;y=0;z=-10000;horiz3D();window();
sxa=sx;sya=sy;
x=8000;y=0;z=-10000;horiz3D();window();
sxb=sx;syb=sy;
clip_2D();
setcolor(g);moveto(sxa,sya);lineto(sxb,syb);
setfillstyle(SOLID_FILL,BROWN);floodfill(319,199,BLUE);
setfillstyle(SOLID_FILL,BLUE);floodfill(319,0,BLUE);
return;

INVERTED:
x=-8000;y=0;z=10000;horiz3D();window();
sxa=sx;sya=sy;
x=8000;y=0;z=10000;horiz3D();window();
sxb=sx;syb=sy;
clip_2D();
setcolor(g);moveto(sxa,sya);lineto(sxb,syb);
setfillstyle(SOLID_FILL,BROWN);floodfill(319,0,BLUE);
setfillstyle(SOLID_FILL,BLUE);floodfill(319,199,BLUE);
return;
}

/*_________________________________________________________________

FUNCTION: 3D formulas for horizon
*/
void horiz3D(void)
{
x=(-1)*x;za=cr3*z-sr3*y;ya=sr3*z+cr3*y;xa=cr2*x+sr2*ya;
y=cr2*ya-sr2*x;sx=d*xa/za;sy=d*y/za;
return;
}

/*_____________________________________________________________

SUBROUTINE: 2D Line-clipping

enter with sxa,sya and sxb,syb endpoints of line to be clipped.
returns display coordinates for line clipped to fit physical
screen viewport defined by minx,miny and maxx,maxy.
sets toggle flag p2 to zero if entire line is off screen.
*/

void clip_2D(void)
{
if (sxa>sxb)
{
temp_swap=sxa;sxa=sxb;sxb=temp_swap;
temp_swap=sya;sya=syb;syb=temp_swap;
};
if (sxa if (sxa>maxx) {if (sxb>maxx) {p2=0;return;}};
if (sya if (sya>maxy) {if (syb>maxy) {p2=0;return;}};

if (sxa {

c=(syb-sya)/(sxb-sxa)*(sxb-minx);
sxa=minx;sya=syb-c;
if (sya if (sya>maxy) if (syb>maxy) return;
};

if (sxb>maxx)
{

c=(syb-sya)/(sxb-sxa)*(maxx-sxa);
sxb=maxx;syb=sya+c;
if (sya if (sya>maxy) if (syb>maxy) return;
};

if (sya>syb)
{
temp_swap=sya;sya=syb;syb=temp_swap;
temp_swap=sxa;sxa=sxb;sxb=temp_swap;
};

if (sya {
c=(sxb-sxa)/(syb-sya)*(syb-miny);
sxa=sxb-c;sya=miny;
};
if (syb>maxy)
{
c=(sxb-sxa)/(syb-sya)*(maxy-sya);
sxb=sxa+c;syb=maxy;
};
return;
}

/*_________________________________________________________________

FUNCTION: window mapping function for horizon
*/
void window(void)
{
sx=sx+399;sy=sy+299;rx=screen_x/799;ry=screen_y/599;sx=sx*rx;sy=sy*ry;
return;
}


/*_________________________________________________________________

FUNCTION: view coords for corners of terrain
calculates rotated/translated view coordinates
for 4 corners of terrain. rest of terrain vertices can be
extrapolated from these 4 vertices.
*/
void corner(void)
{
x=-80;y=0;z=-80;calc_3D(); x1=x; y1=y; z1=z;
x= 80;y=0;z=-80;calc_3D(); x2=x; y2=y; z2=z;
x= 80;y=0;z= 80;calc_3D(); x3=x; y3=y; z3=z;
x=-80;y=0;z= 80;calc_3D(); x4=x; y4=y; z4=z;
return;
}

/*______________________________________________________________

SUBROUTINE: 3D formulas (generic), translation/rotation
*/

void calc_3D(void)
{
x=x-mx;y=y+my;z=z+mz;
xa=cr1*x-sr1*z;za=sr1*x+cr1*z;
z=cr3*za-sr3*y;ya=sr3*za+cr3*y;
x=cr2*xa+sr2*ya;y=cr2*ya-sr2*xa;
return;
}

/*_________________________________________________________________

FUNCTION: extrapolation of vertices for grid
uses simple geometry to extrapolate 16 vertices of grid in
3D space from 4 known corners.
*/

void grid(void)
{
L=0.25*(x2-x1);x5=x1+L;x6=x5+L;x7=x6+L;
L=0.25*(y2-y1);y5=y1+L;y6=y5+L;y7=y6+L;
L=0.25*(z2-z1);z5=z1+L;z6=z5+L;z7=z6+L;

L=0.25*(x3-x4); x8=x4+L; x9=x8+L;x10=x9+L;
L=0.25*(y3-y4); y8=y4+L; y9=y8+L;y10=y9+L;
L=0.25*(z3-z4); z8=z4+L; z9=z8+L;z10=z9+L;

L=0.25*(x4-x1);x11=x1+L;x12=x11+L;x13=x12+L;
L=0.25*(y4-y1);y11=y1+L;y12=y11+L;y13=y12+L;
L=0.25*(z4-z1);z11=z1+L;z12=z11+L;z13=z12+L;

L=0.25*(x3-x2);x14=x2+L;x15=x14+L;x16=x15+L;
L=0.25*(y3-y2);y14=y2+L;y15=y14+L;y16=y15+L;
L=0.25*(z3-z2);z14=z2+L;z15=z14+L;z16=z15+L;

return;
}

/*_____________________________________________________________

FUNCTION: draw the grid terrain
draws 16 square grid.
if p1=0 then line is clipped + invisible.
*/
void draw_grid(void)
{
setcolor(g);
xa=x1;ya=y1;za=z1;xb=x2;yb=y2;zb=z2;p1=1;clip_3D();
if (p1==1) draw_line();
xa=x11;ya=y11;za=z11;xb=x14;yb=y14;zb=z14;p1=1;clip_3D();
if (p1==1) draw_line();
xa=x12;ya=y12;za=z12;xb=x15;yb=y15;zb=z15;p1=1;clip_3D();
if (p1==1) draw_line();
xa=x13;ya=y13;za=z13;xb=x16;yb=y16;zb=z16;p1=1;clip_3D();
if (p1==1) draw_line();
xa=x4;ya=y4;za=z4;xb=x3;yb=y3;zb=z3;p1=1;clip_3D();
if (p1==1) draw_line();

xa=x1;ya=y1;za=z1;xb=x4;yb=y4;zb=z4;p1=1;clip_3D();
if (p1==1) draw_line();
xa=x5;ya=y5;za=z5;xb=x8;yb=y8;zb=z8;p1=1;clip_3D();
if (p1==1) draw_line();
xa=x6;ya=y6;za=z6;xb=x9;yb=y9;zb=z9;p1=1;clip_3D();
if (p1==1) draw_line();
xa=x7;ya=y7;za=z7;xb=x10;yb=y10;zb=z10;p1=1;clip_3D();
if (p1==1) draw_line();
xa=x2;ya=y2;za=z2;xb=x3;yb=y3;zb=z3;p1=1;clip_3D();
if (p1==1) draw_line();

return;
}


/*_____________________________________________________________

FUNCTION: draw clipped line on screen
*/
void draw_line(void)
{
p2=1;clip_2D();
if (p2==1) {moveto(sxa,sya);lineto(sxb,syb);};
return;
}


/*_____________________________________________________________

FUNCTION: window mapping for terrain
maps a world space window of 800x600
to fit 640x200 screen, thus ensuring integrity of
4:3 aspect (screen) ratio to avoid distortion
in 3D rotations.
*/
void window_terrain(void)
{
sxa=sxa+399;sya=sya+299;sxa=sxa*rx;sya=sya*ry;
sxb=sxb+399;syb=syb+299;sxb=sxb*rx;syb=syb*ry;

return;
}

/*_____________________________________________________________

FUNCTION: clip lines in 3D space
*/
void clip_3D(void)
{
if (za>=-1) goto LABEL1630;
goto LABEL1640;

LABEL1630:
if (zb>=-1) {p1=0;return;};
temp_swap=xb;xb=xa;xa=temp_swap;
temp_swap=yb;yb=ya;ya=temp_swap;
temp_swap=zb;zb=xa;za=temp_swap;
goto LABEL1660;

LABEL1640:
if (zb>=-1) goto LABEL1660;

LABEL1650:
sxa=d*xa/za;sya=d*ya/za;sxb=d*xb/zb;syb=d*yb/zb;
window_terrain();return;

LABEL1660:
c=(xb-xa)/(zb-za)*(zb+1);xc=xb-c;
c=(yb-ya)/(zb-za)*(zb+1);yc=yb-c;zc=-1;
xb=xc;yb=yc;zb=zc;goto LABEL1650;

}

/*_______________________________________________________________
SUBROUTINE: check the keyboard buffer
ESC terminates flight.
rolls right
rolls left
nose down
nose up
hold atittude
<+> faster <-> slower
*/
void keyboard(void)
{
union u_type {int a;char b[3];} keystroke;char inkey=0;

if (bioskey(1)==0) return;
keystroke.a=bioskey(0);
inkey=keystroke.b[0];
switch (inkey)
{
case 27: quit_pgm();
case 104: r2a=r2a+0.017453; return;
case 102: r2a=r2a-0.017453; return;
case 116: r3a=r3a-0.008726; return;
case 98: r3a=r3a+0.008726; return;
case 103: r2a=0;r3a=0; return;
case 61: m=m+0.1; return;
case 45: m=m-0.1; return;
default: return;}
}
/*____________________________________________________________________

SUBROUTINE: graceful exit from the program */

void quit_pgm(void)
{
setvisualpage(0);setactivepage(0);
cleardevice(); restorecrtmode();exit(0);
}
/*___________________________________________________________________
SUBROUTINE: vga/ega/cga/mcga compatibility module */

void graphics_setup(void)
{
int graphics_adapter,graphics_mode;
detectgraph(&graphics_adapter,&graphics_mode);
if (graphics_adapter==VGA) goto VGA_EGA_mode;
if (graphics_mode==EGAHI) goto VGA_EGA_mode;
if (graphics_mode==EGALO) goto VGA_EGA_mode;
if (graphics_adapter==CGA) goto abort_message;
if (graphics_adapter==MCGA) goto abort_message;
goto abort_message;

VGA_EGA_mode:
graphics_adapter=EGA;graphics_mode=EGALO;
initgraph(&graphics_adapter,&graphics_mode,"");
maxx=639;minx=0;maxy=199;miny=0;screen_x=639;screen_y=199;
setcolor(C7);
return;

abort_message:
printf("\n\nUnable to proceed.\n");
printf("Requires VGA or EGA adapter\n");
printf(" with appropriate monitor.\n");
printf("Please refer to LEE ADAMS book HIGH-Performance CAD Graphics in TURBO C.\n\n");
exit(0);
}

/*__________________________________________________________
SUBROUTINE: copyright notice */

int copyright[][3]={0x7c00,0x0000,0x0000,0x8231,
0x819c,0x645e,0xba4a,0x4252,0x96d0,0xa231,0x8252,0x955e,0xba4a,
0x43d2,0xf442,0x8231,0x825c,0x945e,0x7c00,0x0000,0x0000};

void notice(float x, float y)
{
int a,b,c; int t1=0;
for (t1=0;t1<=6;t1++){a=copyright[t1][0];b=copyright[t1][1];
c=copyright[t1][2];
setlinestyle(USERBIT_LINE,a,NORM_WIDTH);
moveto(x,y);lineto(x+15,y);
setlinestyle(USERBIT_LINE,b,NORM_WIDTH);
moveto(x+16,y);lineto(x+31,y);
setlinestyle(USERBIT_LINE,c,NORM_WIDTH);
moveto(x+32,y);lineto(x+47,y);y=y+1;
}
setlinestyle(USERBIT_LINE,0xFFFF,NORM_WIDTH);
return;
}
/* END */



  3 Responses to “Category : C Source Code
Archive   : CADSRC.ZIP
Filename : T-17.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/