10 /* APPROX. 170 FPS Minimum */
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);
27 static void updatePropeller(float t);
29 extern void drawFps(unsigned short *vram);
31 #define BG_FILENAME "data/grise.png"
32 #define GROBJ_01_FILENAME "data/grobj_01.png"
34 #define BB_SIZE 512 /* Let's use a power of 2. Maybe we'll zoom/rotate the effect */
36 /* Every backBuffer scanline is guaranteed to have that many dummy pixels before and after */
37 #define PIXEL_PADDING 32
39 /* Make sure this is less than PIXEL_PADDING*/
40 #define MAX_DISPLACEMENT 16
42 #define MIN_SCROLL PIXEL_PADDING
43 #define MAX_SCROLL (backgroundW - fb_width - MIN_SCROLL)
45 #define FAR_SCROLL_SPEED 15.0f
46 #define NEAR_SCROLL_SPEED 120.0f
48 #define HORIZON_HEIGHT 100
49 #define REFLECTION_HEIGHT (240 - HORIZON_HEIGHT)
51 #define NORMALMAP_SCANLINE 372
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);
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);
66 static unsigned short *background = 0;
67 static unsigned int backgroundW = 0;
68 static unsigned int backgroundH = 0;
70 static unsigned int lastFrameTime = 0;
71 static float lastFrameDuration = 0.0f;
73 static short *displacementMap;
75 static unsigned short *backBuffer;
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;
83 static char miniFXBuffer[1024];
85 static RLEBitmap *grobj = 0;
86 static RLEBitmap *rlePropeller = 0;
88 static struct screen scr = {
97 struct screen *grise_screen(void)
102 static int init(void)
104 unsigned char *tmpBitmap;
105 int tmpBitmapW, tmpBitmapH;
107 /* Allocate back buffer */
108 backBuffer = (unsigned short*) calloc(BB_SIZE * BB_SIZE, sizeof(unsigned short));
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");
116 /* Convert to 16bpp */
117 convert32To16((unsigned int*)background, background, backgroundW * NORMALMAP_SCANLINE); /* Normalmap will keep its 32 bit color */
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");
125 grobj = rleEncode(0, tmpBitmap, tmpBitmapW, tmpBitmapH);
127 img_free_pixels(tmpBitmap);
140 static void destroy(void)
145 img_free_pixels(background);
150 static void start(long trans_time)
152 lastFrameTime = time_msec;
155 static void stop(long trans_time)
162 static void draw(void)
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;
175 lastFrameDuration = (time_msec - lastFrameTime) / 1000.0f;
176 lastFrameTime = time_msec;
178 /* Update mini-effects here */
179 updatePropeller(4.0f * time_msec / 1000.0f);
181 /* First, render the horizon */
182 for (scanline = 0; scanline < HORIZON_HEIGHT; scanline++) {
183 memcpy(dst, src, fb_width * 2);
188 /* Create scroll offsets for all scanlines of the normalmap */
189 updateScrollTables(lastFrameDuration);
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
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);
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);
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++) {
211 md = scrollModTable[scanline];
212 sc = scrollTableRounded[scanline];
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;
222 d = dispScanline[scrolledIndex];
226 dst += BB_SIZE - fb_width;
227 dispScanline += backgroundW;
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);
233 /* Blit effect to framebuffer */
234 src = backBuffer + PIXEL_PADDING;
236 for (scanline = 0; scanline < fb_height; scanline++) {
237 memcpy(dst, src, fb_width * 2);
247 /* src and dst can be the same */
248 static void convert32To16(unsigned int *src32, unsigned short *dst16, unsigned int pixelCount) {
252 *dst16++ = ((p << 8) & 0xF800) /* R */
253 | ((p >> 5) & 0x07E0) /* G */
254 | ((p >> 19) & 0x001F); /* B */
259 /* Normal map preprocessing */
260 /* Scale normal with depth and unpack R component (horizontal component) */
261 static void processNormal() {
265 short maxDisplacement = 0;
266 short minDisplacement = 256;
269 unsigned int *normalmap = (unsigned int*)background;
270 normalmap += NORMALMAP_SCANLINE * backgroundW;
271 dst = (unsigned short*)normalmap;
272 displacementMap = (short*)dst;
273 dst2 = displacementMap;
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);
288 normalmap += backgroundW;
291 if (maxDisplacement == minDisplacement) {
292 printf("Warning: grise normalmap fucked up\n");
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*/
307 static float distanceScale(int scanline) {
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);
314 static void initScrollTables() {
316 for (i = 0; i < REFLECTION_HEIGHT; i++) {
317 scrollScaleTable[i] = distanceScale(i);
318 scrollTable[i] = 0.0f;
319 scrollTableRounded[i] = 0;
324 static void updateScrollTables(float dt) {
327 nearScrollAmount += dt * NEAR_SCROLL_SPEED;
328 nearScrollAmount = (float) fmod(nearScrollAmount, 512.0f);
330 for (i = 0; i < REFLECTION_HEIGHT; i++) {
331 scrollTable[i] = nearScrollAmount / scrollScaleTable[i];
332 scrollTableRounded[i] = (int)(scrollTable[i] + 0.5f) % scrollModTable[i];
336 /* -------------------------------------------------------------------------------------------------
338 * -------------------------------------------------------------------------------------------------
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)
347 #define RLE_FIXED_BITS 16
349 static int rleByteCount(int w, int h) {
350 return h * RLE_BYTES_PER_SCANLINE + w;
353 static RLEBitmap *rleCreate(unsigned int w, unsigned int h) {
354 RLEBitmap *ret = (RLEBitmap*)malloc(sizeof(RLEBitmap));
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);
364 static void rleDestroy(RLEBitmap *b) {
370 static RLEBitmap *rleEncode(RLEBitmap *b, unsigned char *pixels, unsigned int w, unsigned int h) {
376 unsigned char *output;
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 */
382 for (scanline = 0; scanline < h; scanline++) {
383 output = b->scans + scanline * RLE_BYTES_PER_SCANLINE;
385 for (i = 0; i < w; i++) {
388 if (counter >= PIXEL_PADDING) {
389 *output++ = (unsigned char) counter;
391 *output++ = (unsigned char)accum;
396 *output++ = (unsigned char)accum;
403 *output++ = (unsigned char)counter;
415 *output++ = (unsigned char)counter;
424 static void rleDistributeStreaks(RLEBitmap *bitmap) {
425 int scanline, halfW = bitmap->w >> 1;
426 unsigned char *ptr, tmp;
429 for (scanline = 0; scanline < bitmap->h; scanline++) {
430 if (ptr[0] >= halfW) {
443 static void rleBlit(unsigned short *dst, int dstW, int dstH, int dstStride,
444 RLEBitmap *bitmap, int blitX, int blitY)
448 int streakLength = 0;
450 unsigned char *input = bitmap->scans;
451 unsigned short *output;
452 unsigned int *output32;
454 dst += blitX + blitY * dstStride;
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++;
462 if ((streakPos + blitX) <= 0) continue;
464 output = dst + streakPos;
466 /* Check if we need to write the first pixel as 16bit */
467 if (streakLength % 2) {
468 *output++ = RLE_FILL_COLOR;
471 /* Then, write 2 pixels at a time */
473 output32 = (unsigned int*) output;
474 while (streakLength--) {
475 *output32++ = RLE_FILL_COLOR_32;
483 static void interpolateScan(unsigned char *output, unsigned char *a, unsigned char *b, float t) {
484 static int div = 1 << 23;
488 ti = (*((unsigned int*)&t)) & 0x7FFFFF;
490 for (i = 0; i < RLE_BYTES_PER_SCANLINE; i++) {
499 *output++ = ((*b++ * ti) + (*a++ * (div - ti))) >> 23;
505 static void rleBlitScale(unsigned short *dst, int dstW, int dstH, int dstStride,
506 RLEBitmap *bitmap, int blitX, int blitY, float scaleX, float scaleY)
510 int streakLength = 0;
512 unsigned short *output;
513 unsigned int *output32;
514 unsigned char *input;
515 int scanlineCounter = 0;
517 static unsigned char scan[512];
519 int blitW = (int)(bitmap->w * scaleX + 0.5f);
520 int blitH = (int)(bitmap->h * scaleY + 0.5f);
522 /* From this point on, scaleY will be inverted */
523 scaleY = 1.0f / scaleY;
525 scaleXFixed = (int)(scaleX * (float)(1 << RLE_FIXED_BITS) + 0.5f);
527 dst += blitX + blitY * dstStride;
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);
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;
543 if ((streakPos + blitX) <= 0) continue;
545 output = dst + streakPos;
547 /* Check if we need to write the first pixel as 16bit */
548 if (streakLength % 2) {
549 *output++ = RLE_FILL_COLOR;
552 /* Then, write 2 pixels at a time */
554 output32 = (unsigned int*)output;
555 while (streakLength--) {
556 *output32++ = RLE_FILL_COLOR_32;
566 static void rleBlitScaleInv(unsigned short *dst, int dstW, int dstH, int dstStride,
567 RLEBitmap *bitmap, int blitX, int blitY, float scaleX, float scaleY)
571 int streakLength = 0;
573 unsigned short *output;
574 unsigned int *output32;
575 unsigned char *input;
576 int scanlineCounter = 0;
578 static unsigned char scan[512];
580 int blitW = (int)(bitmap->w * scaleX + 0.5f);
581 int blitH = (int)(bitmap->h * scaleY + 0.5f);
583 /* From this point on, scaleY will be inverted */
584 scaleY = 1.0f / scaleY;
586 scaleXFixed = (int)(scaleX * (float)(1 << RLE_FIXED_BITS) + 0.5f);
588 dst += blitX + blitY * dstStride;
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);
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;
604 if ((streakPos + blitX) <= 0) continue;
606 output = dst + streakPos;
608 /* Check if we need to write the first pixel as 16bit */
609 if (streakLength % 2) {
610 *output++ = RLE_FILL_COLOR;
613 /* Then, write 2 pixels at a time */
615 output32 = (unsigned int*)output;
616 while (streakLength--) {
617 *output32++ = RLE_FILL_COLOR_32;
625 /* -------------------------------------------------------------------------------------------------
627 * -------------------------------------------------------------------------------------------------
630 #define PROPELLER_CIRCLE_RADIUS 18
631 #define PROPELLER_CIRCLE_RADIUS_SQ (PROPELLER_CIRCLE_RADIUS * PROPELLER_CIRCLE_RADIUS)
638 static void updatePropeller(float t) {
640 int cx, cy, count = 0;
646 static float sin120 = 0.86602540378f;
647 static float cos120 = -0.5f;
652 nx = x * cost - y * sint;
653 ny = y * cost + x * sint;
656 propellerState.circleX[0] = (int)(x + 0.5f) + 16;
657 propellerState.circleY[0] = (int)(y + 0.5f) + 16;
659 /* Rotate by 120 degrees, for the second circle */
660 nx = x * cos120 - y * sin120;
661 ny = y * cos120 + x * sin120;
664 propellerState.circleX[1] = (int)(x + 0.5f) + 16;
665 propellerState.circleY[1] = (int)(y + 0.5f) + 16;
668 nx = x * cos120 - y * sin120;
669 ny = y * cos120 + x * sin120;
672 propellerState.circleX[2] = (int)(x + 0.5f) + 16;
673 propellerState.circleY[2] = (int)(y + 0.5f) + 16;
675 /* Write effect to the mini fx buffer*/
677 for (j = 0; j < 32; j++) {
678 for (i = 0; i < 32; i++) {
682 cx = propellerState.circleX[0] - i;
683 cy = propellerState.circleY[0] - j;
684 if (cx*cx + cy*cy < PROPELLER_CIRCLE_RADIUS_SQ) count++;
687 cx = propellerState.circleX[1] - i;
688 cy = propellerState.circleY[1] - j;
689 if (cx*cx + cy*cy < PROPELLER_CIRCLE_RADIUS_SQ) count++;
692 cx = propellerState.circleX[2] - i;
693 cy = propellerState.circleY[2] - j;
694 if (cx*cx + cy*cy < PROPELLER_CIRCLE_RADIUS_SQ) count++;
700 /* Then, encode to rle */
701 rlePropeller = rleEncode(rlePropeller, miniFXBuffer, 32, 32);
703 /* Distribute the produced streaks so that they don't produce garbage when interpolated */
704 rleDistributeStreaks(rlePropeller);