-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello_world.rs
95 lines (83 loc) · 3.31 KB
/
hello_world.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use dashing::input::*;
use dashing::*;
use pretty_env_logger;
use time;
// Screen dimensions in characters.
const WIDTH: u32 = 21;
const HEIGHT: u32 = 3;
// Client code must implement a "driver", which is a combination of input handler and
// interface to the renderer.
struct ExampleDriver {
// SpriteLayer is a convenient abstraction for representing an orthogonal region of the
// screen.
root_layer: graphics::drawing::SpriteLayer,
message: String,
}
impl Driver for ExampleDriver {
// The driver handles all the user input events the window receives. The `handle_input`
// method needs to be lightweight, because it blocks the render thread.
fn handle_input(&mut self, e: Event) -> EngineSignal {
match e {
Event::Window(WindowEvent::CloseRequested) | Event::Window(WindowEvent::Destroyed) => {
EngineSignal::Halt
}
_ => EngineSignal::Continue,
}
}
// After the engine processes all pending events, `process_frame` is called. This is where
// you would update the screen, as well as run any per-frame processing your game
// requires.
fn process_frame<R>(&mut self, renderer: &mut R) -> EngineSignal
where
R: dashing::graphics::render::RenderInterface,
{
// Clear the sprite layer.
self.root_layer.clear();
// Print the message to the screen.
for (i, c) in self.message.chars().enumerate() {
self.root_layer[WIDTH as usize + 1 + i] = graphics::drawing::SpriteCell {
palette: resources::color::Palette::mono([0, 0, 0]).set(
1,
resources::color::Color::from_hsv(
time::OffsetDateTime::now_utc().millisecond() as f32 * 360.0 / 1000.0,
1.0 - (i as f32 / self.message.len() as f32) * 0.5,
1.0,
),
),
sprite: c as u32,
transparent: false,
};
}
// Update the renderer.
renderer.update(self.root_layer.iter());
EngineSignal::Continue
}
}
pub fn main() {
pretty_env_logger::init();
let tex_png = include_bytes!("../src/graphics/render/testdata/12x12.png");
let mut decoder = png::Decoder::new(&tex_png[..]);
decoder.set_transformations(png::Transformations::IDENTITY);
let mut reader = decoder.read_info().unwrap();
assert!(reader.info().color_type == png::ColorType::Indexed);
let mut imgdata = vec![0u8; reader.output_buffer_size()];
reader.next_frame(&mut imgdata[..]).unwrap();
let tex = dashing::resources::sprite::SpriteTexture::new_from_pixels(
&imgdata[..],
reader.info().size().0 as usize,
reader.info().size().1 as usize,
reader.info().size().0 as usize / 16,
reader.info().size().1 as usize / 16,
256,
)
.unwrap();
let window_builder = window::WindowBuilder::new("hello world", WIDTH, HEIGHT, &tex)
.with_clear_color((0.2, 0.2, 0.2).into())
.with_vsync(true);
let driver = ExampleDriver {
root_layer: graphics::drawing::SpriteLayer::new(WIDTH as usize, HEIGHT as usize),
message: String::from("Swash your buckles!"),
};
let engine = dashing::Engine::new(window_builder, driver).unwrap();
engine.run();
}