Added intermediate buffer to be able to alter the reflected image before applying...
[dosdemo] / src / mike.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include <assert.h>
6 #include "imago2.h"
7 #include "demo.h"
8 #include "screen.h"
9
10 /* APPROX. 170 FPS Minimum */
11
12 #define BG_FILENAME "data/grise.png"
13
14 #define BB_SIZE 512     /* Let's use a power of 2. Maybe we'll zoom/rotate the effect */
15
16 /* Every backBuffer scanline is guaranteed to have that many dummy pixels before and after */
17 #define PIXEL_PADDING 32
18
19 #define MIN_SCROLL PIXEL_PADDING
20 #define MAX_SCROLL (backgroundW - fb_width - MIN_SCROLL)
21
22 #define FAR_SCROLL_SPEED 50.0f
23 #define NEAR_SCROLL_SPEED 400.0f
24
25 #define HORIZON_HEIGHT 100
26 #define REFLECTION_HEIGHT (240 - HORIZON_HEIGHT)
27
28 #define NORMALMAP_SCANLINE 372
29
30 static int init(void);
31 static void destroy(void);
32 static void start(long trans_time);
33 static void stop(long trans_time);
34 static void draw(void);
35
36 static void convert32To16(unsigned int *src32, unsigned short *dst16, unsigned int pixelCount);
37 static void processNormal();
38 static void initScrollTables();
39 static void updateScrollTables(float dt);
40
41 static unsigned short *background = 0;
42 static unsigned int backgroundW = 0;
43 static unsigned int backgroundH = 0;
44
45 static unsigned int lastFrameTime = 0;
46 static float lastFrameDuration = 0.0f;
47
48 static short *displacementMap;
49
50 static unsigned short *backBuffer;
51
52 static float scrollScaleTable[REFLECTION_HEIGHT];
53 static float scrollTable[REFLECTION_HEIGHT];
54 static int scrollTableRounded[REFLECTION_HEIGHT];
55 static int scrollModTable[REFLECTION_HEIGHT];
56 static float nearScrollAmount = 0.0f;
57
58 static struct screen scr = {
59         "mike",
60         init,
61         destroy,
62         start,
63         stop,
64         draw
65 };
66
67 struct screen *mike_screen(void)
68 {
69         return &scr;
70 }
71
72
73 static int init(void)
74 {
75         /* Allocate back buffer */
76         backBuffer = (unsigned short*) malloc(BB_SIZE * BB_SIZE * sizeof(unsigned short));
77
78         if (!(background = img_load_pixels(BG_FILENAME, &backgroundW, &backgroundH, IMG_FMT_RGBA32))) {
79                 fprintf(stderr, "failed to load image " BG_FILENAME "\n");
80                 return -1;
81         }
82
83         /* Convert to 16bpp */
84         convert32To16((unsigned int*)background, background, backgroundW * NORMALMAP_SCANLINE); /* Normalmap will keep its 32 bit color */
85
86         initScrollTables();
87
88         processNormal();
89
90         return 0;
91 }
92
93 static void destroy(void)
94 {
95         free(backBuffer);
96         backBuffer = 0;
97
98         img_free_pixels(background);
99 }
100
101 static void start(long trans_time)
102 {
103         lastFrameTime = time_msec;
104 }
105
106 static void stop(long trans_time)
107 {
108 }
109
110 static void draw(void)
111 {       
112         int scroll = MIN_SCROLL + (MAX_SCROLL - MIN_SCROLL) * mouse_x / fb_width;
113         unsigned short *dst = backBuffer + PIXEL_PADDING;
114         unsigned short *src = background + scroll;
115         int scanline = 0;
116         int i = 0;
117         short *dispScanline;
118         int d;
119
120         lastFrameDuration = (time_msec - lastFrameTime) / 1000.0f;
121         lastFrameTime = time_msec;
122
123         /* First, render the horizon */
124         for (scanline = 0; scanline < HORIZON_HEIGHT; scanline++) {
125                 memcpy(dst, src, fb_width * 2);
126                 src += backgroundW;
127                 dst += BB_SIZE;
128         }
129         
130         /* Create scroll opffsets for all scanlines of the normalmap */
131         updateScrollTables(lastFrameDuration);
132
133         /* Then, render the reflection under the horizon */
134         /* dst is already in place */
135         src = background + HORIZON_HEIGHT * backgroundW;
136         dispScanline = displacementMap;
137         for (scanline = 0; scanline < REFLECTION_HEIGHT; scanline++) {
138                 for (i = 0; i < fb_width; i++) {
139                         d = dispScanline[(i + scrollTableRounded[scanline]) % scrollModTable[scanline]];
140                         *dst++ = src[i + scroll + d];
141                 }
142                 src += backgroundW;
143                 dst += BB_SIZE - fb_width;
144                 dispScanline += backgroundW;
145         }
146
147         /* Blit effect to framebuffer */
148         src = backBuffer + PIXEL_PADDING;
149         dst = fb_pixels;
150         for (scanline = 0; scanline < fb_height; scanline++) {
151                 memcpy(dst, src, fb_width * 2);
152                 src += BB_SIZE;
153                 dst += fb_width;
154         }
155 }
156
157 /* src and dst can be the same */
158 static void convert32To16(unsigned int *src32, unsigned short *dst16, unsigned int pixelCount) {
159         unsigned int p;
160         while (pixelCount) {
161                 p = *src32++;
162                 *dst16++ =      ((p << 8) & 0xF800)             /* R */
163                         |               ((p >> 5) & 0x07E0)             /* G */
164                         |               ((p >> 19) & 0x001F);   /* B */
165                 pixelCount--;
166         }
167 }
168
169 /* Normal map preprocessing */
170 /* Scale normal with depth and unpack R component (horizontal component) */
171 static void processNormal() {
172         int scanline;
173         int i;
174         int x;
175         short maxDisplacement = 0;
176         short minDisplacement = 256;
177         unsigned short *dst;
178         short *dst2;
179         unsigned int *normalmap = (unsigned int*)background;
180         normalmap += NORMALMAP_SCANLINE * backgroundW;
181         dst = (unsigned short*)normalmap;
182         displacementMap = (short*)dst;
183         dst2 = displacementMap;
184
185         for (scanline = 0; scanline < REFLECTION_HEIGHT; scanline++) {
186                 scrollModTable[scanline] = (int) (backgroundW / scrollScaleTable[scanline] + 0.5f);
187                 for (i = 0; i < backgroundW; i++) {
188                         x = (int)(i * scrollScaleTable[scanline] + 0.5f);
189                         if (x < backgroundW) {
190                                 *dst = (unsigned short)(normalmap[x] >> 8) & 0xFF;
191                                 if ((short)*dst > maxDisplacement) maxDisplacement = (short)(*dst);
192                                 if ((short)*dst < minDisplacement) minDisplacement = (short)(*dst);
193                         } else {
194                                 *dst = 0;
195                         }
196                         dst++;
197                 }
198                 normalmap += backgroundW;
199         }
200
201         if (maxDisplacement == minDisplacement) {
202                 printf("Warning: grise normalmap fucked up\n");
203                 return;
204         }
205
206         /* Second pass - subtract half maximum displacement to displace in both directions */
207         for (scanline = 0; scanline < REFLECTION_HEIGHT; scanline++) {
208                 for (i = 0; i < backgroundW; i++) {
209                         /* Remember that MIN_SCROLL is the padding around the screen, so ti's the maximum displacement we can get (positive & negative) */
210                         *dst2 = 2 * MIN_SCROLL * (*dst2 - minDisplacement) / (maxDisplacement - minDisplacement) - MIN_SCROLL;
211                         *dst2 = (short)((float)*dst2 / scrollScaleTable[scanline] + 0.5f); /* Displacements must also scale with distance*/
212                         dst2++;
213                 }
214         }
215 }
216
217 static float distanceScale(int scanline) {
218         float farScale, t;
219         farScale = (float)NEAR_SCROLL_SPEED / (float)FAR_SCROLL_SPEED;
220         t = (float)scanline / ((float)REFLECTION_HEIGHT - 1);
221         return 1.0f / (1.0f / farScale + (1.0f - 1.0f / farScale) * t);
222 }
223
224 static void initScrollTables() {
225         int i = 0;
226         for (i = 0; i < REFLECTION_HEIGHT; i++) {
227                 scrollScaleTable[i] = distanceScale(i);
228                 scrollTable[i] = 0.0f;
229                 scrollTableRounded[i] = 0;
230         }
231 }
232
233
234 static void updateScrollTables(float dt) {
235         int i = 0;
236         
237         nearScrollAmount += dt * NEAR_SCROLL_SPEED;
238         nearScrollAmount = (float) fmod(nearScrollAmount, 512.0f);
239
240         for (i = 0; i < REFLECTION_HEIGHT; i++) {
241                 scrollTable[i] = nearScrollAmount / scrollScaleTable[i];
242                 scrollTableRounded[i] = (int)(scrollTable[i] + 0.5f) % scrollModTable[i];
243         }
244 }