-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPARTICL3.CPP
More file actions
279 lines (245 loc) · 8.46 KB
/
PARTICL3.CPP
File metadata and controls
279 lines (245 loc) · 8.46 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Elementary particle system, amit moskovich.
#include "dgfx2" // Graphic functions
#include <conio> // inp
#include <stdlib> // rand
#include <math> // sin, cos,
#include <timer> // Timing code.
#include <iostream>
struct Vec2D {
float X,Y;
};
struct Particle {
Vec2D Position,Velocity;
};
#define Blur
//#define GravsMove
const int MaxColor=31;
const float Pi=3.1415926535897932384626;
const int ParticleNumber=2048;
const int GravNumber=2;
const float GravStrength=2;
//const Vec2D GravCenter={80,100};
//const Vec2D GravCenter2={220,100};
Particle Gravs[GravNumber]; // Gravitational centers.
Particle Particles[ParticleNumber];
float XPos,YPos,YVel,XVel,BlastStrength;
Vec2D VecAdd(Vec2D V1, Vec2D V2)
{
V1.X+=V2.X;
V1.Y+=V2.Y;
return V1;
}
Vec2D VecSub(Vec2D V1, Vec2D V2)
{
V1.X-=V2.X;
V1.Y-=V2.Y;
return V1;
}
Vec2D VecMul(Vec2D V, float Scalar)
{
V.X*=Scalar;
V.Y*=Scalar;
return V;
}
float DotProduct(Vec2D V1, Vec2D V2)
{
return V1.X*V2.X+V1.Y*V2.Y;
}
float VecLength(Vec2D V)
{
return sqrt(DotProduct(V,V));
}
float FloatSqr(float F)
{
return F*F;
}
void InitGravs()
{
Gravs[0].Position.X=120; Gravs[0].Position.Y=100;
Gravs[0].Velocity.X= 0; Gravs[0].Velocity.Y=0.2;
Gravs[1].Position.X=200; Gravs[1].Position.Y=100;
Gravs[1].Velocity.X= 0; Gravs[1].Velocity.Y=-0.2;
// Gravs[2].Position.X=160; Gravs[2].Position.Y=70;
// Gravs[2].Velocity.X=0; Gravs[2].Velocity.Y=0.5;
// Gravs[3].Position.X=160; Gravs[3].Position.Y=130;
// Gravs[3].Velocity.X=0; Gravs[3].Velocity.Y=-0.5;
}
void InitParticles()
{
for (int i=0; i<ParticleNumber; i++) {
Particles[i].Position.X=XPos; Particles[i].Position.Y=YPos;
Particles[i].Velocity.X=XVel; Particles[i].Velocity.Y=YVel;
// Set initial position 160,100 for all particle.
// Particles[i].Position.X=ScreenX/2;
// Particles[i].Position.Y=ScreenY/2;
// Set random angle (0 - 2pi) and velocity (0 - 1).
float Angle=float(rand())/32768*Pi*2;
float Velocity=float(rand())/32768*BlastStrength;
Particles[i].Velocity.X+=cos(Angle)*Velocity;
Particles[i].Velocity.Y+=sin(Angle)*Velocity;
}
}
void DrawParticles(char* Buffer)
{
for (int i=0; i<ParticleNumber; i++) {
PutPixel(Particles[i].Position.X, Particles[i].Position.Y, MaxColor, Buffer);
}
}
void MoveParticles()
{
// For each particle, add velocity to position.
// Multiply velocity by 0.95 to simulate friction.
for (int i=0; i<ParticleNumber; i++) {
Particles[i].Position.X+=Particles[i].Velocity.X;
Particles[i].Position.Y+=Particles[i].Velocity.Y;
// Particles[i].Velocity.Y*=1;
// Particles[i].Velocity.X*=1;
}
#ifdef GravsMove
for (i=0; i<GravNumber; i++) {
Gravs[i].Position.X+=Gravs[i].Velocity.X;
Gravs[i].Position.Y+=Gravs[i].Velocity.Y;
}
#endif
}
Particle CalcGravity(Particle P, Vec2D Grav)
{
Vec2D Dif=VecSub(Grav,P.Position);
float Force=1/FloatSqr(VecLength(Dif));
Vec2D AddedDif=VecMul(Dif,Force*GravStrength);
P.Velocity=VecAdd(P.Velocity,AddedDif);
return P;
}
void Gravity()
{
for (int i=0; i<GravNumber; i++) {
for (int j=0; j<ParticleNumber; j++) {
// Difference=GravCenter-Position
// Force=1/|Difference|ý
// AddedDifference=Difference*Force;
Particles[j]=CalcGravity(Particles[j],Gravs[i].Position);
}
}
#ifdef GravsMove
for (i=0; i<GravNumber; i++)
for (int j=0; j<GravNumber; j++)
if (i!=j) Gravs[j]=CalcGravity(Gravs[j], Gravs[i].Position);
#endif
}
void DarkenScreen(char* Buffer)
{
for (int y=0; y<ScreenY; y++)
for (int x=0; x<ScreenX; x++) {
char Temp=Buffer[y*ScreenX+x];
if (Temp>0) Temp--;
Buffer[y*ScreenX+x]=Temp;
}
}
void FastBlur(char* Buffer, char* TempBuffer); //320x200 only!!
#pragma aux FastBlur=\
"mov edx,320 "\
"YLoop: "\
" mov ecx,1 "\
" XLoop: "\
" push edx "\
" lea ebx,[edx+ecx] "\
" add ebx,esi "\
" mov eax,0 "\
" mov dx,word ptr [ebx-321] "\
" mov al,dl "\
" add al,dh "\
" adc ah,0 "\
" mov dx,word ptr [ebx-1] "\
" add al,dl "\
" adc ah,0 "\
" add al,dh "\
" adc ah,0 "\
" mov dx,word ptr [ebx+319] "\
" add al,dl "\
" adc ah,0 "\
" add al,dh "\
" adc ah,0 "\
" add al,byte ptr [ebx-319] "\
" adc ah,0 "\
" add al,byte ptr [ebx+1] "\
" adc ah,0 "\
" pop edx "\
" shr eax,3 "\
" lea ebx,[edx+ecx] "\
" mov [edi+ebx],al "\
" "\
" inc ecx "\
" cmp ecx,319 "\
" jb XLoop "\
" add edx,320 "\
" cmp edx,64000-320 "\
"jb YLoop "\
parm [esi] [edi] modify [eax ebx ecx edx edi esi];
void main()
{
srand(*((unsigned short*)0x46c));
// XPos=rand()%ScreenX; YPos=rand()%ScreenY;
// XVel=float(rand())/32768-0.5; YVel=float(rand())/32768-0.5;
// BlastStrength=float(rand())/32768*2;
// XPos=260; YPos=70; XVel=-0.2145; YVel=0.0432; BlastStrength=1.60345;
// XPos=186; YPos=108; XVel=-0.141632; YVel=0.069; BlastStrength=0.0534;
// XPos=92; YPos=139; XVel=0.276611; YVel=0.150513; BlastStrength=0.0355408;
// XPos=130; YPos=140; XVel=1.76611; YVel=-0.60513; BlastStrength=0.0355408;
// XPos=145; YPos=140; XVel=1.76611; YVel=-0.60513; BlastStrength=0.0555408;
XPos=133; YPos=140; XVel=1.76611; YVel=-0.60513; BlastStrength=0.055;
char* VirScr=new char[ScreenX*ScreenY];
char* TempScr=new char[ScreenX*ScreenY];
SetScreen(VirScr,0);
SetScreen(TempScr,0);
SetScreenMode(0x13); // Set Vga 320x200.
Gradient(0,MaxColor/2,0,0,0,32,0,63);
Gradient(MaxColor/2+1,MaxColor,32,0,63,63,63,63);
SetRgb(255,32,63,0);
// DisplayPalette(VgaBuffer);
// while (inp(0x60)>127);
InitGravs();
InitParticles();
int LoopCounter=0;
float Light=1;
int FadingNow=0;
StartTimer();
while ((inp(0x60)!=1)&&(Light>0)&&(LoopCounter<100)) // While escape not pressed.
{
if (Light<0) Light=0;
Gradient(0,MaxColor/2,0,0,0,32*Light,0,63*Light);
Gradient(MaxColor/2+1,MaxColor,32*Light,0,63*Light,63*Light,63*Light,63*Light);
if (inp(0x60)<128) FadingNow=1;
if (FadingNow)
{
Light-=0.001;
for (int i=0; i<ParticleNumber; i++)
Particles[i].Velocity=VecMul(Particles[i].Velocity,1.02);
}
#ifdef Blur
DrawParticles(VirScr);
FastBlur(VirScr,TempScr);
MoveScreen(TempScr,VgaBuffer);
MoveScreen(TempScr,VirScr);
#endif
#ifndef Blur
SetScreen(VirScr,0);
DrawParticles(VirScr);
MoveScreen(VirScr,VgaBuffer);
#endif
// for (int i=0; i<GravNumber; i++)
// PutPixel(Gravs[i].Position.X, Gravs[i].Position.Y, 255, VgaBuffer);
MoveParticles();
Gravity();
LoopCounter++;
}
SetScreenMode(3);
DisplayTime(LoopCounter);
delete[] TempScr;
delete[] VirScr;
cout << "\nNumber of particles: " << ParticleNumber << ".\n";
cout << "Number of Gravitational poles: " << GravNumber << ".\n";
cout << "Gravitational scaling factor: " << GravStrength << ".\n\n";
cout << "Initial explosion position: " << XPos << ", " << YPos << ".\n";
cout << "Initial speed: " << XVel << ", " << YVel << ".\n";
cout << "Blast strength: " << BlastStrength << ".\n";
}