From af69d4ef5753637bc6901febf039cdd8d74a86d4 Mon Sep 17 00:00:00 2001 From: Michael Kargas Date: Sun, 25 Sep 2016 14:48:26 +0100 Subject: [PATCH] Bump effect added. This is unfinished, nothing to see here yet, just commiting initialization to work later on it. --- src/bump.c | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/screen.c | 4 ++ 2 files changed, 134 insertions(+) create mode 100644 src/bump.c diff --git a/src/bump.c b/src/bump.c new file mode 100644 index 0000000..cf020b7 --- /dev/null +++ b/src/bump.c @@ -0,0 +1,130 @@ +// Bump effect (not moving yet of course, I have many ideas on this to commit before it's ready) + +#include +#include +#include + +#include "demo.h" +#include "screen.h" +#include "tinyfps.h" + +static int init(void); +static void destroy(void); +static void start(long trans_time); +static void stop(long trans_time); +static void draw(void); + +static struct screen scr = { + "bump", + init, + destroy, + start, + stop, + draw +}; + +static unsigned long startingTime; + +static unsigned char *heightmap; +static unsigned short *lightmap; +static int *bumpOffset; + +struct screen *bump_screen(void) +{ + return &scr; +} + +static int init(void) +{ + const int numBlurs = 3; + int i, j, x, y; + int fb_size = fb_width * fb_height; + + 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); + + // Create random junk + for (i = 0; i < fb_size; i++) + heightmap[i] = rand() & 255; + + // Blur to smooth + for (j = 0; j < numBlurs; j++) + for (i = 0; i < fb_size; i++) + 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; + + // Inclination precalculation + i = 0; + for (y = 0; y < fb_height; y++) + { + for (x = 0; x < fb_width; x++) + { + const float offsetPower = 2.0f; + int dx, dy, xp, yp; + + dx = (int)((heightmap[i] - heightmap[i + 1]) * offsetPower); + dy = (int)((heightmap[i] - heightmap[i + fb_width]) * offsetPower); + + xp = x + dx; + if (xp < 0) xp = 0; + if (xp > fb_width - 1) xp = fb_width - 1; + + yp = y + dy; + if (yp < 0) yp = 0; + if (yp > fb_height - 1) yp = fb_height - 1; + + bumpOffset[i++] = yp * fb_width + xp; + } + } + + // 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) + i = 0; + for (y = 0; y < fb_height; y++) + { + int yc = y - (fb_height >> 1); + for (x = 0; x < fb_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); + } + } + + return 0; +} + +static void destroy(void) +{ +} + +static void start(long trans_time) +{ + startingTime = time_msec; +} + +static void stop(long trans_time) +{ +} + +static void draw(void) +{ + 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; + } + + drawFps((unsigned short*)fb_pixels); + + swap_buffers(0); +} diff --git a/src/screen.c b/src/screen.c index 17491d5..4e26db4 100644 --- a/src/screen.c +++ b/src/screen.c @@ -10,6 +10,7 @@ struct screen *fract_screen(void); struct screen *grise_screen(void); struct screen *polytest_screen(void); struct screen *plasma_screen(void); +struct screen *bump_screen(void); #define NUM_SCR 32 static struct screen *scr[NUM_SCR]; @@ -37,6 +38,9 @@ int scr_init(void) if (!(scr[idx++] = plasma_screen())) { return -1; } + if (!(scr[idx++] = bump_screen())) { + return -1; + } num_screens = idx; assert(num_screens <= NUM_SCR); -- 1.7.10.4