hacked support for both 16 and 32 bpp framebuffers
authorJohn Tsiombikas <nuclear@mutantstargoat.com>
Sat, 20 Aug 2016 13:29:14 +0000 (16:29 +0300)
committerJohn Tsiombikas <nuclear@mutantstargoat.com>
Sat, 20 Aug 2016 13:29:14 +0000 (16:29 +0300)
src/main.c
src/tunnel.c
src/tunnel.h

index 8881963..9e27509 100644 (file)
@@ -21,16 +21,18 @@ static int quit;
 int main(void)
 {
        fbgfx_save_video_mode();
-       fbgfx_get_video_mode(&xsz, &ysz, &depth);
+       if(!(vmem = fbgfx_get_video_mode(&xsz, &ysz, &depth))) {
+               return 1;
+       }
 
-       if(!(vmem = fbgfx_set_video_mode(xsz, ysz, 16))) {
+       /*if(!(vmem = fbgfx_set_video_mode(xsz, ysz, 16))) {
                return 1;
        }
        fbgfx_get_video_mode(&xsz, &ysz, &depth);
        if(depth != 16) {
                fprintf(stderr, "failed to set color depth: 16bpp\n");
                goto end;
-       }
+       }*/
        if(fbev_init() == -1) {
                goto end;
        }
@@ -38,7 +40,7 @@ int main(void)
        fbev_mbutton(mouse, 0);
        fbev_mmotion(motion, 0);
 
-       if(init_tunnel(xsz, ysz) == -1) {
+       if(init_tunnel(xsz, ysz, depth) == -1) {
                goto end;
        }
 
index 85ca7b4..f4b3984 100644 (file)
 
 extern unsigned long time_msec;
 
-static void draw_tunnel_range(unsigned short *pixels, int starty, int num_lines);
+static void (*draw_tunnel_range)(void*, int, int);
+
+static void draw_tunnel_range16(void *pixels, int starty, int num_lines);
+static void draw_tunnel_range32(void *pixels, int starty, int num_lines);
 static int count_bits(unsigned int x);
 static int count_zeros(unsigned int x);
 
@@ -30,13 +33,25 @@ static unsigned int tex_xmask, tex_ymask;
 static struct thread_pool *tpool;
 
 
-int init_tunnel(int x, int y)
+int init_tunnel(int x, int y, int bpp)
 {
        int i, j, n;
        unsigned int *tmap;
        unsigned char *fog;
        float aspect = (float)x / (float)y;
 
+       switch(bpp) {
+       case 16:
+               draw_tunnel_range = draw_tunnel_range16;
+               break;
+       case 32:
+               draw_tunnel_range = draw_tunnel_range32;
+               break;
+       default:
+               fprintf(stderr, "unsupported color depth: %d\n", bpp);
+               return -1;
+       }
+
        xsz = x;
        ysz = y;
        vxsz = xsz / USCALE;
@@ -111,7 +126,7 @@ void destroy_tunnel(void)
 #define NUM_WORK_ITEMS 32
 
 static struct work {
-       unsigned short *pixels;
+       void *pixels;
        int starty, num_lines;
 } work[NUM_WORK_ITEMS];
 
@@ -121,7 +136,7 @@ static void work_func(void *cls)
        draw_tunnel_range(w->pixels, w->starty, w->num_lines);
 }
 
-void draw_tunnel(unsigned short *pixels)
+void draw_tunnel(void *pixels)
 {
        int i, num_lines = vysz / NUM_WORK_ITEMS;
        for(i=0; i<NUM_WORK_ITEMS; i++) {
@@ -134,58 +149,94 @@ void draw_tunnel(unsigned short *pixels)
        tpool_wait(tpool);
 }
 
+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) & 0x1f))
+#define PACK_RGB32(r, g, b) \
+       ((((r) & 0xff) << 16) | (((g) & 0xff) << 8) | ((b) & 0xff))
+
+#define PUTPIXEL(pixtype, col) \
+       do { \
+               int k; \
+               pixtype *ptr = pixels; \
+               for(k=0; k<VSCALE; k++) { \
+                       switch(USCALE) { \
+                       case 4: \
+                               ptr[3] = col; \
+                       case 3: \
+                               ptr[2] = col; \
+                       case 2: \
+                               ptr[1] = col; \
+                       case 1: \
+                               *ptr = col; \
+                       } \
+                       ptr += xsz; \
+               } \
+       } while(0)
+
+static void draw_tunnel_range16(void *pix, int starty, int num_lines)
+{
+       int i, j;
+       unsigned int *tmap = tunnel_map + starty * vxsz;
+       unsigned char *fog = tunnel_fog + starty * vxsz;
 
-static void draw_tunnel_range(unsigned short *pixels, int starty, int num_lines)
+       long toffs = time_msec / 4;
+       unsigned short *pixels = (unsigned short*)pix + starty * xsz * VSCALE;
+
+       for(i=0; i<num_lines; i++) {
+               for(j=0; j<vxsz; j++) {
+                       unsigned int col;
+                       int r, g, b;
+
+                       tunnel_color(&r, &g, &b, toffs, *tmap++, *fog++);
+                       col = PACK_RGB16(r, g, b);
+
+                       PUTPIXEL(unsigned short, col);
+                       pixels += USCALE;
+               }
+               pixels += xsz * (VSCALE - 1);
+       }
+}
+
+static void draw_tunnel_range32(void *pix, int starty, int num_lines)
 {
-       int i, j, k, r, g, b;
+       int i, j;
        unsigned int *tmap = tunnel_map + starty * vxsz;
        unsigned char *fog = tunnel_fog + starty * vxsz;
 
        long toffs = time_msec / 4;
-       pixels += starty * xsz * VSCALE;
+       unsigned int *pixels = (unsigned int*)pix + starty * xsz * VSCALE;
 
        for(i=0; i<num_lines; i++) {
                for(j=0; j<vxsz; j++) {
-                       unsigned short *ptr;
                        unsigned int col;
-                       unsigned int tx = (((*tmap >> 16) & 0xffff) << tex_xshift) >> 16;
-                       unsigned int ty = ((*tmap & 0xffff) << tex_yshift) >> 16;
-                       ++tmap;
-
-                       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;
-
-                       r = (r * *fog) >> 8;
-                       g = (g * *fog) >> 8;
-                       b = (b * *fog) >> 8;
-                       ++fog;
-
-                       col = ((((r >> 3) & 0x1f) << 11) | (((g >> 2) & 0x3f) << 5) | ((b >> 3) & 0x1f));
-
-                       ptr = pixels;
-                       for(k=0; k<VSCALE; k++) {
-                               switch(USCALE) {
-                               case 4:
-                                       ptr[3] = col;
-                               case 3:
-                                       ptr[2] = col;
-                               case 2:
-                                       ptr[1] = col;
-                               case 1:
-                                       *ptr = col;
-                               }
-                               ptr += xsz;
-                       }
+                       int r, g, b;
+
+                       tunnel_color(&r, &g, &b, toffs, *tmap++, *fog++);
+                       col = PACK_RGB32(r, g, b);
+
+                       PUTPIXEL(unsigned int, col);
                        pixels += USCALE;
                }
                pixels += xsz * (VSCALE - 1);
index c4337e1..05a9864 100644 (file)
@@ -1,8 +1,8 @@
 #ifndef TUNNEL_H_
 #define TUNNEL_H_
 
-int init_tunnel(int x, int y);
+int init_tunnel(int x, int y, int bpp);
 void destroy_tunnel(void);
-void draw_tunnel(unsigned short *pixels);
+void draw_tunnel(void *pixels);
 
 #endif /* TUNNEL_H_ */