Thunder... thunder... THUNDER SCREEN! HOOOOOOOOOOOOO
authorMichael Georgoulopoulos <mgeorgoulopoulos@gmail.com>
Sat, 15 Oct 2016 23:13:45 +0000 (02:13 +0300)
committerMichael Georgoulopoulos <mgeorgoulopoulos@gmail.com>
Sat, 15 Oct 2016 23:13:45 +0000 (02:13 +0300)
Makefile
dosdemo.vcxproj
dosdemo.vcxproj.filters
src/screen.c
thunder.c [new file with mode: 0644]

index d907287..9b99f10 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 demoobj = main.obj demo.obj screen.obj cfgopt.obj music.obj gfxutil.obj 3dgfx.obj &
 polyfill.obj
-scrobj = tunnel.obj fract.obj grise.obj polytest.obj plasma.obj bump.obj
+scrobj = tunnel.obj fract.obj grise.obj polytest.obj plasma.obj bump.obj thunder.obj
 sysobj = gfx.obj vbe.obj dpmi.obj timer.obj keyb.obj mouse.obj logger.obj tinyfps.obj
 obj = $(baseobj) $(demoobj) $(sysobj) $(scrobj)
 bin = demo.exe
index 4c16782..544e97f 100644 (file)
@@ -99,6 +99,7 @@
     <ClCompile Include="src\sdl\music.c" />\r
     <ClCompile Include="src\tinyfps.c" />\r
     <ClCompile Include="src\tunnel.c" />\r
+    <ClCompile Include="thunder.c" />\r
   </ItemGroup>\r
   <ItemGroup>\r
     <ClInclude Include="src\cfgopt.h" />\r
index d0c6178..50a2510 100644 (file)
@@ -55,6 +55,9 @@
     <ClCompile Include="src\bump.c">\r
       <Filter>src</Filter>\r
     </ClCompile>\r
+    <ClCompile Include="thunder.c">\r
+      <Filter>src</Filter>\r
+    </ClCompile>\r
   </ItemGroup>\r
   <ItemGroup>\r
     <ClInclude Include="src\demo.h">\r
index 4e26db4..9aae073 100644 (file)
@@ -11,6 +11,7 @@ struct screen *grise_screen(void);
 struct screen *polytest_screen(void);
 struct screen *plasma_screen(void);
 struct screen *bump_screen(void);
+struct screen *thunder_screen(void);
 
 #define NUM_SCR 32
 static struct screen *scr[NUM_SCR];
@@ -41,6 +42,9 @@ int scr_init(void)
        if (!(scr[idx++] = bump_screen())) {
                return -1;
        }
+       if (!(scr[idx++] = thunder_screen())) {
+               return -1;
+       }
        num_screens = idx;
 
        assert(num_screens <= NUM_SCR);
diff --git a/thunder.c b/thunder.c
new file mode 100644 (file)
index 0000000..82357e9
--- /dev/null
+++ b/thunder.c
@@ -0,0 +1,180 @@
+/* thunder. c */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include <assert.h>
+#include "imago2.h"
+#include "demo.h"
+#include "screen.h"
+
+/* Render blur in half x half dimenstions. Add one pixel padding in all 
+ * directions (2 pixels horizontally, 2 pixels vertically).
+ */
+#define BLUR_BUFFER_WIDTH (320/2 + 2)
+#define BLUR_BUFFER_HEIGHT (240/2 + 2)
+#define BLUR_BUFFER_SIZE (BLUR_BUFFER_WIDTH * BLUR_BUFFER_HEIGHT)
+static unsigned char *blurBuffer, *blurBuffer2;
+
+/* TODO: Load palette from file */
+static unsigned short palette[256];
+
+void clearBlurBuffer();
+void drawPatternOnBlurBuffer();
+void applyBlur();
+void blitEffect();
+
+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 unsigned int lastFrameTime = 0;
+static float lastFrameDuration = 0.0f;
+static struct screen scr = {
+       "thunder",
+       init,
+       destroy,
+       start,
+       0,
+       draw
+};
+
+struct screen *thunder_screen(void)
+{
+       return &scr;
+}
+
+static int init(void)
+{
+       int i = 0;
+
+       blurBuffer = malloc(BLUR_BUFFER_SIZE);
+       blurBuffer2 = malloc(BLUR_BUFFER_SIZE);
+
+       clearBlurBuffer();
+
+       /* For now, map to blue */
+       for (i = 0; i < 256; i++) {
+               palette[i] = i >> 3;
+       }
+
+       return 0;
+}
+
+static void destroy(void)
+{
+       free(blurBuffer);
+       blurBuffer = 0;
+       
+       free(blurBuffer2);
+       blurBuffer2 = 0;
+}
+
+static void start(long trans_time)
+{
+       lastFrameTime = time_msec;
+}
+
+
+static void draw(void)
+{
+
+       lastFrameDuration = (time_msec - lastFrameTime) / 1000.0f;
+       lastFrameTime = time_msec;
+       
+       drawPatternOnBlurBuffer();
+       applyBlur();
+       blitEffect();
+
+       swap_buffers(0);
+}
+
+void clearBlurBuffer() {
+       /* Clear the whole buffer (including its padding ) */
+       memset(blurBuffer, 0, BLUR_BUFFER_SIZE);
+       memset(blurBuffer2, 0, BLUR_BUFFER_SIZE);
+}
+
+void drawPatternOnBlurBuffer() {
+       int i, j;
+       unsigned char *dst = blurBuffer + BLUR_BUFFER_WIDTH + 1;
+       int starty, stopy, startx, stopx;
+
+       starty = rand() % 60;
+       stopy = starty + rand() % 60;
+       startx = rand() % 80;
+       stopx = startx + rand() % 80;
+
+       if (rand() % 2) return;
+
+       for (j = starty; j < stopy; j++) {
+               for (i = startx; i < stopx; i++) {
+                       dst[i + j * BLUR_BUFFER_WIDTH] = 255;
+               }
+       }
+}
+
+void applyBlur() {
+       int i, j;
+       unsigned char *tmp;
+       unsigned char *src = blurBuffer + BLUR_BUFFER_WIDTH + 1;
+       unsigned char *dst = blurBuffer2 + BLUR_BUFFER_WIDTH + 1;
+
+       for (j = 0; j < 120; j++) {
+               for (i = 0; i < 160; i++) {
+                       *dst = (*(src - 1) + *(src + 1) + *(src - BLUR_BUFFER_WIDTH) + *(src + BLUR_BUFFER_WIDTH)) >> 2;
+                       dst++;
+                       src++;
+               }
+               /* Just skip the padding since we went through the scanline in the inner loop (except from the padding) */
+               src += 2;
+               dst += 2;
+       }
+
+       /* Swap blur buffers */
+       tmp = blurBuffer;
+       blurBuffer = blurBuffer2;
+       blurBuffer2 = tmp;
+}
+
+void blitEffect() {
+       unsigned int *dst1 = (unsigned int*) vmem_back;
+       unsigned int *dst2 = dst1 + 160; /* We're writing two pixels at once */
+       unsigned char *src1 = blurBuffer + BLUR_BUFFER_WIDTH + 1;
+       unsigned char *src2 = src1 + BLUR_BUFFER_WIDTH;
+       unsigned char tl, tr, bl, br;
+       int i, j;
+
+       for (j = 0; j < 120; j++) {
+               for (i = 0; i < 160; i++) {
+                       tl = *src1;
+                       tr = (*src1 + *(src1 + 1)) >> 1;
+                       bl = (*src1 + *src2) >> 1;
+                       br = tr + ((*src2 + *(src2 + 1)) >> 1) >> 1;
+
+                       /* Pack 2 pixels in each 32 bit word */
+                       *dst1 = (palette[tr] << 16) | palette[tl];
+                       *dst2 = (palette[br] << 16) | palette[bl];
+
+                       dst1++;
+                       src1++;
+                       dst2++;
+                       src2++;
+               }
+               /* Again, skip padding */
+               src1 += 2;
+               src2 += 2;
+
+               /* For now, skip a scanline */
+               dst1 += 160;
+               dst2 += 160;
+       }
+
+}
+
+
+
+
+