annotate runners/src/bin/eclrenderer.rs @ 760:eba9a3d0c484

formats: Fix the tests.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 05 Jan 2021 04:28:45 +0100
parents 21b186be2590
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
706
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
1 use luminance::blending::{Equation, Factor};
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 use luminance::context::GraphicsContext;
742
0a250ddfae79 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 717
diff changeset
3 use luminance::pipeline::{BoundTexture, PipelineState};
708
3954801b6299 examples: Update to luminance 0.32.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 707
diff changeset
4 use luminance::pixel::NormUnsigned;
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 use luminance::render_state::RenderState;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 use luminance::shader::program::{Program, Uniform};
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 use luminance::tess::{Mode, TessBuilder};
753
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
8 use luminance::texture::Dim2;
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 use luminance_derive::{Semantics, Vertex, UniformInterface};
707
987409d48991 Switch to versioned luminance now that it got released, not hardcoded paths.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 706
diff changeset
10 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
11 use touhou_formats::th06::anm0::Anm0;
21b186be2590 Split the Rust version into multiple crates.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 753
diff changeset
12 use touhou_formats::th06::ecl::{Ecl, Rank};
21b186be2590 Split the Rust version into multiple crates.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 753
diff changeset
13 use touhou_interpreters::th06::anm0::{Sprite, Vertex as FakeVertex};
21b186be2590 Split the Rust version into multiple crates.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 753
diff changeset
14 use touhou_interpreters::th06::ecl::EclRunner;
21b186be2590 Split the Rust version into multiple crates.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 753
diff changeset
15 use touhou_interpreters::th06::enemy::{Enemy, Game, Position};
21b186be2590 Split the Rust version into multiple crates.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 753
diff changeset
16 use touhou_utils::math::{perspective, setup_camera};
21b186be2590 Split the Rust version into multiple crates.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 753
diff changeset
17 use touhou_utils::prng::Prng;
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18 use std::cell::RefCell;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 use std::rc::Rc;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20 use std::env;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21 use std::path::Path;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22
757
21b186be2590 Split the Rust version into multiple crates.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 753
diff changeset
23 use touhou_runners::common::{load_file_into_vec, load_anm_image, LoadedTexture};
706
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
24
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 const VS: &str = r#"
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 in ivec3 in_position;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 in vec2 in_texcoord;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 in uvec4 in_color;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 uniform mat4 mvp;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 out vec2 texcoord;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 out vec4 color;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 void main()
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 {
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 gl_Position = mvp * vec4(vec3(in_position), 1.0);
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 texcoord = vec2(in_texcoord);
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 // Normalized from the u8 being passed.
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 color = vec4(in_color) / 255.;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 "#;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 const FS: &str = r#"
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 in vec2 texcoord;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 in vec4 color;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 uniform sampler2D color_map;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 out vec4 frag_color;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 void main()
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 {
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 frag_color = texture(color_map, texcoord) * color;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 "#;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 #[derive(Clone, Copy, Debug, Eq, PartialEq, Semantics)]
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60 pub enum Semantics {
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61 #[sem(name = "in_position", repr = "[i16; 3]", wrapper = "VertexPosition")]
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 Position,
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64 #[sem(name = "in_texcoord", repr = "[f32; 2]", wrapper = "VertexTexcoord")]
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 Texcoord,
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67 #[sem(name = "in_color", repr = "[u8; 4]", wrapper = "VertexColor")]
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68 Color,
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
70
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 #[repr(C)]
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72 #[derive(Clone, Copy, Debug, PartialEq, Vertex)]
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73 #[vertex(sem = "Semantics")]
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74 struct Vertex {
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75 pos: VertexPosition,
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76 uv: VertexTexcoord,
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77 rgba: VertexColor,
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80 #[derive(UniformInterface)]
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81 struct ShaderInterface {
3a9d82a02c88 Add a contructor for enemy, and a new 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: 752
diff changeset
83 color_map: Uniform<&'static BoundTexture<'static, Dim2, NormUnsigned>>,
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
85 #[uniform(name = "mvp")]
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
86 mvp: Uniform<[[f32; 4]; 4]>,
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
87 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
88
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
89 fn main() {
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
90 // Parse arguments.
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91 let args: Vec<_> = env::args().collect();
706
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
92 if args.len() != 5 {
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
93 eprintln!("Usage: {} <ECL file> <ANM file> <easy|normal|hard|lunatic> <sub number>", args[0]);
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
94 return;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
95 }
706
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
96 let ecl_filename = Path::new(&args[1]);
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
97 let anm_filename = Path::new(&args[2]);
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
98 let rank: Rank = args[3].parse().expect("rank");
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
99 let sub: u16 = args[4].parse().expect("number");
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
100
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
101 // Open the ECL file.
752
5e5e7136ac92 examples: Return a Result for load_file_into_vec()
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 743
diff changeset
102 let buf = load_file_into_vec(ecl_filename).unwrap();
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
103 let (_, ecl) = Ecl::from_slice(&buf).unwrap();
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
104
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
105 // Open the ANM file.
752
5e5e7136ac92 examples: Return a Result for load_file_into_vec()
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 743
diff changeset
106 let buf = load_file_into_vec(anm_filename).unwrap();
701
b6c351ca0a35 anm0: return the nom IResult and the list of Anm0s.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 700
diff changeset
107 let (_, mut anms) = Anm0::from_slice(&buf).unwrap();
b6c351ca0a35 anm0: return the nom IResult and the list of Anm0s.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 700
diff changeset
108 let anm0 = anms.pop().unwrap();
742
0a250ddfae79 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 717
diff changeset
109 let anm0 = Rc::new(RefCell::new([anm0.clone(), anm0]));
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
110
662
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
111 if ecl.subs.len() < sub as usize {
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
112 eprintln!("This ecl doesn’t contain a sub named {}.", sub);
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
113 return;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
114 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
115
687
ac092b70c39a Fix PRNG not to panic, pass it to Enemy, and initialise it with time in eclrenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 670
diff changeset
116 // Get the time since January 1970 as a seed for the PRNG.
ac092b70c39a Fix PRNG not to panic, pass it to Enemy, and initialise it with time in eclrenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 670
diff changeset
117 let time = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap();
ac092b70c39a Fix PRNG not to panic, pass it to Enemy, and initialise it with time in eclrenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 670
diff changeset
118 let prng = Rc::new(RefCell::new(Prng::new(time.subsec_micros() as u16)));
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
119
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
120 // Create the Game god object.
670
cdb484115c5b Use the requested rank in eclrenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 665
diff changeset
121 let game = Game::new(prng, rank);
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
122 let game = Rc::new(RefCell::new(game));
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
123
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
124 // And the enemy object.
742
0a250ddfae79 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 717
diff changeset
125 let enemy = Enemy::new(Position::new(0., 0.), 500, 0, 640, false, Rc::downgrade(&anm0), Rc::downgrade(&game));
662
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
126 let mut ecl_runner = EclRunner::new(&ecl, enemy.clone(), sub);
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
127
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
128 assert_eq!(std::mem::size_of::<Vertex>(), std::mem::size_of::<FakeVertex>());
743
0fed350d1778 examples: Remove deprecated std::mem::uninitialized().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 742
diff changeset
129 let vertices: [Vertex; 4] = {
0fed350d1778 examples: Remove deprecated std::mem::uninitialized().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 742
diff changeset
130 let data = std::mem::MaybeUninit::uninit();
0fed350d1778 examples: Remove deprecated std::mem::uninitialized().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 742
diff changeset
131 unsafe { data.assume_init() }
0fed350d1778 examples: Remove deprecated std::mem::uninitialized().
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 742
diff changeset
132 };
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
133
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
134 let mut surface = GlfwSurface::new(WindowDim::Windowed(384, 448), "Touhou", WindowOpt::default()).unwrap();
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
135
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
136 // Open the image atlas matching this ANM.
742
0a250ddfae79 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 717
diff changeset
137 let tex = load_anm_image(&mut surface, &anm0.borrow()[0], &anm_filename).expect("image loading");
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
138
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
139 // set the uniform interface to our type so that we can read textures from the shader
715
2b2376811f46 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 708
diff changeset
140 let program =
2b2376811f46 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 708
diff changeset
141 Program::<Semantics, (), ShaderInterface>::from_strings(None, VS, None, FS).expect("program creation").ignore_warnings();
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
142
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
143 let mut tess = TessBuilder::new(&mut surface)
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
144 .add_vertices(vertices)
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
145 .set_mode(Mode::TriangleFan)
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
146 .build()
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
147 .unwrap();
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
148
715
2b2376811f46 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 708
diff changeset
149 let mut back_buffer = surface.back_buffer().unwrap();
2b2376811f46 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 708
diff changeset
150 let mut resize = false;
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
151
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
152 'app: loop {
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
153 for event in surface.poll_events() {
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
154 match event {
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
155 WindowEvent::Close | WindowEvent::Key(Key::Escape, _, Action::Release, _) => break 'app,
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
156
715
2b2376811f46 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 708
diff changeset
157 WindowEvent::FramebufferSize(..) => {
2b2376811f46 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 708
diff changeset
158 resize = true;
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
159 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
160
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
161 _ => (),
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
162 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
163 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
164
715
2b2376811f46 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 708
diff changeset
165 if resize {
2b2376811f46 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 708
diff changeset
166 back_buffer = surface.back_buffer().unwrap();
2b2376811f46 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 708
diff changeset
167 resize = false;
2b2376811f46 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 708
diff changeset
168 }
2b2376811f46 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 708
diff changeset
169
661
598f3125cbac Implement enough instructions to execute sub 0 from stage 1.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 660
diff changeset
170 if ecl_runner.running == false {
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
171 break;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
172 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
173
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
174 {
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
175 let mut slice = tess
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
176 .as_slice_mut()
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
177 .unwrap();
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
178
660
31fc0d881105 Make ecl_vm compile, and use it in eclrenderer (doesn’t render yet).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 658
diff changeset
179 ecl_runner.run_frame();
662
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
180 {
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
181 let mut enemy = enemy.borrow_mut();
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
182 enemy.update();
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
183 }
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
184 let mut game = game.borrow_mut();
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
185 game.run_frame();
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
186 let sprites = game.get_sprites();
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
187 fill_vertices_ptr(sprites, slice.as_mut_ptr());
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
188 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
189
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
190 // here, we need to bind the pipeline variable; it will enable us to bind the texture to the GPU
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
191 // and use it in the shader
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
192 surface
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
193 .pipeline_builder()
742
0a250ddfae79 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 717
diff changeset
194 .pipeline(&back_buffer, &PipelineState::default(), |pipeline, mut shd_gate| {
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
195 // bind our fancy texture to the GPU: it gives us a bound texture we can use with the shader
706
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
196 let bound_tex = match &tex {
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
197 LoadedTexture::Rgb(tex) => pipeline.bind_texture(tex),
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
198 LoadedTexture::Rgba(tex) => pipeline.bind_texture(tex),
753
a662dddd4a2b examples: Use array textures for enemy PNGs
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 752
diff changeset
199 LoadedTexture::RgbaArray(tex) => unreachable!(),
706
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
200 };
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
201
715
2b2376811f46 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 708
diff changeset
202 shd_gate.shade(&program, |iface, mut rdr_gate| {
658
3a9d82a02c88 Add a contructor for enemy, and a new 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
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
204 // to use the texture passed as argument (no allocation or copy is performed)
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
205 iface.color_map.update(&bound_tex);
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
206 //let mvp = ortho_2d(0., 384., 448., 0.);
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
207 let proj = perspective(0.5235987755982988, 384. / 448., 101010101./2010101., 101010101./10101.);
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
208 let view = setup_camera(0., 0., 1.);
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
209 let mvp = view * proj;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
210 //println!("{:#?}", mvp);
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
211 // TODO: check how to pass by reference.
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
212 iface.mvp.update(*mvp.borrow_inner());
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
213
706
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
214 let render_state = RenderState::default()
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
215 .set_blending((Equation::Additive, Factor::SrcAlpha, Factor::SrcAlphaComplement));
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
216
742
0a250ddfae79 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 717
diff changeset
217 rdr_gate.render(&render_state, |mut tess_gate| {
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
218 // render the tessellation to the surface the regular way and let the vertex shader’s
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
219 // magic do the rest!
715
2b2376811f46 examples: Update luminance.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 708
diff changeset
220 tess_gate.render(&tess);
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
221 });
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
222 });
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
223 });
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
224
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
225 surface.swap_buffers();
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
226 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
227 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
228
661
598f3125cbac Implement enough instructions to execute sub 0 from stage 1.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 660
diff changeset
229 fn fill_vertices_ptr(sprites: Vec<(f32, f32, f32, Rc<RefCell<Sprite>>)>, vertices: *mut Vertex) {
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
230 let mut fake_vertices = unsafe { std::mem::transmute::<*mut Vertex, &mut [FakeVertex; 4]>(vertices) };
661
598f3125cbac Implement enough instructions to execute sub 0 from stage 1.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 660
diff changeset
231 for (x, y, z, sprite) in sprites {
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
232 let sprite = sprite.borrow();
661
598f3125cbac Implement enough instructions to execute sub 0 from stage 1.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 660
diff changeset
233 sprite.fill_vertices(&mut fake_vertices, x, y, z);
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
234 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
235 }