diff --git a/Cargo.toml b/Cargo.toml index ae8d8e1..8dcf37c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,20 +9,15 @@ authors = ["coeuvre "] name = "rust-2048" path = "src/main.rs" -[dependencies.graphics] - -git = "https://github.com/pistondevelopers/rust-graphics" +[dependencies.piston2d-graphics] +git = "https://github.com/pistondevelopers/graphics" [dependencies.piston] +git = "https://github.com/pistondevelopers/piston" -git = "https://github.com/pistondevelopers/piston/" - -[dependencies.opengl_graphics] - +[dependencies.piston2d-opengl_graphics] git = "https://github.com/pistondevelopers/opengl_graphics" -[dependencies.sdl2_window] - +[dependencies.pistoncore-sdl2_window] git = "https://github.com/pistondevelopers/sdl2_window" - diff --git a/src/app.rs b/src/app.rs index 0fb3521..cdeb2fb 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,6 +1,6 @@ - use graphics::*; use piston::*; +use piston::events::{ RenderArgs, UpdateArgs }; use board::Board; use number_renderer::NumberRenderer; @@ -18,7 +18,7 @@ pub struct App<'a> { logo: Option, comment1: Option, comment2: Option, - window_background_color: [f32, ..4], + window_background_color: [f32; ..4], gl: Gl, } @@ -80,7 +80,7 @@ impl<'a> App<'a> { } pub fn load(&mut self) { - let asset_store = AssetStore::from_folder(self.settings.asset_folder.as_slice()); + let asset_store = Path::new(self.settings.asset_folder.as_slice()); self.number_renderer = Some(NumberRenderer::new(&asset_store)); self.logo = Some(Texture::from_path(&asset_store.path("logo.png").unwrap()).unwrap()); @@ -102,19 +102,26 @@ impl<'a> App<'a> { } pub fn key_press(&mut self, args: &KeyPressArgs) { - if args.key == keyboard::Left { + use piston::input::Button::Keyboard; + use piston::input::keyboard::Key; + + if args.key == Keyboard(Key::Left) { self.board.merge_from_right_to_left(); } - if args.key == keyboard::Right { + + if args.key == Keyboard(Key::Right) { self.board.merge_from_left_to_right(); } - if args.key == keyboard::Up { + + if args.key == Keyboard(Key::Up) { self.board.merge_from_bottom_to_top(); } - if args.key == keyboard::Down { + + if args.key == Keyboard(Key::Down) { self.board.merge_from_top_to_bottom(); } - if args.key == keyboard::Space { + + if args.key == Keyboard(Key::Space) { self.board = Board::new(self.settings); } } diff --git a/src/board.rs b/src/board.rs index 1c3a03b..8af8f20 100644 --- a/src/board.rs +++ b/src/board.rs @@ -1,6 +1,6 @@ use std::iter::range_step; -use std::collections::hashmap::HashSet; +use std::collections::HashSet; use std::rand::random; use graphics::*; use piston::*; @@ -11,7 +11,7 @@ use number_renderer::NumberRenderer; use settings::Settings; use tile::{ Tile, - TileStatic, + TileState, }; pub struct Board<'a> { @@ -61,12 +61,12 @@ impl<'a> Board<'a> { let mut tiles_need_added = Vec::::new(); for i in range(0, self.tiles.len()) { let tile1 = &self.tiles[i]; - if tile1.status != TileStatic { + if tile1.status != TileState::TileStatic { continue; } for j in range(i + 1, self.tiles.len()) { let tile2 = &self.tiles[j]; - if tile2.status != TileStatic + if tile2.status != TileState::TileStatic || tile1.tile_x != tile2.tile_x || tile1.tile_y != tile2.tile_y { continue; @@ -277,7 +277,7 @@ impl<'a> Board<'a> { fn is_locking(&self) -> bool { for tile in self.tiles.iter() { - if tile.status != TileStatic { + if tile.status != TileState::TileStatic { return true; } } diff --git a/src/main.rs b/src/main.rs index 1ba8bf3..58b5043 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,10 +6,10 @@ extern crate serialize; extern crate graphics; extern crate piston; extern crate opengl_graphics; -extern crate sdl2_game_window; +extern crate sdl2_window; use piston::*; -use sdl2_game_window::GameWindowSDL2; +use sdl2_window::Sdl2Window; mod app; mod board; @@ -20,8 +20,8 @@ mod tile; fn main() { let settings = settings::Settings::load(); - let mut window = GameWindowSDL2::new( - GameWindowSettings { + let mut window = Sdl2Window::new( + Sdl2Window { title: "Rust-2048".to_string(), size: settings.window_size, fullscreen: false, @@ -33,12 +33,32 @@ fn main() { app.load(); + /* let game_iter_settings = GameIteratorSettings { updates_per_second: 120, max_frames_per_second: 60, }; + */ + for e in piston::events(&window) { + use piston::event::{ RenderEvent, PressEvent }; + + if let Some(args) = e.render_args() { + app.render(args); + } + + if let Some(args) = e.update_args() { + app.update(args); + } + + if let Some(args) = e.press_args() { + app.key_press(args); + } + } + + /* for e in GameIterator::new(&mut window, &game_iter_settings) { + match e { Render(ref args) => { app.render(args); @@ -52,5 +72,6 @@ fn main() { _ => {}, } } + */ } diff --git a/src/number_renderer.rs b/src/number_renderer.rs index 6bf149d..f0b6a5c 100644 --- a/src/number_renderer.rs +++ b/src/number_renderer.rs @@ -1,8 +1,5 @@ use graphics::*; -use piston::{ - AssetStore, -}; use opengl_graphics::{ Gl, Texture, @@ -16,14 +13,14 @@ pub struct NumberRenderer { } impl NumberRenderer { - pub fn new(asset_store: &AssetStore) -> NumberRenderer { + pub fn new() -> NumberRenderer { NumberRenderer { - image: Texture::from_path(&asset_store.path("digits.png").unwrap()).unwrap(), + image: Texture::from_path(Path::new("digits.png")).unwrap(), } } pub fn render(&self, number: u32, center_x: f64, center_y: f64, max_width: f64, - color: [f32, ..3], c: &Context, gl: &mut Gl) { + color: [f32; ..3], c: &Context, gl: &mut Gl) { let digits = number_to_digits(number); let total_width = DIGITS_WIDTH * digits.len() as f64; let total_width = if total_width > max_width { diff --git a/src/settings.rs b/src/settings.rs index 8ac25ca..b52057b 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -1,7 +1,7 @@ use std::os::self_exe_path; -use std::io::{BufferedWriter, BufferedReader}; -use std::io::fs::File; +use std::old_io::{BufferedWriter, BufferedReader}; +use std::fs::File; use serialize::{ json, Encodable, @@ -12,29 +12,29 @@ static SETTING_FILENAME: &'static str = "settings.json"; pub struct Settings { pub asset_folder: String, - pub window_size: [u32, ..2], - pub window_background_color: [f32, ..3], + pub window_size: [u32; ..2], + pub window_background_color: [f32; ..3], pub comment1_offset_y: f64, pub comment2_offset_y: f64, pub board_padding: f64, - pub board_size: [f64, ..2], + pub board_size: [f64; ..2], pub board_offset_y: f64, pub tile_width: int, pub tile_height: int, pub tile_size: f64, pub tile_padding: f64, - pub tile_background_color: [f32, ..3], - pub tiles_colors: Vec<[f32, ..3]>, - pub tile_unknow_color: [f32, ..3], + pub tile_background_color: [f32; ..3], + pub tiles_colors: Vec<[f32; ..3]>, + pub tile_unknow_color: [f32; ..3], pub tile_move_time: f64, pub tile_new_time: f64, pub tile_combine_time: f64, - pub best_rect: [f64, ..4], - pub score_rect: [f64, ..4], - pub label_color: [f32, ..3], - pub button_color: [f32, ..3], - pub text_dark_color: [f32, ..3], - pub text_light_color: [f32, ..3], + pub best_rect: [f64; ..4], + pub score_rect: [f64; ..4], + pub label_color: [f32; ..3], + pub button_color: [f32; ..3], + pub text_dark_color: [f32; ..3], + pub text_light_color: [f32; ..3], } impl Settings { @@ -49,7 +49,7 @@ impl Settings { ]; - let mut tiles_colors = Vec::<[f32, ..3]>::new(); + let mut tiles_colors = Vec::<[f32; ..3]>::new(); for color in s.tiles_colors.iter() { tiles_colors.push([ color[0] / 255.0, @@ -127,7 +127,7 @@ impl Settings { } } -#[deriving(Encodable, Decodable)] +#[derive(Encodable, Decodable)] struct SettingsInJson { asset_folder: String, diff --git a/src/tile.rs b/src/tile.rs index fbc3800..eb9d66d 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -5,7 +5,7 @@ use opengl_graphics::Gl; use number_renderer::NumberRenderer; use settings::Settings; -#[deriving(Clone, PartialEq)] +#[derive(Clone, PartialEq)] pub enum TileState { TileStatic, /// (t, x, y, origin_x, origin_x) @@ -16,7 +16,7 @@ pub enum TileState { TileCombine(f64, f64), } -#[deriving(Clone)] +#[derive(Clone)] pub struct Tile<'a> { pub score: int, pub tile_x: int, @@ -32,7 +32,7 @@ impl<'a> Tile<'a> { score: score, tile_x: tile_x, tile_y: tile_y, - status: TileNew(settings.tile_new_time, 0.0), + status: TileState::TileNew(settings.tile_new_time, 0.0), settings: settings, } @@ -43,7 +43,7 @@ impl<'a> Tile<'a> { score: score, tile_x: tile_x, tile_y: tile_y, - status: TileCombine(settings.tile_combine_time, 1.2 * settings.tile_size), + status: TileState::TileCombine(settings.tile_combine_time, 1.2 * settings.tile_size), settings: settings, } @@ -57,15 +57,15 @@ impl<'a> Tile<'a> { pub fn start_moving(&mut self, destination_tile_x: int, destination_tile_y: int) { match self.status { - TileMoving(_, _, _, ox, oy) => { + TileState::TileMoving(_, _, _, ox, oy) => { let (x, y) = self.tile_to_pos(ox, oy); - self.status = TileMoving(self.settings.tile_move_time, x, y, ox, oy); + self.status = TileState::TileMoving(self.settings.tile_move_time, x, y, ox, oy); self.tile_x = destination_tile_x; self.tile_y = destination_tile_y; }, - TileStatic => { + TileState::TileStatic => { let (x, y) = self.tile_to_pos(self.tile_x, self.tile_y); - self.status = TileMoving(self.settings.tile_move_time, x, y, self.tile_x, self.tile_y); + self.status = TileState::TileMoving(self.settings.tile_move_time, x, y, self.tile_x, self.tile_y); self.tile_x = destination_tile_x; self.tile_y = destination_tile_y; }, @@ -75,29 +75,29 @@ impl<'a> Tile<'a> { pub fn update(&mut self, dt: f64) { match self.status { - TileMoving(t, x, y, ox, oy) => { + TileState::TileMoving(t, x, y, ox, oy) => { if dt >= t { - self.status = TileStatic; + self.status = TileState::TileStatic; } else { let (dx, dy) = self.tile_to_pos(self.tile_x, self.tile_y); let factor = dt / t; - self.status = TileMoving(t - dt, x + factor * (dx - x), y + factor * (dy - y), ox, oy); + self.status = TileState::TileMoving(t - dt, x + factor * (dx - x), y + factor * (dy - y), ox, oy); } }, - TileNew(t, size) => { + TileState::TileNew(t, size) => { if dt >= t { - self.status = TileStatic; + self.status = TileState::TileStatic; } else { let factor = dt / t; - self.status = TileNew(t - dt, size + factor * (self.settings.tile_size - size)); + self.status = TileState::TileNew(t - dt, size + factor * (self.settings.tile_size - size)); } }, - TileCombine(t, size) => { + TileState::TileCombine(t, size) => { if dt >= t { - self.status = TileStatic; + self.status = TileState::TileStatic; } else { let factor = dt / t; - self.status = TileCombine(t - dt, size + factor * (self.settings.tile_size - size)); + self.status = TileState::TileCombine(t - dt, size + factor * (self.settings.tile_size - size)); } }, _ => {}, @@ -108,13 +108,13 @@ impl<'a> Tile<'a> { let mut pos = self.tile_to_pos(self.tile_x, self.tile_y); let mut size = (self.settings.tile_size, self.settings.tile_size); match self.status { - TileMoving(_, x, y, _, _) => { + TileState::TileMoving(_, x, y, _, _) => { pos = (x, y); }, - TileNew(_, s) => { + TileState::TileNew(_, s) => { size = (s, s); }, - TileCombine(_, s) => { + TileState::TileCombine(_, s) => { size = (s, s); }, _ => {}, @@ -136,7 +136,7 @@ impl<'a> Tile<'a> { number_renderer.render(self.score as u32, x + self.settings.tile_size / 2.0, y + self.settings.tile_size / 2.0, self.settings.tile_size, color, c, gl); } - fn get_color(&self) -> [f32, ..3] { + fn get_color(&self) -> [f32; ..3] { let i = (self.score as f64).log2() as uint; if i > 0 && i < self.settings.tiles_colors.len() { self.settings.tiles_colors[i]