annotate examples/eclrenderer.rs @ 706:bca515da9047

examples: use common module.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 23 Aug 2019 19:46:47 +0200
parents b6c351ca0a35
children 987409d48991
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;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 use luminance::framebuffer::Framebuffer;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 use luminance::pipeline::BoundTexture;
706
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
5 use luminance::pixel::Floating;
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 use luminance::render_state::RenderState;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 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
8 use luminance::tess::{Mode, TessBuilder};
706
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
9 use luminance::texture::{Dim2, Flat};
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 use luminance_derive::{Semantics, Vertex, UniformInterface};
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 use luminance_glfw::event::{Action, Key, WindowEvent};
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 use luminance_glfw::surface::{GlfwSurface, Surface, WindowDim, WindowOpt};
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 use touhou::th06::anm0::Anm0;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14 use touhou::th06::anm0_vm::{Sprite, Vertex as FakeVertex};
664
f08e8e3c6196 Use bitflags for the rank, instead of an u16.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 662
diff changeset
15 use touhou::th06::ecl::{Ecl, Rank};
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
16 use touhou::th06::ecl_vm::EclRunner;
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 use touhou::th06::enemy::{Enemy, Game, Position};
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18 use touhou::util::math::{perspective, setup_camera};
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 use touhou::util::prng::Prng;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20 use std::cell::RefCell;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21 use std::rc::Rc;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22 use std::env;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 use std::path::Path;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24
706
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
25 #[path = "common.rs"]
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
26 mod common;
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
27 use common::{load_file_into_vec, load_anm_image, LoadedTexture};
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
28
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29 const VS: &str = r#"
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 in ivec3 in_position;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 in vec2 in_texcoord;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 in uvec4 in_color;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 uniform mat4 mvp;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 out vec2 texcoord;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 out vec4 color;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 void main()
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 {
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 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
42 texcoord = vec2(in_texcoord);
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 // 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
45 color = vec4(in_color) / 255.;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 "#;
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 const FS: &str = r#"
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50 in vec2 texcoord;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 in vec4 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 uniform sampler2D color_map;
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 out vec4 frag_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 void main()
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 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
60 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61 "#;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63 #[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
64 pub enum Semantics {
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 #[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
66 Position,
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68 #[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
69 Texcoord,
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 #[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
72 Color,
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75 #[repr(C)]
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76 #[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
77 #[vertex(sem = "Semantics")]
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 struct Vertex {
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79 pos: VertexPosition,
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80 uv: VertexTexcoord,
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81 rgba: VertexColor,
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
83
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84 #[derive(UniformInterface)]
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
85 struct ShaderInterface {
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
86 // the 'static lifetime acts as “anything” here
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
87 color_map: Uniform<&'static BoundTexture<'static, Flat, Dim2, Floating>>,
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 #[uniform(name = "mvp")]
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
90 mvp: Uniform<[[f32; 4]; 4]>,
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
92
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
93 fn main() {
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
94 // Parse arguments.
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
95 let args: Vec<_> = env::args().collect();
706
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
96 if args.len() != 5 {
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
97 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
98 return;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
99 }
706
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
100 let ecl_filename = Path::new(&args[1]);
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
101 let anm_filename = Path::new(&args[2]);
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
102 let rank: Rank = args[3].parse().expect("rank");
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
103 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
104
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
105 // Open the ECL file.
700
ccb739c5b66c examples: factorise file reading into a buffer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 696
diff changeset
106 let buf = load_file_into_vec(ecl_filename);
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
107 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
108
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
109 // Open the ANM file.
700
ccb739c5b66c examples: factorise file reading into a buffer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 696
diff changeset
110 let buf = load_file_into_vec(anm_filename);
701
b6c351ca0a35 anm0: return the nom IResult and the list of Anm0s.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 700
diff changeset
111 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
112 let anm0 = anms.pop().unwrap();
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
113 let anm0 = Rc::new(RefCell::new(anm0));
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
114
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
115 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
116 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
117 return;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
118 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
119
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
120 // 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
121 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
122 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
123
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
124 // Create the Game god object.
670
cdb484115c5b Use the requested rank in eclrenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 665
diff changeset
125 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
126 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
127
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
128 // And the enemy object.
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
129 let enemy = Enemy::new(Position::new(0., 0.), 500, 0, 640, 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
130 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
131
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
132 assert_eq!(std::mem::size_of::<Vertex>(), std::mem::size_of::<FakeVertex>());
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
133 let vertices: [Vertex; 4] = unsafe { std::mem::uninitialized() };
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
134
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
135 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
136
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
137 // Open the image atlas matching this ANM.
706
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
138 let tex = load_anm_image(&mut surface, &anm0.borrow(), anm_filename);
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
139
3a9d82a02c88 Add a contructor for enemy, and a new 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
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
141 let (program, _) =
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
142 Program::<Semantics, (), ShaderInterface>::from_strings(None, VS, None, FS).expect("program creation");
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
143
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
144 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
145 .add_vertices(vertices)
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
146 .set_mode(Mode::TriangleFan)
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
147 .build()
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
148 .unwrap();
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
149
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
150 let mut back_buffer = Framebuffer::back_buffer(surface.size());
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
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
157 WindowEvent::FramebufferSize(width, height) => {
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
158 back_buffer = Framebuffer::back_buffer([width as u32, height as u32]);
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
661
598f3125cbac Implement enough instructions to execute sub 0 from stage 1.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 660
diff changeset
165 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
166 break;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
167 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
168
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
169 {
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
170 let mut slice = tess
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
171 .as_slice_mut()
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
172 .unwrap();
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
173
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
174 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
175 {
107bb5ca5cc8 Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 661
diff changeset
176 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
177 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
178 }
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
179 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
180 game.run_frame();
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
181 let sprites = game.get_sprites();
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
182 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
183 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
184
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
185 // 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
186 // 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
187 surface
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
188 .pipeline_builder()
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
189 .pipeline(&back_buffer, [0., 0., 0., 0.], |pipeline, shd_gate| {
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
190 // 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
191 let bound_tex = match &tex {
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
192 LoadedTexture::Rgb(tex) => pipeline.bind_texture(tex),
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
193 LoadedTexture::Rgba(tex) => pipeline.bind_texture(tex),
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
194 };
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
195
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
196 shd_gate.shade(&program, |rdr_gate, iface| {
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
197 // 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
198 // 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
199 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
200 //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
201 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
202 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
203 let mvp = view * proj;
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
204 //println!("{:#?}", mvp);
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
205 // 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
206 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
207
706
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
208 let render_state = RenderState::default()
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
209 .set_blending((Equation::Additive, Factor::SrcAlpha, Factor::SrcAlphaComplement));
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
210
bca515da9047 examples: use common module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
211 rdr_gate.render(render_state, |tess_gate| {
658
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
212 // 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
213 // magic do the rest!
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
214 tess_gate.render(&mut surface, (&tess).into());
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
215 });
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
216 });
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
217 });
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
218
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
219 surface.swap_buffers();
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
220 }
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
661
598f3125cbac Implement enough instructions to execute sub 0 from stage 1.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 660
diff changeset
223 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
224 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
225 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
226 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
227 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
228 }
3a9d82a02c88 Add a contructor for enemy, and a new example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
229 }