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 #define BG_FILENAME "data/grise.png"
30 #define GROBJ_01_FILENAME "data/grobj_01.png"
32 #define BB_SIZE 512 /* Let's use a power of 2. Maybe we'll zoom/rotate the effect */
34 /* Every backBuffer scanline is guaranteed to have that many dummy pixels before and after */
35 #define PIXEL_PADDING 32
37 /* Make sure this is less than PIXEL_PADDING*/
38 #define MAX_DISPLACEMENT 16
40 #define MIN_SCROLL PIXEL_PADDING
41 #define MAX_SCROLL (backgroundW - fb_width - MIN_SCROLL)
43 #define FAR_SCROLL_SPEED 15.0f
44 #define NEAR_SCROLL_SPEED 120.0f
46 #define HORIZON_HEIGHT 100
47 #define REFLECTION_HEIGHT (240 - HORIZON_HEIGHT)
49 #define NORMALMAP_SCANLINE 372
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);
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);
64 static unsigned short *background = 0;
65 static unsigned int backgroundW = 0;
66 static unsigned int backgroundH = 0;
68 static unsigned int lastFrameTime = 0;
69 static float lastFrameDuration = 0.0f;
71 static short *displacementMap;
73 static unsigned short *backBuffer;
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;
81 static char miniFXBuffer[1024];
83 static RLEBitmap *grobj = 0;
84 static RLEBitmap *rlePropeller = 0;
86 static struct screen scr = {
95 struct screen *grise_screen(void)
101 static int init(void)
103 unsigned char *tmpBitmap;
104 int tmpBitmapW, tmpBitmapH;
106 /* Allocate back buffer */
107 backBuffer = (unsigned short*) calloc(BB_SIZE * BB_SIZE, sizeof(unsigned short));
109 /* grise.png contains the background (horizon), baked reflection and normalmap for displacement */
110 if (!(background = img_load_pixels(BG_FILENAME, &backgroundW, &backgroundH, IMG_FMT_RGBA32))) {
111 fprintf(stderr, "failed to load image " BG_FILENAME "\n");
115 /* Convert to 16bpp */
116 convert32To16((unsigned int*)background, background, backgroundW * NORMALMAP_SCANLINE); /* Normalmap will keep its 32 bit color */
118 /* Load reflected objects */
119 if (!(tmpBitmap = img_load_pixels(GROBJ_01_FILENAME, &tmpBitmapW, &tmpBitmapH, IMG_FMT_GREY8))) {
120 fprintf(stderr, "failed to load image " GROBJ_01_FILENAME "\n");
124 grobj = rleEncode(0, tmpBitmap, tmpBitmapW, tmpBitmapH);
126 img_free_pixels(tmpBitmap);
139 static void destroy(void)
144 img_free_pixels(background);
149 static void start(long trans_time)
151 lastFrameTime = time_msec;
154 static void stop(long trans_time)
161 static void draw(void)
163 int scroll = MIN_SCROLL + (MAX_SCROLL - MIN_SCROLL) * mouse_x / fb_width;
164 unsigned short *dst = backBuffer + PIXEL_PADDING;
165 unsigned short *src = background + scroll;
171 lastFrameDuration = (time_msec - lastFrameTime) / 1000.0f;
172 lastFrameTime = time_msec;
174 /* Update mini-effects here */
175 updatePropeller(4.0f * time_msec / 1000.0f);
177 /* First, render the horizon */
178 for (scanline = 0; scanline < HORIZON_HEIGHT; scanline++) {
179 memcpy(dst, src, fb_width * 2);
184 /* Create scroll offsets for all scanlines of the normalmap */
185 updateScrollTables(lastFrameDuration);
187 /* Render the baked reflection one scanline below its place, so that
188 * the displacement that follows will be done in a cache-friendly way
190 src -= PIXEL_PADDING; /* We want to also fill the PADDING pixels here */
191 dst = backBuffer + (HORIZON_HEIGHT + 1) * BB_SIZE;
192 for (scanline = 0; scanline < REFLECTION_HEIGHT; scanline++) {
193 memcpy(dst, src, (fb_width + PIXEL_PADDING) * 2);
198 /* Blit reflections first, to be displaced */
199 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);
201 /* Perform displacement */
202 dst = backBuffer + HORIZON_HEIGHT * BB_SIZE + PIXEL_PADDING;
203 src = dst + BB_SIZE; /* The pixels to be displaced are 1 scanline below */
204 dispScanline = displacementMap;
205 for (scanline = 0; scanline < REFLECTION_HEIGHT; scanline++) {
206 for (i = 0; i < fb_width; i++) {
207 d = dispScanline[(i + scrollTableRounded[scanline]) % scrollModTable[scanline]];
211 dst += BB_SIZE - fb_width;
212 dispScanline += backgroundW;
215 /* Then after displacement, blit the objects */
216 for (i = 0; i < 5; i++) rleBlit(backBuffer + PIXEL_PADDING, fb_width, fb_height, BB_SIZE, rlePropeller, 134 + (i-3) * 60, 100);
218 /* Blit effect to framebuffer */
219 src = backBuffer + PIXEL_PADDING;
221 for (scanline = 0; scanline < fb_height; scanline++) {
222 memcpy(dst, src, fb_width * 2);
230 /* src and dst can be the same */
231 static void convert32To16(unsigned int *src32, unsigned short *dst16, unsigned int pixelCount) {
235 *dst16++ = ((p << 8) & 0xF800) /* R */
236 | ((p >> 5) & 0x07E0) /* G */
237 | ((p >> 19) & 0x001F); /* B */
242 /* Normal map preprocessing */
243 /* Scale normal with depth and unpack R component (horizontal component) */
244 static void processNormal() {
248 short maxDisplacement = 0;
249 short minDisplacement = 256;
252 unsigned int *normalmap = (unsigned int*)background;
253 normalmap += NORMALMAP_SCANLINE * backgroundW;
254 dst = (unsigned short*)normalmap;
255 displacementMap = (short*)dst;
256 dst2 = displacementMap;
258 for (scanline = 0; scanline < REFLECTION_HEIGHT; scanline++) {
259 scrollModTable[scanline] = (int) (backgroundW / scrollScaleTable[scanline] + 0.5f);
260 for (i = 0; i < backgroundW; i++) {
261 x = (int)(i * scrollScaleTable[scanline] + 0.5f);
262 if (x < backgroundW) {
263 *dst = (unsigned short)(normalmap[x] >> 8) & 0xFF;
264 if ((short)*dst > maxDisplacement) maxDisplacement = (short)(*dst);
265 if ((short)*dst < minDisplacement) minDisplacement = (short)(*dst);
271 normalmap += backgroundW;
274 if (maxDisplacement == minDisplacement) {
275 printf("Warning: grise normalmap fucked up\n");
279 /* Second pass - subtract half maximum displacement to displace in both directions */
280 for (scanline = 0; scanline < REFLECTION_HEIGHT; scanline++) {
281 for (i = 0; i < backgroundW; i++) {
282 /* Remember that MIN_SCROLL is the padding around the screen, so ti's the maximum displacement we can get (positive & negative) */
283 *dst2 = 2 * MAX_DISPLACEMENT * (*dst2 - minDisplacement) / (maxDisplacement - minDisplacement) - MAX_DISPLACEMENT;
284 *dst2 = (short)((float)*dst2 / scrollScaleTable[scanline] + 0.5f); /* Displacements must also scale with distance*/
290 static float distanceScale(int scanline) {
292 farScale = (float)NEAR_SCROLL_SPEED / (float)FAR_SCROLL_SPEED;
293 t = (float)scanline / ((float)REFLECTION_HEIGHT - 1);
294 return 1.0f / (1.0f / farScale + (1.0f - 1.0f / farScale) * t);
297 static void initScrollTables() {
299 for (i = 0; i < REFLECTION_HEIGHT; i++) {
300 scrollScaleTable[i] = distanceScale(i);
301 scrollTable[i] = 0.0f;
302 scrollTableRounded[i] = 0;
307 static void updateScrollTables(float dt) {
310 nearScrollAmount += dt * NEAR_SCROLL_SPEED;
311 nearScrollAmount = (float) fmod(nearScrollAmount, 512.0f);
313 for (i = 0; i < REFLECTION_HEIGHT; i++) {
314 scrollTable[i] = nearScrollAmount / scrollScaleTable[i];
315 scrollTableRounded[i] = (int)(scrollTable[i] + 0.5f) % scrollModTable[i];
319 /* -------------------------------------------------------------------------------------------------
321 * -------------------------------------------------------------------------------------------------
323 /* Limit streak count per scanline so we can directly jump to specific scanline */
324 #define RLE_STREAKS_PER_SCANLINE 4
325 /* Every streak is encoded by 2 bytes: offset and count of black pixels in the streak */
326 #define RLE_BYTES_PER_SCANLINE RLE_STREAKS_PER_SCANLINE * 2
327 #define RLE_FILL_COLOR 0
328 #define RLE_FILL_COLOR_32 ((RLE_FILL_COLOR << 16) | RLE_FILL_COLOR)
330 #define RLE_FIXED_BITS 16
332 static int rleByteCount(int w, int h) {
333 return h * RLE_BYTES_PER_SCANLINE + w;
336 static RLEBitmap *rleCreate(unsigned int w, unsigned int h) {
337 RLEBitmap *ret = (RLEBitmap*)malloc(sizeof(RLEBitmap));
341 /* Add some padding at the end of the buffer, with the worst case for a scanline (w/2 streaks) */
342 ret->scans = (unsigned char*) calloc(rleByteCount(w, h), 1);
347 static void rleDestroy(RLEBitmap *b) {
353 static RLEBitmap *rleEncode(RLEBitmap *b, unsigned char *pixels, unsigned int w, unsigned int h) {
359 unsigned char *output;
361 /* https://www.youtube.com/watch?v=RKMR02o1I88&feature=youtu.be&t=55 */
362 if (!b) b = rleCreate(w, h);
363 else memset(b->scans, 0, rleByteCount(b->w, b->h)); /* The following code assumes cleared array */
365 for (scanline = 0; scanline < h; scanline++) {
366 output = b->scans + scanline * RLE_BYTES_PER_SCANLINE;
368 for (i = 0; i < w; i++) {
371 if (counter >= PIXEL_PADDING) {
372 *output++ = (unsigned char) counter;
374 *output++ = (unsigned char)accum;
379 *output++ = (unsigned char)accum;
386 *output++ = (unsigned char)counter;
398 *output++ = (unsigned char)counter;
407 static void rleDistributeStreaks(RLEBitmap *bitmap) {
408 int scanline, halfW = bitmap->w >> 1;
409 unsigned char *ptr, tmp;
412 for (scanline = 0; scanline < bitmap->h; scanline++) {
413 if (ptr[0] >= halfW) {
426 static void rleBlit(unsigned short *dst, int dstW, int dstH, int dstStride,
427 RLEBitmap *bitmap, int blitX, int blitY)
431 int streakLength = 0;
433 unsigned char *input = bitmap->scans;
434 unsigned short *output;
435 unsigned int *output32;
437 dst += blitX + blitY * dstStride;
439 for (scanline = blitY; scanline < blitY + bitmap->h; scanline++) {
440 if (scanline < 0 || scanline >= dstH) continue;
441 for (streak = 0; streak < RLE_STREAKS_PER_SCANLINE; streak++) {
442 streakPos = *input++;
443 streakLength = *input++;
445 if ((streakPos + blitX) <= 0) continue;
447 output = dst + streakPos;
449 /* Check if we need to write the first pixel as 16bit */
450 if (streakLength % 2) {
451 *output++ = RLE_FILL_COLOR;
454 /* Then, write 2 pixels at a time */
456 output32 = (unsigned int*) output;
457 while (streakLength--) {
458 *output32++ = RLE_FILL_COLOR_32;
466 static void interpolateScan(unsigned char *output, unsigned char *a, unsigned char *b, float t) {
467 static int div = 1 << 23;
471 ti = (*((unsigned int*)&t)) & 0x7FFFFF;
473 for (i = 0; i < RLE_BYTES_PER_SCANLINE; i++) {
482 *output++ = ((*b++ * ti) + (*a++ * (div - ti))) >> 23;
488 static void rleBlitScale(unsigned short *dst, int dstW, int dstH, int dstStride,
489 RLEBitmap *bitmap, int blitX, int blitY, float scaleX, float scaleY)
493 int streakLength = 0;
495 unsigned short *output;
496 unsigned int *output32;
497 unsigned char *input;
498 int scanlineCounter = 0;
500 static unsigned char scan[512];
502 int blitW = (int)(bitmap->w * scaleX + 0.5f);
503 int blitH = (int)(bitmap->h * scaleY + 0.5f);
505 /* From this point on, scaleY will be inverted */
506 scaleY = 1.0f / scaleY;
508 scaleXFixed = (int)(scaleX * (float)(1 << RLE_FIXED_BITS) + 0.5f);
510 dst += blitX + blitY * dstStride;
512 for (scanline = blitY; scanline < blitY + blitH; scanline++) {
513 float normalScan = scanlineCounter * scaleY; /* ScaleY is inverted */
514 unsigned char *scan0 = bitmap->scans + RLE_BYTES_PER_SCANLINE * (int)normalScan;
515 unsigned char *scan1 = scan0 + RLE_BYTES_PER_SCANLINE;
516 normalScan -= (int)normalScan;
517 interpolateScan(scan, scan0, scan1, normalScan);
521 if (scanline < 0 || scanline >= dstH) continue;
522 for (streak = 0; streak < RLE_STREAKS_PER_SCANLINE; streak++) {
523 streakPos = (*input++ * scaleXFixed) >> RLE_FIXED_BITS;
524 streakLength = (*input++ * scaleXFixed) >> RLE_FIXED_BITS;
526 if ((streakPos + blitX) <= 0) continue;
528 output = dst + streakPos;
530 /* Check if we need to write the first pixel as 16bit */
531 if (streakLength % 2) {
532 *output++ = RLE_FILL_COLOR;
535 /* Then, write 2 pixels at a time */
537 output32 = (unsigned int*)output;
538 while (streakLength--) {
539 *output32++ = RLE_FILL_COLOR_32;
549 static void rleBlitScaleInv(unsigned short *dst, int dstW, int dstH, int dstStride,
550 RLEBitmap *bitmap, int blitX, int blitY, float scaleX, float scaleY)
554 int streakLength = 0;
556 unsigned short *output;
557 unsigned int *output32;
558 unsigned char *input;
559 int scanlineCounter = 0;
561 static unsigned char scan[512];
563 int blitW = (int)(bitmap->w * scaleX + 0.5f);
564 int blitH = (int)(bitmap->h * scaleY + 0.5f);
566 /* From this point on, scaleY will be inverted */
567 scaleY = 1.0f / scaleY;
569 scaleXFixed = (int)(scaleX * (float)(1 << RLE_FIXED_BITS) + 0.5f);
571 dst += blitX + blitY * dstStride;
573 for (scanline = blitY; scanline > blitY - blitH; scanline--) {
574 float normalScan = scanlineCounter * scaleY; /* ScaleY is inverted */
575 unsigned char *scan0 = bitmap->scans + RLE_BYTES_PER_SCANLINE * (int)normalScan;
576 unsigned char *scan1 = scan0 + RLE_BYTES_PER_SCANLINE;
577 normalScan -= (int)normalScan;
578 interpolateScan(scan, scan0, scan1, normalScan);
582 if (scanline < 0 || scanline >= dstH) continue;
583 for (streak = 0; streak < RLE_STREAKS_PER_SCANLINE; streak++) {
584 streakPos = (*input++ * scaleXFixed) >> RLE_FIXED_BITS;
585 streakLength = (*input++ * scaleXFixed) >> RLE_FIXED_BITS;
587 if ((streakPos + blitX) <= 0) continue;
589 output = dst + streakPos;
591 /* Check if we need to write the first pixel as 16bit */
592 if (streakLength % 2) {
593 *output++ = RLE_FILL_COLOR;
596 /* Then, write 2 pixels at a time */
598 output32 = (unsigned int*)output;
599 while (streakLength--) {
600 *output32++ = RLE_FILL_COLOR_32;
608 /* -------------------------------------------------------------------------------------------------
610 * -------------------------------------------------------------------------------------------------
613 #define PROPELLER_CIRCLE_RADIUS 18
614 #define PROPELLER_CIRCLE_RADIUS_SQ (PROPELLER_CIRCLE_RADIUS * PROPELLER_CIRCLE_RADIUS)
621 static void updatePropeller(float t) {
623 int cx, cy, count = 0;
629 static float sin120 = 0.86602540378f;
630 static float cos120 = -0.5f;
635 nx = x * cost - y * sint;
636 ny = y * cost + x * sint;
639 propellerState.circleX[0] = (int)(x + 0.5f) + 16;
640 propellerState.circleY[0] = (int)(y + 0.5f) + 16;
642 /* Rotate by 120 degrees, for the second circle */
643 nx = x * cos120 - y * sin120;
644 ny = y * cos120 + x * sin120;
647 propellerState.circleX[1] = (int)(x + 0.5f) + 16;
648 propellerState.circleY[1] = (int)(y + 0.5f) + 16;
651 nx = x * cos120 - y * sin120;
652 ny = y * cos120 + x * sin120;
655 propellerState.circleX[2] = (int)(x + 0.5f) + 16;
656 propellerState.circleY[2] = (int)(y + 0.5f) + 16;
658 /* Write effect to the mini fx buffer*/
660 for (j = 0; j < 32; j++) {
661 for (i = 0; i < 32; i++) {
665 cx = propellerState.circleX[0] - i;
666 cy = propellerState.circleY[0] - j;
667 if (cx*cx + cy*cy < PROPELLER_CIRCLE_RADIUS_SQ) count++;
670 cx = propellerState.circleX[1] - i;
671 cy = propellerState.circleY[1] - j;
672 if (cx*cx + cy*cy < PROPELLER_CIRCLE_RADIUS_SQ) count++;
675 cx = propellerState.circleX[2] - i;
676 cy = propellerState.circleY[2] - j;
677 if (cx*cx + cy*cy < PROPELLER_CIRCLE_RADIUS_SQ) count++;
683 /* Then, encode to rle */
684 rlePropeller = rleEncode(rlePropeller, miniFXBuffer, 32, 32);
686 /* Distribute the produced streaks so that they don't produce garbage when interpolated */
687 rleDistributeStreaks(rlePropeller);