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
38 /* TODO: Load palette from file */
39 static unsigned short palette[256];
41 typedef unsigned int PointSprite;
42 #define MAX_POINT_SPRITES 1024
43 static PointSprite pointSprites[MAX_POINT_SPRITES];
44 int pointSpriteCount = 0;
45 #define PACK_POINT_SPRITE(x, y, col) ((col << 16) | (x << 8) | y)
46 #define UNPACK_COLOR(ps) (ps >> 16)
47 #define UNPACK_X(ps) ((ps >> 8) & 0xFF)
48 #define UNPACK_Y(ps) (ps & 0xFF)
54 MyVertex vertexBuffer[VERTEX_COUNT];
55 MyVertex vertexBufferAnimated[VERTEX_COUNT];
56 MyVertex vertexBufferProjected[VERTEX_COUNT];
58 void clearBlurBuffer();
61 void thunder(int x0, int y0, int x1, int y1, unsigned char c0, unsigned char c1, int seed, int randomness, int depth);
66 void renderMeshToPointSprites(int seed);
67 void renderPointSprites();
68 unsigned char fog(float z);
69 void sortPointSprites();
71 static int init(void);
72 static void destroy(void);
73 static void start(long trans_time);
74 static void stop(long trans_time);
75 static void draw(void);
77 static unsigned int lastFrameTime = 0;
78 static float lastFrameDuration = 0.0f;
79 static struct screen scr = {
88 struct screen *thunder_screen(void)
97 blurBuffer = malloc(BLUR_BUFFER_SIZE);
98 blurBuffer2 = malloc(BLUR_BUFFER_SIZE);
102 /* For now, map to blue */
103 for (i = 0; i < 256; i++) {
104 palette[i] = (i * i) >> 11;
112 static void destroy(void)
121 static void start(long trans_time)
123 lastFrameTime = time_msec;
127 static float remainingThunderDuration = THUNDER_SECONDS;
128 static int thunderPattern = 0;
130 static void draw(void)
132 lastFrameDuration = (time_msec - lastFrameTime) / 1000.0f;
133 lastFrameTime = time_msec;
135 remainingThunderDuration -= lastFrameDuration;
136 if (remainingThunderDuration <= 0) {
138 remainingThunderDuration = THUNDER_SECONDS;
143 renderMeshToPointSprites(thunderPattern);
145 renderPointSprites();
156 void clearBlurBuffer() {
157 /* Clear the whole buffer (including its padding ) */
158 memset(blurBuffer, 0, BLUR_BUFFER_SIZE);
159 memset(blurBuffer2, 0, BLUR_BUFFER_SIZE);
166 unsigned char *src = blurBuffer + BLUR_BUFFER_WIDTH + 1;
167 unsigned char *dst = blurBuffer2 + BLUR_BUFFER_WIDTH + 1;
169 for (j = 0; j < 120; j++) {
170 for (i = 0; i < 160; i++) {
171 *dst = (*(src - 1) + *(src + 1) + *(src - BLUR_BUFFER_WIDTH) + *(src + BLUR_BUFFER_WIDTH)) >> 2;
173 if (*dst > BLUR_DARKEN) *dst -= BLUR_DARKEN;
179 /* Just skip the padding since we went through the scanline in the inner loop (except from the padding) */
184 /* Swap blur buffers */
186 blurBuffer = blurBuffer2;
191 unsigned int *dst1 = (unsigned int*) vmem_back;
192 unsigned int *dst2 = dst1 + 160; /* We're writing two pixels at once */
193 unsigned char *src1 = blurBuffer + BLUR_BUFFER_WIDTH + 1;
194 unsigned char *src2 = src1 + BLUR_BUFFER_WIDTH;
195 unsigned char tl, tr, bl, br;
198 for (j = 0; j < 120; j++) {
199 for (i = 0; i < 160; i++) {
201 tr = (*src1 + *(src1 + 1)) >> 1;
202 bl = (*src1 + *src2) >> 1;
203 br = tr + ((*src2 + *(src2 + 1)) >> 1) >> 1;
205 /* Pack 2 pixels in each 32 bit word */
206 *dst1 = (palette[tr] << 16) | palette[tl];
207 *dst2 = (palette[br] << 16) | palette[bl];
214 /* Again, skip padding */
218 /* For now, skip a scanline */
225 void thunder(int x0, int y0, int x1, int y1, unsigned char c0, unsigned char c1, int seed, int randomness, int depth) {
230 if (randomness <= 0) randomness = 1;
231 mx = ((x0 + x1) >> 1) + rand() % randomness - randomness / 2;
232 my = ((y0 + y1) >> 1) + rand() % randomness - randomness / 2;
235 if (depth <= 0) return;
237 /* Insert a new sprite */
238 if (pointSpriteCount >= MAX_POINT_SPRITES) {
242 pointSprites[pointSpriteCount++] = PACK_POINT_SPRITE(mx, my, mc);
246 thunder(x0, y0, mx, my, c0, mc, rand(), randomness >> 1, depth-1);
247 thunder(mx, my, x1, y1, mc, c1, rand(), randomness >> 1, depth - 1);
250 MyVertex randomVertex() {
254 ret.x = rand() % 200 - 100; if (ret.x == 0) ret.x = 1;
255 ret.y = rand() % 200 - 100; if (ret.y == 0) ret.y = 1;
256 ret.z = rand() % 200 - 100; if (ret.z == 0) ret.z = 1;
259 l = sqrt(ret.x * ret.x + ret.y * ret.y + ret.z * ret.z);
270 srand(MESH_RANDOM_SEED);
272 for (i = 0; i < VERTEX_COUNT; i++) {
273 vertexBuffer[i] = randomVertex();
282 yRot = ROTATION_SPEED * time_msec / 1000.0f;
284 /* Create rotated basis */
293 bz.x = cos(yRot + PI/2.0f);
295 bz.z = sin(yRot + PI/2.0f);
297 for (i = 0; i < VERTEX_COUNT; i++) {
299 v1 = vertexBuffer[i];
302 v1.y *= sin(time_msec / 1000.0f + v1.x + v1.z);
304 /* O re panaia mou */
305 v2.x = v1.x * bx.x + v1.y * by.x + v1.z * bz.x;
306 v2.y = v1.x * bx.y + v1.y * by.y + v1.z * bz.y;
307 v2.z = v1.x * bx.z + v1.y * by.z + v1.z * bz.z;
309 v2.z += CAMERA_DISTANCE;
311 vertexBufferAnimated[i] = v2;
318 for (i = 0; i < VERTEX_COUNT; i++) {
320 if (vertexBufferAnimated[i].z <= NEAR_PLANE) {
321 vertexBufferProjected[i].x = vertexBufferProjected[i].y = 1000.0f;
322 vertexBufferProjected[i].z = -1.0f;
326 vertexBufferProjected[i].x = vertexBufferAnimated[i].x * PERSPECTIVE_NEUTRAL_DEPTH / vertexBufferAnimated[i].z;
327 vertexBufferProjected[i].y = vertexBufferAnimated[i].y * PERSPECTIVE_NEUTRAL_DEPTH / vertexBufferAnimated[i].z;
331 void renderMeshToPointSprites(int seed) {
336 unsigned char fogAtOrigin;
338 fogAtOrigin = fog(CAMERA_DISTANCE);
340 pointSpriteCount = 0;
343 for (vertex = 0; vertex < VERTEX_COUNT; vertex++) {
344 sx = (int)(vertexBufferProjected[vertex].x * 80) + 80;
345 sy = (int)(vertexBufferProjected[vertex].y * 60) + 60;
347 thunder(80, 60, sx, sy, fogAtOrigin, fog(vertexBufferAnimated[vertex].z), rand(), THUNDER_RANDOMNESS, 5);
351 void renderPointSprites() {
358 for (i = 0; i < pointSpriteCount; i++) {
359 sprite = pointSprites[i];
361 sx = UNPACK_X(sprite);
362 sy = UNPACK_Y(sprite);
364 if (sx < THUNDER_RECT_SIZE || sx >= 160 - THUNDER_RECT_SIZE || sy < THUNDER_RECT_SIZE || sy >= 120 - THUNDER_RECT_SIZE) continue;
366 dst = blurBuffer + BLUR_BUFFER_WIDTH + 1 + sx + sy * BLUR_BUFFER_WIDTH;
368 color = UNPACK_COLOR(sprite);
370 for (j = 0; j < THUNDER_RECT_SIZE; j++) {
371 memset(dst, color, THUNDER_RECT_SIZE);
372 dst += BLUR_BUFFER_WIDTH;
377 unsigned char fog(float z) {
378 unsigned int ret = (unsigned int) (((-(z - CAMERA_DISTANCE)) * 0.5f + 0.5f) * (255.0f - MIN_FOGGED)) + MIN_FOGGED;
379 if (ret > 255) ret = 255;
380 return (unsigned char)ret;
383 void sort(PointSprite *begin, PointSprite *end) {
\r
384 PointSprite pivotValue;
\r
386 PointSprite *left, *right;
\r
387 int leftCond, rightCond;
\r
392 if (sz < 2) return; /* already sorted */
\r
395 if (begin[1] < begin[0]) {
\r
397 begin[0] = begin[1];
\r
403 /* minimum 3 elements from now on */
\r
405 /* choose a pivot near the middle, since we frequently sort already sorted arrays */
\r
406 pivotValue = begin[sz / 2];
\r
411 while (right > left) {
\r
412 /* check if left and right elements meet the conditions */
\r
413 leftCond = pivotValue >= *left;
\r
414 rightCond = pivotValue < *right;
\r
416 if (!leftCond && !rightCond) {
\r
423 else if (leftCond && rightCond) {
\r
427 else if (leftCond) {
\r
440 void sortPointSprites() {
441 sort(pointSprites, pointSprites + pointSpriteCount);