1 /*
2  * BB: The portable demo
3  *
4  * (C) 1997 by AA-group (e-mail: aa@horac.ta.jcu.cz)
5  *
6  * 3rd August 1997
7  * version: 1.2 [final3]
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public Licences as by published
11  * by the Free Software Foundation; either version 2; or (at your option)
12  * any later version
13  *
14  * This program is distributed in the hope that it will entertaining,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILTY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
17  * Publis License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.
21  * 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23 
24 #include <stdlib.h>
25 #include <string.h>
26 #include <math.h>
27 #include "bb.h"
28 
29 #define DWIDTH aa_imgwidth(context)
30 #define DHEIGHT aa_imgheight(context)
31 #define STIME 11*1000000
32 #define TTIME 1000000
33 #define N_FRAMERATE 35
34 #define ZTABSIZE ((int)((float)(MAXZOOM-MINZOOM)/ZOOMSTEP))
35 
36 #define ZOOMSTEP 0.01
37 #define MINZOOM 0.80
38 #define MAXZOOM 2.70
39 #define POW4(x) (x*x*x*x)
40 
41 #define RAD(angle) ((double) angle * M_PI / 180)
42 #define RANDOM(x) (rand()%x)
43 
44 static aa_palette TempPal, Pal[2];
45 static char *PlasmaTbl;
46 static unsigned char pos1, pos2, pos3, pos4;
47 static unsigned int pnum;
48 static int dir = 1;
49 
compute_custom_palette()50 static void compute_custom_palette()
51 {
52     int i;
53 
54     for (i = 0; i < 64; i++) {
55 	Pal[0][i] = i * 4;
56 	Pal[1][i] = i * 1;
57     }
58     for (i = 64; i < 128; i++) {
59 	Pal[0][i] = (128 - i) * 4;
60 	Pal[1][i] = (128 - i) * 1;
61     }
62     for (i = 128; i < 192; i++) {
63 	Pal[0][i] = (i - 128) * 1;
64 	Pal[1][i] = (i - 128) * 4;
65     }
66     for (i = 192; i < 256; i++) {
67 	Pal[0][i] = (256 - i) * 1;
68 	Pal[1][i] = (256 - i) * 4;
69     }
70 }
71 
initscene3(void)72 void initscene3(void)
73 {
74     int i;
75     float czoom;
76 
77     for (czoom = MINZOOM, pnum = 0; pnum < ZTABSIZE; czoom += ZOOMSTEP, pnum++)
78 	for (i = 0; i < 256; i++)
79 	    PlasmaTbl[(pnum << 8) + i] = (char) (((float) cos(RAD(i * 256 / 180))) * 256 / (POW4(czoom)));
80 
81     compute_custom_palette();
82     pnum = 0;
83 }
84 
draw_plasma(void)85 static void draw_plasma(void)
86 {
87     int i, j, color;
88     unsigned char p1, p2, p3 = pos3, p4 = pos4;
89     char *text[] =
90     {
91 	"STILL",
92 	"WATCHING",
93 	"BB",
94 	"?",
95 	"GREAT",
96 	"",
97 	"",
98 	"NOW",
99 	"IT'S",
100 	"A GREAT",
101 	"TIME",
102 	"TO",
103 	"FILL",
104 	"IN",
105 	"YOUR",
106 	"REGISTRATION",
107 	"CARD",
108 	"",
109 	"????",
110 	"NEVER",
111 	"MORE",
112 	"",
113 	"(E.A. POE)",
114 	"..."
115     };
116     for (i = 0; i < DWIDTH; i++) {
117 	p1 = pos1;
118 	p2 = pos2;
119 	for (j = 0; j < DHEIGHT; j++) {
120 	    color = PlasmaTbl[(pnum << 8) + p1] + PlasmaTbl[(pnum << 8) + p2] + PlasmaTbl[(pnum << 8) + p3] + PlasmaTbl[(pnum << 8) + p4] + PlasmaTbl[(pnum << 8) + ((unsigned char) i)] + PlasmaTbl[(pnum << 8) + ((unsigned char) j)];
121 	    aa_putpixel(context, i, j, color);
122 	    p1 += 3;
123 	    p2 += 1;
124 	}
125 	p3 += 2;
126 	p4 += 3;
127     }
128     for (i = 0; i < sizeof(text) / sizeof(char *); i++) {
129 	message(text[i], starttime + STIME + i * TTIME);
130     }
131 
132     aa_renderpalette(context, TempPal, params, 0, 0, aa_imgwidth(context), aa_imgheight(context));
133     aa_flush(context);
134 }
135 
move_plasma(void)136 static void move_plasma(void)
137 {
138 
139     pos1 -= 4 + RANDOM(2);
140     pos3 += 4 + RANDOM(1);
141     pos2 -= RANDOM(2);
142     pos4 -= RANDOM(2);
143 
144     pnum += dir;
145     if (pnum > (ZTABSIZE - 2))
146 	dir = -1;
147     else if (pnum <= 0)
148 	dir = 1;
149 }
150 
cplasma(aa_palette dest,aa_palette src,aa_palette temp,float step)151 static void cplasma(aa_palette dest, aa_palette src, aa_palette temp, float step)
152 {
153     int j;
154 
155     for (j = 0; j < 256; j++)
156 	temp[j] = src[j] + ((dest[j] - src[j]) * step / 64);
157 
158 }
159 
160 
do_plasma(int step)161 static void do_plasma(int step)
162 {
163     static int m = 0, n = 0, f = 0;
164     int i = 0;
165 
166     if (TIME > endtime - 128 * 1000000 / N_FRAMERATE)
167 	params->bright -= step * 2;
168     else {
169 	params->bright -= step * 2;
170 	if (params->bright < 0)
171 	    params->bright = 0;
172     }
173 
174     if ((f += step) > 64) {
175 	f = 0;
176 	m = n, n = RANDOM(2);
177     }
178     cplasma(Pal[n], Pal[m], TempPal, f);
179     for (i = 0; i < step; i++)
180 	move_plasma();
181 }
182 
scene3(void)183 void scene3(void)
184 {
185     pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0, pnum = 0, dir = 1;
186     PlasmaTbl = (char *) malloc(ZTABSIZE * 256);
187     params->dither = AA_FLOYD_S;
188     initscene3();
189     bbwait(500000);
190     timestuff(-N_FRAMERATE, do_plasma, draw_plasma, 42 * 1000000);
191     params->bright = 0;
192     free(PlasmaTbl);
193     clrscr();
194 }
195