changeset 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 0977d479e37d
children 01915da33b99
files examples/stagerunner.rs src/th06/ecl_vm.rs src/th06/enemy.rs
diffstat 3 files changed, 248 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/stagerunner.rs	Sat Dec 28 23:53:09 2019 +0100
@@ -0,0 +1,238 @@
+use luminance::blending::{Equation, Factor};
+use luminance::context::GraphicsContext;
+use luminance::pipeline::BoundTexture;
+use luminance::pixel::NormUnsigned;
+use luminance::render_state::RenderState;
+use luminance::shader::program::{Program, Uniform};
+use luminance::tess::{Mode, TessBuilder};
+use luminance::texture::{Dim2, Flat};
+use luminance_derive::{Semantics, Vertex, UniformInterface};
+use luminance_glfw::{Action, Key, WindowEvent, GlfwSurface, Surface, WindowDim, WindowOpt};
+use touhou::th06::anm0::Anm0;
+use touhou::th06::anm0_vm::Vertex as FakeVertex;
+use touhou::th06::ecl::{Ecl, Rank, MainInstruction};
+use touhou::th06::ecl_vm::EclRunner;
+use touhou::th06::enemy::{Enemy, Game, Position};
+use touhou::util::math::{perspective, setup_camera};
+use touhou::util::prng::Prng;
+use std::cell::RefCell;
+use std::rc::Rc;
+use std::env;
+use std::path::Path;
+
+#[path = "common.rs"]
+mod common;
+use common::{load_file_into_vec, load_anm_image, LoadedTexture};
+
+const VS: &str = r#"
+in ivec3 in_position;
+in vec2 in_texcoord;
+in uvec4 in_color;
+
+uniform mat4 mvp;
+
+out vec2 texcoord;
+out vec4 color;
+
+void main()
+{
+    gl_Position = mvp * vec4(vec3(in_position), 1.0);
+    texcoord = vec2(in_texcoord);
+
+    // Normalized from the u8 being passed.
+    color = vec4(in_color) / 255.;
+}
+"#;
+
+const FS: &str = r#"
+in vec2 texcoord;
+in vec4 color;
+
+uniform sampler2D color_map;
+
+out vec4 frag_color;
+
+void main()
+{
+    frag_color = texture(color_map, texcoord) * color;
+}
+"#;
+
+#[derive(Clone, Copy, Debug, Eq, PartialEq, Semantics)]
+pub enum Semantics {
+    #[sem(name = "in_position", repr = "[i16; 3]", wrapper = "VertexPosition")]
+    Position,
+
+    #[sem(name = "in_texcoord", repr = "[f32; 2]", wrapper = "VertexTexcoord")]
+    Texcoord,
+
+    #[sem(name = "in_color", repr = "[u8; 4]", wrapper = "VertexColor")]
+    Color,
+}
+
+#[repr(C)]
+#[derive(Clone, Copy, Debug, PartialEq, Vertex)]
+#[vertex(sem = "Semantics")]
+struct Vertex {
+    pos: VertexPosition,
+    uv: VertexTexcoord,
+    rgba: VertexColor,
+}
+
+#[derive(UniformInterface)]
+struct ShaderInterface {
+    // the 'static lifetime acts as “anything” here
+    color_map: Uniform<&'static BoundTexture<'static, Flat, Dim2, NormUnsigned>>,
+
+    #[uniform(name = "mvp")]
+    mvp: Uniform<[[f32; 4]; 4]>,
+}
+
+fn main() {
+    // Parse arguments.
+    let args: Vec<_> = env::args().collect();
+    if args.len() != 4 {
+        eprintln!("Usage: {} <ECL file> <ANM file> <easy|normal|hard|lunatic>", args[0]);
+        return;
+    }
+    let ecl_filename = Path::new(&args[1]);
+    let anm_filename = Path::new(&args[2]);
+    let rank: Rank = args[3].parse().expect("rank");
+
+    // Open the ECL file.
+    let buf = load_file_into_vec(ecl_filename);
+    let (_, ecl) = Ecl::from_slice(&buf).unwrap();
+    assert_eq!(ecl.mains.len(), 1);
+    let main = ecl.mains[0].clone();
+
+    // Open the ANM file.
+    let buf = load_file_into_vec(anm_filename);
+    let (_, mut anms) = Anm0::from_slice(&buf).unwrap();
+    let anm0 = anms.pop().unwrap();
+    let anm0 = Rc::new(RefCell::new(anm0));
+
+    // Get the time since January 1970 as a seed for the PRNG.
+    let time = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap();
+    let prng = Rc::new(RefCell::new(Prng::new(time.subsec_micros() as u16)));
+
+    // Create the Game god object.
+    let game = Game::new(prng, rank);
+    let game = Rc::new(RefCell::new(game));
+
+    assert_eq!(std::mem::size_of::<Vertex>(), std::mem::size_of::<FakeVertex>());
+    let vertices: [Vertex; 4] = unsafe { std::mem::uninitialized() };
+
+    let mut surface = GlfwSurface::new(WindowDim::Windowed(384, 448), "Touhou", WindowOpt::default()).unwrap();
+
+    // Open the image atlas matching this ANM.
+    let tex = load_anm_image(&mut surface, &anm0.borrow(), anm_filename).expect("image loading");
+
+    // set the uniform interface to our type so that we can read textures from the shader
+    let program =
+        Program::<Semantics, (), ShaderInterface>::from_strings(None, VS, None, FS).expect("program creation").ignore_warnings();
+
+    let mut tess = TessBuilder::new(&mut surface)
+        .add_vertices(vertices)
+        .set_mode(Mode::TriangleFan)
+        .build()
+        .unwrap();
+
+    let mut back_buffer = surface.back_buffer().unwrap();
+    let mut resize = false;
+    let mut frame = 0;
+    let mut ecl_runners = vec![];
+
+    'app: loop {
+        for event in surface.poll_events() {
+            match event {
+                WindowEvent::Close | WindowEvent::Key(Key::Escape, _, Action::Release, _) => break 'app,
+
+                WindowEvent::FramebufferSize(..) => {
+                    resize = true;
+                }
+
+                _ => (),
+            }
+        }
+
+        if resize {
+            back_buffer = surface.back_buffer().unwrap();
+            resize = false;
+        }
+
+        for call in main.instructions.iter() {
+            if call.time == frame {
+                let sub = call.sub;
+                let instr = call.instr;
+                let (x, y, _z, life, bonus, score, mirror) = match instr {
+                    MainInstruction::SpawnEnemy(x, y, z, life, bonus, score) => (x, y, z, life, bonus, score, false),
+                    MainInstruction::SpawnEnemyMirrored(x, y, z, life, bonus, score) => (x, y, z, life, bonus, score, true),
+                    MainInstruction::SpawnEnemyRandom(x, y, z, life, bonus, score) => (x, y, z, life, bonus, score, false),
+                    MainInstruction::SpawnEnemyMirroredRandom(x, y, z, life, bonus, score) => (x, y, z, life, bonus, score, true),
+                    _ => continue,
+                };
+                let enemy = Enemy::new(Position::new(x, y), life, bonus, score, mirror, Rc::downgrade(&anm0), Rc::downgrade(&game));
+                let runner = EclRunner::new(&ecl, enemy, sub);
+                ecl_runners.push(runner);
+            }
+        }
+
+        for runner in ecl_runners.iter_mut() {
+            runner.run_frame();
+            let mut enemy = runner.enemy.borrow_mut();
+            enemy.update();
+        }
+
+        // here, we need to bind the pipeline variable; it will enable us to bind the texture to the GPU
+        // and use it in the shader
+        surface
+            .pipeline_builder()
+            .pipeline(&back_buffer, [0., 0., 0., 0.], |pipeline, mut shd_gate| {
+                // bind our fancy texture to the GPU: it gives us a bound texture we can use with the shader
+                let bound_tex = match &tex {
+                    LoadedTexture::Rgb(tex) => pipeline.bind_texture(tex),
+                    LoadedTexture::Rgba(tex) => pipeline.bind_texture(tex),
+                };
+
+                shd_gate.shade(&program, |iface, mut rdr_gate| {
+                    // update the texture; strictly speaking, this update doesn’t do much: it just tells the GPU
+                    // to use the texture passed as argument (no allocation or copy is performed)
+                    iface.color_map.update(&bound_tex);
+                    //let mvp = ortho_2d(0., 384., 448., 0.);
+                    let proj = perspective(0.5235987755982988, 384. / 448., 101010101./2010101., 101010101./10101.);
+                    let view = setup_camera(0., 0., 1.);
+                    let mvp = view * proj;
+                    //println!("{:#?}", mvp);
+                    // TODO: check how to pass by reference.
+                    iface.mvp.update(*mvp.borrow_inner());
+
+                    let render_state = RenderState::default()
+                        .set_blending((Equation::Additive, Factor::SrcAlpha, Factor::SrcAlphaComplement));
+
+                    rdr_gate.render(render_state, |mut tess_gate| {
+                        let mut game = game.borrow_mut();
+                        game.run_frame();
+
+                        for (x, y, z, sprite) in game.get_sprites() {
+                            {
+                                let mut slice = tess
+                                    .as_slice_mut()
+                                    .unwrap();
+
+                                let sprite = sprite.borrow();
+                                let fake_vertices = unsafe { std::mem::transmute::<*mut Vertex, &mut [FakeVertex; 4]>(slice.as_mut_ptr()) };
+                                sprite.fill_vertices(fake_vertices, x, y, z);
+                            }
+
+                            // render the tessellation to the surface the regular way and let the vertex shader’s
+                            // magic do the rest!
+                            tess_gate.render(&tess);
+                        }
+                    });
+                });
+            });
+
+        surface.swap_buffers();
+        frame += 1;
+    }
+}
--- a/src/th06/ecl_vm.rs	Sat Dec 28 22:56:32 2019 +0100
+++ b/src/th06/ecl_vm.rs	Sat Dec 28 23:53:09 2019 +0100
@@ -39,7 +39,9 @@
 /// Interpreter for enemy scripts.
 #[derive(Default)]
 pub struct EclRunner {
-    enemy: Rc<RefCell<Enemy>>,
+    /// XXX
+    pub enemy: Rc<RefCell<Enemy>>,
+
     ecl: Option<Ecl>,
     /// XXX
     pub running: bool,
--- a/src/th06/enemy.rs	Sat Dec 28 22:56:32 2019 +0100
+++ b/src/th06/enemy.rs	Sat Dec 28 23:53:09 2019 +0100
@@ -280,6 +280,8 @@
     pub(crate) boss: bool,
     pub(crate) automatic_orientation: bool,
     pub(crate) delay_attack: bool,
+    // Actually part of type_ atm.
+    pub(crate) mirror: bool,
 
     // Tuples.
     pub(crate) difficulty_coeffs: DifficultyCoeffs,
@@ -315,19 +317,21 @@
 
 impl Enemy {
     /// Create a new enemy.
-    pub fn new(pos: Position, life: i32, bonus_dropped: u32, die_score: u32, anm0: Weak<RefCell<Anm0>>, game: Weak<RefCell<Game>>) -> Rc<RefCell<Enemy>> {
+    pub fn new(pos: Position, life: i16, bonus_dropped: i16, die_score: u32, mirror: bool, anm0: Weak<RefCell<Anm0>>, game: Weak<RefCell<Game>>) -> Rc<RefCell<Enemy>> {
         let game_rc = game.upgrade().unwrap();
         let mut enemy = Enemy {
             pos,
             anm0,
             game,
             visible: true,
-            bonus_dropped,
+            // XXX: shouldn’t be u32, since that can be -1.
+            bonus_dropped: bonus_dropped as u32,
             die_score,
             life: if life < 0 { 1 } else { life as u32 },
             touchable: true,
             collidable: true,
             damageable: true,
+            mirror,
             ..Default::default()
         };
         let mut game = game_rc.borrow_mut();
@@ -457,7 +461,7 @@
 
         let dx = self.angle.cos() * speed;
         let dy = self.angle.sin() * speed;
-        if self.type_ & 2 != 0 {
+        if self.mirror {
             x -= dx;
         } else {
             x += dx;