-
Notifications
You must be signed in to change notification settings - Fork 16
/
helloworld.rs
106 lines (92 loc) · 3.19 KB
/
helloworld.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
96
97
98
99
100
101
102
103
104
105
106
#![deny(clippy::all)]
#![allow(clippy::unreadable_literal)]
#![allow(clippy::single_match)]
use rgx::core::*;
use rgx::kit::sprite2d;
use rgx::kit::{self, ZDepth};
use winit::{
event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::Window,
};
fn main() -> Result<(), std::io::Error> {
let event_loop = EventLoop::new();
let window = Window::new(&event_loop).unwrap();
let size = window.inner_size();
// Setup renderer
let mut renderer = Renderer::new(&window)?;
// Setup render pipeline
let pipeline: kit::sprite2d::Pipeline = renderer.pipeline(Blending::default());
// Setup texture & sampler
#[rustfmt::skip]
let texels: [u8; 16] = [
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
];
let buf = Rgba8::align(&texels);
// Create 4 by 4 texture and sampler.
let texture = renderer.texture(2, 2);
let sampler = renderer.sampler(Filter::Nearest, Filter::Nearest);
// Setup sprite
let binding = pipeline.binding(&renderer, &texture, &sampler);
let buffer = sprite2d::Batch::singleton(
texture.w,
texture.h,
texture.rect(),
Rect::new(0., 0., size.width as f32, size.height as f32),
ZDepth::ZERO,
Rgba::new(0.5, 0.6, 0.8, 1.0),
1.0,
kit::Repeat::new(24. * (size.width / size.height) as f32, 24.),
)
.finish(&renderer);
let mut textures = renderer.swap_chain(
size.width as u32,
size.height as u32,
PresentMode::default(),
);
// Prepare resources
renderer.submit(&[Op::Fill(&texture, buf)]);
event_loop.run(move |event, _, control_flow| match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => {
*control_flow = ControlFlow::Exit;
}
WindowEvent::Resized(size) => {
let (w, h) = (size.width as u32, size.height as u32);
textures = renderer.swap_chain(w, h, PresentMode::default());
*control_flow = ControlFlow::Poll;
}
WindowEvent::KeyboardInput {
input:
KeyboardInput {
virtual_keycode: Some(code),
state: ElementState::Pressed,
..
},
..
} => {
if let VirtualKeyCode::Escape = code {
*control_flow = ControlFlow::Exit;
}
}
_ => {}
},
Event::MainEventsCleared => {
let output = textures.next();
let mut frame = renderer.frame();
renderer.update_pipeline(
&pipeline,
kit::ortho(output.width, output.height, Default::default()),
&mut frame,
);
{
let mut pass = frame.pass(PassOp::Clear(Rgba::TRANSPARENT), &output);
pass.set_pipeline(&pipeline);
pass.draw(&buffer, &binding);
}
renderer.present(frame);
}
_ => {}
});
}