├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── README.md ├── src ├── gtk.rs ├── snake.rs └── web.rs ├── static └── index.html └── switch ├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── Xargo.toml ├── aarch64-horizon-elf.json ├── build.sh ├── clean.sh ├── icon.jpg ├── romfs ├── hack.ttf └── small_font.ttf ├── snake-rs.nro └── src ├── main.rs └── snake.rs /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | /target/ 3 | **/*.rs.bk 4 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "atk-sys" 5 | version = "0.5.0" 6 | source = "registry+https://github.com/rust-lang/crates.io-index" 7 | dependencies = [ 8 | "bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", 9 | "glib-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 10 | "gobject-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 11 | "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", 12 | "pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", 13 | ] 14 | 15 | [[package]] 16 | name = "base-x" 17 | version = "0.2.2" 18 | source = "registry+https://github.com/rust-lang/crates.io-index" 19 | 20 | [[package]] 21 | name = "bitflags" 22 | version = "1.0.1" 23 | source = "registry+https://github.com/rust-lang/crates.io-index" 24 | 25 | [[package]] 26 | name = "c_vec" 27 | version = "1.2.1" 28 | source = "registry+https://github.com/rust-lang/crates.io-index" 29 | 30 | [[package]] 31 | name = "cairo-rs" 32 | version = "0.3.0" 33 | source = "registry+https://github.com/rust-lang/crates.io-index" 34 | dependencies = [ 35 | "c_vec 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 36 | "cairo-sys-rs 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 37 | "glib 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", 38 | "glib-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 39 | "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", 40 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 41 | ] 42 | 43 | [[package]] 44 | name = "cairo-sys-rs" 45 | version = "0.5.0" 46 | source = "registry+https://github.com/rust-lang/crates.io-index" 47 | dependencies = [ 48 | "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", 49 | "pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", 50 | "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", 51 | ] 52 | 53 | [[package]] 54 | name = "discard" 55 | version = "1.0.4" 56 | source = "registry+https://github.com/rust-lang/crates.io-index" 57 | 58 | [[package]] 59 | name = "dtoa" 60 | version = "0.4.2" 61 | source = "registry+https://github.com/rust-lang/crates.io-index" 62 | 63 | [[package]] 64 | name = "fuchsia-zircon" 65 | version = "0.3.3" 66 | source = "registry+https://github.com/rust-lang/crates.io-index" 67 | dependencies = [ 68 | "bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", 69 | "fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", 70 | ] 71 | 72 | [[package]] 73 | name = "fuchsia-zircon-sys" 74 | version = "0.3.3" 75 | source = "registry+https://github.com/rust-lang/crates.io-index" 76 | 77 | [[package]] 78 | name = "gdk" 79 | version = "0.7.0" 80 | source = "registry+https://github.com/rust-lang/crates.io-index" 81 | dependencies = [ 82 | "bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", 83 | "cairo-rs 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 84 | "cairo-sys-rs 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 85 | "gdk-pixbuf 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 86 | "gdk-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 87 | "gio 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 88 | "glib 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", 89 | "glib-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 90 | "gobject-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 91 | "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", 92 | "pango 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 93 | ] 94 | 95 | [[package]] 96 | name = "gdk-pixbuf" 97 | version = "0.3.0" 98 | source = "registry+https://github.com/rust-lang/crates.io-index" 99 | dependencies = [ 100 | "gdk-pixbuf-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 101 | "glib 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", 102 | "glib-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 103 | "gobject-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 104 | "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", 105 | ] 106 | 107 | [[package]] 108 | name = "gdk-pixbuf-sys" 109 | version = "0.5.0" 110 | source = "registry+https://github.com/rust-lang/crates.io-index" 111 | dependencies = [ 112 | "bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", 113 | "gio-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 114 | "glib-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 115 | "gobject-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 116 | "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", 117 | "pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", 118 | ] 119 | 120 | [[package]] 121 | name = "gdk-sys" 122 | version = "0.5.0" 123 | source = "registry+https://github.com/rust-lang/crates.io-index" 124 | dependencies = [ 125 | "bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", 126 | "cairo-sys-rs 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 127 | "gdk-pixbuf-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 128 | "gio-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 129 | "glib-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 130 | "gobject-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 131 | "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", 132 | "pango-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 133 | "pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", 134 | ] 135 | 136 | [[package]] 137 | name = "gio" 138 | version = "0.3.0" 139 | source = "registry+https://github.com/rust-lang/crates.io-index" 140 | dependencies = [ 141 | "bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", 142 | "gio-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 143 | "glib 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", 144 | "glib-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 145 | "gobject-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 146 | "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", 147 | ] 148 | 149 | [[package]] 150 | name = "gio-sys" 151 | version = "0.5.0" 152 | source = "registry+https://github.com/rust-lang/crates.io-index" 153 | dependencies = [ 154 | "bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", 155 | "glib-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 156 | "gobject-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 157 | "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", 158 | "pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", 159 | ] 160 | 161 | [[package]] 162 | name = "glib" 163 | version = "0.4.1" 164 | source = "registry+https://github.com/rust-lang/crates.io-index" 165 | dependencies = [ 166 | "bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", 167 | "glib-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 168 | "gobject-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 169 | "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", 170 | "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", 171 | ] 172 | 173 | [[package]] 174 | name = "glib-sys" 175 | version = "0.5.0" 176 | source = "registry+https://github.com/rust-lang/crates.io-index" 177 | dependencies = [ 178 | "bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", 179 | "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", 180 | "pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", 181 | ] 182 | 183 | [[package]] 184 | name = "gobject-sys" 185 | version = "0.5.0" 186 | source = "registry+https://github.com/rust-lang/crates.io-index" 187 | dependencies = [ 188 | "bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", 189 | "glib-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 190 | "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", 191 | "pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", 192 | ] 193 | 194 | [[package]] 195 | name = "gtk" 196 | version = "0.3.0" 197 | source = "registry+https://github.com/rust-lang/crates.io-index" 198 | dependencies = [ 199 | "bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", 200 | "cairo-rs 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 201 | "cairo-sys-rs 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 202 | "gdk 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", 203 | "gdk-pixbuf 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 204 | "gdk-pixbuf-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 205 | "gdk-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 206 | "gio 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 207 | "gio-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 208 | "glib 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", 209 | "glib-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 210 | "gobject-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 211 | "gtk-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 212 | "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", 213 | "pango 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 214 | ] 215 | 216 | [[package]] 217 | name = "gtk-sys" 218 | version = "0.5.0" 219 | source = "registry+https://github.com/rust-lang/crates.io-index" 220 | dependencies = [ 221 | "atk-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 222 | "bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", 223 | "cairo-sys-rs 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 224 | "gdk-pixbuf-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 225 | "gdk-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 226 | "gio-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 227 | "glib-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 228 | "gobject-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 229 | "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", 230 | "pango-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 231 | "pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", 232 | ] 233 | 234 | [[package]] 235 | name = "itoa" 236 | version = "0.3.4" 237 | source = "registry+https://github.com/rust-lang/crates.io-index" 238 | 239 | [[package]] 240 | name = "lazy_static" 241 | version = "1.0.0" 242 | source = "registry+https://github.com/rust-lang/crates.io-index" 243 | 244 | [[package]] 245 | name = "libc" 246 | version = "0.2.36" 247 | source = "registry+https://github.com/rust-lang/crates.io-index" 248 | 249 | [[package]] 250 | name = "num-traits" 251 | version = "0.1.43" 252 | source = "registry+https://github.com/rust-lang/crates.io-index" 253 | dependencies = [ 254 | "num-traits 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 255 | ] 256 | 257 | [[package]] 258 | name = "num-traits" 259 | version = "0.2.0" 260 | source = "registry+https://github.com/rust-lang/crates.io-index" 261 | 262 | [[package]] 263 | name = "pango" 264 | version = "0.3.0" 265 | source = "registry+https://github.com/rust-lang/crates.io-index" 266 | dependencies = [ 267 | "bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", 268 | "glib 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", 269 | "glib-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 270 | "gobject-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 271 | "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", 272 | "pango-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 273 | ] 274 | 275 | [[package]] 276 | name = "pango-sys" 277 | version = "0.5.0" 278 | source = "registry+https://github.com/rust-lang/crates.io-index" 279 | dependencies = [ 280 | "bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", 281 | "glib-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 282 | "gobject-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", 283 | "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", 284 | "pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", 285 | ] 286 | 287 | [[package]] 288 | name = "pkg-config" 289 | version = "0.3.9" 290 | source = "registry+https://github.com/rust-lang/crates.io-index" 291 | 292 | [[package]] 293 | name = "proc-macro2" 294 | version = "0.4.26" 295 | source = "registry+https://github.com/rust-lang/crates.io-index" 296 | dependencies = [ 297 | "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 298 | ] 299 | 300 | [[package]] 301 | name = "quote" 302 | version = "0.3.15" 303 | source = "registry+https://github.com/rust-lang/crates.io-index" 304 | 305 | [[package]] 306 | name = "quote" 307 | version = "0.6.11" 308 | source = "registry+https://github.com/rust-lang/crates.io-index" 309 | dependencies = [ 310 | "proc-macro2 0.4.26 (registry+https://github.com/rust-lang/crates.io-index)", 311 | ] 312 | 313 | [[package]] 314 | name = "rand" 315 | version = "0.4.2" 316 | source = "registry+https://github.com/rust-lang/crates.io-index" 317 | dependencies = [ 318 | "fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", 319 | "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", 320 | "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", 321 | ] 322 | 323 | [[package]] 324 | name = "rustc_version" 325 | version = "0.2.3" 326 | source = "registry+https://github.com/rust-lang/crates.io-index" 327 | dependencies = [ 328 | "semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", 329 | ] 330 | 331 | [[package]] 332 | name = "semver" 333 | version = "0.9.0" 334 | source = "registry+https://github.com/rust-lang/crates.io-index" 335 | dependencies = [ 336 | "semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", 337 | ] 338 | 339 | [[package]] 340 | name = "semver-parser" 341 | version = "0.7.0" 342 | source = "registry+https://github.com/rust-lang/crates.io-index" 343 | 344 | [[package]] 345 | name = "serde" 346 | version = "1.0.27" 347 | source = "registry+https://github.com/rust-lang/crates.io-index" 348 | 349 | [[package]] 350 | name = "serde_derive" 351 | version = "1.0.27" 352 | source = "registry+https://github.com/rust-lang/crates.io-index" 353 | dependencies = [ 354 | "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", 355 | "serde_derive_internals 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)", 356 | "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", 357 | ] 358 | 359 | [[package]] 360 | name = "serde_derive_internals" 361 | version = "0.19.0" 362 | source = "registry+https://github.com/rust-lang/crates.io-index" 363 | dependencies = [ 364 | "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", 365 | "synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)", 366 | ] 367 | 368 | [[package]] 369 | name = "serde_json" 370 | version = "1.0.9" 371 | source = "registry+https://github.com/rust-lang/crates.io-index" 372 | dependencies = [ 373 | "dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", 374 | "itoa 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", 375 | "num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)", 376 | "serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", 377 | ] 378 | 379 | [[package]] 380 | name = "sha1" 381 | version = "0.6.0" 382 | source = "registry+https://github.com/rust-lang/crates.io-index" 383 | 384 | [[package]] 385 | name = "snake-rs" 386 | version = "0.1.0" 387 | dependencies = [ 388 | "gdk 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", 389 | "gdk-pixbuf 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 390 | "gtk 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 391 | "rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", 392 | "stdweb 0.4.13 (registry+https://github.com/rust-lang/crates.io-index)", 393 | ] 394 | 395 | [[package]] 396 | name = "stdweb" 397 | version = "0.4.13" 398 | source = "registry+https://github.com/rust-lang/crates.io-index" 399 | dependencies = [ 400 | "discard 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", 401 | "rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", 402 | "serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", 403 | "serde_json 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)", 404 | "stdweb-derive 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", 405 | "stdweb-internal-macros 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", 406 | "stdweb-internal-runtime 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", 407 | ] 408 | 409 | [[package]] 410 | name = "stdweb-derive" 411 | version = "0.5.1" 412 | source = "registry+https://github.com/rust-lang/crates.io-index" 413 | dependencies = [ 414 | "proc-macro2 0.4.26 (registry+https://github.com/rust-lang/crates.io-index)", 415 | "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", 416 | "serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", 417 | "serde_derive 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", 418 | "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)", 419 | ] 420 | 421 | [[package]] 422 | name = "stdweb-internal-macros" 423 | version = "0.2.5" 424 | source = "registry+https://github.com/rust-lang/crates.io-index" 425 | dependencies = [ 426 | "base-x 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 427 | "proc-macro2 0.4.26 (registry+https://github.com/rust-lang/crates.io-index)", 428 | "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", 429 | "serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", 430 | "serde_derive 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", 431 | "serde_json 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)", 432 | "sha1 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", 433 | "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)", 434 | ] 435 | 436 | [[package]] 437 | name = "stdweb-internal-runtime" 438 | version = "0.1.3" 439 | source = "registry+https://github.com/rust-lang/crates.io-index" 440 | 441 | [[package]] 442 | name = "syn" 443 | version = "0.11.11" 444 | source = "registry+https://github.com/rust-lang/crates.io-index" 445 | dependencies = [ 446 | "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", 447 | "synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)", 448 | "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", 449 | ] 450 | 451 | [[package]] 452 | name = "syn" 453 | version = "0.15.26" 454 | source = "registry+https://github.com/rust-lang/crates.io-index" 455 | dependencies = [ 456 | "proc-macro2 0.4.26 (registry+https://github.com/rust-lang/crates.io-index)", 457 | "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", 458 | "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 459 | ] 460 | 461 | [[package]] 462 | name = "synom" 463 | version = "0.11.3" 464 | source = "registry+https://github.com/rust-lang/crates.io-index" 465 | dependencies = [ 466 | "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", 467 | ] 468 | 469 | [[package]] 470 | name = "unicode-xid" 471 | version = "0.0.4" 472 | source = "registry+https://github.com/rust-lang/crates.io-index" 473 | 474 | [[package]] 475 | name = "unicode-xid" 476 | version = "0.1.0" 477 | source = "registry+https://github.com/rust-lang/crates.io-index" 478 | 479 | [[package]] 480 | name = "winapi" 481 | version = "0.2.8" 482 | source = "registry+https://github.com/rust-lang/crates.io-index" 483 | 484 | [[package]] 485 | name = "winapi" 486 | version = "0.3.4" 487 | source = "registry+https://github.com/rust-lang/crates.io-index" 488 | dependencies = [ 489 | "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 490 | "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 491 | ] 492 | 493 | [[package]] 494 | name = "winapi-i686-pc-windows-gnu" 495 | version = "0.4.0" 496 | source = "registry+https://github.com/rust-lang/crates.io-index" 497 | 498 | [[package]] 499 | name = "winapi-x86_64-pc-windows-gnu" 500 | version = "0.4.0" 501 | source = "registry+https://github.com/rust-lang/crates.io-index" 502 | 503 | [metadata] 504 | "checksum atk-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "33a67fd81e1922dddc335887516f2f5254534e89c9d39fa89bca5d79bd150d34" 505 | "checksum base-x 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2f59103b47307f76e03bef1633aec7fa9e29bfb5aa6daf5a334f94233c71f6c1" 506 | "checksum bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b3c30d3802dfb7281680d6285f2ccdaa8c2d8fee41f93805dba5c4cf50dc23cf" 507 | "checksum c_vec 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "6237ac5a4b1e81c213c24c6437964c61e646df910a914b4ab1487b46df20bd13" 508 | "checksum cairo-rs 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b6b5695f59fd036fe5741bc5a4eb20c78fbe42256e3b08a2af26bbcbe8070bf3" 509 | "checksum cairo-sys-rs 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7c6e18fecaeac51809db57f45f4553cc0975225a7eb435a7a7e91e5e8113a84d" 510 | "checksum discard 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "212d0f5754cb6769937f4501cc0e67f4f4483c8d2c3e1e922ee9edbe4ab4c7c0" 511 | "checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab" 512 | "checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" 513 | "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" 514 | "checksum gdk 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a1e51db95be6565011bcd5cd99f9b17fdd585001057a999b21e09f1e8c28deb9" 515 | "checksum gdk-pixbuf 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "16160d212ae91abe9f3324c3fb233929ba322dde63585d15cda3336f8c529ed1" 516 | "checksum gdk-pixbuf-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "798f97101eea8180da363d0e80e07ec7ec6d1809306601c0100c1de5bc8b4f52" 517 | "checksum gdk-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d4ee916f5f25c5f4b21bd9dcb12a216ae697406940ff9476358c308a8ececada" 518 | "checksum gio 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "84ba5a2beb559059a0c9c2bd3681743cdede8d9a36c775840bca800333b22867" 519 | "checksum gio-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a303bbf7a5e75ab3b627117ff10e495d1b9e97e1d68966285ac2b1f6270091bc" 520 | "checksum glib 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b9b0452824cc63066940f01adc721804919f0b76cdba3cfab977b00b87f16d4a" 521 | "checksum glib-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d9693049613ff52b93013cc3d2590366d8e530366d288438724b73f6c7dc4be8" 522 | "checksum gobject-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "60d507c87a71b1143c66ed21a969be9b99a76df234b342d733e787e6c9c7d7c2" 523 | "checksum gtk 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0847c507e52c1feaede13ef56fb4847742438602655449d5f1f782e8633f146f" 524 | "checksum gtk-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "905fcfbaaad1b44ec0b4bba9e4d527d728284c62bc2ba41fccedace2b096766f" 525 | "checksum itoa 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8324a32baf01e2ae060e9de58ed0bc2320c9a2833491ee36cd3b4c414de4db8c" 526 | "checksum lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c8f31047daa365f19be14b47c29df4f7c3b581832407daabe6ae77397619237d" 527 | "checksum libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "1e5d97d6708edaa407429faa671b942dc0f2727222fb6b6539bf1db936e4b121" 528 | "checksum num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)" = "92e5113e9fd4cc14ded8e499429f396a20f98c772a47cc8622a736e1ec843c31" 529 | "checksum num-traits 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e7de20f146db9d920c45ee8ed8f71681fd9ade71909b48c3acbd766aa504cf10" 530 | "checksum pango 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3e81c404ab81ea7ea2fc2431a0a7672507b80e4b8bf4b41eac3fc83cc665104e" 531 | "checksum pango-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "34f34a1be107fe16abb2744e0e206bee4b3b07460b5fddd3009a6aaf60bd69ab" 532 | "checksum pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "3a8b4c6b8165cd1a1cd4b9b120978131389f64bdaf456435caa41e630edba903" 533 | "checksum proc-macro2 0.4.26 (registry+https://github.com/rust-lang/crates.io-index)" = "38fddd23d98b2144d197c0eca5705632d4fe2667d14a6be5df8934f8d74f1978" 534 | "checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" 535 | "checksum quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)" = "cdd8e04bd9c52e0342b406469d494fcb033be4bdbe5c606016defbb1681411e1" 536 | "checksum rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "eba5f8cb59cc50ed56be8880a5c7b496bfd9bd26394e176bc67884094145c2c5" 537 | "checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" 538 | "checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" 539 | "checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" 540 | "checksum serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)" = "db99f3919e20faa51bb2996057f5031d8685019b5a06139b1ce761da671b8526" 541 | "checksum serde_derive 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)" = "f4ba7591cfe93755e89eeecdbcc668885624829b020050e6aec99c2a03bd3fd0" 542 | "checksum serde_derive_internals 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6e03f1c9530c3fb0a0a5c9b826bdd9246a5921ae995d75f512ac917fc4dd55b5" 543 | "checksum serde_json 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)" = "c9db7266c7d63a4c4b7fe8719656ccdd51acf1bed6124b174f933b009fb10bcb" 544 | "checksum sha1 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2579985fda508104f7587689507983eadd6a6e84dd35d6d115361f530916fa0d" 545 | "checksum stdweb 0.4.13 (registry+https://github.com/rust-lang/crates.io-index)" = "461e7f2e33670b1c33f1ea22bb2f86de6136fabd0c4d27d167ed425c231143ca" 546 | "checksum stdweb-derive 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0e21ebd9179de08f2300a65454268a17ea3de204627458588c84319c4def3930" 547 | "checksum stdweb-internal-macros 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "432465093692af7379dcd196ce4be398c906958d91b412fff9102a66238d6f26" 548 | "checksum stdweb-internal-runtime 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a2a2f4a2eb556337b2d1a302630bbddf989ae383c70393e89b48152b9896cbda" 549 | "checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" 550 | "checksum syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)" = "f92e629aa1d9c827b2bb8297046c1ccffc57c99b947a680d3ccff1f136a3bee9" 551 | "checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" 552 | "checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" 553 | "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" 554 | "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" 555 | "checksum winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "04e3bd221fcbe8a271359c04f21a76db7d0c6028862d1bb5512d85e1e2eb5bb3" 556 | "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 557 | "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 558 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "snake-rs" 3 | version = "0.1.0" 4 | edition = "2018" 5 | authors = ["kirjavascript"] 6 | 7 | [dependencies] 8 | gtk = { version = "^0", optional = true } 9 | gdk = { version = "^0", optional = true } 10 | gdk-pixbuf = { version = "^0", optional = true } 11 | rand = { version = "0.4.2", optional = true } 12 | stdweb = { version = "0.4.13", optional = true } 13 | 14 | [features] 15 | gtk-deps = ["gtk", "gdk", "gdk-pixbuf", "rand"] 16 | web-deps = ["stdweb"] 17 | 18 | [[bin]] 19 | name = "gtk" 20 | path = "src/gtk.rs" 21 | required-features = ["gtk-deps"] 22 | 23 | [[bin]] 24 | name = "web" 25 | path = "src/web.rs" 26 | required-features = ["web-deps"] 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # snake-rs - [play online](http://snake.kirjava.xyz) 2 | 3 | build for gtk 4 | 5 | ```shell 6 | cargo run --bin gtk --features=gtk-deps 7 | ``` 8 | 9 | build for web 10 | 11 | ```shell 12 | cargo +nightly web start --target wasm32-unknown-unknown --bin web --features web-deps 13 | ``` 14 | 15 | build for nintendo switch 16 | 17 | install [devKitPro](https://devkitpro.org/wiki/Getting_Started#Setup) 18 | and follow the rusty-horizon [setup guide](https://github.com/rusty-horizon/setup-guide) 19 | 20 | ``` 21 | pacman -S switch-sdl2 switch-sdl2_ttf 22 | cd switch && sh build.sh 23 | ``` 24 | 25 | or download the [nro](switch/snake-rs.nro) 26 | -------------------------------------------------------------------------------- /src/gtk.rs: -------------------------------------------------------------------------------- 1 | extern crate gtk; 2 | extern crate gdk; 3 | extern crate gdk_pixbuf; 4 | extern crate rand; 5 | 6 | use gtk::prelude::*; 7 | use gtk::{DrawingArea}; 8 | use gdk::ContextExt; 9 | use gdk_pixbuf::Pixbuf; 10 | 11 | use std::cell::RefCell; 12 | use std::rc::Rc; 13 | 14 | mod snake; 15 | use snake::Snake; 16 | 17 | static TITLE: &str = "snake-rs"; 18 | 19 | fn main() { 20 | if gtk::init().is_err() { 21 | panic!("Failed to initialize GTK."); 22 | } 23 | 24 | let collide_mode = &std::env::args().collect::>()[1..].join("") == "--collide"; 25 | 26 | // init stuff 27 | let window = gtk::Window::new(gtk::WindowType::Toplevel); 28 | 29 | window.set_title(TITLE); 30 | window.set_position(gtk::WindowPosition::Center); 31 | window.set_default_size(640, 480); 32 | window.set_role("__float"); 33 | 34 | window.connect_delete_event(|_, _| { 35 | gtk::main_quit(); 36 | Inhibit(false) 37 | }); 38 | 39 | let canvas = DrawingArea::new(); 40 | window.add(&canvas); 41 | window.show_all(); 42 | 43 | // load snake 44 | let snake = Rc::new(RefCell::new(Snake::new(64, 48, collide_mode))); 45 | 46 | // drawing callback 47 | let snake_draw_clone = snake.clone(); 48 | canvas.connect_draw(move |_, ctx| { 49 | let pixels = Pixbuf::new_from_vec( 50 | snake_draw_clone.borrow_mut().get_rgba(), 51 | 0, // colourspace 52 | true, // has_alpha 53 | 8, // bits_per_sample 54 | 64, // width 55 | 48, // height 56 | 64 * 4, // row_stride (Distance in bytes between row starts) 57 | ); 58 | let pixels_scaled = pixbuf_scale(pixels, 64, 48, 10); 59 | ctx.set_source_pixbuf(&pixels_scaled, 0f64, 0f64); 60 | ctx.paint(); 61 | Inhibit(false) 62 | }); 63 | 64 | // input 65 | let snake_input_clone = snake.clone(); 66 | window.connect_key_press_event(move |_, e| { 67 | if snake_input_clone.borrow().is_running() { 68 | let change = match e.get_keyval() { 69 | 65362 | 119 | 87 => Some(snake::Direction::Up), 70 | 65364 | 115 | 83 => Some(snake::Direction::Down), 71 | 65361 | 97 | 56 => Some(snake::Direction::Left), 72 | 65363 | 100 | 68 => Some(snake::Direction::Right), 73 | _ => None, 74 | }; 75 | if change.is_some() { 76 | snake_input_clone.borrow_mut().change_direction(change.unwrap()); 77 | } 78 | } 79 | else if e.get_keyval() == 32 { 80 | snake_input_clone.borrow_mut().restart(); 81 | } 82 | Inhibit(false) 83 | }); 84 | 85 | // step loop 86 | let tick = move || { 87 | // set score 88 | let mut new_title = format!("{} - score: {}", TITLE, snake.borrow().get_score()); 89 | if !snake.borrow().is_running() { 90 | new_title.push_str(" - press SPACE to restart"); 91 | } 92 | else { 93 | // step & draw 94 | snake.borrow_mut().step(); 95 | canvas.queue_draw(); 96 | } 97 | 98 | window.set_title(&new_title); 99 | gtk::Continue(true) 100 | }; 101 | gtk::timeout_add(45, tick); 102 | 103 | // gtk::idle_add 104 | gtk::main(); 105 | } 106 | 107 | fn pixbuf_scale(buf: Pixbuf, width: i32, height: i32, scale: i32) -> Pixbuf { 108 | let scaled_width = width * scale; 109 | let scaled_height = height * scale; 110 | let pixbuf_scale = Pixbuf::new_from_vec( 111 | vec![0; (scaled_width * scaled_height * 4) as usize], 112 | 0, true, 8, 113 | scaled_width, scaled_height, scaled_width * 4 114 | ); 115 | buf.scale(&pixbuf_scale, 116 | 0, 0, scaled_width, scaled_height, 117 | 0., 0., scale as f64, scale as f64, 0, 118 | ); 119 | pixbuf_scale 120 | } 121 | -------------------------------------------------------------------------------- /src/snake.rs: -------------------------------------------------------------------------------- 1 | use super::rand; 2 | 3 | pub struct Snake { 4 | facing: Direction, 5 | last_dir: Direction, 6 | head: Point, 7 | tail: Vec, 8 | fruit: Point, 9 | width: u32, 10 | height: u32, 11 | score: u64, 12 | running: bool, 13 | increase_amount: u8, 14 | color_seed: u16, 15 | collide_mode: bool, 16 | } 17 | 18 | #[derive(PartialEq)] 19 | pub struct Point { 20 | x: i32, 21 | y: i32, 22 | } 23 | 24 | enum Item { 25 | Snake, Fruit, Nothing, 26 | } 27 | 28 | #[derive(PartialEq, Clone)] 29 | pub enum Direction { 30 | Up, Down, Left, Right, 31 | } 32 | 33 | impl Direction { 34 | fn opposite(direction: &Direction) -> Direction { 35 | match direction { 36 | &Direction::Up => Direction::Down, 37 | &Direction::Down => Direction::Up, 38 | &Direction::Left => Direction::Right, 39 | &Direction::Right => Direction::Left 40 | } 41 | } 42 | } 43 | 44 | impl Point { 45 | fn clone(&self) -> Self { 46 | Point { 47 | x: self.x, 48 | y: self.y, 49 | } 50 | } 51 | 52 | fn collides(&self, point: &Point) -> bool { 53 | self.x == point.x && self.y == point.y 54 | } 55 | 56 | fn random(width: u32, height: u32) -> Self { 57 | let rnd1: u32 = rand::random(); 58 | let rnd2: u32 = rand::random(); 59 | Point { 60 | x: (rnd1 % width) as i32, 61 | y: (rnd2 % height) as i32, 62 | } 63 | } 64 | } 65 | 66 | impl Snake { 67 | pub fn new(width: u32, height: u32, collide_mode: bool) -> Self { 68 | Snake { 69 | facing: Direction::Right, 70 | last_dir: Direction::Right, 71 | head: Point::random(width, height), 72 | tail: Vec::new(), 73 | fruit: Point::random(width, height), 74 | width, 75 | height, 76 | score: 0, 77 | increase_amount: 0, 78 | running: true, 79 | color_seed: 0, 80 | collide_mode: collide_mode, 81 | } 82 | } 83 | 84 | pub fn restart(&mut self) { 85 | if !self.running { 86 | self.score = 0; 87 | self.running = true; 88 | self.tail = Vec::new(); 89 | } 90 | } 91 | 92 | fn collided(&mut self) -> bool { 93 | if self.collide_mode { 94 | self.running = false; 95 | self.head = Point::random(self.width, self.height); 96 | true 97 | } else { 98 | false 99 | } 100 | } 101 | 102 | pub fn step(&mut self) { 103 | if self.running { 104 | // clone head to tail 105 | self.tail.push(self.head.clone()); 106 | 107 | // update last direction 108 | self.last_dir = self.facing.clone(); 109 | 110 | // move head 111 | match self.facing { 112 | Direction::Right => { self.head.x += 1 }, 113 | Direction::Down => { self.head.y += 1 }, 114 | Direction::Up => { self.head.y -= 1 }, 115 | Direction::Left => { self.head.x -= 1 }, 116 | }; 117 | 118 | // wrap 119 | if self.head.x >= self.width as i32 { 120 | if !self.collided() { 121 | self.head.x = 0; 122 | } 123 | } 124 | else if self.head.x < 0 { 125 | if !self.collided() { 126 | self.head.x = self.width as i32 -1; 127 | } 128 | } 129 | else if self.head.y >= self.height as i32 { 130 | if !self.collided() { 131 | self.head.y = 0; 132 | } 133 | } 134 | else if self.head.y < 0 { 135 | if !self.collided() { 136 | self.head.y = self.height as i32 -1; 137 | } 138 | } 139 | 140 | // check collision with tail 141 | if self.tail.contains(&self.head) { 142 | self.running = false; 143 | } 144 | // check collision with fruit 145 | else if self.head == self.fruit { 146 | self.score += 10; 147 | self.fruit = loop { 148 | // create new fruit 149 | let fruit = Point::random(self.width, self.height); 150 | // ensure the new fruit isn't in the head or tail 151 | let head_col = self.head.collides(&fruit); 152 | if !self.tail.contains(&fruit) && !head_col { 153 | break fruit; 154 | } 155 | }; 156 | // add tail extension amount 157 | self.increase_amount = 3; 158 | } 159 | else if self.increase_amount != 0 { 160 | self.increase_amount -= 1; 161 | } 162 | else { 163 | // unshift tail 164 | self.tail.remove(0); 165 | } 166 | } 167 | } 168 | 169 | fn cell_qty(&self) -> usize { 170 | (self.width * self.height) as usize 171 | } 172 | 173 | pub fn change_direction(&mut self, direction: Direction) { 174 | // check if direction is opposite the last direction 175 | let opposite = Direction::opposite(&direction) == self.last_dir; 176 | if !opposite { 177 | self.facing = direction; 178 | } 179 | } 180 | 181 | pub fn get_score(&self) -> u64 { 182 | self.score 183 | } 184 | 185 | pub fn is_running(&self) -> bool { 186 | self.running 187 | } 188 | 189 | fn get_board(&self) -> Vec { 190 | let mut board = Vec::with_capacity(self.cell_qty()); 191 | 192 | for y in 0..self.height as i32 { 193 | for x in 0..self.width as i32 { 194 | // TODO: improve O(n^2) 195 | let point = Point{ x, y }; 196 | let head_col = self.head.collides(&point); 197 | let fruit_col = self.fruit.collides(&point); 198 | let tail_col = self.tail.contains(&Point { x, y }); 199 | let value = if head_col || tail_col { 200 | Item::Snake 201 | } 202 | else if fruit_col { 203 | Item::Fruit 204 | } 205 | else { 206 | Item::Nothing 207 | }; 208 | 209 | board.push(value); 210 | } 211 | } 212 | 213 | board 214 | } 215 | 216 | fn get_color(&mut self) -> (u8, u8, u8) { 217 | self.color_seed = self.color_seed.wrapping_add(4); 218 | let seed = self.color_seed; 219 | let group = ((seed / 255) % 5) as usize; 220 | let i = (seed % 255) as u8; 221 | let groups: [(u8, u8, u8); 5] = [ 222 | (0,255,4), 223 | (0,81,255), 224 | (75,0,130), 225 | (255,141,0), 226 | (227,255,0), 227 | ]; 228 | 229 | let start = groups[group]; 230 | let end = groups[(group + 1) % 5]; 231 | 232 | let r = lerp(start.0, end.0, i); 233 | let g = lerp(start.1, end.1, i); 234 | let b = lerp(start.2, end.2, i); 235 | 236 | (r, g, b) 237 | } 238 | 239 | pub fn get_rgba(&mut self) -> Vec { 240 | let mut rgb_board = Vec::with_capacity(self.cell_qty() * 3); 241 | let (r, g, b) = self.get_color(); 242 | 243 | for cell in self.get_board() { 244 | match cell { 245 | Item::Fruit => { 246 | rgb_board.push(255); 247 | rgb_board.push(60); 248 | rgb_board.push(60); 249 | rgb_board.push(255); 250 | }, 251 | Item::Nothing => { 252 | rgb_board.push(0x60); 253 | rgb_board.push(0x60); 254 | rgb_board.push(0x60); 255 | rgb_board.push(255); 256 | }, 257 | Item::Snake => { 258 | if self.running { 259 | rgb_board.push(r); 260 | rgb_board.push(g); 261 | rgb_board.push(b); 262 | rgb_board.push(255); 263 | } 264 | else { 265 | rgb_board.push(0); 266 | rgb_board.push(0); 267 | rgb_board.push(0); 268 | rgb_board.push(255); 269 | } 270 | }, 271 | } 272 | } 273 | 274 | rgb_board 275 | } 276 | } 277 | 278 | pub fn lerp(start: u8, end: u8, i: u8) -> u8 { 279 | (start as f32 + ((end as i64 - start as i64) as f32 * (i as f32 / 255.0))) as u8 280 | } 281 | -------------------------------------------------------------------------------- /src/web.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate stdweb; 3 | 4 | use stdweb::web::*; 5 | use stdweb::web::html_element::CanvasElement; 6 | use stdweb::traits::*; 7 | use stdweb::unstable::TryInto; 8 | 9 | use std::cell::RefCell; 10 | use std::rc::Rc; 11 | 12 | mod snake; 13 | use snake::Snake; 14 | 15 | mod rand { 16 | use stdweb::unstable::TryInto; 17 | pub fn random() -> u32 { 18 | (js!{ 19 | return Math.floor(Math.random() * 0xFFFFFFFF); 20 | }).try_into().unwrap() 21 | } 22 | } 23 | 24 | 25 | fn main() { 26 | stdweb::initialize(); 27 | 28 | // load snake, canvas 29 | let canvas: CanvasElement = document().query_selector("canvas").unwrap().unwrap().try_into().unwrap(); 30 | let ctx: CanvasRenderingContext2d = canvas.get_context().unwrap(); 31 | let info: Element = document().query_selector("span").unwrap().unwrap(); 32 | let collide_mode = js! { return window.location.hash == "#collide" }.try_into().unwrap(); 33 | let snake = Rc::new(RefCell::new(Snake::new(64, 48, collide_mode))); 34 | 35 | // drawing 36 | async_render_loop(snake.clone(), ctx, info); 37 | 38 | // input 39 | let snake_input_clone = snake.clone(); 40 | document().add_event_listener(move |e: event::KeyDownEvent| { 41 | if snake_input_clone.borrow().is_running() { 42 | let change = match e.key().as_str() { 43 | "ArrowUp" | "W" | "w" => Some(snake::Direction::Up), 44 | "ArrowDown" | "S" | "s" => Some(snake::Direction::Down), 45 | "ArrowRight" | "D" | "d" => Some(snake::Direction::Right), 46 | "ArrowLeft" | "A" | "a" => Some(snake::Direction::Left), 47 | _ => None, 48 | }; 49 | if change.is_some() { 50 | snake_input_clone.borrow_mut().change_direction(change.unwrap()); 51 | } 52 | } 53 | else if e.key().as_str() == " " { 54 | snake_input_clone.borrow_mut().restart(); 55 | } 56 | }); 57 | 58 | stdweb::event_loop(); 59 | } 60 | 61 | fn async_render_loop(snake: Rc>, ctx: CanvasRenderingContext2d, info: Element) { 62 | // TODO: set_timeout can cause lag due to how tasks are queued in the event loop 63 | // replace with requestAnimationFrame + performance.now() to fix 64 | set_timeout(move || { 65 | 66 | let mut info_text = format!("score: {}", snake.borrow().get_score()); 67 | 68 | if !snake.borrow().is_running() { 69 | info_text.push_str(" - press SPACE to restart"); 70 | } 71 | else { 72 | // step n draw 73 | snake.borrow_mut().step(); 74 | let board = snake.borrow_mut().get_rgba(); 75 | js! { 76 | @{&ctx}.putImageData(new ImageData( 77 | // TODO: avoid this clone 78 | Uint8ClampedArray.from(@{board}), 79 | 64, 80 | 48, 81 | ), 0, 0); 82 | } 83 | } 84 | 85 | info.set_text_content(&info_text); 86 | 87 | async_render_loop(snake, ctx, info); 88 | }, 45); 89 | } 90 | -------------------------------------------------------------------------------- /static/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | snake-rs 8 | 41 | 42 | 43 | 44 |
45 | 46 |
47 |

snake-rs

48 | 49 |
50 |
51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /switch/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | /target/ 3 | **/*.rs.bk 4 | -------------------------------------------------------------------------------- /switch/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "aho-corasick" 5 | version = "0.7.6" 6 | source = "registry+https://github.com/rust-lang/crates.io-index" 7 | dependencies = [ 8 | "memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 9 | ] 10 | 11 | [[package]] 12 | name = "ansi_term" 13 | version = "0.11.0" 14 | source = "registry+https://github.com/rust-lang/crates.io-index" 15 | dependencies = [ 16 | "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", 17 | ] 18 | 19 | [[package]] 20 | name = "atty" 21 | version = "0.2.13" 22 | source = "registry+https://github.com/rust-lang/crates.io-index" 23 | dependencies = [ 24 | "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", 25 | "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", 26 | ] 27 | 28 | [[package]] 29 | name = "bindgen" 30 | version = "0.37.4" 31 | source = "registry+https://github.com/rust-lang/crates.io-index" 32 | dependencies = [ 33 | "cexpr 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", 34 | "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 35 | "clang-sys 0.23.0 (registry+https://github.com/rust-lang/crates.io-index)", 36 | "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", 37 | "env_logger 0.5.13 (registry+https://github.com/rust-lang/crates.io-index)", 38 | "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 39 | "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", 40 | "peeking_take_while 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", 41 | "proc-macro2 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", 42 | "quote 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", 43 | "regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", 44 | "which 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", 45 | ] 46 | 47 | [[package]] 48 | name = "bitflags" 49 | version = "1.2.0" 50 | source = "registry+https://github.com/rust-lang/crates.io-index" 51 | 52 | [[package]] 53 | name = "cc" 54 | version = "1.0.45" 55 | source = "registry+https://github.com/rust-lang/crates.io-index" 56 | 57 | [[package]] 58 | name = "cexpr" 59 | version = "0.2.3" 60 | source = "registry+https://github.com/rust-lang/crates.io-index" 61 | dependencies = [ 62 | "nom 3.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 63 | ] 64 | 65 | [[package]] 66 | name = "cfg-if" 67 | version = "0.1.10" 68 | source = "registry+https://github.com/rust-lang/crates.io-index" 69 | 70 | [[package]] 71 | name = "clang-sys" 72 | version = "0.23.0" 73 | source = "registry+https://github.com/rust-lang/crates.io-index" 74 | dependencies = [ 75 | "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", 76 | "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", 77 | "libloading 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", 78 | ] 79 | 80 | [[package]] 81 | name = "clap" 82 | version = "2.33.0" 83 | source = "registry+https://github.com/rust-lang/crates.io-index" 84 | dependencies = [ 85 | "ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", 86 | "atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", 87 | "bitflags 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 88 | "strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", 89 | "textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", 90 | "unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", 91 | "vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", 92 | ] 93 | 94 | [[package]] 95 | name = "env_logger" 96 | version = "0.5.13" 97 | source = "registry+https://github.com/rust-lang/crates.io-index" 98 | dependencies = [ 99 | "atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", 100 | "humantime 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 101 | "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", 102 | "regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", 103 | "termcolor 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", 104 | ] 105 | 106 | [[package]] 107 | name = "glob" 108 | version = "0.2.11" 109 | source = "registry+https://github.com/rust-lang/crates.io-index" 110 | 111 | [[package]] 112 | name = "humantime" 113 | version = "1.3.0" 114 | source = "registry+https://github.com/rust-lang/crates.io-index" 115 | dependencies = [ 116 | "quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", 117 | ] 118 | 119 | [[package]] 120 | name = "lazy_static" 121 | version = "1.4.0" 122 | source = "registry+https://github.com/rust-lang/crates.io-index" 123 | 124 | [[package]] 125 | name = "libc" 126 | version = "0.2.62" 127 | source = "registry+https://github.com/rust-lang/crates.io-index" 128 | 129 | [[package]] 130 | name = "libloading" 131 | version = "0.5.2" 132 | source = "registry+https://github.com/rust-lang/crates.io-index" 133 | dependencies = [ 134 | "cc 1.0.45 (registry+https://github.com/rust-lang/crates.io-index)", 135 | "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", 136 | ] 137 | 138 | [[package]] 139 | name = "log" 140 | version = "0.4.8" 141 | source = "registry+https://github.com/rust-lang/crates.io-index" 142 | dependencies = [ 143 | "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 144 | ] 145 | 146 | [[package]] 147 | name = "memchr" 148 | version = "1.0.2" 149 | source = "registry+https://github.com/rust-lang/crates.io-index" 150 | dependencies = [ 151 | "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", 152 | ] 153 | 154 | [[package]] 155 | name = "memchr" 156 | version = "2.2.1" 157 | source = "registry+https://github.com/rust-lang/crates.io-index" 158 | 159 | [[package]] 160 | name = "nom" 161 | version = "3.2.1" 162 | source = "registry+https://github.com/rust-lang/crates.io-index" 163 | dependencies = [ 164 | "memchr 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", 165 | ] 166 | 167 | [[package]] 168 | name = "nx-rs" 169 | version = "0.1.0" 170 | source = "git+https://github.com/rusty-horizon/nx-rs#d9861e0cf93028c26f6bf08238b8a0b0ecdd3f4a" 171 | dependencies = [ 172 | "nx-sys 0.1.0 (git+https://github.com/rusty-horizon/nx-rs)", 173 | ] 174 | 175 | [[package]] 176 | name = "nx-sys" 177 | version = "0.1.0" 178 | source = "git+https://github.com/rusty-horizon/nx-rs#d9861e0cf93028c26f6bf08238b8a0b0ecdd3f4a" 179 | dependencies = [ 180 | "bindgen 0.37.4 (registry+https://github.com/rust-lang/crates.io-index)", 181 | "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 182 | ] 183 | 184 | [[package]] 185 | name = "peeking_take_while" 186 | version = "0.1.2" 187 | source = "registry+https://github.com/rust-lang/crates.io-index" 188 | 189 | [[package]] 190 | name = "proc-macro2" 191 | version = "0.3.5" 192 | source = "registry+https://github.com/rust-lang/crates.io-index" 193 | dependencies = [ 194 | "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 195 | ] 196 | 197 | [[package]] 198 | name = "quick-error" 199 | version = "1.2.2" 200 | source = "registry+https://github.com/rust-lang/crates.io-index" 201 | 202 | [[package]] 203 | name = "quote" 204 | version = "0.5.2" 205 | source = "registry+https://github.com/rust-lang/crates.io-index" 206 | dependencies = [ 207 | "proc-macro2 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", 208 | ] 209 | 210 | [[package]] 211 | name = "regex" 212 | version = "1.3.1" 213 | source = "registry+https://github.com/rust-lang/crates.io-index" 214 | dependencies = [ 215 | "aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)", 216 | "memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 217 | "regex-syntax 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)", 218 | "thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", 219 | ] 220 | 221 | [[package]] 222 | name = "regex-syntax" 223 | version = "0.6.12" 224 | source = "registry+https://github.com/rust-lang/crates.io-index" 225 | 226 | [[package]] 227 | name = "sdl2-sys" 228 | version = "0.1.0" 229 | source = "git+https://github.com/rusty-horizon/switch-sdl2-sys#9628437bf35c3ad4dc85de659a7b3f7244f286eb" 230 | dependencies = [ 231 | "bindgen 0.37.4 (registry+https://github.com/rust-lang/crates.io-index)", 232 | "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 233 | ] 234 | 235 | [[package]] 236 | name = "snake-rs" 237 | version = "0.0.1" 238 | dependencies = [ 239 | "nx-rs 0.1.0 (git+https://github.com/rusty-horizon/nx-rs)", 240 | "nx-sys 0.1.0 (git+https://github.com/rusty-horizon/nx-rs)", 241 | "sdl2-sys 0.1.0 (git+https://github.com/rusty-horizon/switch-sdl2-sys)", 242 | ] 243 | 244 | [[package]] 245 | name = "strsim" 246 | version = "0.8.0" 247 | source = "registry+https://github.com/rust-lang/crates.io-index" 248 | 249 | [[package]] 250 | name = "termcolor" 251 | version = "1.0.5" 252 | source = "registry+https://github.com/rust-lang/crates.io-index" 253 | dependencies = [ 254 | "wincolor 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", 255 | ] 256 | 257 | [[package]] 258 | name = "textwrap" 259 | version = "0.11.0" 260 | source = "registry+https://github.com/rust-lang/crates.io-index" 261 | dependencies = [ 262 | "unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", 263 | ] 264 | 265 | [[package]] 266 | name = "thread_local" 267 | version = "0.3.6" 268 | source = "registry+https://github.com/rust-lang/crates.io-index" 269 | dependencies = [ 270 | "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 271 | ] 272 | 273 | [[package]] 274 | name = "unicode-width" 275 | version = "0.1.6" 276 | source = "registry+https://github.com/rust-lang/crates.io-index" 277 | 278 | [[package]] 279 | name = "unicode-xid" 280 | version = "0.1.0" 281 | source = "registry+https://github.com/rust-lang/crates.io-index" 282 | 283 | [[package]] 284 | name = "vec_map" 285 | version = "0.8.1" 286 | source = "registry+https://github.com/rust-lang/crates.io-index" 287 | 288 | [[package]] 289 | name = "which" 290 | version = "1.0.5" 291 | source = "registry+https://github.com/rust-lang/crates.io-index" 292 | dependencies = [ 293 | "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", 294 | ] 295 | 296 | [[package]] 297 | name = "winapi" 298 | version = "0.3.8" 299 | source = "registry+https://github.com/rust-lang/crates.io-index" 300 | dependencies = [ 301 | "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 302 | "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 303 | ] 304 | 305 | [[package]] 306 | name = "winapi-i686-pc-windows-gnu" 307 | version = "0.4.0" 308 | source = "registry+https://github.com/rust-lang/crates.io-index" 309 | 310 | [[package]] 311 | name = "winapi-util" 312 | version = "0.1.2" 313 | source = "registry+https://github.com/rust-lang/crates.io-index" 314 | dependencies = [ 315 | "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", 316 | ] 317 | 318 | [[package]] 319 | name = "winapi-x86_64-pc-windows-gnu" 320 | version = "0.4.0" 321 | source = "registry+https://github.com/rust-lang/crates.io-index" 322 | 323 | [[package]] 324 | name = "wincolor" 325 | version = "1.0.2" 326 | source = "registry+https://github.com/rust-lang/crates.io-index" 327 | dependencies = [ 328 | "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", 329 | "winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", 330 | ] 331 | 332 | [metadata] 333 | "checksum aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)" = "58fb5e95d83b38284460a5fda7d6470aa0b8844d283a0b614b8535e880800d2d" 334 | "checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" 335 | "checksum atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "1803c647a3ec87095e7ae7acfca019e98de5ec9a7d01343f611cf3152ed71a90" 336 | "checksum bindgen 0.37.4 (registry+https://github.com/rust-lang/crates.io-index)" = "1b25ab82877ea8fe6ce1ce1f8ac54361f0218bad900af9eb11803994bf67c221" 337 | "checksum bitflags 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8a606a02debe2813760609f57a64a2ffd27d9fdf5b2f133eaca0b248dd92cdd2" 338 | "checksum cc 1.0.45 (registry+https://github.com/rust-lang/crates.io-index)" = "4fc9a35e1f4290eb9e5fc54ba6cf40671ed2a2514c3eeb2b2a908dda2ea5a1be" 339 | "checksum cexpr 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "42aac45e9567d97474a834efdee3081b3c942b2205be932092f53354ce503d6c" 340 | "checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" 341 | "checksum clang-sys 0.23.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d7f7c04e52c35222fffcc3a115b5daf5f7e2bfb71c13c4e2321afe1fc71859c2" 342 | "checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9" 343 | "checksum env_logger 0.5.13 (registry+https://github.com/rust-lang/crates.io-index)" = "15b0a4d2e39f8420210be8b27eeda28029729e2fd4291019455016c348240c38" 344 | "checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb" 345 | "checksum humantime 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f" 346 | "checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" 347 | "checksum libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)" = "34fcd2c08d2f832f376f4173a231990fa5aef4e99fb569867318a227ef4c06ba" 348 | "checksum libloading 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f2b111a074963af1d37a139918ac6d49ad1d0d5e47f72fd55388619691a7d753" 349 | "checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7" 350 | "checksum memchr 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "148fab2e51b4f1cfc66da2a7c32981d1d3c083a803978268bb11fe4b86925e7a" 351 | "checksum memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "88579771288728879b57485cc7d6b07d648c9f0141eb955f8ab7f9d45394468e" 352 | "checksum nom 3.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05aec50c70fd288702bcd93284a8444607f3292dbdf2a30de5ea5dcdbe72287b" 353 | "checksum nx-rs 0.1.0 (git+https://github.com/rusty-horizon/nx-rs)" = "" 354 | "checksum nx-sys 0.1.0 (git+https://github.com/rusty-horizon/nx-rs)" = "" 355 | "checksum peeking_take_while 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" 356 | "checksum proc-macro2 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "77997c53ae6edd6d187fec07ec41b207063b5ee6f33680e9fa86d405cdd313d4" 357 | "checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0" 358 | "checksum quote 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9949cfe66888ffe1d53e6ec9d9f3b70714083854be20fd5e271b232a017401e8" 359 | "checksum regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dc220bd33bdce8f093101afe22a037b8eb0e5af33592e6a9caafff0d4cb81cbd" 360 | "checksum regex-syntax 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)" = "11a7e20d1cce64ef2fed88b66d347f88bd9babb82845b2b858f3edbf59a4f716" 361 | "checksum sdl2-sys 0.1.0 (git+https://github.com/rusty-horizon/switch-sdl2-sys)" = "" 362 | "checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" 363 | "checksum termcolor 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "96d6098003bde162e4277c70665bd87c326f5a0c3f3fbfb285787fa482d54e6e" 364 | "checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" 365 | "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" 366 | "checksum unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7007dbd421b92cc6e28410fe7362e2e0a2503394908f417b68ec8d1c364c4e20" 367 | "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" 368 | "checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" 369 | "checksum which 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e84a603e7e0b1ce1aa1ee2b109c7be00155ce52df5081590d1ffb93f4f515cb2" 370 | "checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" 371 | "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 372 | "checksum winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7168bab6e1daee33b4557efd0e95d5ca70a03706d39fa5f3fe7a236f584b03c9" 373 | "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 374 | "checksum wincolor 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "96f5016b18804d24db43cebf3c77269e7569b8954a8464501c216cc5e070eaa9" 375 | -------------------------------------------------------------------------------- /switch/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "snake-rs" 3 | edition = "2018" 4 | version = "0.0.1" 5 | authors = ["kirjavascript"] 6 | 7 | [dependencies.nx-rs] 8 | git = "https://github.com/rusty-horizon/nx-rs" 9 | features = [] 10 | 11 | [dependencies.nx-sys] 12 | git = "https://github.com/rusty-horizon/nx-rs" 13 | features = [] 14 | 15 | [dependencies.sdl2-sys] 16 | git = "https://github.com/rusty-horizon/switch-sdl2-sys" 17 | branch = "master" 18 | features = ["ttf"] 19 | -------------------------------------------------------------------------------- /switch/Xargo.toml: -------------------------------------------------------------------------------- 1 | [dependencies.core] 2 | stage = 0 3 | 4 | [dependencies.alloc] 5 | stage = 1 6 | 7 | [dependencies.libc] 8 | stage = 2 9 | default-features = false 10 | features = ["align", "rustc-dep-of-std"] 11 | 12 | [dependencies.std] 13 | git = "https://github.com/rusty-horizon/nx-rs" 14 | branch = "master" 15 | default-features = false 16 | features = [ 17 | "panic-unwind", 18 | "backtrace", 19 | ] 20 | stage = 3 21 | -------------------------------------------------------------------------------- /switch/aarch64-horizon-elf.json: -------------------------------------------------------------------------------- 1 | { 2 | "family": "unix", 3 | "env": "newlib", 4 | "target-env": "newlib", 5 | "target-family": "unix", 6 | "target-c-int-width": "32", 7 | "target-endian": "little", 8 | "target-pointer-width": "64", 9 | "os": "horizon-nx", 10 | "arch": "aarch64", 11 | "panic-strategy": "unwind", 12 | "abi-blacklist": [ 13 | "stdcall", 14 | "fastcall", 15 | "vectorcall", 16 | "thiscall", 17 | "win64", 18 | "sysv64" 19 | ], 20 | "dynamic-linking" : false, 21 | "features": "+a57,+strict-align,+crc,+crypto", 22 | "data-layout": "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128", 23 | "executables": true, 24 | "position-independent-executables" : true, 25 | "linker-flavor": "gcc", 26 | "llvm-target": "aarch64-unknown-none", 27 | "has-elf-tls" : false, 28 | "linker-is-gnu" : true, 29 | "disable-redzone" : true, 30 | "linker" : "/opt/devkitpro/devkitA64/bin/aarch64-none-elf-gcc", 31 | "pre-link-args": { 32 | "gcc": [ 33 | "-specs=/opt/devkitpro/libnx/switch.specs", 34 | "-march=armv8-a", 35 | "-mtune=cortex-a57", 36 | "-mtp=soft", 37 | "-nodefaultlibs", 38 | "-nostdlib", 39 | "-nostartfiles", 40 | "-L/opt/devkitpro/portlibs/switch/lib", 41 | "-L/opt/devkitpro/libnx/lib", 42 | "-L/opt/devkitpro/devkitA64/lib/gcc/aarch64-none-elf/8.3.0/pic", 43 | "-L/opt/devkitpro/devkitA64/aarch64-none-elf/lib/pic" 44 | ] 45 | }, 46 | "late-link-args": { 47 | "gcc": [ 48 | "-Wl,--start-group", 49 | "-lSDL2", 50 | "-lSDL2_ttf", 51 | "-lfreetype", 52 | "-lbz2", 53 | "-lpng", 54 | "-lz", 55 | "-lEGL", 56 | "-lGLESv2", 57 | "-lglapi", 58 | "-ldrm_nouveau", 59 | "-lstdc++", 60 | "-lgcc", 61 | "-lc", 62 | "-lnx", 63 | "-lsysbase", 64 | "-lm", 65 | "-l:crtbegin.o", 66 | "-l:crtend.o", 67 | "-l:crti.o", 68 | "-l:crtn.o", 69 | "-Wl,--end-group", 70 | "-fPIE", 71 | "-pie" 72 | ] 73 | }, 74 | "post-link-args": { 75 | "gcc": [ 76 | "--verbose", 77 | "-Wl,-z,text", 78 | "-Wl,-z,muldefs" 79 | ] 80 | }, 81 | "relocation-model" : "pic", 82 | "max-atomic-width": 128, 83 | "exe-suffix": ".elf", 84 | "staticlib-suffix" : ".a", 85 | "trap-unreachable" : true, 86 | "emit-debug-gdb-scripts" : true, 87 | "requires-uwtable" : true 88 | } 89 | -------------------------------------------------------------------------------- /switch/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Rust examples - simple build script for Switch formats 4 | # Customizable options (those you should only edit) are those starting by '*'. 5 | # Most attributes will be commented (disabled), uncomment to set specific attributes 6 | 7 | # * Output type -> { elf, nro, nso } 8 | output_type="nro" 9 | 10 | # * NACP attributes (optional, NRO only) -> NRO name, author and version (try using the same ones that are in Cargo.toml) 11 | nacp_name="snake-rs" 12 | nacp_author="kirjavascript" 13 | nacp_version="1.0.0" 14 | 15 | # * Icon (optional, NRO only) -> NRO's icon (256x256 JPEG), using libnx's one by default 16 | icon=icon.jpg 17 | 18 | # * RomFs directory (optional, NRO only) -> directory name to be embedded within the NRO as a RomFs filesystem. 19 | romfs_dir=romfs 20 | 21 | # --- 22 | # --- 23 | # ! Build implementation, better not to edit this part 24 | # --- 25 | # --- 26 | 27 | # Toolchain, must be the JSON's name (better not to change this or the JSON itself, unless you want to add extra libraries) 28 | target=aarch64-horizon-elf 29 | 30 | # Build the project 31 | CARGO_INCREMENTAL=0 RUST_TARGET_PATH=$PWD RUST_BACKTRACE=1 xargo build --release --target $target 32 | 33 | # Locate ELF output file 34 | for elf in "$PWD/target/$target/release"/*.elf; do 35 | outelf=$elf 36 | break 1 37 | done 38 | 39 | # Output file name (for generating other Switch formats) 40 | outelfname="$(basename -- "$PWD/$outelf")" 41 | outname="${outelfname%.*}" 42 | 43 | # Default value for nacp_name if isn't set 44 | if [ -z "$nacp_name" ]; then 45 | nacp_name=Rust application 46 | fi 47 | 48 | # Default value for nacp_author if isn't set 49 | if [ -z "$nacp_author" ]; then 50 | nacp_name=Unknown author 51 | fi 52 | 53 | # Default value for nacp_version if isn't set 54 | if [ -z "$nacp_version" ]; then 55 | nacp_name=Unknown version 56 | fi 57 | 58 | # Default icon if isn't specified 59 | if [ -z "$icon" ]; then 60 | icon=/opt/devkitpro/libnx/default_icon.jpg 61 | fi 62 | 63 | if [ "$output_type" = "elf" ]; then 64 | # If just plain ELF, copy output one and that's it 65 | newelf=$PWD/$outname 66 | cp -r "$elf" "$newelf" 67 | elif [ "$output_type" = "nro" ]; then 68 | outnro="$PWD/$outname.nro" 69 | 70 | # Generate NACP 71 | outnacp="$PWD/target/$target/release/$outname.nacp" 72 | /opt/devkitpro/tools/bin/nacptool --create "$nacp_name" "$nacp_author" "$nacp_version" "$outnacp" 73 | 74 | # Generate NRO 75 | if [ -n "$romfs_dir" ]; then 76 | romfsdir="--romfsdir=$PWD/$romfs_dir" 77 | fi 78 | /opt/devkitpro/tools/bin/elf2nro "$elf" "$outnro" --nacp="$outnacp" --icon="$icon" "$romfsdir" 79 | 80 | echo "Generated NRO: $outnro" 81 | elif [ "$output_type" = "nso" ]; then 82 | outnso="$PWD/$outname".nso 83 | 84 | # Command for NSO generation 85 | /opt/devkitpro/tools/bin/elf2nso "$elf" "$outnso" 86 | echo "Generated NSO: $outnso" 87 | fi 88 | -------------------------------------------------------------------------------- /switch/clean.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | cargo clean 4 | xargo clean 5 | find $PWD -name "*.elf" -type f -delete 6 | find $PWD -name "*.nro" -type f -delete 7 | find $PWD -name "*.nso" -type f -delete -------------------------------------------------------------------------------- /switch/icon.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirjavascript/snake-rs/8186383eabc78e4de5e29e2b920cb1bba5154e24/switch/icon.jpg -------------------------------------------------------------------------------- /switch/romfs/hack.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirjavascript/snake-rs/8186383eabc78e4de5e29e2b920cb1bba5154e24/switch/romfs/hack.ttf -------------------------------------------------------------------------------- /switch/romfs/small_font.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirjavascript/snake-rs/8186383eabc78e4de5e29e2b920cb1bba5154e24/switch/romfs/small_font.ttf -------------------------------------------------------------------------------- /switch/snake-rs.nro: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirjavascript/snake-rs/8186383eabc78e4de5e29e2b920cb1bba5154e24/switch/snake-rs.nro -------------------------------------------------------------------------------- /switch/src/main.rs: -------------------------------------------------------------------------------- 1 | mod snake; 2 | use snake::Snake; 3 | use sdl2_sys::*; 4 | 5 | mod rand { 6 | pub fn random() -> u32 { 7 | unsafe { sdl2_sys::random() as _ } 8 | } 9 | } 10 | 11 | fn s(s: &str) -> *const u8 { 12 | format!("{}\0", s).as_bytes().as_ptr() 13 | } 14 | 15 | fn main() { 16 | unsafe { 17 | let mut snake = Snake::new(64, 48, false); 18 | 19 | SDL_Init(SDL_INIT_EVERYTHING as u32); 20 | TTF_Init(); 21 | nx_sys::romfsMount(s("romfs")); 22 | let window = SDL_CreateWindow(s("snake-rs"), 0, 0, 1280, 720, 0); 23 | let renderer = SDL_CreateRenderer(window, 0, 2 | 4); 24 | SDL_SetRenderDrawBlendMode(renderer, 1); 25 | SDL_SetHint(s("SDL_RENDER_SCALE_QUALITY"), s("2")); 26 | 27 | let scale = 15i32; 28 | let font = TTF_OpenFont(s("romfs:/small_font.ttf"), 24); 29 | 30 | let mut fc = 0; 31 | loop { 32 | fc += 1; 33 | if fc % 4 == 0 { 34 | snake.step(); 35 | } 36 | 37 | // input 38 | 39 | let ipt = nx::hid::input_down(nx::hid::Controller::Auto); 40 | if nx::input_any!(ipt, nx::hid::Key::Minus) { 41 | break; // minus to exit 42 | } else if nx::input_any!(ipt, nx::hid::Key::Plus) { 43 | snake.restart(); 44 | } else if nx::input_any!(ipt, nx::hid::Key::DPadLeft) { 45 | snake.change_direction(snake::Direction::Left); 46 | } else if nx::input_any!(ipt, nx::hid::Key::DPadRight) { 47 | snake.change_direction(snake::Direction::Right); 48 | } else if nx::input_any!(ipt, nx::hid::Key::DPadUp) { 49 | snake.change_direction(snake::Direction::Up); 50 | } else if nx::input_any!(ipt, nx::hid::Key::DPadDown) { 51 | snake.change_direction(snake::Direction::Down); 52 | } 53 | 54 | // clear screen 55 | 56 | SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); 57 | SDL_RenderClear(renderer); 58 | 59 | // HUD 60 | 61 | // let surface = TTF_RenderText_Solid(font, s("test"), SDL_Color { 62 | // r: 255, 63 | // g: 255, 64 | // b: 255, 65 | // a: 255, 66 | // }); 67 | // let texture = SDL_CreateTextureFromSurface(renderer, surface); 68 | // SDL_RenderCopy(renderer, texture, core::ptr::null(), &SDL_Rect { 69 | // x: scale * 0, 70 | // y: 0, 71 | // w: (*surface).w, 72 | // h: (*surface).h, 73 | // }); 74 | 75 | // draw playfield 76 | for (i, pixel) in snake.get_rgba().chunks(4).enumerate() { 77 | SDL_SetRenderDrawColor( 78 | renderer, 79 | pixel[0], 80 | pixel[1], 81 | pixel[2], 82 | pixel[3], 83 | ); 84 | SDL_RenderFillRect(renderer, &SDL_Rect { 85 | x: (scale * (i as i32 % 64)), 86 | y: scale * (i as i32 / 64), 87 | w: scale, 88 | h: scale, 89 | }); 90 | } 91 | 92 | SDL_RenderPresent(renderer); 93 | } 94 | 95 | SDL_DestroyRenderer(renderer); 96 | SDL_DestroyWindow(window); 97 | TTF_Quit(); 98 | SDL_Quit(); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /switch/src/snake.rs: -------------------------------------------------------------------------------- 1 | ../../src/snake.rs --------------------------------------------------------------------------------