comparison examples/eclrenderer.rs @ 662:107bb5ca5cc8

Implement Enemy::update(), which now renders the first fairy from stage 1 perfectly!
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 11 Aug 2019 18:14:01 +0200
parents 598f3125cbac
children f08e8e3c6196
comparison
equal deleted inserted replaced
661:598f3125cbac 662:107bb5ca5cc8
94 if args.len() != 5 { 94 if args.len() != 5 {
95 eprintln!("Usage: {} <ECL file> <sub number> <ANM file> <PNG file>", args[0]); 95 eprintln!("Usage: {} <ECL file> <sub number> <ANM file> <PNG file>", args[0]);
96 return; 96 return;
97 } 97 }
98 let ecl_filename = &args[1]; 98 let ecl_filename = &args[1];
99 let sub = args[2].parse().expect("number"); 99 let sub: u8 = args[2].parse().expect("number");
100 let anm_filename = &args[3]; 100 let anm_filename = &args[3];
101 let png_filename = &args[4]; 101 let png_filename = &args[4];
102 102
103 // Open the ECL file. 103 // Open the ECL file.
104 let file = File::open(ecl_filename).unwrap(); 104 let file = File::open(ecl_filename).unwrap();
113 let mut buf = vec![]; 113 let mut buf = vec![];
114 file.read_to_end(&mut buf).unwrap(); 114 file.read_to_end(&mut buf).unwrap();
115 let anm0 = Anm0::from_slice(&buf).unwrap(); 115 let anm0 = Anm0::from_slice(&buf).unwrap();
116 let anm0 = Rc::new(RefCell::new(anm0)); 116 let anm0 = Rc::new(RefCell::new(anm0));
117 117
118 if ecl.subs.len() < sub { 118 if ecl.subs.len() < sub as usize {
119 eprintln!("This ecl doesn’t contain a sub named {}.", sub); 119 eprintln!("This ecl doesn’t contain a sub named {}.", sub);
120 return; 120 return;
121 } 121 }
122 122
123 // TODO: seed this PRNG with a valid seed. 123 // TODO: seed this PRNG with a valid seed.
127 let game = Game::new(prng); 127 let game = Game::new(prng);
128 let game = Rc::new(RefCell::new(game)); 128 let game = Rc::new(RefCell::new(game));
129 129
130 // And the enemy object. 130 // And the enemy object.
131 let enemy = Enemy::new(Position::new(0., 0.), 500, 0, 640, Rc::downgrade(&anm0), Rc::downgrade(&game)); 131 let enemy = Enemy::new(Position::new(0., 0.), 500, 0, 640, Rc::downgrade(&anm0), Rc::downgrade(&game));
132 let mut ecl_runner = EclRunner::new(&ecl, enemy.clone(), 0); 132 let mut ecl_runner = EclRunner::new(&ecl, enemy.clone(), sub);
133 133
134 assert_eq!(std::mem::size_of::<Vertex>(), std::mem::size_of::<FakeVertex>()); 134 assert_eq!(std::mem::size_of::<Vertex>(), std::mem::size_of::<FakeVertex>());
135 let vertices: [Vertex; 4] = unsafe { std::mem::uninitialized() }; 135 let vertices: [Vertex; 4] = unsafe { std::mem::uninitialized() };
136 136
137 let mut surface = GlfwSurface::new(WindowDim::Windowed(384, 448), "Touhou", WindowOpt::default()).unwrap(); 137 let mut surface = GlfwSurface::new(WindowDim::Windowed(384, 448), "Touhou", WindowOpt::default()).unwrap();
149 .build() 149 .build()
150 .unwrap(); 150 .unwrap();
151 151
152 let mut back_buffer = Framebuffer::back_buffer(surface.size()); 152 let mut back_buffer = Framebuffer::back_buffer(surface.size());
153 153
154 let mut frame = 0;
155 'app: loop { 154 'app: loop {
156 for event in surface.poll_events() { 155 for event in surface.poll_events() {
157 match event { 156 match event {
158 WindowEvent::Close | WindowEvent::Key(Key::Escape, _, Action::Release, _) => break 'app, 157 WindowEvent::Close | WindowEvent::Key(Key::Escape, _, Action::Release, _) => break 'app,
159 158
166 } 165 }
167 166
168 if ecl_runner.running == false { 167 if ecl_runner.running == false {
169 break; 168 break;
170 } 169 }
171 frame += 1;
172 170
173 { 171 {
174 let mut slice = tess 172 let mut slice = tess
175 .as_slice_mut() 173 .as_slice_mut()
176 .unwrap(); 174 .unwrap();
177 175
178 ecl_runner.run_frame(); 176 ecl_runner.run_frame();
177 {
178 let mut enemy = enemy.borrow_mut();
179 enemy.update();
180 }
179 let mut game = game.borrow_mut(); 181 let mut game = game.borrow_mut();
180 game.run_frame(); 182 game.run_frame();
181 let sprites = game.get_sprites(); 183 let sprites = game.get_sprites();
182 fill_vertices_ptr(sprites, slice.as_mut_ptr()); 184 fill_vertices_ptr(sprites, slice.as_mut_ptr());
183 } 185 }