added tunnel effect and SDL backend
authorJohn Tsiombikas <nuclear@mutantstargoat.com>
Fri, 26 Aug 2016 00:50:32 +0000 (03:50 +0300)
committerJohn Tsiombikas <nuclear@mutantstargoat.com>
Fri, 26 Aug 2016 00:50:32 +0000 (03:50 +0300)
GNUmakefile [new file with mode: 0644]
Makefile
src/demo.c
src/demo.h
src/dos/main.c
src/screen.c [new file with mode: 0644]
src/screen.h [new file with mode: 0644]
src/sdl/main.c [new file with mode: 0644]
src/tunnel.c [new file with mode: 0644]

diff --git a/GNUmakefile b/GNUmakefile
new file mode 100644 (file)
index 0000000..312fd6a
--- /dev/null
@@ -0,0 +1,15 @@
+src = $(wildcard src/*.c) $(wildcard src/sdl/*.c)
+obj = $(src:.c=.o)
+bin = demo
+
+inc = -Isrc -Isrc/sdl
+
+CFLAGS = -pedantic -Wall -g $(inc) `sdl-config --cflags`
+LDFLAGS = `sdl-config --libs` -lm
+
+$(bin): $(obj)
+       $(CC) -o $@ $(obj) $(LDFLAGS)
+
+.PHONY: clean
+clean:
+       rm -f $(obj) $(bin)
index efcc291..1044346 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
 baseobj = main.obj
-demoobj = demo.obj
+demoobj = demo.obj screen.obj tunnel.obj
 sysobj = gfx.obj vbe.obj dpmi.obj timer.obj keyb.obj logger.obj
 obj = $(baseobj) $(demoobj) $(sysobj)
 bin = demo.exe
index 32f764c..4bd02c7 100644 (file)
@@ -4,22 +4,30 @@
 #include <math.h>
 #include <errno.h>
 #include "demo.h"
+#include "screen.h"
 
-int fbwidth = 320;
-int fbheight = 240;
-int fbbpp = 8;
-unsigned char *fbpixels;
+int fb_width = 320;
+int fb_height = 240;
+int fb_bpp = 16;
+unsigned char *fb_pixels;
 unsigned long time_msec;
 
 static unsigned long nframes;
 
 int demo_init(int argc, char **argv)
 {
+       if(scr_init() == -1) {
+               return -1;
+       }
+       scr_change(scr_lookup("tunnel"), 4000);
+
        return 0;
 }
 
 void demo_cleanup(void)
 {
+       scr_shutdown();
+
        if(time_msec) {
                float fps = (float)nframes / ((float)time_msec / 1000.0f);
                printf("average framerate: %.1f\n", fps);
@@ -28,16 +36,8 @@ void demo_cleanup(void)
 
 void demo_draw(void)
 {
-       int i, j;
-       unsigned char *fbptr = fbpixels;
-
-       for(i=0; i<fbheight; i++) {
-               for(j=0; j<fbwidth; j++) {
-                       int val = i^j;
-
-                       *fbptr++ = val;
-               }
-       }
+       scr_update();
+       scr_draw();
 
        ++nframes;
 }
index b2cf284..e31d596 100644 (file)
@@ -1,8 +1,8 @@
 #ifndef DEMO_H_
 #define DEMO_H_
 
-extern int fbwidth, fbheight, fbbpp;
-extern unsigned char *fbpixels;
+extern int fb_width, fb_height, fb_bpp;
+extern unsigned char *fb_pixels;
 extern unsigned long time_msec;
 
 int demo_init(int argc, char **argv);
index b2d2bdd..e1d33c2 100644 (file)
@@ -11,7 +11,7 @@ int main(int argc, char **argv)
        init_timer(100);
        kb_init(32);
 
-       if(!(fbpixels = set_video_mode(fbwidth, fbheight, fbbpp))) {
+       if(!(fb_pixels = set_video_mode(fb_width, fb_height, fb_bpp))) {
                return 1;
        }
 
diff --git a/src/screen.c b/src/screen.c
new file mode 100644 (file)
index 0000000..a0faa4b
--- /dev/null
@@ -0,0 +1,92 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "screen.h"
+#include "demo.h"
+
+struct screen *tunnel_screen(void);
+
+#define NUM_SCR        1
+static struct screen *scr[NUM_SCR];
+
+static struct screen *cur, *prev, *next;
+static long trans_start, trans_dur;
+
+int scr_init(void)
+{
+       int i;
+
+       if(!(scr[0] = tunnel_screen())) {
+               return -1;
+       }
+
+       for(i=0; i<NUM_SCR; i++) {
+               if(scr[i]->init() == -1) {
+                       return -1;
+               }
+       }
+       return 0;
+}
+
+void scr_shutdown(void)
+{
+       int i;
+       for(i=0; i<NUM_SCR; i++) {
+               scr[i]->shutdown();
+       }
+}
+
+void scr_update(void)
+{
+       if(prev) {      /* we're in the middle of a transition */
+               long interval = time_msec - trans_start;
+               if(interval >= trans_dur) {
+                       next->start(trans_dur);
+                       prev = 0;
+                       cur = next;
+                       next = 0;
+               }
+       }
+}
+
+void scr_draw(void)
+{
+       if(cur) cur->draw();
+}
+
+struct screen *scr_lookup(const char *name)
+{
+       int i;
+       for(i=0; i<NUM_SCR; i++) {
+               if(strcmp(scr[i]->name, name) == 0) {
+                       return scr[i];
+               }
+       }
+       return 0;
+}
+
+int scr_change(struct screen *s, long trans_time)
+{
+       if(!s) return -1;
+       if(s == cur) return 0;
+
+       if(trans_time) {
+               trans_dur = trans_time / 2;     /* half for each part transition out then in */
+               trans_start = time_msec;
+       } else {
+               trans_dur = 0;
+       }
+
+       if(cur) {
+               cur->stop(trans_dur);
+
+               prev = cur;
+               next = s;
+       } else {
+               s->start(trans_dur);
+
+               cur = s;
+               prev = 0;
+       }
+       return 0;
+}
diff --git a/src/screen.h b/src/screen.h
new file mode 100644 (file)
index 0000000..9a449e8
--- /dev/null
@@ -0,0 +1,25 @@
+#ifndef SCREEN_H_
+#define SCREEN_H_
+
+struct screen {
+       char *name;
+
+       int (*init)(void);
+       void (*shutdown)(void);
+
+       void (*start)(long trans_time);
+       void (*stop)(long trans_time);
+
+       void (*draw)(void);
+};
+
+int scr_init(void);
+void scr_shutdown(void);
+
+void scr_update(void);
+void scr_draw(void);
+
+struct screen *scr_lookup(const char *name);
+int scr_change(struct screen *s, long trans_time);
+
+#endif /* SCREEN_H_ */
diff --git a/src/sdl/main.c b/src/sdl/main.c
new file mode 100644 (file)
index 0000000..38822f3
--- /dev/null
@@ -0,0 +1,76 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <SDL/SDL.h>
+#include "demo.h"
+
+static void handle_event(SDL_Event *ev);
+
+static int quit;
+static long start_time;
+static SDL_Surface *fbsurf;
+
+int main(int argc, char **argv)
+{
+       unsigned int sdl_flags = SDL_SWSURFACE;
+
+       SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
+       if(!(fbsurf = SDL_SetVideoMode(fb_width, fb_height, fb_bpp, sdl_flags))) {
+               fprintf(stderr, "failed to set video mode %dx%d %dbpp\n", fb_width, fb_height, fb_bpp);
+               return 1;
+       }
+       SDL_WM_SetCaption("dosdemo SDLemu", 0);
+
+       time_msec = 0;
+       if(demo_init(argc, argv) == -1) {
+               return 1;
+       }
+       start_time = SDL_GetTicks();
+
+       while(!quit) {
+               SDL_Event ev;
+               while(SDL_PollEvent(&ev)) {
+                       handle_event(&ev);
+                       if(quit) goto break_evloop;
+               }
+
+               time_msec = SDL_GetTicks() - start_time;
+               if(SDL_MUSTLOCK(fbsurf)) {
+                       SDL_LockSurface(fbsurf);
+               }
+               fb_pixels = fbsurf->pixels;
+
+               demo_draw();
+
+               if(SDL_MUSTLOCK(fbsurf)) {
+                       SDL_UnlockSurface(fbsurf);
+               }
+               SDL_Flip(fbsurf);
+       }
+
+break_evloop:
+       demo_cleanup();
+       SDL_Quit();
+       return 0;
+}
+
+void demo_quit(void)
+{
+       quit = 1;
+}
+
+static void handle_event(SDL_Event *ev)
+{
+       switch(ev->type) {
+       case SDL_QUIT:
+               quit = 1;
+               break;
+
+       case SDL_KEYDOWN:
+       case SDL_KEYUP:
+               demo_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED ? 1 : 0);
+               break;
+
+       default:
+               break;
+       }
+}
diff --git a/src/tunnel.c b/src/tunnel.c
new file mode 100644 (file)
index 0000000..4662420
--- /dev/null
@@ -0,0 +1,281 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+#include <assert.h>
+/*#include <imago2.h>*/
+#include "demo.h"
+#include "screen.h"
+
+#ifndef M_PI
+#define M_PI   3.1415926535
+#endif
+
+#define VSCALE 1.5
+
+#define TEX_FNAME      "data/grid.png"
+#define TEX_USCALE     4
+#define TEX_VSCALE     2
+
+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 void draw_tunnel_range(unsigned short *pixels, int xoffs, int yoffs, int starty, int num_lines, long tm);
+static int count_bits(unsigned int x);
+static int count_zeros(unsigned int x);
+
+static unsigned int *gen_test_image(int *wptr, int *hptr);
+
+static struct screen scr = {
+       "tunnel",
+       init,
+       destroy,
+       start,
+       stop,
+       draw
+};
+
+static int xsz, ysz, vxsz, vysz;
+static int pan_width, pan_height;
+static unsigned int *tunnel_map;
+static unsigned char *tunnel_fog;
+
+static int tex_xsz, tex_ysz;
+static unsigned int *tex_pixels;
+static int tex_xshift, tex_yshift;
+static unsigned int tex_xmask, tex_ymask;
+
+static long trans_start, trans_dur;
+static int trans_dir;
+
+
+struct screen *tunnel_screen(void)
+{
+       return &scr;
+}
+
+
+static int init(void)
+{
+       int i, j, n;
+       unsigned int *tmap;
+       unsigned char *fog;
+       float aspect = (float)fb_width / (float)fb_height;
+
+       xsz = fb_width / 2;
+       ysz = fb_height;
+       vxsz = xsz * VSCALE;
+       vysz = ysz * VSCALE;
+
+       pan_width = vxsz - xsz;
+       pan_height = vysz - ysz;
+
+       if(!(tunnel_map = malloc(vxsz * vysz * sizeof *tunnel_map))) {
+               fprintf(stderr, "failed to allocate tunnel map\n");
+               return -1;
+       }
+       if(!(tunnel_fog = malloc(vxsz * vysz))) {
+               fprintf(stderr, "failed to allocate tunnel fog map\n");
+               return -1;
+       }
+
+       tmap = tunnel_map;
+       fog = tunnel_fog;
+
+       for(i=0; i<vysz; i++) {
+               float y = 2.0 * (float)i / (float)vysz - 1.0;
+               for(j=0; j<vxsz; j++) {
+                       float x = aspect * (2.0 * (float)j / (float)vxsz - 1.0);
+                       float tu = atan2(y, x) / M_PI * 0.5 + 0.5;
+                       float d = sqrt(x * x + y * y);
+                       float tv = d == 0.0 ? 0.0 : 1.0 / d;
+
+                       int tx = (int)(tu * 65535.0 * TEX_USCALE) & 0xffff;
+                       int ty = (int)(tv * 65535.0 * TEX_VSCALE) & 0xffff;
+
+                       int f = (int)(d * 192.0);
+
+                       *tmap++ = (tx << 16) | ty;
+                       *fog++ = f > 255 ? 255 : f;
+               }
+       }
+
+       /*if(!(tex_pixels = img_load_pixels(TEX_FNAME, &tex_xsz, &tex_ysz, IMG_FMT_RGBA32))) {
+               fprintf(stderr, "failed to load image " TEX_FNAME "\n");
+               return -1;
+       }
+       if((count_bits(tex_xsz) | count_bits(tex_ysz)) != 1) {
+               fprintf(stderr, "non-pow2 image (%dx%d)\n", tex_xsz, tex_ysz);
+               return -1;
+       }*/
+
+       tex_pixels = gen_test_image(&tex_xsz, &tex_ysz);
+
+       n = count_zeros(tex_xsz);
+       for(i=0; i<n; i++) {
+               tex_xmask |= 1 << i;
+       }
+       tex_xshift = n;
+
+       n = count_zeros(tex_ysz);
+       for(i=0; i<n; i++) {
+               tex_ymask |= 1 << i;
+       }
+       tex_yshift = n;
+
+       return 0;
+}
+
+static void destroy(void)
+{
+       free(tunnel_map);
+       free(tunnel_fog);
+       free(tex_pixels);
+}
+
+static void start(long trans_time)
+{
+       if(trans_time) {
+               trans_start = time_msec;
+               trans_dur = trans_time;
+               trans_dir = 1;
+       }
+}
+
+static void stop(long trans_time)
+{
+       if(trans_time) {
+               trans_start = time_msec;
+               trans_dur = trans_time;
+               trans_dir = -1;
+       }
+}
+
+#define NUM_WORK_ITEMS 8
+
+static void draw(void)
+{
+       int i, num_lines = ysz / NUM_WORK_ITEMS;
+       int draw_lines = num_lines;
+       float t;
+       int xoffs, yoffs;
+
+       if(trans_dir) {
+               long interval = time_msec - trans_start;
+               int progr = num_lines * interval / trans_dur;
+               if(trans_dir < 0) {
+                       draw_lines = num_lines - progr - 1;
+               } else {
+                       draw_lines = progr;
+               }
+               if(progr >= num_lines) {
+                       trans_dir = 0;
+               }
+       }
+
+       t = time_msec / 10000.0;
+       xoffs = (int)(cos(t * 3.0) * pan_width / 2) + pan_width / 2;
+       yoffs = (int)(sin(t * 4.0) * pan_height / 2) + pan_height / 2;
+
+       for(i=0; i<NUM_WORK_ITEMS; i++) {
+               int starty = i * num_lines;
+               draw_tunnel_range((unsigned short*)fb_pixels, xoffs, yoffs, starty, draw_lines, time_msec);
+       }
+}
+
+static void tunnel_color(int *rp, int *gp, int *bp, long toffs, unsigned int tpacked, int fog)
+{
+       int r, g, b;
+       unsigned int col;
+       unsigned int tx = (((tpacked >> 16) & 0xffff) << tex_xshift) >> 16;
+       unsigned int ty = ((tpacked & 0xffff) << tex_yshift) >> 16;
+       tx += toffs;
+       ty += toffs << 1;
+
+       tx &= tex_xmask;
+       ty &= tex_ymask;
+
+       col = tex_pixels[(ty << tex_xshift) + tx];
+       r = col & 0xff;
+       g = (col >> 8) & 0xff;
+       b = (col >> 16) & 0xff;
+
+       *rp = (r * fog) >> 8;
+       *gp = (g * fog) >> 8;
+       *bp = (b * fog) >> 8;
+}
+
+#define PACK_RGB16(r, g, b) \
+       (((((r) >> 3) & 0x1f) << 11) | ((((g) >> 2) & 0x3f) << 5) | (((b) >> 3) & 0x1f))
+#define PACK_RGB32(r, g, b) \
+       ((((r) & 0xff) << 16) | (((g) & 0xff) << 8) | ((b) & 0xff))
+
+static void draw_tunnel_range(unsigned short *pix, int xoffs, int yoffs, int starty, int num_lines, long tm)
+{
+       int i, j;
+       unsigned int *tmap = tunnel_map + (starty + yoffs) * vxsz + xoffs;
+       unsigned char *fog = tunnel_fog + (starty + yoffs) * vxsz + xoffs;
+
+       long toffs = tm / 4;
+       unsigned int *pixels = (unsigned int*)pix + starty * (fb_width >> 1);
+
+       for(i=0; i<num_lines; i++) {
+               for(j=0; j<xsz; j++) {
+                       unsigned int col;
+                       int r, g, b;
+
+                       tunnel_color(&r, &g, &b, toffs, tmap[j], fog[j]);
+                       col = PACK_RGB16(r, g, b);
+                       *pixels++ = (col << 16) | col;
+               }
+               tmap += vxsz;
+               fog += vxsz;
+       }
+}
+
+static int count_bits(unsigned int x)
+{
+       int i, nbits = 0;
+       for(i=0; i<32; i++) {
+               if(x & 1) ++nbits;
+               x >>= 1;
+       }
+       return nbits;
+}
+
+static int count_zeros(unsigned int x)
+{
+       int i, num = 0;
+       for(i=0; i<32; i++) {
+               if(x & 1) break;
+               ++num;
+               x >>= 1;
+       }
+       return num;
+}
+
+static unsigned int *gen_test_image(int *wptr, int *hptr)
+{
+       int i, j;
+       int xsz = 256, ysz = 256;
+       unsigned int *pixels, *pix;
+
+       if(!(pixels = malloc(xsz * ysz * sizeof *pix))) {
+               return 0;
+       }
+       pix = pixels;
+
+       for(i=0; i<ysz; i++) {
+               for(j=0; j<xsz; j++) {
+                       int val = i ^ j;
+
+                       *pix++ = PACK_RGB32(val, val / 2, val / 4);
+               }
+       }
+
+       *wptr = xsz;
+       *hptr = ysz;
+       return pixels;
+}