File: particle.c

package info (click to toggle)
ketm 0.0.6-17sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 9,356 kB
  • ctags: 875
  • sloc: ansic: 7,303; sh: 410; makefile: 185
file content (314 lines) | stat: -rw-r--r-- 6,760 bytes parent folder | download | duplicates (6)
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
#include "particle.h"

PARSYS *parsys=NULL;
extern SDL_Surface *screen;
extern double fps_factor;

/* Neues Partikelsystem
   src:		Quell-Surface (bei PIXELATE NULL)
   xs,ys:	Partikelgroesse (bei PIXELATE: xs=Anzahl der Partikel, ys=N/A)
   xp,yp:	Startposition
   maxspeed:	Max. Speed
   gangle:	Grav. Winkel
   gspeed:	Grav. Geschwindigkeit
   ttl:		TimeToLive
   tx:		Effekt (EXPLODE, LINESPLIT, PIXELATE, evtl. or mit DIFFSIZE)
   msg:		NULL oder pointer auf int, wird auf 0 gesetzt wenn explosion beendet
*/
void parsys_add(SDL_Surface *src, int xs, int ys, int xp, int yp, int maxspeed, int gangle, double gspeed, int ttl, int fx, int *msg)
{
	PARSYS *ps,*pl=parsys;
	SDL_Surface *t;
	SDL_Rect d;
	int i;
	int txp=0,typ=0;

	if((fx==(PIXELATE|DIFFSIZE))||(fx==(PIXELIZE|DIFFSIZE))) {
		CHECKPOINT;
		error(ERR_FATAL,"PIXELEFFECT|DIFFSIZE!");
	}
	if(fx>>4) {
		/* DIFFSIZE gesezt -
		 * es werden 4 Partikelsysteme mit verschieden grossen Partikeln erzeugt, um
		 * die Explosion etwas realistischer zu machen
		 */
		for(i=0;i<4;i++) {
			if((t=SDL_CreateRGBSurface(SDL_SRCCOLORKEY,src->w/2,src->h/2,
				screen->format->BitsPerPixel,
				screen->format->Rmask,
				screen->format->Gmask,
				screen->format->Bmask,
				screen->format->Amask))==NULL) {
				CHECKPOINT;
				error(ERR_FATAL,"no RGBSurface for particle system: %s",SDL_GetError());
			}
			d.w=src->w/2;
			d.h=src->h/2;
			switch(i) {
				case 0:
					d.x=0;
					d.y=0;
					txp=xp;
					typ=yp;
					break;
				case 1:
					d.x=d.w;
					d.y=0;
					txp=xp+d.w;
					typ=yp;
					break;
				case 2:
					d.x=0;
					d.y=d.h;
					txp=xp;
					typ=yp+d.h;
					break;
				case 4:
					d.x=d.w;
					d.y=d.h;
					txp=xp+d.w;
					typ=yp+d.h;
					break;
			}
			SDL_BlitSurface(src,&d,t,NULL);
			parsys_add(t, xs+i, ys+i, txp, typ, maxspeed, gangle, gspeed, ttl, fx&0x0f, msg);
			SDL_FreeSurface(t);
			t=NULL;
		}
		return;
	}
	

		
	ps=mmalloc(sizeof(PARSYS));
	ps->x_size=xs;
	ps->y_size=ys;
	ps->x_pos=xp;
	ps->y_pos=yp;
	ps->maxspeed=maxspeed;
	ps->gangle=gangle;
	ps->gspeed=gspeed;
	ps->ttl=ttl;
	ps->fx=fx&0x0f;
	ps->active=0;
	ps->msg=msg;
	ps->particle=particle_init(src,xs,ys,xp,yp,maxspeed,gangle,gspeed,ttl,fx);
	ps->next=NULL;

	if(ps->msg!=NULL) *(ps->msg)=1;
	
	if(parsys==NULL) {
		parsys=ps;
	} else {
		while(pl->next!=NULL) pl=pl->next;
		pl->next=ps;
	}
}

void parsys_display()
{
	PARSYS *p=parsys,*prev=NULL,*next=NULL;

	while(p!=NULL) {
		next=p->next;
		if((p->active=particle_calc(p->particle))==0) {
			particle_free(p->particle);
			if(prev==NULL) {
				parsys=p->next;
			} else {
				prev->next=next;
			}
			if(p->msg!=NULL) *(p->msg)=0;
			free(p);
		} else {
			if(p->fx==PIXELATE) {
				if(SDL_MUSTLOCK(screen))
					SDL_LockSurface(screen);
			}
			particle_display(p->particle);
			if(p->fx==PIXELATE) {
				if(SDL_MUSTLOCK(screen))
					SDL_UnlockSurface(screen);
			}
			prev=p;
		}
		
		p=next;
	}
}

void parsys_remove_all()
{
	PARSYS *p=parsys,*prev=NULL,*next=NULL;

	while(p!=NULL) {
		next=p->next;
		particle_free(p->particle);
		if(prev==NULL) {
			parsys=p->next;
		} else {
			prev->next=next;
		}
		if(p->msg!=NULL) *(p->msg)=0;
		free(p);
		p=next;
	}
}
	

PAR *particle_init(SDL_Surface *src,int xs,int ys, int xp, int yp, int maxspeed, int gangle, double gspeed, int ttl, int fx)
{
	PAR *first=NULL,*akt=NULL,*last=NULL;
	int i,j;
	SDL_Rect r;
	double angle, speed;
	Uint32 key;
	Uint32 color;

	switch(fx) {
		case PIXELATE:
			for(i=0;i<xs;i++) {
				last=akt;
				akt=mmalloc(sizeof(PAR));
				if(first==NULL){ first=akt; last=akt; }
				last->next=akt;
				akt->x=xp;
				akt->y=yp;
				angle=(rand()*2*M_PI)/RAND_MAX;
				speed=((rand()%100)/maxspeed)+0.1;
				akt->yv=sin(angle)*speed;
				akt->yv+=sin(degtorad(gangle))*gspeed;
				akt->xv=cos(angle)*speed;
				akt->xv+=cos(degtorad(gangle))*gspeed;
				akt->ttl=(rand()%ttl)+1;
				akt->img=NULL;
				j=rand()%255;
				akt->color=SDL_MapRGB(screen->format,j,j,j);
			}
			break;
		case PIXELIZE:
			if(SDL_MUSTLOCK(src))
				SDL_LockSurface(src);
			key=src->format->colorkey;
			for(i=0;i<src->w;i++) {
				for(j=0;j<src->h;j++) {
					color=getpixel(src,i,j);
					if(color!=key) {
						last=akt;
						akt=mmalloc(sizeof(PAR));
						if(first==NULL){ first=akt; last=akt; }
						last->next=akt;
						akt->x=i+xp;
						akt->y=j+yp;
						angle=(rand()*2*M_PI)/RAND_MAX;
						speed=((rand()%100)/maxspeed)+0.1;
						akt->yv=sin(angle)*speed;
						akt->yv+=sin(degtorad(gangle))*gspeed;
						akt->xv=cos(angle)*speed;
						akt->xv+=cos(degtorad(gangle))*gspeed;
						akt->ttl=(rand()%ttl)+1;
						akt->img=NULL;
						akt->color=color;
					}
				}
			}
			break;
		default:
			for(i=0;i<src->w;i+=xs) {
				for(j=0;j<src->h;j+=ys) {
					last=akt;
					akt=mmalloc(sizeof(PAR));
					if(first==NULL){ first=akt; last=akt; }
					last->next=akt;
					akt->x=i+xp;
					akt->y=j+yp;

					angle=(rand()*2*M_PI)/RAND_MAX;
					speed=((rand()%100)/maxspeed)+0.1;
					akt->yv=sin(angle)*speed;
					akt->yv+=sin(degtorad(gangle))*gspeed;

					if(fx==LINESPLIT) {
						akt->xv=0;
					} else {
						akt->xv=cos(angle)*speed;
						akt->xv+=cos(degtorad(gangle))*gspeed;
					}

					akt->ttl=(rand()%ttl)+1;
					akt->img=SDL_CreateRGBSurface(SDL_SRCCOLORKEY,xs,ys,
						src->format->BitsPerPixel,
						src->format->Rmask,
						src->format->Gmask,
						src->format->Bmask,
						src->format->Amask);
					if(akt->img==NULL) {
						CHECKPOINT;
						error(ERR_FATAL,"cant create SDL_Surface: %s",SDL_GetError());
					}
					
					SDL_SetColorKey(akt->img,SDL_SRCCOLORKEY,0x00000000);
					r.w=xs;
					r.h=ys;
					r.x=i;
					r.y=j;
					SDL_BlitSurface(src,&r,akt->img,NULL);
				}
			}
			break;
	}

	akt->next=NULL;

	return(first);
}

int particle_calc(PAR *p)
{
	int alive=0;

	while(p!=NULL) {
		// if(p->ttl) {
		if(p->ttl>0) {
			alive++;
			p->x+=p->xv*fps_factor;
			p->y+=p->yv*fps_factor;
			// p->ttl--;
			p->ttl-=fps_factor;
		}
		p=p->next;
	}
	return(alive);
}

void particle_free(PAR *p)
{
	PAR *l;
	do {
		if(p->img!=NULL)
			SDL_FreeSurface(p->img);
		l=p;
		p=p->next;
		free(l);
	} while(p!=NULL);
}

void particle_display(PAR *p)
{
	SDL_Rect re;
	do {
		// if(p->ttl) {
		if(p->ttl>0) {
			if(p->img!=NULL) {
				re.x=p->x;
				re.y=p->y;
				re.w=p->img->w;
				re.h=p->img->h;
				SDL_BlitSurface(p->img,NULL,screen,&re);
			} else {
				putpixel(screen,p->x,p->y,p->color);
			}
		}
		p=p->next;
	} while(p!=NULL);
}