11 /* Render blur in half x half dimenstions. Add one pixel padding in all
12 * directions (2 pixels horizontally, 2 pixels vertically).
14 #define BLUR_BUFFER_WIDTH (320/2 + 2)
15 #define BLUR_BUFFER_HEIGHT (240/2 + 2)
16 #define BLUR_BUFFER_SIZE (BLUR_BUFFER_WIDTH * BLUR_BUFFER_HEIGHT)
17 static unsigned char *blurBuffer, *blurBuffer2;
19 #define THUNDER_RECT_SIZE 4
20 #define THUNDER_RANDOMNESS 80
21 #define THUNDER_SECONDS 0.1f
23 /* TODO: Load palette from file */
24 static unsigned short palette[256];
26 void clearBlurBuffer();
27 void drawPatternOnBlurBuffer(int seed);
30 void thunder(int x0, int y0, int x1, int y1, int seed, int randomness, int depth);
32 static int init(void);
33 static void destroy(void);
34 static void start(long trans_time);
35 static void stop(long trans_time);
36 static void draw(void);
38 static unsigned int lastFrameTime = 0;
39 static float lastFrameDuration = 0.0f;
40 static struct screen scr = {
49 struct screen *thunder_screen(void)
58 blurBuffer = malloc(BLUR_BUFFER_SIZE);
59 blurBuffer2 = malloc(BLUR_BUFFER_SIZE);
63 /* For now, map to blue */
64 for (i = 0; i < 256; i++) {
71 static void destroy(void)
80 static void start(long trans_time)
82 lastFrameTime = time_msec;
86 static float remainingThunderDuration = THUNDER_SECONDS;
87 static int thunderPattern = 0;
89 static void draw(void)
91 lastFrameDuration = (time_msec - lastFrameTime) / 1000.0f;
92 lastFrameTime = time_msec;
94 remainingThunderDuration -= lastFrameDuration;
95 if (remainingThunderDuration <= 0) {
97 remainingThunderDuration = THUNDER_SECONDS;
102 drawPatternOnBlurBuffer(thunderPattern);
109 void clearBlurBuffer() {
110 /* Clear the whole buffer (including its padding ) */
111 memset(blurBuffer, 0, BLUR_BUFFER_SIZE);
112 memset(blurBuffer2, 0, BLUR_BUFFER_SIZE);
115 void drawPatternOnBlurBuffer(int seed) {
116 thunder(20, 20, 140, 100, seed, THUNDER_RANDOMNESS, 6);
122 unsigned char *src = blurBuffer + BLUR_BUFFER_WIDTH + 1;
123 unsigned char *dst = blurBuffer2 + BLUR_BUFFER_WIDTH + 1;
125 for (j = 0; j < 120; j++) {
126 for (i = 0; i < 160; i++) {
127 *dst = (*(src - 1) + *(src + 1) + *(src - BLUR_BUFFER_WIDTH) + *(src + BLUR_BUFFER_WIDTH)) >> 2;
128 if (*dst > 4) *dst -= 4;
133 /* Just skip the padding since we went through the scanline in the inner loop (except from the padding) */
138 /* Swap blur buffers */
140 blurBuffer = blurBuffer2;
145 unsigned int *dst1 = (unsigned int*) vmem_back;
146 unsigned int *dst2 = dst1 + 160; /* We're writing two pixels at once */
147 unsigned char *src1 = blurBuffer + BLUR_BUFFER_WIDTH + 1;
148 unsigned char *src2 = src1 + BLUR_BUFFER_WIDTH;
149 unsigned char tl, tr, bl, br;
152 for (j = 0; j < 120; j++) {
153 for (i = 0; i < 160; i++) {
155 tr = (*src1 + *(src1 + 1)) >> 1;
156 bl = (*src1 + *src2) >> 1;
157 br = tr + ((*src2 + *(src2 + 1)) >> 1) >> 1;
159 /* Pack 2 pixels in each 32 bit word */
160 *dst1 = (palette[tr] << 16) | palette[tl];
161 *dst2 = (palette[br] << 16) | palette[bl];
168 /* Again, skip padding */
172 /* For now, skip a scanline */
179 void thunder(int x0, int y0, int x1, int y1, int seed, int randomness, int depth) {
180 int mx = ((x0 + x1) >> 1) + rand() % randomness - randomness / 2;
181 int my = ((y0 + y1) >> 1) + rand() % randomness - randomness / 2;
183 unsigned char *dst = blurBuffer + BLUR_BUFFER_WIDTH + 1 + mx + my * BLUR_BUFFER_WIDTH;
185 if (depth <= 0) return;
189 for (j = 0; j < THUNDER_RECT_SIZE; j++) {
190 memset(dst, 255, THUNDER_RECT_SIZE);
191 dst += BLUR_BUFFER_WIDTH;
194 thunder(x0, y0, mx, my, rand(), randomness >> 1, depth-1);
195 thunder(mx, my, x1, y1, rand(), randomness >> 1, depth - 1);