1 // Bump effect (not moving yet of course, I have many ideas on this to commit before it's ready)
11 static int init(void);
12 static void destroy(void);
13 static void start(long trans_time);
14 static void draw(void);
16 static struct screen scr = {
29 #define NUM_BIG_LIGHTS 3
30 #define BIG_LIGHT_WIDTH 256
31 #define BIG_LIGHT_HEIGHT BIG_LIGHT_WIDTH
33 static unsigned long startingTime;
35 static unsigned char *heightmap;
36 static unsigned short *lightmap;
37 static int *bumpOffset;
39 static unsigned short *bigLight[NUM_BIG_LIGHTS];
40 static struct point bigLightPoint[NUM_BIG_LIGHTS];
42 struct screen *bump_screen(void)
49 int i, j, x, y, c, r, g, b;
51 const int numBlurs = 3;
52 const int lightRadius = BIG_LIGHT_WIDTH / 2;
54 const int bigLightSize = BIG_LIGHT_WIDTH * BIG_LIGHT_HEIGHT;
55 const int fb_size = fb_width * fb_height;
57 // Just some parameters to temporary test the colors of 3 lights
58 // if every light uses it's own channel bits, it's better
59 const float rgbMul[9] = { 1.0f, 0.0f, 0.0f,
63 heightmap = malloc(sizeof(*heightmap) * fb_size);
64 lightmap = malloc(sizeof(*lightmap) * fb_size);
65 bumpOffset = malloc(sizeof(*bumpOffset) * fb_size);
67 for (i = 0; i < NUM_BIG_LIGHTS; i++)
68 bigLight[i] = malloc(sizeof(*bigLight[i]) * bigLightSize);
70 memset(lightmap, 0, sizeof(*lightmap) * fb_size);
71 memset(bumpOffset, 0, sizeof(*bumpOffset) * fb_size);
74 for (i = 0; i < fb_size; i++)
75 heightmap[i] = rand() & 255;
78 for (j = 0; j < numBlurs; j++)
79 for (i = 0; i < fb_size; i++)
80 heightmap[i] = (heightmap[abs((i - 1) % fb_size)] + heightmap[abs((i + 1) % fb_size)] + heightmap[abs((i - fb_width) % fb_size)] + heightmap[abs((i + fb_width) % fb_size)]) >> 2;
82 // Inclination precalculation
84 for (y = 0; y < fb_height; y++)
86 for (x = 0; x < fb_width; x++)
88 const float offsetPower = 2.0f;
91 dx = i < fb_size - 1 ? (int)((heightmap[i] - heightmap[i + 1]) * offsetPower) : 0;
92 dy = i < fb_size - fb_width ? (int)((heightmap[i] - heightmap[i + fb_width]) * offsetPower) : 0;
96 if (xp > fb_width - 1) xp = fb_width - 1;
100 if (yp > fb_height - 1) yp = fb_height - 1;
102 bumpOffset[i++] = yp * fb_width + xp;
106 // Generate three lights
108 for (y = 0; y < BIG_LIGHT_HEIGHT; y++)
110 int yc = y - (BIG_LIGHT_HEIGHT / 2);
111 for (x = 0; x < BIG_LIGHT_WIDTH; x++)
113 int xc = x - (BIG_LIGHT_WIDTH / 2);
114 float d = (float)sqrt(xc * xc + yc * yc);
115 float invDist = ((float)lightRadius - (float)sqrt(xc * xc + yc * yc)) / (float)lightRadius;
116 if (invDist < 0.0f) invDist = 0.0f;
118 c = (int)(invDist * 63);
123 for (j = 0; j < NUM_BIG_LIGHTS; j++)
125 bigLight[j][i] = ((int)(r * rgbMul[j * 3]) << 11) | ((int)(g * rgbMul[j * 3 + 1]) << 5) | (int)(b * rgbMul[j * 3 + 2]);
134 static void destroy(void)
138 static void start(long trans_time)
140 startingTime = time_msec;
143 static void eraseArea(struct point *p, int width, int height)
150 int x1 = p->x + width;
151 int y1 = p->y + height;
155 if (x1 > fb_width) x1 = fb_width;
156 if (y1 > fb_height) y1 = fb_height;
160 dst = lightmap + y0 * fb_width + x0;
162 for (y = y0; y < y1; y++)
164 for (x = x0; x < x1; x++)
168 dst += fb_width - dx;
173 static void renderLight(struct point *p, int width, int height, unsigned short *light)
176 unsigned short *src, *dst;
180 int x1 = p->x + width;
181 int y1 = p->y + height;
198 if (x1 > fb_width) x1 = fb_width;
199 if (y1 > fb_height) y1 = fb_height;
203 dst = lightmap + y0 * fb_width + x0;
204 src = light + yl * width + xl;
206 for (y = y0; y < y1; y++)
208 for (x = x0; x < x1; x++)
212 dst += fb_width - dx;
217 static void eraseLights()
220 for (i = 0; i < NUM_BIG_LIGHTS; i++)
221 eraseArea(&bigLightPoint[i], BIG_LIGHT_WIDTH, BIG_LIGHT_HEIGHT);
224 static void renderLights()
227 for (i = 0; i < NUM_BIG_LIGHTS; i++)
228 renderLight(&bigLightPoint[i], BIG_LIGHT_WIDTH, BIG_LIGHT_HEIGHT, bigLight[i]);
231 static void animateLights()
234 float dt = (float)(time_msec - startingTime) / 1000.0f;
236 center.x = (fb_width >> 1) - (BIG_LIGHT_WIDTH / 2);
237 center.y = (fb_height >> 1) - (BIG_LIGHT_HEIGHT / 2);
239 bigLightPoint[0].x = center.x + sin(1.2f * dt) * 144.0f;
240 bigLightPoint[0].y = center.y + sin(0.8f * dt) * 148.0f;
242 bigLightPoint[1].x = center.x + sin(1.5f * dt) * 156.0f;
243 bigLightPoint[1].y = center.y + sin(1.1f * dt) * 92.0f;
245 bigLightPoint[2].x = center.x + sin(2.0f * dt) * 112.0f;
246 bigLightPoint[2].y = center.y + sin(1.3f * dt) * 136.0f;
249 static void renderBump(unsigned short *vram)
252 for (i = 0; i < fb_width * fb_height; i++)
254 unsigned short c = lightmap[bumpOffset[i]];
259 static void draw(void)
264 renderBump((unsigned short*)vmem_back);