diff options
| -rw-r--r-- | Cargo.toml | 7 | ||||
| -rw-r--r-- | assets/binary_blob.png | bin | 0 -> 1674 bytes | |||
| -rw-r--r-- | assets/binary_blob.xcf | bin | 0 -> 2936 bytes | |||
| -rw-r--r-- | assets/gates.png | bin | 0 -> 3484 bytes | |||
| -rw-r--r-- | assets/gates.xcf | bin | 0 -> 8595 bytes | |||
| -rw-r--r-- | assets/player_pepp.png | bin | 0 -> 2439 bytes | |||
| -rw-r--r-- | assets/player_pepp.xcf | bin | 0 -> 3723 bytes | |||
| -rw-r--r-- | assets/stallman.png | bin | 0 -> 4740 bytes | |||
| -rw-r--r-- | assets/stallman.xcf | bin | 0 -> 7364 bytes | |||
| -rw-r--r-- | default.nix | 2 | ||||
| -rw-r--r-- | src/main.rs | 24 | ||||
| -rw-r--r-- | src/player.rs | 6 | ||||
| -rw-r--r-- | src/state.rs | 41 |
13 files changed, 44 insertions, 36 deletions
@@ -4,5 +4,10 @@ version = "0.1.0" authors = ["Raymond Hogenson <rhogenson@posteo.net>"] [dependencies] -sdl2 = "0.31.0" rand = "0.5.5" +lazy_static = "1.1.0" + +[dependencies.sdl2] +git = "https://github.com/AngryLawyer/rust-sdl2" +default-features = false +features = ["image"] diff --git a/assets/binary_blob.png b/assets/binary_blob.png Binary files differnew file mode 100644 index 0000000..e1dab24 --- /dev/null +++ b/assets/binary_blob.png diff --git a/assets/binary_blob.xcf b/assets/binary_blob.xcf Binary files differnew file mode 100644 index 0000000..f8eb16a --- /dev/null +++ b/assets/binary_blob.xcf diff --git a/assets/gates.png b/assets/gates.png Binary files differnew file mode 100644 index 0000000..e86a5b6 --- /dev/null +++ b/assets/gates.png diff --git a/assets/gates.xcf b/assets/gates.xcf Binary files differnew file mode 100644 index 0000000..7eda13a --- /dev/null +++ b/assets/gates.xcf diff --git a/assets/player_pepp.png b/assets/player_pepp.png Binary files differnew file mode 100644 index 0000000..35df0cb --- /dev/null +++ b/assets/player_pepp.png diff --git a/assets/player_pepp.xcf b/assets/player_pepp.xcf Binary files differnew file mode 100644 index 0000000..e160c04 --- /dev/null +++ b/assets/player_pepp.xcf diff --git a/assets/stallman.png b/assets/stallman.png Binary files differnew file mode 100644 index 0000000..03dae08 --- /dev/null +++ b/assets/stallman.png diff --git a/assets/stallman.xcf b/assets/stallman.xcf Binary files differnew file mode 100644 index 0000000..2e0ebc4 --- /dev/null +++ b/assets/stallman.xcf diff --git a/default.nix b/default.nix index a9e20f7..738d469 100644 --- a/default.nix +++ b/default.nix @@ -1,6 +1,6 @@ with import <nixpkgs> {}; ((import ./bullet_hell.nix).bullet_hell {}).override { crateOverrides = defaultCrateOverrides // { - bullet-hell = attrs: { buildInputs = [ SDL2 ]; }; + bullet-hell = attrs: { buildInputs = [ SDL2 SDL2_image ]; }; }; } diff --git a/src/main.rs b/src/main.rs index 76b7a5b..5c62576 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,13 @@ enum Result { Nothing, } +struct Surfaces<'a> { + player: sdl2::surface::Surface<'a>, + enemy: sdl2::surface::Surface<'a>, + pbullet: sdl2::surface::Surface<'a>, + ebullet: sdl2::surface::Surface<'a>, +} + fn spawn(count: u64) -> Vec<state::Enemy> { if count == 0 { vec![state::Enemy::new()] @@ -21,7 +28,7 @@ fn spawn(count: u64) -> Vec<state::Enemy> { } } -fn frame(canvas: &mut sdl2::render::WindowCanvas, state: &mut state::State, events: &mut sdl2::event::EventPollIterator) -> Result { +fn frame(canvas: &mut sdl2::render::WindowCanvas, state: &mut state::State, events: &mut sdl2::event::EventPollIterator, surfaces: &Surfaces) -> Result { let mut new_bullets = Vec::new(); let count = state.count; for enemy in state.enemies.iter_mut() { @@ -57,15 +64,15 @@ fn frame(canvas: &mut sdl2::render::WindowCanvas, state: &mut state::State, even state.bullets.append(&mut new_bullets); state.player_bullets.retain(|bullet| !state::PBullet::outside(bullet)); for bullet in state.player_bullets.iter() { - bullet.draw(canvas); + bullet.draw(canvas, &surfaces.pbullet); } for enemy in state.enemies.iter() { - enemy.draw(canvas); + enemy.draw(canvas, &surfaces.enemy); } for bullet in state.bullets.iter() { - bullet.draw(canvas); + bullet.draw(canvas, &surfaces.ebullet); } - state.player.draw(canvas); + state.player.draw(canvas, &surfaces.player); state.count += 1; Result::Nothing } @@ -86,6 +93,11 @@ fn main() { let mut frame_count = 0; let mut state = state::State::new(); let mut won = false; + let player_surface = sdl2::image::LoadSurface::from_file("assets/stallman.png").unwrap(); + let enemy_surface = sdl2::image::LoadSurface::from_file("assets/gates.png").unwrap(); + let ebullet_surface = sdl2::image::LoadSurface::from_file("assets/binary_blob.png").unwrap(); + let pbullet_surface = sdl2::image::LoadSurface::from_file("assets/player_pepp.png").unwrap(); + let surfaces = Surfaces { player: player_surface, enemy: enemy_surface, pbullet: pbullet_surface, ebullet: ebullet_surface }; 'running: loop { if won { for event in event_pump.poll_iter() { @@ -99,7 +111,7 @@ fn main() { } else { canvas.set_draw_color(sdl2::pixels::Color::RGB(255, 255, 255)); canvas.clear(); - match frame(&mut canvas, &mut state, &mut event_pump.poll_iter()) { + match frame(&mut canvas, &mut state, &mut event_pump.poll_iter(), &surfaces) { Result::Loss => { break; } diff --git a/src/player.rs b/src/player.rs index efd396e..9e4d418 100644 --- a/src/player.rs +++ b/src/player.rs @@ -104,12 +104,12 @@ impl state::Positioned for Player { self.y } fn radius(&self) -> u32 { - 5 + 13 } } impl state::Drawable for Player { - fn color(&self) -> sdl2::pixels::Color { - sdl2::pixels::Color::RGB(0, 0, 0) + fn center(&self) -> (u32, u32) { + (30, 19) } } diff --git a/src/state.rs b/src/state.rs index da53054..f70c0ba 100644 --- a/src/state.rs +++ b/src/state.rs @@ -239,42 +239,33 @@ impl Positioned for Enemy { } pub trait Drawable : Positioned { - fn color(&self) -> sdl2::pixels::Color; - fn draw(&self, canvas: &mut sdl2::render::WindowCanvas) { - let x = self.x(); - let y = self.y(); - let r = self.radius(); - let r = r as i32; - let x_min = x - r; - let x_max = x + r; - let y_min = y - r; - let y_max = y + r; - canvas.set_draw_color(self.color()); - for x in x_min..x_max + 1 { - for y in y_min..y_max + 1 { - if self.contains(x, y) { - canvas.fill_rect(sdl2::rect::Rect::new(x, y, 1, 1)).unwrap(); - } - } - } + fn center(&self) -> (u32, u32); + fn draw(&self, canvas: &mut sdl2::render::WindowCanvas, surface: &sdl2::surface::Surface) { + let texture_creator = canvas.texture_creator(); + let texture = texture_creator.create_texture_from_surface(surface).unwrap(); + let tw = texture.query().width; + let th = texture.query().height; + let (cx, cy) = self.center(); + let x0 = self.x() - cx as i32; + let y0 = self.y() - cy as i32; + canvas.copy(&texture, None, Some(sdl2::rect::Rect::new(x0, y0, tw, th))).unwrap(); } } impl Drawable for Enemy { - fn color(&self) -> sdl2::pixels::Color { - let red_scale = self.health as f64 / STARTING_HEALTH as f64 * 255.; - sdl2::pixels::Color::RGB(red_scale as u8, 255 - red_scale as u8, 0) + fn center(&self) -> (u32, u32) { + (0, 0) } } impl Drawable for EBullet { - fn color(&self) -> sdl2::pixels::Color { - sdl2::pixels::Color::RGB(0, 0, 255) + fn center(&self) -> (u32, u32) { + (20, 20) } } impl Drawable for PBullet { - fn color(&self) -> sdl2::pixels::Color { - sdl2::pixels::Color::RGB(230, 255, 230) + fn center(&self) -> (u32, u32) { + (16, 22) } } |
