My first commit in this project, just a lame plasma test effect and my tinyfps with...
authorMichael Kargas <Michael Kargas>
Sun, 18 Sep 2016 21:26:29 +0000 (22:26 +0100)
committerMichael Kargas <Michael Kargas>
Sun, 18 Sep 2016 21:26:29 +0000 (22:26 +0100)
dosdemo.vcxproj
dosdemo.vcxproj.filters
src/plasma.c [new file with mode: 0644]
src/screen.c
src/tinyfps.c [new file with mode: 0644]
src/tinyfps.h [new file with mode: 0644]

index a1791c3..b1bb483 100644 (file)
     <ClCompile Include="src\fract.c" />\r
     <ClCompile Include="src\gfxutil.c" />\r
     <ClCompile Include="src\grise.c" />\r
+    <ClCompile Include="src\plasma.c" />\r
     <ClCompile Include="src\polyfill.c" />\r
     <ClCompile Include="src\polytest.c" />\r
     <ClCompile Include="src\screen.c" />\r
     <ClCompile Include="src\sdl\main.c" />\r
+    <ClCompile Include="src\tinyfps.c" />\r
     <ClCompile Include="src\tunnel.c" />\r
   </ItemGroup>\r
   <ItemGroup>\r
     <ClInclude Include="src\demo.h" />\r
     <ClInclude Include="src\gfxutil.h" />\r
     <ClInclude Include="src\screen.h" />\r
+    <ClInclude Include="src\tinyfps.h" />\r
   </ItemGroup>\r
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />\r
   <ImportGroup Label="ExtensionTargets">\r
index 0af8b7c..fd7403d 100644 (file)
     <ClCompile Include="src\polytest.c">\r
       <Filter>src</Filter>\r
     </ClCompile>\r
+    <ClCompile Include="src\plasma.c">\r
+      <Filter>src</Filter>\r
+    </ClCompile>\r
+    <ClCompile Include="src\tinyfps.c">\r
+      <Filter>src</Filter>\r
+    </ClCompile>\r
   </ItemGroup>\r
   <ItemGroup>\r
     <ClInclude Include="src\demo.h">\r
@@ -51,5 +57,8 @@
     <ClInclude Include="src\gfxutil.h">\r
       <Filter>src</Filter>\r
     </ClInclude>\r
+    <ClInclude Include="src\tinyfps.h">\r
+      <Filter>src</Filter>\r
+    </ClInclude>\r
   </ItemGroup>\r
 </Project>
\ No newline at end of file
diff --git a/src/plasma.c b/src/plasma.c
new file mode 100644 (file)
index 0000000..ac13e6a
--- /dev/null
@@ -0,0 +1,122 @@
+// Just a test with a run of the mill plasma
+
+#include <stdlib.h>
+#include <math.h>
+
+#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 = {
+       "plasma",
+       init,
+       destroy,
+       start,
+       stop,
+       draw
+};
+
+unsigned long startingTime;
+
+#define PSIN_SIZE 4096
+#define PPAL_SIZE 256
+
+unsigned char *psin1, *psin2, *psin3;
+unsigned short *plasmaPal;
+
+unsigned short myBuffer[320*240];
+
+
+struct screen *plasma_screen(void)
+{
+       return &scr;
+}
+
+static int init(void)
+{
+       int i;
+
+       initFpsFonts();
+
+       psin1 = (unsigned char*)malloc(sizeof(unsigned char) * PSIN_SIZE);
+       psin2 = (unsigned char*)malloc(sizeof(unsigned char) * PSIN_SIZE);
+       psin3 = (unsigned char*)malloc(sizeof(unsigned char) * PSIN_SIZE);
+
+       for (i = 0; i < PSIN_SIZE; i++)
+       {
+               psin1[i] = (unsigned char)(sin((double)i / 45.0) * 63.0 + 63.0);
+               psin2[i] = (unsigned char)(sin((double)i / 75.0) * 42.0 + 42.0);
+               psin3[i] = (unsigned char)(sin((double)i / 32.0) * 88.0 + 88.0);
+       }
+
+       plasmaPal = (unsigned short*)malloc(sizeof(unsigned short) * PPAL_SIZE);
+       for (i=0; i<PPAL_SIZE; i++)
+       {
+               int r, g, b;
+               int c = i;
+               if (c > 127) c = 255 - c;
+               c >>= 2;
+               g = 31 - c;
+               r = c;
+               b = g;
+               plasmaPal[i] = (r<<11) | (g<<5) | b;
+       }
+
+       return 0xCAFE;
+       //return 0;
+}
+
+static void destroy(void)
+{
+       free(psin1);
+       free(psin2);
+       free(psin3);
+}
+
+static void start(long trans_time)
+{
+       startingTime = time_msec;
+}
+
+static void stop(long trans_time)
+{
+}
+
+static void draw(void)
+{
+       int x, y;
+       unsigned char c;
+       unsigned char s1, s2;
+
+       float dt = (float)(time_msec - startingTime) / 1000.0f;
+       int t1 = sin(0.1f * dt) * 132 + 132;
+       int t2 = sin(0.2f * dt) * 248 + 248;
+       int t3 = sin(0.5f * dt) * 380 + 380;
+
+       unsigned int *vram32 = (unsigned int*)vmem_back;
+       unsigned int p0, p1;
+       for (y = 0; y < fb_height; y++)
+       {
+               s1 = psin2[y + t2];
+               s2 = psin3[y + t1];
+               for (x = 0; x < fb_width; x+=2)
+               {
+                       c = psin1[x + t1] + s1 + psin3[x + y + t3] + psin1[psin2[x + t2] + s2 + t3];
+                       p0 = plasmaPal[c];
+                       c = psin1[x + 1 + t1] + s1 + psin3[x + 1 + y + t3] + psin1[psin2[x + 1 + t2] + s2 + t3];
+                       p1 = plasmaPal[c];
+
+                       *vram32++ = (p1 << 16) | p0;
+               }
+       }
+
+       drawFps((unsigned short*)fb_pixels);
+
+       swap_buffers(0);
+}
index 927903b..c91cb82 100644 (file)
@@ -9,8 +9,9 @@ struct screen *tunnel_screen(void);
 struct screen *fract_screen(void);
 struct screen *grise_screen(void);
 struct screen *polytest_screen(void);
+struct screen *plasma_screen(void);
 
-#define NUM_SCR        32
+#define NUM_SCR 32
 static struct screen *scr[NUM_SCR];
 static int num_screens;
 
@@ -33,6 +34,9 @@ int scr_init(void)
        if(!(scr[idx++] = polytest_screen())) {
                return -1;
        }
+       if (!(scr[idx++] = plasma_screen())) {
+               return -1;
+       }
        num_screens = idx;
 
        assert(num_screens <= NUM_SCR);
@@ -66,7 +70,7 @@ void scr_shutdown(void)
 
 void scr_update(void)
 {
-       if(prev) {      /* we're in the middle of a transition */
+       if(prev) {  /* we're in the middle of a transition */
                long interval = time_msec - trans_start;
                if(interval >= trans_dur) {
                        if(next->start) {
@@ -111,7 +115,7 @@ int scr_change(struct screen *s, long trans_time)
        if(s == cur) return 0;
 
        if(trans_time) {
-               trans_dur = trans_time / 2;     /* half for each part transition out then in */
+               trans_dur = trans_time / 2; /* half for each part transition out then in */
                trans_start = time_msec;
        } else {
                trans_dur = 0;
diff --git a/src/tinyfps.c b/src/tinyfps.c
new file mode 100644 (file)
index 0000000..4b9d9db
--- /dev/null
@@ -0,0 +1,125 @@
+#include <stdlib.h>
+
+#include "tinyfps.h"
+#include "demo.h"
+
+// TinyFPS, just a minimal fraps like font to show FPS during the demo and not just after.
+// I'll be using it in my effects for my performance test purposes, just adding it here.
+// Maybe it would be nice if initFpsFonts would be called in demo.c once but I avoided touching that code.
+
+/*
+1110 0010 1110 1110 1010 1110 1110 1110 1110 1110
+1010 0010 0010 0010 1010 1000 1000 0010 1010 1010
+1010 0010 1110 0110 1110 1110 1110 0010 1110 1110
+1010 0010 1000 0010 0010 0010 1010 0010 1010 0010
+1110 0010 1110 1110 0010 1110 1110 0010 1110 1110
+*/
+
+const unsigned char miniDecimalData[] = { 0xE2, 0xEE, 0xAE, 0xEE, 0xEE,
+0xA2, 0x22, 0xA8, 0x82, 0xAA,
+0xA2, 0xE6, 0xEE, 0xE2, 0xEE,
+0xA2, 0x82, 0x22, 0xA2, 0xA2,
+0xE2, 0xEE, 0x2E, 0xE2, 0xEE };
+
+unsigned short miniDecimalFonts[FPS_FONT_NUM_PIXELS];
+
+unsigned long startingFpsTime = 0;
+int fpsFontsInit = 0;
+int nFrames = 0;
+
+void initFpsFonts()
+{
+       if (fpsFontsInit == 0)
+       {
+               unsigned char miniDecimalPixels[FPS_FONT_NUM_PIXELS];
+
+               int k = 0;
+               for (int i = 0; i < FPS_FONT_NUM_PIXELS / 8; i++)
+               {
+                       unsigned char d = miniDecimalData[i];
+                       for (int j = 0; j < 8; j++)
+                       {
+                               unsigned char c = (d & 0x80) >> 7;
+                               miniDecimalPixels[k++] = c;
+                               d <<= 1;
+                       }
+               }
+
+               int i = 0;
+               for (int n = 0; n < FPS_FONTS_NUM; n++)
+               {
+                       for (int y = 0; y < FPS_FONT_HEIGHT; y++)
+                       {
+                               for (int x = 0; x < FPS_FONT_WIDTH; x++)
+                               {
+                                       miniDecimalFonts[i++] = miniDecimalPixels[n * FPS_FONT_WIDTH + x + y * FPS_FONT_WIDTH * FPS_FONTS_NUM] * 0xFFFF;
+                               }
+                       }
+               }
+
+               fpsFontsInit = 1;
+       }
+}
+
+void drawFont(unsigned char decimal, int posX, int posY, unsigned char zoom, unsigned short *vram)
+{
+       int x, y, j, k;
+       unsigned short c;
+
+    unsigned short *fontData = (unsigned short*)&miniDecimalFonts[decimal * FPS_FONT_WIDTH * FPS_FONT_HEIGHT];
+
+       vram += posY * fb_width + posX;
+
+       if (zoom < 1) zoom = 1;
+       if (zoom > 4) zoom = 4;
+
+    for (y = 0; y<FPS_FONT_HEIGHT; y++)
+    {
+               for (x = 0; x<FPS_FONT_WIDTH; x++)
+        {
+                       c = *fontData++;
+
+            if (c!=0)
+            {
+                for (j=0; j<zoom; j++)
+                {
+                    for (k=0; k<zoom; k++)
+                    {
+                        *(vram + j * fb_width + k) ^= c;
+                    }
+                }
+            }
+            vram += zoom;
+        }
+        vram += (-FPS_FONT_WIDTH * zoom + fb_width * zoom);
+    }
+}
+
+void drawDecimal(unsigned int number, int posX, int posY, unsigned char zoom, unsigned short *vram)
+{
+       int i = 0;
+    char buffer[8];
+
+    itoa(number, buffer, 10);
+
+       while(i < 8 && buffer[i] != 0)
+    {
+        drawFont(buffer[i] - 48, posX + i * zoom * FPS_FONT_WIDTH, posY, zoom, vram);
+               i++;
+    }
+}
+
+void drawFps(unsigned short *vram)
+{
+       unsigned long dt = time_msec - startingFpsTime;
+       static int fps = 0;
+
+       nFrames++;
+       if (dt >= 1000)
+       {
+               fps = (nFrames * 1000) / dt;
+               startingFpsTime = time_msec;
+               nFrames = 0;
+       }
+       drawDecimal(fps, 4, 4, 2, vram);
+}
diff --git a/src/tinyfps.h b/src/tinyfps.h
new file mode 100644 (file)
index 0000000..6dc74c0
--- /dev/null
@@ -0,0 +1,12 @@
+#ifndef TINY_FPS_H
+#define TINY_FPS_H
+
+#define FPS_FONT_WIDTH 4
+#define FPS_FONT_HEIGHT 5
+#define FPS_FONTS_NUM 10
+#define FPS_FONT_NUM_PIXELS (FPS_FONTS_NUM * FPS_FONT_WIDTH * FPS_FONT_HEIGHT)
+
+void initFpsFonts();
+void drawFps(unsigned short *vram);
+
+#endif