Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.
C calculatorcalculator-gamescasiochesscrossy-roaddrive-madfx-cg50gamesgeometry-dashgraphing-calculatorndlessnumworksnumworks-appsnumworks-calculatornumworks-gamenumworks-gamestetristi-84-plus-ceti-nspire
NumBlocks CE: the 3D view's inner loops in eZ80 assembly
Mason3633236e8f88444
diff --git a/games/numblocks/ce/.gitignore b/games/numblocks/ce/.gitignoreindex c3864b8d..928125af 100644--- a/games/numblocks/ce/.gitignore+++ b/games/numblocks/ce/.gitignore@@ -1,3 +1,5 @@build/obj/bin/+obj_*/+bin_*/diff --git a/games/numblocks/ce/gfx/NBCETEX.bin b/games/numblocks/ce/gfx/NBCETEX.binindex 14f6188a..3274b8fc 100644Binary files a/games/numblocks/ce/gfx/NBCETEX.bin and b/games/numblocks/ce/gfx/NBCETEX.bin differdiff --git a/games/numblocks/ce/host/plat_host.c b/games/numblocks/ce/host/plat_host.cindex 3cc6298b..166b8810 100644--- a/games/numblocks/ce/host/plat_host.c+++ b/games/numblocks/ce/host/plat_host.c@@ -48,11 +48,7 @@ static void load_bin(const char *name, uint8_t *mem, unsigned size) {}fclose(g);}-extern int render_debug;void plat_init(void) {- if (getenv("NB_DEBUG")) render_debug = atoi(getenv("NB_DEBUG"));- extern int probe_px, probe_py;- if (getenv("NB_PROBE")) sscanf(getenv("NB_PROBE"), "%d,%d", &probe_px, &probe_py);scr = screen_mem;load_bin("NBCEDAT.bin", gfx_mem, GFX_SIZE);load_bin("NBCETEX.bin", tex_mem, TEXDAT_SIZE);@@ -110,6 +106,11 @@ void plat_frame(void) {char p[512];snprintf(p, sizeof p, "%s/shot_%04d.ppm", out, frame_no);shot(p);+ extern unsigned long rs_visited, rs_drawn, rs_rows, rs_edges, rs_polys;+ fprintf(stderr, "polys %lu edges %lu rows %lu visited %lu drawn %lu\n", rs_polys, rs_edges, rs_rows, rs_visited, rs_drawn);+ rs_visited = rs_drawn = rs_rows = rs_edges = rs_polys = 0;+ extern unsigned long st_cols, st_list, st_frus, st_cov, st_culled;+ fprintf(stderr, "cols %lu listed %lu in view %lu tested %lu culled %lu\n", st_cols, st_list, st_frus, st_cov, st_culled);fprintf(stderr, "frame %d: %u cells, %u faces, %u spans, %u pixels; at %.2f %.2f %.2f yaw %d pitch %d, win %d %d %d\n",frame_no, stat_cells, stat_faces, stat_spans, stat_px, pl.x / 4096.0, pl.y / 4096.0, pl.z / 4096.0, pl.yaw,pl.pitch, win_x, win_y, win_z);diff --git a/games/numblocks/ce/src/data.h b/games/numblocks/ce/src/data.hindex 393ba7ff..eb55ccb9 100644--- a/games/numblocks/ce/src/data.h+++ b/games/numblocks/ce/src/data.h@@ -112,7 +112,9 @@ extern const uint8_t pal_rgb[256 * 3]; /* 1-15: the sky's (set at run time) *//* the appvar NBCETEX: the textures (v * 16 + u; 0 see-through), each colour at each level of light* (31 full), the nearest colour of a point [r][g][b] of 16 levels each (cube_levels) */extern const uint8_t *texdat;-#define TEXDAT_SIZE (NTEX * 256 + N_SHADE * 256 + 4096)+#define TEXDAT_RECIP (NTEX * 256 + N_SHADE * 256 + 4096)+#define TEXDAT_RATIO (TEXDAT_RECIP + 2545 * 3)+#define TEXDAT_SIZE (TEXDAT_RATIO + 256)#define tex_px ((const uint8_t (*)[256])texdat)#define shade_cm ((const uint8_t (*)[256])(texdat + NTEX * 256))#define pal_cube (texdat + NTEX * 256 + N_SHADE * 256)diff --git a/games/numblocks/ce/src/draw.c b/games/numblocks/ce/src/draw.cindex 85957e64..0b6c4012 100644--- a/games/numblocks/ce/src/draw.c+++ b/games/numblocks/ce/src/draw.c@@ -9,9 +9,13 @@ const uint8_t *gfx, *texdat;void canvas_screen(void) { cv = (Canvas){scr, SCREEN_W, 0, 0, SCREEN_W, SCREEN_H}; }-static inline void put(int x, int y, uint8_t c) {- x -= cv.ox, y -= cv.oy;- if ((unsigned)x < (unsigned)cv.w && (unsigned)y < (unsigned)cv.h) cv.p[y * cv.stride + x] = c;+/* (a row at a time: the row's pixel 0 in screen x, 0 off the canvas; x kept in [x0, x1)) */+static int x0, x1;+static uint8_t *row_at(int y) {+ y -= cv.oy;+ if ((unsigned)y >= (unsigned)cv.h) return 0;+ x0 = cv.ox, x1 = cv.ox + cv.w;+ return cv.p + y * cv.stride - cv.ox;}void fill(int x, int y, int w, int h, uint8_t c) {@@ -35,17 +39,30 @@ int text_width(const char *s) {}static void glyphs(const char *s, int x, int y, uint8_t c, int scale) {- for (; *s; s++) {- int k = *s - 32;- if (k < 0 || k >= 96) continue;- const uint8_t *g = font_bits + k * 8;- for (int r = 0; r < 8; r++)- for (int b = 0; b < 8; b++)- if (g[r] >> b & 1) {- if (scale == 1) put(x + b, y + r, c);- else fill(x + b * scale, y + r * scale, scale, scale, c);- }- x += font_w[k] * scale;+ if (scale > 1) {+ for (; *s; s++) {+ int k = *s - 32;+ if (k < 0 || k >= 96) continue;+ const uint8_t *g = font_bits + k * 8;+ for (int r = 0; r < 8; r++)+ for (int b = 0; b < 8; b++)+ if (g[r] >> b & 1) fill(x + b * scale, y + r * scale, scale, scale, c);+ x += font_w[k] * scale;+ }+ return;+ }+ for (int r = 0; r < 8; r++) {+ uint8_t *d = row_at(y + r);+ if (!d) continue;+ int gx = x;+ for (const char *p = s; *p; p++) {+ int k = *p - 32;+ if (k < 0 || k >= 96) continue;+ uint8_t bits = font_bits[k * 8 + r];+ for (int b = gx; bits; b++, bits >>= 1)+ if (bits & 1 && b >= x0 && b < x1) d[b] = c;+ gx += font_w[k];+ }}}@@ -60,21 +77,28 @@ void text_big(int x, int y, const char *s, uint8_t c, int scale) { glyphs(s, x,/* a picture kept as runs (tools/pack.py rle): x, y its top left; sx, sy, w, h a part of it */static void runs(const uint8_t *base, int sw, int sh, int x, int y, int sx, int sy, int w, int h) {for (int r = sy; r < sy + h && r < sh; r++) {+ uint8_t *d = row_at(y + r - sy);+ if (!d) continue;const uint8_t *p = base + (base[r * 2] | base[r * 2 + 1] << 8) + sh * 2;- int col = 0, dy = y + r - sy;+ /* (screen x = col + dx, within [a, b): the part asked for, on the canvas) */+ int dx = x - sx, a = x > x0 ? x : x0, b = x + w < x1 ? x + w : x1;+ int col = 0;while (col < sw) {int n = *p++;if (n < 128) {for (int k = 0; k <= n; k++, col++) {uint8_t c = *p++;- if (c && col >= sx && col < sx + w) put(x + col - sx, dy, c);+ int px = col + dx;+ if (c && px >= a && px < b) d[px] = c;}} else {int len = n - 126;uint8_t c = *p++;if (c) {- int a = col < sx ? sx : col, b = col + len > sx + w ? sx + w : col + len;- for (int k = a; k < b; k++) put(x + k - sx, dy, c);+ int s0 = col + dx, s1 = s0 + len;+ if (s0 < a) s0 = a;+ if (s1 > b) s1 = b;+ if (s1 > s0) memset(d + s0, c, (size_t)(s1 - s0));}col += len;}@@ -129,43 +153,30 @@ void button(int x, int y, int w, int state, const char *label) {text_center(x + w / 2, y + 6, label, c);}-/* the options background: dirt, 32 pixels a tile, darkened */+/* the options background: dirt, 32 pixels a tile, darkened (the texture at 2x, a quarter as bright) */void dirt_background(int y0, int y1) {- for (int y = y0; y < y1; y++)- for (int x = 0; x < SCREEN_W; x += 32) {- for (int k = 0; k < 32 && x + k < SCREEN_W; k += 2) {- /* (the dirt texture at 2x, darkened to a quarter) */- const uint8_t *row = tex_px[TX_DIRT] + ((y >> 1) & 15) * 16;- uint8_t c = dark(row[(k >> 1) & 15], 19);- put(x + k, y, c);- put(x + k + 1, y, c);- }+ static uint8_t line[SCREEN_W];+ int tr = -1;+ for (int y = y0; y < y1; y++) {+ uint8_t *d = row_at(y);+ if (!d) continue;+ int r = (y >> 1) & 15;+ if (r != tr) {+ const uint8_t *t = tex_px[TX_DIRT] + r * 16;+ for (int k = 0; k < 16; k++) line[2 * k] = line[2 * k + 1] = dark(t[k], 19);+ for (int k = 32; k < SCREEN_W; k += 32) memcpy(line + k, line, 32);+ tr = r;}+ int a = x0 > 0 ? x0 : 0, b = x1 < SCREEN_W ? x1 : SCREEN_W;+ if (b > a) memcpy(d + a, line + a, (size_t)(b - a));+ }}/* ---------------------------------------------------------------- the 3D view on the screen */-uint8_t hud_layer[HUD_W * HUD_H];+uint8_t *hud_layer;-void compose(const uint8_t *src, bool hud, bool crosshair) {- plat_expand(src, scr, HUD_Y / 2);- /* the rows under the HUD: doubled, then the HUD's own pixels over them */- for (int y = HUD_Y; y < SCREEN_H; y += 2) {- uint8_t *d = scr + y * SCREEN_W;- const uint8_t *s = src + (y / 2) * RW;- for (int x = 0; x < RW; x++) d[2 * x] = d[2 * x + 1] = s[x];- if (hud) {- const uint8_t *h = hud_layer + (y - HUD_Y) * HUD_W;- for (int x = 0; x < HUD_W; x++)- if (h[x]) d[HUD_X + x] = h[x];- }- uint8_t *d2 = d + SCREEN_W;- for (int x = 0; x < RW; x++) d2[2 * x] = d2[2 * x + 1] = s[x];- if (hud) {- const uint8_t *h = hud_layer + (y + 1 - HUD_Y) * HUD_W;- for (int x = 0; x < HUD_W; x++)- if (h[x]) d2[HUD_X + x] = h[x];- }- }+void compose(bool hud, bool crosshair) {+ render_show(hud);if (crosshair) {/* the crosshair (icons.png's, light) */uint8_t *m = scr + (SCREEN_H / 2) * SCREEN_W + SCREEN_W / 2;diff --git a/games/numblocks/ce/src/draw.h b/games/numblocks/ce/src/draw.hindex c4ba17ed..eb6e0ddf 100644--- a/games/numblocks/ce/src/draw.h+++ b/games/numblocks/ce/src/draw.h@@ -30,10 +30,10 @@ void button(int x, int y, int w, int state, const char *label); /* state 0 offvoid dirt_background(int y0, int y1);/* the HUD: drawn into its own layer, laid over the 3D view as it goes to the screen */-#define HUD_X 66-#define HUD_W 188+#define HUD_X 67+#define HUD_W 187#define HUD_Y 180#define HUD_H (SCREEN_H - HUD_Y)-extern uint8_t hud_layer[HUD_W * HUD_H];-void compose(const uint8_t *src, bool hud, bool crosshair);+extern uint8_t *hud_layer; /* HUD_W x HUD_H (in the CE's screen memory, after the world) */+void compose(bool hud, bool crosshair); /* the 3D view (rbuf) onto the screen, with the HUD and the crosshair */#endifdiff --git a/games/numblocks/ce/src/gui.c b/games/numblocks/ce/src/gui.cindex 54afdad4..3f8c9b9a 100644--- a/games/numblocks/ce/src/gui.c+++ b/games/numblocks/ce/src/gui.c@@ -66,7 +66,7 @@ void hud_draw(void) {if (s == sig && !hud_dirty) return;sig = s, hud_dirty = false;cv = (Canvas){hud_layer, HUD_W, HUD_X, HUD_Y, HUD_W, HUD_H};- memset(hud_layer, 0, sizeof hud_layer);+ memset(hud_layer, 0, HUD_W * HUD_H);int x0 = SCREEN_W / 2 - 91, yb = SCREEN_H - 22;sprite(SP_HOTBAR, x0, yb);sprite(SP_HOTBAR_SEL, x0 - 1 + pl.slot * 20, yb - 1);diff --git a/games/numblocks/ce/src/main.c b/games/numblocks/ce/src/main.cindex ade8188f..e86b5e36 100644--- a/games/numblocks/ce/src/main.c+++ b/games/numblocks/ce/src/main.c@@ -42,7 +42,7 @@ static void title_backdrop(void) {world_time = 3000;render_sky_palette();render_title(0x3000);- compose(rbuf, false, false);+ compose(false, false);}static void to_title(void) {@@ -67,7 +67,7 @@ static void start_game(void) {static const uint8_t one = 1;plat_save("NBCEOPT", &one, 1, 0, 0);render_frame();- compose(rbuf, false, false);+ compose(false, false);gui_open(GUI_CONTROLS);}}@@ -167,7 +167,7 @@ static void play(uint32_t keys, uint32_t pressed) {}render_frame();hud_draw();- compose(rbuf, true, true);+ compose(true, true);}}@@ -175,8 +175,83 @@ int main(void) {plat_init();wblk = plat_spare();wlight = wblk + WS * WS * WH;+ hud_layer = wblk + 2 * WS * WS * WH;render_init();canvas_screen();+#ifdef NB_BENCH+#include <stdio.h>+ {+ /* (timing, built with -DNB_BENCH: each part's ms x 10, as text in the appvar NBBENCH) */+ static char out[400];+ int n = 0;+ uint32_t t = plat_ms();+#define LAP(name) \+ do { \+ uint32_t now = plat_ms(); \+ n += sprintf(out + n, "%s %lu\n", name, (unsigned long)(now - t)); \+ t = plat_ms(); \+ } while (0)+#if NB_BENCH == 3+ {+ /* the arithmetic's costs: 3000 of each (volatile: not folded away) */+ static volatile int a = 12345, b = 77, r;+ static volatile int32_t la = 1234567, lb = 777, lr;+ for (int i = 0; i < 3000; i++) r = a + b;+ LAP("loop");+ for (int i = 0; i < 3000; i++) r = a * b;+ LAP("imul");+ for (int i = 0; i < 3000; i++) r = a / b;+ LAP("idiv");+ for (int i = 0; i < 3000; i++) r = a >> 5;+ LAP("ishr5");+ for (int i = 0; i < 3000; i++) r = a & 4095;+ LAP("iand");+ for (int i = 0; i < 3000; i++) lr = la * lb;+ LAP("lmul");+ for (int i = 0; i < 3000; i++) lr = la / lb;+ LAP("ldiv");+ for (int i = 0; i < 3000; i++) lr = la >> 12;+ LAP("lshr12");+ static volatile uint8_t ba = 200, bb = 3, br;+ for (int i = 0; i < 3000; i++) br = (uint8_t)(ba & 0xF0);+ LAP("band");+ for (int i = 0; i < 3000; i++) br = (uint8_t)(ba * bb);+ LAP("bmul");+ static volatile int16_t sa = 1234, sb = 56;+ static volatile int32_t sr;+ for (int i = 0; i < 3000; i++) sr = (int32_t)sa * sb;+ LAP("s16mul32");+ plat_save("NBBENCH", out, (unsigned)n, 0, 0);+ plat_exit();+ }+#endif+ strcpy(cur_name, "Bench");+ new_world(1, cur_name, 12345, 0);+ LAP("world");+ render_sky_palette();+ LAP("sky");+ for (int v = 0; v < (NB_BENCH > 1 ? 24 : 4); v++) {+ pl.yaw = (uint16_t)((v & 3) * 16384);+ pl.pitch = (v & 3) == 3 ? 6000 : 0;+ render_frame();+ LAP("render");+ hud_draw();+ LAP("hud");+ compose(true, true);+ LAP("compose");+ }+ pl.x += FX1 * 4;+ world_center(FXI(pl.x), FXI(pl.y), FXI(pl.z), false);+ world_step();+ LAP("row");+ world_step();+ LAP("row");+ player_tick(K_FWD, 0);+ LAP("tick");+ plat_save("NBBENCH", out, (unsigned)n, 0, 0);+ plat_exit();+ }+#endif#ifdef HOST/* (tests: straight into a new world, NB_NEW=seed,mode[,x,y,z,yaw,pitch,time]) */extern bool host_start(uint32_t *seed, int *mode, int *at);diff --git a/games/numblocks/ce/src/nb.h b/games/numblocks/ce/src/nb.hindex a6cb86d1..9dfdea25 100644--- a/games/numblocks/ce/src/nb.h+++ b/games/numblocks/ce/src/nb.h@@ -29,7 +29,9 @@ extern uint8_t *wblk; /* blocks [y & 31][z & 31][x & 31] */extern uint8_t *wlight; /* light: sky (low 4 bits) | block (high 4 bits) */extern uint32_t wexp[WS * WS]; /* per column: the cells with a face to draw (bit y & 31) */extern uint8_t wtop[WS * WS]; /* per column: the lowest y the sky shines down to (in the window) */-extern uint8_t wbiome[WS * WS];+typedef struct { uint8_t h, biome, tf; } CCol; /* a window column's ground (gen.c's Column, packed) */+extern CCol wcol[WS * WS];+#define wbiome(ci) (wcol[ci].biome)extern int win_x, win_z, win_y; /* the world position of the window's low corner */#define WI(x, y, z) ((((unsigned)(y) & 31) << 10) | (((unsigned)(z) & 31) << 5) | ((unsigned)(x) & 31))#define CI(x, z) ((((unsigned)(z) & 31) << 5) | ((unsigned)(x) & 31))@@ -105,6 +107,7 @@ extern uint8_t *rbuf; /* the picture, RW x RH */void render_init(void);void render_frame(void); /* from the player's eyes */void render_title(uint16_t yaw); /* the title's backdrop: the world turning */+void render_show(bool hud); /* the picture onto the screen at 2x, the sky in it (and the HUD) */void render_sky_palette(void); /* the sky's colours and the light, for the time of day */extern uint32_t world_time; /* ticks: 24000 a day, 0 sunrise */extern int view_dist; /* blocks seen */@@ -135,7 +138,7 @@ typedef struct {int16_t burn, cook, burn_max;Stack s[27]; /* a chest's 27; a furnace's input, fuel, output */} Tile;-#define MAX_TILES 24+#define MAX_TILES 16extern Tile tiles[MAX_TILES];extern int n_tiles;Tile *tile_at(int x, int y, int z);diff --git a/games/numblocks/ce/src/plat_ce.c b/games/numblocks/ce/src/plat_ce.cindex 59cd0cb1..59c51beb 100644--- a/games/numblocks/ce/src/plat_ce.c+++ b/games/numblocks/ce/src/plat_ce.c@@ -121,7 +121,8 @@ uint32_t plat_keys(void) {}uint32_t plat_ms(void) { return (uint32_t)clock() * 125 >> 12; } /* (32768 a second) */-void plat_frame(void) {}+/* (a frame is done: tools/ce_emu counts frames here) */+__attribute__((noinline, used)) void plat_frame(void) { __asm__ volatile("" ::: "memory"); }uint8_t *plat_spare(void) { return (uint8_t *)lcd_Ram + SCREEN_W * SCREEN_H; }/* the edit log's memory: a RAM appvar, made once as big as it will be (resizing one loses its data) */diff --git a/games/numblocks/ce/src/render.c b/games/numblocks/ce/src/render.cindex e94f3358..a0ecba16 100644--- a/games/numblocks/ce/src/render.c+++ b/games/numblocks/ce/src/render.c@@ -4,8 +4,8 @@* eye: in that order nothing can hide what came before it), and in each column the cells with a face to draw* (wexp) outward from the eye's level. A face is projected, clipped to the screen and filled a row of pixels* at a time with its texture, mapped linearly across it (faces near the eye are cut into smaller ones, so the- * mapping stays true). A bit per pixel says which pixels are drawn: each is drawn once, and a cell whose- * square on screen is all drawn is passed by. What is left at the end is sky.+ * mapping stays true). A pixel still 0 is not drawn yet: each is drawn once, and a cell whose square on screen is+ * all drawn is passed by. What is left at the end is sky. The loops that fill are in rs.s (eZ80 assembly).** Like Minecraft 1.8 with Fast graphics and smooth lighting off: a face is lit by the light of the cell in* front of it, times its side's shade (top 1.0, north and south 0.8, east and west 0.6, bottom 0.5); grass and@@ -13,24 +13,21 @@* behind show through their see-through texels; fog fades the far blocks to the sky's horizon. The palette* does the shading: a texel's colour goes through a row of shade_cm (32 levels of light), then a fog row. */#include "nb.h"+#include "draw.h"+#include "rs.h"#ifndef NB_NO_RBUFstatic uint8_t rbuf_mem[RW * RH];uint8_t *rbuf = rbuf_mem;#endif-static uint8_t cov[RH][RW / 8]; /* 1: drawn */-static int px_left; /* pixels not drawn yet */uint32_t stat_faces, stat_spans, stat_px, stat_cells;+#ifdef HOST+extern unsigned long st_cols, st_list, st_frus, st_cov, st_culled;+#endifint view_dist = 13;uint32_t world_time;int crack_stage = -1;-#define C_RED_DEBUG (FIRST_STATIC + 13)-int render_debug; /* (tests: 1 draws every cell, past the occlusion test; 2 the sky red; 4 probe) */-#ifdef HOST-#include <stdio.h>-int probe_px, probe_py;-static int probe_x, probe_y, probe_z, probe_b, probe_f, probe_t;-#endif+const uint8_t *rs_recip, *rs_ratio;/* ---------------------------------------------------------------- the camera */#define Q 14@@ -45,10 +42,10 @@ static int bx0, by0, bz0; /* the camera-space position of the eyestatic uint8_t lvl_sky[16], lvl_blk[16]; /* shade level of each light level */static const uint8_t face_off[6] = {6, 0, 2, 2, 4, 4}; /* -Y +Y -Z +Z -X +X: 0.5 1.0 0.8 0.8 0.6 0.6 */static uint8_t fog_cm[8][256]; /* fog rows: 1/9 to 8/9 of the way to the fog's colour */+#define FC_N 8+static uint16_t fc_key[FC_N]; /* the shade and fog rows made into one (shade_of) */static uint8_t fog_rgb[3], sky_rgb[3];static int fog_q; /* the fog's colour as last made into fog_cm */-static int fog0_2, fog_step; /* fog: from fog0 (squared, 1/32 blocks) in 8 steps to view_dist */-static uint8_t crack_tex[256]; /* the mined block's texture with its cracks */static int crack_x, crack_y, crack_z;int16_t fsin(uint16_t a) {@@ -96,16 +93,18 @@ static int level_of(int32_t b) {return k;}-static uint16_t lerp_pal(int c, const uint8_t *to, int t9) {- /* (a colour t9 ninths of the way to `to`, as the nearest of the cube) */- int r = pal_rgb[c * 3], g = pal_rgb[c * 3 + 1], b = pal_rgb[c * 3 + 2];- r += (to[0] - r) * t9 / 9, g += (to[1] - g) * t9 / 9, b += (to[2] - b) * t9 / 9;- /* the cube's levels: (i / 15) ^ 1.8 */- int ri = 0, gi = 0, bi = 0;- while (ri < 15 && (cube_levels[ri] + cube_levels[ri + 1]) / 2 < r) ri++;- while (gi < 15 && (cube_levels[gi] + cube_levels[gi + 1]) / 2 < g) gi++;- while (bi < 15 && (cube_levels[bi] + cube_levels[bi + 1]) / 2 < b) bi++;- return pal_cube[ri << 8 | gi << 4 | bi];+/* the cube's level (0..15) nearest each value 0..255 */+static uint8_t cube_of[256];+static uint8_t lerp_pal(int c, const uint8_t *to, uint8_t w) {+ /* (a colour w / 256 of the way to `to`, as the nearest of the cube) */+ const uint8_t *p = pal_rgb + c * 3;+ uint8_t k[3];+ for (int i = 0; i < 3; i++) {+ uint8_t a = p[i], b = to[i];+ k[i] = b >= a ? (uint8_t)(a + (uint8_t)((uint16_t)(uint8_t)(b - a) * w >> 8))+ : (uint8_t)(a - (uint8_t)((uint16_t)(uint8_t)(a - b) * w >> 8));+ }+ return pal_cube[cube_of[k[0]] << 8 | cube_of[k[1]] << 4 | cube_of[k[2]]];}/* celestial angle (World.calculateCelestialAngle), 0..65535: 0 noon */@@ -155,82 +154,67 @@ void render_sky_palette(void) {fog_q = qf;for (int f = 0; f < 8; f++) {fog_cm[f][0] = 0;- for (int i = 1; i < 256; i++) fog_cm[f][i] = i < FIRST_STATIC ? (uint8_t)i : (uint8_t)lerp_pal(i, fog_rgb, f + 1);+ uint8_t w = (uint8_t)((f + 1) * 256 / 9);+ for (int i = 1; i < 256; i++) fog_cm[f][i] = i < FIRST_STATIC ? (uint8_t)i : lerp_pal(i, fog_rgb, w);}+ memset(fc_key, 255, sizeof fc_key);}}-/* ---------------------------------------------------------------- filling a polygon */-/* (the eZ80's int has 24 bits: what is drawn stays in them; 32-bit only where products need it) */-typedef struct { int x, y, u, v; } SV; /* screen x, y in 1/16 pixels; u, v in 1/256 texels */-typedef struct { int x, y, z, u, v; } CV; /* camera space, Q14; u, v */--static const uint8_t *s_tex, *s_cm, *s_fog;-static bool s_alpha;+/* ---------------------------------------------------------------- textures and shades in RAM, 256-aligned */+/* (rs_poly's loop reads a texel and its shade by setting a page's low byte: both tables 256-byte aligned) */+#define TC_N 32+static uint8_t tcache[TC_N][256] __attribute__((aligned(256)));+static uint8_t shade_ram[N_SHADE][256] __attribute__((aligned(256)));+static uint8_t fogc[FC_N][256] __attribute__((aligned(256)));+static uint8_t crack_tex[256] __attribute__((aligned(256)));+static uint8_t tc_slot[NTEX], tc_tex[TC_N], tc_used[TC_N], tc_next, frame_no;+static uint8_t fc_next;++static void caches_reset(void) {+ memset(tc_slot, 255, sizeof tc_slot);+ memset(tc_tex, 255, sizeof tc_tex);+ memset(fc_key, 255, sizeof fc_key);+}-/* a row of pixels [x0, x1) at row y, those not drawn yet, from texel u, v (1/65536 texels) stepping du, dv */-static void span(int y, int x0, int x1, int u, int v, int du, int dv) {- uint8_t *c = cov[y], *d = rbuf + y * RW;- stat_spans++;-#ifdef HOST- if ((render_debug & 4) && y == probe_py && probe_px >= x0 && probe_px < x1 && !(c[probe_px >> 3] >> (probe_px & 7) & 1))- fprintf(stderr, "probe: cell %d %d %d block %d face %d tex %d\n", probe_x, probe_y, probe_z, probe_b, probe_f, probe_t);-#endif- int x = x0;- while (x < x1) {- int byte = x >> 3, bit = x & 7;- int end = (byte + 1) << 3;- if (end > x1) end = x1;- unsigned m = c[byte];- unsigned want = (0xFFu << bit) & (0xFFu >> (8 - (end - (byte << 3))));- int n = end - x;- if ((m & want) == want) { /* all drawn */- while (n--) u += du, v += dv;- x = end;- continue;+/* texture t, in RAM */+static const uint8_t *tex_of(int t) {+ uint8_t s = tc_slot[t];+ if (s == 255) {+ /* a slot not used in this frame (round and round) */+ for (int n = 0; n < TC_N; n++) {+ s = (uint8_t)(tc_next++ & (TC_N - 1));+ if (tc_used[s] != frame_no) break;}- if (!(m & want) && !s_alpha) { /* none drawn: the whole run */- c[byte] = (uint8_t)(m | want);- px_left -= n;- stat_px += n;- uint8_t *p = d + x;- if (s_fog)- while (n--) {- *p++ = s_fog[s_cm[s_tex[((v >> 12) & 0xF0) | ((u >> 16) & 15)]]];- u += du, v += dv;- }- else- while (n--) {- *p++ = s_cm[s_tex[((v >> 12) & 0xF0) | ((u >> 16) & 15)]];- u += du, v += dv;- }- x = end;- continue;- }- for (; x < end; x++, u += du, v += dv) {- unsigned b = 1u << (x & 7);- if (m & b) continue;- int t = s_tex[((v >> 12) & 0xF0) | ((u >> 16) & 15)];- if (s_alpha && !t) continue;- m |= b;- px_left--;- stat_px++;- d[x] = s_fog ? s_fog[s_cm[t]] : s_cm[t];- }- c[byte] = (uint8_t)m;+ if (tc_tex[s] != 255) tc_slot[tc_tex[s]] = 255;+ tc_tex[s] = (uint8_t)t, tc_slot[t] = s;+ memcpy(tcache[s], tex_px[t], 256);}+ tc_used[s] = frame_no;+ return tcache[s];}-/* n x 4096 / d (32-bit arithmetic, the most the eZ80 does) */-static __attribute__((noinline)) int muldiv(int32_t n, int32_t d) {- int s = 12;- int32_t an = n < 0 ? -n : n;- while (s && an < (1L << 29)) an <<= 1, n <<= 1, s--;- while (s) d >>= 1, s--;- return d ? (int)(n / d) : 0;+/* the shade row of a level (0..31), through fog (1..8 eighths of the way, 0 none) */+static const uint8_t *shade_of(int level, int fog) {+ if (!fog) return shade_ram[level];+ uint16_t key = (uint16_t)(level << 4 | fog);+ for (int i = 0; i < FC_N; i++)+ if (fc_key[i] == key) return fogc[i];+ uint8_t s = (uint8_t)(fc_next++ & (FC_N - 1));+ fc_key[s] = key;+ const uint8_t *a = shade_ram[level], *f = fog_cm[fog - 1];+ uint8_t *o = fogc[s];+ for (int i = 0; i < 256; i++) o[i] = f[a[i]];+ return o;}-/* a + (b - a) x t / 4096 */-static __attribute__((noinline)) int lerp12(int a, int b, int t) { return a + (int)((int32_t)(b - a) * t >> 12); }++/* ---------------------------------------------------------------- faces */+static const uint8_t *s_tex, *s_cm;+static uint8_t s_alpha;++typedef struct { int x, y, z, u, v; } CV; /* camera space, Q14; u, v the texel x 4096 */++static int lerp12(int a, int b, int t) { return a + (int)((int32_t)(b - a) * t >> 12); }/* clipping in 2D against x >= 0 (k 0), x <= W (1), y >= 0 (2), y <= H (3) */static int clip2(SV *in, int n, SV *out, int k) {@@ -259,9 +243,6 @@ static int clip2(SV *in, int n, SV *out, int k) {return m;}-/* fills a convex polygon (any winding) with its texture, mapped linearly */-static int16_t row_xl[RH], row_xr[RH];-static int row_u[RH], row_v[RH];static void raster(SV *p, int n) {static SV buf_a[16], buf_b[16];int xmin = p[0].x, xmax = p[0].x, ymin = p[0].y, ymax = p[0].y;@@ -280,99 +261,27 @@ static void raster(SV *p, int n) {if (ymax > RH * 16 && n) n = clip2(a, n, b, 3), a = b, b = a == buf_a ? buf_b : buf_a;if (n < 3) return;p = a;- ymin = ymax = p[0].y;- for (int i = 1; i < n; i++) {- if (p[i].y < ymin) ymin = p[i].y;- if (p[i].y > ymax) ymax = p[i].y;- }- }- int y0 = (ymin + 7) >> 4, y1 = (ymax + 7) >> 4; /* rows whose middle is inside */- if (y0 < 0) y0 = 0;- if (y1 > RH) y1 = RH;- if (y0 >= y1) return;- /* the texture's gradient: from the largest triangle of the polygon's first vertex */- int32_t det = 0;- int ia = 1, ib = 2;- for (int i = 1; i + 1 < n; i++) {- int32_t d = (int32_t)(p[i].x - p[0].x) * (p[i + 1].y - p[0].y) - (int32_t)(p[i + 1].x - p[0].x) * (p[i].y - p[0].y);- if ((d < 0 ? -d : d) > (det < 0 ? -det : det)) det = d, ia = i, ib = i + 1;}- if (det > -64 && det < 64) return; /* (a sliver of less than a pixel) */- int ax = p[ia].x - p[0].x, ay = p[ia].y - p[0].y, bx = p[ib].x - p[0].x, by = p[ib].y - p[0].y;- int au = p[ia].u - p[0].u, bu = p[ib].u - p[0].u, av = p[ia].v - p[0].v, bv = p[ib].v - p[0].v;- /* d/dx and d/dy per pixel, in 1/65536 texels */- int dudx = muldiv((int32_t)au * by - (int32_t)bu * ay, det), dudy = muldiv((int32_t)bu * ax - (int32_t)au * bx, det);- int dvdx = muldiv((int32_t)av * by - (int32_t)bv * ay, det), dvdy = muldiv((int32_t)bv * ax - (int32_t)av * bx, det);- /* k/16 of a pixel's step, to step from an edge to the next pixel's middle */- static int ku[16], kv[16];- for (int k = 0, a = 0, b = 0; k < 16; k++, a += dudx >> 4, b += dvdx >> 4) ku[k] = a, kv[k] = b;- /* the edges: walk the outline once, noting each row's left and right x, and the texel at the left one */- for (int y = y0; y < y1; y++) row_xl[y] = 32767, row_xr[y] = -32768;- for (int i = 0; i < n; i++) {- const SV *a = &p[i], *b = &p[i + 1 == n ? 0 : i + 1];- if (a->y == b->y) continue;- if (a->y > b->y) {- const SV *t = a;- a = b, b = t;- }- int ya = (a->y + 7) >> 4, yb = (b->y + 7) >> 4;- if (ya < y0) ya = y0;- if (yb > y1) yb = y1;- if (ya >= yb) continue;- /* x in 1/1024 pixels, a row's step; u, v at the edge, their step */- int step = (int)(((int32_t)(b->x - a->x) << 10) / (b->y - a->y));- int dy0 = (ya << 4) + 8 - a->y;- int x = (a->x << 6) + (int)((int32_t)step * dy0 >> 4);- int ex = (x >> 6) - a->x; /* the edge's x at the first row, from the vertex (1/16 px) */- int u = (a->u << 8) + (int)(((int32_t)dudx * ex + (int32_t)dudy * dy0) >> 4);- int v = (a->v << 8) + (int)(((int32_t)dvdx * ex + (int32_t)dvdy * dy0) >> 4);- int su = dudy + (int)((int32_t)dudx * step >> 10), sv = dvdy + (int)((int32_t)dvdx * step >> 10);- for (int y = ya; y < yb; y++, x += step, u += su, v += sv) {- int px = (x + (7 << 6)) >> 10; /* the first pixel whose middle is right of the edge */- if (px < row_xl[y]) {- row_xl[y] = (int16_t)px;- int f = (px << 4) + 8 - (x >> 6); /* from the edge to its middle: 0..15 sixteenths */- row_u[y] = u + ku[f & 15], row_v[y] = v + kv[f & 15];- }- if (px > row_xr[y]) row_xr[y] = (int16_t)px;- }- }- for (int y = y0; y < y1; y++) {- int a = row_xl[y], b = row_xr[y];- int u = row_u[y], v = row_v[y];- if (a < 0) u -= dudx * a, v -= dvdx * a, a = 0;- if (b > RW) b = RW;- if (a >= b) continue;- span(y, a, b, u, v, dudx, dvdx);- }-}--/* ---------------------------------------------------------------- faces */-/* projects a camera-space vertex: x, y in 1/16 pixels */-static void project(const CV *c, SV *s) {- int32_t inv = (F16 << 18) / c->z;- s->x = RW * 8 + (int)((c->x * inv) >> 18);- s->y = RH * 8 - (int)((c->y * inv) >> 18);- s->u = c->u, s->v = c->v;+ stat_spans++;+ rs_poly(p, (uint8_t)n);}-/* clips a camera-space polygon to z >= NEAR and the guard planes |x|, |y| <= 4z */+/* clips a camera-space polygon to z >= NEAR and the guard planes |x|, |y| <= 3z */static int clip3(CV *in, int n, CV *out, int k) {int m = 0;for (int i = 0; i < n; i++) {CV *a = &in[i], *b = &in[i + 1 == n ? 0 : i + 1];- int da, db;+ int za = a->z + a->z + a->z, zb = b->z + b->z + b->z, da, db;switch (k) {case 0: da = a->z - NEAR, db = b->z - NEAR; break;- case 1: da = 4 * a->z - a->x, db = 4 * b->z - b->x; break;- case 2: da = 4 * a->z + a->x, db = 4 * b->z + b->x; break;- case 3: da = 4 * a->z - a->y, db = 4 * b->z - b->y; break;- default: da = 4 * a->z + a->y, db = 4 * b->z + b->y;+ case 1: da = za - a->x, db = zb - b->x; break;+ case 2: da = za + a->x, db = zb + b->x; break;+ case 3: da = za - a->y, db = zb - b->y; break;+ default: da = za + a->y, db = zb + b->y;}if (da >= 0) out[m++] = *a;if ((da >= 0) != (db >= 0)) {- /* (where it crosses, 0..4096: both brought under 2^18 first) */- int32_t dd = da - db, aa = da;+ int32_t dd = (int32_t)da - db, aa = da;while (dd > 262143 || dd < -262143) dd >>= 1, aa >>= 1;int t = (int)((aa << 12) / dd);CV *o = &out[m++];@@ -384,95 +293,168 @@ static int clip3(CV *in, int n, CV *out, int k) {return m;}+/* (the general way: clipped to the near plane and the guard band, then projected; near faces, edges of the view) */static void draw_quad(CV *q, int n) {- bool far = true;- for (int i = 0; i < n; i++) {- int z4 = 4 * q[i].z;- if (q[i].z < NEAR || q[i].x > z4 || -q[i].x > z4 || q[i].y > z4 || -q[i].y > z4) far = false;- }static CV ca[16], cb[16];- if (!far) {- CV *a = q, *b = ca;- for (int k = 0; k < 5 && n >= 3; k++) {- n = clip3(a, n, b, k);- a = b, b = a == ca ? cb : ca;- }- if (n < 3) return;- q = a;+ CV *a = q, *b = ca;+ for (int k = 0; k < 5 && n >= 3; k++) {+ n = clip3(a, n, b, k);+ a = b, b = a == ca ? cb : ca;}+ if (n < 3) return;static SV s[16];- for (int i = 0; i < n; i++) project(&q[i], &s[i]);+ int o[2];+ for (int i = 0; i < n; i++) {+ rs_proj(a[i].x, a[i].y, a[i].z, o);+ s[i].x = o[0], s[i].y = o[1], s[i].u = a[i].u, s[i].v = a[i].v;+ }raster(s, n);}-/* a face given by its four corners (camera space) and texture corners; near faces are cut in 2 x 2 or 4 x 4 */-static void face4(const int (*c)[3], int split) {+/* The face being drawn: its corners in camera space (Q14), and on the screen. (Only constant indices below: on+ * the eZ80 an index into ints is a multiplication, which is a call. So are shifts, negations, masks.) */+static int fq[12];+static SV fs[4] = {{0, 0, 0, 0}, {0, 0, 65535, 0}, {0, 0, 65535, 65535}, {0, 0, 0, 65535}};++static bool fq_far(int x, int y, int z) {+ /* in front, and well inside the guard band (|x|, |y| < 4z) */+ if (z < NEAR + NEAR) return false;+ int z4 = z + z;+ z4 += z4;+ return x < z4 && x + z4 > 0 && y < z4 && y + z4 > 0;+}++/* four projected corners: drawn, cut to the screen when they are not all on it */+static void poly4(SV *v) {+ if (v[0].x >= 0 && v[1].x >= 0 && v[2].x >= 0 && v[3].x >= 0 && v[0].x <= RW * 16 && v[1].x <= RW * 16 &&+ v[2].x <= RW * 16 && v[3].x <= RW * 16 && v[0].y >= 0 && v[1].y >= 0 && v[2].y >= 0 && v[3].y >= 0 &&+ v[0].y <= RH * 16 && v[1].y <= RH * 16 && v[2].y <= RH * 16 && v[3].y <= RH * 16) {+ stat_spans++;+ rs_poly(v, 4);+ } else+ raster(v, 4);+}++/* a near face's grid of (split + 1) x (split + 1) points: camera space, projected when far enough */+static CV gp[25];+static SV gq[25];+static uint8_t gfar[25];+static const uint16_t GUV2[3] = {0, 32768, 65535}, GUV4[5] = {0, 16384, 32768, 49152, 65535};++/* the face in fq, split in split x split parts (1: whole; near faces 2 or 4, as the texture is not drawn in+ * perspective) */+static void quad(uint8_t split) {stat_faces++;- static const int uv[4][2] = {{0, 0}, {4096, 0}, {4096, 4096}, {0, 4096}};- if (split <= 1) {- CV q[4];- for (int i = 0; i < 4; i++) q[i] = (CV){c[i][0], c[i][1], c[i][2], uv[i][0], uv[i][1]};+ rs_setup(s_tex, s_cm, s_alpha);+ if (split == 1) {+ if (fq_far(fq[0], fq[1], fq[2]) && fq_far(fq[3], fq[4], fq[5]) && fq_far(fq[6], fq[7], fq[8]) &&+ fq_far(fq[9], fq[10], fq[11])) {+ rs_proj(fq[0], fq[1], fq[2], &fs[0].x);+ rs_proj(fq[3], fq[4], fq[5], &fs[1].x);+ rs_proj(fq[6], fq[7], fq[8], &fs[2].x);+ rs_proj(fq[9], fq[10], fq[11], &fs[3].x);+ poly4(fs);+ return;+ }+ static CV q[4] = {{0, 0, 0, 0, 0}, {0, 0, 0, 65535, 0}, {0, 0, 0, 65535, 65535}, {0, 0, 0, 0, 65535}};+ q[0].x = fq[0], q[0].y = fq[1], q[0].z = fq[2], q[1].x = fq[3], q[1].y = fq[4], q[1].z = fq[5];+ q[2].x = fq[6], q[2].y = fq[7], q[2].z = fq[8], q[3].x = fq[9], q[3].y = fq[10], q[3].z = fq[11];draw_quad(q, 4);return;}- /* corners: c0 + s * (c1 - c0) + t * (c3 - c0) */- int e1[3], e3[3];- for (int a = 0; a < 3; a++) e1[a] = (c[1][a] - c[0][a]) / split, e3[a] = (c[3][a] - c[0][a]) / split;- for (int j = 0; j < split; j++)- for (int i = 0; i < split; i++) {- CV q[4];- static const int8_t o[4][2] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};- for (int k = 0; k < 4; k++) {- int s = i + o[k][0], t = j + o[k][1];- /* (the far edges exactly on the face's: no cracks between the parts and the faces around) */- for (int a = 0; a < 3; a++) {- int v = s == split ? c[1][a] + t * e3[a] : c[0][a] + s * e1[a] + t * e3[a];- if (t == split) v = s == split ? c[2][a] : c[3][a] + s * e1[a];- if (a == 0) q[k].x = v;- else if (a == 1) q[k].y = v;- else q[k].z = v;- }- q[k].u = 4096 * s / split, q[k].v = 4096 * t / split;+ /* the grid: rows from c0 + t e3 to c1 + t e3, the last from c3 to c2 (the face's edges exact: no cracks) */+ int e1x = fq[3] - fq[0], e1y = fq[4] - fq[1], e1z = fq[5] - fq[2];+ int e3x = fq[9] - fq[0], e3y = fq[10] - fq[1], e3z = fq[11] - fq[2];+ e1x >>= 1, e1y >>= 1, e1z >>= 1, e3x >>= 1, e3y >>= 1, e3z >>= 1;+ if (split == 4) e1x >>= 1, e1y >>= 1, e1z >>= 1, e3x >>= 1, e3y >>= 1, e3z >>= 1;+ const uint16_t *uvt = split == 2 ? GUV2 : GUV4;+ CV *p = gp;+ SV *q = gq;+ uint8_t *fa = gfar;+ int rx = fq[0], ry = fq[1], rz = fq[2], ex = fq[3], ey = fq[4], ez = fq[5];+ for (uint8_t t = 0; t <= split; t++) {+ if (t == split) rx = fq[9], ry = fq[10], rz = fq[11], ex = fq[6], ey = fq[7], ez = fq[8];+ int x = rx, y = ry, z = rz;+ uint16_t v = uvt[t];+ for (uint8_t s = 0; s <= split; s++) {+ if (s == split) x = ex, y = ey, z = ez;+ p->x = x, p->y = y, p->z = z, p->u = uvt[s], p->v = v;+ if ((*fa = fq_far(x, y, z))) {+ rs_proj(x, y, z, &q->x);+ q->u = p->u, q->v = v;}- draw_quad(q, 4);+ p++, q++, fa++;+ x += e1x, y += e1y, z += e1z;}+ rx += e3x, ry += e3y, rz += e3z, ex += e3x, ey += e3y, ez += e3z;+ }+ /* its parts: corners (s, t) (s + 1, t) (s + 1, t + 1) (s, t + 1) */+ uint8_t w = (uint8_t)(split + 1);+ CV *a = gp, *b = gp + w;+ SV *qa = gq, *qb = gq + w;+ uint8_t *f0 = gfar, *f1 = gfar + w;+ for (uint8_t t = 0; t < split; t++) {+ for (uint8_t s = 0; s < split; s++) {+ if (f0[0] && f0[1] && f1[0] && f1[1]) {+ static SV v4[4];+ v4[0] = qa[0], v4[1] = qa[1], v4[2] = qb[1], v4[3] = qb[0];+ poly4(v4);+ } else if (a[0].z >= NEAR || a[1].z >= NEAR || b[0].z >= NEAR || b[1].z >= NEAR) {+ static CV c4[4];+ c4[0] = a[0], c4[1] = a[1], c4[2] = b[1], c4[3] = b[0];+ draw_quad(c4, 4);+ }+ a++, b++, qa++, qb++, f0++, f1++;+ }+ a++, b++, qa++, qb++, f0++, f1++;+ }}-/* each face's corners as offsets of the cell's low corner: x, y, z bits (top-left, top-right, bottom-right,- * bottom-left, as seen from outside) */-static const uint8_t FACE_C[6][4] = {- {0x4, 0x5, 0x1, 0x0}, /* -Y: (0,0,1) (1,0,1) (1,0,0) (0,0,0) */- {0x2, 0x3, 0x7, 0x6}, /* +Y: (0,1,0) (1,1,0) (1,1,1) (0,1,1) */- {0x3, 0x2, 0x0, 0x1}, /* -Z: (1,1,0) (0,1,0) (0,0,0) (1,0,0) */- {0x6, 0x7, 0x5, 0x4}, /* +Z: (0,1,1) (1,1,1) (1,0,1) (0,0,1) */- {0x2, 0x6, 0x4, 0x0}, /* -X: (0,1,0) (0,1,1) (0,0,1) (0,0,0) */- {0x7, 0x3, 0x1, 0x5}, /* +X: (1,1,1) (1,1,0) (1,0,0) (1,0,1) */-};-static const int8_t FACE_D[6][3] = {{0, -1, 0}, {0, 1, 0}, {0, 0, -1}, {0, 0, 1}, {-1, 0, 0}, {1, 0, 0}};-/* the cross of plants: two diagonal quads */-static const uint8_t CROSS_C[2][4] = {{0x2, 0x7, 0x5, 0x0}, {0x6, 0x3, 0x1, 0x4}};--static int corner[8][3]; /* the cell's corners, camera space */+/* the cell's corners, camera space: corner k (bits x 1, y 2, z 4) at ck[3k..3k+2] */+static int ck[24];static void corners(int x, int y, int z) {- for (int k = 0; k < 8; k++) {- int cx = x, cy = y, cz = z;- if (k & 1) cx += rxx, cy += uxx, cz += fxx;- if (k & 2) cy += uxy, cz += fxy;- if (k & 4) cx += rxz, cy += uxz, cz += fxz;- corner[k][0] = cx, corner[k][1] = cy, corner[k][2] = cz;+ ck[0] = x, ck[1] = y, ck[2] = z;+ ck[3] = x + rxx, ck[4] = y + uxx, ck[5] = z + fxx;+ ck[6] = x, ck[7] = y + uxy, ck[8] = z + fxy;+ ck[9] = ck[3], ck[10] = ck[4] + uxy, ck[11] = ck[5] + fxy;+ ck[12] = x + rxz, ck[13] = y + uxz, ck[14] = z + fxz;+ ck[15] = ck[3] + rxz, ck[16] = ck[4] + uxz, ck[17] = ck[5] + fxz;+ ck[18] = ck[12], ck[19] = ck[13] + uxy, ck[20] = ck[14] + fxy;+ ck[21] = ck[15], ck[22] = ck[16] + uxy, ck[23] = ck[17] + fxy;+}+#define FC(i, k) (fq[3 * (i)] = ck[3 * (k)], fq[3 * (i) + 1] = ck[3 * (k) + 1], fq[3 * (i) + 2] = ck[3 * (k) + 2])+/* each face's corners (top-left, top-right, bottom-right, bottom-left, as seen from outside) into fq */+static void face_corners(int f) {+ switch (f) {+ case 0: FC(0, 4), FC(1, 5), FC(2, 1), FC(3, 0); break; /* -Y */+ case 1: FC(0, 2), FC(1, 3), FC(2, 7), FC(3, 6); break; /* +Y */+ case 2: FC(0, 3), FC(1, 2), FC(2, 0), FC(3, 1); break; /* -Z */+ case 3: FC(0, 6), FC(1, 7), FC(2, 5), FC(3, 4); break; /* +Z */+ case 4: FC(0, 2), FC(1, 6), FC(2, 4), FC(3, 0); break; /* -X */+ case 5: FC(0, 7), FC(1, 3), FC(2, 1), FC(3, 5); break; /* +X */+ case 6: FC(0, 2), FC(1, 7), FC(2, 5), FC(3, 0); break; /* a plant's two crossing quads */+ default: FC(0, 6), FC(1, 3), FC(2, 1), FC(3, 4);}}+/* the faces' corners, for the outline (corner numbers) */+static const uint8_t FACE_C[6][4] = {{4, 5, 1, 0}, {2, 3, 7, 6}, {3, 2, 0, 1}, {6, 7, 5, 4}, {2, 6, 4, 0}, {7, 3, 1, 5}};+static const uint8_t CROSS_C[2][4] = {{2, 7, 5, 0}, {6, 3, 1, 4}};-static const uint8_t *face_cm(int lb, int side) {- int l = lvl_sky[lb & 15], b = lvl_blk[lb >> 4];+static int light_level(uint8_t lb, uint8_t side) {+ uint8_t l = lvl_sky[lb & 15], b = lvl_blk[lb >> 4];if (b > l) l = b;- l -= face_off[side];- return shade_cm[l < 0 ? 0 : l];+ b = face_off[side];+ return l < b ? 0 : l - b;}-static void crack_make(int tex) {- const uint8_t *t = tex_px[tex], *c = tex_px[TX_DESTROY_STAGE_0 + crack_stage];- for (int i = 0; i < 256; i++) crack_tex[i] = c[i] ? shade_cm[18][t[i]] : t[i];+static int crack_for = -1;+static const uint8_t *crack_of(int tex) {+ if (crack_for != tex * 16 + crack_stage) {+ crack_for = tex * 16 + crack_stage;+ const uint8_t *t = tex_px[tex], *c = tex_px[TX_DESTROY_STAGE_0 + crack_stage];+ for (int i = 0; i < 256; i++) crack_tex[i] = c[i] ? shade_ram[18][t[i]] : t[i];+ }+ return crack_tex;}static int tile_front(int x, int y, int z) {@@ -480,191 +462,172 @@ static int tile_front(int x, int y, int z) {return t ? t->facing : 3;}-static void cell(int x, int y, int z, int b, int X, int Y, int Z, int dist2) {- stat_cells++;- int fog = 0;- if (dist2 > fog0_2) {- fog = (int)((dist2 - fog0_2) / fog_step) + 1;- if (fog > 8) return;+/* the cell being drawn: where it is, its index in the window, its column's, its slots there */+static int c_x, c_y, c_z, c_idx, c_ci;+static uint8_t c_sx, c_sy, c_sz;++/* the neighbour across face f (-Y +Y -Z +Z -X +X): its index, or -1 past the window (below it: solid, else air) */+static int nb_index(int f) {+ switch (f) {+ case 0: return c_y - 1 < win_y ? -1 : c_sy ? c_idx - 1024 : c_idx + 31 * 1024;+ case 1: return c_y + 1 >= win_y + WH ? -1 : c_sy != 31 ? c_idx + 1024 : c_idx - 31 * 1024;+ case 2: return c_z - 1 < win_z ? -1 : c_sz ? c_idx - 32 : c_idx + 31 * 32;+ case 3: return c_z + 1 >= win_z + WS ? -1 : c_sz != 31 ? c_idx + 32 : c_idx - 31 * 32;+ case 4: return c_x - 1 < win_x ? -1 : c_sx ? c_idx - 1 : c_idx + 31;+ default: return c_x + 1 >= win_x + WS ? -1 : c_sx != 31 ? c_idx + 1 : c_idx - 31;}- s_fog = fog ? fog_cm[fog - 1] : 0;+}++static const uint8_t BIT[8] = {1, 2, 4, 8, 16, 32, 64, 128};+static uint16_t blk_tex_off[B_N]; /* b x 6 */++__attribute__((noinline)) static void cell(uint8_t b, int X, int Y, int Z, int fog) {+ stat_cells++;corners(X, Y, Z);- int zmin = corner[0][2];- for (int k = 1; k < 8; k++)- if (corner[k][2] < zmin) zmin = corner[k][2];- int split = zmin < Q1 / 2 ? 4 : zmin < 2 * Q1 ? 2 : 1;- int m = blk_model[b];- int ci = CI(x, z);+ int zmin = ck[2];+ if (ck[5] < zmin) zmin = ck[5];+ if (ck[8] < zmin) zmin = ck[8];+ if (ck[11] < zmin) zmin = ck[11];+ if (ck[14] < zmin) zmin = ck[14];+ if (ck[17] < zmin) zmin = ck[17];+ if (ck[20] < zmin) zmin = ck[20];+ if (ck[23] < zmin) zmin = ck[23];+ int split = zmin < (int)(Q1 / 2) ? 4 : zmin < (int)(2 * Q1) ? 2 : 1;+ const uint8_t *bt = &blk_tex[0][0] + blk_tex_off[b];+ uint8_t m = blk_model[b], tint = blk_tint[b];if (m == M_CROSS || m == M_TORCH) {- int tex = blk_tex[b][2];- if (blk_tint[b]) tex += tint_of_biome[wbiome[ci]];- s_tex = tex_px[tex];- s_cm = face_cm(wlight[WI(x, y, z)], 1);- s_alpha = true;-#ifdef HOST- probe_x = x, probe_y = y, probe_z = z, probe_b = b, probe_f = 9, probe_t = tex;-#endif- for (int k = 0; k < 2; k++) {- int q[4][3];- for (int i = 0; i < 4; i++) memcpy(q[i], corner[CROSS_C[k][i]], sizeof q[i]);- face4(q, split);- }- s_alpha = false;+ int tex = bt[2];+ if (tint) tex += tint_of_biome[wbiome(c_ci)];+ s_tex = tex_of(tex);+ s_cm = shade_of(light_level(wlight[c_idx], 1), fog);+ s_alpha = 1;+ face_corners(6);+ quad(split);+ face_corners(7);+ quad(split);return;}s_alpha = (blk_flags[b] & BF_ALPHA) != 0;- bool mined = crack_stage >= 0 && x == crack_x && y == crack_y && z == crack_z;- for (int f = 0; f < 6; f++) {+ bool mined = crack_stage >= 0 && c_x == crack_x && c_y == crack_y && c_z == crack_z;+ uint8_t fl = blk_flags[b];+ for (uint8_t f = 0; f < 6; f++) {/* only the faces turned to the eye */- int d = f >> 1, pos = f & 1;- int e = d == 0 ? eye_by - y : d == 1 ? eye_bz - z : eye_bx - x;- if (pos ? e <= 0 : e >= 0) continue;- int nx = x + FACE_D[f][0], ny = y + FACE_D[f][1], nz = z + FACE_D[f][2];- int nb = world_get(nx, ny, nz);- if (blk_flags[nb] & BF_OPAQUE) continue;+ int e = f < 2 ? eye_by - c_y : f < 4 ? eye_bz - c_z : eye_bx - c_x;+ if (f & 1 ? e <= 0 : e >= 0) continue;+ int n = nb_index(f);+ uint8_t nb, lb;+ if (n < 0) nb = f ? B_AIR : B_STONE, lb = f == 1 ? 15 : 0;+ else nb = wblk[n], lb = wlight[n];+ uint8_t nf = blk_flags[nb];+ if (nf & BF_OPAQUE) continue;if (nb == b) continue;- if ((blk_flags[b] & BF_LIQUID) && (blk_flags[nb] & BF_LIQUID)) continue;- int tex = blk_tex[b][f];- if (blk_tint[b] >> f & 1) tex += tint_of_biome[wbiome[ci]];+ if ((fl & BF_LIQUID) && (nf & BF_LIQUID)) continue;+ int tex = bt[f];+ if (tint & BIT[f]) tex += tint_of_biome[wbiome(c_ci)];if ((b == B_FURNACE || b == B_FURNACE_LIT || b == B_CHEST) && f >= 2) {- int fr = tile_front(x, y, z);- tex = f == fr ? blk_tex[b][3] : blk_tex[b][2];+ int fr = tile_front(c_x, c_y, c_z);+ tex = f == fr ? bt[3] : bt[2];} else if (b == B_CRAFTING_TABLE && f >= 2)- tex = f == 3 || f == 4 ? blk_tex[b][3] : blk_tex[b][2];- if (mined) {- crack_make(tex);- s_tex = crack_tex;- } else s_tex = tex_px[tex];- s_cm = face_cm(world_light(nx, ny, nz), f);-#ifdef HOST- probe_x = x, probe_y = y, probe_z = z, probe_b = b, probe_f = f, probe_t = tex;-#endif- int q[4][3];- for (int i = 0; i < 4; i++) memcpy(q[i], corner[FACE_C[f][i]], sizeof q[i]);- face4(q, split);- }- s_alpha = false;-}--/* is the square of radius r (1/16 px) around (sx, sy) all drawn? */-static bool covered(int sx, int sy, int r) {- int x0 = (int)((sx - r) >> 4), x1 = (int)((sx + r) >> 4), y0 = (int)((sy - r) >> 4), y1 = (int)((sy + r) >> 4);- if (x0 < 0) x0 = 0;- if (y0 < 0) y0 = 0;- if (x1 >= RW) x1 = RW - 1;- if (y1 >= RH) y1 = RH - 1;- if (x0 > x1 || y0 > y1) return true; /* off screen */- int b0 = x0 >> 3, b1 = x1 >> 3;- uint8_t m0 = (uint8_t)(0xFF << (x0 & 7)), m1 = (uint8_t)(0xFF >> (7 - (x1 & 7)));- for (int y = y0; y <= y1; y++) {- const uint8_t *c = cov[y];- if (b0 == b1) {- if ((c[b0] & (m0 & m1)) != (m0 & m1)) return false;- continue;- }- if ((c[b0] & m0) != m0 || (c[b1] & m1) != m1) return false;- for (int b = b0 + 1; b < b1; b++)- if (c[b] != 0xFF) return false;- }- return true;-}--/* a column's cells, outward from the eye's level */-static void column(int x, int z) {- if ((unsigned)(x - win_x) >= WS || (unsigned)(z - win_z) >= WS) return;- uint32_t mask = wexp[CI(x, z)];- if (!mask) return;- int i = x - eye_bx, k = z - eye_bz;- /* the column's low corner at the eye's level */- int X0 = bx0 + i * rxx + k * rxz;- int Y0 = by0 + i * uxx + k * uxz;- int Z0 = bz0 + i * fxx + k * fxz;- int ylo = win_y, yhi = win_y + WH - 1;- const int R = (int)(Q1 * 87 / 100); /* a cell's half diagonal */- int vd2 = (view_dist * 32 + 28) * (view_dist * 32 + 28);- for (int pass = 0; pass < 2; pass++) {- int y = pass ? eye_by - 1 : eye_by, dy = pass ? -1 : 1;- if (y < ylo) y = ylo;- if (y > yhi) y = yhi;- for (; pass ? y >= ylo : y <= yhi; y += dy) {- if (!(mask >> (y & 31) & 1)) continue;- int j = y - eye_by;- int X = X0, Y = Y0 + j * uxy, Z = Z0 + j * fxy;- /* its middle, its distance, the view's sides */- int cx = X + (rxx + rxz) / 2, cy = Y + (uxx + uxy + uxz) / 2, cz = Z + (fxx + fxy + fxz) / 2;- if (cz < -R) continue;- int ax = cx < 0 ? -cx : cx, ay = cy < 0 ? -cy : cy;- /* (|x| <= z * tan(43 degrees) + r / cos: 0.933 and 1.37; |y| <= z * 0.7 + r * 1.22) */- if (ax > cz - cz / 16 + R * 137 / 100 || ay > cz / 10 * 7 + R * 122 / 100) continue;- int qx = cx >> 9, qy = cy >> 9, qz = cz >> 9; /* (1/32 blocks) */- int d2 = qx * qx + qy * qy + qz * qz;- if (d2 > vd2) continue;- /* passed by when what is in front of it covers its square already */- if (cz > R) {- int32_t inv = (F16 << 18) / cz;- int sx = RW * 8 + (int)(((int32_t)cx * inv) >> 18), sy = RH * 8 - (int)(((int32_t)cy * inv) >> 18);- int r = (int)(((int32_t)R * inv) >> 18) + 16;- if (!render_debug && covered(sx, sy, r)) continue;- }- cell(x, y, z, wblk[WI(x, y, z)], X, Y, Z, d2);- if (px_left <= 0) return;- }+ tex = f == 3 || f == 4 ? bt[3] : bt[2];+ s_tex = mined ? crack_of(tex) : tex_of(tex);+ s_cm = shade_of(light_level(lb, f), fog);+ face_corners(f);+ quad(split);}}-/* ---------------------------------------------------------------- the sky */-static void sky(void) {- /* each row: its height in the sky (the ray through its middle, at the screen's middle column) */+/* ---------------------------------------------------------------- the sky's colour, row by row */+static uint8_t sky_row[RH];+static void sky_rows(void) {+ /* the height in the sky of the ray through a row's middle (at the screen's middle column): 0 the horizon,+ * the sky's top colour from 0.4 up */+ int32_t t0 = (int32_t)(RH / 2 * 16 - 8) * Q1 / F16, dt = 16 * Q1 / F16; /* Q14 */for (int y = 0; y < RH; y++) {- uint8_t *c = cov[y];- bool any = false;- for (int b = 0; b < RW / 8; b++)- if (c[b] != 0xFF) any = true;- if (!any) continue;- /* the ray: forward + (60 - y - 0.5) / F * up; its world height part */- int32_t t = (int32_t)(RH / 2 * 16 - y * 16 - 8) * Q1 / F16; /* Q14 */- int32_t ry = fxy + (t * uxy >> Q);- int32_t len = Q1; /* (approximately: elevation from ry) */- int e = (int)(ry * 15 * 5 / 2 / len); /* 0 at the horizon, 15 at 0.4 up */- uint8_t col = (uint8_t)(e <= 0 ? 1 : e >= 14 ? 15 : e + 1);- if (render_debug & 2) col = C_RED_DEBUG;- uint8_t *d = rbuf + y * RW;- for (int b = 0; b < RW / 8; b++) {- uint8_t m = c[b];- if (m == 0xFF) continue;- for (int k = 0; k < 8; k++)- if (!(m >> k & 1)) d[b * 8 + k] = col;- }+ int32_t ry = fxy + ((t0 - dt * y) * uxy >> Q);+ int e = (int)(ry * 75 / 2 >> Q);+ sky_row[y] = (uint8_t)(e <= 0 ? 1 : e >= 14 ? 15 : e + 1);}}/* ---------------------------------------------------------------- a frame */+/* (no multiplying, shifting or masking in the loops below: on the eZ80 those are calls; tables and adds) */+static const uint16_t SQ[34] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256,+ 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089};+static uint16_t zrow[32], ylev[32]; /* z slot x 32, y slot x 1024 */+/* a cell rs_column found in the view: drawn */+void rs_cell(int Y, int Z, uint8_t fog, uint8_t o, uint8_t ys) {+ c_x = rs_x, c_y = eye_by + (int)o - 32, c_z = rs_z, c_ci = rs_ci, c_idx = ylev[ys] + rs_ci;+ c_sx = rs_xs, c_sy = ys, c_sz = rs_zs;+ cell(wblk[c_idx], rs_X0, Y, Z, fog);+}+static void frame_from(fx ex, fx ey, fx ez, uint16_t yaw, int16_t pitch) {camera(ex, ey, ez, yaw, pitch);- memset(cov, 0, sizeof cov);- px_left = RW * RH;+ frame_no++;+ memset(rbuf, 0, RW * RH);stat_faces = stat_spans = stat_px = stat_cells = 0;- int fd = view_dist * 32;- fog0_2 = fd * fd / 16 * 9; /* the fog starts at 3/4 of the view (squared, 1/32 blocks) */- fog_step = (fd * fd - fog0_2) / 9 + 1;- int R = view_dist;+#ifdef HOST+ st_cols = st_list = st_frus = st_cov = st_culled = 0;+#endif+ if (!ylev[1])+ for (int k = 0; k < 32; k++) zrow[k] = (uint16_t)(k * 32), ylev[k] = (uint16_t)(k * 1024);+ /* fog: from 3/4 of the view to all of it, in 8 steps (squared distances, blocks) */+ int vd = view_dist, f0 = vd * vd * 9 / 16, fstep = (vd * vd - f0 + 8) / 9, vlim = (vd + 1) * (vd + 1);+ for (int d = 0, f = 0; d <= vlim; d++) {+ while (f < 9 && d > f0 + fstep * f) f++;+ rs_fog[d] = (uint8_t)f;+ }+ rs_vlim = vlim;+ /* the frustum: |x| <= z (45 degrees, wider than the view's 43) + a cell's half diagonal, |y| <= 0.7 z + ... */+ const int R = (int)(Q1 * 87 / 100), KX = R * 141 / 100, KY = R * 122 / 100;+ int vzx = (int)((int32_t)fxx * 7 / 10), vzy = (int)((int32_t)fxy * 7 / 10), vzz = (int)((int32_t)fxz * 7 / 10);+ int cx0 = (rxx + rxz) / 2, cy0 = (uxx + uxy + uxz) / 2, cz0 = (fxx + fxy + fxz) / 2;+ int vz0 = (int)((int32_t)cz0 * 7 / 10);+ rs_R = R, rs_KX = KX, rs_cy0 = cy0, rs_cz0 = cz0, rs_vzk = vz0 + KY;+ for (int j = 0, a = 0, b = 0, c = 0; j <= 32; j++, a += uxy, b += fxy, c += vzy) {+ rs_jY[32 + j] = a, rs_jZ[32 + j] = b, rs_jV[32 + j] = c, rs_jY[32 - j] = -a, rs_jZ[32 - j] = -b, rs_jV[32 - j] = -c;+ rs_jS[32 + j] = rs_jS[32 - j] = SQ[j];+ }+ if (!rs_jS[127]) memset(rs_jS + 65, 255, sizeof rs_jS - 65 * 2);+ /* the eye's slots; the levels up and down from it in the window */+ int ey_s = eye_by & 31;+ int up = win_y + WH - eye_by, down = eye_by - win_y;+ if (up < 0) up = 0;+ if (up > WH) up = WH;+ if (down < 0) down = 0;+ if (down > WH) down = WH;+ if (eye_by < win_y) ey_s = win_y & 31, up = WH, down = 0; /* (under the window: from its bottom up) */+ int jbase = eye_by < win_y ? win_y - eye_by : 0;+ if (jbase > 32) jbase = 32; /* (all past the view) */+ rs_eys = (uint8_t)ey_s, rs_up = (uint8_t)up, rs_down = (uint8_t)down, rs_jbase = (uint8_t)jbase;+ /* a column is passed by when none of its levels can be in the view's sides: the forward distance of its+ * farthest level */+ int jlo = jbase - down, jhi = jbase + up - 1;+ if (jlo < -32) jlo = -32;+ if (jhi > 32) jhi = 32;+ if (jhi < jlo) jhi = jlo;+ int czm = (rs_jZ[32 + jlo] > rs_jZ[32 + jhi] ? rs_jZ[32 + jlo] : rs_jZ[32 + jhi]) + cz0 + KX;+ rs_czm = czm, rs_cx0 = cx0, rs_vd = (uint8_t)vd;+ rs_wx0 = win_x, rs_wx1 = win_x + WS, rs_wz0 = win_z, rs_wz1 = win_z + WS;/* the quadrants: the one looked into first */int qx = fxx >= 0 ? 0 : 1, qz = fxz >= 0 ? 0 : 2;- static const uint8_t order[4] = {0, 1, 2, 3};- for (int n = 0; n < 4 && px_left > 0; n++) {- int q = order[n] ^ qx ^ qz;- int sx = q & 1 ? -1 : 1, sz = q & 2 ? -1 : 1;- for (int i = 0; i <= R && px_left > 0; i++) {- int x = sx > 0 ? eye_bx + i : eye_bx - 1 - i;- for (int k = 0; k <= R && px_left > 0; k++) {- int z = sz > 0 ? eye_bz + k : eye_bz - 1 - k;- int dx = sx > 0 ? i : i + 1, dz = sz > 0 ? k : k + 1;- if ((dx - 1) * (dx - 1) + (dz - 1) * (dz - 1) > R * R) break;- column(x, z);- }- }+ for (int n = 0; n < 4; n++) {+ int q = n ^ qx ^ qz;+ bool px = !(q & 1), pz = !(q & 2);+ /* the first column's low corner at the eye's level, and the steps out along x and z */+ rs_qXi = bx0 + (px ? 0 : -rxx) + (pz ? 0 : -rxz), rs_qYi = by0 + (px ? 0 : -uxx) + (pz ? 0 : -uxz);+ rs_qZi = bz0 + (px ? 0 : -fxx) + (pz ? 0 : -fxz);+ rs_qVi = (int)((int32_t)rs_qZi * 7 / 10);+ rs_qsXx = px ? rxx : -rxx, rs_qsYx = px ? uxx : -uxx, rs_qsZx = px ? fxx : -fxx, rs_qsVx = px ? vzx : -vzx;+ rs_qsXz = pz ? rxz : -rxz, rs_qsYz = pz ? uxz : -uxz, rs_qsZz = pz ? fxz : -fxz, rs_qsVz = pz ? vzz : -vzz;+ rs_qsxi = px ? 1 : -1, rs_qszi = pz ? 1 : -1, rs_qsx = (uint8_t)rs_qsxi, rs_qsz = (uint8_t)rs_qszi;+ rs_qx0 = eye_bx + (px ? 0 : -1), rs_qz0 = eye_bz + (pz ? 0 : -1);+ rs_qxs0 = (uint8_t)(rs_qx0 & 31), rs_qzs0 = (uint8_t)(rs_qz0 & 31);+ /* (|i| and |k| from the loops' counts: 0, 1, 2... or 1, 2, 3...) */+ rs_qsqi = SQ + (px ? 0 : 1), rs_qsqk = SQ + (pz ? 0 : 1);+ rs_quad();}- sky();+ sky_rows();}/* ---------------------------------------------------------------- the block looked at, the hand */@@ -699,8 +662,9 @@ static void outline(void) {fc = FACE_C[f];}for (int n = 0; n < 4; n++) {- CV a = {corner[fc[n]][0], corner[fc[n]][1], corner[fc[n]][2], 0, 0};- CV c = {corner[fc[(n + 1) & 3]][0], corner[fc[(n + 1) & 3]][1], corner[fc[(n + 1) & 3]][2], 0, 0};+ int ka = fc[n] * 3, kc = fc[(n + 1) & 3] * 3;+ CV a = {ck[ka], ck[ka + 1], ck[ka + 2], 0, 0};+ CV c = {ck[kc], ck[kc + 1], ck[kc + 2], 0, 0};/* (an end behind the eye: moved to the near plane) */if (a.z < NEAR && c.z < NEAR) continue;if (a.z < NEAR || c.z < NEAR) {@@ -708,11 +672,13 @@ static void outline(void) {int t = (int)(((int32_t)(NEAR - p->z) << 12) / (q->z - p->z));p->x = lerp12(p->x, q->x, t), p->y = lerp12(p->y, q->y, t), p->z = NEAR;}- SV sa, sc;- project(&a, &sa);- project(&c, &sc);- if (sa.x < -8000 || sa.x > 8000 || sc.x < -8000 || sc.x > 8000) continue;- dark_line(sa.x >> 4, sa.y >> 4, sc.x >> 4, sc.y >> 4);+ /* (an end far off the sides: left out) */+ if (a.x > 3 * a.z || -a.x > 3 * a.z || a.y > 3 * a.z || -a.y > 3 * a.z) continue;+ if (c.x > 3 * c.z || -c.x > 3 * c.z || c.y > 3 * c.z || -c.y > 3 * c.z) continue;+ int sa[2], sc[2];+ rs_proj(a.x, a.y, a.z, sa);+ rs_proj(c.x, c.y, c.z, sc);+ dark_line(sa[0] >> 4, sa[1] >> 4, sc[0] >> 4, sc[1] >> 4);}}}@@ -732,19 +698,22 @@ static void hand(void) {/* the icon's runs, 3 x 3 pixels each */for (int r = 0; r < 16; r++) {const uint8_t *p = img + (img[r * 2] | img[r * 2 + 1] << 8) + 32;- int col = 0;+ int col = 0, y = y0 + r * 3;while (col < 16) {int n = *p++, len = n < 128 ? n + 1 : n - 126;for (int q = 0; q < len; q++, col++) {uint8_t c = n < 128 ? p[q] : p[0];- if (!c) continue;- for (int dy = 0; dy < 3; dy++) {- int y = y0 + r * 3 + dy;- if (y >= RH) break;- for (int dx = 0; dx < 3; dx++) {- int x = x0 + col * 3 + dx;- if ((unsigned)x < RW) rbuf[y * RW + x] = c;- }+ int x = x0 + col * 3;+ if (!c || x < 0 || x + 3 > RW) continue;+ for (int dy = 0; dy < 3 && y + dy < RH; dy++) memset(rbuf + (y + dy) * RW + x, c, 3);+ if (n >= 128) {+ /* (a run: the rest of it at once) */+ int m = len - q - 1, w = m * 3;+ if (x + 3 + w > RW) w = RW - x - 3;+ if (w > 0)+ for (int dy = 0; dy < 3 && y + dy < RH; dy++) memset(rbuf + (y + dy) * RW + x + 3, c, (size_t)w);+ col += m;+ break;}}p += n < 128 ? n + 1 : 1;@@ -767,4 +736,18 @@ void render_title(uint16_t yaw) {frame_from(pl.x, pl.y + FX1 * 162 / 100, pl.z, yaw, 1200);}-void render_init(void) { fog_q = -1; }+void render_init(void) {+ fog_q = -1;+ rs_recip = texdat + TEXDAT_RECIP, rs_ratio = texdat + TEXDAT_RATIO;+ memcpy(shade_ram, shade_cm, sizeof shade_ram);+ for (int b = 0; b < B_N; b++) blk_tex_off[b] = (uint16_t)(b * 6);+ caches_reset();+ for (int v = 0; v < 256; v++) {+ int k = 0;+ while (k < 15 && (cube_levels[k] + cube_levels[k + 1]) / 2 < v) k++;+ cube_of[v] = (uint8_t)k;+ }+}++/* the picture to the screen: the sky in what is not drawn, the HUD over it */+void render_show(bool hud) { rs_expand(rbuf, scr, sky_row, hud ? hud_layer : 0); }diff --git a/games/numblocks/ce/src/rs.c b/games/numblocks/ce/src/rs.cnew file mode 100644index 00000000..173a5436--- /dev/null+++ b/games/numblocks/ce/src/rs.c@@ -0,0 +1,320 @@+/* The 3D view's inner loops in C: what rs.s does on the CE, the same arithmetic step for step (24-bit numbers+ * wrap as on the eZ80), for a computer. (On the CE, built with -DNB_BENCH, these are rsc_* to check rs.s with.) */+#include <string.h>+#include "rs.h"+#include "draw.h"++#ifdef __ez80__+#ifndef NB_BENCH+#define RS_NONE+#endif+#define RS(n) rsc_##n+#else+#define RS(n) n+#endif++#ifndef RS_NONE+void RS(rs_setup)(const uint8_t *tex, const uint8_t *cm, uint8_t alpha);+void RS(rs_poly)(const SV *p, uint8_t n);+void RS(rs_proj)(int x, int y, int z, int *out);+uint8_t RS(rs_hidden)(int x, int y, int z, int r);+void RS(rs_expand)(const uint8_t *src, uint8_t *dst, const uint8_t *sky, const uint8_t *hud);+/* (a range of pixels around c, r each way: its first, how many; 0: none on the screen) */+static int h_range(int c, int r, int last, int *first) {+ int lo = c - r, hi = c + r;+ int a = lo < 0 ? 0 : lo >> 4, b;+ if (a > last) return 0;+ if (hi < 0) return 0;+ b = hi >> 4;+ if (b > last) b = last;+ *first = a;+ return b - a + 1;+}++uint8_t RS(rs_hidden)(int x, int y, int z, int r) {+ int o[2], q[2], x0, y0;+ RS(rs_proj)(r, 0, z, q);+ r = q[0] - 1280 + 16;+ RS(rs_proj)(x, y, z, o);+ int rows = h_range(o[1], r, RH - 1, &y0);+ if (!rows) return 1;+ int w = h_range(o[0], r, RW - 1, &x0);+ if (!w) return 1;+ const uint8_t *row = rbuf + y0 * RW + x0;+ for (; rows; rows--, row += RW)+ if (memchr(row, 0, (size_t)w)) return 0;+ return 1;+}++uint8_t RS(rs_cells)(const uint8_t *mask, uint8_t slot, uint8_t up, uint8_t down, int8_t *out);+void RS(rs_column)(const uint8_t *mask, int h2, int Y0, int Z0, int V0, int cx);+void RS(rs_quad)(void);++#ifndef __ez80__+unsigned long rs_visited, rs_drawn, rs_rows, rs_edges, rs_polys;+#define COUNT(x) (x)+#else+#define COUNT(x)+#endif+static const uint8_t *r_tex, *r_cm;+static uint8_t r_alpha;+#define W24(x) ((int32_t)((uint32_t)(x) << 8) >> 8) /* (as a 24-bit register holds it) */++void RS(rs_setup)(const uint8_t *tex, const uint8_t *cm, uint8_t alpha) { r_tex = tex, r_cm = cm, r_alpha = alpha; }++/* a x r >> 24, a signed, r 0..2^24 (the low partial products left out, as rs.s does) */+static int32_t mulhi(int32_t a, uint32_t r) {+ int neg = a < 0;+ uint32_t m = (uint32_t)(neg ? -a : a) & 0xFFFFFF;+ uint32_t a0 = m & 255, a1 = m >> 8 & 255, a2 = m >> 16, r0 = r & 255, r1 = r >> 8 & 255, r2 = r >> 16 & 255;+ uint32_t t = a2 * r0 + a1 * r1 + a0 * r2;+ int32_t res = (int32_t)((a2 * r2 << 8) + a2 * r1 + a1 * r2 + (t >> 8));+ return W24(neg ? -res : res);+}+/* s x f >> 8, s signed, f 0..255 */+static int32_t mul8(int32_t s, unsigned f) {+ int neg = s < 0;+ uint32_t m = (uint32_t)(neg ? -s : s) & 0xFFFFFF;+ int32_t res = (int32_t)((m * f) >> 8);+ return W24(neg ? -res : res);+}+static uint32_t recip(int k) { const uint8_t *p = rs_recip + (k - 16) * 3; return p[0] | p[1] << 8 | (uint32_t)p[2] << 16; }++typedef struct { int32_t x, u, v, sx, su, sv; int rows, idx; } Walker;++/* the walker's next edge with rows (from vertex idx on, dir 1 or n - 1); false: none left */+static int edge(Walker *w, const SV *p, int n, int dir) {+ COUNT(rs_edges++);+ for (int tries = 0; tries < n; tries++) {+ const SV *a = &p[w->idx];+ int nx = w->idx + dir;+ if (nx >= n) nx -= n;+ const SV *b = &p[nx];+ w->idx = nx;+ int r0 = (a->y + 7) >> 4, r1 = (b->y + 7) >> 4;+ if (r1 <= r0) continue;+ int d = b->y - a->y, f = r0 * 16 + 8 - a->y;+ int32_t dx = (int32_t)(b->x - a->x) * 16, du = (int32_t)(b->u - a->u) * 16, dv = (int32_t)(b->v - a->v) * 16;+ if (d >= 16) {+ uint32_t r = recip(d);+ w->sx = mulhi(dx, r), w->su = mulhi(du, r), w->sv = mulhi(dv, r);+ w->x = (int32_t)a->x * 16 + mul8(w->sx, (unsigned)f * 16);+ w->u = (int32_t)a->u * 16 + mul8(w->su, (unsigned)f * 16);+ w->v = (int32_t)a->v * 16 + mul8(w->sv, (unsigned)f * 16);+ } else {+ unsigned q = rs_ratio[d * 16 + f];+ w->sx = w->su = w->sv = 0;+ w->x = (int32_t)a->x * 16 + mul8(dx, q);+ w->u = (int32_t)a->u * 16 + mul8(du, q);+ w->v = (int32_t)a->v * 16 + mul8(dv, q);+ }+ w->x = W24(w->x), w->u = W24(w->u), w->v = W24(w->v);+ w->rows = r1 - r0;+ return 1;+ }+ return 0;+}++static void span(uint8_t *dst, int n, int u, int v, int du, int dv) {+ do {+ COUNT(rs_visited++);+ if (!*dst) {+ COUNT(rs_drawn++);+ uint8_t t = r_tex[((v >> 8) & 0xF0) | ((u >> 8) & 0xFF)];+ if (!(r_alpha && !t)) *dst = r_cm[t];+ }+ dst++;+ u += du, v += dv;+ } while (--n);+}++void RS(rs_poly)(const SV *p, uint8_t n) {+ int top = 0, ymin = p[0].y, ymax = p[0].y;+ for (int i = 1; i < n; i++) {+ if (p[i].y < ymin) ymin = p[i].y, top = i;+ if (p[i].y > ymax) ymax = p[i].y;+ }+ int y = (ymin + 7) >> 4, yend = (ymax + 7) >> 4;+ if (y >= yend) return;+ Walker a = {0}, b = {0}, *l, *r;+ a.idx = b.idx = top;+ if (!edge(&a, p, n, 1) || !edge(&b, p, n, n - 1)) return;+ l = &a, r = &b;+ if (a.x > b.x || (a.x == b.x && a.sx > b.sx)) l = &b, r = &a;+ uint8_t *row = rbuf + y * RW;+ int lock = 0;+ int32_t du = 0, dv = 0, ku[16], kv[16];+ COUNT(rs_polys++);+ for (;;) {+ COUNT(rs_rows++);+ int px0 = (int)((l->x + 127) >> 8), px1 = (int)((r->x + 127) >> 8);+ if (px1 > RW) px1 = RW;+ if (px0 < px1) {+ int32_t u0, v0;+ if (!lock) {+ du = dv = 0;+ int w = (int)((r->x - l->x) >> 4);+ if (w >= 16) {+ uint32_t rc = recip(w);+ du = mulhi(W24(r->u - l->u), rc), dv = mulhi(W24(r->v - l->v), rc);+ if (w >= 64) {+ /* (four pixels wide or more: this gradient for the rows below; the steps from an edge to a pixel's+ * middle in sixteenths) */+ lock = 1;+ int32_t bu = mul8(du, 8), su = mul8(du, 16), bv = mul8(dv, 8), sv = mul8(dv, 16);+ for (int k = 0; k < 16; k++) ku[k] = W24(bu + su * k), kv[k] = W24(bv + sv * k);+ }+ }+ }+ if (lock) {+ int k = (int)((unsigned)(128 - l->x) & 255) >> 4;+ u0 = W24(l->u + ku[k]), v0 = W24(l->v + kv[k]);+ } else {+ unsigned frac = (unsigned)(128 - l->x) & 255;+ u0 = W24(l->u + mul8(du, frac)), v0 = W24(l->v + mul8(dv, frac));+ }+ if (u0 < 0) u0 = 0;+ if (u0 > 0xFFFFF) u0 = 0xFFFFF;+ if (v0 < 0) v0 = 0;+ if (v0 > 0xFFFFF) v0 = 0xFFFFF;+ span(row + px0, px1 - px0, (int)(u0 >> 8), (int)(v0 >> 4), (int)(du >> 8), (int)(dv >> 4));+ }+ if (++y >= yend) return;+ row += RW;+ l->x = W24(l->x + l->sx), l->u = W24(l->u + l->su), l->v = W24(l->v + l->sv);+ r->x = W24(r->x + r->sx), r->u = W24(r->u + r->su), r->v = W24(r->v + r->sv);+ if (!--l->rows && !edge(l, p, n, l == &a ? 1 : n - 1)) return;+ if (!--r->rows && !edge(r, p, n, r == &a ? 1 : n - 1)) return;+ }+}++/* 1371 << 12 / (256 + i) */+static uint16_t rtab[256];+void RS(rs_proj)(int x, int y, int z, int *out) {+ if (!rtab[0])+ for (int i = 0; i < 256; i++) rtab[i] = (uint16_t)((1371L << 12) / (256 + i));+ int c = 0;+ int32_t zz = z;+ while (!(zz & 0x800000)) zz <<= 1, c++;+ int32_t t = rtab[(zz >> 15) & 255];+ /* (x << (c - 3), its top 16 bits as the size, the sign apart: as rs.s does) */+ int32_t xs = W24((int32_t)x << (c - 3)), ys = W24((int32_t)y << (c - 3));+ int32_t mx = ((xs < 0 ? -xs : xs) & 0xFFFFFF) >> 8, my = ((ys < 0 ? -ys : ys) & 0xFFFFFF) >> 8;+ int32_t ox = (mx * t) >> 16, oy = (my * t) >> 16;+ if (xs < 0) ox = -ox;+ if (ys < 0) oy = -oy;+ out[0] = 1280 + (int)ox;+ out[1] = 960 - (int)oy;+}++uint8_t RS(rs_cells)(const uint8_t *mask, uint8_t slot, uint8_t up, uint8_t down, int8_t *out) {+ uint8_t n = 0;+ for (int t = 0; t < up; t++) {+ int b = (slot + t) & 31;+ if (mask[b >> 3] >> (b & 7) & 1) out[n++] = (int8_t)t;+ }+ for (int t = 1; t <= down; t++) {+ int b = (slot - t) & 31;+ if (mask[b >> 3] >> (b & 7) & 1) out[n++] = (int8_t)-t;+ }+ return n;+}++#ifndef __ez80__+int rs_jY[65], rs_jZ[65], rs_jV[65];+uint16_t rs_jS[128];+uint8_t rs_fog[257];+int rs_vlim, rs_cy0, rs_cz0, rs_vzk, rs_R, rs_KX;+uint8_t rs_eys, rs_up, rs_down, rs_jbase;+unsigned long st_list, st_frus, st_cov, st_culled, st_cols;+int rs_qXi, rs_qYi, rs_qZi, rs_qVi, rs_qsXx, rs_qsYx, rs_qsZx, rs_qsVx, rs_qsXz, rs_qsYz, rs_qsZz, rs_qsVz;+int rs_qx0, rs_qz0, rs_qsxi, rs_qszi, rs_czm, rs_cx0, rs_wx0, rs_wx1, rs_wz0, rs_wz1;+const uint16_t *rs_qsqi, *rs_qsqk;+uint8_t rs_qsx, rs_qsz, rs_qxs0, rs_qzs0, rs_vd;+int rs_x, rs_z, rs_ci, rs_X0;+uint8_t rs_xs, rs_zs;+#define ST(x) (x++)+#else+#define ST(x)+#endif++void RS(rs_column)(const uint8_t *mask, int h2, int Y0, int Z0, int V0, int cx) {+ int8_t list[64];+ uint8_t n = RS(rs_cells)(mask, rs_eys, rs_up, rs_down, list);+#ifndef __ez80__+ st_list += n;+#endif+ for (uint8_t t = 0; t < n; t++) {+ int8_t lj = list[t];+ uint8_t o = (uint8_t)(lj + rs_jbase + 32);+ int d2 = h2 + rs_jS[o];+ if (d2 > rs_vlim) continue;+ int Y = Y0 + rs_jY[o], Z = Z0 + rs_jZ[o], cy = Y + rs_cy0, cz = Z + rs_cz0, vz = V0 + rs_vzk + rs_jV[o];+ if (cz + rs_R < 0) continue;+ if (cz - (cx - rs_KX) < 0 || cz + (cx + rs_KX) < 0) continue;+ if (vz - cy < 0 || vz + cy < 0) continue;+ uint8_t fog = rs_fog[d2];+ if (fog > 8) continue;+ ST(st_frus);+ if (rs_R + rs_R - cz < 0) {+ ST(st_cov);+ if (RS(rs_hidden)(cx, cy, cz, rs_R)) {+ ST(st_culled);+ continue;+ }+ }+ rs_cell(Y, Z, fog, o, (uint8_t)((uint8_t)(lj + rs_eys) & 31));+ }+}++void RS(rs_quad)(void) {+ uint8_t xs = rs_qxs0;+ int x = rs_qx0;+ for (int a = 0; a <= rs_vd; a++) {+ if (x < rs_wx0 || x >= rs_wx1) return;+ int X = rs_qXi, Y = rs_qYi, Z = rs_qZi, V = rs_qVi, z = rs_qz0, hi = rs_qsqi[a];+ uint8_t zs = rs_qzs0;+ for (int c = 0; c <= rs_vd; c++) {+ int h2 = hi + rs_qsqk[c];+ if (h2 > rs_vlim) break;+ if (z < rs_wz0 || z >= rs_wz1) break;+ int cx = X + rs_cx0, lim = Z + rs_czm;+ if (lim - cx >= 0 && lim + cx >= 0) {+ int ci = zs * 32 + xs;+ const uint8_t *mask = (const uint8_t *)&wexp[ci];+#ifndef __ez80__+ st_cols++;+#endif+ if (mask[0] | mask[1] | mask[2] | mask[3]) {+ rs_x = x, rs_z = z, rs_ci = ci, rs_X0 = X, rs_xs = xs, rs_zs = zs;+ RS(rs_column)(mask, h2, Y, Z, V, cx);+ }+ }+ zs = (uint8_t)((zs + rs_qsz) & 31), z += rs_qszi;+ X += rs_qsXz, Y += rs_qsYz, Z += rs_qsZz, V += rs_qsVz;+ }+ xs = (uint8_t)((xs + rs_qsx) & 31), x += rs_qsxi;+ rs_qXi += rs_qsXx, rs_qYi += rs_qsYx, rs_qZi += rs_qsZx, rs_qVi += rs_qsVx;+ }+}++void RS(rs_expand)(const uint8_t *src, uint8_t *dst, const uint8_t *sky, const uint8_t *hud) {+ for (int y = 0; y < RH; y++, src += RW) {+ uint8_t *d = dst + y * 2 * SCREEN_W, s = sky[y];+ for (int x = 0; x < RW; x++) {+ uint8_t c = src[x] ? src[x] : s;+ d[2 * x] = d[2 * x + 1] = c;+ }+ memcpy(d + SCREEN_W, d, SCREEN_W);+ for (int k = 0; k < 2; k++) {+ int r = y * 2 + k;+ if (hud && r >= HUD_Y) {+ const uint8_t *h = hud + (r - HUD_Y) * HUD_W;+ uint8_t *o = d + k * SCREEN_W + HUD_X;+ for (int x = 0; x < HUD_W; x++)+ if (h[x]) o[x] = h[x];+ }+ }+ }+}+#endifdiff --git a/games/numblocks/ce/src/rs.h b/games/numblocks/ce/src/rs.hnew file mode 100644index 00000000..c67e2bff--- /dev/null+++ b/games/numblocks/ce/src/rs.h@@ -0,0 +1,53 @@+/* The 3D view's inner loops: eZ80 assembly on the CE (rs.s), C on a computer (rs.c, the same arithmetic). */+#ifndef NBCE_RS_H+#define NBCE_RS_H+#include <stdint.h>++/* a vertex on the screen: x 0..2560, y 0..1920 (1/16 pixels); u, v the texel x 4096 (0..65535) */+typedef struct { int x, y, u, v; } SV;++/* what rs_poly fills with: a texture and a shade row, 256-byte aligned (tex[v * 16 + u], cm[texel]); alpha: texel 0+ * is see-through */+void rs_setup(const uint8_t *tex, const uint8_t *cm, uint8_t alpha);+/* a convex polygon (n 3..12) all on the screen, into rbuf's pixels that are still 0, its texture mapped linearly+ * along its edges */+void rs_poly(const SV *p, uint8_t n);+/* camera space (Q14, z >= 819, |x|, |y| < 4z) to the screen: out[0] x, out[1] y, in 1/16 pixels */+void rs_proj(int x, int y, int z, int *out);+/* 1 when the pixels around (x, y, z)'s projection (z > 2r), as far as r's projection and a pixel more each way, are+ * all drawn already (or off the screen) */+uint8_t rs_hidden(int x, int y, int z, int r);+/* the 160 x 120 picture to the screen at 2x, 0s as the row's sky colour (sky[y]); from screen row 180 on, the HUD's+ * layer (187 wide at x 67, 0 see-through) over it (hud 0: none) */+void rs_expand(const uint8_t *src, uint8_t *dst, const uint8_t *sky, const uint8_t *hud);+/* a column's cells with a face (mask: 4 bytes, bit y & 31) from bit slot: up levels up from it (offsets 0, 1..)+ * then down levels down (-1, -2..), those set, in that order, into out; how many */+uint8_t rs_cells(const uint8_t *mask, uint8_t slot, uint8_t up, uint8_t down, int8_t *out);+/* the view rs_column tests cells against, set each frame: by j + 32 (j the levels up from the eye), the camera+ * space steps up and j squared (past 64: 65535); by squared distance, the fog (9: past the view); the limits */+extern int rs_jY[65], rs_jZ[65], rs_jV[65];+extern uint16_t rs_jS[128];+extern uint8_t rs_fog[257];+extern int rs_vlim, rs_cy0, rs_cz0, rs_vzk, rs_R, rs_KX;+extern uint8_t rs_eys, rs_up, rs_down, rs_jbase;+/* a column's cells with a face (as rs_cells finds them), each in the view and not hidden by what is drawn already+ * (rs_hidden): rs_cell(Y, Z, fog, j + 32, its y slot) (render.c) */+void rs_column(const uint8_t *mask, int h2, int Y0, int Z0, int V0, int cx);+void rs_cell(int Y, int Z, uint8_t fog, uint8_t o, uint8_t ys);+/* a quadrant's columns: rows out along x from the first (rs_qx0, its slot rs_qxs0, step rs_qsxi = rs_qsx), columns+ * out along z (rs_qz0, rs_qzs0, rs_qszi = rs_qsz), their low corners at the eye's level in camera space (rs_qXi..,+ * steps rs_qsXx.. along x and rs_qsXz.. along z, V = 0.7 Z), |i| and |k| squared from the tables rs_qsqi and rs_qsqk+ * by the loops' counts (0..rs_vd); those in the window (rs_wx0 <= x < rs_wx1, z the same), the view's distance+ * (rs_vlim) and its sides (|X + rs_cx0| <= Z + rs_czm) and with a face, to rs_column (the column in rs_x, rs_z, rs_ci,+ * rs_X0, rs_xs, rs_zs). (rs_qXi.. are moved along.) */+void rs_quad(void);+extern int rs_qXi, rs_qYi, rs_qZi, rs_qVi, rs_qsXx, rs_qsYx, rs_qsZx, rs_qsVx, rs_qsXz, rs_qsYz, rs_qsZz, rs_qsVz;+extern int rs_qx0, rs_qz0, rs_qsxi, rs_qszi, rs_czm, rs_cx0, rs_wx0, rs_wx1, rs_wz0, rs_wz1;+extern const uint16_t *rs_qsqi, *rs_qsqk;+extern uint8_t rs_qsx, rs_qsz, rs_qxs0, rs_qzs0, rs_vd;+extern int rs_x, rs_z, rs_ci, rs_X0;+extern uint8_t rs_xs, rs_zs;+/* 2^28 / k for k 16..2560 (3 bytes each, from k = 16), and f * 256 / d for d 1..15, f < d ([d * 16 + f]): the+ * tables rs_poly divides with (in NBCETEX) */+extern const uint8_t *rs_recip, *rs_ratio;+#endifdiff --git a/games/numblocks/ce/src/rs.s b/games/numblocks/ce/src/rs.snew file mode 100644index 00000000..c9eb13b7--- /dev/null+++ b/games/numblocks/ce/src/rs.s@@ -0,0 +1,1897 @@+; The 3D view's inner loops on the CE (rs.h; rs.c does the same in C). eZ80, ADL mode.+; (Cycles: a byte read from RAM 4, written 2; code is read too, 4 a byte.)++ .assume adl=1++; walkers: an edge each side of a polygon, stepped a row at a time+ .equ W_X, 0 ; x in 1/256 pixels+ .equ W_U, 3 ; u, v: the texel x 65536+ .equ W_V, 6+ .equ W_SX, 9 ; their steps a row+ .equ W_SU, 12+ .equ W_SV, 15+ .equ W_ROWS, 18 ; rows left on this edge+ .equ W_IDX, 19 ; the vertex it goes to+ .equ W_DIR, 20 ; 1 or n - 1+ .equ W_SIZE, 21+ .equ RW, 160++; ------------------------------------------------------------------------------------------------- rs_setup+; void rs_setup(const uint8_t *tex, const uint8_t *cm, uint8_t alpha)+ .section .text._rs_setup+ .global _rs_setup+ .type _rs_setup, @function+_rs_setup:+ ld iy, 0+ add iy, sp+ ld hl, (iy+3)+ ld (r_tex), hl+ ld hl, (iy+6)+ ld (r_cm), hl+ ld hl, span_plain+ ld a, (iy+9)+ or a, a+ jr z, .Lsetup_plain+ ld hl, span_alpha+.Lsetup_plain:+ ld (r_loop), hl+ ret++ .section .text._rs_span+; ------------------------------------------------------------------------------------------------- the span+; de dst, a n (1..160); r_u, r_v, r_du, r_dv (u 8.8, v 4.12)+; main set: de dst, hl the texture (h its page), bc the shade row (b its page, c the texel)+; other set: hl u, de du, bc dv; ix v; iy points at the end's low byte+span:+ push ix+ add a, e+ ld (r_end), a ; the end's low byte (n <= 160)+ ld hl, (r_u)+ push de+ ld de, (r_du)+ ld bc, (r_dv)+ exx+ pop de+ ld ix, (r_v)+ ld hl, (r_tex)+ ld bc, (r_cm)+ ld iy, r_end+ push hl+ ld hl, (r_loop)+ ex (sp), hl+ ret ; to the loop++span_plain:+ ld a, (de)+ or a, a+ jr nz, .Lp_skip+ exx+ ld a, h ; u's texel column+ exx+ ld l, a+ ld a, ixh ; v's row, x 16+ and a, 0xF0+ or a, l+ ld l, a+ ld c, (hl) ; the texel+ ld a, (bc) ; shaded+ ld (de), a+.Lp_skip:+ inc de+ exx+ add hl, de+ add ix, bc+ exx+ ld a, e+ cp a, (iy)+ jr nz, span_plain+ pop ix+ ret++span_alpha:+ ld a, (de)+ or a, a+ jr nz, .La_skip+ exx+ ld a, h+ exx+ ld l, a+ ld a, ixh+ and a, 0xF0+ or a, l+ ld l, a+ ld a, (hl)+ or a, a ; see-through+ jr z, .La_skip+ ld c, a+ ld a, (bc)+ ld (de), a+.La_skip:+ inc de+ exx+ add hl, de+ add ix, bc+ exx+ ld a, e+ cp a, (iy)+ jr nz, span_alpha+ pop ix+ ret++ .section .text._rs_arith+; ------------------------------------------------------------------------------------------------- arithmetic+; hl = hl x de >> 24: hl signed, de 0..2^24 (the low partial products left out)+mulhi:+ ld (m_a), hl+ ld (m_r), de+ xor a, a+ ld (m_neg), a+ ld a, (m_a+2)+ bit 7, a+ jr z, .Lmh_pos+ ex de, hl+ or a, a+ sbc hl, hl+ sbc hl, de+ ld (m_a), hl+ ld a, 1+ ld (m_neg), a+.Lmh_pos:+ ; t = a2 r0 + a1 r1 + a0 r2+ ld a, (m_a+2)+ ld h, a+ ld a, (m_r)+ ld l, a+ mlt hl+ ld a, (m_a+1)+ ld d, a+ ld a, (m_r+1)+ ld e, a+ mlt de+ add hl, de+ ld a, (m_a)+ ld d, a+ ld a, (m_r+2)+ ld e, a+ mlt de+ add hl, de+ ld (m_t), hl+ ld hl, (m_t+1) ; t >> 8 (m_t+3 is 0)+ ; + a2 r1 + a1 r2+ ld a, (m_a+2)+ ld d, a+ ld a, (m_r+1)+ ld e, a+ mlt de+ add hl, de+ ld a, (m_a+1)+ ld d, a+ ld a, (m_r+2)+ ld e, a+ mlt de+ add hl, de+ ; + a2 r2 << 8+ ld a, (m_a+2)+ ld d, a+ ld a, (m_r+2)+ ld e, a+ mlt de+ xor a, a+ ld (m_t), a+ ld (m_t+1), de ; (de's top byte is 0 after mlt: m_t+3 stays 0)+ ld de, (m_t)+ add hl, de+ jr neg_if++; hl = hl x a >> 8: hl signed, a 0..255+mul8:+ ld (m_f), a+ ld (m_a), hl+ xor a, a+ ld (m_neg), a+ ld a, (m_a+2)+ bit 7, a+ jr z, .Lm8_pos+ ex de, hl+ or a, a+ sbc hl, hl+ sbc hl, de+ ld (m_a), hl+ ld a, 1+ ld (m_neg), a+.Lm8_pos:+ ; (s0 f >> 8) + s1 f + (s2 f << 8)+ ld a, (m_f)+ ld e, a+ ld a, (m_a)+ ld d, a+ mlt de ; s0 f+ ld c, d ; s0 f >> 8+ ld a, (m_f)+ ld e, a+ ld a, (m_a+2)+ ld d, a+ mlt de ; s2 f+ xor a, a+ ld (m_t), a+ ld (m_t+1), de+ ld hl, (m_t) ; s2 f << 8+ ld a, (m_f)+ ld e, a+ ld a, (m_a+1)+ ld d, a+ mlt de ; s1 f+ add hl, de+ ld de, 0+ ld e, c+ add hl, de+ ; (fall through)+neg_if:+ ld a, (m_neg)+ or a, a+ ret z+ ex de, hl+ or a, a+ sbc hl, hl+ sbc hl, de+ ret++; hl = p + a * 12+vptr:+ ld l, a+ ld h, 12+ mlt hl+ ld de, (p_base)+ add hl, de+ ret++ .section .text._rs_edge+; ------------------------------------------------------------------------------------------------- edges+; the walker ix's next edge that crosses a row's middle; nz: one, z: none left+edge:+ ld a, (p_n)+ ld b, a+.Led_try:+ push bc+ ld a, (ix+W_IDX)+ call vptr+ push hl+ pop iy ; iy: the vertex a+ ld a, (ix+W_IDX)+ add a, (ix+W_DIR)+ ld hl, p_n+ cp a, (hl)+ jr c, .Led_in+ sub a, (hl)+.Led_in:+ ld (ix+W_IDX), a+ call vptr ; hl: the vertex b+ ld (e_b), hl+ ; r0 = (ya + 7) >> 4, r1 = (yb + 7) >> 4+ push hl+ pop de+ inc de+ inc de+ inc de+ ex de, hl+ ld hl, (hl) ; yb+ ld (e_yb), hl+ ld de, 7+ add hl, de+ add hl, hl+ add hl, hl+ add hl, hl+ add hl, hl+ ld a, h+ ld (e_r1), a+ ld hl, (iy+3) ; ya+ add hl, de+ add hl, hl+ add hl, hl+ add hl, hl+ add hl, hl+ ld c, h ; r0+ ld a, (e_r1)+ sub a, c+ jp z, .Led_next+ jp c, .Led_next+ ld (ix+W_ROWS), a+ ; f = r0 * 16 + 8 - ya (0..15), x 16+ ld a, c+ add a, a+ add a, a+ add a, a+ add a, a+ add a, 8+ sub a, (iy+3)+ add a, a+ add a, a+ add a, a+ add a, a+ ld (e_f), a+ ; d = yb - ya+ ld hl, (e_yb)+ ld de, (iy+3)+ or a, a+ sbc hl, de+ ld (e_d), hl+ ; the deltas, x 16+ ld hl, (e_b)+ ld de, (hl) ; xb+ ex de, hl+ ld de, (iy+0)+ or a, a+ sbc hl, de+ call x16+ ld (e_dx), hl+ ld hl, (e_b)+ ld de, 6+ add hl, de+ ld de, (hl) ; ub+ ex de, hl+ ld de, (iy+6)+ or a, a+ sbc hl, de+ call x16+ ld (e_du), hl+ ld hl, (e_b)+ ld de, 9+ add hl, de+ ld de, (hl) ; vb+ ex de, hl+ ld de, (iy+9)+ or a, a+ sbc hl, de+ call x16+ ld (e_dv), hl+ ld hl, (e_d)+ ld de, 16+ or a, a+ sbc hl, de+ jr c, .Led_short+ ; d >= 16: the steps, delta x 2^28 / d >> 24+ add hl, hl ; (d - 16) * 3+ ld de, (e_d)+ add hl, de+ ld de, -16+ add hl, de+ ld de, (_rs_recip)+ add hl, de+ ld de, (hl)+ ld (e_r), de+ ld hl, (e_dx)+ call mulhi+ ld (ix+W_SX), hl+ ld de, (e_r)+ ld hl, (e_du)+ call mulhi+ ld (ix+W_SU), hl+ ld de, (e_r)+ ld hl, (e_dv)+ call mulhi+ ld (ix+W_SV), hl+ ; the first row: the vertex + step x f / 16+ ld hl, (ix+W_SX)+ ld a, (e_f)+ call mul8+ ld (e_dx), hl+ ld hl, (ix+W_SU)+ ld a, (e_f)+ call mul8+ ld (e_du), hl+ ld hl, (ix+W_SV)+ ld a, (e_f)+ call mul8+ ld (e_dv), hl+ jr .Led_start+.Led_short:+ ; d < 16 (one row): delta x f / d+ xor a, a+ sbc hl, hl+ ld (ix+W_SX), hl+ ld (ix+W_SU), hl+ ld (ix+W_SV), hl+ ld a, (e_d)+ add a, a+ add a, a+ add a, a+ add a, a+ ld c, a+ ld a, (e_f)+ rrca+ rrca+ rrca+ rrca+ add a, c ; d * 16 + f+ ld de, 0+ ld e, a+ ld hl, (_rs_ratio)+ add hl, de+ ld a, (hl)+ ld (e_q), a+ ld hl, (e_dx)+ call mul8+ ld (e_dx), hl+ ld a, (e_q)+ ld hl, (e_du)+ call mul8+ ld (e_du), hl+ ld a, (e_q)+ ld hl, (e_dv)+ call mul8+ ld (e_dv), hl+.Led_start:+ ; x = xa * 16 + e_dx, ...+ ld hl, (iy+0)+ call x16+ ld de, (e_dx)+ add hl, de+ ld (ix+W_X), hl+ ld hl, (iy+6)+ call x16+ ld de, (e_du)+ add hl, de+ ld (ix+W_U), hl+ ld hl, (iy+9)+ call x16+ ld de, (e_dv)+ add hl, de+ ld (ix+W_V), hl+ pop bc+ or a, 1 ; nz: found+ ret+.Led_next:+ pop bc+ dec b+ jp nz, .Led_try+ xor a, a ; z: none+ ret++x16:+ add hl, hl+ add hl, hl+ add hl, hl+ add hl, hl+ ret++; de: a table of 16 steps of hl (texel x 65536 a pixel): hl x (k x 16 + 8) / 256, k 0..15+ktable:+ push de+ push hl+ ld a, 8+ call mul8+ ld (kt_base), hl+ pop hl+ ld a, 16+ call mul8+ ex de, hl ; the step+ pop iy+ ld hl, (kt_base)+ ld b, 16+.Lkt:+ ld (iy+0), hl+ lea iy, iy+3+ add hl, de+ djnz .Lkt+ ret++; ------------------------------------------------------------------------------------------------- rs_poly+; void rs_poly(const SV *p, uint8_t n)+ .section .text._rs_poly+ .global _rs_poly+ .type _rs_poly, @function+_rs_poly:+ push ix+ ld iy, 0+ add iy, sp+ ld hl, (iy+6)+ ld (p_base), hl+ xor a, a+ ld (r_lock), a+ ld a, (iy+9)+ ld (p_n), a+ ; the top vertex, the lowest and highest y+ ld b, a+ ld c, 0 ; index+ ld ix, (p_base)+ ld hl, (ix+3)+ ld (e_ymin), hl+ ld (e_ymax), hl+ xor a, a+ ld (e_top), a+.Lpo_scan:+ ld hl, (ix+3)+ ld de, (e_ymin)+ or a, a+ sbc hl, de+ jr nc, .Lpo_notmin+ add hl, de+ ld (e_ymin), hl+ ld a, c+ ld (e_top), a+ jr .Lpo_nextv+.Lpo_notmin:+ add hl, de+ ld de, (e_ymax)+ or a, a+ sbc hl, de+ jr c, .Lpo_nextv+ add hl, de+ ld (e_ymax), hl+.Lpo_nextv:+ lea ix, ix+12+ inc c+ djnz .Lpo_scan+ ; rows y .. yend+ ld hl, (e_ymin)+ ld de, 7+ add hl, de+ call x16+ ld a, h+ ld (r_y), a+ ld hl, (e_ymax)+ add hl, de+ call x16+ ld a, h+ ld (r_yend), a+ ld hl, r_y+ sub a, (hl)+ jp z, .Lpo_done+ jp c, .Lpo_done+ ; the walkers: a forwards, b backwards+ ld a, (e_top)+ ld (wa+W_IDX), a+ ld (wb+W_IDX), a+ ld a, 1+ ld (wa+W_DIR), a+ ld a, (p_n)+ dec a+ ld (wb+W_DIR), a+ ld ix, wa+ call edge+ jp z, .Lpo_done+ ld ix, wb+ call edge+ jp z, .Lpo_done+ ; which is left: the smaller x (or step)+ ld hl, wa+ ld (w_l), hl+ ld hl, wb+ ld (w_r), hl+ ld hl, (wa+W_X)+ ld de, (wb+W_X)+ or a, a+ sbc hl, de+ jr z, .Lpo_tie+ jp m, .Lpo_row ; (x small: no overflow)+ jr .Lpo_swap+.Lpo_tie:+ ld hl, (wa+W_SX)+ ld de, (wb+W_SX)+ or a, a+ sbc hl, de+ jp m, .Lpo_row+ jr z, .Lpo_row+.Lpo_swap:+ ld hl, wb+ ld (w_l), hl+ ld hl, wa+ ld (w_r), hl+ ; the row's start in rbuf+.Lpo_row:+ ld a, (r_y)+ ld l, a+ ld h, RW+ mlt hl+ ld de, (_rbuf)+ add hl, de+ ld (r_row), hl+.Lpo_loop:+ ; px0 = (xl + 127) >> 8 (0 if xl < 0), px1 = (xr + 127) >> 8, at most 160+ ld ix, (w_l)+ ld hl, (ix+W_X)+ ld de, 127+ add hl, de+ ld (m_t), hl+ ld a, (m_t+2)+ bit 7, a+ ld a, (m_t+1)+ jr z, .Lpo_x0+ xor a, a+.Lpo_x0:+ ld (r_px0), a+ ld ix, (w_r)+ ld hl, (ix+W_X)+ add hl, de+ ld (m_t), hl+ ld a, (m_t+2)+ or a, a+ ld a, (m_t+1)+ jr z, .Lpo_x1+ ld a, RW ; (past the right: never, on screen)+.Lpo_x1:+ cp a, RW + 1+ jr c, .Lpo_x1ok+ ld a, RW+.Lpo_x1ok:+ ld hl, r_px0+ sub a, (hl)+ jp z, .Lpo_step+ jp c, .Lpo_step+ ld (r_n), a+ ld a, (r_lock)+ or a, a+ jp nz, .Lpo_locked+ ; w = (xr - xl) >> 4: du, dv across it (0 under a pixel)+ ld hl, (ix+W_X)+ ld ix, (w_l)+ ld de, (ix+W_X)+ or a, a+ sbc hl, de+ call x16+ ld (m_t), hl+ ld hl, (m_t+1) ; (xr - xl) >> 4 (m_t+3 is 0)+ ld de, 16+ or a, a+ sbc hl, de+ jr nc, .Lpo_wide+ or a, a+ sbc hl, hl+ ld (r_du), hl+ ld (r_dv), hl+ jp .Lpo_grad+.Lpo_wide:+ push hl+ ld de, 48+ or a, a+ sbc hl, de ; (w >= 64: four pixels or more, the gradient kept for the rows below)+ pop hl+ jr c, .Lpo_nolock+ ld a, 1+ ld (r_lock), a+.Lpo_nolock:+ push hl ; (w - 16) * 3+ pop de+ add hl, hl+ add hl, de+ ld de, (_rs_recip)+ add hl, de+ ld de, (hl)+ ld (e_r), de+ ld ix, (w_r)+ ld hl, (ix+W_U)+ ld ix, (w_l)+ ld de, (ix+W_U)+ or a, a+ sbc hl, de+ ld de, (e_r)+ call mulhi+ ld (r_du), hl+ ld ix, (w_r)+ ld hl, (ix+W_V)+ ld ix, (w_l)+ ld de, (ix+W_V)+ or a, a+ sbc hl, de+ ld de, (e_r)+ call mulhi+ ld (r_dv), hl+ ld a, (r_lock)+ or a, a+ jr z, .Lpo_grad+ ; locked: k/16 + 1/32 of a pixel's step, k 0..15, to step from the edge to a pixel's middle+ ld hl, (r_du)+ ld de, r_ku+ call ktable+ ld hl, (r_dv)+ ld de, r_kv+ call ktable+ ; the steps in the span's units: du >> 8, dv >> 4 (kept for the rows below)+ ld hl, (r_du)+ ld (m_t), hl+ ld hl, (m_t+1)+ ld (r_du), hl+ ld hl, (r_dv)+ call x16+ ld (m_t), hl+ ld hl, (m_t+1)+ ld (r_dv), hl+ jr .Lpo_locked+.Lpo_grad:+ ; frac = 128 - xl (low byte): from the edge to the first pixel's middle+ ld ix, (w_l)+ ld a, 128+ sub a, (ix+W_X)+ ld (e_q), a+ ld hl, (r_du)+ call mul8+ ld de, (ix+W_U)+ add hl, de+ call clamp20+ ld (m_t), hl+ ld hl, (m_t+1)+ ld (r_u), hl ; u >> 8: 8.8+ ld a, (e_q)+ ld hl, (r_dv)+ call mul8+ ld de, (ix+W_V)+ add hl, de+ call clamp20+ call x16+ ld (m_t), hl+ ld hl, (m_t+1)+ ld (r_v), hl ; v >> 4: 4.12+ ld hl, (r_du)+ ld (m_t), hl+ ld hl, (m_t+1)+ ld (r_du), hl ; du >> 8+ ld hl, (r_dv)+ call x16+ ld (m_t), hl+ ld hl, (m_t+1)+ ld (r_dv), hl ; dv >> 4+ jr .Lpo_span+.Lpo_locked:+ ; from the edge to the first pixel's middle: the table's step for (128 - xl) >> 4+ ld ix, (w_l)+ ld a, 128+ sub a, (ix+W_X)+ rrca+ rrca+ rrca+ rrca+ and a, 15+ ld l, a+ ld h, 3+ mlt hl+ push hl+ ld de, r_ku+ add hl, de+ ld de, (hl)+ ld hl, (ix+W_U)+ add hl, de+ call clamp20+ ld (m_t), hl+ ld hl, (m_t+1)+ ld (r_u), hl+ pop hl+ ld de, r_kv+ add hl, de+ ld de, (hl)+ ld hl, (ix+W_V)+ add hl, de+ call clamp20+ call x16+ ld (m_t), hl+ ld hl, (m_t+1)+ ld (r_v), hl+.Lpo_span:+ ld a, (r_px0)+ ld de, 0+ ld e, a+ ld hl, (r_row)+ add hl, de+ ex de, hl+ ld a, (r_n)+ call span+.Lpo_step:+ ld hl, r_y+ inc (hl)+ ld a, (r_yend)+ cp a, (hl)+ jr z, .Lpo_done+ ld hl, (r_row)+ ld de, RW+ add hl, de+ ld (r_row), hl+ ld ix, (w_l)+ call wstep+ ld ix, (w_r)+ call wstep+ jp nz, .Lpo_loop+.Lpo_done:+ pop ix+ ret++ .section .text._rs_wstep+; the walker ix a row on; its next edge when this one is done; nz: going on+wstep:+ ld hl, (ix+W_X)+ ld de, (ix+W_SX)+ add hl, de+ ld (ix+W_X), hl+ ld hl, (ix+W_U)+ ld de, (ix+W_SU)+ add hl, de+ ld (ix+W_U), hl+ ld hl, (ix+W_V)+ ld de, (ix+W_SV)+ add hl, de+ ld (ix+W_V), hl+ dec (ix+W_ROWS)+ ret nz+ call edge+ ret nz+ pop hl ; (none left: the polygon is done)+ jp .Lpo_done++; hl into 0..0xFFFFF+clamp20:+ ld (m_t), hl+ ld a, (m_t+2)+ bit 7, a+ jr nz, .Lcl_zero+ cp a, 0x10+ ret c+ ld hl, 0xFFFFF+ ret+.Lcl_zero:+ or a, a+ sbc hl, hl+ ret++; ------------------------------------------------------------------------------------------------- rs_proj+; void rs_proj(int x, int y, int z, int *out)+; z shifted up until its top bit falls out (c shifts): the next 8 bits pick t = 1371 << 12 / (256 + i);+; x and y shifted up c - 3 (the top 16 bits kept), times t, >> 16: the offset from the middle in 1/16 pixels+ .section .text._rs_proj+ .global _rs_proj+ .type _rs_proj, @function+_rs_proj:+ ld iy, 0+ add iy, sp+ ld hl, (iy+9) ; z+ call pr_z+ ld hl, (iy+3)+ call pr_mul+ ld de, 1280+ add hl, de+ push hl+ ld hl, (iy+6)+ call pr_mul+ ex de, hl+ ld hl, 960+ or a, a+ sbc hl, de+ ld iy, (iy+12)+ ld (iy+3), hl+ pop hl+ ld (iy+0), hl+ ret++ .section .text._rs_prz+; m_r = t, m_f = the shift, for z (hl)+pr_z:+ ld b, 0+.Lpr_norm:+ add hl, hl+ jr c, .Lpr_found+ inc b+ jr .Lpr_norm+.Lpr_found:+ ld (m_t), hl+ ld a, (m_t+2) ; the index: the 8 bits after the top one+ or a, a+ sbc hl, hl+ ld l, a+ add hl, hl+ ld de, r_rtab+ add hl, de+ ld de, (hl)+ ld (m_r), de ; t+ ld a, b+ sub a, 3+ ld (m_f), a ; the shift+ ret++; hl = ((hl << m_f), its top 16 bits) x t >> 16, the sign apart+pr_mul:+ ld a, (m_f)+ or a, a+ jr z, .Lpm_shifted+ ld b, a+.Lpm_shift:+ add hl, hl+ djnz .Lpm_shift+.Lpm_shifted:+ ld (m_a), hl+ xor a, a+ ld (m_neg), a+ ld a, (m_a+2)+ bit 7, a+ jr z, .Lpm_pos+ ex de, hl+ or a, a+ sbc hl, hl+ sbc hl, de+ ld (m_a), hl+ ld a, 1+ ld (m_neg), a+.Lpm_pos:+ ; (ah al) x (th tl) >> 16 = ah th + (ah tl + al th + (al tl >> 8)) >> 8, a = m_a+1, m_a+2+ ld a, (m_a+1)+ ld h, a+ ld a, (m_r)+ ld l, a+ mlt hl ; al tl+ ld a, h+ ld de, 0+ ld e, a+ push de+ ld a, (m_a+2)+ ld h, a+ ld a, (m_r)+ ld l, a+ mlt hl ; ah tl+ ld a, (m_a+1)+ ld d, a+ ld a, (m_r+1)+ ld e, a+ mlt de ; al th+ add hl, de+ pop de+ add hl, de+ ld (m_t), hl+ ld hl, (m_t+1) ; >> 8+ ld a, (m_a+2)+ ld d, a+ ld a, (m_r+1)+ ld e, a+ mlt de ; ah th+ add hl, de+ jp neg_if++; ------------------------------------------------------------------------------------------------- rs_hidden+; uint8_t rs_hidden(int x, int y, int z, int r)+; 1 when the pixels around (x, y, z)'s projection, as far as r's projection and a pixel more each way, are all+; drawn already (or off the screen)+ .section .text._rs_hidden+ .global _rs_hidden+ .type _rs_hidden, @function+_rs_hidden:+ ld iy, 0+ add iy, sp+ ld hl, (iy+9)+ call pr_z+ ld hl, (iy+12)+ call pr_mul+ ld de, 16+ add hl, de+ ld (h_r), hl+ ld hl, (iy+6)+ call pr_mul+ ex de, hl+ ld hl, 960+ or a, a+ sbc hl, de+ ld c, 119+ call h_range+ jr c, .Lh_yes+ ld a, b+ ld (h_y0), a+ ld a, c+ ld (h_rows), a+ ld hl, (iy+3)+ call pr_mul+ ld de, 1280+ add hl, de+ ld c, RW - 1+ call h_range+ jr c, .Lh_yes+ ld a, c+ ld (h_w), a+ ld a, (h_y0)+ ld h, a+ ld l, RW+ mlt hl+ ld de, (_rbuf)+ add hl, de+ ld de, 0+ ld e, b+ add hl, de+.Lh_row:+ push hl+ ld bc, 0+ ld a, (h_w)+ ld c, a+ xor a, a+ cpir+ pop hl+ jr z, .Lh_no+ ld de, RW+ add hl, de+ ld a, (h_rows)+ dec a+ ld (h_rows), a+ jr nz, .Lh_row+.Lh_yes:+ ld a, 1+ ret+.Lh_no:+ xor a, a+ ret++; hl the middle (1/16 pixels), c the last pixel: b the first pixel of (hl - h_r, hl + h_r), c how many; carry: none+h_range:+ ld (h_c), hl+ ld de, (h_r)+ or a, a+ sbc hl, de+ call h_px+ jr nc, .Lhr_lo+ xor a, a ; (from 0)+.Lhr_lo:+ cp a, c+ jr z, .Lhr_lok+ jr nc, .Lhr_off ; (past the last)+.Lhr_lok:+ ld b, a+ ld hl, (h_c)+ ld de, (h_r)+ add hl, de+ call h_px+ ret c ; (before the first)+ cp a, c+ jr c, .Lhr_hok+ ld a, c+.Lhr_hok:+ sub a, b+ inc a+ ld c, a+ or a, a+ ret+.Lhr_off:+ scf+ ret++; a = hl >> 4 (hl in 1/16 pixels, |hl| < 2^19), 255 past 255; carry: hl < 0+h_px:+ add hl, hl+ ret c+ add hl, hl+ add hl, hl+ add hl, hl+ ld (m_t), hl+ ld a, (m_t+2)+ or a, a+ ld a, (m_t+1)+ ret z+ ld a, 255+ or a, a+ ret++; ------------------------------------------------------------------------------------------------- rs_expand+; void rs_expand(const uint8_t *src, uint8_t *dst, const uint8_t *sky, const uint8_t *hud)+; 120 rows of 160 to 240 rows of 320; 0 is the row's sky colour; from screen row 180 on, the HUD's layer+; (187 wide at x 67, 0 see-through) over both rows, the rows made in a buffer first (nothing flickers)+ .section .text._rs_expand+ .global _rs_expand+ .type _rs_expand, @function+ .equ HUD_X, 67+ .equ HUD_W, 187+_rs_expand:+ push ix+ ld iy, 0+ add iy, sp+ ld hl, (iy+15)+ ld (r_hud), hl+ ld a, 31 ; (rows left under 31: under the HUD; none without one)+ add hl, de+ or a, a+ sbc hl, de+ jr nz, .Lex_withhud+ xor a, a+.Lex_withhud:+ ld (r_hudlim), a+ ld hl, (iy+6) ; src+ ld de, (iy+9) ; dst+ ld ix, (iy+12) ; sky+ ld a, 120+ ld (r_rows), a+ ld iy, r_hudlim+.Lex_row:+ ld a, (r_rows)+ cp a, (iy)+ jr c, .Lex_hudrow+ ld c, (ix+0)+ ld b, RW+.Lex_px:+ ld a, (hl)+ inc hl+ or a, a+ jr nz, .Lex_set+ ld a, c+.Lex_set:+ ld (de), a+ inc de+ ld (de), a+ inc de+ djnz .Lex_px+ push hl+ ld hl, -320+ add hl, de+ ld bc, 320+ ldir+ pop hl+.Lex_next:+ inc ix+ ld a, (r_rows)+ dec a+ ld (r_rows), a+ jr nz, .Lex_row+ pop ix+ ret++.Lex_hudrow:+ push de+ ld de, r_line+ ld c, (ix+0)+ ld b, RW+.Lex_hpx:+ ld a, (hl)+ inc hl+ or a, a+ jr nz, .Lex_hset+ ld a, c+.Lex_hset:+ ld (de), a+ inc de+ ld (de), a+ inc de+ djnz .Lex_hpx+ push hl+ ld hl, r_line+ ld de, r_line2+ ld bc, 320+ ldir+ ld de, r_line + HUD_X+ ld hl, (r_hud)+ call r_overlay+ ld de, r_line2 + HUD_X+ call r_overlay+ ld (r_hud), hl+ pop hl+ pop de+ push hl+ ld hl, r_line+ ld bc, 640+ ldir+ pop hl+ jr .Lex_next++; HUD_W pixels of the layer (hl) over de, 0 see-through; hl after them+r_overlay:+ ld b, HUD_W+.Lov_px:+ ld a, (hl)+ inc hl+ or a, a+ jr z, .Lov_skip+ ld (de), a+.Lov_skip:+ inc de+ djnz .Lov_px+ ret+++; ------------------------------------------------------------------------------------------------- rs_cells+; uint8_t rs_cells(const uint8_t *mask, uint8_t slot, uint8_t up, uint8_t down, int8_t *out)+; a column's cells with a face (mask: 32 bits, bit y & 31): from slot up (0, 1, .. up - 1 levels above), then+; down (-1, -2, .. -down): the offsets of those set, in that order; how many+ .section .text._rs_cells+ .global _rs_cells+ .type _rs_cells, @function+_rs_cells:+ push ix+ ld ix, 0+ add ix, sp+ ld iy, (ix+18) ; out+ ld hl, (ix+6)+ ld de, cl_mask+ ld bc, 4+ ldir ; (the mask, twice over: no wrapping to mind)+ ld hl, (ix+6)+ ld bc, 4+ ldir+ ld c, 0 ; how many+ ; up: bit slot onwards+ ld a, (ix+12)+ or a, a+ jr z, .Lcl_down+ ld b, a ; levels+ ld a, (ix+9)+ call cl_at ; hl: the byte, e: the bit+ ld d, 0 ; offset+.Lcl_up:+ ld a, (hl)+ and a, e+ jr z, .Lcl_up_no+ ld (iy), d+ inc iy+ inc c+.Lcl_up_no:+ inc d+ rlc e+ jr nc, .Lcl_up_same+ inc hl+.Lcl_up_same:+ djnz .Lcl_up+.Lcl_down:+ ld a, (ix+15)+ or a, a+ jr z, .Lcl_done+ ld b, a+ ld a, (ix+9)+ add a, 31 ; slot - 1 (+ 32)+ call cl_at+ ld d, -1+.Lcl_dn:+ ld a, (hl)+ and a, e+ jr z, .Lcl_dn_no+ ld (iy), d+ inc iy+ inc c+.Lcl_dn_no:+ dec d+ rrc e+ jr nc, .Lcl_dn_same+ dec hl+.Lcl_dn_same:+ djnz .Lcl_dn+.Lcl_done:+ ld a, c+ pop ix+ ret++; a: a bit 0..63 of cl_mask: hl its byte, e its bit (bc kept)+cl_at:+ push af+ rrca+ rrca+ rrca+ and a, 7+ ld hl, cl_mask+ ld de, 0+ ld e, a+ add hl, de+ pop af+ and a, 7+ ld e, 1+ ret z+.Lcla_shift:+ rlc e+ dec a+ jr nz, .Lcla_shift+ ret++; ------------------------------------------------------------------------------------------------- rs_column+; void rs_column(const uint8_t *mask, int h2, int Y0, int Z0, int V0, int cx)+; a column's cells with a face (rs_cells' order), each in the view (distance, sides, top and bottom) and not+; hidden by what is drawn already: rs_cell(Y, Z, fog, j + 32, its y slot)+ .section .text._rs_column+ .global _rs_column+ .type _rs_column, @function+_rs_column:+ push ix+ ld ix, 0+ add ix, sp+ ld hl, (ix+9)+ ld (c_h2), hl+ ld hl, (ix+12)+ ld (c_Y0), hl+ ld hl, (ix+15)+ ld (c_Z0), hl+ ld hl, (ix+18)+ ld de, (_rs_vzk)+ add hl, de+ ld (c_V0), hl ; (V0 + vz0 + KY)+ ld hl, (ix+21)+ ld (c_cx), hl+ ld de, (_rs_KX)+ or a, a+ sbc hl, de+ ld (c_xa), hl ; cx - KX+ add hl, de+ add hl, de+ ld (c_xb), hl ; cx + KX+ ld hl, c_list+ push hl+ ld a, (_rs_down)+ ld l, a+ push hl+ ld a, (_rs_up)+ ld l, a+ push hl+ ld a, (_rs_eys)+ ld l, a+ push hl+ ld hl, (ix+6)+ push hl+ call _rs_cells+ pop hl+ pop hl+ pop hl+ pop hl+ pop hl+ ld (c_left), a+ ld hl, c_list+ ld (c_ptr), hl+.Lco_next:+ ld a, (c_left)+ or a, a+ jp z, .Lco_done+ dec a+ ld (c_left), a+ ld hl, (c_ptr)+ ld a, (hl)+ inc hl+ ld (c_ptr), hl+ ld (c_lj), a+ ld hl, _rs_jbase+ add a, (hl)+ add a, 32+ ld (c_o), a+ ; d2 = h2 + j^2, past the view: passed by+ or a, a+ sbc hl, hl+ ld l, a+ add hl, hl+ ld de, _rs_jS+ add hl, de+ ld de, 0+ ld e, (hl)+ inc hl+ ld d, (hl)+ ld hl, (c_h2)+ add hl, de+ ld (c_d2), hl+ ex de, hl+ ld hl, (_rs_vlim)+ or a, a+ sbc hl, de+ jr c, .Lco_next+ ; Y, Z, V from the tables (3 bytes each)+ ld a, (c_o)+ ld l, a+ ld h, 3+ mlt hl+ push hl+ ld de, _rs_jY+ add hl, de+ ld de, (hl)+ ld hl, (c_Y0)+ add hl, de+ ld (c_Y), hl+ ld de, (_rs_cy0)+ add hl, de+ ld (c_cy), hl+ pop hl+ push hl+ ld de, _rs_jZ+ add hl, de+ ld de, (hl)+ ld hl, (c_Z0)+ add hl, de+ ld (c_Z), hl+ ld de, (_rs_cz0)+ add hl, de+ ld (c_cz), hl+ pop hl+ ld de, _rs_jV+ add hl, de+ ld de, (hl)+ ld hl, (c_V0)+ add hl, de+ ld (c_vz), hl+ ; the view: cz >= -R, |cx| <= cz + KX, |cy| <= vz+ ld hl, (c_cz)+ ld de, (_rs_R)+ or a, a+ adc hl, de+ jp m, .Lco_next+ ld hl, (c_cz)+ ld de, (c_xa)+ or a, a+ sbc hl, de+ jp m, .Lco_next+ ld hl, (c_cz)+ ld de, (c_xb)+ or a, a+ adc hl, de+ jp m, .Lco_next+ ld hl, (c_vz)+ ld de, (c_cy)+ or a, a+ sbc hl, de+ jp m, .Lco_next+ ld hl, (c_vz)+ or a, a+ adc hl, de+ jp m, .Lco_next+ ; the fog+ ld hl, (c_d2)+ ld de, _rs_fog+ add hl, de+ ld a, (hl)+ cp a, 9+ jp nc, .Lco_next+ ld (c_fog), a+ ; hidden? (far enough to tell: cz > 2R)+ ld hl, (_rs_R)+ add hl, hl+ ld de, (c_cz)+ or a, a+ sbc hl, de+ jp p, .Lco_draw+ ld hl, (_rs_R)+ push hl+ push de+ ld hl, (c_cy)+ push hl+ ld hl, (c_cx)+ push hl+ call _rs_hidden+ pop hl+ pop hl+ pop hl+ pop hl+ or a, a+ jp nz, .Lco_next+.Lco_draw:+ ld a, (c_lj)+ ld hl, _rs_eys+ add a, (hl)+ and a, 31+ ld l, a+ push hl+ ld a, (c_o)+ ld l, a+ push hl+ ld a, (c_fog)+ ld l, a+ push hl+ ld hl, (c_Z)+ push hl+ ld hl, (c_Y)+ push hl+ call _rs_cell+ pop hl+ pop hl+ pop hl+ pop hl+ pop hl+ jp .Lco_next+.Lco_done:+ pop ix+ ret++; ------------------------------------------------------------------------------------------------- rs_quad+; void rs_quad(void)+; a quadrant's columns (rs_q*), rows out from the eye along x, columns out along z, near to far: those in the+; window, in the view's distance and sides and with a face, to rs_column (rs_x, rs_z, rs_ci, rs_X0, rs_xs, rs_zs:+; the column)+ .section .text._rs_quad+ .global _rs_quad+ .type _rs_quad, @function+_rs_quad:+ push ix+ xor a, a+ ld (q_a), a+ ld a, (_rs_qxs0)+ ld (q_xs), a+ ld hl, (_rs_qx0)+ ld (q_x), hl+.Lq_row:+ ; x in the window?+ ld hl, (q_x)+ ld de, (_rs_wx0)+ or a, a+ sbc hl, de+ jp m, .Lq_done+ ld hl, (q_x)+ ld de, (_rs_wx1)+ or a, a+ sbc hl, de+ jp p, .Lq_done+ ; the row's first column+ ld hl, (_rs_qXi)+ ld (q_X), hl+ ld hl, (_rs_qYi)+ ld (q_Y), hl+ ld hl, (_rs_qZi)+ ld (q_Z), hl+ ld hl, (_rs_qVi)+ ld (q_V), hl+ ld a, (_rs_qzs0)+ ld (q_zs), a+ ld hl, (_rs_qz0)+ ld (q_z), hl+ ld a, (q_a)+ or a, a+ sbc hl, hl+ ld l, a+ add hl, hl+ ld de, (_rs_qsqi)+ add hl, de+ ld de, 0+ ld e, (hl)+ inc hl+ ld d, (hl)+ ld (q_hi), de ; |i| squared+ xor a, a+ ld (q_c), a+.Lq_col:+ ; h2 = |i|^2 + |k|^2, past the view: the row's end+ ld a, (q_c)+ or a, a+ sbc hl, hl+ ld l, a+ add hl, hl+ ld de, (_rs_qsqk)+ add hl, de+ ld de, 0+ ld e, (hl)+ inc hl+ ld d, (hl)+ ld hl, (q_hi)+ add hl, de+ ld (q_h2), hl+ ex de, hl+ ld hl, (_rs_vlim)+ or a, a+ sbc hl, de+ jp c, .Lq_rowend+ ; z in the window?+ ld hl, (q_z)+ ld de, (_rs_wz0)+ or a, a+ sbc hl, de+ jp m, .Lq_rowend+ ld hl, (q_z)+ ld de, (_rs_wz1)+ or a, a+ sbc hl, de+ jp p, .Lq_rowend+ ; the view's sides: |cx| <= Z + czm+ ld hl, (q_X)+ ld de, (_rs_cx0)+ add hl, de+ ld (q_cx), hl+ ex de, hl+ ld hl, (q_Z)+ ld bc, (_rs_czm)+ add hl, bc+ push hl+ or a, a+ sbc hl, de+ pop hl+ jp m, .Lq_next+ or a, a+ adc hl, de+ jp m, .Lq_next+ ; the column: ci = zs * 32 + xs, its mask+ ld a, (q_zs)+ ld l, a+ ld h, 32+ mlt hl+ ld a, (q_xs)+ ld de, 0+ ld e, a+ add hl, de+ ld (_rs_ci), hl+ add hl, hl+ add hl, hl+ ld de, _wexp+ add hl, de+ push hl+ ld a, (hl)+ inc hl+ or a, (hl)+ inc hl+ or a, (hl)+ inc hl+ or a, (hl)+ pop de+ jp z, .Lq_next+ ld hl, (q_x)+ ld (_rs_x), hl+ ld hl, (q_z)+ ld (_rs_z), hl+ ld hl, (q_X)+ ld (_rs_X0), hl+ ld a, (q_xs)+ ld (_rs_xs), a+ ld a, (q_zs)+ ld (_rs_zs), a+ ld hl, (q_cx)+ push hl+ ld hl, (q_V)+ push hl+ ld hl, (q_Z)+ push hl+ ld hl, (q_Y)+ push hl+ ld hl, (q_h2)+ push hl+ push de+ call _rs_column+ ld hl, 18+ add hl, sp+ ld sp, hl+.Lq_next:+ ; the next column out along z+ ld a, (q_c)+ inc a+ ld (q_c), a+ ld hl, _rs_vd+ cp a, (hl)+ jr z, .Lq_cok+ jp nc, .Lq_rowend+.Lq_cok:+ ld a, (_rs_qsz)+ ld hl, q_zs+ add a, (hl)+ and a, 31+ ld (hl), a+ ld hl, (q_z)+ ld de, (_rs_qszi)+ add hl, de+ ld (q_z), hl+ ld hl, (q_X)+ ld de, (_rs_qsXz)+ add hl, de+ ld (q_X), hl+ ld hl, (q_Y)+ ld de, (_rs_qsYz)+ add hl, de+ ld (q_Y), hl+ ld hl, (q_Z)+ ld de, (_rs_qsZz)+ add hl, de+ ld (q_Z), hl+ ld hl, (q_V)+ ld de, (_rs_qsVz)+ add hl, de+ ld (q_V), hl+ jp .Lq_col+.Lq_rowend:+ ; the next row out along x+ ld a, (q_a)+ inc a+ ld (q_a), a+ ld hl, _rs_vd+ cp a, (hl)+ jr z, .Lq_aok+ jp nc, .Lq_done+.Lq_aok:+ ld a, (_rs_qsx)+ ld hl, q_xs+ add a, (hl)+ and a, 31+ ld (hl), a+ ld hl, (q_x)+ ld de, (_rs_qsxi)+ add hl, de+ ld (q_x), hl+ ld hl, (_rs_qXi)+ ld de, (_rs_qsXx)+ add hl, de+ ld (_rs_qXi), hl+ ld hl, (_rs_qYi)+ ld de, (_rs_qsYx)+ add hl, de+ ld (_rs_qYi), hl+ ld hl, (_rs_qZi)+ ld de, (_rs_qsZx)+ add hl, de+ ld (_rs_qZi), hl+ ld hl, (_rs_qVi)+ ld de, (_rs_qsVx)+ add hl, de+ ld (_rs_qVi), hl+ jp .Lq_row+.Lq_done:+ pop ix+ ret++; ------------------------------------------------------------------------------------------------- data+ .section .data._rs_vars+r_tex: .d24 0+r_cm: .d24 0+r_loop: .d24 span_plain+r_u: .d24 0+r_v: .d24 0+r_du: .d24 0+r_dv: .d24 0+r_end: .db 0+r_row: .d24 0+r_y: .db 0+r_yend: .db 0+r_px0: .db 0+r_n: .db 0+m_a: .d24 0+m_r: .d24 0+m_f: .d24 0+m_neg: .db 0+m_t: .d24 0+ .db 0+p_base: .d24 0+p_n: .db 0+e_b: .d24 0+e_yb: .d24 0+e_r1: .db 0+e_f: .db 0+e_q: .db 0+e_d: .d24 0+e_dx: .d24 0+e_du: .d24 0+e_dv: .d24 0+e_r: .d24 0+e_ymin: .d24 0+e_ymax: .d24 0+e_top: .db 0+w_l: .d24 0+w_r: .d24 0+wa: .ds W_SIZE+wb: .ds W_SIZE+r_hud: .d24 0+r_rows: .db 0+r_hudlim: .db 0+r_line: .ds 320+r_line2: .ds 320+cl_mask: .ds 8+r_lock: .db 0+r_ku: .ds 48+r_kv: .ds 48+kt_base: .d24 0+h_r: .d24 0+h_c: .d24 0+h_y0: .db 0+h_rows: .db 0+h_w: .db 0+c_h2: .d24 0+c_Y0: .d24 0+c_Z0: .d24 0+c_V0: .d24 0+c_cx: .d24 0+c_xa: .d24 0+c_xb: .d24 0+c_ptr: .d24 0+c_d2: .d24 0+c_Y: .d24 0+c_cy: .d24 0+c_Z: .d24 0+c_cz: .d24 0+c_vz: .d24 0+c_left: .db 0+c_lj: .db 0+c_o: .db 0+c_fog: .db 0+c_list: .ds 64+ .global _rs_jY+ .global _rs_jZ+ .global _rs_jV+ .global _rs_jS+ .global _rs_fog+ .global _rs_vlim+ .global _rs_cy0+ .global _rs_cz0+ .global _rs_vzk+ .global _rs_R+ .global _rs_KX+ .global _rs_eys+ .global _rs_up+ .global _rs_down+ .global _rs_jbase+_rs_jY: .ds 65 * 3+_rs_jZ: .ds 65 * 3+_rs_jV: .ds 65 * 3+_rs_jS: .ds 128 * 2+_rs_fog: .ds 257+_rs_vlim: .d24 0+_rs_cy0: .d24 0+_rs_cz0: .d24 0+_rs_vzk: .d24 0+_rs_R: .d24 0+_rs_KX: .d24 0+_rs_eys: .db 0+_rs_up: .db 0+_rs_down: .db 0+_rs_jbase: .db 0+ .global _rs_qXi+ .global _rs_qYi+ .global _rs_qZi+ .global _rs_qVi+ .global _rs_qsXx+ .global _rs_qsYx+ .global _rs_qsZx+ .global _rs_qsVx+ .global _rs_qsXz+ .global _rs_qsYz+ .global _rs_qsZz+ .global _rs_qsVz+ .global _rs_qx0+ .global _rs_qz0+ .global _rs_qsxi+ .global _rs_qszi+ .global _rs_qsqi+ .global _rs_qsqk+ .global _rs_czm+ .global _rs_cx0+ .global _rs_wx0+ .global _rs_wx1+ .global _rs_wz0+ .global _rs_wz1+ .global _rs_x+ .global _rs_z+ .global _rs_ci+ .global _rs_X0+ .global _rs_qsx+ .global _rs_qsz+ .global _rs_qxs0+ .global _rs_qzs0+ .global _rs_vd+ .global _rs_xs+ .global _rs_zs+_rs_qXi: .d24 0+_rs_qYi: .d24 0+_rs_qZi: .d24 0+_rs_qVi: .d24 0+_rs_qsXx: .d24 0+_rs_qsYx: .d24 0+_rs_qsZx: .d24 0+_rs_qsVx: .d24 0+_rs_qsXz: .d24 0+_rs_qsYz: .d24 0+_rs_qsZz: .d24 0+_rs_qsVz: .d24 0+_rs_qx0: .d24 0+_rs_qz0: .d24 0+_rs_qsxi: .d24 0+_rs_qszi: .d24 0+_rs_qsqi: .d24 0+_rs_qsqk: .d24 0+_rs_czm: .d24 0+_rs_cx0: .d24 0+_rs_wx0: .d24 0+_rs_wx1: .d24 0+_rs_wz0: .d24 0+_rs_wz1: .d24 0+_rs_x: .d24 0+_rs_z: .d24 0+_rs_ci: .d24 0+_rs_X0: .d24 0+_rs_qsx: .db 0+_rs_qsz: .db 0+_rs_qxs0: .db 0+_rs_qzs0: .db 0+_rs_vd: .db 0+_rs_xs: .db 0+_rs_zs: .db 0+q_X: .d24 0+q_Y: .d24 0+q_Z: .d24 0+q_V: .d24 0+q_x: .d24 0+q_z: .d24 0+q_hi: .d24 0+q_h2: .d24 0+q_cx: .d24 0+q_a: .db 0+q_c: .db 0+q_xs: .db 0+q_zs: .db 0++ .section .rodata._rs_rtab+; 1371 << 12 / (256 + i)+r_rtab:+ .irp i, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255+ .dw (1371 * 4096) / (256 + \i)+ .endr++ .extern _rbuf+ .extern _rs_cell+ .extern _wexp+ .extern _rs_recip+ .extern _rs_ratiodiff --git a/games/numblocks/ce/src/save.c b/games/numblocks/ce/src/save.cindex eed78d29..3b867f42 100644--- a/games/numblocks/ce/src/save.c+++ b/games/numblocks/ce/src/save.c@@ -83,7 +83,7 @@ bool save_world(void) {h.n_tiles = (uint8_t)n_tiles;h.n_edits = (uint16_t)n_edits;/* the header and the tiles, then the edits */- static uint8_t buf[sizeof(Header) + MAX_TILES * sizeof(Tile)];+ uint8_t *buf = rbuf; /* (the 3D view's picture: not drawn while saving) */memcpy(buf, &h, sizeof h);memcpy(buf + sizeof h, tiles, n_tiles * sizeof(Tile));char n[8];diff --git a/games/numblocks/ce/src/world.c b/games/numblocks/ce/src/world.cindex c0200996..8bb222c0 100644--- a/games/numblocks/ce/src/world.c+++ b/games/numblocks/ce/src/world.c@@ -6,8 +6,21 @@uint8_t *wblk, *wlight;uint32_t wexp[WS * WS];-uint8_t wtop[WS * WS], wbiome[WS * WS];-static Column wcol[WS * WS];+uint8_t wtop[WS * WS];+CCol wcol[WS * WS];+/* a column's top and filler blocks and depth, packed (tf) */+static const uint8_t TOPS[6] = {B_GRASS, B_SAND, B_GRAVEL, B_DIRT, B_GRASS_SNOWED, B_STONE};+static const uint8_t FILLS[3] = {B_DIRT, B_SAND, B_GRAVEL};+static void col_pack(CCol *k, const Column *c) {+ int t = 0, f = 0;+ while (t < 5 && TOPS[t] != c->top) t++;+ while (f < 2 && FILLS[f] != c->filler) f++;+ k->h = c->h, k->biome = c->biome, k->tf = (uint8_t)(t | f << 3 | (c->depth - 3) << 5);+}+static void col_unpack(const CCol *k, Column *c) {+ c->h = k->h, c->biome = k->biome, c->top = TOPS[k->tf & 7], c->filler = FILLS[k->tf >> 3 & 3];+ c->depth = (uint8_t)(3 + (k->tf >> 5));+}int win_x, win_z, win_y;uint32_t world_seed;Edit *edits;@@ -222,11 +235,11 @@ static void fill(int x0, int z0, int x1, int z1, int y0, int y1, bool cols) {for (int z = z0; z < z1; z++)for (int x = x0; x < x1; x++) {int c = CI(x, z);- Column *col = &wcol[c];+ Column cc, *col = &cc;if (cols) {gen_column(x, z, col);- wbiome[c] = col->biome;- }+ col_pack(&wcol[c], col);+ } else col_unpack(&wcol[c], col);for (int y = y0; y < y1; y++) wblk[WI(x, y, z)] = (uint8_t)gen_cell(x, y, z, col);int py = col->h + 1;if (py >= y0 && py < y1 && wblk[WI(x, py, z)] == B_AIR && wblk[WI(x, py - 1, z)] == col->top) {diff --git a/games/numblocks/ce/tools/pack.py b/games/numblocks/ce/tools/pack.pyindex d1c37044..7081bce1 100644--- a/games/numblocks/ce/tools/pack.py+++ b/games/numblocks/ce/tools/pack.py@@ -593,7 +593,10 @@ def main():out = ['/* Made by tools/pack.py from Minecraft 1.8.8\'s pictures: see data.h. */', '#include "data.h"', '']out += carr('pal_rgb[256 * 3]', 'uint8_t', [int(v) for c in pal for v in c])# the appvar NBCETEX (gfx/NBCETEX.bin): the textures, the shade rows and the colour cube, read in place- texbin = bytes([v for t in tex_idx for v in t]) + bytes(shade) + bytes(cube)+ # (and the 3D view's division tables: 2^28 / k for k 16..2560, 3 bytes each; f * 256 / d for d 1..15, f < d)+ recip = b''.join((min((1 << 28) // k, (1 << 24) - 1)).to_bytes(3, 'little') for k in range(16, 2561))+ ratio = bytes((f * 256 // d) if d and f < d else 0 for d in range(16) for f in range(16))+ texbin = bytes([v for t in tex_idx for v in t]) + bytes(shade) + bytes(cube) + recip + ratioassert len(texbin) <= 65505, len(texbin)os.makedirs(os.path.join(CE, 'gfx'), exist_ok=True)open(os.path.join(CE, 'gfx', 'NBCETEX.bin'), 'wb').write(texbin)@@ -731,7 +734,9 @@ def main():'/* the appvar NBCETEX: the textures (v * 16 + u; 0 see-through), each colour at each level of light',' * (31 full), the nearest colour of a point [r][g][b] of 16 levels each (cube_levels) */','extern const uint8_t *texdat;',- '#define TEXDAT_SIZE (NTEX * 256 + N_SHADE * 256 + 4096)',+ '#define TEXDAT_RECIP (NTEX * 256 + N_SHADE * 256 + 4096)',+ '#define TEXDAT_RATIO (TEXDAT_RECIP + 2545 * 3)',+ '#define TEXDAT_SIZE (TEXDAT_RATIO + 256)','#define tex_px ((const uint8_t (*)[256])texdat)','#define shade_cm ((const uint8_t (*)[256])(texdat + NTEX * 256))','#define pal_cube (texdat + NTEX * 256 + N_SHADE * 256)',