diff --git a/Cargo.lock b/Cargo.lock index b5f07fe..1322f26 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,7 +2,8 @@ name = "super-matte-bros" version = "0.0.1" dependencies = [ - "sdl2 0.0.16 (git+https://github.com/AngryLawyer/rust-sdl2)", + "sdl2 0.0.17 (git+https://github.com/AngryLawyer/rust-sdl2)", + "sdl2_image 0.0.1-alpha.1 (git+https://github.com/fgeldenhuys/rust-sdl2_image)", ] [[package]] @@ -12,8 +13,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "sdl2" -version = "0.0.16" -source = "git+https://github.com/AngryLawyer/rust-sdl2#2dd0986162fa48fd930ab59c696c0eabc1ac2344" +version = "0.0.17" +source = "git+https://github.com/AngryLawyer/rust-sdl2#32b5df94f0e29805b7087d7b49ef9699e85d5640" dependencies = [ "sdl2-sys 0.0.16 (git+https://github.com/AngryLawyer/rust-sdl2)", ] @@ -21,8 +22,16 @@ dependencies = [ [[package]] name = "sdl2-sys" version = "0.0.16" -source = "git+https://github.com/AngryLawyer/rust-sdl2#2dd0986162fa48fd930ab59c696c0eabc1ac2344" +source = "git+https://github.com/AngryLawyer/rust-sdl2#32b5df94f0e29805b7087d7b49ef9699e85d5640" dependencies = [ "pkg-config 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "sdl2_image" +version = "0.0.1-alpha.1" +source = "git+https://github.com/fgeldenhuys/rust-sdl2_image#21bf12fed5d6f6a8ae49fbd20637921f97d17fe6" +dependencies = [ + "sdl2 0.0.17 (git+https://github.com/AngryLawyer/rust-sdl2)", +] + diff --git a/Cargo.toml b/Cargo.toml index b6cd026..06d6983 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,5 +6,8 @@ authors = ["logaritmisk "] [dependencies.sdl2] git = "https://github.com/AngryLawyer/rust-sdl2" +[dependencies.sdl2_image] +git = "https://github.com/fgeldenhuys/rust-sdl2_image" + [[bin]] name = "super-matte-bros" diff --git a/gfx/floor.png b/gfx/floor.png new file mode 100644 index 0000000..51c0a83 Binary files /dev/null and b/gfx/floor.png differ diff --git a/src/main.rs b/src/main.rs index 5c7220b..449d1f2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ extern crate sdl2; +extern crate sdl2_image; - +use std::os; use std::num::Float; use std::iter::range_step; @@ -34,7 +35,7 @@ const PLAYER_SPEED_X : f32 = 4.0; const PLAYER_THRESHOLD_X : f32 = 0.2; const PLAYER_ACCELERATION_X_START : f32 = 0.02; -const PLAYER_ACCELERATION_X_STOP : f32 = 0.1; +const PLAYER_ACCELERATION_X_STOP : f32 = 0.15; const PLAYER_ACCELERATION_X_CHANGE : f32 = 0.06; @@ -47,6 +48,7 @@ enum Tile { fn main() { sdl2::init(sdl2::INIT_EVERYTHING); + sdl2_image::init(sdl2_image::INIT_PNG); let window = match Window::new("Super Matte Bros", WindowPos::PosCentered, WindowPos::PosCentered, SCREEN_WIDTH as isize, SCREEN_HEIGHT as isize, OPENGL) { Ok(window) => window, @@ -58,6 +60,18 @@ fn main() { Err(err) => panic!("failed to create renderer: {}", err) }; + let floor = Path::new("gfx/floor.png"); + + let surface = match sdl2_image::LoadSurface::from_file(&floor) { + Ok(surface) => surface, + Err(err) => panic!(format!("failed to load png: {}", err)) + }; + + let texture = match renderer.create_texture_from_surface(&surface) { + Ok(texture) => texture, + Err(err) => panic!(format!("failed to create surface: {}", err)) + }; + let mut keyboard = KeyboardHandler::new(); let mut layer = Layer::new(120, 20, TILE_WIDTH, TILE_HEIGHT, Tile::Empty); @@ -111,7 +125,6 @@ fn main() { break 'main; } - if keyboard.is_held(KeyCode::Right) && (player.dx >= 0.0 || player.on_ground) { let a = if player.dx > 0.0 { PLAYER_ACCELERATION_X_START @@ -120,8 +133,7 @@ fn main() { }; player.dx = a * PLAYER_SPEED_X + (1.0 - a) * player.dx; - } - else if keyboard.is_held(KeyCode::Left) && (player.dx <= 0.0 || player.on_ground) { + } else if keyboard.is_held(KeyCode::Left) && (player.dx <= 0.0 || player.on_ground) { let a = if player.dx < 0.0 { PLAYER_ACCELERATION_X_START } else { @@ -130,9 +142,7 @@ fn main() { player.dx = a * -PLAYER_SPEED_X + (1.0 - a) * player.dx; } else if player.on_ground { - let a = PLAYER_ACCELERATION_X_STOP; - - player.dx = (1.0 - a) * player.dx; + player.dx = (1.0 - PLAYER_ACCELERATION_X_STOP) * player.dx; if player.dx.abs() <= PLAYER_THRESHOLD_X { player.dx = 0.0; @@ -333,11 +343,14 @@ fn main() { let _ = renderer.set_draw_color(Color::RGB(0, 255, 0)); let _ = renderer.fill_rect(&player_rect); + let _ = renderer.copy(&texture, None, Some(Rect::new(0, 0, 32, 32))); + renderer.present(); delay(5); } + sdl2_image::quit(); sdl2::quit(); } diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 0000000..e69de29