summaryrefslogtreecommitdiff
path: root/stern.cpp
blob: c94f44979d93197599c4935023a094687e89f395 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// STERN.CPP 11.02.2000
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <process.h>
#include <math.h>

void InitProgram(void);
void MainLoop(void);
void Calc3D(void);
void Rotation(void);
void DrawObject(void);

#define COPYRIGHT "Dark Alliance (c) 1996-2000 DreamDemon"
#define Lines 72
static int World[Lines][3] = {
	 90,  0,  0,   10, 10, 10,     90,  0,  0,   10, 10,-10,
	 90,  0,  0,   10,-10,-10,     90,  0,  0,   10,-10, 10,
	-90,  0,  0,  -10, 10, 10,    -90,  0,  0,  -10, 10,-10,
	-90,  0,  0,  -10,-10,-10,    -90,  0,  0,  -10,-10, 10,
	  0, 90,  0,   10, 10, 10,      0, 90,  0,  -10, 10, 10,
	  0, 90,  0,  -10, 10,-10,      0, 90,  0,   10, 10,-10,
	  0,-90,  0,   10,-10, 10,      0,-90,  0,  -10,-10, 10,
	  0,-90,  0,  -10,-10,-10,      0,-90,  0,   10,-10,-10,
	  0,  0, 90,   10, 10, 10,      0,  0, 90,   10,-10, 10,
	  0,  0, 90,  -10,-10, 10,      0,  0, 90,  -10, 10, 10,
	  0,  0,-90,   10, 10,-10,      0,  0,-90,   10,-10,-10,
	  0,  0,-90,  -10,-10,-10,      0,  0,-90,  -10, 10,-10,
	 10, 10, 10,  -10, 10, 10,     10,-10, 10,  -10,-10, 10,
	 10, 10,-10,  -10, 10,-10,     10,-10,-10,  -10,-10,-10,
	 10, 10, 10,   10,-10, 10,    -10, 10, 10,  -10,-10, 10,
	 10, 10,-10,   10,-10,-10,    -10, 10,-10,  -10,-10,-10,
	 10, 10, 10,   10, 10,-10,     10,-10, 10,   10,-10,-10,
	-10,-10, 10,  -10,-10,-10,    -10, 10, 10,  -10, 10,-10};

float X, Y, Z, sX, sY, Xa, Ya, Za, sXs, sYs, D, Page;
float R1, R2, R3, Sr1, Sr2, Sr3, Cr1, Cr2, Cr3, mX, mY, mZ;

void main(void)
{
	InitProgram();
	MainLoop();
	closegraph();
	printf("%s\n",COPYRIGHT);
	getch();
}

void Calc3D(void)
{
	X  = -1 * X;
	Xa = Cr1 * X - Sr1 * Z;
	Za = Sr1 * X + Cr1 * Z;
	X  = Cr2 * Xa + Sr2 * Y;
	Ya = Cr2 * Y - Sr2 * Xa;
	Z  = Cr3 * Za - Sr3 * Ya;
	Y  = Sr3 * Za + Cr3 * Ya;
	X  = X + mX;
	Y  = Y + mY;
	Z  = Z + mZ;
	sX = 320 + D * X / Z;
	sY = 175 + D * Y / Z;
}

void DrawObject(void)
{
	int i;

	Rotation();
	for(i=0; i<Lines; i+=2) {
		X = World[i][0];
		Y = World[i][1];
		Z = World[i][2];
		Calc3D();
		sXs = sX;
		sYs = sY;
		X = World[i+1][0];
		Y = World[i+1][1];
		Z = World[i+1][2];
		Calc3D();
		line(sXs, sYs, sX, sY);
	}
}

void InitProgram(void)
{
	int gdriver = EGA, gmode = EGAHI, errorcode;
	initgraph(&gdriver, &gmode, "");
	errorcode = graphresult();
	if (errorcode != grOk) {
		printf("Graphics error: %s\n", grapherrormsg(errorcode));
		printf("Press any key to halt:");
		getch();
		exit(1);
	}
	D  = 1200;      // View point and rotation values
	mZ = -850;
	mX = -5;
	R1 = 0;
	R2 = 0;
	R3 = 0.3;
}

void MainLoop(void)
{
	while(!kbhit()) {
		R1 = R1 + 0.1;
		if(R1 > 6.28) R1 = 0;
		R2 = R1;
		R3 = R1;
		clearviewport();
		DrawObject();
		(Page == 0)? Page = 1: Page = 0;
		setactivepage(1-Page);
		setvisualpage(Page);
	}
}

void Rotation(void)
{
	Sr1 = sin(R1);
	Sr2 = sin(R2);
	Sr3 = sin(R3);
	Cr1 = cos(R1);
	Cr2 = cos(R2);
	Cr3 = cos(R3);
}