Mercurial > touhou
comparison examples/stagerunner.rs @ 738:817c453b7223
stagerunner: Add a binary able to run multiple enemies.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sat, 28 Dec 2019 23:53:09 +0100 |
parents | |
children | 3555845f8cf4 |
comparison
equal
deleted
inserted
replaced
737:0977d479e37d | 738:817c453b7223 |
---|---|
1 use luminance::blending::{Equation, Factor}; | |
2 use luminance::context::GraphicsContext; | |
3 use luminance::pipeline::BoundTexture; | |
4 use luminance::pixel::NormUnsigned; | |
5 use luminance::render_state::RenderState; | |
6 use luminance::shader::program::{Program, Uniform}; | |
7 use luminance::tess::{Mode, TessBuilder}; | |
8 use luminance::texture::{Dim2, Flat}; | |
9 use luminance_derive::{Semantics, Vertex, UniformInterface}; | |
10 use luminance_glfw::{Action, Key, WindowEvent, GlfwSurface, Surface, WindowDim, WindowOpt}; | |
11 use touhou::th06::anm0::Anm0; | |
12 use touhou::th06::anm0_vm::Vertex as FakeVertex; | |
13 use touhou::th06::ecl::{Ecl, Rank, MainInstruction}; | |
14 use touhou::th06::ecl_vm::EclRunner; | |
15 use touhou::th06::enemy::{Enemy, Game, Position}; | |
16 use touhou::util::math::{perspective, setup_camera}; | |
17 use touhou::util::prng::Prng; | |
18 use std::cell::RefCell; | |
19 use std::rc::Rc; | |
20 use std::env; | |
21 use std::path::Path; | |
22 | |
23 #[path = "common.rs"] | |
24 mod common; | |
25 use common::{load_file_into_vec, load_anm_image, LoadedTexture}; | |
26 | |
27 const VS: &str = r#" | |
28 in ivec3 in_position; | |
29 in vec2 in_texcoord; | |
30 in uvec4 in_color; | |
31 | |
32 uniform mat4 mvp; | |
33 | |
34 out vec2 texcoord; | |
35 out vec4 color; | |
36 | |
37 void main() | |
38 { | |
39 gl_Position = mvp * vec4(vec3(in_position), 1.0); | |
40 texcoord = vec2(in_texcoord); | |
41 | |
42 // Normalized from the u8 being passed. | |
43 color = vec4(in_color) / 255.; | |
44 } | |
45 "#; | |
46 | |
47 const FS: &str = r#" | |
48 in vec2 texcoord; | |
49 in vec4 color; | |
50 | |
51 uniform sampler2D color_map; | |
52 | |
53 out vec4 frag_color; | |
54 | |
55 void main() | |
56 { | |
57 frag_color = texture(color_map, texcoord) * color; | |
58 } | |
59 "#; | |
60 | |
61 #[derive(Clone, Copy, Debug, Eq, PartialEq, Semantics)] | |
62 pub enum Semantics { | |
63 #[sem(name = "in_position", repr = "[i16; 3]", wrapper = "VertexPosition")] | |
64 Position, | |
65 | |
66 #[sem(name = "in_texcoord", repr = "[f32; 2]", wrapper = "VertexTexcoord")] | |
67 Texcoord, | |
68 | |
69 #[sem(name = "in_color", repr = "[u8; 4]", wrapper = "VertexColor")] | |
70 Color, | |
71 } | |
72 | |
73 #[repr(C)] | |
74 #[derive(Clone, Copy, Debug, PartialEq, Vertex)] | |
75 #[vertex(sem = "Semantics")] | |
76 struct Vertex { | |
77 pos: VertexPosition, | |
78 uv: VertexTexcoord, | |
79 rgba: VertexColor, | |
80 } | |
81 | |
82 #[derive(UniformInterface)] | |
83 struct ShaderInterface { | |
84 // the 'static lifetime acts as “anything” here | |
85 color_map: Uniform<&'static BoundTexture<'static, Flat, Dim2, NormUnsigned>>, | |
86 | |
87 #[uniform(name = "mvp")] | |
88 mvp: Uniform<[[f32; 4]; 4]>, | |
89 } | |
90 | |
91 fn main() { | |
92 // Parse arguments. | |
93 let args: Vec<_> = env::args().collect(); | |
94 if args.len() != 4 { | |
95 eprintln!("Usage: {} <ECL file> <ANM file> <easy|normal|hard|lunatic>", args[0]); | |
96 return; | |
97 } | |
98 let ecl_filename = Path::new(&args[1]); | |
99 let anm_filename = Path::new(&args[2]); | |
100 let rank: Rank = args[3].parse().expect("rank"); | |
101 | |
102 // Open the ECL file. | |
103 let buf = load_file_into_vec(ecl_filename); | |
104 let (_, ecl) = Ecl::from_slice(&buf).unwrap(); | |
105 assert_eq!(ecl.mains.len(), 1); | |
106 let main = ecl.mains[0].clone(); | |
107 | |
108 // Open the ANM file. | |
109 let buf = load_file_into_vec(anm_filename); | |
110 let (_, mut anms) = Anm0::from_slice(&buf).unwrap(); | |
111 let anm0 = anms.pop().unwrap(); | |
112 let anm0 = Rc::new(RefCell::new(anm0)); | |
113 | |
114 // Get the time since January 1970 as a seed for the PRNG. | |
115 let time = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap(); | |
116 let prng = Rc::new(RefCell::new(Prng::new(time.subsec_micros() as u16))); | |
117 | |
118 // Create the Game god object. | |
119 let game = Game::new(prng, rank); | |
120 let game = Rc::new(RefCell::new(game)); | |
121 | |
122 assert_eq!(std::mem::size_of::<Vertex>(), std::mem::size_of::<FakeVertex>()); | |
123 let vertices: [Vertex; 4] = unsafe { std::mem::uninitialized() }; | |
124 | |
125 let mut surface = GlfwSurface::new(WindowDim::Windowed(384, 448), "Touhou", WindowOpt::default()).unwrap(); | |
126 | |
127 // Open the image atlas matching this ANM. | |
128 let tex = load_anm_image(&mut surface, &anm0.borrow(), anm_filename).expect("image loading"); | |
129 | |
130 // set the uniform interface to our type so that we can read textures from the shader | |
131 let program = | |
132 Program::<Semantics, (), ShaderInterface>::from_strings(None, VS, None, FS).expect("program creation").ignore_warnings(); | |
133 | |
134 let mut tess = TessBuilder::new(&mut surface) | |
135 .add_vertices(vertices) | |
136 .set_mode(Mode::TriangleFan) | |
137 .build() | |
138 .unwrap(); | |
139 | |
140 let mut back_buffer = surface.back_buffer().unwrap(); | |
141 let mut resize = false; | |
142 let mut frame = 0; | |
143 let mut ecl_runners = vec![]; | |
144 | |
145 'app: loop { | |
146 for event in surface.poll_events() { | |
147 match event { | |
148 WindowEvent::Close | WindowEvent::Key(Key::Escape, _, Action::Release, _) => break 'app, | |
149 | |
150 WindowEvent::FramebufferSize(..) => { | |
151 resize = true; | |
152 } | |
153 | |
154 _ => (), | |
155 } | |
156 } | |
157 | |
158 if resize { | |
159 back_buffer = surface.back_buffer().unwrap(); | |
160 resize = false; | |
161 } | |
162 | |
163 for call in main.instructions.iter() { | |
164 if call.time == frame { | |
165 let sub = call.sub; | |
166 let instr = call.instr; | |
167 let (x, y, _z, life, bonus, score, mirror) = match instr { | |
168 MainInstruction::SpawnEnemy(x, y, z, life, bonus, score) => (x, y, z, life, bonus, score, false), | |
169 MainInstruction::SpawnEnemyMirrored(x, y, z, life, bonus, score) => (x, y, z, life, bonus, score, true), | |
170 MainInstruction::SpawnEnemyRandom(x, y, z, life, bonus, score) => (x, y, z, life, bonus, score, false), | |
171 MainInstruction::SpawnEnemyMirroredRandom(x, y, z, life, bonus, score) => (x, y, z, life, bonus, score, true), | |
172 _ => continue, | |
173 }; | |
174 let enemy = Enemy::new(Position::new(x, y), life, bonus, score, mirror, Rc::downgrade(&anm0), Rc::downgrade(&game)); | |
175 let runner = EclRunner::new(&ecl, enemy, sub); | |
176 ecl_runners.push(runner); | |
177 } | |
178 } | |
179 | |
180 for runner in ecl_runners.iter_mut() { | |
181 runner.run_frame(); | |
182 let mut enemy = runner.enemy.borrow_mut(); | |
183 enemy.update(); | |
184 } | |
185 | |
186 // here, we need to bind the pipeline variable; it will enable us to bind the texture to the GPU | |
187 // and use it in the shader | |
188 surface | |
189 .pipeline_builder() | |
190 .pipeline(&back_buffer, [0., 0., 0., 0.], |pipeline, mut shd_gate| { | |
191 // bind our fancy texture to the GPU: it gives us a bound texture we can use with the shader | |
192 let bound_tex = match &tex { | |
193 LoadedTexture::Rgb(tex) => pipeline.bind_texture(tex), | |
194 LoadedTexture::Rgba(tex) => pipeline.bind_texture(tex), | |
195 }; | |
196 | |
197 shd_gate.shade(&program, |iface, mut rdr_gate| { | |
198 // update the texture; strictly speaking, this update doesn’t do much: it just tells the GPU | |
199 // to use the texture passed as argument (no allocation or copy is performed) | |
200 iface.color_map.update(&bound_tex); | |
201 //let mvp = ortho_2d(0., 384., 448., 0.); | |
202 let proj = perspective(0.5235987755982988, 384. / 448., 101010101./2010101., 101010101./10101.); | |
203 let view = setup_camera(0., 0., 1.); | |
204 let mvp = view * proj; | |
205 //println!("{:#?}", mvp); | |
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 let mut game = game.borrow_mut(); | |
214 game.run_frame(); | |
215 | |
216 for (x, y, z, sprite) in game.get_sprites() { | |
217 { | |
218 let mut slice = tess | |
219 .as_slice_mut() | |
220 .unwrap(); | |
221 | |
222 let sprite = sprite.borrow(); | |
223 let fake_vertices = unsafe { std::mem::transmute::<*mut Vertex, &mut [FakeVertex; 4]>(slice.as_mut_ptr()) }; | |
224 sprite.fill_vertices(fake_vertices, x, y, z); | |
225 } | |
226 | |
227 // render the tessellation to the surface the regular way and let the vertex shader’s | |
228 // magic do the rest! | |
229 tess_gate.render(&tess); | |
230 } | |
231 }); | |
232 }); | |
233 }); | |
234 | |
235 surface.swap_buffers(); | |
236 frame += 1; | |
237 } | |
238 } |