Imported contents: kraptor_final_apr_03_2004.tar.gz
[kraptor.git] / src / partic.c
blob7e80f6a7e2ff3804d62cee35b624bdc20a97916a
1 /*-------------------------------------------------------
2 partic.c
3 --------------------------------------------------------
4 Copyright (c) 2002, Kronoman
5 En memoria de mi querido padre
6 Agosto - 2002
7 --------------------------------------------------------
8 Engine de particulas usando una lista enlazada
9 muy sencilla
10 --------------------------------------------------------*/
12 #ifndef PARTIC_C
13 #define PARTIC_C
15 #include <stdio.h>
16 #include "allegro.h"
18 #include "partic.h"
21 /* DEBUG - estas variables son especificas a Kraptor */
23 #include "global.h" // para saber el nivel de detalle unicamente...
25 /* Cache de bmps representando particulas para 'reventar' naves */
26 BITMAP *particula_cache_bmp[3];
28 /* esta variable NO es necesaria, solo la uso para
29 ver cuantos enemigos hay en memoria, y de esa manera,
30 revisar la performance... */
31 int cant_particulas_debug = 0;
34 /* --- globales internas --- */
36 static PARTICULA *prt_1era = NULL; /* puntero al comienzo de la lista de particulas */
39 /* Esta funcion agrega una nueva particula a la lista enlazada [al principio].
40 Devuelve puntero a la particula recien creada.
41 Los parametros son los parametros de la particula
42 Si vida <= 0, NO se crea la particula... (return derecho...)
44 PARTICULA *agrega_particula( fixed x, fixed y,
45 fixed dx, fixed dy,
46 int vida,
47 int col, int r, int t,
48 int transp, fixed rg,
49 BITMAP *spr )
51 PARTICULA *nueva = NULL;
53 if (vida <= 0) return NULL; /* ni pierdo el tiempo... */
55 if (nivel_detalle < 1) return NULL; // nivel de detalle minimo, no hacer nada
57 nueva = malloc(sizeof(PARTICULA));
58 nueva->next = prt_1era;
59 prt_1era = nueva;
61 if (nueva != NULL) /* si el malloc funciono, seteo los datos... */
63 nueva->x = x;
64 nueva->y = y;
65 nueva->transp = transp;
66 nueva->dx = dx;
67 nueva->dy = dy;
68 nueva->vida = vida;
69 nueva->col = col;
70 nueva->r = itofix(r);
71 nueva->rg = rg;
72 nueva->t = t;
73 nueva->spr = spr;
74 nueva->rot = itofix(rand()%255); /* rotacion del bitmap */
77 return nueva;
81 Esta funcion actualiza la logica (mueve) las particulas
82 tambien las elimina si vida < 0 o si estan fuera de pantalla
83 Pasar en x, y la posicion de scroll en pantalla
84 en w, h el ancho y alto de la pantalla
86 void mover_particulas(int x, int y, int w, int h)
88 PARTICULA **tmp_p = &prt_1era;
89 PARTICULA *tmp = NULL;
91 cant_particulas_debug = 0; /* DEBUG: innecesario, solo para testear performance */
93 while (*tmp_p) {
95 cant_particulas_debug++; /* DEBUG: innecesario, solo para testear performance */
97 tmp = *tmp_p;
99 /* aqui muevo la particula */
100 tmp->x = fixadd(tmp->x, tmp->dx);
101 tmp->y = fixadd(tmp->y, tmp->dy);
102 tmp->vida--;
104 /* crecer radio si es necesario */
105 tmp->r = fixadd ( tmp->r , tmp->rg );
107 /* girar sprite (si lo hubiere... */
108 tmp->rot = fixadd(tmp->rot, itofix(rand()%16)+1); /* girar */
109 if (fixtoi(tmp->rot) > 255) tmp->rot = 0;
111 /* DEBUG: verificar si estan fuera de pantalla (da 5 pixeles de margen para
112 permitir que la particula salga totalmente de pantalla) */
113 if (tmp->y < itofix(y-5)) tmp->vida = -1;
114 if (tmp->y > itofix(y+h+5)) tmp->vida = -1;
115 if (tmp->x < itofix(x-5)) tmp->vida = -1;
116 if (tmp->x > itofix(x+w+5)) tmp->vida = -1;
118 if (tmp->vida < 0) {
119 /* la particula murio, eliminar!!! */
120 *tmp_p = tmp->next;
121 free(tmp);
122 } else {
123 tmp_p = &tmp->next; /* siguiente por favor! */
129 Esta funcion dibuja las particulas en el bitmap bmp
130 Las dibuja desplazadas x,y
132 void dibujar_particulas(BITMAP *bmp, int x, int y) {
134 PARTICULA *tmp = prt_1era;
136 while (tmp) {
138 /* dibujar */
140 // es transparente, y hay suf nivel de detalle?
141 if ((tmp->transp) && (nivel_detalle > 9)) drawing_mode(DRAW_MODE_TRANS, NULL, 0,0);
143 if (tmp->spr == NULL) {
144 switch(tmp->t)
146 case 0: /* pixel */
147 putpixel(bmp, fixtoi(tmp->x)-x, fixtoi(tmp->y)-y, tmp->col);
148 break;
150 case 1: /* circulo */
151 circlefill(bmp, fixtoi(tmp->x)-x, fixtoi(tmp->y)-y, fixtoi(tmp->r), tmp->col);
152 break;
154 case 2: /* cuadrado (QU*KE?) */
155 rectfill(bmp, fixtoi(tmp->x)-x, fixtoi(tmp->y)-y, fixtoi(tmp->x)+fixtoi(tmp->r)-x,fixtoi(tmp->y)+fixtoi(tmp->r)-y , tmp->col);
156 break;
158 case 3: /* linea */
159 line(bmp, fixtoi(tmp->x)-x, fixtoi(tmp->y)-y, fixtoi(fixadd(tmp->x, tmp->dx))-x, fixtoi(fixadd(tmp->y, tmp->dy))-y, tmp->col);
160 break;
162 case 4: /* triangulo */
163 triangle(bmp,
164 fixtoi(tmp->x)-x, fixtoi(tmp->y)-y,
165 fixtoi(tmp->x)+fixtoi(tmp->r)-x, fixtoi(tmp->y)+fixtoi(tmp->r)-y,
166 fixtoi(tmp->x)-fixtoi(tmp->r)-x, fixtoi(tmp->y)+fixtoi(tmp->r)-y,
167 tmp->col);
168 break;
170 default: /* default: */
171 /* si no coincide con ninguno, eliminar la particula... sorry */
172 tmp->vida = 0;
173 break;
175 } else { /* tiene un sprite de imagen */
176 rotate_sprite(bmp, tmp->spr, fixtoi(tmp->x)-x, fixtoi(tmp->y)-y, tmp->rot);
179 solid_mode(); // sacar transparencia...
181 tmp = tmp->next; /* proximo... */
185 /* Esta funcion se debe llamar cuando no se precise mas la lista
186 Libera la RAM usada y reinicia la lista
188 void liberar_lista_particulas() {
189 PARTICULA *tmp = prt_1era;
190 prt_1era = NULL;
192 while (tmp) {
193 PARTICULA *next = tmp->next;
194 free(tmp);
195 tmp = next;
200 #endif