Added boundary checks for lights, made lights bigger, moved TinyFPS to bottom of...
[dosdemo] / src / bump.c
index 66c6bbc..228e141 100644 (file)
@@ -24,12 +24,23 @@ static struct screen scr = {
        draw
 };
 
+static struct point {
+       int x, y;
+};
+
+#define NUM_BIG_LIGHTS 3
+#define BIG_LIGHT_WIDTH 256
+#define BIG_LIGHT_HEIGHT BIG_LIGHT_WIDTH
+
 static unsigned long startingTime;
 
 static unsigned char *heightmap;
 static unsigned short *lightmap;
 static int *bumpOffset;
 
+static unsigned short *bigLight[NUM_BIG_LIGHTS];
+static struct point bigLightPoint[NUM_BIG_LIGHTS];
+
 struct screen *bump_screen(void)
 {
        return &scr;
@@ -37,18 +48,30 @@ struct screen *bump_screen(void)
 
 static int init(void)
 {
+       int i, j, x, y, c, r, g, b;
+
        const int numBlurs = 3;
-       int i, j, x, y;
-       int fb_size = fb_width * fb_height;
+       const int lightRadius = BIG_LIGHT_WIDTH / 2;
+
+       const int bigLightSize = BIG_LIGHT_WIDTH * BIG_LIGHT_HEIGHT;
+       const int fb_size = fb_width * fb_height;
 
+       // Just some parameters to temporary test the colors of 3 lights
+       // if every light uses it's own channel bits, it's better
+       const float rgbMul[9] = { 1.0f, 0.0f, 0.0f, 
+                                                                 0.0f, 1.0f, 0.0f,
+                                                                 0.0f, 0.0f, 1.0f};
        initFpsFonts();
 
        heightmap = malloc(sizeof(*heightmap) * fb_size);
        lightmap = malloc(sizeof(*lightmap) * fb_size);
        bumpOffset = malloc(sizeof(*bumpOffset) * fb_size);
 
-       memset(lightmap, 0, fb_size);
-       memset(bumpOffset, 0, fb_size);
+       for (i = 0; i < NUM_BIG_LIGHTS; i++)
+               bigLight[i] = malloc(sizeof(*bigLight[i]) * bigLightSize);
+
+       memset(lightmap, 0, sizeof(*lightmap) * fb_size);
+       memset(bumpOffset, 0, sizeof(*bumpOffset) * fb_size);
 
        // Create random junk
        for (i = 0; i < fb_size; i++)
@@ -83,19 +106,28 @@ static int init(void)
                }
        }
 
-       // Lightmap test (this will be replaced by code in the main loop later on, the idea is you can render moving lights and other light geometry or sprites in light buffer and the bumpOffset will catch them)
+       // Generate three lights
        i = 0;
-       for (y = 0; y < fb_height; y++)
+       for (y = 0; y < BIG_LIGHT_HEIGHT; y++)
        {
-               int yc = y - (fb_height >> 1);
-               for (x = 0; x < fb_width; x++)
+               int yc = y - (BIG_LIGHT_HEIGHT / 2);
+               for (x = 0; x < BIG_LIGHT_WIDTH; x++)
                {
-                       int xc = x - (fb_width >> 1);
-                       int c = (int)sqrt(xc * xc + yc * yc) << 1;
-                       int r;
-                       if (c > 255) c = 255;
-                       r = 255 - c;
-                       lightmap[i++] = ((r >> 4) << 11) | ((r >> 3) << 5) | (r >> 3);
+                       int xc = x - (BIG_LIGHT_WIDTH / 2);
+                       float d = (float)sqrt(xc * xc + yc * yc);
+                       float invDist = ((float)lightRadius - (float)sqrt(xc * xc + yc * yc)) / (float)lightRadius;
+                       if (invDist < 0.0f) invDist = 0.0f;
+
+                       c = (int)(invDist * 63);
+                       r = c >> 1;
+                       g = c;
+                       b = c >> 1;
+
+                       for (j = 0; j < NUM_BIG_LIGHTS; j++)
+                       {
+                               bigLight[j][i] = ((int)(r * rgbMul[j * 3]) << 11) | ((int)(g * rgbMul[j * 3 + 1]) << 5) | (int)(b * rgbMul[j * 3 + 2]);
+                       }
+                       i++;
                }
        }
 
@@ -115,17 +147,130 @@ static void stop(long trans_time)
 {
 }
 
-static void draw(void)
+static void eraseArea(struct point *p, int width, int height)
+{
+       int x, y, dx;
+       unsigned short *dst;
+
+       int x0 = p->x;
+       int y0 = p->y;
+       int x1 = p->x + width;
+       int y1 = p->y + height;
+
+       if (x0 < 0) x0 = 0;
+       if (y0 < 0) y0 = 0;
+       if (x1 > fb_width) x1 = fb_width;
+       if (y1 > fb_height) y1 = fb_height;
+
+       dx = x1 - x0;
+
+       dst = lightmap + y0 * fb_width + x0;
+
+       for (y = y0; y < y1; y++)
+       {
+               for (x = x0; x < x1; x++)
+               {
+                       *dst++ = 0;
+               }
+               dst += fb_width - dx;
+       }
+}
+
+
+static void renderLight(struct point *p, int width, int height, unsigned short *light)
+{
+       int x, y, dx;
+       unsigned short *src, *dst;
+
+       int x0 = p->x;
+       int y0 = p->y;
+       int x1 = p->x + width;
+       int y1 = p->y + height;
+
+       int xl = 0;
+       int yl = 0;
+
+       if (x0 < 0)
+       {
+               xl = -x0;
+               x0 = 0;
+       }
+
+       if (y0 < 0)
+       {
+               yl = -y0;
+               y0 = 0;
+       }
+
+       if (x1 > fb_width) x1 = fb_width;
+       if (y1 > fb_height) y1 = fb_height;
+
+       dx = x1 - x0;
+
+       dst = lightmap + y0 * fb_width + x0;
+       src = light + yl * width + xl;
+
+       for (y = y0; y < y1; y++)
+       {
+               for (x = x0; x < x1; x++)
+               {
+                       *dst++ |= *src++;
+               }
+               dst += fb_width - dx;
+               src += width - dx;
+       }
+}
+
+static void eraseLights()
+{
+       int i;
+       for (i = 0; i < NUM_BIG_LIGHTS; i++)
+               eraseArea(&bigLightPoint[i], BIG_LIGHT_WIDTH, BIG_LIGHT_HEIGHT);
+}
+
+static void renderLights()
+{
+       int i;
+       for (i = 0; i < NUM_BIG_LIGHTS; i++)
+               renderLight(&bigLightPoint[i], BIG_LIGHT_WIDTH, BIG_LIGHT_HEIGHT, bigLight[i]);
+}
+
+static void animateLights()
+{
+       struct point center;
+       float dt = (float)(time_msec - startingTime) / 1000.0f;
+
+       center.x = (fb_width >> 1) - (BIG_LIGHT_WIDTH / 2);
+       center.y = (fb_height >> 1) - (BIG_LIGHT_HEIGHT / 2);
+
+       bigLightPoint[0].x = center.x + sin(1.2f * dt) * 144.0f;
+       bigLightPoint[0].y = center.y + sin(0.8f * dt) * 148.0f;
+
+       bigLightPoint[1].x = center.x + sin(1.5f * dt) * 156.0f;
+       bigLightPoint[1].y = center.y + sin(1.1f * dt) * 92.0f;
+
+       bigLightPoint[2].x = center.x + sin(2.0f * dt) * 112.0f;
+       bigLightPoint[2].y = center.y + sin(1.3f * dt) * 136.0f;
+}
+
+static void renderBump(unsigned short *vram)
 {
        int i;
-       unsigned short *vram = (unsigned short*)fb_pixels;
        for (i = 0; i < fb_width * fb_height; i++)
        {
                unsigned short c = lightmap[bumpOffset[i]];
                *vram++ = c;
        }
+}
+
+static void draw(void)
+{
+       eraseLights();
+       animateLights();
+       renderLights();
+       renderBump((unsigned short*)vmem_back);
 
-       drawFps((unsigned short*)fb_pixels);
+       drawFps((unsigned short*)vmem_back);
 
        swap_buffers(0);
 }