10 /* APPROX. 170 FPS Minimum */
12 #define BG_FILENAME "data/grise.png"
14 #define BB_SIZE 512 /* Let's use a power of 2. Maybe we'll zoom/rotate the effect */
16 /* Every backBuffer scanline is guaranteed to have that many dummy pixels before and after */
17 #define PIXEL_PADDING 32
19 #define MIN_SCROLL PIXEL_PADDING
20 #define MAX_SCROLL (backgroundW - fb_width - MIN_SCROLL)
22 #define FAR_SCROLL_SPEED 50.0f
23 #define NEAR_SCROLL_SPEED 400.0f
25 #define HORIZON_HEIGHT 100
26 #define REFLECTION_HEIGHT (240 - HORIZON_HEIGHT)
28 #define NORMALMAP_SCANLINE 372
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);
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);
41 static unsigned short *background = 0;
42 static unsigned int backgroundW = 0;
43 static unsigned int backgroundH = 0;
45 static unsigned int lastFrameTime = 0;
46 static float lastFrameDuration = 0.0f;
48 static short *displacementMap;
50 static unsigned short *backBuffer;
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;
58 static struct screen scr = {
67 struct screen *grise_screen(void)
75 /* Allocate back buffer */
76 backBuffer = (unsigned short*) malloc(BB_SIZE * BB_SIZE * sizeof(unsigned short));
78 if (!(background = img_load_pixels(BG_FILENAME, &backgroundW, &backgroundH, IMG_FMT_RGBA32))) {
79 fprintf(stderr, "failed to load image " BG_FILENAME "\n");
83 /* Convert to 16bpp */
84 convert32To16((unsigned int*)background, background, backgroundW * NORMALMAP_SCANLINE); /* Normalmap will keep its 32 bit color */
97 static void destroy(void)
102 img_free_pixels(background);
105 static void start(long trans_time)
107 lastFrameTime = time_msec;
110 static void stop(long trans_time)
114 static void draw(void)
116 int scroll = MIN_SCROLL + (MAX_SCROLL - MIN_SCROLL) * mouse_x / fb_width;
117 unsigned short *dst = backBuffer + PIXEL_PADDING;
118 unsigned short *src = background + scroll;
124 lastFrameDuration = (time_msec - lastFrameTime) / 1000.0f;
125 lastFrameTime = time_msec;
127 /* First, render the horizon */
128 for (scanline = 0; scanline < HORIZON_HEIGHT; scanline++) {
129 memcpy(dst, src, fb_width * 2);
134 /* Create scroll opffsets for all scanlines of the normalmap */
135 updateScrollTables(lastFrameDuration);
137 /* Then, render the reflection under the horizon */
138 /* dst is already in place */
139 src = background + HORIZON_HEIGHT * backgroundW;
140 dispScanline = displacementMap;
141 for (scanline = 0; scanline < REFLECTION_HEIGHT; scanline++) {
142 for (i = 0; i < fb_width; i++) {
143 d = dispScanline[(i + scrollTableRounded[scanline]) % scrollModTable[scanline]];
144 *dst++ = src[i + scroll + d];
147 dst += BB_SIZE - fb_width;
148 dispScanline += backgroundW;
151 /* Blit effect to framebuffer */
152 src = backBuffer + PIXEL_PADDING;
154 for (scanline = 0; scanline < fb_height; scanline++) {
155 memcpy(dst, src, fb_width * 2);
161 /* src and dst can be the same */
162 static void convert32To16(unsigned int *src32, unsigned short *dst16, unsigned int pixelCount) {
166 *dst16++ = ((p << 8) & 0xF800) /* R */
167 | ((p >> 5) & 0x07E0) /* G */
168 | ((p >> 19) & 0x001F); /* B */
173 /* Normal map preprocessing */
174 /* Scale normal with depth and unpack R component (horizontal component) */
175 static void processNormal() {
179 short maxDisplacement = 0;
180 short minDisplacement = 256;
183 unsigned int *normalmap = (unsigned int*)background;
184 normalmap += NORMALMAP_SCANLINE * backgroundW;
185 dst = (unsigned short*)normalmap;
186 displacementMap = (short*)dst;
187 dst2 = displacementMap;
189 for (scanline = 0; scanline < REFLECTION_HEIGHT; scanline++) {
190 scrollModTable[scanline] = (int) (backgroundW / scrollScaleTable[scanline] + 0.5f);
191 for (i = 0; i < backgroundW; i++) {
192 x = (int)(i * scrollScaleTable[scanline] + 0.5f);
193 if (x < backgroundW) {
194 *dst = (unsigned short)(normalmap[x] >> 8) & 0xFF;
195 if ((short)*dst > maxDisplacement) maxDisplacement = (short)(*dst);
196 if ((short)*dst < minDisplacement) minDisplacement = (short)(*dst);
202 normalmap += backgroundW;
205 if (maxDisplacement == minDisplacement) {
206 printf("Warning: grise normalmap fucked up\n");
210 /* Second pass - subtract half maximum displacement to displace in both directions */
211 for (scanline = 0; scanline < REFLECTION_HEIGHT; scanline++) {
212 for (i = 0; i < backgroundW; i++) {
213 /* Remember that MIN_SCROLL is the padding around the screen, so ti's the maximum displacement we can get (positive & negative) */
214 *dst2 = 2 * MIN_SCROLL * (*dst2 - minDisplacement) / (maxDisplacement - minDisplacement) - MIN_SCROLL;
215 *dst2 = (short)((float)*dst2 / scrollScaleTable[scanline] + 0.5f); /* Displacements must also scale with distance*/
221 static float distanceScale(int scanline) {
223 farScale = (float)NEAR_SCROLL_SPEED / (float)FAR_SCROLL_SPEED;
224 t = (float)scanline / ((float)REFLECTION_HEIGHT - 1);
225 return 1.0f / (1.0f / farScale + (1.0f - 1.0f / farScale) * t);
228 static void initScrollTables() {
230 for (i = 0; i < REFLECTION_HEIGHT; i++) {
231 scrollScaleTable[i] = distanceScale(i);
232 scrollTable[i] = 0.0f;
233 scrollTableRounded[i] = 0;
238 static void updateScrollTables(float dt) {
241 nearScrollAmount += dt * NEAR_SCROLL_SPEED;
242 nearScrollAmount = (float) fmod(nearScrollAmount, 512.0f);
244 for (i = 0; i < REFLECTION_HEIGHT; i++) {
245 scrollTable[i] = nearScrollAmount / scrollScaleTable[i];
246 scrollTableRounded[i] = (int)(scrollTable[i] + 0.5f) % scrollModTable[i];