1 // Bump effect (not moving yet of course, I have many ideas on this to commit before it's ready)
12 static int init(void);
13 static void destroy(void);
14 static void start(long trans_time);
15 static void draw(void);
17 static struct screen scr = {
30 #define NUM_BIG_LIGHTS 3
31 #define BIG_LIGHT_WIDTH 256
32 #define BIG_LIGHT_HEIGHT BIG_LIGHT_WIDTH
34 static unsigned long startingTime;
36 static unsigned char *heightmap;
37 static unsigned short *lightmap;
38 static int *bumpOffset;
40 static unsigned short *bigLight[NUM_BIG_LIGHTS];
41 static struct point bigLightPoint[NUM_BIG_LIGHTS];
43 struct screen *bump_screen(void)
50 int i, j, x, y, c, r, g, b;
52 const int numBlurs = 3;
53 const int lightRadius = BIG_LIGHT_WIDTH / 2;
55 const int bigLightSize = BIG_LIGHT_WIDTH * BIG_LIGHT_HEIGHT;
56 const int fb_size = fb_width * fb_height;
58 // Just some parameters to temporary test the colors of 3 lights
59 // if every light uses it's own channel bits, it's better
60 const float rgbMul[9] = { 1.0f, 0.0f, 0.0f,
65 heightmap = malloc(sizeof(*heightmap) * fb_size);
66 lightmap = malloc(sizeof(*lightmap) * fb_size);
67 bumpOffset = malloc(sizeof(*bumpOffset) * fb_size);
69 for (i = 0; i < NUM_BIG_LIGHTS; i++)
70 bigLight[i] = malloc(sizeof(*bigLight[i]) * bigLightSize);
72 memset(lightmap, 0, sizeof(*lightmap) * fb_size);
73 memset(bumpOffset, 0, sizeof(*bumpOffset) * fb_size);
76 for (i = 0; i < fb_size; i++)
77 heightmap[i] = rand() & 255;
80 for (j = 0; j < numBlurs; j++)
81 for (i = 0; i < fb_size; i++)
82 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;
84 // Inclination precalculation
86 for (y = 0; y < fb_height; y++)
88 for (x = 0; x < fb_width; x++)
90 const float offsetPower = 2.0f;
93 dx = i < fb_size - 1 ? (int)((heightmap[i] - heightmap[i + 1]) * offsetPower) : 0;
94 dy = i < fb_size - fb_width ? (int)((heightmap[i] - heightmap[i + fb_width]) * offsetPower) : 0;
98 if (xp > fb_width - 1) xp = fb_width - 1;
102 if (yp > fb_height - 1) yp = fb_height - 1;
104 bumpOffset[i++] = yp * fb_width + xp;
108 // Generate three lights
110 for (y = 0; y < BIG_LIGHT_HEIGHT; y++)
112 int yc = y - (BIG_LIGHT_HEIGHT / 2);
113 for (x = 0; x < BIG_LIGHT_WIDTH; x++)
115 int xc = x - (BIG_LIGHT_WIDTH / 2);
116 float d = (float)sqrt(xc * xc + yc * yc);
117 float invDist = ((float)lightRadius - (float)sqrt(xc * xc + yc * yc)) / (float)lightRadius;
118 if (invDist < 0.0f) invDist = 0.0f;
120 c = (int)(invDist * 63);
125 for (j = 0; j < NUM_BIG_LIGHTS; j++)
127 bigLight[j][i] = ((int)(r * rgbMul[j * 3]) << 11) | ((int)(g * rgbMul[j * 3 + 1]) << 5) | (int)(b * rgbMul[j * 3 + 2]);
136 static void destroy(void)
140 static void start(long trans_time)
142 startingTime = time_msec;
145 static void eraseArea(struct point *p, int width, int height)
152 int x1 = p->x + width;
153 int y1 = p->y + height;
157 if (x1 > fb_width) x1 = fb_width;
158 if (y1 > fb_height) y1 = fb_height;
162 dst = lightmap + y0 * fb_width + x0;
164 for (y = y0; y < y1; y++)
166 for (x = x0; x < x1; x++)
170 dst += fb_width - dx;
175 static void renderLight(struct point *p, int width, int height, unsigned short *light)
178 unsigned short *src, *dst;
182 int x1 = p->x + width;
183 int y1 = p->y + height;
200 if (x1 > fb_width) x1 = fb_width;
201 if (y1 > fb_height) y1 = fb_height;
205 dst = lightmap + y0 * fb_width + x0;
206 src = light + yl * width + xl;
208 for (y = y0; y < y1; y++)
210 for (x = x0; x < x1; x++)
214 dst += fb_width - dx;
219 static void eraseLights()
222 for (i = 0; i < NUM_BIG_LIGHTS; i++)
223 eraseArea(&bigLightPoint[i], BIG_LIGHT_WIDTH, BIG_LIGHT_HEIGHT);
226 static void renderLights()
229 for (i = 0; i < NUM_BIG_LIGHTS; i++)
230 renderLight(&bigLightPoint[i], BIG_LIGHT_WIDTH, BIG_LIGHT_HEIGHT, bigLight[i]);
233 static void animateLights()
236 float dt = (float)(time_msec - startingTime) / 1000.0f;
238 center.x = (fb_width >> 1) - (BIG_LIGHT_WIDTH / 2);
239 center.y = (fb_height >> 1) - (BIG_LIGHT_HEIGHT / 2);
241 bigLightPoint[0].x = center.x + sin(1.2f * dt) * 144.0f;
242 bigLightPoint[0].y = center.y + sin(0.8f * dt) * 148.0f;
244 bigLightPoint[1].x = center.x + sin(1.5f * dt) * 156.0f;
245 bigLightPoint[1].y = center.y + sin(1.1f * dt) * 92.0f;
247 bigLightPoint[2].x = center.x + sin(2.0f * dt) * 112.0f;
248 bigLightPoint[2].y = center.y + sin(1.3f * dt) * 136.0f;
251 static void renderBump(unsigned short *vram)
254 for (i = 0; i < fb_width * fb_height; i++)
256 unsigned short c = lightmap[bumpOffset[i]];
261 static void draw(void)
266 renderBump((unsigned short*)vmem_back);
268 drawFps((unsigned short*)vmem_back);