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;
21 #define THUNDER_RECT_SIZE 2
22 #define THUNDER_RANDOMNESS 16
23 #define THUNDER_SECONDS 0.075f
25 #define VERTEX_COUNT 12
26 #define PERSPECTIVE_NEUTRAL_DEPTH 0.5f
27 #define NEAR_PLANE 0.01f
28 #define ROTATION_SPEED 1.5f
30 #define MESH_RANDOM_SEED 173
34 #define CAMERA_DISTANCE 1.1f
36 /* TODO: Load palette from file */
37 static unsigned short palette[256];
39 typedef unsigned int PointSprite;
40 #define MAX_POINT_SPRITES 1024
41 static PointSprite pointSprites[MAX_POINT_SPRITES];
42 int pointSpriteCount = 0;
43 #define PACK_POINT_SPRITE(x, y, col) ((col << 16) | (x << 8) | y)
44 #define UNPACK_COLOR(ps) (ps >> 16)
45 #define UNPACK_X(ps) ((ps >> 8) & 0xFF)
46 #define UNPACK_Y(ps) (ps & 0xFF)
52 MyVertex vertexBuffer[VERTEX_COUNT];
53 MyVertex vertexBufferAnimated[VERTEX_COUNT];
54 MyVertex vertexBufferProjected[VERTEX_COUNT];
56 void clearBlurBuffer();
59 void thunder(int x0, int y0, int x1, int y1, unsigned char c0, unsigned char c1, int seed, int randomness, int depth);
64 void renderMeshToPointSprites(int seed);
65 void renderPointSprites();
66 unsigned char fog(float z);
67 void sortPointSprites();
69 static int init(void);
70 static void destroy(void);
71 static void start(long trans_time);
72 static void draw(void);
74 static unsigned int lastFrameTime = 0;
75 static float lastFrameDuration = 0.0f;
76 static struct screen scr = {
85 struct screen *thunder_screen(void)
94 blurBuffer = malloc(BLUR_BUFFER_SIZE);
95 blurBuffer2 = malloc(BLUR_BUFFER_SIZE);
99 /* For now, map to blue */
100 for (i = 0; i < 256; i++) {
101 palette[i] = (i * i) >> 11;
109 static void destroy(void)
118 static void start(long trans_time)
120 lastFrameTime = time_msec;
124 static float remainingThunderDuration = THUNDER_SECONDS;
125 static int thunderPattern = 0;
127 static void draw(void)
129 lastFrameDuration = (time_msec - lastFrameTime) / 1000.0f;
130 lastFrameTime = time_msec;
132 remainingThunderDuration -= lastFrameDuration;
133 if (remainingThunderDuration <= 0) {
135 remainingThunderDuration = THUNDER_SECONDS;
140 renderMeshToPointSprites(thunderPattern);
142 renderPointSprites();
153 void clearBlurBuffer() {
154 /* Clear the whole buffer (including its padding ) */
155 memset(blurBuffer, 0, BLUR_BUFFER_SIZE);
156 memset(blurBuffer2, 0, BLUR_BUFFER_SIZE);
163 unsigned char *src = blurBuffer + BLUR_BUFFER_WIDTH + 1;
164 unsigned char *dst = blurBuffer2 + BLUR_BUFFER_WIDTH + 1;
166 for (j = 0; j < 120; j++) {
167 for (i = 0; i < 160; i++) {
168 *dst = (*(src - 1) + *(src + 1) + *(src - BLUR_BUFFER_WIDTH) + *(src + BLUR_BUFFER_WIDTH)) >> 2;
170 if (*dst > BLUR_DARKEN) *dst -= BLUR_DARKEN;
176 /* Just skip the padding since we went through the scanline in the inner loop (except from the padding) */
181 /* Swap blur buffers */
183 blurBuffer = blurBuffer2;
188 unsigned int *dst1 = (unsigned int*) fb_pixels;
189 unsigned int *dst2 = dst1 + 160; /* We're writing two pixels at once */
190 unsigned char *src1 = blurBuffer + BLUR_BUFFER_WIDTH + 1;
191 unsigned char *src2 = src1 + BLUR_BUFFER_WIDTH;
192 unsigned char tl, tr, bl, br;
195 for (j = 0; j < 120; j++) {
196 for (i = 0; i < 160; i++) {
198 tr = (*src1 + *(src1 + 1)) >> 1;
199 bl = (*src1 + *src2) >> 1;
200 br = (tr + ((*src2 + *(src2 + 1)) >> 1)) >> 1;
202 /* Pack 2 pixels in each 32 bit word */
203 *dst1 = (palette[tr] << 16) | palette[tl];
204 *dst2 = (palette[br] << 16) | palette[bl];
211 /* Again, skip padding */
215 /* For now, skip a scanline */
222 void thunder(int x0, int y0, int x1, int y1, unsigned char c0, unsigned char c1, int seed, int randomness, int depth) {
227 if (randomness <= 0) randomness = 1;
228 mx = ((x0 + x1) >> 1) + rand() % randomness - randomness / 2;
229 my = ((y0 + y1) >> 1) + rand() % randomness - randomness / 2;
232 if (depth <= 0) return;
234 /* Insert a new sprite */
235 if (pointSpriteCount >= MAX_POINT_SPRITES) {
239 pointSprites[pointSpriteCount++] = PACK_POINT_SPRITE(mx, my, mc);
243 thunder(x0, y0, mx, my, c0, mc, rand(), randomness >> 1, depth-1);
244 thunder(mx, my, x1, y1, mc, c1, rand(), randomness >> 1, depth - 1);
247 MyVertex randomVertex() {
251 ret.x = rand() % 200 - 100; if (ret.x == 0) ret.x = 1;
252 ret.y = rand() % 200 - 100; if (ret.y == 0) ret.y = 1;
253 ret.z = rand() % 200 - 100; if (ret.z == 0) ret.z = 1;
256 l = sqrt(ret.x * ret.x + ret.y * ret.y + ret.z * ret.z);
267 srand(MESH_RANDOM_SEED);
269 for (i = 0; i < VERTEX_COUNT; i++) {
270 vertexBuffer[i] = randomVertex();
279 yRot = ROTATION_SPEED * time_msec / 1000.0f;
281 /* Create rotated basis */
290 bz.x = cos(yRot + M_PI/2.0f);
292 bz.z = sin(yRot + M_PI/2.0f);
294 for (i = 0; i < VERTEX_COUNT; i++) {
296 v1 = vertexBuffer[i];
299 v1.y *= sin(time_msec / 1000.0f + v1.x + v1.z);
301 /* O re panaia mou */
302 v2.x = v1.x * bx.x + v1.y * by.x + v1.z * bz.x;
303 v2.y = v1.x * bx.y + v1.y * by.y + v1.z * bz.y;
304 v2.z = v1.x * bx.z + v1.y * by.z + v1.z * bz.z;
306 v2.z += CAMERA_DISTANCE;
308 vertexBufferAnimated[i] = v2;
315 for (i = 0; i < VERTEX_COUNT; i++) {
317 if (vertexBufferAnimated[i].z <= NEAR_PLANE) {
318 vertexBufferProjected[i].x = vertexBufferProjected[i].y = 1000.0f;
319 vertexBufferProjected[i].z = -1.0f;
323 vertexBufferProjected[i].x = vertexBufferAnimated[i].x * PERSPECTIVE_NEUTRAL_DEPTH / vertexBufferAnimated[i].z;
324 vertexBufferProjected[i].y = vertexBufferAnimated[i].y * PERSPECTIVE_NEUTRAL_DEPTH / vertexBufferAnimated[i].z;
328 void renderMeshToPointSprites(int seed) {
333 unsigned char fogAtOrigin;
335 fogAtOrigin = fog(CAMERA_DISTANCE);
337 pointSpriteCount = 0;
340 for (vertex = 0; vertex < VERTEX_COUNT; vertex++) {
341 sx = (int)(vertexBufferProjected[vertex].x * 80) + 80;
342 sy = (int)(vertexBufferProjected[vertex].y * 60) + 60;
344 thunder(80, 60, sx, sy, fogAtOrigin, fog(vertexBufferAnimated[vertex].z), rand(), THUNDER_RANDOMNESS, 5);
348 void renderPointSprites() {
355 for (i = 0; i < pointSpriteCount; i++) {
356 sprite = pointSprites[i];
358 sx = UNPACK_X(sprite);
359 sy = UNPACK_Y(sprite);
361 if (sx < THUNDER_RECT_SIZE || sx >= 160 - THUNDER_RECT_SIZE || sy < THUNDER_RECT_SIZE || sy >= 120 - THUNDER_RECT_SIZE) continue;
363 dst = blurBuffer + BLUR_BUFFER_WIDTH + 1 + sx + sy * BLUR_BUFFER_WIDTH;
365 color = UNPACK_COLOR(sprite);
367 for (j = 0; j < THUNDER_RECT_SIZE; j++) {
368 memset(dst, color, THUNDER_RECT_SIZE);
369 dst += BLUR_BUFFER_WIDTH;
374 unsigned char fog(float z) {
375 unsigned int ret = (unsigned int) (((-(z - CAMERA_DISTANCE)) * 0.5f + 0.5f) * (255.0f - MIN_FOGGED)) + MIN_FOGGED;
376 if (ret > 255) ret = 255;
377 return (unsigned char)ret;
380 void sort(PointSprite *begin, PointSprite *end) {
\r
381 PointSprite pivotValue;
\r
383 PointSprite *left, *right;
\r
384 int leftCond, rightCond;
\r
389 if (sz < 2) return; /* already sorted */
\r
392 if (begin[1] < begin[0]) {
\r
394 begin[0] = begin[1];
\r
400 /* minimum 3 elements from now on */
\r
402 /* choose a pivot near the middle, since we frequently sort already sorted arrays */
\r
403 pivotValue = begin[sz / 2];
\r
408 while (right > left) {
\r
409 /* check if left and right elements meet the conditions */
\r
410 leftCond = pivotValue >= *left;
\r
411 rightCond = pivotValue < *right;
\r
413 if (!leftCond && !rightCond) {
\r
420 else if (leftCond && rightCond) {
\r
424 else if (leftCond) {
\r
437 void sortPointSprites() {
438 sort(pointSprites, pointSprites + pointSpriteCount);