comparison runners/src/bin/menu.rs @ 757:21b186be2590

Split the Rust version into multiple crates.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 05 Jan 2021 02:16:32 +0100
parents examples/menu.rs@a662dddd4a2b
children
comparison
equal deleted inserted replaced
756:4d91790cf8ab 757:21b186be2590
1 use ears::{Music, AudioController};
2 use luminance::blending::{Equation, Factor};
3 use luminance::context::GraphicsContext;
4 use luminance::pipeline::{BoundTexture, PipelineState};
5 use luminance::pixel::NormUnsigned;
6 use luminance::render_state::RenderState;
7 use luminance::shader::program::{Program, Uniform};
8 use luminance::tess::{Mode, TessBuilder};
9 use luminance::texture::Dim2;
10 use luminance_derive::{Semantics, Vertex, UniformInterface};
11 use luminance_glfw::{Action, Key, WindowEvent, GlfwSurface, Surface, WindowDim, WindowOpt};
12 use touhou_formats::th06::pbg3;
13 use touhou_formats::th06::anm0::Anm0;
14 use touhou_interpreters::th06::anm0::{AnmRunner, Sprite, Vertex as FakeVertex};
15 use touhou_utils::math::{perspective, setup_camera, ortho_2d};
16 use touhou_utils::prng::Prng;
17 use std::cell::RefCell;
18 use std::rc::Rc;
19 use std::env;
20 use std::path::Path;
21
22 use touhou_runners::common::{self, LoadedTexture};
23
24 const VS: &str = r#"
25 in ivec3 in_position;
26 in vec2 in_texcoord;
27 in vec4 in_color;
28
29 uniform mat4 mvp;
30
31 out vec2 texcoord;
32 out vec4 color;
33
34 void main()
35 {
36 gl_Position = mvp * vec4(vec3(in_position), 1.0);
37 texcoord = vec2(in_texcoord);
38
39 // It’s already normalized from the u8 being passed.
40 color = in_color;
41 }
42 "#;
43
44 const FS: &str = r#"
45 in vec2 texcoord;
46 in vec4 color;
47
48 uniform sampler2D color_map;
49
50 out vec4 frag_color;
51
52 void main()
53 {
54 frag_color = texture(color_map, texcoord) * color;
55 }
56 "#;
57
58 #[derive(Clone, Copy, Debug, Eq, PartialEq, Semantics)]
59 pub enum Semantics {
60 #[sem(name = "in_position", repr = "[i16; 3]", wrapper = "VertexPosition")]
61 Position,
62
63 #[sem(name = "in_texcoord", repr = "[f32; 2]", wrapper = "VertexTexcoord")]
64 Texcoord,
65
66 #[sem(name = "in_color", repr = "[u8; 4]", wrapper = "VertexColor")]
67 Color,
68 }
69
70 #[repr(C)]
71 #[derive(Clone, Copy, Debug, PartialEq, Vertex)]
72 #[vertex(sem = "Semantics")]
73 struct Vertex {
74 pos: VertexPosition,
75 uv: VertexTexcoord,
76 #[vertex(normalized = "true")]
77 rgba: VertexColor,
78 }
79
80 #[derive(UniformInterface)]
81 struct ShaderInterface {
82 // the 'static lifetime acts as “anything” here
83 color_map: Uniform<&'static BoundTexture<'static, Dim2, NormUnsigned>>,
84
85 #[uniform(name = "mvp")]
86 mvp: Uniform<[[f32; 4]; 4]>,
87 }
88
89 const DEFAULT_VERTICES: [Vertex; 4] = [
90 Vertex::new(VertexPosition::new([0, 0, 0]), VertexTexcoord::new([0., 0.]), VertexColor::new([255, 255, 255, 255])),
91 Vertex::new(VertexPosition::new([640, 0, 0]), VertexTexcoord::new([1., 0.]), VertexColor::new([255, 255, 255, 255])),
92 Vertex::new(VertexPosition::new([640, 480, 0]), VertexTexcoord::new([1., 1.]), VertexColor::new([255, 255, 255, 255])),
93 Vertex::new(VertexPosition::new([0, 480, 0]), VertexTexcoord::new([0., 1.]), VertexColor::new([255, 255, 255, 255])),
94 ];
95
96 fn main() {
97 // Parse arguments.
98 let args: Vec<_> = env::args().collect();
99 if args.len() != 2 {
100 eprintln!("Usage: {} <unarchived directory>", args[0]);
101 return;
102 }
103 let directory = Path::new(&args[1]);
104
105 let in_dat = directory.join("IN.DAT");
106 // Since GLFW can be slow to create its window, let’s decode the splash screen in another
107 // thread in the meantime.
108 let jpeg_thread = std::thread::spawn(|| {
109 let mut in_pbg3 = pbg3::from_path_buffered(in_dat).expect("IN.DAT present");
110 let jpeg = in_pbg3.get_file("th06logo.jpg", true).expect("th06logo.jpg in IN.DAT");
111 let image = common::load_from_data(&jpeg).expect("th06logo.jpg decodable");
112 image
113 });
114
115 let music_filename = directory.join("bgm").join("th06_01.wav");
116 let music_filename = music_filename.to_str().expect("non-UTF-8 music filename");
117 let music = match Music::new(music_filename) {
118 Ok(mut music) => {
119 music.set_looping(true);
120 music.play();
121 music
122 }
123 Err(err) => {
124 eprintln!("Impossible to open or play music file: {}", err);
125 return;
126 }
127 };
128
129 let mut surface = GlfwSurface::new(WindowDim::Windowed(640, 480), "Touhou", WindowOpt::default()).expect("GLFW window");
130
131 let image = jpeg_thread.join().expect("image loading");
132 let background = common::upload_texture_from_rgb_image(&mut surface, image).expect("upload data to texture");
133
134 let mut background = match background {
135 LoadedTexture::Rgb(tex) => tex,
136 LoadedTexture::Rgba(tex) => unreachable!(),
137 LoadedTexture::RgbaArray(tex) => unreachable!(),
138 };
139
140 // set the uniform interface to our type so that we can read textures from the shader
141 let program =
142 Program::<Semantics, (), ShaderInterface>::from_strings(None, VS, None, FS).expect("program creation").ignore_warnings();
143
144 let mut tess = TessBuilder::new(&mut surface)
145 .add_vertices(DEFAULT_VERTICES)
146 .set_mode(Mode::TriangleFan)
147 .build()
148 .unwrap();
149
150 let tl_dat = directory.join("TL.DAT");
151 let mut tl_pbg3 = pbg3::from_path_buffered(tl_dat).expect("TL.DAT present");
152
153 let mut back_buffer = surface.back_buffer().unwrap();
154 let mut resize = false;
155 let mut frame = 0;
156 let mut z_pressed = false;
157 let mut x_pressed = false;
158
159 'app: loop {
160 for event in surface.poll_events() {
161 match event {
162 WindowEvent::Close | WindowEvent::Key(Key::Escape, _, Action::Release, _) => break 'app,
163
164 WindowEvent::Key(Key::Z, _, Action::Press, _) => z_pressed = true,
165 WindowEvent::Key(Key::X, _, Action::Press, _) => x_pressed = true,
166
167 WindowEvent::FramebufferSize(..) => {
168 resize = true;
169 }
170
171 _ => (),
172 }
173 }
174
175 if resize {
176 back_buffer = surface.back_buffer().unwrap();
177 resize = false;
178 }
179
180 frame += 1;
181 if frame == 60 {
182 let jpeg = tl_pbg3.get_file("title00.jpg", true).expect("title00.jpg in TL.DAT");
183 let image = common::load_from_data(&jpeg).expect("th06logo.jpg decodable");
184 common::reupload_texture_from_rgb_image(&mut background, image).expect("upload data to texture");
185 }
186
187 if frame >= 60 && z_pressed {
188 let jpeg = tl_pbg3.get_file("select00.jpg", true).expect("select00.jpg in TL.DAT");
189 let image = common::load_from_data(&jpeg).expect("select00.jpg decodable");
190 common::reupload_texture_from_rgb_image(&mut background, image).expect("upload data to texture");
191 }
192
193 // here, we need to bind the pipeline variable; it will enable us to bind the texture to the GPU
194 // and use it in the shader
195 surface
196 .pipeline_builder()
197 .pipeline(&back_buffer, &PipelineState::default(), |pipeline, mut shd_gate| {
198 // bind our fancy texture to the GPU: it gives us a bound texture we can use with the shader
199 let tex = pipeline.bind_texture(&background);
200
201 shd_gate.shade(&program, |iface, mut rdr_gate| {
202 // update the texture; strictly speaking, this update doesn’t do much: it just tells the GPU
203 // to use the texture passed as argument (no allocation or copy is performed)
204 iface.color_map.update(&tex);
205 let mvp = ortho_2d(0., 640., 480., 0.);
206 // TODO: check how to pass by reference.
207 iface.mvp.update(*mvp.borrow_inner());
208
209 let render_state = RenderState::default()
210 .set_blending((Equation::Additive, Factor::SrcAlpha, Factor::SrcAlphaComplement));
211
212 rdr_gate.render(&render_state, |mut tess_gate| {
213 tess_gate.render(&tess);
214 });
215 });
216 });
217
218 surface.swap_buffers();
219 }
220 }