diff --git a/Cargo.lock b/Cargo.lock index d40925d..7fc6673 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,62 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "kids-alphabet" version = "0.1.0" +dependencies = [ + "sdl2", +] + +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "libc" +version = "0.2.169" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" + +[[package]] +name = "sdl2" +version = "0.37.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b498da7d14d1ad6c839729bd4ad6fc11d90a57583605f3b4df2cd709a9cd380" +dependencies = [ + "bitflags", + "lazy_static", + "libc", + "sdl2-sys", +] + +[[package]] +name = "sdl2-sys" +version = "0.37.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "951deab27af08ed9c6068b7b0d05a93c91f0a8eb16b6b816a5e73452a43521d3" +dependencies = [ + "cfg-if", + "libc", + "version-compare", +] + +[[package]] +name = "version-compare" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "579a42fc0b8e0c63b76519a339be31bed574929511fa53c1a3acae26eb258f29" diff --git a/Cargo.toml b/Cargo.toml index 245a547..60cf33b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" edition = "2021" [dependencies] +sdl2 = { version = "0.37.0", features = ["image"]} diff --git a/assets/a.png b/assets/a.png new file mode 100644 index 0000000..bcb0a9e Binary files /dev/null and b/assets/a.png differ diff --git a/assets/a.xcf b/assets/a.xcf new file mode 100644 index 0000000..51e1fed Binary files /dev/null and b/assets/a.xcf differ diff --git a/assets/b.png b/assets/b.png new file mode 100644 index 0000000..f00610a Binary files /dev/null and b/assets/b.png differ diff --git a/assets/b.xcf b/assets/b.xcf new file mode 100644 index 0000000..8600189 Binary files /dev/null and b/assets/b.xcf differ diff --git a/assets/c.png b/assets/c.png new file mode 100644 index 0000000..f233a8a Binary files /dev/null and b/assets/c.png differ diff --git a/assets/c.xcf b/assets/c.xcf new file mode 100644 index 0000000..26a2981 Binary files /dev/null and b/assets/c.xcf differ diff --git a/assets/cat.jpg b/assets/cat.jpg new file mode 100644 index 0000000..f454998 Binary files /dev/null and b/assets/cat.jpg differ diff --git a/assets/cat.png b/assets/cat.png new file mode 100644 index 0000000..2d2fbb7 Binary files /dev/null and b/assets/cat.png differ diff --git a/assets/cat.xcf b/assets/cat.xcf new file mode 100644 index 0000000..774a8b9 Binary files /dev/null and b/assets/cat.xcf differ diff --git a/assets/d.png b/assets/d.png new file mode 100644 index 0000000..85a13bc Binary files /dev/null and b/assets/d.png differ diff --git a/assets/d.xcf b/assets/d.xcf new file mode 100644 index 0000000..6d6ec6b Binary files /dev/null and b/assets/d.xcf differ diff --git a/assets/dog.jpg b/assets/dog.jpg new file mode 100644 index 0000000..aa9c851 Binary files /dev/null and b/assets/dog.jpg differ diff --git a/assets/dog.png b/assets/dog.png new file mode 100644 index 0000000..a2af762 Binary files /dev/null and b/assets/dog.png differ diff --git a/assets/dog.xcf b/assets/dog.xcf new file mode 100644 index 0000000..ce4a3d1 Binary files /dev/null and b/assets/dog.xcf differ diff --git a/assets/e.png b/assets/e.png new file mode 100644 index 0000000..04170cf Binary files /dev/null and b/assets/e.png differ diff --git a/assets/e.xcf b/assets/e.xcf new file mode 100644 index 0000000..4d5cace Binary files /dev/null and b/assets/e.xcf differ diff --git a/assets/f.png b/assets/f.png new file mode 100644 index 0000000..2f703a7 Binary files /dev/null and b/assets/f.png differ diff --git a/assets/f.xcf b/assets/f.xcf new file mode 100644 index 0000000..cd1260d Binary files /dev/null and b/assets/f.xcf differ diff --git a/assets/g.png b/assets/g.png new file mode 100644 index 0000000..66c13f5 Binary files /dev/null and b/assets/g.png differ diff --git a/assets/g.xcf b/assets/g.xcf new file mode 100644 index 0000000..a0c96be Binary files /dev/null and b/assets/g.xcf differ diff --git a/assets/h.png b/assets/h.png new file mode 100644 index 0000000..78a919c Binary files /dev/null and b/assets/h.png differ diff --git a/assets/h.xcf b/assets/h.xcf new file mode 100644 index 0000000..8f9709e Binary files /dev/null and b/assets/h.xcf differ diff --git a/assets/i.png b/assets/i.png new file mode 100644 index 0000000..388ed25 Binary files /dev/null and b/assets/i.png differ diff --git a/assets/i.xcf b/assets/i.xcf new file mode 100644 index 0000000..edce199 Binary files /dev/null and b/assets/i.xcf differ diff --git a/assets/j.png b/assets/j.png new file mode 100644 index 0000000..5f90656 Binary files /dev/null and b/assets/j.png differ diff --git a/assets/j.xcf b/assets/j.xcf new file mode 100644 index 0000000..2d80eae Binary files /dev/null and b/assets/j.xcf differ diff --git a/assets/k.png b/assets/k.png new file mode 100644 index 0000000..ed6957f Binary files /dev/null and b/assets/k.png differ diff --git a/assets/k.xcf b/assets/k.xcf new file mode 100644 index 0000000..96a7d1b Binary files /dev/null and b/assets/k.xcf differ diff --git a/assets/l.png b/assets/l.png new file mode 100644 index 0000000..3fcd9a2 Binary files /dev/null and b/assets/l.png differ diff --git a/assets/l.xcf b/assets/l.xcf new file mode 100644 index 0000000..d27d73a Binary files /dev/null and b/assets/l.xcf differ diff --git a/assets/m.png b/assets/m.png new file mode 100644 index 0000000..7319187 Binary files /dev/null and b/assets/m.png differ diff --git a/assets/m.xcf b/assets/m.xcf new file mode 100644 index 0000000..dc7287e Binary files /dev/null and b/assets/m.xcf differ diff --git a/assets/n.png b/assets/n.png new file mode 100644 index 0000000..d41791b Binary files /dev/null and b/assets/n.png differ diff --git a/assets/n.xcf b/assets/n.xcf new file mode 100644 index 0000000..290e678 Binary files /dev/null and b/assets/n.xcf differ diff --git a/assets/o.png b/assets/o.png new file mode 100644 index 0000000..a631145 Binary files /dev/null and b/assets/o.png differ diff --git a/assets/o.xcf b/assets/o.xcf new file mode 100644 index 0000000..e3d8714 Binary files /dev/null and b/assets/o.xcf differ diff --git a/assets/p.png b/assets/p.png new file mode 100644 index 0000000..3c29761 Binary files /dev/null and b/assets/p.png differ diff --git a/assets/p.xcf b/assets/p.xcf new file mode 100644 index 0000000..ec733b1 Binary files /dev/null and b/assets/p.xcf differ diff --git a/assets/q.png b/assets/q.png new file mode 100644 index 0000000..296cfb3 Binary files /dev/null and b/assets/q.png differ diff --git a/assets/q.xcf b/assets/q.xcf new file mode 100644 index 0000000..464e8ca Binary files /dev/null and b/assets/q.xcf differ diff --git a/assets/r.png b/assets/r.png new file mode 100644 index 0000000..c8a1a5c Binary files /dev/null and b/assets/r.png differ diff --git a/assets/s.png b/assets/s.png new file mode 100644 index 0000000..fce3302 Binary files /dev/null and b/assets/s.png differ diff --git a/assets/t.png b/assets/t.png new file mode 100644 index 0000000..d95bbdd Binary files /dev/null and b/assets/t.png differ diff --git a/assets/t.xcf b/assets/t.xcf new file mode 100644 index 0000000..c7c68f9 Binary files /dev/null and b/assets/t.xcf differ diff --git a/assets/u.png b/assets/u.png new file mode 100644 index 0000000..e3c3308 Binary files /dev/null and b/assets/u.png differ diff --git a/assets/u.xcf b/assets/u.xcf new file mode 100644 index 0000000..f186c9f Binary files /dev/null and b/assets/u.xcf differ diff --git a/assets/v.png b/assets/v.png new file mode 100644 index 0000000..8a5b865 Binary files /dev/null and b/assets/v.png differ diff --git a/assets/v.xcf b/assets/v.xcf new file mode 100644 index 0000000..900e61e Binary files /dev/null and b/assets/v.xcf differ diff --git a/assets/w.png b/assets/w.png new file mode 100644 index 0000000..383d248 Binary files /dev/null and b/assets/w.png differ diff --git a/assets/w.xcf b/assets/w.xcf new file mode 100644 index 0000000..0651d2c Binary files /dev/null and b/assets/w.xcf differ diff --git a/assets/x.png b/assets/x.png new file mode 100644 index 0000000..6855fdb Binary files /dev/null and b/assets/x.png differ diff --git a/assets/x.xcf b/assets/x.xcf new file mode 100644 index 0000000..014a009 Binary files /dev/null and b/assets/x.xcf differ diff --git a/assets/y.png b/assets/y.png new file mode 100644 index 0000000..be2aded Binary files /dev/null and b/assets/y.png differ diff --git a/assets/y.xcf b/assets/y.xcf new file mode 100644 index 0000000..f1be626 Binary files /dev/null and b/assets/y.xcf differ diff --git a/assets/z.png b/assets/z.png new file mode 100644 index 0000000..dda35d2 Binary files /dev/null and b/assets/z.png differ diff --git a/assets/z.xcf b/assets/z.xcf new file mode 100644 index 0000000..bc6ee2e Binary files /dev/null and b/assets/z.xcf differ diff --git a/flake.nix b/flake.nix index 680de52..bc2552d 100644 --- a/flake.nix +++ b/flake.nix @@ -14,7 +14,7 @@ { defaultPackage = naersk-lib.buildPackage ./.; devShell = with pkgs; mkShell { - buildInputs = [ cargo rustc rustfmt pre-commit rustPackages.clippy ]; + buildInputs = [ cargo rustc rustfmt pre-commit rustPackages.clippy SDL2 SDL2_image ]; RUST_SRC_PATH = rustPlatform.rustLibSrc; }; } diff --git a/src/main.rs b/src/main.rs index e7a11a9..56a07b8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,297 @@ -fn main() { - println!("Hello, world!"); +extern crate sdl2; + +use sdl2::event::Event; +use sdl2::image::{InitFlag, LoadTexture}; +use sdl2::keyboard::Keycode; +use sdl2::pixels::Color; +use sdl2::render::Texture; +use std::collections::HashMap; +use std::time::Duration; + +pub fn main() -> Result<(), String> { + let sdl_context = sdl2::init()?; + let video_subsystem = sdl_context.video()?; + let _image = sdl2::image::init(InitFlag::PNG); + + let window = video_subsystem + .window("rust-sdl2 demo: Video", 1024, 600) + .position_centered() + .opengl() + .build() + .map_err(|e| e.to_string())?; + + let mut canvas = window.into_canvas().build().map_err(|e| e.to_string())?; + + let texture_creator = canvas.texture_creator(); + + let mut textures: HashMap<&str, Texture> = HashMap::default(); + textures.insert( + "a", + texture_creator + .load_texture_bytes(include_bytes!("../assets/a.png")) + .unwrap(), + ); + textures.insert( + "b", + texture_creator + .load_texture_bytes(include_bytes!("../assets/b.png")) + .unwrap(), + ); + textures.insert( + "c", + texture_creator + .load_texture_bytes(include_bytes!("../assets/c.png")) + .unwrap(), + ); + textures.insert( + "d", + texture_creator + .load_texture_bytes(include_bytes!("../assets/d.png")) + .unwrap(), + ); + textures.insert( + "e", + texture_creator + .load_texture_bytes(include_bytes!("../assets/e.png")) + .unwrap(), + ); + textures.insert( + "f", + texture_creator + .load_texture_bytes(include_bytes!("../assets/f.png")) + .unwrap(), + ); + textures.insert( + "g", + texture_creator + .load_texture_bytes(include_bytes!("../assets/g.png")) + .unwrap(), + ); + textures.insert( + "h", + texture_creator + .load_texture_bytes(include_bytes!("../assets/h.png")) + .unwrap(), + ); + textures.insert( + "i", + texture_creator + .load_texture_bytes(include_bytes!("../assets/i.png")) + .unwrap(), + ); + textures.insert( + "j", + texture_creator + .load_texture_bytes(include_bytes!("../assets/j.png")) + .unwrap(), + ); + textures.insert( + "k", + texture_creator + .load_texture_bytes(include_bytes!("../assets/k.png")) + .unwrap(), + ); + textures.insert( + "l", + texture_creator + .load_texture_bytes(include_bytes!("../assets/l.png")) + .unwrap(), + ); + textures.insert( + "m", + texture_creator + .load_texture_bytes(include_bytes!("../assets/m.png")) + .unwrap(), + ); + textures.insert( + "n", + texture_creator + .load_texture_bytes(include_bytes!("../assets/n.png")) + .unwrap(), + ); + textures.insert( + "o", + texture_creator + .load_texture_bytes(include_bytes!("../assets/o.png")) + .unwrap(), + ); + textures.insert( + "p", + texture_creator + .load_texture_bytes(include_bytes!("../assets/p.png")) + .unwrap(), + ); + textures.insert( + "q", + texture_creator + .load_texture_bytes(include_bytes!("../assets/q.png")) + .unwrap(), + ); + textures.insert( + "r", + texture_creator + .load_texture_bytes(include_bytes!("../assets/r.png")) + .unwrap(), + ); + textures.insert( + "s", + texture_creator + .load_texture_bytes(include_bytes!("../assets/s.png")) + .unwrap(), + ); + textures.insert( + "t", + texture_creator + .load_texture_bytes(include_bytes!("../assets/t.png")) + .unwrap(), + ); + textures.insert( + "u", + texture_creator + .load_texture_bytes(include_bytes!("../assets/u.png")) + .unwrap(), + ); + textures.insert( + "v", + texture_creator + .load_texture_bytes(include_bytes!("../assets/v.png")) + .unwrap(), + ); + textures.insert( + "w", + texture_creator + .load_texture_bytes(include_bytes!("../assets/w.png")) + .unwrap(), + ); + textures.insert( + "x", + texture_creator + .load_texture_bytes(include_bytes!("../assets/x.png")) + .unwrap(), + ); + textures.insert( + "y", + texture_creator + .load_texture_bytes(include_bytes!("../assets/y.png")) + .unwrap(), + ); + textures.insert( + "z", + texture_creator + .load_texture_bytes(include_bytes!("../assets/z.png")) + .unwrap(), + ); + + // Special words + textures.insert( + "cat", + texture_creator + .load_texture_bytes(include_bytes!("../assets/cat.png")) + .unwrap(), + ); + textures.insert( + "dog", + texture_creator + .load_texture_bytes(include_bytes!("../assets/dog.png")) + .unwrap(), + ); + + let opts: Vec<&str> = textures.keys().map(|s| *s).collect(); + + canvas.set_draw_color(Color::RGB(255, 0, 0)); + canvas.clear(); + canvas.present(); + let mut event_pump = sdl_context.event_pump()?; + + let mut line = String::new(); + + 'running: loop { + for event in event_pump.poll_iter() { + match event { + Event::KeyDown { + keycode: Some(kc), .. + } => { + line.extend( + match kc { + Keycode::A => "a", + Keycode::B => "b", + Keycode::C => "c", + Keycode::D => "d", + Keycode::E => "e", + Keycode::F => "f", + Keycode::G => "g", + Keycode::H => "h", + Keycode::I => "i", + Keycode::J => "j", + Keycode::K => "k", + Keycode::L => "l", + Keycode::M => "m", + Keycode::N => "n", + Keycode::O => "o", + Keycode::P => "p", + Keycode::Q => "q", + Keycode::R => "r", + Keycode::S => "s", + Keycode::T => "t", + Keycode::U => "u", + Keycode::V => "v", + Keycode::W => "w", + Keycode::X => "x", + Keycode::Y => "y", + Keycode::Z => "z", + + Keycode::Escape => break 'running, + _ => "", + } + .chars(), + ); + } + Event::Quit { .. } => break 'running, + _ => {} + } + } + + let mut matches = vec![]; + let mut skip_chars = 0; + let mut chars = line.chars(); + for i in 0..line.len() { + let part_line = find_matches(chars.as_str(), &opts); + for part in part_line { + matches.push((i, part)); + } + if matches.len() == 0 { + // Advance the line so we don't leak chars + skip_chars = i; + } + chars.next(); + } + let matches: Vec<&str> = matches.into_iter().map(|(_, s)| s).collect(); + let show = if matches.len() > 0 { matches[0] } else { "" }; + // Discard extra characters from line + let (_, tline) = line.split_at(skip_chars); + line = String::from(tline); + + canvas.clear(); + if let Some(tex) = textures.get(show) { + canvas.copy(tex, None, None).unwrap(); + } + canvas.present(); + ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 30)); + // The rest of the game loop goes here... + } + + Ok(()) +} + +fn find_matches<'a, 'b>(word: &'b str, opts: &'a [&'a str]) -> Vec<&'a str> { + if word.len() == 0 { + return vec![]; + } + let mut ret = vec![]; + for opt in opts { + if &word == opt { + ret.push(*opt) + } + } + ret }