#include <stdio.h>
#include <stdlib.h>
#include <math.h>
+#include <assert.h>
#include <imago2.h>
#include "tpool.h"
-#include "tunnel.h"
+#include "demo.h"
+#include "screen.h"
#define TEX_FNAME "data/grid.png"
#define TEX_USCALE 4
#define USCALE 2
#define VSCALE 1
-extern unsigned long time_msec;
+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 starty, int num_lines);
+static void (*draw_tunnel_range)(void*, int, int, long);
+
+static void draw_tunnel_range16(void *pixels, int starty, int num_lines, long tm);
+static void draw_tunnel_range32(void *pixels, int starty, int num_lines, long tm);
static int count_bits(unsigned int x);
static int count_zeros(unsigned int x);
+static struct screen scr = {
+ "tunnel",
+ init,
+ destroy,
+ start,
+ stop,
+ draw
+};
+
static int xsz, ysz, vxsz, vysz;
static unsigned int *tunnel_map;
static unsigned char *tunnel_fog;
static struct thread_pool *tpool;
+static long trans_start, trans_dur;
+static int trans_dir;
+
+
+struct screen *tunnel_screen(void)
+{
+ return &scr;
+}
+
-int init_tunnel(int x, int y)
+static int init(void)
{
int i, j, n;
unsigned int *tmap;
unsigned char *fog;
- float aspect = (float)x / (float)y;
+ float aspect = (float)fb_width / (float)fb_height;
+
+ switch(fb_depth) {
+ 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", fb_depth);
+ return -1;
+ }
- xsz = x;
- ysz = y;
+ xsz = fb_width;
+ ysz = fb_height;
vxsz = xsz / USCALE;
vysz = ysz / VSCALE;
return 0;
}
-void destroy_tunnel(void)
+static void destroy(void)
{
tpool_destroy(tpool);
free(tunnel_map);
free(tunnel_fog);
}
+static void start(long trans_time)
+{
+ trans_start = time_msec;
+ trans_dur = trans_time;
+ trans_dir = 1;
+}
+
+static void stop(long trans_time)
+{
+ trans_start = time_msec;
+ trans_dur = trans_time;
+ trans_dir = -1;
+}
+
#define NUM_WORK_ITEMS 32
static struct work {
- unsigned short *pixels;
+ void *pixels;
int starty, num_lines;
+ long tm;
} work[NUM_WORK_ITEMS];
static void work_func(void *cls)
{
struct work *w = (struct work*)cls;
- draw_tunnel_range(w->pixels, w->starty, w->num_lines);
+ draw_tunnel_range(w->pixels, w->starty, w->num_lines, w->tm);
}
-void draw_tunnel(unsigned short *pixels)
+static void draw(void)
{
int i, num_lines = vysz / NUM_WORK_ITEMS;
+ int draw_lines = num_lines;
+
+ 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;
+ }
+ }
+
for(i=0; i<NUM_WORK_ITEMS; i++) {
- work[i].pixels = pixels;
+ work[i].pixels = fb_pixels;
work[i].starty = i * num_lines;
- work[i].num_lines = num_lines;
+ work[i].num_lines = draw_lines;
+ work[i].tm = time_msec;
tpool_enqueue(tpool, work + i, work_func, 0);
}
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))
+ (((((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_range16(void *pix, int starty, int num_lines, long tm)
+{
+ int i, j;
+ unsigned int *tmap = tunnel_map + starty * vxsz;
+ unsigned char *fog = tunnel_fog + starty * vxsz;
+
+ long toffs = tm / 4;
+ unsigned int *pixels = (unsigned int*)pix + starty * (xsz >> 1);
+
+ for(i=0; i<num_lines; i++) {
+ for(j=0; j<vxsz; j++) {
+ unsigned int col;
+ int r, g, b;
-static void draw_tunnel_range(unsigned short *pixels, int starty, int num_lines)
+ tunnel_color(&r, &g, &b, toffs, *tmap++, *fog++);
+ col = PACK_RGB16(r, g, b);
+ *pixels++ = col;
+ }
+ }
+}
+
+static void draw_tunnel_range32(void *pix, int starty, int num_lines, long tm)
{
- 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;
+ long toffs = tm / 4;
+ 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;
- }
- pixels += USCALE;
+ int r, g, b;
+
+ tunnel_color(&r, &g, &b, toffs, *tmap++, *fog++);
+ col = PACK_RGB32(r, g, b);
+
+ *pixels++ = col;
+ *pixels++ = col;
}
- pixels += xsz * (VSCALE - 1);
}
}