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
Doom: distant floors and ceilings no longer shimmer
Mason3633c7d4973b068
diff --git a/games/doom/Makefile b/games/doom/Makefileindex 2e1d6ca2..cf0d12c8 100644--- a/games/doom/Makefile+++ b/games/doom/Makefile@@ -4,7 +4,8 @@# make host build/play: the game on a computer, for tests and screenshots# make test the game played on a computer (tests/check.py, with the sanitizers)# make media the pictures of docs/ and the animations of the repository's docs/media (tools/media.py)-# make data src/doom1.whx from the shareware DOOM1.WAD (tools/fetch.py downloads it; WAD=its path)+# make data src/doom1.whx and src/doom1.mip from the shareware DOOM1.WAD (tools/fetch.py downloads it;+# WAD=its path)# make WAD=path/to/doom2.wad [PWAD="maps.wad ..."] [MAPS="MAP01-MAP04"]# output/doom.nwa from another WAD (the README's "Doom from your own WAD"); host, test and check tooQ ?= @@@ -19,11 +20,13 @@ SIZE = $(BIN)arm-none-eabi-sizeBUILD_DIR = outputWAD ?=-# the packed WAD: the shareware one, or one made from WAD= (below)+# the packed WAD: the shareware one, or one made from WAD= (below); and its flats made smaller (tools/mips.py)WHX = src/doom1.whx+MIP = src/doom1.mipifneq ($(WAD),)ifeq ($(filter data,$(MAKECMDGOALS)),)WHX = build/wad/doom.whx+MIP = build/wad/doom.mipendifendif@@ -71,8 +74,8 @@ $(BUILD_DIR)/obj/%.o: src/%.c $(HEADERS)$(BUILD_DIR)/obj/pd_render.o: src/pd_render.cpp $(HEADERS)@mkdir -p $(@D)$(Q) $(CXX) $(CXXFLAGS) -Wall -Wextra $(ENGINE_W) -c $< -o $@-$(BUILD_DIR)/data.o: src/nw/data.S $(WHX) $(BUILD_DIR)/whx | $(BUILD_DIR)- $(Q) $(CC) -c -DDOOM_WHX='"$(WHX)"' $< -o $@+$(BUILD_DIR)/data.o: src/nw/data.S $(WHX) $(MIP) $(BUILD_DIR)/whx | $(BUILD_DIR)+ $(Q) $(CC) -c -DDOOM_WHX='"$(WHX)"' -DDOOM_MIP='"$(MIP)"' $< -o $@$(BUILD_DIR)/whx: FORCE | $(BUILD_DIR)@echo '$(WHX)' | cmp -s - $@ || echo '$(WHX)' > $@@@ -104,9 +107,9 @@ build/host/pd_render.o: src/pd_render.cpp $(HEADERS)build/host/play.o: tests/play.c $(HEADERS)@mkdir -p $(@D)$(Q) cc -std=gnu11 $(HOST_FLAGS) $(HOST_SAN) -Wall -Wextra -c $< -o $@-build/host/data.o: src/nw/data.S $(WHX) build/host/whx+build/host/data.o: src/nw/data.S $(WHX) $(MIP) build/host/whx@mkdir -p $(@D)- $(Q) cc -c -DDOOM_WHX='"$(WHX)"' $< -o $@+ $(Q) cc -c -DDOOM_WHX='"$(WHX)"' -DDOOM_MIP='"$(MIP)"' $< -o $@build/host/whx: FORCE@mkdir -p $(@D)@echo '$(WHX)' | cmp -s - $@ || echo '$(WHX)' > $@@@ -140,6 +143,7 @@ $(WHD_GEN): $(WHD_GEN_OBJ)data: $(WHD_GEN)@test -n "$(WAD)" || { echo "make data WAD=<the shareware DOOM1.WAD> (python3 tools/fetch.py downloads it)"; exit 1; }$(WHD_GEN) -silent -lossless $(WAD) src/doom1.whx+ python3 tools/mips.py $(WAD) src/doom1.mip# another WAD: its chosen maps and what they show (tools/pick.py), packed as the calculator reads itPICKED = build/wad/$(shell echo '$(notdir $(WAD))' | tr A-Z a-z)@@ -151,6 +155,8 @@ build/wad/doom.whx: build/wad/args $(WHD_GEN) tools/pick.py $(WAD) $(PWAD)$(Q) $(WHD_GEN) -silent -lossless -drop-unused $(PICKED) $@ > build/wad/whd_gen.log || \{ tail -3 build/wad/whd_gen.log; rm -f $@; exit 1; }@echo "WHX $@: $$(wc -c < $@) bytes"+build/wad/doom.mip: build/wad/doom.whx tools/mips.py+ python3 tools/mips.py $(PICKED) $@.PHONY: FORCEclean:diff --git a/games/doom/README.md b/games/doom/README.mdindex 4a7bff9f..cbfef34b 100644--- a/games/doom/README.md+++ b/games/doom/README.md@@ -25,7 +25,7 @@* **The real game:** the Doom engine itself (Chocolate Doom, by way of RP2040 Doom), not a lookalike. Monsters think, doors open, lifts rise and fall, barrels explode and the levels play exactly as on a PC in 1993: even the demos it plays on the title screen run as they were recorded.* **Knee-Deep in the Dead, all of it:** Hangar, Nuclear Plant, Toxin Refinery, Command Control, Phobos Lab, Central Processing, Computer Station, Phobos Anomaly and the secret Military Base, with their secrets, keys, switches and the Barons of Hell at the end.* **Everything from the shareware WAD:** every wall, floor, sprite, menu and screen, the status bar with the marine's face, the automap, the intermission maps of Phobos with your kills, items, secrets and time, and the episode's ending.-* **The whole 320 x 200 picture:** drawn at Doom's own resolution with its light levels and its palette, the screen flashing red when you're hurt and gold when you pick something up, and stretched to the calculator's 320 x 240 the way monitors of the day made Doom's pixels a little taller.+* **The whole 320 x 200 picture:** drawn at Doom's own resolution with its light levels and its palette (far floors and ceilings smoothed, so they don't shimmer as you move), the screen flashing red when you're hurt and gold when you pick something up, and stretched to the calculator's 320 x 240 the way monitors of the day made Doom's pixels a little taller.* **The five skill levels,** from I'm too young to die to Nightmare!, and the menus as they were: New Game, Options, Load Game, Save Game, Read This! and Quit Game, with their questions and Doom's quit messages.* **All of vanilla's options:** messages, Graphic Detail (high, or low with its blocky double pixels), Screen Size from the smallest view in its border to the full screen without the status bar, five levels of brightness, and the automap's follow mode, grid, marks and zoom.* **The calculator's own options:** every key can do any action (Options, Keys), the automap can lie over the game, secret areas can show on it in their own colour, and a Cheats menu has every code: god mode, all weapons and keys, no clipping, warping, the powers, the whole map and the chainsaw. The codes can be typed too, letters and all.@@ -145,9 +145,9 @@ make test WAD=... MAPS=... # plays it on a compThe calculator gives an app about 130 KB of RAM: Doom on a PC asked for 4 MB. This port runs Graham Sanderson's [RP2040 Doom](https://github.com/kilograham/rp2040-doom), which made all of the shareware DOOM1.WAD playable on a 264 KB microcontroller: the WAD is packed into its WHD format (1.4 MB without the sounds and music, read in place from flash), the levels are never copied to RAM, and the game's objects are squeezed with 16-bit pointers.-The calculator has less RAM still, and no room for a frame buffer: the screen is drawn in five bands of 64 columns. Each frame the engine walks the level once and keeps every column of wall and floor it would draw, then draws each band whole, its floors and ceilings, walls, then its sprites, masked walls and weapon in vanilla's order, clipped as vanilla clips them; then the status bar, messages and menus, and sends it to the screen through Doom's palette. The picture is vanilla's to the pixel: the three demos play tic for tic as in Chocolate Doom, and their frames match Chocolate Doom's (but for a few pixels where vanilla reads past the end of a texture).+The calculator has less RAM still, and no room for a frame buffer: the screen is drawn in five bands of 64 columns. Each frame the engine walks the level once and keeps every column of wall and floor it would draw, then draws each band whole, its floors and ceilings, walls, then its sprites, masked walls and weapon in vanilla's order, clipped as vanilla clips them; then the status bar, messages and menus, and sends it to the screen through Doom's palette. The picture is vanilla's to the pixel: the three demos play tic for tic as in Chocolate Doom, and their frames match Chocolate Doom's (but for a few pixels where vanilla reads past the end of a texture), except on floors and ceilings far enough that their texels are more than a pixel apart. There vanilla's point sampling jumps from texel to texel as the view moves, and a flat a half, a quarter or an eighth the size (each pixel the average of the texels under it, in the palette's nearest colour) is drawn instead.-`make` builds `output/doom.nwa` (needs `arm-none-eabi-gcc` and Node.js for nwlink). `make test` plays it on a computer with the sanitizers on, and `make media` takes the pictures on this page from there. `python3 tools/fetch.py build/ref` downloads the shareware DOOM1.WAD and `make data WAD=build/ref/doom1.wad` packs it again into `src/doom1.whx`. `tools/fps.py` measures the frame rate of every level in the emulator, from every spot of each map.+`make` builds `output/doom.nwa` (needs `arm-none-eabi-gcc` and Node.js for nwlink). `make test` plays it on a computer with the sanitizers on, and `make media` takes the pictures on this page from there. `python3 tools/fetch.py build/ref` downloads the shareware DOOM1.WAD and `make data WAD=build/ref/doom1.wad` packs it again into `src/doom1.whx`, with its flats made smaller for far floors and ceilings in `src/doom1.mip` (`tools/mips.py`). `tools/fps.py` measures the frame rate of every level in the emulator, from every spot of each map.## Credits@@ -163,4 +163,4 @@ Made by Mason Chen as part of NumPlay.## License-The engine is licensed under the GNU General Public License, version 2 or later (`COPYING.md`); RP2040 Doom's own files under the BSD 3-Clause license, as they say at their top; the converter's libraries under their own licenses (`tools/whd_gen`). NumPlay's code is licensed under the GNU General Public License v3.0: see the `LICENSE` file at the root of the repository. The game's data in `src/doom1.whx` is id Software's shareware DOOM1.WAD (version 1.9), the free episode they let players share, repacked for the calculator: it stays id Software's.+The engine is licensed under the GNU General Public License, version 2 or later (`COPYING.md`); RP2040 Doom's own files under the BSD 3-Clause license, as they say at their top; the converter's libraries under their own licenses (`tools/whd_gen`). NumPlay's code is licensed under the GNU General Public License v3.0: see the `LICENSE` file at the root of the repository. The game's data in `src/doom1.whx` and `src/doom1.mip` is id Software's shareware DOOM1.WAD (version 1.9), the free episode they let players share, repacked for the calculator: it stays id Software's.diff --git a/games/doom/src/doom1.mip b/games/doom/src/doom1.mipnew file mode 100644index 00000000..bf2aca1cBinary files /dev/null and b/games/doom/src/doom1.mip differdiff --git a/games/doom/src/nw/data.S b/games/doom/src/nw/data.Sindex 0ce2ccbc..83a5ba42 100644--- a/games/doom/src/nw/data.S+++ b/games/doom/src/nw/data.S@@ -1,13 +1,24 @@-/* The shareware DOOM1.WAD, packed by tools/whd_gen into RP2040 Doom's WHX format: read in place. */+/* The shareware DOOM1.WAD, packed by tools/whd_gen into RP2040 Doom's WHX format: read in place. And its flats+ * made smaller by tools/mips.py, for the floors and ceilings far away (pd_render.cpp). */#ifdef __APPLE__ /* (a Mac's computer build: Mach-O's names and sections) */.const_data.balign 4.globl _doom_whd_doom_whd:+ .incbin DOOM_WHX+ .balign 4+ .globl _doom_mips+_doom_mips:+ .incbin DOOM_MIP#else.section .rodata.doom_whd, "a".balign 4.global doom_whddoom_whd:-#endif.incbin DOOM_WHX+ .section .rodata.doom_mips, "a"+ .balign 4+ .global doom_mips+doom_mips:+ .incbin DOOM_MIP+#endifdiff --git a/games/doom/src/pd_render.cpp b/games/doom/src/pd_render.cppindex 9df78260..f9f21a5f 100644--- a/games/doom/src/pd_render.cpp+++ b/games/doom/src/pd_render.cpp@@ -1429,6 +1429,39 @@ static uint8_t *decode_flat_to_slot(int cache_slot, int picnum) {return flat_data;}+#if NUMWORKS+// far floors and ceilings: the flat made smaller (tools/mips.py packs 32 x 32, 16 x 16 and 8 x 8 of each flat next to+// the WHX, each pixel what the texels under it average to). Where a row's texels are more than one to a pixel apart,+// the full flat gives one of them, a different one each time the view moves a little (far lights flicker and jump);+// the smaller copy whose texels are about a pixel apart gives what they average to. Nearer rows are vanilla's.+extern "C" const uint8_t doom_mips[];+extern "C" lumpindex_t numflats;+#define NW_MIP_STRIDE (32 * 32 + 16 * 16 + 8 * 8)+static const uint8_t *nw_mips_base;+static int8_t nw_mips_ok = -1;++static const uint8_t *nw_flat_mips(int picnum) {+ if (nw_mips_ok < 0) {+ // (the build's mips are for its flats: as many as the WHX has)+ const uint8_t *m = doom_mips;+ nw_mips_ok = m[0] == 'N' && m[1] == 'W' && m[2] == 'M' && m[3] == 'P' && (m[4] | m[5] << 8) == numflats;+ nw_mips_base = m + 8;+ }+ return nw_mips_ok ? nw_mips_base + picnum * NW_MIP_STRIDE : nullptr;+}++// a run of a row from the flat at level K (64 >> K texels square; flats are stored a column at a time, flat[x * size+// + y], and the packed position has x's 6.10 in its top half and y's in its bottom one, as R_DrawSpan has them)+template<int K> static inline void nw_flat_run(uint8_t *p, uint8_t *p_end, uint32_t position, uint32_t step,+ const uint8_t *src, const lighttable_t *colormap) {+ constexpr uint32_t S = 64 >> K;+ while (p < p_end) {+ *p++ = colormap[src[((position >> (20 + 2 * K)) & ((S - 1) << (6 - K))) | ((position >> (10 + K)) & (S - 1))]];+ position += step;+ }+}+#endif+static void flush_visplanes(int8_t *flatnum_next, int numvisplanes) {// printf("FRAME %d %d\n", pd_frame, numvisplanes);angle_t angle = (viewangle + x_to_viewangle(0)) >> ANGLETOFINESHIFT;@@ -1438,6 +1471,16 @@ static void flush_visplanes(int8_t *flatnum_next, int numvisplanes) {const fixed_t distscale0 = distscale(0); //0x00016a75; // todo i guess this is screen size basedviewcosangle = FixedMul(distscale0, viewcosangle);viewsinangle = FixedMul(distscale0, viewsinangle);+#endif+#if NUMWORKS+ // (the distance from which a row's texels are more than a pixel apart, and the smaller flats are drawn)+ fixed_t mip_from = centerx << FRACBITS;+#if HOST+ // (tests: NWNOMIPS=1 draws vanilla's flats everywhere)+ static int no_mips = -1;+ if (no_mips < 0) no_mips = getenv("NWNOMIPS") != nullptr;+ if (no_mips) mip_from = 0x1fffffff;+#endif#endif// two passes; first pass we try to reuse flats we have decodedint cache_slot = 0;@@ -1512,12 +1555,21 @@ static void flush_visplanes(int8_t *flatnum_next, int numvisplanes) {// at its own first pixel, and the packed position carries from y into x as in vanilla (the flat is// stored a column at a time, flat[x * 64 + y])fixed_t distance = 0;+ const uint8_t *mips = nw_flat_mips(picnum), *src = flat_data;+ int level = 0;for (int16_t fr = visplane_heads[vp]; fr != -1; fr = flat_runs[fr].next) {int y = flat_runs[fr].y, x1 = flat_runs[fr].x_true;if (y != last_y) {distance = FixedMul(rel_height, yslope[y]);xstep = FixedMul(distance, basexscale);ystep = FixedMul(distance, baseyscale);+ // (the texels are distance / centerx apart along the row)+ level = 0;+ src = flat_data;+ if (mips && distance > mip_from) {+ level = distance > mip_from * 4 ? 3 : distance > mip_from * 2 ? 2 : 1;+ src = mips + (level == 1 ? 0 : level == 2 ? 32 * 32 : 32 * 32 + 16 * 16);+ }int colormap_index;if (fixedcolormap) {colormap_index = fixedcolormap;@@ -1541,9 +1593,11 @@ static void flush_visplanes(int8_t *flatnum_next, int numvisplanes) {position += step * (uint32_t) (flat_runs[fr].x_start - x1);uint8_t *p = render_frame_buffer + y * NW_STRIDE + flat_runs[fr].x_start;uint8_t *p_end = render_frame_buffer + y * NW_STRIDE + flat_runs[fr].x_end;- while (p < p_end) {- *p++ = colormap[flat_data[((position >> 20) & 0x0fc0) | ((position >> 10) & 63)]];- position += step;+ switch (level) {+ case 0: nw_flat_run<0>(p, p_end, position, step, src, colormap); break;+ case 1: nw_flat_run<1>(p, p_end, position, step, src, colormap); break;+ case 2: nw_flat_run<2>(p, p_end, position, step, src, colormap); break;+ default: nw_flat_run<3>(p, p_end, position, step, src, colormap); break;}}#elsediff --git a/games/doom/tools/mips.py b/games/doom/tools/mips.pynew file mode 100644index 00000000..1d5a8b73--- /dev/null+++ b/games/doom/tools/mips.py@@ -0,0 +1,73 @@+#!/usr/bin/env python3+"""mips.py WAD OUT: the flats made smaller, for the floors and ceilings far away (src/pd_render.cpp), packed next to+the WHX (src/nw/data.S): each flat of the WAD (every lump from F_START to F_END, as the game numbers them) at 32 x 32,+16 x 16 and 8 x 8, each pixel the average of the flat's pixels under it, in the palette's colours, then the+palette's nearest colour. Point-sampled from 64 x 64, a flat seen far away jumps from one texel to another as the+view moves (the red lights of E1M1 from its first door); its smaller copy has what all those texels average to.++OUT: "NWMP", the number of flats (16 bits), 0 (16 bits), then 1344 bytes for each flat: 32 x 32, 16 x 16, 8 x 8,+stored as the game stores flats, a column at a time (pixel [x][y] at x * size + y, x across the flat as the WAD+has it). A lump that is not a 64 x 64 flat (a marker) has zeros."""+import struct+import sys++SIZES = (32, 16, 8)+STRIDE = sum(s * s for s in SIZES)+++def read_wad(path):+ d = open(path, "rb").read()+ if d[:4] not in (b"IWAD", b"PWAD"):+ sys.exit(f"mips.py: {path} is not a WAD")+ n, off = struct.unpack("<II", d[4:12])+ lumps = []+ for i in range(n):+ o, s, name = struct.unpack("<II8s", d[off + 16 * i:off + 16 * i + 16])+ lumps.append((name.split(b"\0")[0].decode("latin1").upper(), d[o:o + s]))+ return lumps+++def last(lumps, name):+ found = [i for i, (n, _) in enumerate(lumps) if n == name]+ if not found:+ sys.exit(f"mips.py: no {name}")+ return found[-1]+++def main():+ lumps = read_wad(sys.argv[1])+ pal = lumps[last(lumps, "PLAYPAL")][1][:768]+ rgb = [tuple(pal[3 * i:3 * i + 3]) for i in range(256)]+ nearest = {}++ def colour(r, g, b):+ k = (r, g, b)+ if k not in nearest:+ nearest[k] = min(range(256), key=lambda i: (rgb[i][0] - r) ** 2 + (rgb[i][1] - g) ** 2 + (rgb[i][2] - b) ** 2)+ return nearest[k]++ first, end = last(lumps, "F_START") + 1, last(lumps, "F_END")+ out = bytearray(b"NWMP" + struct.pack("<HH", end - first, 0))+ for name, flat in lumps[first:end]:+ if len(flat) != 4096:+ out += bytes(STRIDE)+ continue+ for size in SIZES:+ k = 64 // size+ n = k * k+ for x in range(size):+ for y in range(size):+ r = g = b = 0+ for v in range(y * k, y * k + k):+ row = v * 64+ for u in range(x * k, x * k + k):+ c = rgb[flat[row + u]]+ r += c[0]+ g += c[1]+ b += c[2]+ out.append(colour((r + n // 2) // n, (g + n // 2) // n, (b + n // 2) // n))+ open(sys.argv[2], "wb").write(bytes(out))+++if __name__ == "__main__":+ main()diff --git a/games/doom/tools/whd_gen/huff_sink.h b/games/doom/tools/whd_gen/huff_sink.hindex b3642c07..67f25bc3 100644--- a/games/doom/tools/whd_gen/huff_sink.h+++ b/games/doom/tools/whd_gen/huff_sink.h@@ -34,7 +34,7 @@ template<typename H, typename BO=std::shared_ptr<byte_vector_bit_output>, bool Dif (!huff.initialized()) {huff = stats.template create_huffman_encoding<H>();}- return huff.template decode(bi);+ return huff.decode(bi);}void dump(bool force_debug = false) {diff --git a/games/doom/tools/whd_gen/huffman.h b/games/doom/tools/whd_gen/huffman.hindex aacde942..f9edfa4f 100644--- a/games/doom/tools/whd_gen/huffman.h+++ b/games/doom/tools/whd_gen/huffman.h@@ -137,7 +137,7 @@ template<typename S, typename H> struct huffman_encoding {classify_by_length(node->zero, length + 1);classify_by_length(node->one, length + 1);} else {- symbol_indexes_and_lengths.template emplace_back(node->symbol_index, length);+ symbol_indexes_and_lengths.emplace_back(node->symbol_index, length);if (length >= length_counts.size()) length_counts.resize(length+1);length_counts[length]++;}@@ -635,7 +635,7 @@ template<typename BI> std::vector<std::pair<uint8_t, int>> decode_min_max8(BI &bfor (uint val = min; val <= max; val++) {if (bi.bit()) {printf(" %d: %d bits\n", val, min_cl);- symbol_lengths.template emplace_back(val, min_cl);+ symbol_lengths.emplace_back(val, min_cl);}}} else {@@ -647,7 +647,7 @@ template<typename BI> std::vector<std::pair<uint8_t, int>> decode_min_max8(BI &bif (bi.bit()) {for (uint i = 0; i <= std::min(7, max - base_val); i++) {int code_length = min_cl + bi.read(bit_count);- symbol_lengths.template emplace_back(base_val + i, code_length);+ symbol_lengths.emplace_back(base_val + i, code_length);printf(" %d: %d bits\n", base_val + i, code_length);}}@@ -655,7 +655,7 @@ template<typename BI> std::vector<std::pair<uint8_t, int>> decode_min_max8(BI &bfor (int i = 0; i <= std::min(7, max - base_val); i++) {if (bi.bit()) {int code_length = min_cl + bi.read(bit_count);- symbol_lengths.template emplace_back(base_val + i, code_length);+ symbol_lengths.emplace_back(base_val + i, code_length);printf(" %d: %d bits\n", base_val + i, code_length);}}@@ -665,7 +665,7 @@ template<typename BI> std::vector<std::pair<uint8_t, int>> decode_min_max8(BI &bfor (int val = min; val <= max; val++) {if (bi.bit()) {int code_length = min_cl + bi.read(bit_count);- symbol_lengths.template emplace_back(val, code_length);+ symbol_lengths.emplace_back(val, code_length);printf(" %d: %d bits\n", val, code_length);}}diff --git a/games/doom/tools/whd_gen/whd_gen.cpp b/games/doom/tools/whd_gen/whd_gen.cppindex b1c54317..f3967196 100644--- a/games/doom/tools/whd_gen/whd_gen.cpp+++ b/games/doom/tools/whd_gen/whd_gen.cpp@@ -778,7 +778,7 @@ T get_field(const std::vector<uint8_t> &data, int offset) {template<typename T>void append_field(std::vector<uint8_t> &data, const T &field) {const uint8_t *p = (uint8_t *) &field;- data.template insert(data.end(), p, p + sizeof(T));+ data.insert(data.end(), p, p + sizeof(T));}template<typename T>