annotate examples/menu.rs @ 751:eab7dde1164f

examples: Add music playback to the menu example
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 25 Feb 2020 19:37:19 +0100
parents 31897f650d2d
children a662dddd4a2b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
751
eab7dde1164f examples: Add music playback to the menu example
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 750
diff changeset
1 use ears::{Music, AudioController};
746
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 use luminance::blending::{Equation, Factor};
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 use luminance::context::GraphicsContext;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 use luminance::pipeline::{BoundTexture, PipelineState};
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 use luminance::pixel::NormUnsigned;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 use luminance::render_state::RenderState;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 use luminance::shader::program::{Program, Uniform};
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 use luminance::tess::{Mode, TessBuilder};
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 use luminance::texture::{Dim2, Flat};
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 use luminance_derive::{Semantics, Vertex, UniformInterface};
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 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
12 use touhou::th06::pbg3;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 use touhou::th06::anm0::Anm0;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14 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
15 use touhou::util::math::{perspective, setup_camera, ortho_2d};
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16 use touhou::util::prng::Prng;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 use std::cell::RefCell;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18 use std::rc::Rc;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 use std::env;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20 use std::path::Path;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22 #[path = "common.rs"]
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 mod common;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 use common::LoadedTexture;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 const VS: &str = r#"
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 in ivec3 in_position;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 in vec2 in_texcoord;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29 in vec4 in_color;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 uniform mat4 mvp;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 out vec2 texcoord;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 out vec4 color;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 void main()
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 gl_Position = mvp * vec4(vec3(in_position), 1.0);
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 texcoord = vec2(in_texcoord);
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 // It’s already normalized from the u8 being passed.
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 color = in_color;
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
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 const FS: &str = r#"
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 in vec2 texcoord;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 in vec4 color;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50 uniform sampler2D color_map;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 out vec4 frag_color;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 void main()
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56 frag_color = texture(color_map, texcoord) * color;
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
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60 #[derive(Clone, Copy, Debug, Eq, PartialEq, Semantics)]
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61 pub enum Semantics {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 #[sem(name = "in_position", repr = "[i16; 3]", wrapper = "VertexPosition")]
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63 Position,
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 #[sem(name = "in_texcoord", repr = "[f32; 2]", wrapper = "VertexTexcoord")]
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66 Texcoord,
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68 #[sem(name = "in_color", repr = "[u8; 4]", wrapper = "VertexColor")]
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69 Color,
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
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72 #[repr(C)]
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73 #[derive(Clone, Copy, Debug, PartialEq, Vertex)]
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74 #[vertex(sem = "Semantics")]
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75 struct Vertex {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76 pos: VertexPosition,
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77 uv: VertexTexcoord,
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 #[vertex(normalized = "true")]
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79 rgba: VertexColor,
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
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82 #[derive(UniformInterface)]
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
83 struct ShaderInterface {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84 // the 'static lifetime acts as “anything” here
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
85 color_map: Uniform<&'static BoundTexture<'static, Flat, Dim2, NormUnsigned>>,
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
86
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
87 #[uniform(name = "mvp")]
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
88 mvp: Uniform<[[f32; 4]; 4]>,
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
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91 const DEFAULT_VERTICES: [Vertex; 4] = [
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
92 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
93 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
94 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
95 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
96 ];
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
97
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
98 fn main() {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
99 // Parse arguments.
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
100 let args: Vec<_> = env::args().collect();
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
101 if args.len() != 2 {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
102 eprintln!("Usage: {} <unarchived directory>", args[0]);
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
103 return;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
104 }
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
105 let directory = Path::new(&args[1]);
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
106
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
107 let in_dat = directory.join("IN.DAT");
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
108 // 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
109 // thread in the meantime.
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
110 let jpeg_thread = std::thread::spawn(|| {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
111 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
112 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
113 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
114 image
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
751
eab7dde1164f examples: Add music playback to the menu example
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 750
diff changeset
117 let music_filename = directory.join("bgm").join("th06_01.wav");
eab7dde1164f examples: Add music playback to the menu example
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 750
diff changeset
118 let music_filename = music_filename.to_str().expect("non-UTF-8 music filename");
eab7dde1164f examples: Add music playback to the menu example
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 750
diff changeset
119 let music = match Music::new(music_filename) {
eab7dde1164f examples: Add music playback to the menu example
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 750
diff changeset
120 Ok(mut music) => {
eab7dde1164f examples: Add music playback to the menu example
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 750
diff changeset
121 music.set_looping(true);
eab7dde1164f examples: Add music playback to the menu example
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 750
diff changeset
122 music.play();
eab7dde1164f examples: Add music playback to the menu example
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 750
diff changeset
123 music
eab7dde1164f examples: Add music playback to the menu example
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 750
diff changeset
124 }
eab7dde1164f examples: Add music playback to the menu example
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 750
diff changeset
125 Err(err) => {
eab7dde1164f examples: Add music playback to the menu example
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 750
diff changeset
126 eprintln!("Impossible to open or play music file: {}", err);
eab7dde1164f examples: Add music playback to the menu example
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 750
diff changeset
127 return;
eab7dde1164f examples: Add music playback to the menu example
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 750
diff changeset
128 }
eab7dde1164f examples: Add music playback to the menu example
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 750
diff changeset
129 };
eab7dde1164f examples: Add music playback to the menu example
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 750
diff changeset
130
746
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
131 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
132
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
133 let image = jpeg_thread.join().expect("image loading");
747
8a022334a4c4 menu: Load the title image at frame 60.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
134 let background = common::upload_texture_from_rgb_image(&mut surface, image).expect("upload data to texture");
8a022334a4c4 menu: Load the title image at frame 60.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
135
8a022334a4c4 menu: Load the title image at frame 60.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
136 let mut background = match background {
8a022334a4c4 menu: Load the title image at frame 60.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
137 LoadedTexture::Rgb(tex) => tex,
8a022334a4c4 menu: Load the title image at frame 60.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
138 LoadedTexture::Rgba(tex) => unreachable!(),
8a022334a4c4 menu: Load the title image at frame 60.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
139 };
746
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
140
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
141 // 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
142 let program =
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
143 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
144
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
145 let mut tess = TessBuilder::new(&mut surface)
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
146 .add_vertices(DEFAULT_VERTICES)
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
147 .set_mode(Mode::TriangleFan)
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
148 .build()
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
149 .unwrap();
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
150
750
31897f650d2d examples: Add a third screen to the menu
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 747
diff changeset
151 let tl_dat = directory.join("TL.DAT");
31897f650d2d examples: Add a third screen to the menu
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 747
diff changeset
152 let mut tl_pbg3 = pbg3::from_path_buffered(tl_dat).expect("TL.DAT present");
31897f650d2d examples: Add a third screen to the menu
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 747
diff changeset
153
746
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
154 let mut back_buffer = surface.back_buffer().unwrap();
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
155 let mut resize = false;
747
8a022334a4c4 menu: Load the title image at frame 60.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
156 let mut frame = 0;
750
31897f650d2d examples: Add a third screen to the menu
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 747
diff changeset
157 let mut z_pressed = false;
31897f650d2d examples: Add a third screen to the menu
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 747
diff changeset
158 let mut x_pressed = false;
746
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
159
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
160 'app: loop {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
161 for event in surface.poll_events() {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
162 match event {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
163 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
164
750
31897f650d2d examples: Add a third screen to the menu
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 747
diff changeset
165 WindowEvent::Key(Key::Z, _, Action::Press, _) => z_pressed = true,
31897f650d2d examples: Add a third screen to the menu
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 747
diff changeset
166 WindowEvent::Key(Key::X, _, Action::Press, _) => x_pressed = true,
31897f650d2d examples: Add a third screen to the menu
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 747
diff changeset
167
746
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
168 WindowEvent::FramebufferSize(..) => {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
169 resize = true;
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
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
172 _ => (),
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 }
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
175
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
176 if resize {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
177 back_buffer = surface.back_buffer().unwrap();
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
178 resize = false;
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
747
8a022334a4c4 menu: Load the title image at frame 60.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
181 frame += 1;
8a022334a4c4 menu: Load the title image at frame 60.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
182 if frame == 60 {
750
31897f650d2d examples: Add a third screen to the menu
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 747
diff changeset
183 let jpeg = tl_pbg3.get_file("title00.jpg", true).expect("title00.jpg in TL.DAT");
747
8a022334a4c4 menu: Load the title image at frame 60.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
184 let image = common::load_from_data(&jpeg).expect("th06logo.jpg decodable");
8a022334a4c4 menu: Load the title image at frame 60.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
185 common::reupload_texture_from_rgb_image(&mut background, image).expect("upload data to texture");
8a022334a4c4 menu: Load the title image at frame 60.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
186 }
8a022334a4c4 menu: Load the title image at frame 60.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
187
750
31897f650d2d examples: Add a third screen to the menu
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 747
diff changeset
188 if frame >= 60 && z_pressed {
31897f650d2d examples: Add a third screen to the menu
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 747
diff changeset
189 let jpeg = tl_pbg3.get_file("select00.jpg", true).expect("select00.jpg in TL.DAT");
31897f650d2d examples: Add a third screen to the menu
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 747
diff changeset
190 let image = common::load_from_data(&jpeg).expect("select00.jpg decodable");
31897f650d2d examples: Add a third screen to the menu
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 747
diff changeset
191 common::reupload_texture_from_rgb_image(&mut background, image).expect("upload data to texture");
31897f650d2d examples: Add a third screen to the menu
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 747
diff changeset
192 }
31897f650d2d examples: Add a third screen to the menu
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 747
diff changeset
193
746
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
194 // 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
195 // and use it in the shader
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
196 surface
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
197 .pipeline_builder()
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
198 .pipeline(&back_buffer, &PipelineState::default(), |pipeline, mut shd_gate| {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
199 // bind our fancy texture to the GPU: it gives us a bound texture we can use with the shader
747
8a022334a4c4 menu: Load the title image at frame 60.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
200 let tex = pipeline.bind_texture(&background);
746
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
201
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
202 shd_gate.shade(&program, |iface, mut rdr_gate| {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
203 // 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
204 // to use the texture passed as argument (no allocation or copy is performed)
747
8a022334a4c4 menu: Load the title image at frame 60.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
205 iface.color_map.update(&tex);
746
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
206 let mvp = ortho_2d(0., 640., 480., 0.);
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
207 // TODO: check how to pass by reference.
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
208 iface.mvp.update(*mvp.borrow_inner());
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
209
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
210 let render_state = RenderState::default()
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
211 .set_blending((Equation::Additive, Factor::SrcAlpha, Factor::SrcAlphaComplement));
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
212
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
213 rdr_gate.render(&render_state, |mut tess_gate| {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
214 tess_gate.render(&tess);
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
215 });
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
216 });
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
217 });
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
218
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
219 surface.swap_buffers();
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
220 }
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
221 }