aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.rs28
-rw-r--r--src/shuffle.rs5
-rw-r--r--src/text.rs45
-rw-r--r--src/time.rs20
4 files changed, 54 insertions, 44 deletions
diff --git a/src/main.rs b/src/main.rs
index c997be1..26b5468 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,7 +3,6 @@ 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;
@@ -29,9 +28,10 @@ fn main() {
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);
+ let mut text_factory = text::Factory::new(&font, &tex_creator, Color::RGB(0, 0, 0));
+
+ let mut shuffle_str = shuffle::Move::string_sequence(10);
'running: loop {
for e in event_pump.poll_iter() {
@@ -39,9 +39,12 @@ fn main() {
Event::Quit {..} => break 'running,
Event::KeyDown { keycode: Some(Keycode::Space), repeat: false, .. } => {
match timer.state {
- time::State::Active => timer.stop(),
- time::State::Inactive => timer.idle(),
time::State::Idle => {},
+ time::State::Active => timer.stop(),
+ time::State::Inactive => {
+ timer.idle();
+ shuffle_str = shuffle::Move::string_sequence(10);
+ },
}
}
Event::KeyUp { keycode: Some(Keycode::Space), repeat: false, .. } => {
@@ -61,21 +64,18 @@ fn main() {
time::State::Inactive => Color::RGB(0, 0, 0),
};
canvas.set_draw_color(bg_color);
+ text_factory.set_bg(bg_color);
canvas.clear();
if timer.state != time::State::Idle {
- canvas.copy(&timer.to_texture(&font, &tex_creator, &bg_color),
- None,
- timer_rect)
- .unwrap();
+ canvas.copy(&text_factory.from_string(&timer.to_string()), 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();
-
+ if timer.state == time::State::Inactive {
+ canvas.copy(&text_factory.from_string(&shuffle_str), None, Rect::new(10, 100, 500, 40)).unwrap();
+ }
canvas.present();
- std::thread::sleep(std::time::Duration::new(0, 3_000_000));
+ std::thread::sleep(std::time::Duration::new(0, 30_000_000));
}
}
diff --git a/src/shuffle.rs b/src/shuffle.rs
index 8d421b6..7f8f88f 100644
--- a/src/shuffle.rs
+++ b/src/shuffle.rs
@@ -1,4 +1,7 @@
-use rand::{distributions::{Distribution, Standard}, Rng};
+use rand::{
+ distributions::{Distribution, Standard},
+ Rng
+};
#[derive(PartialEq)]
enum MoveDirection {
diff --git a/src/text.rs b/src/text.rs
index 10ca700..a27ca49 100644
--- a/src/text.rs
+++ b/src/text.rs
@@ -2,27 +2,42 @@ use sdl2::ttf;
use sdl2::render::{Texture, TextureCreator};
use sdl2::pixels::Color;
-struct Factory<'a, T> {
- font: ttf::Font<'a, 'a>,
+const WHITE: Color = Color::RGB(255, 255, 255);
+
+pub struct Factory<'a, T> {
+ font: &'a 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 {
+impl<'a, T> Factory<'a, T> {
+ pub fn new(font: &'a ttf::Font,
+ creator: &'a TextureCreator<T>,
+ bg: Color
+ ) -> Factory<'a, T>
+ {
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 from_string(&self, s: &String) -> Texture
+ {
+ let surface = self.font.render(s).shaded(WHITE, self.bg).unwrap();
+ self.creator.create_texture_from_surface(&surface).unwrap()
+ }
+
+ pub fn set_bg(&mut self, bg: Color) {
+ self.bg = bg;
+ }
}
-// pub fn width(s: &String
+
+// pub trait TextTexture {
+// fn to_texture<T>(&self, factory: &Factory<T>) -> Texture;
+// }
+//
+// impl TextTexture for String {
+// fn to_texture<T>(&self, factory: &Factory<T>) -> Texture {
+// let surface = factory.font.render(self).shaded(WHITE, factory.bg).unwrap();
+// factory.creator.create_texture_from_surface(&surface).unwrap()
+// }
+// }
diff --git a/src/time.rs b/src/time.rs
index eba7507..791ff2e 100644
--- a/src/time.rs
+++ b/src/time.rs
@@ -1,7 +1,4 @@
use std::time::{SystemTime, Duration};
-use sdl2::ttf;
-use sdl2::render::{Texture, TextureCreator};
-use sdl2::pixels::Color;
#[derive(PartialEq)]
pub enum State {
@@ -38,23 +35,18 @@ impl Timer {
pub fn idle(&mut self) {
self.state = State::Idle;
}
+}
+
+use std::fmt;
- pub fn to_texture<'a, T>(
- &'a self,
- font: &ttf::Font,
- tex_creator: &'a TextureCreator<T>,
- bg: &Color
- ) -> Texture
- {
+impl fmt::Display for Timer {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
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()
+ write!(f, "{}.{}", current / 1000, current % 1000)
}
}