annotate examples/menu.rs @ 746:0ebf6467e4ff

examples: Add a menu example.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 18 Jan 2020 19:19:51 +0100
parents
children 8a022334a4c4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
746
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 use luminance::blending::{Equation, Factor};
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 use luminance::context::GraphicsContext;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 use luminance::pipeline::{BoundTexture, PipelineState};
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 use luminance::pixel::NormUnsigned;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 use luminance::render_state::RenderState;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 use luminance::shader::program::{Program, Uniform};
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 use luminance::tess::{Mode, TessBuilder};
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 use luminance::texture::{Dim2, Flat};
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 use luminance_derive::{Semantics, Vertex, UniformInterface};
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 use luminance_glfw::{Action, Key, WindowEvent, GlfwSurface, Surface, WindowDim, WindowOpt};
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 use touhou::th06::pbg3;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 use touhou::th06::anm0::Anm0;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 use touhou::th06::anm0_vm::{AnmRunner, Sprite, Vertex as FakeVertex};
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14 use touhou::util::math::{perspective, setup_camera, ortho_2d};
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15 use touhou::util::prng::Prng;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16 use std::cell::RefCell;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 use std::rc::Rc;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18 use std::env;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 use std::path::Path;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21 #[path = "common.rs"]
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22 mod common;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 use common::LoadedTexture;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 const VS: &str = r#"
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 in ivec3 in_position;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 in vec2 in_texcoord;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 in vec4 in_color;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 uniform mat4 mvp;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 out vec2 texcoord;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 out vec4 color;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 void main()
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 gl_Position = mvp * vec4(vec3(in_position), 1.0);
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 texcoord = vec2(in_texcoord);
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 // It’s already normalized from the u8 being passed.
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 color = in_color;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 }
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 "#;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 const FS: &str = r#"
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 in vec2 texcoord;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 in vec4 color;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 uniform sampler2D color_map;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 out vec4 frag_color;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 void main()
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 frag_color = texture(color_map, texcoord) * color;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56 }
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 "#;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 #[derive(Clone, Copy, Debug, Eq, PartialEq, Semantics)]
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60 pub enum Semantics {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61 #[sem(name = "in_position", repr = "[i16; 3]", wrapper = "VertexPosition")]
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 Position,
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64 #[sem(name = "in_texcoord", repr = "[f32; 2]", wrapper = "VertexTexcoord")]
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 Texcoord,
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67 #[sem(name = "in_color", repr = "[u8; 4]", wrapper = "VertexColor")]
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68 Color,
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69 }
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
70
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 #[repr(C)]
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72 #[derive(Clone, Copy, Debug, PartialEq, Vertex)]
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73 #[vertex(sem = "Semantics")]
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74 struct Vertex {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75 pos: VertexPosition,
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76 uv: VertexTexcoord,
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77 #[vertex(normalized = "true")]
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 rgba: VertexColor,
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79 }
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81 #[derive(UniformInterface)]
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82 struct ShaderInterface {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
83 // the 'static lifetime acts as “anything” here
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84 color_map: Uniform<&'static BoundTexture<'static, Flat, Dim2, NormUnsigned>>,
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
85
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
86 #[uniform(name = "mvp")]
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
87 mvp: Uniform<[[f32; 4]; 4]>,
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
88 }
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
89
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
90 const DEFAULT_VERTICES: [Vertex; 4] = [
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91 Vertex::new(VertexPosition::new([0, 0, 0]), VertexTexcoord::new([0., 0.]), VertexColor::new([255, 255, 255, 255])),
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
92 Vertex::new(VertexPosition::new([640, 0, 0]), VertexTexcoord::new([1., 0.]), VertexColor::new([255, 255, 255, 255])),
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
93 Vertex::new(VertexPosition::new([640, 480, 0]), VertexTexcoord::new([1., 1.]), VertexColor::new([255, 255, 255, 255])),
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
94 Vertex::new(VertexPosition::new([0, 480, 0]), VertexTexcoord::new([0., 1.]), VertexColor::new([255, 255, 255, 255])),
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
95 ];
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
96
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
97 fn main() {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
98 // Parse arguments.
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
99 let args: Vec<_> = env::args().collect();
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
100 if args.len() != 2 {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
101 eprintln!("Usage: {} <unarchived directory>", args[0]);
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
102 return;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
103 }
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
104 let directory = Path::new(&args[1]);
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
105
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
106 let in_dat = directory.join("IN.DAT");
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
107 // Since GLFW can be slow to create its window, let’s decode the splash screen in another
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
108 // thread in the meantime.
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
109 let jpeg_thread = std::thread::spawn(|| {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
110 let mut in_pbg3 = pbg3::from_path_buffered(in_dat).expect("IN.DAT present");
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
111 let jpeg = in_pbg3.get_file("th06logo.jpg", true).expect("th06logo.jpg in IN.DAT");
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
112 let image = common::load_from_data(&jpeg).expect("th06logo.jpg decodable");
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
113 image
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
114 });
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
115
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
116 let mut surface = GlfwSurface::new(WindowDim::Windowed(640, 480), "Touhou", WindowOpt::default()).expect("GLFW window");
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
117
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
118 let image = jpeg_thread.join().expect("image loading");
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
119 let tex = common::upload_texture_from_rgb_image(&mut surface, image).expect("image loading");
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
120
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
121 // set the uniform interface to our type so that we can read textures from the shader
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
122 let program =
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
123 Program::<Semantics, (), ShaderInterface>::from_strings(None, VS, None, FS).expect("program creation").ignore_warnings();
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
124
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
125 let mut tess = TessBuilder::new(&mut surface)
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
126 .add_vertices(DEFAULT_VERTICES)
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
127 .set_mode(Mode::TriangleFan)
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
128 .build()
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
129 .unwrap();
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
130
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
131 let mut back_buffer = surface.back_buffer().unwrap();
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
132 let mut resize = false;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
133
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
134 'app: loop {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
135 for event in surface.poll_events() {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
136 match event {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
137 WindowEvent::Close | WindowEvent::Key(Key::Escape, _, Action::Release, _) => break 'app,
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
138
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
139 WindowEvent::FramebufferSize(..) => {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
140 resize = true;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
141 }
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
142
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
143 _ => (),
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
144 }
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
145 }
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
146
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
147 if resize {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
148 back_buffer = surface.back_buffer().unwrap();
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
149 resize = false;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
150 }
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
151
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
152 // here, we need to bind the pipeline variable; it will enable us to bind the texture to the GPU
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
153 // and use it in the shader
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
154 surface
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
155 .pipeline_builder()
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
156 .pipeline(&back_buffer, &PipelineState::default(), |pipeline, mut shd_gate| {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
157 // bind our fancy texture to the GPU: it gives us a bound texture we can use with the shader
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
158 let bound_tex = match &tex {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
159 LoadedTexture::Rgb(tex) => pipeline.bind_texture(tex),
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
160 LoadedTexture::Rgba(tex) => pipeline.bind_texture(tex),
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
161 };
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
162
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
163 shd_gate.shade(&program, |iface, mut rdr_gate| {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
164 // update the texture; strictly speaking, this update doesn’t do much: it just tells the GPU
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
165 // to use the texture passed as argument (no allocation or copy is performed)
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
166 iface.color_map.update(&bound_tex);
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
167 let mvp = ortho_2d(0., 640., 480., 0.);
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
168 // TODO: check how to pass by reference.
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
169 iface.mvp.update(*mvp.borrow_inner());
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
170
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
171 let render_state = RenderState::default()
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
172 .set_blending((Equation::Additive, Factor::SrcAlpha, Factor::SrcAlphaComplement));
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
173
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
174 rdr_gate.render(&render_state, |mut tess_gate| {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
175 tess_gate.render(&tess);
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
176 });
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
177 });
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
178 });
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
179
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
180 surface.swap_buffers();
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
181 }
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
182 }