diff options
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | src/main.rs | 37 | ||||
| -rw-r--r-- | src/state.rs | 7 |
3 files changed, 37 insertions, 8 deletions
@@ -6,6 +6,7 @@ authors = ["Raymond Hogenson <rhogenson@posteo.net>"] [dependencies] rand = "0.5.5" lazy_static = "1.1.0" +tempfile = "3" [dependencies.sdl2] git = "https://github.com/AngryLawyer/rust-sdl2" diff --git a/src/main.rs b/src/main.rs index 9089daf..55066a2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ extern crate sdl2; +extern crate tempfile; mod state; mod player; @@ -8,6 +9,7 @@ mod gates; use state::Positioned; use state::Drawable; use state::Bullet; +use std::io::Write; enum Result { Win, @@ -36,7 +38,7 @@ fn frame<T: state::Enemy<U>, U: state::Bullet>(canvas: &mut sdl2::render::Window } state.player.act(events); if count % state.player.fire_rate as u64 == 0 { - state.player_bullets.push(state::PBullet::new(state.player.x, 0., state.player.y, -3.)); + state.player_bullets.push(state::PBullet::new(state.player.x, 0., state.player.y, -3.5)); } if state.bullets.iter().any(|bullet| state.player.overlaps(bullet)) || new_bullets.iter().any(|bullet| state.player.overlaps(bullet)) { return Result::Loss; @@ -73,6 +75,31 @@ fn frame<T: state::Enemy<U>, U: state::Bullet>(canvas: &mut sdl2::render::Window Result::Nothing } +/* Why tf do I not need a lifetime here? */ +fn get_surface(bytes: &[u8]) -> sdl2::surface::Surface { + /* Wow what a risky function */ + let mut f = tempfile::NamedTempFile::new().unwrap(); + f.write(bytes).unwrap(); + f.flush().unwrap(); + sdl2::image::LoadSurface::from_file(f.path()).unwrap() +} + +fn get_player_surface() -> sdl2::surface::Surface<'static> { + get_surface(include_bytes!("assets/stallman.png")) +} + +fn get_enemy_surface() -> sdl2::surface::Surface<'static> { + get_surface(include_bytes!("assets/gates.png")) +} + +fn get_ebullet_surface() -> sdl2::surface::Surface<'static> { + get_surface(include_bytes!("assets/binary_blob.png")) +} + +fn get_pbullet_surface() -> sdl2::surface::Surface<'static> { + get_surface(include_bytes!("assets/player_pepp.png")) +} + fn main() { let sdl_context = sdl2::init().unwrap(); let video_subsystem = sdl_context.video().unwrap(); @@ -87,10 +114,10 @@ fn main() { let frame_duration = std::time::Duration::new(0, 1_000_000_000u32 / 60); let mut state: state::State<gates::Gates, gates::GatesBullet> = state::State::new(); let mut won = false; - let player_surface: sdl2::surface::Surface = sdl2::image::LoadSurface::from_file("assets/stallman.png").unwrap(); - let enemy_surface: sdl2::surface::Surface = sdl2::image::LoadSurface::from_file("assets/gates.png").unwrap(); - let ebullet_surface: sdl2::surface::Surface = sdl2::image::LoadSurface::from_file("assets/binary_blob.png").unwrap(); - let pbullet_surface: sdl2::surface::Surface = sdl2::image::LoadSurface::from_file("assets/player_pepp.png").unwrap(); + let player_surface = get_player_surface(); + let enemy_surface = get_enemy_surface(); + let ebullet_surface = get_ebullet_surface(); + let pbullet_surface = get_pbullet_surface(); let texture_creator = canvas.texture_creator(); let player_texture = texture_creator.create_texture_from_surface(player_surface).unwrap(); let enemy_texture = texture_creator.create_texture_from_surface(enemy_surface).unwrap(); diff --git a/src/state.rs b/src/state.rs index d80c550..820c1e4 100644 --- a/src/state.rs +++ b/src/state.rs @@ -57,7 +57,8 @@ impl<T: StraightBullet> Bullet for T { self.set_real_y(real_y + d_y); } fn delete(&mut self) { - self.set_real_x(-10.); + /* TODO: maybe a more robust way of deleting */ + self.set_real_x(-1000.); } } @@ -120,7 +121,7 @@ pub trait Positioned { fn outside(&self) -> bool { let x = self.x(); let y = self.y(); - x < 0 || x > constants::X_DIM as i32 || y < 0 || y > constants::Y_DIM as i32 + x < -100 || x > constants::X_DIM as i32 + 100 || y < -100 || y > constants::Y_DIM as i32 + 100 } } @@ -136,7 +137,7 @@ impl Positioned for PBullet { } } -pub trait Drawable : Positioned { +pub trait Drawable: Positioned { fn center() -> (u32, u32); fn draw(&self, canvas: &mut sdl2::render::WindowCanvas, texture: &sdl2::render::Texture) { let tw = texture.query().width; |
