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;
150 static void draw(void)
152 int scroll = MIN_SCROLL + (MAX_SCROLL - MIN_SCROLL) * mouse_x / fb_width;
153 unsigned short *dst = backBuffer + PIXEL_PADDING;
154 unsigned short *src = background + scroll;
163 lastFrameDuration = (time_msec - lastFrameTime) / 1000.0f;
164 lastFrameTime = time_msec;
166 /* Update mini-effects here */
167 updatePropeller(4.0f * time_msec / 1000.0f);
169 /* First, render the horizon */
170 for (scanline = 0; scanline < HORIZON_HEIGHT; scanline++) {
171 memcpy(dst, src, fb_width * 2);
176 /* Create scroll offsets for all scanlines of the normalmap */
177 updateScrollTables(lastFrameDuration);
179 /* Render the baked reflection one scanline below its place, so that
180 * the displacement that follows will be done in a cache-friendly way
182 src -= PIXEL_PADDING; /* We want to also fill the PADDING pixels here */
183 dst = backBuffer + (HORIZON_HEIGHT + 1) * BB_SIZE;
184 for (scanline = 0; scanline < REFLECTION_HEIGHT; scanline++) {
185 memcpy(dst, src, (fb_width + PIXEL_PADDING) * 2);
190 /* Blit reflections first, to be displaced */
191 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);
193 /* Perform displacement */
194 dst = backBuffer + HORIZON_HEIGHT * BB_SIZE + PIXEL_PADDING;
195 src = dst + BB_SIZE; /* The pixels to be displaced are 1 scanline below */
196 dispScanline = displacementMap;
197 for (scanline = 0; scanline < REFLECTION_HEIGHT; scanline++) {
199 md = scrollModTable[scanline];
200 sc = scrollTableRounded[scanline];
203 for (i = 0; i < fb_width; i++) {
204 /* Try to immitate modulo without the division */
205 if (i == md) accum += md;
206 scrolledIndex = i - accum + sc;
207 if (scrolledIndex >= md) scrolledIndex -= md;
210 d = dispScanline[scrolledIndex];
214 dst += BB_SIZE - fb_width;
215 dispScanline += backgroundW;
218 /* Then after displacement, blit the objects */
219 for (i = 0; i < 5; i++) rleBlit(backBuffer + PIXEL_PADDING, fb_width, fb_height, BB_SIZE, rlePropeller, 134 + (i-3) * 60, 100);
221 /* Blit effect to framebuffer */
222 src = backBuffer + PIXEL_PADDING;
224 for (scanline = 0; scanline < fb_height; scanline++) {
225 memcpy(dst, src, fb_width * 2);
233 /* src and dst can be the same */
234 static void convert32To16(unsigned int *src32, unsigned short *dst16, unsigned int pixelCount) {
238 *dst16++ = ((p << 8) & 0xF800) /* R */
239 | ((p >> 5) & 0x07E0) /* G */
240 | ((p >> 19) & 0x001F); /* B */
245 /* Normal map preprocessing */
246 /* Scale normal with depth and unpack R component (horizontal component) */
247 static void processNormal() {
251 short maxDisplacement = 0;
252 short minDisplacement = 256;
255 unsigned int *normalmap = (unsigned int*)background;
256 normalmap += NORMALMAP_SCANLINE * backgroundW;
257 dst = (unsigned short*)normalmap;
258 displacementMap = (short*)dst;
259 dst2 = displacementMap;
261 for (scanline = 0; scanline < REFLECTION_HEIGHT; scanline++) {
262 scrollModTable[scanline] = (int) (backgroundW / scrollScaleTable[scanline] + 0.5f);
263 for (i = 0; i < backgroundW; i++) {
264 x = (int)(i * scrollScaleTable[scanline] + 0.5f);
265 if (x < backgroundW) {
266 *dst = (unsigned short)(normalmap[x] >> 8) & 0xFF;
267 if ((short)*dst > maxDisplacement) maxDisplacement = (short)(*dst);
268 if ((short)*dst < minDisplacement) minDisplacement = (short)(*dst);
274 normalmap += backgroundW;
277 if (maxDisplacement == minDisplacement) {
278 printf("Warning: grise normalmap fucked up\n");
282 /* Second pass - subtract half maximum displacement to displace in both directions */
283 for (scanline = 0; scanline < REFLECTION_HEIGHT; scanline++) {
284 for (i = 0; i < backgroundW; i++) {
285 /* Remember that MIN_SCROLL is the padding around the screen, so ti's the maximum displacement we can get (positive & negative) */
286 *dst2 = 2 * MAX_DISPLACEMENT * (*dst2 - minDisplacement) / (maxDisplacement - minDisplacement) - MAX_DISPLACEMENT;
287 *dst2 = (short)((float)*dst2 / scrollScaleTable[scanline] + 0.5f); /* Displacements must also scale with distance*/
293 static float distanceScale(int scanline) {
295 farScale = (float)NEAR_SCROLL_SPEED / (float)FAR_SCROLL_SPEED;
296 t = (float)scanline / ((float)REFLECTION_HEIGHT - 1);
297 return 1.0f / (1.0f / farScale + (1.0f - 1.0f / farScale) * t);
300 static void initScrollTables() {
302 for (i = 0; i < REFLECTION_HEIGHT; i++) {
303 scrollScaleTable[i] = distanceScale(i);
304 scrollTable[i] = 0.0f;
305 scrollTableRounded[i] = 0;
310 static void updateScrollTables(float dt) {
313 nearScrollAmount += dt * NEAR_SCROLL_SPEED;
314 nearScrollAmount = (float) fmod(nearScrollAmount, 512.0f);
316 for (i = 0; i < REFLECTION_HEIGHT; i++) {
317 scrollTable[i] = nearScrollAmount / scrollScaleTable[i];
318 scrollTableRounded[i] = (int)(scrollTable[i] + 0.5f) % scrollModTable[i];
322 /* -------------------------------------------------------------------------------------------------
324 * -------------------------------------------------------------------------------------------------
326 /* Limit streak count per scanline so we can directly jump to specific scanline */
327 #define RLE_STREAKS_PER_SCANLINE 4
328 /* Every streak is encoded by 2 bytes: offset and count of black pixels in the streak */
329 #define RLE_BYTES_PER_SCANLINE RLE_STREAKS_PER_SCANLINE * 2
330 #define RLE_FILL_COLOR 0
331 #define RLE_FILL_COLOR_32 ((RLE_FILL_COLOR << 16) | RLE_FILL_COLOR)
333 #define RLE_FIXED_BITS 16
335 static int rleByteCount(int w, int h) {
336 return h * RLE_BYTES_PER_SCANLINE + w;
339 static RLEBitmap *rleCreate(unsigned int w, unsigned int h) {
340 RLEBitmap *ret = (RLEBitmap*)malloc(sizeof(RLEBitmap));
344 /* Add some padding at the end of the buffer, with the worst case for a scanline (w/2 streaks) */
345 ret->scans = (unsigned char*) calloc(rleByteCount(w, h), 1);
350 static void rleDestroy(RLEBitmap *b) {
356 static RLEBitmap *rleEncode(RLEBitmap *b, unsigned char *pixels, unsigned int w, unsigned int h) {
362 unsigned char *output;
364 /* https://www.youtube.com/watch?v=RKMR02o1I88&feature=youtu.be&t=55 */
365 if (!b) b = rleCreate(w, h);
366 else memset(b->scans, 0, rleByteCount(b->w, b->h)); /* The following code assumes cleared array */
368 for (scanline = 0; scanline < h; scanline++) {
369 output = b->scans + scanline * RLE_BYTES_PER_SCANLINE;
371 for (i = 0; i < w; i++) {
374 if (counter >= PIXEL_PADDING) {
375 *output++ = (unsigned char) counter;
377 *output++ = (unsigned char)accum;
382 *output++ = (unsigned char)accum;
389 *output++ = (unsigned char)counter;
401 *output++ = (unsigned char)counter;
410 static void rleDistributeStreaks(RLEBitmap *bitmap) {
411 int scanline, halfW = bitmap->w >> 1;
412 unsigned char *ptr, tmp;
415 for (scanline = 0; scanline < bitmap->h; scanline++) {
416 if (ptr[0] >= halfW) {
429 static void rleBlit(unsigned short *dst, int dstW, int dstH, int dstStride,
430 RLEBitmap *bitmap, int blitX, int blitY)
434 int streakLength = 0;
436 unsigned char *input = bitmap->scans;
437 unsigned short *output;
438 unsigned int *output32;
440 dst += blitX + blitY * dstStride;
442 for (scanline = blitY; scanline < blitY + bitmap->h; scanline++) {
443 if (scanline < 0 || scanline >= dstH) continue;
444 for (streak = 0; streak < RLE_STREAKS_PER_SCANLINE; streak++) {
445 streakPos = *input++;
446 streakLength = *input++;
448 if ((streakPos + blitX) <= 0) continue;
450 output = dst + streakPos;
452 /* Check if we need to write the first pixel as 16bit */
453 if (streakLength % 2) {
454 *output++ = RLE_FILL_COLOR;
457 /* Then, write 2 pixels at a time */
459 output32 = (unsigned int*) output;
460 while (streakLength--) {
461 *output32++ = RLE_FILL_COLOR_32;
469 static void interpolateScan(unsigned char *output, unsigned char *a, unsigned char *b, float t) {
470 static int div = 1 << 23;
474 ti = (*((unsigned int*)&t)) & 0x7FFFFF;
476 for (i = 0; i < RLE_BYTES_PER_SCANLINE; i++) {
485 *output++ = ((*b++ * ti) + (*a++ * (div - ti))) >> 23;
491 static void rleBlitScale(unsigned short *dst, int dstW, int dstH, int dstStride,
492 RLEBitmap *bitmap, int blitX, int blitY, float scaleX, float scaleY)
496 int streakLength = 0;
498 unsigned short *output;
499 unsigned int *output32;
500 unsigned char *input;
501 int scanlineCounter = 0;
503 static unsigned char scan[512];
505 /*int blitW = (int)(bitmap->w * scaleX + 0.5f);*/
506 int blitH = (int)(bitmap->h * scaleY + 0.5f);
508 /* From this point on, scaleY will be inverted */
509 scaleY = 1.0f / scaleY;
511 scaleXFixed = (int)(scaleX * (float)(1 << RLE_FIXED_BITS) + 0.5f);
513 dst += blitX + blitY * dstStride;
515 for (scanline = blitY; scanline < blitY + blitH; scanline++) {
516 float normalScan = scanlineCounter * scaleY; /* ScaleY is inverted */
517 unsigned char *scan0 = bitmap->scans + RLE_BYTES_PER_SCANLINE * (int)normalScan;
518 unsigned char *scan1 = scan0 + RLE_BYTES_PER_SCANLINE;
519 normalScan -= (int)normalScan;
520 interpolateScan(scan, scan0, scan1, normalScan);
524 if (scanline < 0 || scanline >= dstH) continue;
525 for (streak = 0; streak < RLE_STREAKS_PER_SCANLINE; streak++) {
526 streakPos = (*input++ * scaleXFixed) >> RLE_FIXED_BITS;
527 streakLength = (*input++ * scaleXFixed) >> RLE_FIXED_BITS;
529 if ((streakPos + blitX) <= 0) continue;
531 output = dst + streakPos;
533 /* Check if we need to write the first pixel as 16bit */
534 if (streakLength % 2) {
535 *output++ = RLE_FILL_COLOR;
538 /* Then, write 2 pixels at a time */
540 output32 = (unsigned int*)output;
541 while (streakLength--) {
542 *output32++ = RLE_FILL_COLOR_32;
552 static void rleBlitScaleInv(unsigned short *dst, int dstW, int dstH, int dstStride,
553 RLEBitmap *bitmap, int blitX, int blitY, float scaleX, float scaleY)
557 int streakLength = 0;
559 unsigned short *output;
560 unsigned int *output32;
561 unsigned char *input;
562 int scanlineCounter = 0;
564 static unsigned char scan[512];
566 /*int blitW = (int)(bitmap->w * scaleX + 0.5f);*/
567 int blitH = (int)(bitmap->h * scaleY + 0.5f);
569 /* From this point on, scaleY will be inverted */
570 scaleY = 1.0f / scaleY;
572 scaleXFixed = (int)(scaleX * (float)(1 << RLE_FIXED_BITS) + 0.5f);
574 dst += blitX + blitY * dstStride;
576 for (scanline = blitY; scanline > blitY - blitH; scanline--) {
577 float normalScan = scanlineCounter * scaleY; /* ScaleY is inverted */
578 unsigned char *scan0 = bitmap->scans + RLE_BYTES_PER_SCANLINE * (int)normalScan;
579 unsigned char *scan1 = scan0 + RLE_BYTES_PER_SCANLINE;
580 normalScan -= (int)normalScan;
581 interpolateScan(scan, scan0, scan1, normalScan);
585 if (scanline < 0 || scanline >= dstH) continue;
586 for (streak = 0; streak < RLE_STREAKS_PER_SCANLINE; streak++) {
587 streakPos = (*input++ * scaleXFixed) >> RLE_FIXED_BITS;
588 streakLength = (*input++ * scaleXFixed) >> RLE_FIXED_BITS;
590 if ((streakPos + blitX) <= 0) continue;
592 output = dst + streakPos;
594 /* Check if we need to write the first pixel as 16bit */
595 if (streakLength % 2) {
596 *output++ = RLE_FILL_COLOR;
599 /* Then, write 2 pixels at a time */
601 output32 = (unsigned int*)output;
602 while (streakLength--) {
603 *output32++ = RLE_FILL_COLOR_32;
611 /* -------------------------------------------------------------------------------------------------
613 * -------------------------------------------------------------------------------------------------
616 #define PROPELLER_CIRCLE_RADIUS 18
617 #define PROPELLER_CIRCLE_RADIUS_SQ (PROPELLER_CIRCLE_RADIUS * PROPELLER_CIRCLE_RADIUS)
624 static void updatePropeller(float t) {
626 int cx, cy, count = 0;
632 static float sin120 = 0.86602540378f;
633 static float cos120 = -0.5f;
638 nx = x * cost - y * sint;
639 ny = y * cost + x * sint;
642 propellerState.circleX[0] = (int)(x + 0.5f) + 16;
643 propellerState.circleY[0] = (int)(y + 0.5f) + 16;
645 /* Rotate by 120 degrees, for the second circle */
646 nx = x * cos120 - y * sin120;
647 ny = y * cos120 + x * sin120;
650 propellerState.circleX[1] = (int)(x + 0.5f) + 16;
651 propellerState.circleY[1] = (int)(y + 0.5f) + 16;
654 nx = x * cos120 - y * sin120;
655 ny = y * cos120 + x * sin120;
658 propellerState.circleX[2] = (int)(x + 0.5f) + 16;
659 propellerState.circleY[2] = (int)(y + 0.5f) + 16;
661 /* Write effect to the mini fx buffer*/
663 for (j = 0; j < 32; j++) {
664 for (i = 0; i < 32; i++) {
668 cx = propellerState.circleX[0] - i;
669 cy = propellerState.circleY[0] - j;
670 if (cx*cx + cy*cy < PROPELLER_CIRCLE_RADIUS_SQ) count++;
673 cx = propellerState.circleX[1] - i;
674 cy = propellerState.circleY[1] - j;
675 if (cx*cx + cy*cy < PROPELLER_CIRCLE_RADIUS_SQ) count++;
678 cx = propellerState.circleX[2] - i;
679 cy = propellerState.circleY[2] - j;
680 if (cx*cx + cy*cy < PROPELLER_CIRCLE_RADIUS_SQ) count++;
686 /* Then, encode to rle */
687 rlePropeller = rleEncode(rlePropeller, miniFXBuffer, 32, 32);
689 /* Distribute the produced streaks so that they don't produce garbage when interpolated */
690 rleDistributeStreaks(rlePropeller);