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