Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions glitch/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "glitch"
edition.workspace = true
license.workspace = true
version.workspace = true

[dependencies]
cmf-wp2-app.workspace = true
cmf-wp2-ui.workspace = true

[[bin]]
name = "glitch"
bench = false
19 changes: 19 additions & 0 deletions glitch/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
MIT License

Copyright (c) 2026 X0D4N

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

This licence covers the app code (src/*.rs except src/sprites.rs, tools/, icon.svg, docs). The bitmaps in
src/sprites.rs come from Chromium and stay under the BSD-3-Clause licence in NOTICE-CHROMIUM.
41 changes: 41 additions & 0 deletions glitch/NOTICE-CHROMIUM
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Third-party notice: Chromium offline dinosaur game
==================================================

src/sprites.rs holds 1-bit bitmaps cut from the sprite sheet of the Chromium offline "T-Rex runner" game
(components/neterror/resources/images/default_100_percent/offline/100-offline-sprite.png), and the physics in
src/dino.rs is a port of the constants and rules in components/neterror/resources/dino_game/*.ts, rewritten in
integer Rust. Source: https://chromium.googlesource.com/chromium/src/+/main/components/neterror/resources/

Credit: the T-Rex, cacti, pterodactyl, cloud and ground art and the game's rules are (c) The Chromium Authors.

The Chromium LICENSE follows verbatim (fetched from https://chromium.googlesource.com/chromium/src/+/main/LICENSE):

----------------------------------------------------------------------------------------------------------------

// Copyright 2015 The Chromium Authors
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google LLC nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 changes: 15 additions & 0 deletions glitch/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added glitch/shots/2048.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added glitch/shots/bottle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added glitch/shots/bottle_spin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added glitch/shots/bottle_stop.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added glitch/shots/dice.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added glitch/shots/dice_rolled.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added glitch/shots/dino.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added glitch/shots/dino_jump.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added glitch/shots/g2048_play.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added glitch/shots/hub.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added glitch/shots/snake.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added glitch/shots/snake_play.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added glitch/shots/tictactoe.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added glitch/shots/tictactoe_win.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added glitch/shots/ttt_play.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
242 changes: 242 additions & 0 deletions glitch/src/app.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
// The hub: a list of games, one tap to start, back (crown press / right swipe) to return. Pure logic over a
// `Surface`, so the host tests drive it exactly as the device loop does.

use crate::dino::Dino;
use crate::g2048::G2048;
use crate::bottle::Bottle;
use crate::dice::Dice;
use crate::gfx::*;
use crate::snake::Snake;
use crate::ttt::Ttt;
use cmf_wp2_ui::haptic::{DONE, DOUBLE, Pattern, TICK};
use cmf_wp2_ui::text::chars;
use cmf_wp2_ui::Surface;

pub const NAMES: [&str; 6] = ["DINO RUN", "SNAKE", "BOTTLE", "DICE", "2048", "TIC TAC TOE"];
const MAGIC: u8 = 0x47;
const ROW0: i32 = 96;
#[cfg(test)]
pub const ROW0_FOR_TESTS: i32 = ROW0;
const ROWH: i32 = 52;

pub enum Cur {
Dino(Dino),
Snake(Snake),
Bottle(Bottle),
Dice(Dice),
G2048(G2048),
Ttt(Ttt),
}

macro_rules! each {
($c:expr, $g:ident => $e:expr) => {
match $c {
Cur::Dino($g) => $e,
Cur::Snake($g) => $e,
Cur::Bottle($g) => $e,
Cur::Dice($g) => $e,
Cur::G2048($g) => $e,
Cur::Ttt($g) => $e,
}
};
}

#[derive(Default, Clone, Copy)]
pub struct Out {
pub haptic: Option<Pattern>,
pub save: bool,
pub exit: bool,
pub redraw: bool,
}

pub struct App {
pub cur: Option<Cur>,
pub idx: usize,
pub sel: usize,
pub best: [u32; 6],
pub seed: u32,
need_full: bool,
}

fn out(ev: u8) -> Out {
Out {
haptic: if ev & EV_DONE != 0 {
Some(DONE)
} else if ev & EV_DOUBLE != 0 {
Some(DOUBLE)
} else if ev & EV_TICK != 0 {
Some(TICK)
} else {
None
},
save: ev & EV_DONE != 0,
exit: false,
redraw: ev & EV_DIRTY != 0,
}
}

impl App {
pub fn new(blob: &[u8]) -> App {
let mut best = [0u32; 6];
if blob.len() >= 25 && blob[0] == MAGIC {
for i in 0..6 {
best[i] = u32::from_le_bytes([blob[1 + i * 4], blob[2 + i * 4], blob[3 + i * 4], blob[4 + i * 4]]);
}
}
App { cur: None, idx: 0, sel: 0, best, seed: 1, need_full: true }
}

pub fn blob(&self) -> [u8; 25] {
let mut b = [0u8; 25];
b[0] = MAGIC;
for i in 0..6 {
b[1 + i * 4..5 + i * 4].copy_from_slice(&self.best[i].to_le_bytes());
}
b
}

pub fn full(&mut self) {
self.need_full = true;
}

pub fn in_game(&self) -> bool {
self.cur.is_some()
}

/// ms until the next tick, 0 when nothing animates.
pub fn period(&self) -> u32 {
match &self.cur {
Some(c) => each!(c, g => if g.running() { g.period() } else { 0 }),
None => 0,
}
}

fn launch(&mut self, i: usize) {
let (seed, best) = (self.seed, self.best[i]);
self.cur = Some(match i {
0 => Cur::Dino(Dino::new(seed, best)),
1 => Cur::Snake(Snake::new(seed, best)),
2 => Cur::Bottle(Bottle::new(seed, best)),
3 => Cur::Dice(Dice::new(seed, best)),
4 => Cur::G2048(G2048::new(seed, best)),
_ => Cur::Ttt(Ttt::new(seed, best)),
});
self.idx = i;
self.need_full = true;
}

fn note(&mut self, mut o: Out) -> Out {
if let Some(c) = &self.cur {
let sc = each!(c, g => g.score());
if sc > self.best[self.idx] {
self.best[self.idx] = sc;
if o.save || o.haptic.is_some() && matches!(c, Cur::Ttt(_)) {
o.save = true;
}
}
}
o
}

pub fn touch(&mut self, x: i32, y: i32, down: bool) -> Out {
if let Some(c) = &mut self.cur {
let ev = each!(c, g => g.touch(x, y, down));
return self.note(out(ev));
}
if down && y >= ROW0 && y < ROW0 + ROWH * 6 {
let i = ((y - ROW0) / ROWH) as usize;
self.sel = i;
self.launch(i);
return Out { haptic: Some(TICK), redraw: true, ..Out::default() };
}
Out::default()
}

pub fn crown(&mut self, d: i32) -> Out {
if let Some(c) = &mut self.cur {
let ev = each!(c, g => g.crown(d));
return self.note(out(ev));
}
self.sel = (self.sel as i32 + d.signum() + 6) as usize % 6;
Out { haptic: Some(TICK), redraw: true, ..Out::default() }
}

pub fn tick(&mut self) -> Out {
if let Some(c) = &mut self.cur {
let ev = each!(c, g => g.tick());
return self.note(out(ev));
}
Out::default()
}

/// Back: a game returns to the hub, the hub leaves the app.
pub fn back(&mut self) -> Out {
if self.cur.is_some() {
self.cur = None;
self.need_full = true;
return Out { save: true, redraw: true, ..Out::default() };
}
Out { save: true, exit: true, ..Out::default() }
}

pub fn draw<S: Surface>(&mut self, s: &mut S) {
let full = self.need_full;
self.need_full = false;
if let Some(c) = &mut self.cur {
each!(c, g => g.draw(s, full));
return;
}
clear(s);
txt(s, C, 40, "GAMES", 4, 3, RED);
for (i, n) in NAMES.iter().enumerate() {
let y = ROW0 + ROWH * i as i32 + 14;
let on = i == self.sel;
txt(s, C, y, n, 3, 2, if on { WHITE } else { MID });
if on {
dot(s, C - chars(n) as i32 * 12 - 22, y + 11, 10, RED);
}
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn scores_roundtrip_through_the_blob() {
let mut a = App::new(&[]);
a.best = [1, 2, 3, 4, 5, 6];
let b = a.blob();
assert_eq!(App::new(&b).best, [1, 2, 3, 4, 5, 6]);
assert_eq!(App::new(&[9, 9, 9]).best, [0; 6]);
}

#[test]
fn hub_launches_every_game_and_back_returns() {
for i in 0..6 {
let mut a = App::new(&[]);
let o = a.touch(233, ROW0 + ROWH * i + 20, true);
assert!(o.redraw && a.in_game() && a.idx == i as usize);
let o = a.back();
assert!(!o.exit && !a.in_game());
}
let mut a = App::new(&[]);
assert!(a.back().exit);
assert!(!a.touch(233, 20, true).redraw); // title area does nothing
}

#[test]
fn high_score_is_kept_and_saved_on_game_over() {
let mut a = App::new(&[]);
a.touch(233, ROW0 + 10, true); // dino
a.touch(233, 233, true); // start
for _ in 0..4000 {
let o = a.tick();
if o.save {
break;
}
}
assert!(a.best[0] > 0, "no score recorded");
}
}
Loading