Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

häte: Examples #131

Merged
merged 6 commits into from
Oct 2, 2017
Merged
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ script:
- cargo build
- cargo test
- cargo build --release
- cargo build --examples --manifest-path hate/Cargo.toml

before_deploy:
- git clone --depth=1 https://github.com/ozkriff/zemeroth_assets assets
Expand Down
41 changes: 24 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

85 changes: 85 additions & 0 deletions hate/examples/actions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
extern crate hate;
extern crate cgmath;

use cgmath::vec2;
use hate::{Context, Event, Scene, Screen, Time};
use hate::geom::Point;
use hate::scene::Layer;
use hate::scene::action;
use hate::gui;

#[derive(Debug, Clone, Default)]
struct Layers {
fg: Layer,
}

impl Layers {
fn sorted(self) -> Vec<Layer> {
vec![self.fg]
}
}

#[derive(Debug)]
struct ActionsScreen {
scene: Scene,
layers: Layers,
}

impl ActionsScreen {
fn new(context: &mut Context) -> Self {
let layers = Layers::default();
let scene = Scene::new(layers.clone().sorted());
let mut screen = Self { scene, layers };
screen.demo_move(context);
screen.demo_show_hide(context);
screen
}

fn demo_move(&mut self, context: &mut Context) {
let mut sprite = gui::text_sprite(context, "move", 0.2);
sprite.set_pos(Point(vec2(0.0, -1.0)));
let delta = Point(vec2(0.0, 2.0));
let action = Box::new(action::Sequence::new(vec![
Box::new(action::Show::new(&self.layers.fg, &sprite)),
Box::new(action::MoveBy::new(&sprite, delta, Time(2.0))),
Box::new(action::Hide::new(&self.layers.fg, &sprite)),
]));
self.scene.add_action(action);
}

fn demo_show_hide(&mut self, context: &mut Context) {
let visible = [0.0, 0.0, 0.0, 1.0];
let invisible = [0.0, 0.0, 0.0, 0.0];
let mut sprite = gui::text_sprite(context, "abc", 0.3);
sprite.set_color(invisible);
let action = Box::new(action::Sequence::new(vec![
Box::new(action::Show::new(&self.layers.fg, &sprite)),
Box::new(action::ChangeColorTo::new(&sprite, visible, Time(0.3))),
Box::new(action::Sleep::new(Time(1.0))),
Box::new(action::ChangeColorTo::new(&sprite, invisible, Time(1.0))),
Box::new(action::Hide::new(&self.layers.fg, &sprite)),
]));
self.scene.add_action(action);
}
}

impl Screen for ActionsScreen {
fn tick(&mut self, context: &mut Context, dtime: Time) {
self.scene.tick(dtime);
self.scene.draw(context);
}

fn handle_event(&mut self, _: &mut Context, _: Event) {}
}

fn main() {
let settings = hate::Settings {
text_texture_height: 80.0,
tap_tolerance: 0.05,
font: None,
max_fps: 60.0,
};
let mut visualizer = hate::Visualizer::new(settings);
let screen = Box::new(ActionsScreen::new(visualizer.context_mut()));
visualizer.run(screen);
}
11 changes: 11 additions & 0 deletions hate/examples/basic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
extern crate hate;

pub fn main() {
let settings = hate::Settings {
text_texture_height: 80.0,
tap_tolerance: 0.05,
font: None,
max_fps: 60.0,
};
let _visualizer = hate::Visualizer::new(settings);
}
23 changes: 23 additions & 0 deletions hate/examples/empty_screen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
extern crate hate;

pub struct EmptyScreen;

use hate::{Context, Event, Screen, Time};

impl Screen for EmptyScreen {
fn tick(&mut self, _: &mut Context, _: Time) {}

fn handle_event(&mut self, _: &mut Context, _: Event) {}
}

pub fn main() {
let settings = hate::Settings {
text_texture_height: 80.0,
tap_tolerance: 0.05,
font: None,
max_fps: 60.0,
};
let mut visualizer = hate::Visualizer::new(settings);
let start_screen = Box::new(EmptyScreen);
visualizer.run(start_screen);
}
Binary file added hate/src/Karla-Regular.ttf
Binary file not shown.
35 changes: 30 additions & 5 deletions hate/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,35 @@ fn shader_version_string(api: Api) -> String {
}

fn vertex_shader(api: Api) -> String {
shader_version_string(api) + &fs::load_as_string("shader/v.glsl")
let shader = r#"
uniform mat4 u_ModelViewProj;
attribute vec2 a_Pos;
attribute vec2 a_Uv;
varying vec2 v_Uv;

void main() {
v_Uv = a_Uv;
gl_Position = u_ModelViewProj * vec4(a_Pos, 0.0, 1.0);
}
"#;
shader_version_string(api) + shader
}

fn fragment_shader(api: Api) -> String {
let mut text = shader_version_string(api);
if api == Api::OpenGlEs || api == Api::WebGl {
text += "precision mediump float;\n";
}
text + &fs::load_as_string("shader/f.glsl")
let shader = r#"
uniform vec4 u_Basic_color;
uniform sampler2D t_Tex;
varying vec2 v_Uv;

void main() {
gl_FragColor = u_Basic_color * texture2D(t_Tex, v_Uv);
}
"#;
text + shader
}

fn new_shader(
Expand All @@ -61,8 +81,8 @@ fn new_pso(
pso.unwrap()
}

fn new_font<P: AsRef<Path>>(path: P) -> rusttype::Font<'static> {
let collection = rusttype::FontCollection::from_bytes(fs::load(path));
fn new_font_from_vec(data: Vec<u8>) -> rusttype::Font<'static> {
let collection = rusttype::FontCollection::from_bytes(data);
collection.into_font().unwrap()
}

Expand Down Expand Up @@ -159,7 +179,12 @@ impl Context {
last_press_pos: Point(Vector2::zero()),
pos: Point(Vector2::zero()),
};
let font = new_font(&settings.font);
let font = if let Some(ref fontpath) = settings.font {
new_font_from_vec(fs::load(fontpath))
} else {
let data = include_bytes!("Karla-Regular.ttf");
new_font_from_vec(data.to_vec())
};
Context {
settings,
events_loop,
Expand Down
2 changes: 1 addition & 1 deletion hate/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ use std::path::PathBuf;
pub struct Settings {
pub tap_tolerance: f32,
pub text_texture_height: f32,
pub font: PathBuf,
pub font: Option<PathBuf>,
pub max_fps: f32,
}
Loading