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 int backgroundW = 0;
66 static 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 unsigned char miniFXBuffer[1024];
83 static RLEBitmap *grobj = 0;
84 static RLEBitmap *rlePropeller = 0;
86 static struct screen scr = {
95 struct screen *grise_screen(void)
100 static int init(void)
102 unsigned char *tmpBitmap;
103 int tmpBitmapW, tmpBitmapH;
105 /* Allocate back buffer */
106 backBuffer = (unsigned short*) calloc(BB_SIZE * BB_SIZE, sizeof(unsigned short));
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");
114 /* Convert to 16bpp */
115 convert32To16((unsigned int*)background, background, backgroundW * NORMALMAP_SCANLINE); /* Normalmap will keep its 32 bit color */
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");
123 grobj = rleEncode(0, tmpBitmap, tmpBitmapW, tmpBitmapH);
125 img_free_pixels(tmpBitmap);
134 static void destroy(void)
139 img_free_pixels(background);
144 static void start(long trans_time)
146 lastFrameTime = time_msec;
149 static void stop(long trans_time)
156 static void draw(void)
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;
169 lastFrameDuration = (time_msec - lastFrameTime) / 1000.0f;
170 lastFrameTime = time_msec;
172 /* Update mini-effects here */
173 updatePropeller(4.0f * time_msec / 1000.0f);
175 /* First, render the horizon */
176 for (scanline = 0; scanline < HORIZON_HEIGHT; scanline++) {
177 memcpy(dst, src, fb_width * 2);
182 /* Create scroll offsets for all scanlines of the normalmap */
183 updateScrollTables(lastFrameDuration);
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
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);
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);
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++) {
205 md = scrollModTable[scanline];
206 sc = scrollTableRounded[scanline];
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;
216 d = dispScanline[scrolledIndex];
220 dst += BB_SIZE - fb_width;
221 dispScanline += backgroundW;
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);
227 /* Blit effect to framebuffer */
228 src = backBuffer + PIXEL_PADDING;
230 for (scanline = 0; scanline < fb_height; scanline++) {
231 memcpy(dst, src, fb_width * 2);
239 /* src and dst can be the same */
240 static void convert32To16(unsigned int *src32, unsigned short *dst16, unsigned int pixelCount) {
244 *dst16++ = ((p << 8) & 0xF800) /* R */
245 | ((p >> 5) & 0x07E0) /* G */
246 | ((p >> 19) & 0x001F); /* B */
251 /* Normal map preprocessing */
252 /* Scale normal with depth and unpack R component (horizontal component) */
253 static void processNormal() {
257 short maxDisplacement = 0;
258 short minDisplacement = 256;
261 unsigned int *normalmap = (unsigned int*)background;
262 normalmap += NORMALMAP_SCANLINE * backgroundW;
263 dst = (unsigned short*)normalmap;
264 displacementMap = (short*)dst;
265 dst2 = displacementMap;
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);
280 normalmap += backgroundW;
283 if (maxDisplacement == minDisplacement) {
284 printf("Warning: grise normalmap fucked up\n");
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*/
299 static float distanceScale(int scanline) {
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);
306 static void initScrollTables() {
308 for (i = 0; i < REFLECTION_HEIGHT; i++) {
309 scrollScaleTable[i] = distanceScale(i);
310 scrollTable[i] = 0.0f;
311 scrollTableRounded[i] = 0;
316 static void updateScrollTables(float dt) {
319 nearScrollAmount += dt * NEAR_SCROLL_SPEED;
320 nearScrollAmount = (float) fmod(nearScrollAmount, 512.0f);
322 for (i = 0; i < REFLECTION_HEIGHT; i++) {
323 scrollTable[i] = nearScrollAmount / scrollScaleTable[i];
324 scrollTableRounded[i] = (int)(scrollTable[i] + 0.5f) % scrollModTable[i];
328 /* -------------------------------------------------------------------------------------------------
330 * -------------------------------------------------------------------------------------------------
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)
339 #define RLE_FIXED_BITS 16
341 static int rleByteCount(int w, int h) {
342 return h * RLE_BYTES_PER_SCANLINE + w;
345 static RLEBitmap *rleCreate(unsigned int w, unsigned int h) {
346 RLEBitmap *ret = (RLEBitmap*)malloc(sizeof(RLEBitmap));
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);
356 static void rleDestroy(RLEBitmap *b) {
362 static RLEBitmap *rleEncode(RLEBitmap *b, unsigned char *pixels, unsigned int w, unsigned int h) {
368 unsigned char *output;
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 */
374 for (scanline = 0; scanline < h; scanline++) {
375 output = b->scans + scanline * RLE_BYTES_PER_SCANLINE;
377 for (i = 0; i < w; i++) {
380 if (counter >= PIXEL_PADDING) {
381 *output++ = (unsigned char) counter;
383 *output++ = (unsigned char)accum;
388 *output++ = (unsigned char)accum;
395 *output++ = (unsigned char)counter;
407 *output++ = (unsigned char)counter;
416 static void rleDistributeStreaks(RLEBitmap *bitmap) {
417 int scanline, halfW = bitmap->w >> 1;
418 unsigned char *ptr, tmp;
421 for (scanline = 0; scanline < bitmap->h; scanline++) {
422 if (ptr[0] >= halfW) {
435 static void rleBlit(unsigned short *dst, int dstW, int dstH, int dstStride,
436 RLEBitmap *bitmap, int blitX, int blitY)
440 int streakLength = 0;
442 unsigned char *input = bitmap->scans;
443 unsigned short *output;
444 unsigned int *output32;
446 dst += blitX + blitY * dstStride;
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++;
454 if ((streakPos + blitX) <= 0) continue;
456 output = dst + streakPos;
458 /* Check if we need to write the first pixel as 16bit */
459 if (streakLength % 2) {
460 *output++ = RLE_FILL_COLOR;
463 /* Then, write 2 pixels at a time */
465 output32 = (unsigned int*) output;
466 while (streakLength--) {
467 *output32++ = RLE_FILL_COLOR_32;
475 static void interpolateScan(unsigned char *output, unsigned char *a, unsigned char *b, float t) {
476 static int div = 1 << 23;
480 ti = (*((unsigned int*)&t)) & 0x7FFFFF;
482 for (i = 0; i < RLE_BYTES_PER_SCANLINE; i++) {
491 *output++ = ((*b++ * ti) + (*a++ * (div - ti))) >> 23;
497 static void rleBlitScale(unsigned short *dst, int dstW, int dstH, int dstStride,
498 RLEBitmap *bitmap, int blitX, int blitY, float scaleX, float scaleY)
502 int streakLength = 0;
504 unsigned short *output;
505 unsigned int *output32;
506 unsigned char *input;
507 int scanlineCounter = 0;
509 static unsigned char scan[512];
511 /*int blitW = (int)(bitmap->w * scaleX + 0.5f);*/
512 int blitH = (int)(bitmap->h * scaleY + 0.5f);
514 /* From this point on, scaleY will be inverted */
515 scaleY = 1.0f / scaleY;
517 scaleXFixed = (int)(scaleX * (float)(1 << RLE_FIXED_BITS) + 0.5f);
519 dst += blitX + blitY * dstStride;
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);
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;
535 if ((streakPos + blitX) <= 0) continue;
537 output = dst + streakPos;
539 /* Check if we need to write the first pixel as 16bit */
540 if (streakLength % 2) {
541 *output++ = RLE_FILL_COLOR;
544 /* Then, write 2 pixels at a time */
546 output32 = (unsigned int*)output;
547 while (streakLength--) {
548 *output32++ = RLE_FILL_COLOR_32;
558 static void rleBlitScaleInv(unsigned short *dst, int dstW, int dstH, int dstStride,
559 RLEBitmap *bitmap, int blitX, int blitY, float scaleX, float scaleY)
563 int streakLength = 0;
565 unsigned short *output;
566 unsigned int *output32;
567 unsigned char *input;
568 int scanlineCounter = 0;
570 static unsigned char scan[512];
572 /*int blitW = (int)(bitmap->w * scaleX + 0.5f);*/
573 int blitH = (int)(bitmap->h * scaleY + 0.5f);
575 /* From this point on, scaleY will be inverted */
576 scaleY = 1.0f / scaleY;
578 scaleXFixed = (int)(scaleX * (float)(1 << RLE_FIXED_BITS) + 0.5f);
580 dst += blitX + blitY * dstStride;
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);
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;
596 if ((streakPos + blitX) <= 0) continue;
598 output = dst + streakPos;
600 /* Check if we need to write the first pixel as 16bit */
601 if (streakLength % 2) {
602 *output++ = RLE_FILL_COLOR;
605 /* Then, write 2 pixels at a time */
607 output32 = (unsigned int*)output;
608 while (streakLength--) {
609 *output32++ = RLE_FILL_COLOR_32;
617 /* -------------------------------------------------------------------------------------------------
619 * -------------------------------------------------------------------------------------------------
622 #define PROPELLER_CIRCLE_RADIUS 18
623 #define PROPELLER_CIRCLE_RADIUS_SQ (PROPELLER_CIRCLE_RADIUS * PROPELLER_CIRCLE_RADIUS)
630 static void updatePropeller(float t) {
632 int cx, cy, count = 0;
638 static float sin120 = 0.86602540378f;
639 static float cos120 = -0.5f;
644 nx = x * cost - y * sint;
645 ny = y * cost + x * sint;
648 propellerState.circleX[0] = (int)(x + 0.5f) + 16;
649 propellerState.circleY[0] = (int)(y + 0.5f) + 16;
651 /* Rotate by 120 degrees, for the second circle */
652 nx = x * cos120 - y * sin120;
653 ny = y * cos120 + x * sin120;
656 propellerState.circleX[1] = (int)(x + 0.5f) + 16;
657 propellerState.circleY[1] = (int)(y + 0.5f) + 16;
660 nx = x * cos120 - y * sin120;
661 ny = y * cos120 + x * sin120;
664 propellerState.circleX[2] = (int)(x + 0.5f) + 16;
665 propellerState.circleY[2] = (int)(y + 0.5f) + 16;
667 /* Write effect to the mini fx buffer*/
669 for (j = 0; j < 32; j++) {
670 for (i = 0; i < 32; i++) {
674 cx = propellerState.circleX[0] - i;
675 cy = propellerState.circleY[0] - j;
676 if (cx*cx + cy*cy < PROPELLER_CIRCLE_RADIUS_SQ) count++;
679 cx = propellerState.circleX[1] - i;
680 cy = propellerState.circleY[1] - j;
681 if (cx*cx + cy*cy < PROPELLER_CIRCLE_RADIUS_SQ) count++;
684 cx = propellerState.circleX[2] - i;
685 cy = propellerState.circleY[2] - j;
686 if (cx*cx + cy*cy < PROPELLER_CIRCLE_RADIUS_SQ) count++;
692 /* Then, encode to rle */
693 rlePropeller = rleEncode(rlePropeller, miniFXBuffer, 32, 32);
695 /* Distribute the produced streaks so that they don't produce garbage when interpolated */
696 rleDistributeStreaks(rlePropeller);