From 3b39c7561ec71dadb0cb03a153550e9a6e2cddff Mon Sep 17 00:00:00 2001 From: Mica White Date: Sat, 31 Oct 2026 12:38:00 -0400 Subject: Optimization --- Cargo.lock | 806 ++++------------------------------------------- Cargo.toml | 2 +- Shakefile | 2 +- src/detect_controller.rs | 13 + src/exit_on_escape.rs | 4 +- src/main.rs | 43 ++- src/move_snake.rs | 30 +- src/render.rs | 259 +++++++++++---- src/render_frametime.rs | 37 +++ src/snake_controller.rs | 90 +++++- src/update_high_score.rs | 14 + 11 files changed, 458 insertions(+), 842 deletions(-) create mode 100644 src/detect_controller.rs create mode 100644 src/render_frametime.rs create mode 100644 src/update_high_score.rs diff --git a/Cargo.lock b/Cargo.lock index 4c70879..83e3bbf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14,21 +14,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c880a97d28a3681c0267bd29cff89621202715b065127cd445fa0f0fe0aa2880" -[[package]] -name = "android_system_properties" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae221649c9976a6f6c56ae1facf410f3ddb33cc661c4b7b61020a912d4237fbc" -dependencies = [ - "libc", -] - -[[package]] -name = "anyhow" -version = "1.0.104" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" - [[package]] name = "async-channel" version = "2.5.0" @@ -148,15 +133,6 @@ version = "4.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b75356056920673b02621b35afd0f7dda9306d03c79a30f5c56c44cf256e3de" -[[package]] -name = "atomic-polyfill" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8cf2bce30dfe09ef0bfaef228b9d414faaf7e563035494d7fe092dba54b300f4" -dependencies = [ - "critical-section", -] - [[package]] name = "atomic-waker" version = "1.1.2" @@ -169,20 +145,11 @@ version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" -[[package]] -name = "bincode" -version = "1.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" -dependencies = [ - "serde", -] - [[package]] name = "bitflags" -version = "2.13.1" +version = "2.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b588b76d00fde79687d7646a9b5bdf3cc0f655e0bbd080335a95d7e96f3587da" +checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" [[package]] name = "blocking" @@ -199,9 +166,9 @@ dependencies = [ [[package]] name = "bpaf" -version = "0.9.27" +version = "0.9.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c670d65eea33846872d5ccf668c00a94c5c67dbdcc0289ea1c362671ce1ddb82" +checksum = "597e81e56404b7a3e26ef7b449f82a4b7a661ae76c3cac692b42d6bad06a0fbe" dependencies = [ "bpaf_derive", ] @@ -217,43 +184,11 @@ dependencies = [ "syn 2.0.119", ] -[[package]] -name = "bstr" -version = "1.13.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6bb31b46c14244e20ee9984b11bf5c992b91fb6939fea616e3512c8baecdbe5f" -dependencies = [ - "memchr", - "serde_core", -] - -[[package]] -name = "bumpalo" -version = "3.20.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" - -[[package]] -name = "byteorder" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" - -[[package]] -name = "cc" -version = "1.4.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "005ec2760ca554fae18df7a11195552ec576cd665632a881bc011d5bb2fd4d80" -dependencies = [ - "find-msvc-tools", - "shlex", -] - [[package]] name = "cfg-if" -version = "1.0.4" +version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" +checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" [[package]] name = "chacha20" @@ -263,29 +198,7 @@ checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06" dependencies = [ "cfg-if", "cpufeatures", - "rand_core 0.10.1", -] - -[[package]] -name = "chrono" -version = "0.4.45" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1aa79e62e7697b8e29b513a68abacf485adcd1fe8284a4316c5ae868e6633327" -dependencies = [ - "iana-time-zone", - "js-sys", - "num-traits", - "wasm-bindgen", - "windows-link", -] - -[[package]] -name = "cobs" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fa961b519f0b462e3a3b4a34b64d119eeaca1d59af726fe450bbba07a9fc0a1" -dependencies = [ - "thiserror", + "rand_core", ] [[package]] @@ -306,12 +219,6 @@ dependencies = [ "unicode-segmentation", ] -[[package]] -name = "core-foundation-sys" -version = "0.8.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" - [[package]] name = "cpufeatures" version = "0.3.1" @@ -321,21 +228,6 @@ dependencies = [ "libc", ] -[[package]] -name = "critical-section" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b" - -[[package]] -name = "crossbeam-channel" -version = "0.5.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98b0cc327b5bc766e7fda9c9260cc0fa81b43a8e240440422dff70788e3f9ef1" -dependencies = [ - "crossbeam-utils", -] - [[package]] name = "crossbeam-utils" version = "0.8.23" @@ -400,40 +292,11 @@ dependencies = [ "litrs", ] -[[package]] -name = "either" -version = "1.18.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "252afb9ae5eaa683babdc6a068b3f5726eb19e05070c731f9b2a23a7c3e8ed34" - -[[package]] -name = "embedded-io" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef1a6892d9eef45c8fa6b9e0086428a2cca8491aca8f787c534a3d6d0bcb3ced" - -[[package]] -name = "embedded-io" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "edd0f118536f44f5ccd48bcb8b111bdc3de888b58c74639dfb034a357d0f206d" - [[package]] name = "equivalent" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" - -[[package]] -name = "erased-serde" -version = "0.4.10" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2add8a07dd6a8d93ff627029c51de145e12686fbc36ecb298ac22e74cf02dec" -dependencies = [ - "serde", - "serde_core", - "typeid", -] +checksum = "00d174d5400e5e8fd687ad1049e2f578285fa914201b1af7e8b112a4546bd826" [[package]] name = "errno" @@ -481,12 +344,6 @@ version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223" -[[package]] -name = "find-msvc-tools" -version = "0.1.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e0f1c7c3a72c66fd80abe965175f7523475c0489a87d3ff9d6e8c87d87a9d2d" - [[package]] name = "foldhash" version = "0.2.0" @@ -518,35 +375,6 @@ dependencies = [ "pin-project-lite", ] -[[package]] -name = "futures-task" -version = "0.3.34" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd417de3d1d015fc3bfd2b1ea46dfc7bab72ef86f1cc7cc9c78e728b34a6d1fd" - -[[package]] -name = "futures-util" -version = "0.3.34" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d50a92467f8ba5dd6e3ee5d4bd04d73ab2e4e1c44474a0674821dfce14b79bc" -dependencies = [ - "futures-core", - "futures-task", - "pin-project-lite", - "slab", -] - -[[package]] -name = "getrandom" -version = "0.2.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0" -dependencies = [ - "cfg-if", - "libc", - "wasi", -] - [[package]] name = "getrandom" version = "0.4.3" @@ -556,16 +384,7 @@ dependencies = [ "cfg-if", "libc", "r-efi", - "rand_core 0.10.1", -] - -[[package]] -name = "hash32" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0c35f58762feb77d74ebe43bdbc3210f09be9fe6742234d573bacc26ed92b67" -dependencies = [ - "byteorder", + "rand_core", ] [[package]] @@ -581,20 +400,6 @@ dependencies = [ "serde_core", ] -[[package]] -name = "heapless" -version = "0.7.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdc6457c0eb62c71aac4bc17216026d8410337c4126773b9c5daba343f17964f" -dependencies = [ - "atomic-polyfill", - "hash32", - "rustc_version", - "serde", - "spin 0.9.9", - "stable_deref_trait", -] - [[package]] name = "hermit-abi" version = "0.5.3" @@ -602,79 +407,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e17592d60ebacc7d5e169f4663c5f84f9161cc90328abcfe8456f41e4dfcb284" [[package]] -name = "hex_color" -version = "3.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d37f101bf4c633f7ca2e4b5e136050314503dd198e78e325ea602c327c484ef0" -dependencies = [ - "rand 0.8.8", -] - -[[package]] -name = "iana-time-zone" -version = "0.1.65" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470" -dependencies = [ - "android_system_properties", - "core-foundation-sys", - "iana-time-zone-haiku", - "js-sys", - "log", - "wasm-bindgen", - "windows-core", -] - -[[package]] -name = "iana-time-zone-haiku" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" -dependencies = [ - "cc", -] - -[[package]] -name = "indexmap" -version = "2.14.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc4e190f5d26ca7051642629da2c52fc03bde85a03197c99408dcd291734c855" -dependencies = [ - "equivalent", - "hashbrown", -] - -[[package]] -name = "itertools" -version = "0.14.0" +name = "itoa" +version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285" -dependencies = [ - "either", -] - -[[package]] -name = "js-sys" -version = "0.3.105" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce57d20d1ea864ce2ac172ab472d409214f4fd359f0b2a2775abdf522e2af99e" -dependencies = [ - "cfg-if", - "futures-util", - "wasm-bindgen", -] +checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" [[package]] name = "libc" -version = "0.2.189" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2" - -[[package]] -name = "libm" -version = "0.2.16" +version = "0.2.191" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981" +checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a" [[package]] name = "linux-raw-sys" @@ -684,9 +426,9 @@ checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" [[package]] name = "litrs" -version = "1.0.0" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "11d3d7f243d5c5a8b9bb5d6dd2b1602c0cb0b9db1621bafc7ed66e35ff9fe092" +checksum = "4744e383959f0db86ede514b809b1c53251889093803c05267acc7d4e7030d70" [[package]] name = "lock_api" @@ -699,34 +441,9 @@ dependencies = [ [[package]] name = "log" -version = "0.4.34" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6" - -[[package]] -name = "lua-src" -version = "551.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d400ffef0e3d4d29287092bdc5a276d0bf468b2c69709d5bab0d469312d9f947" -dependencies = [ - "cc", -] - -[[package]] -name = "luajit-src" -version = "210.7.3+1ee778a" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "869665372263eb337b14f480cfb864b89f12eade4eb42cb415f71517b4a67572" -dependencies = [ - "cc", - "which", -] - -[[package]] -name = "lz4_flex" -version = "0.13.1" +version = "0.4.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ef0d4ed8669f8f8826eb00dc878084aa8f253506c4fd5e8f58f5bce72ddb97e" +checksum = "5d15f86d12da44cd8d4ec3c73984537eb7f4cadcb9776a575a0a8a05ca11005c" [[package]] name = "memchr" @@ -756,9 +473,9 @@ dependencies = [ [[package]] name = "mio" -version = "1.2.3" +version = "1.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4b18443e9c262bfe8fa82f51666e2642c53393f7e5c27b3e1aeab922cff5b9d8" +checksum = "1788edb87fdc09c7e26304471e2f5be8cdefb1b6930d6e3985fc02ff53bf86ee" dependencies = [ "libc", "log", @@ -766,38 +483,6 @@ dependencies = [ "windows-sys", ] -[[package]] -name = "mlua" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d96e5d00f19d8c46c71ceaced99593b90c31c57aa1fe2cb3e93a8b1698eedba9" -dependencies = [ - "bstr", - "either", - "erased-serde", - "libc", - "mlua-sys", - "num-traits", - "parking_lot", - "rustc-hash", - "serde", - "serde-value", -] - -[[package]] -name = "mlua-sys" -version = "0.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b806d7ade031f5d6607eae3e283fb034cb795a76247dd0d1ba753c8c42debccf" -dependencies = [ - "cc", - "cfg-if", - "libc", - "lua-src", - "luajit-src", - "pkg-config", -] - [[package]] name = "num-traits" version = "0.2.19" @@ -807,21 +492,6 @@ dependencies = [ "autocfg", ] -[[package]] -name = "once_cell" -version = "1.21.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" - -[[package]] -name = "ordered-float" -version = "2.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68f19d67e5a2795c94e73e0bb1cc1a7edeb2e28efd39e2e1c9b7a40c1108b11c" -dependencies = [ - "num-traits", -] - [[package]] name = "parking" version = "2.2.1" @@ -911,12 +581,6 @@ dependencies = [ "futures-io", ] -[[package]] -name = "pkg-config" -version = "0.3.34" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548" - [[package]] name = "polling" version = "3.11.0" @@ -931,28 +595,6 @@ dependencies = [ "windows-sys", ] -[[package]] -name = "postcard" -version = "1.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6764c3b5dd454e283a30e6dfe78e9b31096d9e32036b5d1eaac7a6119ccb9a24" -dependencies = [ - "cobs", - "embedded-io 0.4.0", - "embedded-io 0.6.1", - "heapless", - "serde", -] - -[[package]] -name = "ppv-lite86" -version = "0.2.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" -dependencies = [ - "zerocopy", -] - [[package]] name = "proc-macro2" version = "1.0.107" @@ -971,35 +613,6 @@ dependencies = [ "log", ] -[[package]] -name = "puffin" -version = "0.20.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84b514d95a258be801fde8a1ff1c974f4a4841d9750f5d1d6690fc07a5ad4049" -dependencies = [ - "anyhow", - "bincode", - "byteorder", - "cfg-if", - "itertools", - "lz4_flex", - "parking_lot", - "serde", -] - -[[package]] -name = "puffin_http" -version = "0.17.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4f912991aab1adae69d2be9455e8db0f41e5ad3da87706ab2de64908678c2c76" -dependencies = [ - "anyhow", - "crossbeam-channel", - "log", - "parking_lot", - "puffin", -] - [[package]] name = "quote" version = "1.0.47" @@ -1017,43 +630,13 @@ checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" [[package]] name = "rand" -version = "0.8.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e058c7de0b26af77780c769414d6257830bb240f3c38477dbc2c16e5f54d6d4c" -dependencies = [ - "libc", - "rand_chacha", - "rand_core 0.6.4", -] - -[[package]] -name = "rand" -version = "0.10.2" +version = "0.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7f5fa3a058cd35567ef9bfa5e75732bee0f9e4c55fa90477bef2dfcdbc4be80" +checksum = "65c9fb96cbc91e3478eaae79a69fcd3f1ae4ad052e471fe6732fff548984b4af" dependencies = [ "chacha20", - "getrandom 0.4.3", - "rand_core 0.10.1", -] - -[[package]] -name = "rand_chacha" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" -dependencies = [ - "ppv-lite86", - "rand_core 0.6.4", -] - -[[package]] -name = "rand_core" -version = "0.6.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" -dependencies = [ - "getrandom 0.2.17", + "getrandom", + "rand_core", ] [[package]] @@ -1088,9 +671,9 @@ dependencies = [ [[package]] name = "rustix" -version = "1.1.4" +version = "1.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6fe4565b9518b83ef4f91bb47ce29620ca828bd32cb7e408f0062e9930ba190" +checksum = "e5e76742e5294153d4dde1207dbec5bdd81eef6fabdc0d2118babadabfd858aa" dependencies = [ "bitflags", "errno", @@ -1099,12 +682,6 @@ dependencies = [ "windows-sys", ] -[[package]] -name = "rustversion" -version = "1.0.23" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" - [[package]] name = "scopeguard" version = "1.2.0" @@ -1127,16 +704,6 @@ dependencies = [ "serde_derive", ] -[[package]] -name = "serde-value" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3a1a3341211875ef120e117ea7fd5228530ae7e7036a779fdc9117be6b3282c" -dependencies = [ - "ordered-float", - "serde", -] - [[package]] name = "serde_core" version = "1.0.229" @@ -1154,24 +721,22 @@ checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348" dependencies = [ "proc-macro2", "quote", - "syn 3.0.5", + "syn 3.0.9", ] [[package]] -name = "serde_spanned" -version = "1.1.1" +name = "serde_json" +version = "1.0.154" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6662b5879511e06e8999a8a235d848113e942c9124f211511b16466ee2995f26" +checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6" dependencies = [ + "itoa", + "memchr", + "serde", "serde_core", + "zmij", ] -[[package]] -name = "shlex" -version = "2.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" - [[package]] name = "signal-hook" version = "0.3.18" @@ -1205,9 +770,9 @@ dependencies = [ [[package]] name = "siphasher" -version = "1.0.3" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ee5873ec9cce0195efcb7a4e9507a04cd49aec9c83d0389df45b1ef7ba2e649" +checksum = "33f4fe9184a62d842c9ef383018f3306d8ba224fd9d836f56d7288308847c256" [[package]] name = "slab" @@ -1217,9 +782,9 @@ checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" [[package]] name = "smallvec" -version = "1.16.0" +version = "1.16.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9be42f50aa861c555654aa3a37f52f4b1074bacf4e48fe0ef7fa584e80f1f0f" +checksum = "d0d5d4b1981b8bbbb7aed204cae5726d683d5d6ac7e74ec54122ea28f315e7c8" [[package]] name = "smol" @@ -1238,15 +803,6 @@ dependencies = [ "futures-lite", ] -[[package]] -name = "spin" -version = "0.9.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3763264f6b73151db08c50ff20d7d8a0b8796e021cdea7ceedad07b80155fa0e" -dependencies = [ - "lock_api", -] - [[package]] name = "spin" version = "0.12.3" @@ -1257,10 +813,13 @@ dependencies = [ ] [[package]] -name = "stable_deref_trait" -version = "1.2.1" +name = "spin_sleep" +version = "1.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" +checksum = "9c07347b7c0301b9adba4350bdcf09c039d0e7160922050db0439b3c6723c8ab" +dependencies = [ + "windows-sys", +] [[package]] name = "syn" @@ -1275,9 +834,9 @@ dependencies = [ [[package]] name = "syn" -version = "3.0.5" +version = "3.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12df2e0110f65b775f769bb17ef989067a1d931b2eb822bd4346631eeada89f9" +checksum = "d78c8dee4c7bf0e14673097256fed6142ce9d3b85a408189d07482442145823b" dependencies = [ "proc-macro2", "quote", @@ -1294,120 +853,29 @@ dependencies = [ "lock_api", ] -[[package]] -name = "thiserror" -version = "2.0.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec86235f5fcc2a73650310756d2ac5b138a5780bbbdfae3eeccec992c435ba4f" -dependencies = [ - "thiserror-impl", -] - -[[package]] -name = "thiserror-impl" -version = "2.0.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc04cd3e1236dd4a98afca4569f2deb3f120e5422a4023be2cb683f8486292af" -dependencies = [ - "proc-macro2", - "quote", - "syn 3.0.5", -] - -[[package]] -name = "tinywasm" -version = "0.11.0-pre.0" -dependencies = [ - "libm", - "log", - "tinywasm-parser", - "tinywasm-types", -] - -[[package]] -name = "tinywasm-parser" -version = "0.11.0-pre.0" -dependencies = [ - "log", - "tinywasm-types", - "wasmparser", -] - -[[package]] -name = "tinywasm-types" -version = "0.11.0-pre.0" -dependencies = [ - "log", - "postcard", - "serde", -] - -[[package]] -name = "toml" -version = "1.1.5+spec-1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12c0ba9680044b4ce98d391a62094047eada0d64860b80166c39f4a6b5640785" -dependencies = [ - "indexmap", - "serde_core", - "serde_spanned", - "toml_datetime", - "toml_parser", - "toml_writer", - "winnow", -] - -[[package]] -name = "toml_datetime" -version = "1.1.1+spec-1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3165f65f62e28e0115a00b2ebdd37eb6f3b641855f9d636d3cd4103767159ad7" -dependencies = [ - "serde_core", -] - -[[package]] -name = "toml_parser" -version = "1.1.3+spec-1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d38ac1cf9b95face32296c0a3ede1fdc270627c9d9c02a7274dd6d960dc4d56" -dependencies = [ - "winnow", -] - -[[package]] -name = "toml_writer" -version = "1.1.2+spec-1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d56353a2a665ad0f41a421187180aab746c8c325620617ad883a99a1cbe66d2" - [[package]] name = "tortuise" version = "0.1.0" +source = "git+https://botahamec.dev/cgit/tortuise.git#c9e708c00665c09adb93144175f11cc11fa1e7f8" dependencies = [ "bpaf", - "chrono", "crossterm", "etcetera", - "getrandom 0.4.3", - "hex_color", + "getrandom", "log", "messagepack-serde", - "mlua", - "postcard", "prologger", - "puffin", - "puffin_http", "rustc-hash", "serde", "smol", - "spin 0.12.3", - "tinywasm", + "spin", + "spin_sleep", ] [[package]] name = "tortuise-script" version = "0.1.0" +source = "git+https://botahamec.dev/cgit/tortuise-script.git#ad44c70e918c695298c2480a8b3df5be60c3e95d" dependencies = [ "tortuise-script-compiled", "tortuise-script-wasm", @@ -1416,29 +884,30 @@ dependencies = [ [[package]] name = "tortuise-script-compiled" version = "0.1.0" +source = "git+https://botahamec.dev/cgit/tortuise-script.git#ad44c70e918c695298c2480a8b3df5be60c3e95d" dependencies = [ "crossterm", "log", "messagepack-serde", - "puffin", - "rand 0.10.2", + "rand", "serde", - "spin 0.12.3", - "toml", + "serde_json", + "spin", "tortuise", ] [[package]] name = "tortuise-script-wasm" version = "0.1.0" +source = "git+https://botahamec.dev/cgit/tortuise-script.git#ad44c70e918c695298c2480a8b3df5be60c3e95d" dependencies = [ "log", "messagepack-serde", - "rand 0.10.2", + "rand", "serde", - "spin 0.12.3", + "serde_json", + "spin", "talc", - "toml", ] [[package]] @@ -1448,22 +917,16 @@ dependencies = [ "hashbrown", "log", "phf", - "rand 0.10.2", + "rand", "serde", "tortuise-script", ] -[[package]] -name = "typeid" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc7d623258602320d5c55d1bc22793b57daff0ec7efc270ea7d55ce1d5f5471c" - [[package]] name = "unicode-ident" -version = "1.0.24" +version = "1.0.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" +checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" [[package]] name = "unicode-segmentation" @@ -1477,70 +940,6 @@ version = "0.11.1+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" -[[package]] -name = "wasm-bindgen" -version = "0.2.128" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aecb87a33d3b0c5e3b7aa46336eaf486cffafbd281b195e4c8b80d50df2351bf" -dependencies = [ - "cfg-if", - "once_cell", - "rustversion", - "wasm-bindgen-macro", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-macro" -version = "0.2.128" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a690d511e3c1a8b3a55e33511e3c2c00c78415cd23650f32b808627f5696b9ed" -dependencies = [ - "quote", - "wasm-bindgen-macro-support", -] - -[[package]] -name = "wasm-bindgen-macro-support" -version = "0.2.128" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "411e4887f0071ef2d2164a9d5fdf2d20efbef78fccd3a78b0c10a1dc5295e48a" -dependencies = [ - "bumpalo", - "proc-macro2", - "quote", - "syn 3.0.5", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-shared" -version = "0.2.128" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81941cd78d0c92026c33e5e01312845a4cb1e9af3407f9134b100dd03144103e" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "wasmparser" -version = "0.255.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8e329ef4b5d46e73b91d3ac6924417cad55a8cbbf869c199283383427c3320b" -dependencies = [ - "bitflags", - "indexmap", -] - -[[package]] -name = "which" -version = "8.0.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bae2f2b2b816647a1cab1acc91f5bd20812d53cb344382635ec2181940c8034f" -dependencies = [ - "libc", -] - [[package]] name = "winapi" version = "0.3.9" @@ -1563,65 +962,12 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" -[[package]] -name = "windows-core" -version = "0.62.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb" -dependencies = [ - "windows-implement", - "windows-interface", - "windows-link", - "windows-result", - "windows-strings", -] - -[[package]] -name = "windows-implement" -version = "0.60.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.119", -] - -[[package]] -name = "windows-interface" -version = "0.59.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.119", -] - [[package]] name = "windows-link" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" -[[package]] -name = "windows-result" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5" -dependencies = [ - "windows-link", -] - -[[package]] -name = "windows-strings" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091" -dependencies = [ - "windows-link", -] - [[package]] name = "windows-sys" version = "0.61.2" @@ -1632,27 +978,7 @@ dependencies = [ ] [[package]] -name = "winnow" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23b97319f7b8343df12cc98938e5c3eb436064524c8d2b4e30a1d3a36eecdf81" - -[[package]] -name = "zerocopy" -version = "0.8.57" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d35102a9f36d089ccae9e4c6802bc118be4487b80aaffc0ab4e0cf5ce92d2873" -dependencies = [ - "zerocopy-derive", -] - -[[package]] -name = "zerocopy-derive" -version = "0.8.57" +name = "zmij" +version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "146c01f5ab44258da43cf276c74a2763db2ff3969c9c652c3f2de07041d0b2bc" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.119", -] +checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" diff --git a/Cargo.toml b/Cargo.toml index 494efc9..e1cee97 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2024" [dependencies] -tortuise-script = { path = "../tortuise-script" } +tortuise-script = { git = "https://botahamec.dev/cgit/tortuise-script.git", default-features = false } hashbrown = { version = "0.17", features = ["serde"] } log = "0.4" rand = { version = "0.10", default-features = false } diff --git a/Shakefile b/Shakefile index 9df1ef5..2cccaee 100644 --- a/Shakefile +++ b/Shakefile @@ -5,7 +5,7 @@ run() { } profile() { - cargo run --release + cargo run --release --features tortuise-script/profiling } build() { diff --git a/src/detect_controller.rs b/src/detect_controller.rs new file mode 100644 index 0000000..2890826 --- /dev/null +++ b/src/detect_controller.rs @@ -0,0 +1,13 @@ +use tortuise_script::TortuiseScript; + +use crate::CONNECTED_GAMEPAD_QUEUE; + +pub const DETECT_CONTROLLER: TortuiseScript = TortuiseScript { + update: |context| { + let mut connected_gamepads = CONNECTED_GAMEPAD_QUEUE.get_mut(&context); + if let Some(gamepad) = context.is_gamepad_connected_this_frame() { + connected_gamepads.push(gamepad); + } + }, + ..TortuiseScript::empty() +}; diff --git a/src/exit_on_escape.rs b/src/exit_on_escape.rs index a7885ca..8748244 100644 --- a/src/exit_on_escape.rs +++ b/src/exit_on_escape.rs @@ -2,7 +2,9 @@ use tortuise_script::{input::KeyCode, ScriptContext, TortuiseScript}; pub const EXIT_ON_ESCAPE: TortuiseScript = TortuiseScript { update: |mut context: ScriptContext| { - if context.is_key_pressed_this_frame(KeyCode::Esc) { + if context.is_key_pressed_this_frame(KeyCode::Esc) + || context.is_key_pressed_this_frame(KeyCode::Char('q')) + { context.exit(); } }, diff --git a/src/main.rs b/src/main.rs index 3b6605a..bf6eae4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,25 @@ -#![cfg_attr(target_arch = "wasm32", no_std)] +#![no_std] #![cfg_attr(target_arch = "wasm32", no_main)] extern crate alloc; +mod detect_controller; mod exit_on_escape; mod move_snake; mod render; +mod render_frametime; mod snake_controller; +mod update_high_score; use alloc::vec::Vec; use core::time::Duration; -use tortuise_script::{entity::Entity, time::Instant, TortuiseScene}; +use tortuise_script::{ + entity::Entity, + input::Gamepad, + storage::{Config, Storage}, + time::Instant, + TortuiseScene, +}; use serde::{Deserialize, Serialize}; @@ -20,8 +29,25 @@ struct Position { pub y: i16, } +#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] +struct SaveData { + high_score: usize, +} + +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] +struct ConfigOptions { + #[serde(default)] + framerate_limit: Option, +} + const GRID_SIZE: u16 = 16; +static SAVE_DATA: Storage = Storage::new(SaveData::default); +static CONFIG: Config = Config::new(ConfigOptions::default); + +static CONNECTED_GAMEPAD_QUEUE: Entity> = + Entity::new("connected_gamepad_queue", |_| Vec::new()); +static IS_PAUSED: Entity = Entity::new("is_paused", |_| false); static APPLE_LOCATION: Entity = Entity::new("apple_location", |_| Position { x: 2, y: 2 }); static SNAKE_CELLS: Entity> = Entity::new("snake_cells", |_| { @@ -39,10 +65,21 @@ tortuise_script::game! { first_scene: "main", scenes: { "main" => TortuiseScene { - scripts: &["snake_controller", "exit_on_escape", "move_snake", "render"], + scripts: &[ + "frametime", + "detect_controller", + "update_high_score", + "snake_controller", + "exit_on_escape", + "move_snake", + "render" + ], } }, scripts: { + "frametime" => render_frametime::RENDER_FRAMETIME, + "detect_controller" => detect_controller::DETECT_CONTROLLER, + "update_high_score" => update_high_score::UPDATE_HIGH_SCORE, "snake_controller" => snake_controller::SNAKE_CONTROLLER, "exit_on_escape" => exit_on_escape::EXIT_ON_ESCAPE, "move_snake" => move_snake::MOVE_SNAKE, diff --git a/src/move_snake.rs b/src/move_snake.rs index 453e795..4439be5 100644 --- a/src/move_snake.rs +++ b/src/move_snake.rs @@ -1,10 +1,11 @@ use core::time::Duration; -use alloc::vec::Vec; use rand::RngExt; use tortuise_script::{random::TortuiseRng, time::Instant, ScriptContext, TortuiseScript}; -use crate::{Position, APPLE_LOCATION, DIRECTION, GRID_SIZE, NEXT_MOVE_TIME, SNAKE_CELLS}; +use crate::{ + Position, APPLE_LOCATION, DIRECTION, GRID_SIZE, IS_PAUSED, NEXT_MOVE_TIME, SNAKE_CELLS, +}; pub const MOVE_SNAKE: TortuiseScript = TortuiseScript { update: |context: ScriptContext| { @@ -12,26 +13,16 @@ pub const MOVE_SNAKE: TortuiseScript = TortuiseScript { let mut snake_cells = SNAKE_CELLS.get_mut(&context); let mut next_move = NEXT_MOVE_TIME.get_mut(&context); let direction = DIRECTION.get(&context); - - let mut available_squares = - Vec::with_capacity((GRID_SIZE * GRID_SIZE) as usize - snake_cells.len()); - for i in 0..GRID_SIZE { - for j in 0..GRID_SIZE { - let position = Position { - x: i as i16, - y: j as i16, - }; - if !snake_cells.contains(&position) { - available_squares.push(position); - } - } - } + let is_paused = IS_PAUSED.get(&context); let mut rng = TortuiseRng::new(); let time = Instant::now(&context); - if time > *next_move { + if time > *next_move && !*is_paused { if snake_cells[0] == *apple_location { - *apple_location = available_squares[rng.random_range(0..available_squares.len())]; + *apple_location = Position { + x: rng.random_range(0..GRID_SIZE as i16), + y: rng.random_range(0..GRID_SIZE as i16), + }; let new_head = Position { x: snake_cells[0].x + direction.x, y: snake_cells[0].y + direction.y, @@ -45,6 +36,9 @@ pub const MOVE_SNAKE: TortuiseScript = TortuiseScript { snake_cells[0].x += direction.x; snake_cells[0].y += direction.y; } + if time > *next_move { + *next_move += Duration::from_millis(200); + } }, ..TortuiseScript::empty() }; diff --git a/src/render.rs b/src/render.rs index 80b06f7..77075f7 100644 --- a/src/render.rs +++ b/src/render.rs @@ -1,39 +1,82 @@ -use alloc::string::ToString; +use alloc::format; +use hashbrown::HashSet; +use serde::{Deserialize, Serialize}; use tortuise_script::{ - buffer::{Cell, Color}, + buffer::{Cell, Color, WindowSize}, + state::ScriptState, ScriptContext, TortuiseScript, }; -use crate::{APPLE_LOCATION, GRID_SIZE, SNAKE_CELLS}; +use crate::{Position, APPLE_LOCATION, GRID_SIZE, IS_PAUSED, SAVE_DATA, SNAKE_CELLS}; + +#[derive(Serialize, Deserialize)] +struct OldState { + old_snake_cells: HashSet, + old_score: usize, + old_high_score: usize, + old_window_size: WindowSize, +} + +static OLD_STATE: ScriptState = ScriptState::new(|_| OldState { + old_snake_cells: HashSet::new(), + old_score: usize::MAX, + old_high_score: usize::MAX, + old_window_size: WindowSize { + rows: 0, + columns: 0, + }, +}); + +fn draw_text(cx: &mut ScriptContext, text: &str, x: u16, y: u16) { + for (x, character) in (x..).zip(text.chars()) { + cx.add_eternal_cell(Cell { + character, + x, + y, + ..Cell::default() + }); + } +} + +fn remove_text(cx: &mut ScriptContext, text: &str, x: u16, y: u16) { + for (x, character) in (x..).zip(text.chars()) { + cx.remove_eternal_cell(Cell { + character, + x, + y, + ..Cell::default() + }); + } +} fn draw_box(cx: &mut ScriptContext, char: char, x: u16, y: u16) { for i in 1..5 { - cx.add_cell(Cell { + cx.add_eternal_cell(Cell { character: '-', x: x + i, y, ..Default::default() }); - cx.add_cell(Cell { + cx.add_eternal_cell(Cell { character: '-', x: x + i, y: y + 2, ..Default::default() }); } - cx.add_cell(Cell { + cx.add_eternal_cell(Cell { character: '|', x, y: y + 1, ..Default::default() }); - cx.add_cell(Cell { + cx.add_eternal_cell(Cell { character: '|', x: x + 5, y: y + 1, ..Default::default() }); - cx.add_cell(Cell { + cx.add_eternal_cell(Cell { character: char, x: x + 3, y: y + 1, @@ -41,71 +84,144 @@ fn draw_box(cx: &mut ScriptContext, char: char, x: u16, y: u16) { }); } +fn draw_grid(context: &mut ScriptContext<'_>, offset_x: u16, offset_y: u16) { + for i in 0..GRID_SIZE { + context.add_eternal_cell(Cell { + character: '|', + x: offset_x.saturating_sub(1), + y: offset_y + i, + z: 0, + ..Default::default() + }); + context.add_eternal_cell(Cell { + character: '|', + x: offset_x + GRID_SIZE, + y: offset_y + i, + z: 0, + ..Default::default() + }); + context.add_eternal_cell(Cell { + character: '-', + x: offset_x + i, + y: offset_y.saturating_sub(1), + z: 0, + ..Default::default() + }); + context.add_eternal_cell(Cell { + character: '-', + x: offset_x + i, + y: offset_y + GRID_SIZE, + z: 0, + ..Default::default() + }); + } +} + +fn delete_grid(context: &mut ScriptContext<'_>, offset_x: u16, offset_y: u16) { + for i in 0..GRID_SIZE { + context.remove_eternal_cell(Cell { + character: '|', + x: offset_x.saturating_sub(1), + y: offset_y + i, + z: 0, + ..Default::default() + }); + context.remove_eternal_cell(Cell { + character: '|', + x: offset_x + GRID_SIZE, + y: offset_y + i, + z: 0, + ..Default::default() + }); + context.remove_eternal_cell(Cell { + character: '-', + x: offset_x + i, + y: offset_y.saturating_sub(1), + z: 0, + ..Default::default() + }); + context.remove_eternal_cell(Cell { + character: '-', + x: offset_x + i, + y: offset_y + GRID_SIZE, + z: 0, + ..Default::default() + }); + } +} + pub const RENDER: TortuiseScript = TortuiseScript { + start: |mut context| { + draw_box(&mut context, '<', 4, 4); + draw_box(&mut context, '>', 16, 4); + draw_box(&mut context, '^', 10, 1); + draw_box(&mut context, 'v', 10, 7); + }, update: |mut context: ScriptContext| { + let mut old_state = OLD_STATE.get_mut(&context); let apple_location = APPLE_LOCATION.get(&context); let snake_cells = SNAKE_CELLS.get(&context); + let is_paused = IS_PAUSED.get(&context); + let high_score = SAVE_DATA.load(&context).high_score; + + let old_window_size = old_state.old_window_size; + let old_offset_x = (old_window_size.columns.saturating_sub(GRID_SIZE) / 2).max(24); + let old_offset_y = old_window_size.rows.saturating_sub(GRID_SIZE) / 2; let window_size = context.window_size(); - let offset_x = window_size.columns.saturating_sub(GRID_SIZE) / 2; + let offset_x = (window_size.columns.saturating_sub(GRID_SIZE) / 2).max(24); let offset_y = window_size.rows.saturating_sub(GRID_SIZE) / 2; - - for i in 0..GRID_SIZE { - context.add_cell(Cell { - character: '|', - x: offset_x.saturating_sub(1), - y: offset_y + i, - z: 0, - ..Default::default() - }); - context.add_cell(Cell { - character: '|', - x: offset_x + GRID_SIZE, - y: offset_y + i, - z: 0, - ..Default::default() - }); - context.add_cell(Cell { - character: '-', - x: offset_x + i, - y: offset_y.saturating_sub(1), - z: 0, - ..Default::default() - }); - context.add_cell(Cell { - character: '-', - x: offset_x + i, - y: offset_y + GRID_SIZE, - z: 0, - ..Default::default() - }); + if window_size != old_window_size { + old_state.old_window_size = window_size; + delete_grid(&mut context, old_offset_x, old_offset_y); + draw_grid(&mut context, offset_x, offset_y); } - for (i, char) in "Score:".chars().enumerate() { - context.add_cell(Cell { - character: char, - x: offset_x + GRID_SIZE - 10 + i as u16, - y: offset_y.saturating_sub(2), - ..Default::default() - }); + let old_score = old_state.old_score; + let score = snake_cells.len() - 2; + if score != old_score || window_size != old_window_size { + old_state.old_score = score; + let score_counter = format!("Score: {score:>3}"); + let old_score_counter = format!("Score: {old_score:>3}"); + remove_text( + &mut context, + &old_score_counter, + offset_x + GRID_SIZE - 10, + offset_y.saturating_sub(2), + ); + draw_text( + &mut context, + &score_counter, + offset_x + GRID_SIZE - 10, + offset_y.saturating_sub(2), + ); } - draw_box(&mut context, '<', 4, 4); - draw_box(&mut context, '>', 16, 4); - draw_box(&mut context, '^', 10, 1); - draw_box(&mut context, 'v', 10, 7); + let old_high_score = old_state.old_high_score; + if high_score != old_high_score || window_size != old_window_size { + old_state.old_high_score = high_score; + let score_counter = format!("High Score: {high_score:>3}"); + let old_score_counter = format!("High Score: {old_high_score:>3}"); + remove_text( + &mut context, + &old_score_counter, + offset_x + GRID_SIZE - 15, + offset_y.saturating_sub(3), + ); + draw_text( + &mut context, + &score_counter, + offset_x + GRID_SIZE - 15, + offset_y.saturating_sub(3), + ); + } - for (i, char) in (snake_cells.len() - 2) - .to_string() - .chars() - .rev() - .enumerate() - { - context.add_cell(Cell { - character: char, - x: offset_x + GRID_SIZE - i as u16, - y: offset_y.saturating_sub(2), - ..Default::default() - }); + if *is_paused { + draw_text( + &mut context, + "-- Paused --", + offset_x + 2, + offset_y + GRID_SIZE + 1, + ); } context.add_cell(Cell { @@ -116,8 +232,22 @@ pub const RENDER: TortuiseScript = TortuiseScript { z: 0, ..Default::default() }); - for cell in &*snake_cells { - context.add_cell(Cell { + + let snake_cells = HashSet::from_iter(snake_cells.iter().cloned()); + let old_snake_cells = old_state.old_snake_cells.difference(&snake_cells); + let new_snake_cells = snake_cells.difference(&old_state.old_snake_cells); + for cell in old_snake_cells { + context.remove_eternal_cell(Cell { + character: '█', + foreground_color: Color::Green, + x: cell.x.unsigned_abs() + offset_x, + y: cell.y.unsigned_abs() + offset_y, + z: 1, + ..Default::default() + }); + } + for cell in new_snake_cells { + context.add_eternal_cell(Cell { character: '█', foreground_color: Color::Green, x: cell.x.unsigned_abs() + offset_x, @@ -126,6 +256,7 @@ pub const RENDER: TortuiseScript = TortuiseScript { ..Default::default() }); } + old_state.old_snake_cells = snake_cells; }, ..TortuiseScript::empty() }; diff --git a/src/render_frametime.rs b/src/render_frametime.rs new file mode 100644 index 0000000..6756fa7 --- /dev/null +++ b/src/render_frametime.rs @@ -0,0 +1,37 @@ +use alloc::format; +use tortuise_script::{buffer::Cell, TortuiseScript}; + +use crate::CONFIG; + +pub const RENDER_FRAMETIME: TortuiseScript = TortuiseScript { + start: |mut cx| { + let config = CONFIG.load_mut(&mut cx); + let fps_limit = config.framerate_limit; + drop(config); + cx.set_framerate_limit(fps_limit); + }, + update: |mut cx| { + let frametime = cx.delta_time().as_micros(); + let fps = 1_000_000u128.checked_div(frametime).unwrap_or(u128::MAX); + let frametime = format!("{frametime:05} us"); + let counter = format!("{fps} FPS"); + + for (x, character) in (9..).zip(frametime.chars()) { + cx.add_cell(Cell { + character, + y: 14, + x, + ..Cell::default() + }); + } + for (x, character) in (9..).zip(counter.chars()) { + cx.add_cell(Cell { + character, + y: 15, + x, + ..Cell::default() + }); + } + }, + ..TortuiseScript::empty() +}; diff --git a/src/snake_controller.rs b/src/snake_controller.rs index 0c48175..15dfcea 100644 --- a/src/snake_controller.rs +++ b/src/snake_controller.rs @@ -1,9 +1,9 @@ use tortuise_script::{ - input::{KeyCode, Position as MousePosition}, + input::{Button, Direction, Gamepad, KeyCode, Position as MousePosition}, ScriptContext, TortuiseScript, }; -use crate::{Position, DIRECTION}; +use crate::{Position, CONNECTED_GAMEPAD_QUEUE, DIRECTION, IS_PAUSED}; fn is_inside_box(mouse: Option, button: MousePosition, size: u16) -> bool { mouse.is_some_and(|mouse| { @@ -12,25 +12,87 @@ fn is_inside_box(mouse: Option, button: MousePosition, size: u16) }) } +fn did_move_left( + context: &ScriptContext, + mouse_location: Option, + scroll_direction: Option, + gamepad: Option, +) -> bool { + is_inside_box(mouse_location, MousePosition { x: 4, y: 4 }, 3) + || scroll_direction == Some(Direction::Right) + || gamepad.is_some_and(|gamepad| gamepad.is_button_down(context, Button::DPadLeft)) + || context.is_key_pressed_this_frame(KeyCode::Left) + || context.is_key_pressed_this_frame(KeyCode::Char('h')) + || context.is_key_pressed_this_frame(KeyCode::Char('a')) +} + +fn did_move_right( + context: &ScriptContext, + mouse_location: Option, + scroll_direction: Option, + gamepad: Option, +) -> bool { + is_inside_box(mouse_location, MousePosition { x: 16, y: 4 }, 3) + || scroll_direction == Some(Direction::Left) + || gamepad.is_some_and(|gamepad| gamepad.is_button_down(context, Button::DPadRight)) + || context.is_key_pressed_this_frame(KeyCode::Right) + || context.is_key_pressed_this_frame(KeyCode::Char('d')) + || context.is_key_pressed_this_frame(KeyCode::Char('l')) +} + +fn did_move_up( + context: &ScriptContext, + mouse_location: Option, + scroll_direction: Option, + gamepad: Option, +) -> bool { + is_inside_box(mouse_location, MousePosition { x: 10, y: 1 }, 3) + || scroll_direction == Some(Direction::Down) + || gamepad.is_some_and(|gamepad| gamepad.is_button_down(context, Button::DPadUp)) + || context.is_key_pressed_this_frame(KeyCode::Up) + || context.is_key_pressed_this_frame(KeyCode::Char('w')) + || context.is_key_pressed_this_frame(KeyCode::Char('k')) +} + +fn did_move_down( + context: &ScriptContext, + mouse_location: Option, + scroll_direction: Option, + gamepad: Option, +) -> bool { + is_inside_box(mouse_location, MousePosition { x: 10, y: 7 }, 3) + || scroll_direction == Some(Direction::Up) + || gamepad.is_some_and(|gamepad| gamepad.is_button_down(context, Button::DPadDown)) + || context.is_key_pressed_this_frame(KeyCode::Down) + || context.is_key_pressed_this_frame(KeyCode::Char('s')) + || context.is_key_pressed_this_frame(KeyCode::Char('j')) +} + pub const SNAKE_CONTROLLER: TortuiseScript = TortuiseScript { update: |context: ScriptContext| { let mut direction = DIRECTION.get_mut(&context); + let mut is_paused = IS_PAUSED.get_mut(&context); + let gamepad = CONNECTED_GAMEPAD_QUEUE.get(&context).first().copied(); + + if context.was_unfocused_this_frame() { + *is_paused = true; + } + if context.is_key_pressed_this_frame(KeyCode::Char('p')) { + *is_paused = !*is_paused; + } + if *is_paused { + return; + } + let mouse_location = context.mouse_location(); - if is_inside_box(mouse_location, MousePosition { x: 4, y: 4 }, 3) { + let scroll_direction = context.scroll_direction_this_frame(); + if did_move_left(&context, mouse_location, scroll_direction, gamepad) { *direction = Position { x: -1, y: 0 }; - } else if is_inside_box(mouse_location, MousePosition { x: 16, y: 4 }, 3) { + } else if did_move_right(&context, mouse_location, scroll_direction, gamepad) { *direction = Position { x: 1, y: 0 }; - } else if is_inside_box(mouse_location, MousePosition { x: 10, y: 1 }, 3) { + } else if did_move_up(&context, mouse_location, scroll_direction, gamepad) { *direction = Position { x: 0, y: -1 }; - } else if is_inside_box(mouse_location, MousePosition { x: 10, y: 7 }, 3) { - *direction = Position { x: 0, y: 1 }; - } else if context.is_key_pressed_this_frame(KeyCode::Up) { - *direction = Position { x: 0, y: -1 }; - } else if context.is_key_pressed_this_frame(KeyCode::Right) { - *direction = Position { x: 1, y: 0 }; - } else if context.is_key_pressed_this_frame(KeyCode::Left) { - *direction = Position { x: -1, y: 0 }; - } else if context.is_key_pressed_this_frame(KeyCode::Down) { + } else if did_move_down(&context, mouse_location, scroll_direction, gamepad) { *direction = Position { x: 0, y: 1 }; } }, diff --git a/src/update_high_score.rs b/src/update_high_score.rs new file mode 100644 index 0000000..b38b08a --- /dev/null +++ b/src/update_high_score.rs @@ -0,0 +1,14 @@ +use tortuise_script::TortuiseScript; + +use crate::{SAVE_DATA, SNAKE_CELLS}; + +pub const UPDATE_HIGH_SCORE: TortuiseScript = TortuiseScript { + update: |mut context| { + let score = SNAKE_CELLS.get(&context).len() - 2; + let mut save = SAVE_DATA.load_mut(&mut context); + if save.high_score < score { + save.high_score = score; + } + }, + ..TortuiseScript::empty() +}; -- cgit v1.3.1