From bbeef604729e57a163299ca44d23ef0aa595cf4e Mon Sep 17 00:00:00 2001 From: logaritmisk Date: Wed, 14 Jan 2015 17:19:04 +0100 Subject: [PATCH] jay, working png support --- Cargo.lock | 17 +++++++++++++---- Cargo.toml | 3 +++ gfx/floor.png | Bin 0 -> 1106 bytes src/main.rs | 29 +++++++++++++++++++++-------- src/util.rs | 0 5 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 gfx/floor.png create mode 100644 src/util.rs 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 0000000000000000000000000000000000000000..51c0a83193211f228fc5101521454b45a10495cc GIT binary patch literal 1106 zcmZ{j&x_MQ6vwA7h*pa%y09#J7~)~QG?VlPONTbF+pODyU2Cn-s<6$rQw=tmkj&Eb zs;51A6%T?JL0J&-AMoNy1uqsn3wqf9K+u^q-Nx7kCNuNi_cL$aOWsj^Z#R`(O9B8= z)r#KWGZf?88UFsgdh(J_39qrc4URvr|KgkTPNnSuaN)8T3GnRsDgaBb%w~(VYBz9~ zTC&lj2SoNQhpPc7`j{^*!VKtJ1KY!XNeUF0uZ0;&Fp#iEC8<@b!!mUV%*#1hk+dX) zu;}*sxS?MUalR`_4;gbXLc`%u9%g0gK0s=rP(VrsWin~5NP8oj8GhRKu1*e_o}&}5 z>zWQTsSU-s#sNKKB}o#2PM`3emU9}&_QI@qe#kc*q{<4K2QjlhyWT&P@GQiLZ!W-u zXMzRAnB1|*D~fBhXZA=dz#v^dzddQ3z{)#EOY$!|?LsX%UK_pPjajbhTg^)9 NqgQwK^yBT$ 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