upgrade to rust alpha 1.0.0
This commit is contained in:
16
Cargo.lock
generated
16
Cargo.lock
generated
@@ -2,27 +2,27 @@
|
|||||||
name = "super-matte-bros"
|
name = "super-matte-bros"
|
||||||
version = "0.0.1"
|
version = "0.0.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"sdl2 0.0.14 (git+https://github.com/AngryLawyer/rust-sdl2)",
|
"sdl2 0.0.16 (git+https://github.com/AngryLawyer/rust-sdl2)",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pkg-config"
|
name = "pkg-config"
|
||||||
version = "0.1.3"
|
version = "0.1.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "sdl2"
|
name = "sdl2"
|
||||||
version = "0.0.14"
|
version = "0.0.16"
|
||||||
source = "git+https://github.com/AngryLawyer/rust-sdl2#7ff85a93a677ed8a02f4e49109a8613a7544aa21"
|
source = "git+https://github.com/AngryLawyer/rust-sdl2#2dd0986162fa48fd930ab59c696c0eabc1ac2344"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"sdl2-sys 0.0.14 (git+https://github.com/AngryLawyer/rust-sdl2)",
|
"sdl2-sys 0.0.16 (git+https://github.com/AngryLawyer/rust-sdl2)",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "sdl2-sys"
|
name = "sdl2-sys"
|
||||||
version = "0.0.14"
|
version = "0.0.16"
|
||||||
source = "git+https://github.com/AngryLawyer/rust-sdl2#7ff85a93a677ed8a02f4e49109a8613a7544aa21"
|
source = "git+https://github.com/AngryLawyer/rust-sdl2#2dd0986162fa48fd930ab59c696c0eabc1ac2344"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"pkg-config 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
"pkg-config 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
56
src/keyboard.rs
Normal file
56
src/keyboard.rs
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use sdl2::keycode::KeyCode;
|
||||||
|
|
||||||
|
|
||||||
|
pub struct KeyboardHandler {
|
||||||
|
pressed_keys: HashMap<KeyCode, bool>,
|
||||||
|
released_keys: HashMap<KeyCode, bool>,
|
||||||
|
held_keys: HashMap<KeyCode, bool>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl KeyboardHandler {
|
||||||
|
pub fn new() -> KeyboardHandler {
|
||||||
|
KeyboardHandler {
|
||||||
|
pressed_keys: HashMap::new(),
|
||||||
|
released_keys: HashMap::new(),
|
||||||
|
held_keys: HashMap::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn clear(&mut self) {
|
||||||
|
self.pressed_keys.clear();
|
||||||
|
self.released_keys.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn key_down(&mut self, keycode: KeyCode) {
|
||||||
|
self.pressed_keys.insert(keycode, true);
|
||||||
|
self.held_keys.insert(keycode, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn key_up(&mut self, keycode: KeyCode) {
|
||||||
|
self.released_keys.insert(keycode, true);
|
||||||
|
self.held_keys.insert(keycode, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn was_pressed(&self, keycode: KeyCode) -> bool {
|
||||||
|
match self.pressed_keys.get(&keycode) {
|
||||||
|
Some(state) => *state,
|
||||||
|
None => false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn was_released(&self, keycode: KeyCode) -> bool {
|
||||||
|
match self.released_keys.get(&keycode) {
|
||||||
|
Some(state) => *state,
|
||||||
|
None => false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_held(&self, keycode: KeyCode) -> bool {
|
||||||
|
match self.held_keys.get(&keycode) {
|
||||||
|
Some(state) => *state,
|
||||||
|
None => false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
108
src/main.rs
108
src/main.rs
@@ -1,24 +1,24 @@
|
|||||||
extern crate sdl2;
|
extern crate sdl2;
|
||||||
|
|
||||||
|
use std::num::Float;
|
||||||
use std::num::FloatMath;
|
|
||||||
use std::iter::range_step_inclusive;
|
use std::iter::range_step_inclusive;
|
||||||
|
|
||||||
use sdl2::video::{Window, WindowPos, OPENGL};
|
use sdl2::video::{Window, WindowPos, OPENGL};
|
||||||
use sdl2::event::{poll_event, Event};
|
use sdl2::event::{poll_event, Event};
|
||||||
use sdl2::timer::{get_ticks, delay};
|
use sdl2::timer::{get_ticks, delay};
|
||||||
use sdl2::rect::Rect;
|
use sdl2::rect::{Point, Rect};
|
||||||
use sdl2::keycode::KeyCode;
|
use sdl2::keycode::KeyCode;
|
||||||
use sdl2::pixels::Color;
|
use sdl2::pixels::Color;
|
||||||
|
|
||||||
use tile::Layer;
|
use tile::Layer;
|
||||||
use camera::Camera;
|
use camera::Camera;
|
||||||
use player::Player;
|
use player::Player;
|
||||||
|
use keyboard::KeyboardHandler;
|
||||||
|
|
||||||
mod tile;
|
mod tile;
|
||||||
mod camera;
|
mod camera;
|
||||||
mod player;
|
mod player;
|
||||||
|
mod keyboard;
|
||||||
|
|
||||||
|
|
||||||
const SCREEN_WIDTH : i32 = 960;
|
const SCREEN_WIDTH : i32 = 960;
|
||||||
@@ -27,7 +27,7 @@ const SCREEN_HEIGHT : i32 = 640;
|
|||||||
const TILE_WIDTH : i32 = 32;
|
const TILE_WIDTH : i32 = 32;
|
||||||
const TILE_HEIGHT : i32 = 32;
|
const TILE_HEIGHT : i32 = 32;
|
||||||
|
|
||||||
const MS_PER_UPDATE : uint = 10;
|
const MS_PER_UPDATE : usize = 10;
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@@ -40,7 +40,7 @@ enum Tile {
|
|||||||
fn main() {
|
fn main() {
|
||||||
sdl2::init(sdl2::INIT_EVERYTHING);
|
sdl2::init(sdl2::INIT_EVERYTHING);
|
||||||
|
|
||||||
let window = match Window::new("Super Matte Bros", WindowPos::PosCentered, WindowPos::PosCentered, SCREEN_WIDTH as int, SCREEN_HEIGHT as int, OPENGL) {
|
let window = match Window::new("Super Matte Bros", WindowPos::PosCentered, WindowPos::PosCentered, SCREEN_WIDTH as isize, SCREEN_HEIGHT as isize, OPENGL) {
|
||||||
Ok(window) => window,
|
Ok(window) => window,
|
||||||
Err(err) => panic!("failed to create window: {}", err)
|
Err(err) => panic!("failed to create window: {}", err)
|
||||||
};
|
};
|
||||||
@@ -50,74 +50,97 @@ fn main() {
|
|||||||
Err(err) => panic!("failed to create renderer: {}", err)
|
Err(err) => panic!("failed to create renderer: {}", err)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let mut keyboard = KeyboardHandler::new();
|
||||||
|
|
||||||
let mut layer = Layer::new(120, 20, TILE_WIDTH, TILE_HEIGHT, Tile::Empty);
|
let mut layer = Layer::new(120, 20, TILE_WIDTH, TILE_HEIGHT, Tile::Empty);
|
||||||
|
|
||||||
let colors = vec![Color::RGB(0, 0, 255), Color::RGB(0, 128, 255)];
|
let colors = vec![Color::RGB(0, 0, 255), Color::RGB(0, 128, 255)];
|
||||||
|
|
||||||
for x in range(5, 120) {
|
for x in range(5, 120) {
|
||||||
layer.set_tile(x, 14, Tile::Floor(colors[(x % 2) as uint]));
|
layer.set_tile(x, 14, Tile::Floor(colors[(x % 2) as usize]));
|
||||||
}
|
}
|
||||||
for x in range(11, 20) {
|
for x in range(11, 20) {
|
||||||
layer.set_tile(x, 13, Tile::Floor(colors[(x % 2) as uint]));
|
layer.set_tile(x, 13, Tile::Floor(colors[(x % 2) as usize]));
|
||||||
|
}
|
||||||
|
for x in range(14, 20) {
|
||||||
|
layer.set_tile(x, 11, Tile::Floor(colors[(x % 2) as usize]));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut camera = Camera::new(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, layer.to_rect());
|
let mut camera = Camera::new(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, layer.to_rect());
|
||||||
|
|
||||||
let mut player = Player::new(290.0, 390.0);
|
let mut player = Player::new(290.0, 390.0);
|
||||||
|
|
||||||
let mut current : uint;
|
let mut current : usize;
|
||||||
let mut elapsed : uint;
|
let mut elapsed : usize;
|
||||||
let mut previous : uint = get_ticks();
|
let mut previous : usize = get_ticks();
|
||||||
let mut lag : uint = 0;
|
let mut lag : usize = 0;
|
||||||
|
|
||||||
loop {
|
'main : loop {
|
||||||
current = get_ticks();
|
current = get_ticks();
|
||||||
elapsed = current - previous;
|
elapsed = current - previous;
|
||||||
previous = current;
|
previous = current;
|
||||||
lag += elapsed;
|
lag += elapsed;
|
||||||
|
|
||||||
|
keyboard.clear();
|
||||||
|
|
||||||
|
'event : loop {
|
||||||
match poll_event() {
|
match poll_event() {
|
||||||
Event::Quit(_) => break,
|
Event::Quit(_) => break 'main,
|
||||||
Event::KeyDown(_, _, key, _, _, repeat) => {
|
Event::KeyDown(_, _, key, _, _, repeat) => {
|
||||||
if key == KeyCode::Escape {
|
keyboard.key_down(key);
|
||||||
break;
|
},
|
||||||
} else if key == KeyCode::Right {
|
Event::KeyUp(_, _, key, _, _, _) => {
|
||||||
|
keyboard.key_up(key);
|
||||||
|
},
|
||||||
|
Event::None => break 'event,
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if keyboard.was_pressed(KeyCode::Escape) {
|
||||||
|
break 'main;
|
||||||
|
}
|
||||||
|
|
||||||
|
if keyboard.is_held(KeyCode::Right) {
|
||||||
player.dx = 4.0;
|
player.dx = 4.0;
|
||||||
} else if key == KeyCode::Left {
|
}
|
||||||
|
|
||||||
|
if keyboard.is_held(KeyCode::Left) {
|
||||||
player.dx = -4.0;
|
player.dx = -4.0;
|
||||||
} else if key == KeyCode::Up {
|
}
|
||||||
if player.on_ground && repeat == false {
|
|
||||||
|
if keyboard.was_pressed(KeyCode::Up) {
|
||||||
|
if player.on_ground {
|
||||||
player.dy = -12.0;
|
player.dy = -12.0;
|
||||||
|
|
||||||
player.on_ground = false;
|
player.on_ground = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
|
||||||
Event::KeyUp(_, _, key, _, _, _) => {
|
if keyboard.was_released(KeyCode::Right) {
|
||||||
if key == KeyCode::Right {
|
|
||||||
if player.dx > 0.0 {
|
if player.dx > 0.0 {
|
||||||
player.dx = 0.0;
|
player.dx = 0.0;
|
||||||
}
|
}
|
||||||
} else if key == KeyCode::Left {
|
}
|
||||||
|
|
||||||
|
if keyboard.was_released(KeyCode::Left) {
|
||||||
if player.dx < 0.0 {
|
if player.dx < 0.0 {
|
||||||
player.dx = 0.0;
|
player.dx = 0.0;
|
||||||
}
|
}
|
||||||
} else if key == KeyCode::Up {
|
}
|
||||||
|
|
||||||
|
if keyboard.was_released(KeyCode::Up) {
|
||||||
if player.dy < -6.0 {
|
if player.dy < -6.0 {
|
||||||
player.dy = -6.0;
|
player.dy = -6.0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
|
|
||||||
while lag >= MS_PER_UPDATE {
|
while lag >= MS_PER_UPDATE {
|
||||||
player.update();
|
player.update();
|
||||||
|
|
||||||
player.on_ground = false;
|
player.on_ground = false;
|
||||||
|
|
||||||
let intersecting = layer.find_intersecting(&player.to_rect());
|
if let Some(intersecting) = layer.find_intersecting(&player.to_rect()) {
|
||||||
|
|
||||||
if player.dx > 0.0 {
|
if player.dx > 0.0 {
|
||||||
let p_x = player.x + player.w as f32;
|
let p_x = player.x + player.w as f32;
|
||||||
let t_x = intersecting.x + intersecting.w;
|
let t_x = intersecting.x + intersecting.w;
|
||||||
@@ -126,9 +149,9 @@ fn main() {
|
|||||||
|
|
||||||
for y in range_step_inclusive(intersecting.y, intersecting.y + intersecting.h, 1) {
|
for y in range_step_inclusive(intersecting.y, intersecting.y + intersecting.h, 1) {
|
||||||
for x in range_step_inclusive(t_x, t_x + 1, 1) {
|
for x in range_step_inclusive(t_x, t_x + 1, 1) {
|
||||||
match *layer.get_tile(x, y) {
|
d_x = match *layer.get_tile(x, y) {
|
||||||
Tile::Empty => (),
|
Tile::Empty => d_x,
|
||||||
Tile::Floor(_) => d_x = d_x.min((x * TILE_WIDTH) as f32 - p_x)
|
Tile::Floor(_) => d_x.min((x * TILE_WIDTH) as f32 - p_x)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -146,9 +169,9 @@ fn main() {
|
|||||||
|
|
||||||
for y in range_step_inclusive(intersecting.y, intersecting.y + intersecting.h, 1) {
|
for y in range_step_inclusive(intersecting.y, intersecting.y + intersecting.h, 1) {
|
||||||
for x in range_step_inclusive(t_x, t_x - 1, -1) {
|
for x in range_step_inclusive(t_x, t_x - 1, -1) {
|
||||||
match *layer.get_tile(x, y) {
|
d_x = match *layer.get_tile(x, y) {
|
||||||
Tile::Empty => (),
|
Tile::Empty => d_x,
|
||||||
Tile::Floor(_) => d_x = d_x.max((x * TILE_WIDTH + TILE_WIDTH) as f32 - p_x)
|
Tile::Floor(_) => d_x.max((x * TILE_WIDTH + TILE_WIDTH) as f32 - p_x)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -168,9 +191,9 @@ fn main() {
|
|||||||
|
|
||||||
for y in range_step_inclusive(t_y, t_y + 1, 1) {
|
for y in range_step_inclusive(t_y, t_y + 1, 1) {
|
||||||
for x in range_step_inclusive(intersecting.x, intersecting.x + intersecting.w, 1) {
|
for x in range_step_inclusive(intersecting.x, intersecting.x + intersecting.w, 1) {
|
||||||
match *layer.get_tile(x, y) {
|
d_y = match *layer.get_tile(x, y) {
|
||||||
Tile::Empty => (),
|
Tile::Empty => d_y,
|
||||||
Tile::Floor(_) => d_y = d_y.min((y * TILE_HEIGHT) as f32 - p_y)
|
Tile::Floor(_) => d_y.min((y * TILE_HEIGHT) as f32 - p_y)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -190,9 +213,9 @@ fn main() {
|
|||||||
|
|
||||||
for y in range_step_inclusive(t_y, t_y - 1, -1) {
|
for y in range_step_inclusive(t_y, t_y - 1, -1) {
|
||||||
for x in range_step_inclusive(intersecting.x, intersecting.x + intersecting.w, 1) {
|
for x in range_step_inclusive(intersecting.x, intersecting.x + intersecting.w, 1) {
|
||||||
match *layer.get_tile(x, y) {
|
d_y = match *layer.get_tile(x, y) {
|
||||||
Tile::Empty => (),
|
Tile::Empty => d_y,
|
||||||
Tile::Floor(_) => d_y = d_y.max((y * TILE_HEIGHT + TILE_HEIGHT) as f32 - p_y)
|
Tile::Floor(_) => d_y.max((y * TILE_HEIGHT + TILE_HEIGHT) as f32 - p_y)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -203,6 +226,7 @@ fn main() {
|
|||||||
player.dy = 0.0;
|
player.dy = 0.0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
camera.center(&player.to_rect());
|
camera.center(&player.to_rect());
|
||||||
|
|
||||||
|
|||||||
35
src/tile.rs
35
src/tile.rs
@@ -1,7 +1,7 @@
|
|||||||
use std::iter::repeat;
|
use std::iter::repeat;
|
||||||
use std::cmp::{min, max};
|
use std::cmp::{min, max};
|
||||||
|
|
||||||
use sdl2::rect::Rect;
|
use sdl2::rect::{Point, Rect};
|
||||||
|
|
||||||
|
|
||||||
pub struct Layer<T> {
|
pub struct Layer<T> {
|
||||||
@@ -15,7 +15,7 @@ pub struct Layer<T> {
|
|||||||
impl<T> Layer<T> where T: Clone {
|
impl<T> Layer<T> where T: Clone {
|
||||||
pub fn new(width: i32, height: i32, tile_width: i32, tile_height: i32, tile: T) -> Layer<T> {
|
pub fn new(width: i32, height: i32, tile_width: i32, tile_height: i32, tile: T) -> Layer<T> {
|
||||||
Layer {
|
Layer {
|
||||||
tiles: repeat(tile).take((width * height) as uint).collect(),
|
tiles: repeat(tile).take((width * height) as usize).collect(),
|
||||||
width: width,
|
width: width,
|
||||||
height: height,
|
height: height,
|
||||||
tile_width: tile_width,
|
tile_width: tile_width,
|
||||||
@@ -24,36 +24,38 @@ impl<T> Layer<T> where T: Clone {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_tile(&self, x: i32, y: i32) -> &T {
|
pub fn get_tile(&self, x: i32, y: i32) -> &T {
|
||||||
let offset = (x + y * self.width) as uint;
|
let offset = (x + y * self.width) as usize;
|
||||||
|
|
||||||
&self.tiles[offset]
|
&self.tiles[offset]
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_tile(&mut self, x: i32, y: i32, tile: T) {
|
pub fn set_tile(&mut self, x: i32, y: i32, tile: T) {
|
||||||
let offset = (x + y * self.width) as uint;
|
let offset = (x + y * self.width) as usize;
|
||||||
|
|
||||||
self.tiles[offset] = tile;
|
self.tiles[offset] = tile;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn find_intersecting(&self, rect: &Rect) -> Rect {
|
pub fn find_intersecting(&self, rect: &Rect) -> Option<Rect> {
|
||||||
let x1 = max(rect.x / self.tile_width, 0);
|
let x1 = max(rect.x / self.tile_width, 0);
|
||||||
let y1 = max(rect.y / self.tile_height, 0);
|
let y1 = max(rect.y / self.tile_height, 0);
|
||||||
let x2 = min((rect.x + rect.w - 1) / self.tile_width, self.width - 1);
|
let x2 = min((rect.x + rect.w - 1) / self.tile_width, self.width - 1);
|
||||||
let y2 = min((rect.y + rect.h - 1) / self.tile_height, self.height - 1);
|
let y2 = min((rect.y + rect.h - 1) / self.tile_height, self.height - 1);
|
||||||
|
|
||||||
Rect::new(x1, y1, x2 - x1, y2 - y1)
|
println!("{}, {}, {}, {}", x1, y1, x2, y2);
|
||||||
}
|
|
||||||
|
if x1 < 0 || x2 >= self.width {
|
||||||
pub fn for_each_intersecting<F>(&self, rect: &Rect, mut f: F) where F: FnMut(&T, &Rect) {
|
None
|
||||||
let intersect = self.find_intersecting(rect);
|
}
|
||||||
|
else if y1 < 0 || y2 >= self.height {
|
||||||
if intersect.x < 0 || intersect.x + intersect.w > self.width {
|
None
|
||||||
return;
|
}
|
||||||
}
|
else {
|
||||||
else if intersect.y < 0 || intersect.y + intersect.h > self.height {
|
Some(Rect::new(x1, y1, x2 - x1, y2 - y1))
|
||||||
return;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn for_each_intersecting<F: FnMut(&T, &Rect)>(&self, rect: &Rect, mut f: F) {
|
||||||
|
if let Some(intersect) = self.find_intersecting(rect) {
|
||||||
for y in range(intersect.y, intersect.y + intersect.h + 1) {
|
for y in range(intersect.y, intersect.y + intersect.h + 1) {
|
||||||
for x in range(intersect.x, intersect.x + intersect.w + 1) {
|
for x in range(intersect.x, intersect.x + intersect.w + 1) {
|
||||||
let position = Rect::new(x * self.tile_width, y * self.tile_height, self.tile_width, self.tile_height);
|
let position = Rect::new(x * self.tile_width, y * self.tile_height, self.tile_width, self.tile_height);
|
||||||
@@ -62,6 +64,7 @@ impl<T> Layer<T> where T: Clone {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn to_rect(&self) -> Rect {
|
pub fn to_rect(&self) -> Rect {
|
||||||
Rect::new(0, 0, self.width * self.tile_width, self.height * self.tile_height)
|
Rect::new(0, 0, self.width * self.tile_width, self.height * self.tile_height)
|
||||||
|
|||||||
Reference in New Issue
Block a user