aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-06-10 20:55:43 +0200
committerCharles <sircharlesaze@gmail.com>2020-06-10 20:55:43 +0200
commit7fa4f497f77471fa145881d759afe0b521971e04 (patch)
tree4cb813362075667db6dbec140dc2ce166dc4d894
parent47d15d537c61b828e72113cc8d3d8da3db9a2efb (diff)
downloadrutikmer-7fa4f497f77471fa145881d759afe0b521971e04.tar.gz
rutikmer-7fa4f497f77471fa145881d759afe0b521971e04.tar.bz2
rutikmer-7fa4f497f77471fa145881d759afe0b521971e04.zip
Added shuffle generator
-rw-r--r--Cargo.lock69
-rw-r--r--Cargo.toml2
-rw-r--r--src/main.rs55
-rw-r--r--src/shuffle.rs99
-rw-r--r--src/state.rs0
-rw-r--r--src/text.rs28
-rw-r--r--src/time.rs24
-rw-r--r--src/ui.rs23
8 files changed, 275 insertions, 25 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 78fcd3c..1cbd525 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -13,6 +13,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
+name = "getrandom"
+version = "0.1.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "wasi",
+]
+
+[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -25,17 +36,65 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9457b06509d27052635f90d6466700c65095fdf75409b3fbdd903e988b886f49"
[[package]]
+name = "ppv-lite86"
+version = "0.2.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "237a5ed80e274dbc66f86bd59c1e25edc039660be53194b5fe0a482e0f2612ea"
+
+[[package]]
+name = "rand"
+version = "0.7.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
+dependencies = [
+ "getrandom",
+ "libc",
+ "rand_chacha",
+ "rand_core",
+ "rand_hc",
+]
+
+[[package]]
+name = "rand_chacha"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
+dependencies = [
+ "ppv-lite86",
+ "rand_core",
+]
+
+[[package]]
+name = "rand_core"
+version = "0.5.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
+dependencies = [
+ "getrandom",
+]
+
+[[package]]
+name = "rand_hc"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
+dependencies = [
+ "rand_core",
+]
+
+[[package]]
name = "rutikmer"
version = "0.1.0"
dependencies = [
+ "rand",
"sdl2",
]
[[package]]
name = "sdl2"
-version = "0.34.0"
+version = "0.34.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6693f362118c0cd32858fe33a14d74cc3111bbc3a7de1aee88e5289fd9289f97"
+checksum = "45c68e7a45838d6dd8723347a5d233f2c989f0b291f18a46158d5dfbb275ada1"
dependencies = [
"bitflags",
"lazy_static",
@@ -52,3 +111,9 @@ dependencies = [
"cfg-if",
"libc",
]
+
+[[package]]
+name = "wasi"
+version = "0.9.0+wasi-snapshot-preview1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
diff --git a/Cargo.toml b/Cargo.toml
index d373e5e..beaf7e9 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -5,6 +5,8 @@ authors = ["Charles <sircharlesaze@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+[dependencies]
+rand = "*"
[dependencies.sdl2]
version = "0.34.0"
diff --git a/src/main.rs b/src/main.rs
index d87720a..c997be1 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,57 +1,80 @@
use sdl2::ttf;
-use sdl2::event::Event;
+use sdl2::event::{Event};
use sdl2::keyboard::Keycode;
use sdl2::pixels::Color;
+use sdl2::rect::Rect;
+use sdl2::render::{Texture, TextureCreator};
pub mod time;
+pub mod shuffle;
+pub mod text;
+
+const WIDTH: u32 = 640;
+const HEIGHT: u32 = 480;
+const TITLE: &str = "rutikmer";
fn main() {
let sdl = sdl2::init().unwrap();
- let video_subsys = sdl.video().unwrap();
let ttf = ttf::init().unwrap();
- let font = ttf.load_font("font/FiraMono-Regular.ttf", 40).unwrap();
-
+ let video_subsys = sdl.video().unwrap();
let window = video_subsys
- .window("rutikmer", 640, 480)
+ .window(TITLE, WIDTH, HEIGHT)
+ .resizable()
.build()
.unwrap();
-
- let mut timer = time::Timer::new();
+ let font = ttf.load_font("font/FiraMono-Regular.ttf", 40).unwrap();
let mut canvas = window.into_canvas().build().unwrap();
- let texture_creator = canvas.texture_creator();
let mut event_pump = sdl.event_pump().unwrap();
+ let tex_creator = canvas.texture_creator();
+
+ let mut timer = time::Timer::new();
+ // let mut ui = ui::UI::new(WIDTH, HEIGHT);
+ //
+ let timer_rect = Rect::new(10, 10, 100, 40);
'running: loop {
for e in event_pump.poll_iter() {
match e {
Event::Quit {..} => break 'running,
- Event::KeyDown { keycode: Some(Keycode::Space), .. } => {
+ Event::KeyDown { keycode: Some(Keycode::Space), repeat: false, .. } => {
match timer.state {
time::State::Active => timer.stop(),
time::State::Inactive => timer.idle(),
time::State::Idle => {},
}
}
- Event::KeyUp { keycode: Some(Keycode::Space), .. } => {
+ Event::KeyUp { keycode: Some(Keycode::Space), repeat: false, .. } => {
if timer.state == time::State::Idle {
timer.start();
}
},
+
+ // Event::Window { win_event: WindowEvent::Resized(w, h), .. } =>
+ // ui.set_layout(w as u32, h as u32),
_ => {}
}
}
- match timer.state {
- time::State::Idle => canvas.set_draw_color(Color::RGB(100, 100, 0)),
- time::State::Active => canvas.set_draw_color(Color::RGB(0, 100, 0)),
- time::State::Inactive => canvas.set_draw_color(Color::RGB(0, 0, 0)),
- }
+ let bg_color = match timer.state {
+ time::State::Idle => Color::RGB(100, 100, 0),
+ time::State::Active => Color::RGB(0, 100, 0),
+ time::State::Inactive => Color::RGB(0, 0, 0),
+ };
+ canvas.set_draw_color(bg_color);
canvas.clear();
if timer.state != time::State::Idle {
- canvas.copy(&timer.to_texture(&font, &texture_creator), None, None).unwrap();
+ canvas.copy(&timer.to_texture(&font, &tex_creator, &bg_color),
+ None,
+ timer_rect)
+ .unwrap();
}
+ let s = shuffle::Move::string_sequence(10);
+ let shuff_tex = text::to_texture(&s, &font, &tex_creator, &bg_color);
+ canvas.copy(&shuff_tex, None, Rect::new(10, 100, 500, 40)).unwrap();
+
+
canvas.present();
std::thread::sleep(std::time::Duration::new(0, 3_000_000));
}
diff --git a/src/shuffle.rs b/src/shuffle.rs
index e69de29..8d421b6 100644
--- a/src/shuffle.rs
+++ b/src/shuffle.rs
@@ -0,0 +1,99 @@
+use rand::{distributions::{Distribution, Standard}, Rng};
+
+#[derive(PartialEq)]
+enum MoveDirection {
+ Front,
+ Back,
+ Down,
+ Up,
+ Right,
+ Left,
+}
+
+enum MoveModifier {
+ None,
+ Twice,
+ Prime,
+}
+
+pub struct Move {
+ direction: MoveDirection,
+ modifier: MoveModifier,
+}
+
+
+// https://stackoverflow.com/questions/48490049
+impl Distribution<MoveDirection> for Standard {
+ fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> MoveDirection {
+ use MoveDirection::*;
+
+ match rng.gen_range(0, 6) {
+ 0 => Front,
+ 1 => Back,
+ 2 => Down,
+ 3 => Up,
+ 4 => Right,
+ _ => Left,
+ }
+ }
+}
+
+impl Distribution<MoveModifier> for Standard {
+ fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> MoveModifier {
+ use MoveModifier::*;
+
+ match rng.gen_range(0, 3) {
+ 0 => None,
+ 1 => Twice,
+ _ => Prime,
+ }
+ }
+}
+
+impl Move {
+ pub fn sequence(n: usize) -> Vec<Move> {
+ let mut sequence: Vec<Move> = Vec::with_capacity(n);
+
+ while sequence.len() != n {
+ let direction = rand::random::<MoveDirection>();
+ let modifier = rand::random::<MoveModifier>();
+
+ if let Some(l) = sequence.last() {
+ if l.direction == direction {
+ continue;
+ }
+ }
+ sequence.push(Move { direction, modifier });
+ }
+ sequence
+ }
+
+ pub fn string_sequence(n: usize) -> String {
+ let seq = Move::sequence(n);
+ seq.iter().map(|m| m.to_string() + " ").collect::<Vec<String>>().join(" ")
+ }
+}
+
+use std::fmt;
+
+impl fmt::Display for Move {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ use MoveDirection::*;
+ use MoveModifier::*;
+
+ let letter = match self.direction {
+ Front => "F",
+ Back => "B",
+ Down => "D",
+ Up => "U",
+ Right => "R",
+ Left => "L",
+ };
+ let modifier = match self.modifier {
+ None => "",
+ Twice => "2",
+ Prime => "'",
+ };
+ write!(f, "{}{}", letter, modifier)
+ }
+}
diff --git a/src/state.rs b/src/state.rs
deleted file mode 100644
index e69de29..0000000
--- a/src/state.rs
+++ /dev/null
diff --git a/src/text.rs b/src/text.rs
new file mode 100644
index 0000000..10ca700
--- /dev/null
+++ b/src/text.rs
@@ -0,0 +1,28 @@
+use sdl2::ttf;
+use sdl2::render::{Texture, TextureCreator};
+use sdl2::pixels::Color;
+
+struct Factory<'a, T> {
+ font: ttf::Font<'a, 'a>,
+ creator: &'a TextureCreator<T>,
+ bg: Color,
+}
+
+impl Factory<'a, T> {
+ pub fn new(font: &ttf::Font<'a, 'a>, creator: &'a TextureCreator<T>, bg: Color) -> Factory {
+ Factory { font, creator, bg }
+ }
+}
+
+pub fn to_texture<'a, T>(
+ s: &'a String,
+ font: &ttf::Font,
+ tex_creator: &'a TextureCreator<T>,
+ bg: &Color
+) -> Texture<'a>
+{
+ let surface = font.render(s).shaded(Color::RGB(255, 255, 255), *bg).unwrap();
+ tex_creator.create_texture_from_surface(&surface).unwrap()
+}
+
+// pub fn width(s: &String
diff --git a/src/time.rs b/src/time.rs
index 2cdd768..eba7507 100644
--- a/src/time.rs
+++ b/src/time.rs
@@ -39,12 +39,22 @@ impl Timer {
self.state = State::Idle;
}
- pub fn to_texture<'a, T>(&'a self, font: &ttf::Font, texture_creator: &'a TextureCreator<T>) -> Texture {
- let rendered = if self.state == State::Active { self.time.elapsed().unwrap() } else { self.result };
-
- let surface = font.render(&rendered.as_millis().to_string())
- .solid(Color::RGB(255, 255, 255))
- .unwrap();
- texture_creator.create_texture_from_surface(&surface).unwrap()
+ pub fn to_texture<'a, T>(
+ &'a self,
+ font: &ttf::Font,
+ tex_creator: &'a TextureCreator<T>,
+ bg: &Color
+ ) -> Texture
+ {
+ let current = if self.state == State::Active {
+ self.time.elapsed().unwrap()
+ } else {
+ self.result
+ }.as_millis();
+
+ let s = format!("{}.{}", current / 1000, current % 1000);
+
+ let surface = font.render(&s).shaded(Color::RGB(255, 255, 255), *bg).unwrap();
+ tex_creator.create_texture_from_surface(&surface).unwrap()
}
}
diff --git a/src/ui.rs b/src/ui.rs
new file mode 100644
index 0000000..011e5ed
--- /dev/null
+++ b/src/ui.rs
@@ -0,0 +1,23 @@
+use sdl2::rect::Rect;
+
+pub struct UI {
+ pub history_rect: Rect,
+ pub shuffle_rect: Rect,
+ pub timer_rect: Rect,
+}
+
+impl UI {
+ pub fn new(width: u32, height: u32) -> UI {
+ let default = Rect::new(0, 0, 0, 0);
+ let mut ret = UI {history_rect: default, shuffle_rect: default, timer_rect:default};
+ ret.set_layout(width, height);
+ ret
+ }
+
+ pub fn set_layout(&mut self, width: u32, height: u32) {
+ self.history_rect = Rect::new(0, 0, width / 3, height);
+ self.shuffle_rect = Rect::new((width / 3) as i32, 0, width - width / 3, height / 4);
+ self.timer_rect = Rect::new((width / 3) as i32, (height / 4 + (height - height / 4) / 2) as i32,
+ 100, 40);
+ }
+}