File: humo.c

package info (click to toggle)
kraptor 0.0.20040403-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 12,876 kB
  • ctags: 781
  • sloc: ansic: 7,107; makefile: 109; sh: 63
file content (281 lines) | stat: -rw-r--r-- 8,751 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
/*-------------------------------------------------------
 humo.c
 -------------------------------------------------------- 
 Copyright (c) 2002, Kronoman 
 En memoria de mi querido padre
 Enero - 2003
 -------------------------------------------------------- 
 Engine de humo (para edificios en llamas)
 usando una lista enlazada  muy sencilla
 --------------------------------------------------------*/ 

#ifndef HUMO_C
#define HUMO_C

#include <stdio.h>
#include <allegro.h>

#include "humo.h"
#include "azar.h"

/* DEBUG - estas variables son especificas a Kraptor */
#include "global.h" // para saber el nivel de detalle unicamente...

BITMAP *humo_spr = NULL; // sprite de representacion del humo...

/* --- globales internas --- */

static HUMO *prt_1er_humo = NULL; /* puntero al comienzo de la lista */
static EMISOR_HUMO *prt1er_emisor = NULL; /* lista de emisores de humo */

//======================================================================== 
// Funciones de los emisores de humo,
// son zonas de 40x40 que emiten humo por un determinado espacio de tiempo


/* Esta funcion agrega un nuevo emisor de humo
   Devuelve puntero al emisor creado.
   Si vida <= 0, NO se crea... 
   */
EMISOR_HUMO *agrega_emisor_humo(fixed x, fixed y, int vida )
{
    EMISOR_HUMO *nueva = NULL;

    if (vida <= 0) return NULL; /* ni pierdo el tiempo... */

    if (nivel_detalle < 2) return NULL; // nivel de detalle minimo, no hacer nada

    nueva = (EMISOR_HUMO *)malloc(sizeof(EMISOR_HUMO));
    nueva->next = prt1er_emisor;
    prt1er_emisor = nueva;

    if (nueva != NULL) /* si el malloc funciono, seteo los datos... */
    {
        nueva->x = x;
        nueva->y = y;
        nueva->vida = vida;
    }

    return nueva;
}

/*
   Esta funcion actualiza la logica (mueve) los emisores de humo
   En realidad, los emisores de humo agregan particulas, pero no se mueven...
   tambien los elimina si vida < 0 o si estan fuera de pantalla
   Pasar en x, y la posicion de scroll en pantalla
         en w, h el ancho y alto de la pantalla
   */
void mover_emisor_humo(int x, int y, int w, int h)
{
    int i;
    EMISOR_HUMO **tmp_p = &prt1er_emisor;
    EMISOR_HUMO *tmp = NULL;

    while (*tmp_p) {

        tmp = *tmp_p;

        /* DEBUG: aqui agrego particulas de humo */
        // fueguito...
        if (rand()%100 < 80)
                agrega_humo( fixadd(tmp->x, itofix(rand_ex(-5,5))) ,
                             fixadd(tmp->y, itofix(rand_ex(-5,5))) ,
                             ftofix(rand_ex(-150,150) / 100.0),
                             ftofix(rand_ex(-150,150) / 100.0),
                             rand_ex(5, 15),
                             makecol(255,rand()%255,0),
                             itofix(rand_ex(2,6)),
                             ftofix(rand_ex(0, 10) / 100.0));

         // agregar un 'humo' hacia la derecha y arriba
        if (rand()%100 < 95)
               {
                i = rand_ex(0,96); // gris oscuro
                agrega_humo( fixadd(tmp->x, itofix(rand_ex(-5,5))),
                             fixadd(tmp->y, itofix(rand_ex(-5,5))),
                             ftofix(rand_ex(10,200) / 100.0),
                             ftofix(-1.0 * (rand_ex(50,300) / 100.0)),
                             rand_ex(10, 50),
                             makecol(i,i,i),
                             itofix(rand_ex(3,10)),
                             ftofix(rand_ex(0, 25) / 100.0));
               }

        tmp->vida--;

        /* verificar si estan fuera de pantalla (da 40 pixeles de margen para
           permitir que salga totalmente de pantalla) */
         // NOTA: no los elimino por arriba, sino los edificos aparecen 'apagados'
//      if (tmp->y < itofix(y-80)) tmp->vida = -1;
        if (tmp->y > itofix(y+h+80)) tmp->vida = -1;
        if (tmp->x < itofix(x-80)) tmp->vida = -1;
        if (tmp->x > itofix(x+w+80)) tmp->vida = -1;

        if (tmp->vida < 0) {
          /*  murio, eliminar!!! */
          *tmp_p = tmp->next;
          free(tmp);
        } else {
            tmp_p = &tmp->next; /* siguiente por favor! */
        }
    }
}

/* Esta funcion se debe llamar cuando no se precise
   mas la lista de emisores de humo
   Libera la RAM usada y reinicia la lista
   */
void liberar_emisores_humo() {
    EMISOR_HUMO *tmp = prt1er_emisor;
    prt1er_emisor = NULL;

    while (tmp) {
        EMISOR_HUMO *next = tmp->next;
        free(tmp);
        tmp = next;
    }
}


//======================================================================== 
//
// ---------------------- FUNCIONES DE PARTICULAS DE HUMO EN SI ----------
//
//========================================================================

/* Esta funcion agrega humo a la lista enlazada [al principio].
   Devuelve puntero a la particula recien creada.
   Los parametros son los parametros de la particula
   Si vida <= 0, NO se crea la particula... (return derecho...)
   */
HUMO *agrega_humo( fixed x,  fixed y,
                   fixed dx, fixed dy,
                   int vida,
                   int col, fixed r, fixed ri)
{
    HUMO *nueva = NULL;

    if (vida <= 0) return NULL; /* ni pierdo el tiempo... */

    if (nivel_detalle < 2) return NULL; // nivel de detalle minimo, no hacer nada

    nueva =malloc(sizeof(HUMO));
    nueva->next = prt_1er_humo;
    prt_1er_humo = nueva;

    if (nueva != NULL) /* si el malloc funciono, seteo los datos... */
    {
        nueva->x = x;
        nueva->y = y;
        nueva->dx = dx;
        nueva->dy = dy;
        nueva->vida = vida;
        nueva->col = col;
        nueva->r = r;
        nueva->ri = ri;
    }

    return nueva;
}

/*
   Esta funcion actualiza la logica (mueve) el humo
   tambien las elimina si vida < 0 o si estan fuera de pantalla
   Pasar en x, y la posicion de scroll en pantalla
         en w, h el ancho y alto de la pantalla
   */
void mover_humo(int x, int y, int w, int h)
{
    HUMO **tmp_p = &prt_1er_humo;
    HUMO *tmp = NULL;

    while (*tmp_p) {

        tmp = *tmp_p;

        /* aqui muevo la particula */
        tmp->x = fixadd(tmp->x, tmp->dx);

        // efecto 'sacudida' para simular el humo...
        tmp->x = fixadd(tmp->x, ftofix( rand_ex(-200, 200 ) / 100.0 ) );

        tmp->y = fixadd(tmp->y, tmp->dy);
        tmp->vida--;

        // aumentar el radio
        tmp->r = fixadd(tmp->r, tmp->ri);
        if (fixtoi(tmp->r) < 1) tmp->vida = -1;


        /* DEBUG: verificar si estan fuera de pantalla (da 15 pixeles de margen para
           permitir que la particula salga totalmente de pantalla) */
        if (tmp->y < itofix(y-15)) tmp->vida = -1;
        if (tmp->y > itofix(y+h+15)) tmp->vida = -1;
        if (tmp->x < itofix(x-15)) tmp->vida = -1;
        if (tmp->x > itofix(x+w+15)) tmp->vida = -1;

        if (tmp->vida < 0) {
          /* la particula murio, eliminar!!! */
          *tmp_p = tmp->next;
          free(tmp);
        } else {
            tmp_p = &tmp->next; /* siguiente por favor! */
        }
    }
}

/*
   Esta funcion dibuja el humo en el bitmap bmp
   Las dibuja desplazadas x,y
   */
void dibujar_humo(BITMAP *bmp, int x, int y)
{
    HUMO *tmp = prt_1er_humo;
    BITMAP *tbmp = NULL; // bitmap temporal para el dibujado de sprites escalados
    
    if (nivel_detalle > 5)
                drawing_mode(DRAW_MODE_TRANS, NULL, 0,0); // usar transparencias (queda joya!)

    while (tmp) {
      /* dibujar */

      // si el nivel de detalle es maximo, usara sprites transparentes
      // escalados, lo cual es *muy* lento!
      if (nivel_detalle >= 10 && fixtoi(tmp->r) > 5 && humo_spr != NULL)
        {
          tbmp = create_bitmap(fixtoi(tmp->r)*2,fixtoi(tmp->r)*2);
          if (tbmp != NULL)
          {
            stretch_blit(humo_spr, tbmp, 0, 0, humo_spr->w, humo_spr->h,0,0,tbmp->w, tbmp->h);
            draw_trans_sprite(bmp, tbmp, fixtoi(tmp->x)-x-(tbmp->w/2), fixtoi(tmp->y)-y-(tbmp->h/2));
            destroy_bitmap(tbmp);
            tbmp = NULL;
          }
        }
      else
        { circlefill(bmp, fixtoi(tmp->x)-x, fixtoi(tmp->y)-y, fixtoi(tmp->r), tmp->col);}
        
        tmp = tmp->next; /* proximo... */
      }

    solid_mode();
}

/* Esta funcion se debe llamar cuando no se precise mas el humo
   Libera la RAM usada y reinicia la lista
   */
void liberar_humo()
{
    HUMO *tmp = prt_1er_humo;
    prt_1er_humo = NULL;

    while (tmp) {
        HUMO *next = tmp->next;
        free(tmp);
        tmp = next;
    }

}

#endif