annotate runners/src/bin/menu.rs @ 767:ccb04468c5fa

formats: Use Read::read_exact() and Write::write_all() Thanks clippy!
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 30 Aug 2022 17:07:21 +0200
parents 21b186be2590
children
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};
753
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 751
diff changeset
9 use luminance::texture::Dim2;
746
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};
757
21b186be2590 Split the Rust version into multiple crates.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 753
diff changeset
12 use touhou_formats::th06::pbg3;
21b186be2590 Split the Rust version into multiple crates.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 753
diff changeset
13 use touhou_formats::th06::anm0::Anm0;
21b186be2590 Split the Rust version into multiple crates.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 753
diff changeset
14 use touhou_interpreters::th06::anm0::{AnmRunner, Sprite, Vertex as FakeVertex};
21b186be2590 Split the Rust version into multiple crates.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 753
diff changeset
15 use touhou_utils::math::{perspective, setup_camera, ortho_2d};
21b186be2590 Split the Rust version into multiple crates.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 753
diff changeset
16 use touhou_utils::prng::Prng;
746
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
757
21b186be2590 Split the Rust version into multiple crates.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 753
diff changeset
22 use touhou_runners::common::{self, LoadedTexture};
746
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 const VS: &str = r#"
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 in ivec3 in_position;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 in vec2 in_texcoord;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 in vec4 in_color;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29 uniform mat4 mvp;
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 out vec2 texcoord;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 out vec4 color;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 void main()
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 gl_Position = mvp * vec4(vec3(in_position), 1.0);
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 texcoord = vec2(in_texcoord);
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 // It’s already normalized from the u8 being passed.
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 color = in_color;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 }
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 const FS: &str = r#"
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 in vec2 texcoord;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 in vec4 color;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 uniform sampler2D color_map;
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 out vec4 frag_color;
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 void main()
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 frag_color = texture(color_map, texcoord) * color;
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 "#;
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 #[derive(Clone, Copy, Debug, Eq, PartialEq, Semantics)]
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 pub enum Semantics {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60 #[sem(name = "in_position", repr = "[i16; 3]", wrapper = "VertexPosition")]
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61 Position,
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63 #[sem(name = "in_texcoord", repr = "[f32; 2]", wrapper = "VertexTexcoord")]
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64 Texcoord,
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66 #[sem(name = "in_color", repr = "[u8; 4]", wrapper = "VertexColor")]
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67 Color,
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68 }
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 #[repr(C)]
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 #[derive(Clone, Copy, Debug, PartialEq, Vertex)]
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72 #[vertex(sem = "Semantics")]
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73 struct Vertex {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74 pos: VertexPosition,
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75 uv: VertexTexcoord,
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76 #[vertex(normalized = "true")]
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77 rgba: VertexColor,
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 }
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 #[derive(UniformInterface)]
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81 struct ShaderInterface {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82 // the 'static lifetime acts as “anything” here
753
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 751
diff changeset
83 color_map: Uniform<&'static BoundTexture<'static, Dim2, NormUnsigned>>,
746
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
85 #[uniform(name = "mvp")]
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
86 mvp: Uniform<[[f32; 4]; 4]>,
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
87 }
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 const DEFAULT_VERTICES: [Vertex; 4] = [
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
90 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
91 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
92 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
93 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
94 ];
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 fn main() {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
97 // Parse arguments.
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
98 let args: Vec<_> = env::args().collect();
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
99 if args.len() != 2 {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
100 eprintln!("Usage: {} <unarchived directory>", args[0]);
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
101 return;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
102 }
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
103 let directory = Path::new(&args[1]);
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 in_dat = directory.join("IN.DAT");
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
106 // 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
107 // thread in the meantime.
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
108 let jpeg_thread = std::thread::spawn(|| {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
109 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
110 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
111 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
112 image
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
113 });
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
114
751
eab7dde1164f examples: Add music playback to the menu example
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 750
diff changeset
115 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
116 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
117 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
118 Ok(mut music) => {
eab7dde1164f examples: Add music playback to the menu example
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 750
diff changeset
119 music.set_looping(true);
eab7dde1164f examples: Add music playback to the menu example
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 750
diff changeset
120 music.play();
eab7dde1164f examples: Add music playback to the menu example
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 750
diff changeset
121 music
eab7dde1164f examples: Add music playback to the menu example
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 750
diff changeset
122 }
eab7dde1164f examples: Add music playback to the menu example
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 750
diff changeset
123 Err(err) => {
eab7dde1164f examples: Add music playback to the menu example
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 750
diff changeset
124 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
125 return;
eab7dde1164f examples: Add music playback to the menu example
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 750
diff changeset
126 }
eab7dde1164f examples: Add music playback to the menu example
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 750
diff changeset
127 };
eab7dde1164f examples: Add music playback to the menu example
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 750
diff changeset
128
746
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
129 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
130
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
131 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
132 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
133
8a022334a4c4 menu: Load the title image at frame 60.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
134 let mut background = match background {
8a022334a4c4 menu: Load the title image at frame 60.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
135 LoadedTexture::Rgb(tex) => tex,
8a022334a4c4 menu: Load the title image at frame 60.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
136 LoadedTexture::Rgba(tex) => unreachable!(),
753
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 751
diff changeset
137 LoadedTexture::RgbaArray(tex) => unreachable!(),
747
8a022334a4c4 menu: Load the title image at frame 60.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
138 };
746
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
139
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
140 // 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
141 let program =
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
142 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
143
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
144 let mut tess = TessBuilder::new(&mut surface)
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
145 .add_vertices(DEFAULT_VERTICES)
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
146 .set_mode(Mode::TriangleFan)
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
147 .build()
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
148 .unwrap();
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
149
750
31897f650d2d examples: Add a third screen to the menu
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 747
diff changeset
150 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
151 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
152
746
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
153 let mut back_buffer = surface.back_buffer().unwrap();
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
154 let mut resize = false;
747
8a022334a4c4 menu: Load the title image at frame 60.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
155 let mut frame = 0;
750
31897f650d2d examples: Add a third screen to the menu
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 747
diff changeset
156 let mut z_pressed = false;
31897f650d2d examples: Add a third screen to the menu
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 747
diff changeset
157 let mut x_pressed = false;
746
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
158
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
159 'app: loop {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
160 for event in surface.poll_events() {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
161 match event {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
162 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
163
750
31897f650d2d examples: Add a third screen to the menu
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 747
diff changeset
164 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
165 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
166
746
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
167 WindowEvent::FramebufferSize(..) => {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
168 resize = true;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
169 }
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 if resize {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
176 back_buffer = surface.back_buffer().unwrap();
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
177 resize = false;
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
747
8a022334a4c4 menu: Load the title image at frame 60.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
180 frame += 1;
8a022334a4c4 menu: Load the title image at frame 60.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
181 if frame == 60 {
750
31897f650d2d examples: Add a third screen to the menu
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 747
diff changeset
182 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
183 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
184 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
185 }
8a022334a4c4 menu: Load the title image at frame 60.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
186
750
31897f650d2d examples: Add a third screen to the menu
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 747
diff changeset
187 if frame >= 60 && z_pressed {
31897f650d2d examples: Add a third screen to the menu
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 747
diff changeset
188 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
189 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
190 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
191 }
31897f650d2d examples: Add a third screen to the menu
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 747
diff changeset
192
746
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
193 // 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
194 // and use it in the shader
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
195 surface
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
196 .pipeline_builder()
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
197 .pipeline(&back_buffer, &PipelineState::default(), |pipeline, mut shd_gate| {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
198 // 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
199 let tex = pipeline.bind_texture(&background);
746
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
200
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
201 shd_gate.shade(&program, |iface, mut rdr_gate| {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
202 // 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
203 // 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
204 iface.color_map.update(&tex);
746
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
205 let mvp = ortho_2d(0., 640., 480., 0.);
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
206 // TODO: check how to pass by reference.
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
207 iface.mvp.update(*mvp.borrow_inner());
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
208
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
209 let render_state = RenderState::default()
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
210 .set_blending((Equation::Additive, Factor::SrcAlpha, Factor::SrcAlphaComplement));
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
211
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
212 rdr_gate.render(&render_state, |mut tess_gate| {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
213 tess_gate.render(&tess);
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
214 });
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 surface.swap_buffers();
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
219 }
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
220 }