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;
85 static struct screen scr = {
94 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);
138 static void destroy(void)
143 img_free_pixels(background);
148 static void start(long trans_time)
150 lastFrameTime = time_msec;
153 static void stop(long trans_time)
160 static void draw(void)
162 int scroll = MIN_SCROLL + (MAX_SCROLL - MIN_SCROLL) * mouse_x / fb_width;
163 unsigned short *dst = backBuffer + PIXEL_PADDING;
164 unsigned short *src = background + scroll;
170 lastFrameDuration = (time_msec - lastFrameTime) / 1000.0f;
171 lastFrameTime = time_msec;
173 /* Update mini-effects here */
174 updatePropeller(time_msec / 1000.0f);
176 /* First, render the horizon */
177 for (scanline = 0; scanline < HORIZON_HEIGHT; scanline++) {
178 memcpy(dst, src, fb_width * 2);
183 /* Create scroll offsets for all scanlines of the normalmap */
184 updateScrollTables(lastFrameDuration);
186 /* Render the baked reflection one scanline below its place, so that
187 * the displacement that follows will be done in a cache-friendly way
189 src -= PIXEL_PADDING; /* We want to also fill the PADDING pixels here */
190 dst = backBuffer + (HORIZON_HEIGHT + 1) * BB_SIZE;
191 for (scanline = 0; scanline < REFLECTION_HEIGHT; scanline++) {
192 memcpy(dst, src, (fb_width + PIXEL_PADDING) * 2);
197 /* Blit reflections first, to be displaced */
198 for (i = 0; i < 5; i++) rleBlitScaleInv(backBuffer + PIXEL_PADDING, fb_width, fb_height, BB_SIZE, grobj, 134 + (i-3) * 60, 235, 1.0f, 1.8f);
200 /* Perform displacement */
201 dst = backBuffer + HORIZON_HEIGHT * BB_SIZE + PIXEL_PADDING;
202 src = dst + BB_SIZE; /* The pixels to be displaced are 1 scanline below */
203 dispScanline = displacementMap;
204 for (scanline = 0; scanline < REFLECTION_HEIGHT; scanline++) {
205 for (i = 0; i < fb_width; i++) {
206 d = dispScanline[(i + scrollTableRounded[scanline]) % scrollModTable[scanline]];
210 dst += BB_SIZE - fb_width;
211 dispScanline += backgroundW;
214 /* Then after displacement, blit the objects */
215 for (i = 0; i < 5; i++) rleBlit(backBuffer + PIXEL_PADDING, fb_width, fb_height, BB_SIZE, grobj, 134 + (i-3) * 60, 100);
217 for (scanline = 0; scanline < 32; scanline++) {
218 for (i = 0; i < 32; i++) {
219 backBuffer[PIXEL_PADDING + scanline * BB_SIZE + i] = miniFXBuffer[i + 32 * scanline] ? 0xFFFF : 0x0000;
223 /* Blit effect to framebuffer */
224 src = backBuffer + PIXEL_PADDING;
226 for (scanline = 0; scanline < fb_height; scanline++) {
227 memcpy(dst, src, fb_width * 2);
235 /* src and dst can be the same */
236 static void convert32To16(unsigned int *src32, unsigned short *dst16, unsigned int pixelCount) {
240 *dst16++ = ((p << 8) & 0xF800) /* R */
241 | ((p >> 5) & 0x07E0) /* G */
242 | ((p >> 19) & 0x001F); /* B */
247 /* Normal map preprocessing */
248 /* Scale normal with depth and unpack R component (horizontal component) */
249 static void processNormal() {
253 short maxDisplacement = 0;
254 short minDisplacement = 256;
257 unsigned int *normalmap = (unsigned int*)background;
258 normalmap += NORMALMAP_SCANLINE * backgroundW;
259 dst = (unsigned short*)normalmap;
260 displacementMap = (short*)dst;
261 dst2 = displacementMap;
263 for (scanline = 0; scanline < REFLECTION_HEIGHT; scanline++) {
264 scrollModTable[scanline] = (int) (backgroundW / scrollScaleTable[scanline] + 0.5f);
265 for (i = 0; i < backgroundW; i++) {
266 x = (int)(i * scrollScaleTable[scanline] + 0.5f);
267 if (x < backgroundW) {
268 *dst = (unsigned short)(normalmap[x] >> 8) & 0xFF;
269 if ((short)*dst > maxDisplacement) maxDisplacement = (short)(*dst);
270 if ((short)*dst < minDisplacement) minDisplacement = (short)(*dst);
276 normalmap += backgroundW;
279 if (maxDisplacement == minDisplacement) {
280 printf("Warning: grise normalmap fucked up\n");
284 /* Second pass - subtract half maximum displacement to displace in both directions */
285 for (scanline = 0; scanline < REFLECTION_HEIGHT; scanline++) {
286 for (i = 0; i < backgroundW; i++) {
287 /* Remember that MIN_SCROLL is the padding around the screen, so ti's the maximum displacement we can get (positive & negative) */
288 *dst2 = 2 * MAX_DISPLACEMENT * (*dst2 - minDisplacement) / (maxDisplacement - minDisplacement) - MAX_DISPLACEMENT;
289 *dst2 = (short)((float)*dst2 / scrollScaleTable[scanline] + 0.5f); /* Displacements must also scale with distance*/
295 static float distanceScale(int scanline) {
297 farScale = (float)NEAR_SCROLL_SPEED / (float)FAR_SCROLL_SPEED;
298 t = (float)scanline / ((float)REFLECTION_HEIGHT - 1);
299 return 1.0f / (1.0f / farScale + (1.0f - 1.0f / farScale) * t);
302 static void initScrollTables() {
304 for (i = 0; i < REFLECTION_HEIGHT; i++) {
305 scrollScaleTable[i] = distanceScale(i);
306 scrollTable[i] = 0.0f;
307 scrollTableRounded[i] = 0;
312 static void updateScrollTables(float dt) {
315 nearScrollAmount += dt * NEAR_SCROLL_SPEED;
316 nearScrollAmount = (float) fmod(nearScrollAmount, 512.0f);
318 for (i = 0; i < REFLECTION_HEIGHT; i++) {
319 scrollTable[i] = nearScrollAmount / scrollScaleTable[i];
320 scrollTableRounded[i] = (int)(scrollTable[i] + 0.5f) % scrollModTable[i];
324 /* -------------------------------------------------------------------------------------------------
326 * -------------------------------------------------------------------------------------------------
328 /* Limit streak count per scanline so we can directly jump to specific scanline */
329 #define RLE_STREAKS_PER_SCANLINE 4
330 /* Every streak is encoded by 2 bytes: offset and count of black pixels in the streak */
331 #define RLE_BYTES_PER_SCANLINE RLE_STREAKS_PER_SCANLINE * 2
332 #define RLE_FILL_COLOR 0
333 #define RLE_FILL_COLOR_32 ((RLE_FILL_COLOR << 16) | RLE_FILL_COLOR)
335 #define RLE_FIXED_BITS 16
337 static RLEBitmap *rleCreate(unsigned int w, unsigned int h) {
338 RLEBitmap *ret = (RLEBitmap*)malloc(sizeof(RLEBitmap));
342 /* Add some padding at the end of the buffer, with the worst case for a scanline (w/2 streaks) */
343 ret->scans = (unsigned char*) calloc(h * RLE_BYTES_PER_SCANLINE + w, 1);
348 static void rleDestroy(RLEBitmap *b) {
354 static RLEBitmap *rleEncode(RLEBitmap *b, unsigned char *pixels, unsigned int w, unsigned int h) {
360 unsigned char *output;
362 /* https://www.youtube.com/watch?v=RKMR02o1I88&feature=youtu.be&t=55 */
363 if (!b) b = rleCreate(w, h);
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 rleBlit(unsigned short *dst, int dstW, int dstH, int dstStride,
408 RLEBitmap *bitmap, int blitX, int blitY)
412 int streakLength = 0;
414 unsigned char *input = bitmap->scans;
415 unsigned short *output;
416 unsigned int *output32;
418 dst += blitX + blitY * dstStride;
420 for (scanline = blitY; scanline < blitY + bitmap->h; scanline++) {
421 if (scanline < 0 || scanline >= dstH) continue;
422 for (streak = 0; streak < RLE_STREAKS_PER_SCANLINE; streak++) {
423 streakPos = *input++;
424 streakLength = *input++;
426 if ((streakPos + blitX) <= 0) continue;
428 output = dst + streakPos;
430 /* Check if we need to write the first pixel as 16bit */
431 if (streakLength % 2) {
432 *output++ = RLE_FILL_COLOR;
435 /* Then, write 2 pixels at a time */
437 output32 = (unsigned int*) output;
438 while (streakLength--) {
439 *output32++ = RLE_FILL_COLOR_32;
447 static void interpolateScan(unsigned char *output, unsigned char *a, unsigned char *b, float t) {
448 static int div = 1 << 23;
452 ti = (*((unsigned int*)&t)) & 0x7FFFFF;
454 for (i = 0; i < RLE_BYTES_PER_SCANLINE; i++) {
463 *output++ = ((*b++ * ti) + (*a++ * (div - ti))) >> 23;
469 static void rleBlitScale(unsigned short *dst, int dstW, int dstH, int dstStride,
470 RLEBitmap *bitmap, int blitX, int blitY, float scaleX, float scaleY)
474 int streakLength = 0;
476 unsigned short *output;
477 unsigned int *output32;
478 unsigned char *input;
479 int scanlineCounter = 0;
480 static unsigned char scan[512];
482 int blitW = (int)(bitmap->w * scaleX + 0.5f);
483 int blitH = (int)(bitmap->h * scaleY + 0.5f);
485 /* From this point on, scaleY will be inverted */
486 scaleY = 1.0f / scaleY;
488 int scaleXFixed = (int)(scaleX * (float)(1 << RLE_FIXED_BITS) + 0.5f);
490 dst += blitX + blitY * dstStride;
492 for (scanline = blitY; scanline < blitY + blitH; scanline++) {
493 float normalScan = scanlineCounter * scaleY; /* ScaleY is inverted */
494 unsigned char *scan0 = bitmap->scans + RLE_BYTES_PER_SCANLINE * (int)normalScan;
495 unsigned char *scan1 = scan0 + RLE_BYTES_PER_SCANLINE;
496 normalScan -= (int)normalScan;
497 interpolateScan(scan, scan0, scan1, normalScan);
501 if (scanline < 0 || scanline >= dstH) continue;
502 for (streak = 0; streak < RLE_STREAKS_PER_SCANLINE; streak++) {
503 streakPos = (*input++ * scaleXFixed) >> RLE_FIXED_BITS;
504 streakLength = (*input++ * scaleXFixed) >> RLE_FIXED_BITS;
506 if ((streakPos + blitX) <= 0) continue;
508 output = dst + streakPos;
510 /* Check if we need to write the first pixel as 16bit */
511 if (streakLength % 2) {
512 *output++ = RLE_FILL_COLOR;
515 /* Then, write 2 pixels at a time */
517 output32 = (unsigned int*)output;
518 while (streakLength--) {
519 *output32++ = RLE_FILL_COLOR_32;
529 static void rleBlitScaleInv(unsigned short *dst, int dstW, int dstH, int dstStride,
530 RLEBitmap *bitmap, int blitX, int blitY, float scaleX, float scaleY)
534 int streakLength = 0;
536 unsigned short *output;
537 unsigned int *output32;
538 unsigned char *input;
539 int scanlineCounter = 0;
540 static unsigned char scan[512];
542 int blitW = (int)(bitmap->w * scaleX + 0.5f);
543 int blitH = (int)(bitmap->h * scaleY + 0.5f);
545 /* From this point on, scaleY will be inverted */
546 scaleY = 1.0f / scaleY;
548 int scaleXFixed = (int)(scaleX * (float)(1 << RLE_FIXED_BITS) + 0.5f);
550 dst += blitX + blitY * dstStride;
552 for (scanline = blitY; scanline > blitY - blitH; scanline--) {
553 float normalScan = scanlineCounter * scaleY; /* ScaleY is inverted */
554 unsigned char *scan0 = bitmap->scans + RLE_BYTES_PER_SCANLINE * (int)normalScan;
555 unsigned char *scan1 = scan0 + RLE_BYTES_PER_SCANLINE;
556 normalScan -= (int)normalScan;
557 interpolateScan(scan, scan0, scan1, normalScan);
561 if (scanline < 0 || scanline >= dstH) continue;
562 for (streak = 0; streak < RLE_STREAKS_PER_SCANLINE; streak++) {
563 streakPos = (*input++ * scaleXFixed) >> RLE_FIXED_BITS;
564 streakLength = (*input++ * scaleXFixed) >> RLE_FIXED_BITS;
566 if ((streakPos + blitX) <= 0) continue;
568 output = dst + streakPos;
570 /* Check if we need to write the first pixel as 16bit */
571 if (streakLength % 2) {
572 *output++ = RLE_FILL_COLOR;
575 /* Then, write 2 pixels at a time */
577 output32 = (unsigned int*)output;
578 while (streakLength--) {
579 *output32++ = RLE_FILL_COLOR_32;
587 /* -------------------------------------------------------------------------------------------------
589 * -------------------------------------------------------------------------------------------------
592 #define PROPELLER_CIRCLE_RADIUS 18
593 #define PROPELLER_CIRCLE_RADIUS_SQ (PROPELLER_CIRCLE_RADIUS * PROPELLER_CIRCLE_RADIUS)
600 static void updatePropeller(float t) {
602 int cx, cy, count = 0;
608 static float sin120 = 0.86602540378f;
609 static float cos120 = -0.5f;
614 nx = x * cost - y * sint;
615 ny = y * cost + x * sint;
618 propellerState.circleX[0] = (int)(x + 0.5f) + 16;
619 propellerState.circleY[0] = (int)(y + 0.5f) + 16;
621 /* Rotate by 120 degrees, for the second circle */
622 nx = x * cos120 - y * sin120;
623 ny = y * cos120 + x * sin120;
626 propellerState.circleX[1] = (int)(x + 0.5f) + 16;
627 propellerState.circleY[1] = (int)(y + 0.5f) + 16;
630 nx = x * cos120 - y * sin120;
631 ny = y * cos120 + x * sin120;
634 propellerState.circleX[2] = (int)(x + 0.5f) + 16;
635 propellerState.circleY[2] = (int)(y + 0.5f) + 16;
637 /* Write effect to the mini fx buffer*/
639 for (j = 0; j < 32; j++) {
640 for (i = 0; i < 32; i++) {
644 cx = propellerState.circleX[0] - i;
645 cy = propellerState.circleY[0] - j;
646 if (cx*cx + cy*cy < PROPELLER_CIRCLE_RADIUS_SQ) count++;
649 cx = propellerState.circleX[1] - i;
650 cy = propellerState.circleY[1] - j;
651 if (cx*cx + cy*cy < PROPELLER_CIRCLE_RADIUS_SQ) count++;
654 cx = propellerState.circleX[2] - i;
655 cy = propellerState.circleY[2] - j;
656 if (cx*cx + cy*cy < PROPELLER_CIRCLE_RADIUS_SQ) count++;