-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfire8.c
More file actions
283 lines (209 loc) · 7.85 KB
/
fire8.c
File metadata and controls
283 lines (209 loc) · 7.85 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
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include "fb.h"
#include <stdlib.h> // rand()
#include <fcntl.h> // open()
#include <sys/stat.h>
#include <unistd.h>
// Cuando se descomenta la siguiente línea, muestra en la consola el tiempo
// en microsegundos que toma ejecutar ciertas partes de este programa.
// #define PROFILING
// Cantidad de numeros random para el lookup table
#define MAXRAND 10240
int main(int argc, char* argv[]) {
// Rampa de colores. Unsigned Short, por que eso es lo que quiere el
// framebuffer
unsigned short r[256], g[256], b[256];
// Variables de apoyo.
int i, x, y;
int tmpval;
char *p;
// Inicializamos el framebuffer.
fb_init(8);
// Creamos un buffer de trabako
unsigned char *buffer = malloc(fb_size);
fb_buffer_pointer = buffer;
// Creamos una rampa de colores bonita.
for (i = 0; i < 64; i++) {
// Negro a rojo
b[i] = 0;
g[i] = 0;
r[i] = (i * 4) << 8;
// rojo a naranja
b[64+i] = 0;
g[64+i] = (i * 2) << 8;
r[64+i] = 255 << 8;
// Naranja a amarillo
b[128+i] = 0;
g[128+i] = (128 + i * 2) << 8;
r[128+i] = 255 << 8;
// amarillo a blanco
b[192+i] = ( i * 4) << 8;
g[192+i] = 255 << 8;
r[192+i] = 255 << 8;
}
// El 255 es negro, para que se note el logo
r[254] = 0;
g[254] = 0;
b[254] = 0;
// Colocamos el color_ramp en el framebuffer
fb_putpal ( r, g, b );
// Un lookup table de valores randoms,
unsigned int rf[MAXRAND];
int rf_c = 0;
for ( i = 0; i < MAXRAND; i++ ) {
rf[i] = rand();
}
// Puntero a la memoria donde almacenaremos el loguito mono.
unsigned char * logo;
// Tamaño fijo. Por ahora.
int logo_w = 445;
int logo_h = 522;
// Obtenemos un pedazo de memoria
logo = malloc(logo_w * logo_h);
// Abriemos el fichero de datos, y lo leemos
int f = open ( "fury-road.dat", O_RDONLY);
read (f, logo, logo_w * logo_h);
close (f);
// Posición inicial de logo
int logo_x = fb_sx / 2 - logo_w / 2;
int logo_y = fb_sy / 2 - logo_h / 2;
int lc; // Esta variable la usamos para promediar los colores del ogo
int logo_dir = 0; // Dirección de la animación. Empieza quieto.
// Valores para animar el fuego inicialmente
int fire_anim = 1;
int fire_anim_delay = 0;
int fire_anim_top = 100;
// También animamos el logo
unsigned char logo_state = 0;
int logo_timer = 0;
#ifdef PROFILING
struct timeval stop,start;
#endif
while (1) {
#ifdef PROFILING
gettimeofday (&start, NULL );
#endif
// Dibujamos los pixeles que generarán el fuego
for (i = 0; i < 100; i++) {
x = rf[rf_c] % fb_sx;
rf_c = ++rf_c % MAXRAND;
// Esto le da un poquito de randomness a la velocidad de
// crecimiento del fuego.
if (fire_anim < fb_sx) {
if (fire_anim_delay++ > fire_anim_top ) {
fire_anim_delay = 0;
fire_anim_top = rf[rf_c] % 100;
rf_c = ++rf_c % MAXRAND;
fire_anim++;
}
}
// Evitamos que dibuje el fuego más allá del límite de la animación
if (x > fire_anim) continue;
// Dibujamos 3 pixeles directo a la memoria. Más rápido que
// usar tres PIXEL8
memset ((char *)(buffer + (fb_sy - 1) * fb_finfo.line_length + x - 1), 255, 3);
}
#ifdef PROFILING
gettimeofday (&stop, NULL );
printf ( "Noise: %lu -", stop.tv_usec - start.tv_usec );
#endif
#ifdef PROFILING
gettimeofday (&start, NULL );
#endif
// Dibujamos el logo si está activo.
if (logo_state == 1) {
for (y = 0; y < logo_h; y++) {
for (x = 0; x < logo_w; x++) {
// Al color del fondo le añadimos el color del logo. Esto
// lo hará transparente, con algo de alias.
lc = GETPIXEL8(x + logo_x, y + logo_y) + *(logo + y * logo_w + x);
if ( lc > 255 ) lc = 255;
// Regresamos el pixel
PIXEL8(x + logo_x, y + logo_y, (char)lc);
}
}
}
// Movemos el logo
logo_x += logo_dir;
// Evitamos que salga de los límites.
if (logo_x < 0) {
logo_x = 0;
logo_dir = -logo_dir;
}
if (logo_x > fb_sx - logo_w) {
logo_x = fb_sx - logo_w;
logo_dir = -logo_dir;
}
// Cada 300 frames, cambiamos el estado del logo
if (logo_timer++ > 300) {
logo_timer = 0;
logo_state = !logo_state;
// También le cambiamos de posición
logo_x = rf[rf_c] % (fb_sx - logo_w);
rf_c = ++rf_c & MAXRAND;
// Y de dirección
logo_dir = (rand() % 2) ? 1 : -1;
// Añadimos un poco de brillo a la pantalla
for (y = 0; y < fb_sy; y++) {
for (x = 0; x < fb_sx; x++) {
lc = GETPIXEL8(x, y) + 50 + (rf[rf_c] % 128);
rf_c = ++rf_c % MAXRAND;
if ( lc > 254 ) lc = 254; // 254 para no usar el negro de 255
PIXEL8 ( x, y, (char)lc);
}
}
}
#ifdef PROFILING
gettimeofday (&stop, NULL );
printf ( "Logo: %lu -", stop.tv_usec - start.tv_usec );
#endif
#ifdef PROFILING
gettimeofday (&start, NULL );
#endif
// Ahora analizamos todo el buffer para calcular el
// efecto de fuego. Esto necesita más optimación
for ( y = 0; y < fb_sy; y++ ) {
for ( x = 0; x < fb_sx; x++ ) {
// Solía tener un IF aqui para evitar leer los píxeles fuera
// de los límites de la pantalla. Pero al quitarlos funcionó
// bien, y la velocidad de este bucle bajó de 28ms a 18ms en
// el Raspberry Pi :)
// También solia usar 3 veces la función GETPIXEL8. Pero usando
// aritmética de punteros, la velocidad bajó 1ms más. A parte,
// es más paja :D
p = (char *)(buffer + y * fb_finfo.line_length + x);
tmpval = *p;
p += fb_finfo.line_length - 1;
tmpval += *(p++);
tmpval += *(p++);
tmpval += *(p++);
// Le restamos 1 para ir oscureciendo
if (tmpval) tmpval--;
// Y dividimos todo entre 4,
tmpval >>= 2;
// Dibujamos el pixel de vuelta al buffer
PIXEL8(x,y,tmpval);
}
}
#ifdef PROFILING
gettimeofday (&stop, NULL );
printf ( "Draw: %lu -", stop.tv_usec - start.tv_usec );
#endif
#ifdef PROFILING
gettimeofday (&start, NULL );
#endif
// Y copiamos el buffer al framebuffer. Esto hace un memcpy(), pero
// creo que hay una forma más eficiente de hacerlo ("imageblit"?)
// TODO
DRAW();
#ifdef PROFILING
gettimeofday (&stop, NULL );
printf ( "Blit: %lu \n", stop.tv_usec - start.tv_usec );
#endif
}
// Bu-bye!
fb_close();
return 0;
}