removed drawFps from grise as it's now part of the main demo code
[dosdemo] / src / grise.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include <assert.h>
6 #include "imago2.h"
7 #include "demo.h"
8 #include "screen.h"
9
10 /* APPROX. 170 FPS Minimum */
11
12 typedef struct {
13         unsigned int w, h;
14         unsigned char *scans;
15 } RLEBitmap;
16
17 static RLEBitmap *rleCreate(unsigned int w, unsigned int h);
18 static void rleDestroy(RLEBitmap *b);
19 static void rleBlit(unsigned short *dst, int dstW, int dstH, int dstStride,
20         RLEBitmap *bitmap, int blitX, int blitY);
21 static void rleBlitScale(unsigned short *dst, int dstW, int dstH, int dstStride,
22         RLEBitmap *bitmap, int blitX, int blitY, float scaleX, float scaleY);
23 static void rleBlitScaleInv(unsigned short *dst, int dstW, int dstH, int dstStride,
24         RLEBitmap *bitmap, int blitX, int blitY, float scaleX, float scaleY);
25 static RLEBitmap *rleEncode(RLEBitmap *b, unsigned char *pixels, unsigned int w, unsigned int h);
26
27 static void updatePropeller(float t);
28
29 #define BG_FILENAME "data/grise.png"
30 #define GROBJ_01_FILENAME "data/grobj_01.png"
31
32 #define BB_SIZE 512     /* Let's use a power of 2. Maybe we'll zoom/rotate the effect */
33
34 /* Every backBuffer scanline is guaranteed to have that many dummy pixels before and after */
35 #define PIXEL_PADDING 32
36
37 /* Make sure this is less than PIXEL_PADDING*/
38 #define MAX_DISPLACEMENT 16
39
40 #define MIN_SCROLL PIXEL_PADDING
41 #define MAX_SCROLL (backgroundW - fb_width - MIN_SCROLL)
42
43 #define FAR_SCROLL_SPEED 15.0f
44 #define NEAR_SCROLL_SPEED 120.0f
45
46 #define HORIZON_HEIGHT 100
47 #define REFLECTION_HEIGHT (240 - HORIZON_HEIGHT)
48
49 #define NORMALMAP_SCANLINE 372
50
51 static int init(void);
52 static void destroy(void);
53 static void start(long trans_time);
54 static void stop(long trans_time);
55 static void draw(void);
56
57 static void convert32To16(unsigned int *src32, unsigned short *dst16, unsigned int pixelCount);
58 static void processNormal();
59 static void initScrollTables();
60 static void updateScrollTables(float dt);
61
62
63
64 static unsigned short *background = 0;
65 static int backgroundW = 0;
66 static int backgroundH = 0;
67
68 static unsigned int lastFrameTime = 0;
69 static float lastFrameDuration = 0.0f;
70
71 static short *displacementMap;
72
73 static unsigned short *backBuffer;
74
75 static float scrollScaleTable[REFLECTION_HEIGHT];
76 static float scrollTable[REFLECTION_HEIGHT];
77 static int scrollTableRounded[REFLECTION_HEIGHT];
78 static int scrollModTable[REFLECTION_HEIGHT];
79 static float nearScrollAmount = 0.0f;
80
81 static unsigned char miniFXBuffer[1024];
82
83 static RLEBitmap *grobj = 0;
84 static RLEBitmap *rlePropeller = 0;
85
86 static struct screen scr = {
87         "galaxyrise",
88         init,
89         destroy,
90         start,
91         stop,
92         draw
93 };
94
95 struct screen *grise_screen(void)
96 {
97         return &scr;
98 }
99
100 static int init(void)
101 {
102         unsigned char *tmpBitmap;
103         int tmpBitmapW, tmpBitmapH;
104
105         /* Allocate back buffer */
106         backBuffer = (unsigned short*) calloc(BB_SIZE * BB_SIZE, sizeof(unsigned short));
107
108         /* grise.png contains the background (horizon), baked reflection and normalmap for displacement */
109         if (!(background = img_load_pixels(BG_FILENAME, &backgroundW, &backgroundH, IMG_FMT_RGBA32))) {
110                 fprintf(stderr, "failed to load image " BG_FILENAME "\n");
111                 return -1;
112         }
113
114         /* Convert to 16bpp */
115         convert32To16((unsigned int*)background, background, backgroundW * NORMALMAP_SCANLINE); /* Normalmap will keep its 32 bit color */
116
117         /* Load reflected objects */
118         if (!(tmpBitmap = img_load_pixels(GROBJ_01_FILENAME, &tmpBitmapW, &tmpBitmapH, IMG_FMT_GREY8))) {
119                 fprintf(stderr, "failed to load image " GROBJ_01_FILENAME "\n");
120                 return -1;
121         }
122
123         grobj = rleEncode(0, tmpBitmap, tmpBitmapW, tmpBitmapH);
124
125         img_free_pixels(tmpBitmap);
126
127         initScrollTables();
128
129         processNormal();
130
131         return 0;
132 }
133
134 static void destroy(void)
135 {
136         free(backBuffer);
137         backBuffer = 0;
138
139         img_free_pixels(background);
140
141         rleDestroy(grobj);
142 }
143
144 static void start(long trans_time)
145 {
146         lastFrameTime = time_msec;
147 }
148
149 static void stop(long trans_time)
150 {
151 }
152
153
154
155
156 static void draw(void)
157 {
158         int scroll = MIN_SCROLL + (MAX_SCROLL - MIN_SCROLL) * mouse_x / fb_width;
159         unsigned short *dst = backBuffer + PIXEL_PADDING;
160         unsigned short *src = background + scroll;
161         int scanline = 0;
162         int i = 0;
163         short *dispScanline;
164         int d;
165         int accum = 0;
166         int md, sc;
167         int scrolledIndex;
168
169         lastFrameDuration = (time_msec - lastFrameTime) / 1000.0f;
170         lastFrameTime = time_msec;
171
172         /* Update mini-effects here */
173         updatePropeller(4.0f * time_msec / 1000.0f);
174
175         /* First, render the horizon */
176         for (scanline = 0; scanline < HORIZON_HEIGHT; scanline++) {
177                 memcpy(dst, src, fb_width * 2);
178                 src += backgroundW;
179                 dst += BB_SIZE;
180         }
181
182         /* Create scroll offsets for all scanlines of the normalmap */
183         updateScrollTables(lastFrameDuration);
184
185         /* Render the baked reflection one scanline below its place, so that
186          * the displacement that follows will be done in a cache-friendly way
187          */
188         src -= PIXEL_PADDING; /* We want to also fill the PADDING pixels here */
189         dst = backBuffer + (HORIZON_HEIGHT + 1) * BB_SIZE;
190         for (scanline = 0; scanline < REFLECTION_HEIGHT; scanline++) {
191                 memcpy(dst, src, (fb_width + PIXEL_PADDING) * 2);
192                 src += backgroundW;
193                 dst += BB_SIZE;
194         }
195
196         /* Blit reflections first, to be  displaced */
197         for (i = 0; i < 5; i++) rleBlitScaleInv(backBuffer + PIXEL_PADDING, fb_width, fb_height, BB_SIZE, rlePropeller, 134 + (i-3) * 60, 200, 1.0f, 1.8f);
198
199         /* Perform displacement */
200         dst = backBuffer + HORIZON_HEIGHT * BB_SIZE + PIXEL_PADDING;
201         src = dst + BB_SIZE; /* The pixels to be displaced are 1 scanline below */
202         dispScanline = displacementMap;
203         for (scanline = 0; scanline < REFLECTION_HEIGHT; scanline++) {
204
205                 md = scrollModTable[scanline];
206                 sc = scrollTableRounded[scanline];
207                 accum = 0;
208
209                 for (i = 0; i < fb_width; i++) {
210                         /* Try to immitate modulo without the division */
211                         if (i == md) accum += md;
212                         scrolledIndex = i - accum + sc;
213                         if (scrolledIndex >= md) scrolledIndex -= md;
214
215                         /* Displace */
216                         d = dispScanline[scrolledIndex];
217                         *dst++ = src[i + d];
218                 }
219                 src += backgroundW;
220                 dst += BB_SIZE - fb_width;
221                 dispScanline += backgroundW;
222         }
223
224         /* Then after displacement, blit the objects */
225         for (i = 0; i < 5; i++) rleBlit(backBuffer + PIXEL_PADDING, fb_width, fb_height, BB_SIZE, rlePropeller, 134 + (i-3) * 60, 100);
226
227         /* Blit effect to framebuffer */
228         src = backBuffer + PIXEL_PADDING;
229         dst = vmem_back;
230         for (scanline = 0; scanline < fb_height; scanline++) {
231                 memcpy(dst, src, fb_width * 2);
232                 src += BB_SIZE;
233                 dst += fb_width;
234         }
235
236         swap_buffers(0);
237 }
238
239 /* src and dst can be the same */
240 static void convert32To16(unsigned int *src32, unsigned short *dst16, unsigned int pixelCount) {
241         unsigned int p;
242         while (pixelCount) {
243                 p = *src32++;
244                 *dst16++ =      ((p << 8) & 0xF800)             /* R */
245                         |               ((p >> 5) & 0x07E0)             /* G */
246                         |               ((p >> 19) & 0x001F);   /* B */
247                 pixelCount--;
248         }
249 }
250
251 /* Normal map preprocessing */
252 /* Scale normal with depth and unpack R component (horizontal component) */
253 static void processNormal() {
254         int scanline;
255         int i;
256         int x;
257         short maxDisplacement = 0;
258         short minDisplacement = 256;
259         unsigned short *dst;
260         short *dst2;
261         unsigned int *normalmap = (unsigned int*)background;
262         normalmap += NORMALMAP_SCANLINE * backgroundW;
263         dst = (unsigned short*)normalmap;
264         displacementMap = (short*)dst;
265         dst2 = displacementMap;
266
267         for (scanline = 0; scanline < REFLECTION_HEIGHT; scanline++) {
268                 scrollModTable[scanline] = (int) (backgroundW / scrollScaleTable[scanline] + 0.5f);
269                 for (i = 0; i < backgroundW; i++) {
270                         x = (int)(i * scrollScaleTable[scanline] + 0.5f);
271                         if (x < backgroundW) {
272                                 *dst = (unsigned short)(normalmap[x] >> 8) & 0xFF;
273                                 if ((short)*dst > maxDisplacement) maxDisplacement = (short)(*dst);
274                                 if ((short)*dst < minDisplacement) minDisplacement = (short)(*dst);
275                         } else {
276                                 *dst = 0;
277                         }
278                         dst++;
279                 }
280                 normalmap += backgroundW;
281         }
282
283         if (maxDisplacement == minDisplacement) {
284                 printf("Warning: grise normalmap fucked up\n");
285                 return;
286         }
287
288         /* Second pass - subtract half maximum displacement to displace in both directions */
289         for (scanline = 0; scanline < REFLECTION_HEIGHT; scanline++) {
290                 for (i = 0; i < backgroundW; i++) {
291                         /* Remember that MIN_SCROLL is the padding around the screen, so ti's the maximum displacement we can get (positive & negative) */
292                         *dst2 = 2 * MAX_DISPLACEMENT * (*dst2 - minDisplacement) / (maxDisplacement - minDisplacement) - MAX_DISPLACEMENT;
293                         *dst2 = (short)((float)*dst2 / scrollScaleTable[scanline] + 0.5f); /* Displacements must also scale with distance*/
294                         dst2++;
295                 }
296         }
297 }
298
299 static float distanceScale(int scanline) {
300         float farScale, t;
301         farScale = (float)NEAR_SCROLL_SPEED / (float)FAR_SCROLL_SPEED;
302         t = (float)scanline / ((float)REFLECTION_HEIGHT - 1);
303         return 1.0f / (1.0f / farScale + (1.0f - 1.0f / farScale) * t);
304 }
305
306 static void initScrollTables() {
307         int i = 0;
308         for (i = 0; i < REFLECTION_HEIGHT; i++) {
309                 scrollScaleTable[i] = distanceScale(i);
310                 scrollTable[i] = 0.0f;
311                 scrollTableRounded[i] = 0;
312         }
313 }
314
315
316 static void updateScrollTables(float dt) {
317         int i = 0;
318
319         nearScrollAmount += dt * NEAR_SCROLL_SPEED;
320         nearScrollAmount = (float) fmod(nearScrollAmount, 512.0f);
321
322         for (i = 0; i < REFLECTION_HEIGHT; i++) {
323                 scrollTable[i] = nearScrollAmount / scrollScaleTable[i];
324                 scrollTableRounded[i] = (int)(scrollTable[i] + 0.5f) % scrollModTable[i];
325         }
326 }
327
328 /* -------------------------------------------------------------------------------------------------
329  *                                   RLE STUFF
330  * -------------------------------------------------------------------------------------------------
331  */
332 /* Limit streak count per scanline so we can directly jump to specific scanline */
333 #define RLE_STREAKS_PER_SCANLINE 4
334 /* Every streak is encoded by 2 bytes: offset and count of black pixels in the streak */
335 #define RLE_BYTES_PER_SCANLINE RLE_STREAKS_PER_SCANLINE * 2
336 #define RLE_FILL_COLOR 0
337 #define RLE_FILL_COLOR_32 ((RLE_FILL_COLOR << 16) | RLE_FILL_COLOR)
338
339 #define RLE_FIXED_BITS 16
340
341 static int rleByteCount(int w, int h) {
342         return h * RLE_BYTES_PER_SCANLINE + w;
343 }
344
345 static RLEBitmap *rleCreate(unsigned int w, unsigned int h) {
346         RLEBitmap *ret = (RLEBitmap*)malloc(sizeof(RLEBitmap));
347         ret->w = w;
348         ret->h = h;
349
350         /* Add some padding at the end of the buffer, with the worst case for a scanline (w/2 streaks) */
351         ret->scans = (unsigned char*) calloc(rleByteCount(w, h), 1);
352
353         return ret;
354 }
355
356 static void rleDestroy(RLEBitmap *b) {
357         if (!b) return;
358         free(b->scans);
359         free(b);
360 }
361
362 static RLEBitmap *rleEncode(RLEBitmap *b, unsigned char *pixels, unsigned int w, unsigned int h) {
363         int scanline;
364         int i;
365         int penActive = 0;
366         int counter = 0;
367         int accum = 0;
368         unsigned char *output;
369
370         /* https://www.youtube.com/watch?v=RKMR02o1I88&feature=youtu.be&t=55 */
371         if (!b) b = rleCreate(w, h);
372         else memset(b->scans, 0, rleByteCount(b->w, b->h)); /* The following code assumes cleared array */
373
374         for (scanline = 0; scanline < h; scanline++) {
375                 output = b->scans + scanline * RLE_BYTES_PER_SCANLINE;
376                 accum = 0;
377                 for (i = 0; i < w; i++) {
378                         if (*pixels++) {
379                                 if (penActive) {
380                                         if (counter >= PIXEL_PADDING) {
381                                                 *output++ = (unsigned char) counter;
382                                                 counter = 0;
383                                                 *output++ = (unsigned char)accum;
384                                         }
385                                         counter++;
386                                         accum++;
387                                 } else {
388                                         *output++ = (unsigned char)accum;
389                                         counter = 1;
390                                         accum++;
391                                         penActive = 1;
392                                 }
393                         } else {
394                                 if (penActive) {
395                                         *output++ = (unsigned char)counter;
396                                         counter = 1;
397                                         accum++;
398                                         penActive = 0;
399                                 } else {
400                                         counter++;
401                                         accum++;
402                                 }
403                         }
404                 }
405
406                 if (penActive) {
407                         *output++ = (unsigned char)counter;
408                 }
409                 penActive = 0;
410                 counter = 0;
411         }
412
413         return b;
414 }
415
416 static void rleDistributeStreaks(RLEBitmap *bitmap) {
417         int scanline, halfW = bitmap->w >> 1;
418         unsigned char *ptr, tmp;
419
420         ptr = bitmap->scans;
421         for (scanline = 0; scanline < bitmap->h; scanline++) {
422                 if (ptr[0] >= halfW) {
423                         tmp = ptr[0];
424                         ptr[0] = ptr[6];
425                         ptr[6] = tmp;
426                         tmp = ptr[1];
427                         ptr[1] = ptr[7];
428                         ptr[7] = tmp;
429                 }
430
431                 ptr += 8;
432         }
433 }
434
435 static void rleBlit(unsigned short *dst, int dstW, int dstH, int dstStride,
436         RLEBitmap *bitmap, int blitX, int blitY)
437 {
438         int scanline = 0;
439         int streakPos = 0;
440         int streakLength = 0;
441         int streak = 0;
442         unsigned char *input = bitmap->scans;
443         unsigned short *output;
444         unsigned int *output32;
445
446         dst += blitX + blitY * dstStride;
447
448         for (scanline = blitY; scanline < blitY + bitmap->h; scanline++) {
449                 if (scanline < 0 || scanline >= dstH) continue;
450                 for (streak = 0; streak < RLE_STREAKS_PER_SCANLINE; streak++) {
451                         streakPos = *input++;
452                         streakLength = *input++;
453
454                         if ((streakPos + blitX) <= 0) continue;
455
456                         output = dst + streakPos;
457
458                         /* Check if we need to write the first pixel as 16bit */
459                         if (streakLength % 2) {
460                                 *output++ = RLE_FILL_COLOR;
461                         }
462
463                         /* Then, write 2 pixels at a time */
464                         streakLength >>= 1;
465                         output32 = (unsigned int*) output;
466                         while (streakLength--) {
467                                 *output32++ = RLE_FILL_COLOR_32;
468                         }
469                 }
470
471                 dst += dstStride;
472         }
473 }
474
475 static void interpolateScan(unsigned char *output, unsigned char *a, unsigned char *b, float t) {
476         static int div = 1 << 23;
477         int ti, i;
478
479         t += 1.0f;
480         ti = (*((unsigned int*)&t)) & 0x7FFFFF;
481
482         for (i = 0; i < RLE_BYTES_PER_SCANLINE; i++) {
483                 if (*a == 0) {
484                         *output++ = *b++;
485                         a++;
486                 } else {
487                         if (*b == 0) {
488                                 *output++ = *a++;
489                                 b++;
490                         } else {
491                                 *output++ = ((*b++ * ti) + (*a++ * (div - ti))) >> 23;
492                         }
493                 }
494         }
495 }
496
497 static void rleBlitScale(unsigned short *dst, int dstW, int dstH, int dstStride,
498         RLEBitmap *bitmap, int blitX, int blitY, float scaleX, float scaleY)
499 {
500         int scanline = 0;
501         int streakPos = 0;
502         int streakLength = 0;
503         int streak = 0;
504         unsigned short *output;
505         unsigned int *output32;
506         unsigned char *input;
507         int scanlineCounter = 0;
508         int scaleXFixed;
509         static unsigned char scan[512];
510
511         /*int blitW = (int)(bitmap->w * scaleX + 0.5f);*/
512         int blitH = (int)(bitmap->h * scaleY + 0.5f);
513
514         /* From this point on, scaleY will be inverted */
515         scaleY = 1.0f / scaleY;
516
517         scaleXFixed = (int)(scaleX * (float)(1 << RLE_FIXED_BITS) + 0.5f);
518
519         dst += blitX + blitY * dstStride;
520
521         for (scanline = blitY; scanline < blitY + blitH; scanline++) {
522                 float normalScan = scanlineCounter * scaleY; /* ScaleY  is inverted */
523                 unsigned char *scan0 = bitmap->scans + RLE_BYTES_PER_SCANLINE * (int)normalScan;
524                 unsigned char *scan1 = scan0 + RLE_BYTES_PER_SCANLINE;
525                 normalScan -= (int)normalScan;
526                 interpolateScan(scan, scan0, scan1, normalScan);
527                 input = scan;
528                 scanlineCounter++;
529
530                 if (scanline < 0 || scanline >= dstH) continue;
531                 for (streak = 0; streak < RLE_STREAKS_PER_SCANLINE; streak++) {
532                         streakPos = (*input++ * scaleXFixed) >> RLE_FIXED_BITS;
533                         streakLength = (*input++ * scaleXFixed) >> RLE_FIXED_BITS;
534
535                         if ((streakPos + blitX) <= 0) continue;
536
537                         output = dst + streakPos;
538
539                         /* Check if we need to write the first pixel as 16bit */
540                         if (streakLength % 2) {
541                                 *output++ = RLE_FILL_COLOR;
542                         }
543
544                         /* Then, write 2 pixels at a time */
545                         streakLength >>= 1;
546                         output32 = (unsigned int*)output;
547                         while (streakLength--) {
548                                 *output32++ = RLE_FILL_COLOR_32;
549                         }
550                 }
551
552                 dst += dstStride;
553         }
554 }
555
556
557
558 static void rleBlitScaleInv(unsigned short *dst, int dstW, int dstH, int dstStride,
559         RLEBitmap *bitmap, int blitX, int blitY, float scaleX, float scaleY)
560 {
561         int scanline = 0;
562         int streakPos = 0;
563         int streakLength = 0;
564         int streak = 0;
565         unsigned short *output;
566         unsigned int *output32;
567         unsigned char *input;
568         int scanlineCounter = 0;
569         int scaleXFixed;
570         static unsigned char scan[512];
571
572         /*int blitW = (int)(bitmap->w * scaleX + 0.5f);*/
573         int blitH = (int)(bitmap->h * scaleY + 0.5f);
574
575         /* From this point on, scaleY will be inverted */
576         scaleY = 1.0f / scaleY;
577
578         scaleXFixed = (int)(scaleX * (float)(1 << RLE_FIXED_BITS) + 0.5f);
579
580         dst += blitX + blitY * dstStride;
581
582         for (scanline = blitY; scanline > blitY - blitH; scanline--) {
583                 float normalScan = scanlineCounter * scaleY; /* ScaleY is inverted */
584                 unsigned char *scan0 = bitmap->scans + RLE_BYTES_PER_SCANLINE * (int)normalScan;
585                 unsigned char *scan1 = scan0 + RLE_BYTES_PER_SCANLINE;
586                 normalScan -= (int)normalScan;
587                 interpolateScan(scan, scan0, scan1, normalScan);
588                 input = scan;
589                 scanlineCounter++;
590
591                 if (scanline < 0 || scanline >= dstH) continue;
592                 for (streak = 0; streak < RLE_STREAKS_PER_SCANLINE; streak++) {
593                         streakPos = (*input++ * scaleXFixed) >> RLE_FIXED_BITS;
594                         streakLength = (*input++ * scaleXFixed) >> RLE_FIXED_BITS;
595
596                         if ((streakPos + blitX) <= 0) continue;
597
598                         output = dst + streakPos;
599
600                         /* Check if we need to write the first pixel as 16bit */
601                         if (streakLength % 2) {
602                                 *output++ = RLE_FILL_COLOR;
603                         }
604
605                         /* Then, write 2 pixels at a time */
606                         streakLength >>= 1;
607                         output32 = (unsigned int*)output;
608                         while (streakLength--) {
609                                 *output32++ = RLE_FILL_COLOR_32;
610                         }
611                 }
612
613                 dst -= dstStride;
614         }
615 }
616
617 /* -------------------------------------------------------------------------------------------------
618 *                                   PROPELLER STUFF
619 * -------------------------------------------------------------------------------------------------
620 */
621
622 #define PROPELLER_CIRCLE_RADIUS 18
623 #define PROPELLER_CIRCLE_RADIUS_SQ (PROPELLER_CIRCLE_RADIUS * PROPELLER_CIRCLE_RADIUS)
624
625 static struct {
626         int circleX[3];
627         int circleY[3];
628 } propellerState;
629
630 static void updatePropeller(float t) {
631         int i, j;
632         int cx, cy, count = 0;
633         unsigned char *dst;
634         float x = 0.0f;
635         float y = 18.0f;
636         float nx, ny;
637         float cost, sint;
638         static float sin120 = 0.86602540378f;
639         static float cos120 = -0.5f;
640
641         /* Rotate */
642         sint = sin(t);
643         cost = cos(t);
644         nx = x * cost - y * sint;
645         ny = y * cost + x * sint;
646         x = nx;
647         y = ny;
648         propellerState.circleX[0] = (int)(x + 0.5f) + 16;
649         propellerState.circleY[0] = (int)(y + 0.5f) + 16;
650
651         /* Rotate by 120 degrees, for the second circle */
652         nx = x * cos120 - y * sin120;
653         ny = y * cos120 + x * sin120;
654         x = nx;
655         y = ny;
656         propellerState.circleX[1] = (int)(x + 0.5f) + 16;
657         propellerState.circleY[1] = (int)(y + 0.5f) + 16;
658
659         /* 3rd circle */
660         nx = x * cos120 - y * sin120;
661         ny = y * cos120 + x * sin120;
662         x = nx;
663         y = ny;
664         propellerState.circleX[2] = (int)(x + 0.5f) + 16;
665         propellerState.circleY[2] = (int)(y + 0.5f) + 16;
666
667         /* Write effect to the mini fx buffer*/
668         dst = miniFXBuffer;
669         for (j = 0; j < 32; j++) {
670                 for (i = 0; i < 32; i++) {
671                         count = 0;
672
673                         /* First circle */
674                         cx = propellerState.circleX[0] - i;
675                         cy = propellerState.circleY[0] - j;
676                         if (cx*cx + cy*cy < PROPELLER_CIRCLE_RADIUS_SQ) count++;
677
678                         /* 2nd circle */
679                         cx = propellerState.circleX[1] - i;
680                         cy = propellerState.circleY[1] - j;
681                         if (cx*cx + cy*cy < PROPELLER_CIRCLE_RADIUS_SQ) count++;
682
683                         /* 3rd circle */
684                         cx = propellerState.circleX[2] - i;
685                         cy = propellerState.circleY[2] - j;
686                         if (cx*cx + cy*cy < PROPELLER_CIRCLE_RADIUS_SQ) count++;
687
688                         *dst++ = count >= 2;
689                 }
690         }
691
692         /* Then, encode to rle */
693         rlePropeller = rleEncode(rlePropeller, miniFXBuffer, 32, 32);
694
695         /* Distribute the produced streaks so that they don't produce garbage when interpolated */
696         rleDistributeStreaks(rlePropeller);
697 }