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(unsigned char *pixels, unsigned int w, unsigned int h);
27 #define BG_FILENAME "data/grise.png"
28 #define GROBJ_01_FILENAME "data/grobj_01.png"
30 #define BB_SIZE 512 /* Let's use a power of 2. Maybe we'll zoom/rotate the effect */
32 /* Every backBuffer scanline is guaranteed to have that many dummy pixels before and after */
33 #define PIXEL_PADDING 32
35 /* Make sure this is less than PIXEL_PADDING*/
36 #define MAX_DISPLACEMENT 16
38 #define MIN_SCROLL PIXEL_PADDING
39 #define MAX_SCROLL (backgroundW - fb_width - MIN_SCROLL)
41 #define FAR_SCROLL_SPEED 15.0f
42 #define NEAR_SCROLL_SPEED 120.0f
44 #define HORIZON_HEIGHT 100
45 #define REFLECTION_HEIGHT (240 - HORIZON_HEIGHT)
47 #define NORMALMAP_SCANLINE 372
49 static int init(void);
50 static void destroy(void);
51 static void start(long trans_time);
52 static void stop(long trans_time);
53 static void draw(void);
55 static void convert32To16(unsigned int *src32, unsigned short *dst16, unsigned int pixelCount);
56 static void processNormal();
57 static void initScrollTables();
58 static void updateScrollTables(float dt);
62 static unsigned short *background = 0;
63 static unsigned int backgroundW = 0;
64 static unsigned int backgroundH = 0;
66 static unsigned int lastFrameTime = 0;
67 static float lastFrameDuration = 0.0f;
69 static short *displacementMap;
71 static unsigned short *backBuffer;
73 static float scrollScaleTable[REFLECTION_HEIGHT];
74 static float scrollTable[REFLECTION_HEIGHT];
75 static int scrollTableRounded[REFLECTION_HEIGHT];
76 static int scrollModTable[REFLECTION_HEIGHT];
77 static float nearScrollAmount = 0.0f;
79 static RLEBitmap grobj;
81 static struct screen scr = {
90 struct screen *grise_screen(void)
98 unsigned char *tmpBitmap;
99 int tmpBitmapW, tmpBitmapH;
101 /* Allocate back buffer */
102 backBuffer = (unsigned short*) malloc(BB_SIZE * BB_SIZE * sizeof(unsigned short));
104 /* grise.png contains the background (horizon), baked reflection and normalmap for displacement */
105 if (!(background = img_load_pixels(BG_FILENAME, &backgroundW, &backgroundH, IMG_FMT_RGBA32))) {
106 fprintf(stderr, "failed to load image " BG_FILENAME "\n");
110 /* Convert to 16bpp */
111 convert32To16((unsigned int*)background, background, backgroundW * NORMALMAP_SCANLINE); /* Normalmap will keep its 32 bit color */
113 /* Load reflected objects */
114 if (!(tmpBitmap = img_load_pixels(GROBJ_01_FILENAME, &tmpBitmapW, &tmpBitmapH, IMG_FMT_GREY8))) {
115 fprintf(stderr, "failed to load image " GROBJ_01_FILENAME "\n");
119 grobj = rleEncode(tmpBitmap, tmpBitmapW, tmpBitmapH);
121 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)
153 static void draw(void)
155 int scroll = MIN_SCROLL + (MAX_SCROLL - MIN_SCROLL) * mouse_x / fb_width;
156 unsigned short *dst = backBuffer + PIXEL_PADDING;
157 unsigned short *src = background + scroll;
163 lastFrameDuration = (time_msec - lastFrameTime) / 1000.0f;
164 lastFrameTime = time_msec;
166 /* First, render the horizon */
167 for (scanline = 0; scanline < HORIZON_HEIGHT; scanline++) {
168 memcpy(dst, src, fb_width * 2);
173 /* Create scroll offsets for all scanlines of the normalmap */
174 updateScrollTables(lastFrameDuration);
176 /* Render the baked reflection one scanline below its place, so that
177 * the displacement that follows will be done in a cache-friendly way
179 src -= PIXEL_PADDING; /* We want to also fill the PADDING pixels here */
180 dst = backBuffer + (HORIZON_HEIGHT + 1) * BB_SIZE;
181 for (scanline = 0; scanline < REFLECTION_HEIGHT; scanline++) {
182 memcpy(dst, src, (fb_width + PIXEL_PADDING) * 2);
187 /* Blit reflections first, to be displaced */
188 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);
190 /* Perform displacement */
191 dst = backBuffer + HORIZON_HEIGHT * BB_SIZE + PIXEL_PADDING;
192 src = dst + BB_SIZE; /* The pixels to be displaced are 1 scanline below */
193 dispScanline = displacementMap;
194 for (scanline = 0; scanline < REFLECTION_HEIGHT; scanline++) {
195 for (i = 0; i < fb_width; i++) {
196 d = dispScanline[(i + scrollTableRounded[scanline]) % scrollModTable[scanline]];
200 dst += BB_SIZE - fb_width;
201 dispScanline += backgroundW;
204 /* Then after displacement, blit the objects */
205 for (i = 0; i < 5; i++) rleBlit(backBuffer + PIXEL_PADDING, fb_width, fb_height, BB_SIZE, grobj, 134 + (i-3) * 60, 100);
207 /* Blit effect to framebuffer */
208 src = backBuffer + PIXEL_PADDING;
210 for (scanline = 0; scanline < fb_height; scanline++) {
211 memcpy(dst, src, fb_width * 2);
216 swap_buffers(fb_pixels);
219 /* src and dst can be the same */
220 static void convert32To16(unsigned int *src32, unsigned short *dst16, unsigned int pixelCount) {
224 *dst16++ = ((p << 8) & 0xF800) /* R */
225 | ((p >> 5) & 0x07E0) /* G */
226 | ((p >> 19) & 0x001F); /* B */
231 /* Normal map preprocessing */
232 /* Scale normal with depth and unpack R component (horizontal component) */
233 static void processNormal() {
237 short maxDisplacement = 0;
238 short minDisplacement = 256;
241 unsigned int *normalmap = (unsigned int*)background;
242 normalmap += NORMALMAP_SCANLINE * backgroundW;
243 dst = (unsigned short*)normalmap;
244 displacementMap = (short*)dst;
245 dst2 = displacementMap;
247 for (scanline = 0; scanline < REFLECTION_HEIGHT; scanline++) {
248 scrollModTable[scanline] = (int) (backgroundW / scrollScaleTable[scanline] + 0.5f);
249 for (i = 0; i < backgroundW; i++) {
250 x = (int)(i * scrollScaleTable[scanline] + 0.5f);
251 if (x < backgroundW) {
252 *dst = (unsigned short)(normalmap[x] >> 8) & 0xFF;
253 if ((short)*dst > maxDisplacement) maxDisplacement = (short)(*dst);
254 if ((short)*dst < minDisplacement) minDisplacement = (short)(*dst);
260 normalmap += backgroundW;
263 if (maxDisplacement == minDisplacement) {
264 printf("Warning: grise normalmap fucked up\n");
268 /* Second pass - subtract half maximum displacement to displace in both directions */
269 for (scanline = 0; scanline < REFLECTION_HEIGHT; scanline++) {
270 for (i = 0; i < backgroundW; i++) {
271 /* Remember that MIN_SCROLL is the padding around the screen, so ti's the maximum displacement we can get (positive & negative) */
272 *dst2 = 2 * MAX_DISPLACEMENT * (*dst2 - minDisplacement) / (maxDisplacement - minDisplacement) - MAX_DISPLACEMENT;
273 *dst2 = (short)((float)*dst2 / scrollScaleTable[scanline] + 0.5f); /* Displacements must also scale with distance*/
279 static float distanceScale(int scanline) {
281 farScale = (float)NEAR_SCROLL_SPEED / (float)FAR_SCROLL_SPEED;
282 t = (float)scanline / ((float)REFLECTION_HEIGHT - 1);
283 return 1.0f / (1.0f / farScale + (1.0f - 1.0f / farScale) * t);
286 static void initScrollTables() {
288 for (i = 0; i < REFLECTION_HEIGHT; i++) {
289 scrollScaleTable[i] = distanceScale(i);
290 scrollTable[i] = 0.0f;
291 scrollTableRounded[i] = 0;
296 static void updateScrollTables(float dt) {
299 nearScrollAmount += dt * NEAR_SCROLL_SPEED;
300 nearScrollAmount = (float) fmod(nearScrollAmount, 512.0f);
302 for (i = 0; i < REFLECTION_HEIGHT; i++) {
303 scrollTable[i] = nearScrollAmount / scrollScaleTable[i];
304 scrollTableRounded[i] = (int)(scrollTable[i] + 0.5f) % scrollModTable[i];
308 /* -------------------------------------------------------------------------------------------------
310 * -------------------------------------------------------------------------------------------------
312 /* Limit streak count per scanline so we can directly jump to specific scanline */
313 #define RLE_STREAKS_PER_SCANLINE 4
314 /* Every streak is encoded by 2 bytes: offset and count of black pixels in the streak */
315 #define RLE_BYTES_PER_SCANLINE RLE_STREAKS_PER_SCANLINE * 2
316 #define RLE_FILL_COLOR 0
317 #define RLE_FILL_COLOR_32 ((RLE_FILL_COLOR << 16) | RLE_FILL_COLOR)
319 static RLEBitmap rleCreate(unsigned int w, unsigned int h) {
324 /* Add some padding at the end of the buffer, with the worst case for a scanline (w/2 streaks) */
325 ret.scans = (unsigned char*) calloc(h * RLE_BYTES_PER_SCANLINE + w, 1);
330 static void rleDestroy(RLEBitmap b) {
334 static RLEBitmap rleEncode(unsigned char *pixels, unsigned int w, unsigned int h) {
341 unsigned char *output;
343 /* https://www.youtube.com/watch?v=RKMR02o1I88&feature=youtu.be&t=55 */
344 ret = rleCreate(w, h);
346 for (scanline = 0; scanline < h; scanline++) {
347 output = ret.scans + scanline * RLE_BYTES_PER_SCANLINE;
349 for (i = 0; i < w; i++) {
352 if (counter >= PIXEL_PADDING) {
353 *output++ = (unsigned char) counter;
355 *output++ = (unsigned char)accum;
360 *output++ = (unsigned char)accum;
367 *output++ = (unsigned char)counter;
379 *output++ = (unsigned char)counter;
388 static void rleBlit(unsigned short *dst, int dstW, int dstH, int dstStride,
389 RLEBitmap bitmap, int blitX, int blitY)
393 int streakLength = 0;
395 unsigned char *input = bitmap.scans;
396 unsigned short *output;
397 unsigned int *output32;
399 dst += blitX + blitY * dstStride;
401 for (scanline = blitY; scanline < blitY + bitmap.h; scanline++) {
402 if (scanline < 0 || scanline >= dstH) continue;
403 for (streak = 0; streak < RLE_STREAKS_PER_SCANLINE; streak++) {
404 streakPos = *input++;
405 streakLength = *input++;
407 if ((streakPos + blitX) <= 0) continue;
409 output = dst + streakPos;
411 /* Check if we need to write the first pixel as 16bit */
412 if (streakLength % 2) {
413 *output++ = RLE_FILL_COLOR;
416 /* Then, write 2 pixels at a time */
418 output32 = (unsigned int*) output;
419 while (streakLength--) {
420 *output32++ = RLE_FILL_COLOR_32;
428 static void interpolateScan(unsigned char *output, unsigned char *a, unsigned char *b, float t) {
429 static int div = 1 << 23;
433 ti = (*((unsigned int*)&t)) & 0x7FFFFF;
435 for (i = 0; i < RLE_BYTES_PER_SCANLINE; i++) {
436 *output++ = ((*b++ * ti) + (*a++ * (div - ti))) >> 23;
440 static void rleBlitScale(unsigned short *dst, int dstW, int dstH, int dstStride,
441 RLEBitmap bitmap, int blitX, int blitY, float scaleX, float scaleY)
445 int streakLength = 0;
447 unsigned short *output;
448 unsigned int *output32;
449 unsigned char *input;
450 int scanlineCounter = 0;
451 static unsigned char scan[512];
453 int blitW = (int) (bitmap.w * scaleX + 0.5f);
454 int blitH = (int)(bitmap.h * scaleY + 0.5f);
456 dst += blitX + blitY * dstStride;
458 for (scanline = blitY; scanline < blitY + blitH; scanline++) {
459 float normalScan = scanlineCounter / scaleY;
460 unsigned char *scan0 = bitmap.scans + RLE_BYTES_PER_SCANLINE * (int)normalScan;
461 unsigned char *scan1 = scan0 + RLE_BYTES_PER_SCANLINE;
462 normalScan -= (int)normalScan;
463 interpolateScan(scan, scan0, scan1, normalScan);
467 if (scanline < 0 || scanline >= dstH) continue;
468 for (streak = 0; streak < RLE_STREAKS_PER_SCANLINE; streak++) {
469 streakPos = (int) ((*input++) * scaleX + 0.5f);
470 streakLength = (int)((*input++) * scaleX + 0.5f);
472 if ((streakPos + blitX) <= 0) continue;
474 output = dst + streakPos;
476 /* Check if we need to write the first pixel as 16bit */
477 if (streakLength % 2) {
478 *output++ = RLE_FILL_COLOR;
481 /* Then, write 2 pixels at a time */
483 output32 = (unsigned int*)output;
484 while (streakLength--) {
485 *output32++ = RLE_FILL_COLOR_32;
493 static void rleBlitScaleInv(unsigned short *dst, int dstW, int dstH, int dstStride,
494 RLEBitmap bitmap, int blitX, int blitY, float scaleX, float scaleY)
498 int streakLength = 0;
500 unsigned short *output;
501 unsigned int *output32;
502 unsigned char *input;
503 int scanlineCounter = 0;
504 static unsigned char scan[512];
506 int blitW = (int)(bitmap.w * scaleX + 0.5f);
507 int blitH = (int)(bitmap.h * scaleY + 0.5f);
509 dst += blitX + blitY * dstStride;
511 for (scanline = blitY; scanline > blitY - blitH; scanline--) {
512 float normalScan = scanlineCounter / scaleY;
513 unsigned char *scan0 = bitmap.scans + RLE_BYTES_PER_SCANLINE * (int)normalScan;
514 unsigned char *scan1 = scan0 + RLE_BYTES_PER_SCANLINE;
515 normalScan -= (int)normalScan;
516 interpolateScan(scan, scan0, scan1, normalScan);
520 if (scanline < 0 || scanline >= dstH) continue;
521 for (streak = 0; streak < RLE_STREAKS_PER_SCANLINE; streak++) {
522 streakPos = (int)((*input++) * scaleX + 0.5f);
523 streakLength = (int)((*input++) * scaleX + 0.5f);
525 if ((streakPos + blitX) <= 0) continue;
527 output = dst + streakPos;
529 /* Check if we need to write the first pixel as 16bit */
530 if (streakLength % 2) {
531 *output++ = RLE_FILL_COLOR;
534 /* Then, write 2 pixels at a time */
536 output32 = (unsigned int*)output;
537 while (streakLength--) {
538 *output32++ = RLE_FILL_COLOR_32;