-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpart.c
More file actions
403 lines (299 loc) · 9.13 KB
/
part.c
File metadata and controls
403 lines (299 loc) · 9.13 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include <allegro.h>
#include <math.h>
#include "blocks3.h"
int counter;
particle *list = NULL; // make sure it starts by being NULL!!!
struct particle *create_snow_flake(struct particle *parent);
struct particle *clean_particles(BITMAP *bmp, struct particle *t,int all);
void draw_particles(BITMAP *bmp, struct particle *t);
void update_particles(struct particle *t) ;
void remove_particle_drawing(BITMAP *bmp, struct particle *t);
void Add_Part(int x, int y, int dx, int dy, int col)
{
part[part_count].x = x;
part[part_count].y = y;
part[part_count].dx = dx;
part[part_count].dy = dy;
part[part_count].col = col;
part_count++;
}
void Del_Part(int p_no)
{
int i;
for (i = p_no; i < part_count; i++)
{
part[i].x = part[i + 1].x;
part[i].y = part[i + 1].y;
part[i].dx = part[i + 1].dx;
part[i].dy = part[i + 1].dy;
part[i].col = part[i + 1].col;
}
part_count--;
}
void Particle(void)
{
#if 0
if (part_count < MAX_PARTICLES)
{
if (rand()%2 == 1) Add_Part(320 + rand()%16, 200 + rand()%32, (rand()%50 + 20), -(rand()%10 + 5), rand()%255);
else Add_Part(320 + rand()%16, 200 + rand()%32, -(rand()%50 + 20), -(rand()%10 + 5), rand()%255);
}
#else
//list=create_snow_flake(list);
#endif
Particle_Move();
}
void Particle_Move(void)
{
#if 0
int i;
counter++;
if (counter > 50) counter = 0;
for (i = 0; i < part_count; i++)
{
putpixel(screen, part[i].x, part[i].y, getpixel(temp, part[i].x, part[i].y));
if (!(counter%part[i].dx)) part[i].x += part[i].dx/abs(part[i].dx);
if (!(counter%part[i].dy)) part[i].y += part[i].dy/abs(part[i].dy);
part[i].col++;
if (part[i].col == 256)
{
Del_Part(i);
i--;
}
else
{
putpixel(screen, part[i].x, part[i].y, makecol8(255, 255 - part[i].col, 255 - part[i].col));
if ((part[i].col > 100) && (part_count < 900))
{
Add_Part(part[i].x, part[i].y + 1, part[i].dx, part[i].dy, part[i].col);
}
}
}
#else
if (!_particles)
return;
blit(screen, temp_particle, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
list=clean_particles(temp_particle, list,FALSE);
if (part_count < 25)
list=create_snow_flake(list);
update_particles(list);
draw_particles(temp_particle,list);
blit(temp_particle, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
#endif
}
void Reset_Particles()
{
part_count = 0; // old
list=clean_particles(NULL, list,FALSE);
}
struct particle *create_particle(struct particle *parent)
{
struct particle *tmp;
tmp = (struct particle *)malloc(sizeof(struct particle));
tmp->next = parent;
part_count++;
return tmp;
}
struct particle *create_rain_drop(struct particle *parent,double direction)
{
struct particle *tmp;
double r;
tmp = create_particle (parent); // We allocate the memory for the particle here.
// place the rain in a random place on the screen
tmp->x = (double) (rand()%(SCREEN_W+40))-20;
tmp->y = (double) (rand()%SCREEN_H)-60;
// decide speed of the particle
r = ((double)(rand()%30))/10+20; // good rain needs to be fast! ;)
// set the speed & direction
tmp->dx = r*sin(direction);
tmp->dy = -r*cos(direction);
// rx & ry should be 0 since we don't want the rain to wobble.
tmp->rx = 0;
tmp->ry = 0;
// don't forget to add life to the particle.
tmp->life = rand()%30+60;
// I like my rain grey coloured ;)
tmp->colour = makecol(220,220,220);
// and finally set the drawing type
tmp->type = RAIN_PARTICLE;
tmp->first_update = 1;
return tmp;
}
struct particle *create_snow_flake(struct particle *parent)
{
struct particle *tmp;
double r,direction;
int c;
tmp = create_particle (parent); // once again start by creating the particle
// place the snow in a random place on the screen
tmp->x = (double) (rand()%(SCREEN_W+20))-20;
tmp->y = (double) (rand()%(SCREEN_H+20))-20;
// decide speed and direction of this particle of snow
r = (double) (rand()%15)/15+1; // notice the snow is much slower than the rain was
direction=rand()%15; // 15; //rand()%64+64;
// set the speed & direction
tmp->dx = r*sin(direction);
tmp->dy = -r*cos(direction);
// snow is very light so it tends to be blown about, so we add some randomness
// to its movement.
tmp->rx = 1;
tmp->ry = 1;
// don't forget to add life to the particle.
tmp->life = 1; //rand()%30+60;
// snow should be all white.. but I made it a random shade of grey to make it look
// more... interesting.
c = rand()%128+128;
tmp->colour=makecol(c,c,c);
// and finally set the drawing type
// randomly set it to either of the two flake types.
tmp->type = rand()%2+SNOW_PARTICLE1;
tmp->first_update = 1;
return tmp;
}
struct particle *clean_particles(BITMAP *bmp, struct particle *t,int all)
{
struct particle *tmp;
struct particle *top;
top=t;
tmp=NULL;
while(t) // basically we are looping through all the particles in the list.
{
if(t->life<=0||all) { // is the particle is dead or if we are deleting all particles
if(tmp) { // then delete it!
if (bmp != NULL)
remove_particle_drawing(bmp, t);
tmp->next=t->next;
free(t);
t=tmp->next;
part_count--;
}
else {
if (bmp != NULL)
remove_particle_drawing(bmp, t);
top=t->next;
free(t);
t=top;
part_count--;
}
}
else {
tmp=t;
t=t->next;
}
}
return top; // return the list back, possibly empty :)
}
void remove_particle_drawing(BITMAP *bmp, struct particle *t)
{
switch(t->type)
{
case SNOW_PARTICLE1: // just draw a single pixel for a small flake of snow
// put whatever was there before back
if ((t->tx > -1) && (t->ty > -1) && (t->first_update == 0))
{
if ((t->tx != t->x) && (t->ty != t->y))
{
putpixel(bmp, (int)t->tx, (int)t->ty, t->oldcol[0]);
}
}
break;
case SNOW_PARTICLE2:
// put whatever was there before back
if ((t->tx > -1) && (t->ty > -1) && (t->first_update == 0))
{
if ((t->tx != t->x) && (t->ty != t->y))
{
putpixel(bmp, (int)t->tx, (int)t->ty, t->oldcol[0]);
putpixel(bmp, (int)t->tx-1, (int)t->ty, t->oldcol[1]);
putpixel(bmp, (int)t->tx+1, (int)t->ty, t->oldcol[2]);
putpixel(bmp, (int)t->tx, (int)t->ty-1, t->oldcol[3]);
putpixel(bmp, (int)t->tx, (int)t->ty+1, t->oldcol[4]);
}
}
break;
}
}
void draw_particle(BITMAP *bmp, struct particle *t)
{
switch(t->type)
{
case RAIN_PARTICLE: // for rain just draw a simple line
line(bmp, (int)t->x, (int)t->y, (int)t->tx, (int)t->ty, t->colour);
break;
case SNOW_PARTICLE1: // just draw a single pixel for a small flake of snow
// put whatever was there before back
if ((t->tx > -1) && (t->ty > -1) && (t->first_update == 0))
{
if ((t->tx != t->x) && (t->ty != t->y))
putpixel(bmp, (int)t->tx, (int)t->ty, t->oldcol[0]);
}
t->oldcol[0] = getpixel(bmp, (int)t->x, (int)t->y);
t->first_update = 0;
putpixel(bmp, (int)t->x, (int)t->y, t->colour);
break;
case SNOW_PARTICLE2:
// put whatever was there before back
if ((t->tx > -1) && (t->ty > -1) && (t->first_update == 0))
{
if ((t->tx != t->x) && (t->ty != t->y))
{
putpixel(bmp, (int)t->tx, (int)t->ty, t->oldcol[0]);
putpixel(bmp, (int)t->tx-1, (int)t->ty, t->oldcol[1]);
putpixel(bmp, (int)t->tx+1, (int)t->ty, t->oldcol[2]);
putpixel(bmp, (int)t->tx, (int)t->ty-1, t->oldcol[3]);
putpixel(bmp, (int)t->tx, (int)t->ty+1, t->oldcol[4]);
}
}
t->oldcol[0] = getpixel(bmp, (int)t->x, (int)t->y);
t->oldcol[1] = getpixel(bmp, (int)t->x-1, (int)t->y);
t->oldcol[2] = getpixel(bmp, (int)t->x+1, (int)t->y);
t->oldcol[3] = getpixel(bmp, (int)t->x, (int)t->y-1);
t->oldcol[4] = getpixel(bmp, (int)t->x, (int)t->y+1);
t->first_update = 0;
putpixel(bmp, (int)t->x, (int)t->y, t->colour);
putpixel(bmp, (int)t->x-1, (int)t->y, t->colour);
putpixel(bmp, (int)t->x+1, (int)t->y, t->colour);
putpixel(bmp, (int)t->x, (int)t->y-1, t->colour);
putpixel(bmp, (int)t->x, (int)t->y+1, t->colour);
break;
}
}
void draw_particles(BITMAP *bmp, struct particle *t)
{
while(t) { // once again we are just looping through all the particles
draw_particle(bmp,t);
t=t->next;
}
}
void update_particle(struct particle *t)
{
int rnd;
// first store the old positon as the end of the trail
t->tx = t->x; // first things first! store the current position of the particle
t->ty = t->y;
// then update the position using its speed & direction
t->x += t->dx;
t->y += t->dy;
// now add any randomness
if(t->rx>0)
t->x += (rand() % (int) (t->rx*2)) - t->rx;
if(t->ry>0)
{
rnd = (rand() % (int) (t->ry*2)) - t->ry;
if (rnd < 0)
rnd = 0;
t->y += rnd;
}
// subtract one from the particles life.
// test
if ((t->x >= SCREEN_W) || (t->y >= SCREEN_H) || (t->x <= 0) || (t->y <= 0))
t->life = 0;
//t->life--;
}
void update_particles(struct particle *t)
{
while(t) { // and once again we are just looping through all the particles
update_particle(t);
t=t->next;
}
}