From 4faf3b05aa8e0f552fced83181f953c7648de70f Mon Sep 17 00:00:00 2001 From: Pavel Potocek Date: Thu, 9 Apr 2015 17:04:19 +0200 Subject: [PATCH] Upgraded to latest Rust Nightly --- Cargo.toml | 5 +- src/app.rs | 124 ++++++++++++++++++++++------------------- src/board.rs | 107 +++++++++++++++++------------------ src/main.rs | 36 ++++++------ src/number_renderer.rs | 18 +++--- src/settings.rs | 90 +++++++++++++++++------------- src/tile.rs | 35 ++++++------ 7 files changed, 220 insertions(+), 195 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8dcf37c..e25007b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,10 @@ authors = ["coeuvre "] name = "rust-2048" path = "src/main.rs" +[dependencies] +rustc-serialize = "0.3" +rand = "0.3.7" + [dependencies.piston2d-graphics] git = "https://github.com/pistondevelopers/graphics" @@ -20,4 +24,3 @@ git = "https://github.com/pistondevelopers/opengl_graphics" [dependencies.pistoncore-sdl2_window] git = "https://github.com/pistondevelopers/sdl2_window" - diff --git a/src/app.rs b/src/app.rs index cdeb2fb..9d391de 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,15 +1,18 @@ +use std::path::{Path, PathBuf}; + use graphics::*; -use piston::*; -use piston::events::{ RenderArgs, UpdateArgs }; +use piston::input::Button; +use piston::event::*; use board::Board; use number_renderer::NumberRenderer; use settings::Settings; use opengl_graphics::{ - Gl, + GlGraphics, Texture, }; + pub struct App<'a> { board: Board<'a>, number_renderer: Option, @@ -18,11 +21,11 @@ pub struct App<'a> { logo: Option, comment1: Option, comment2: Option, - window_background_color: [f32; ..4], - - gl: Gl, + window_background_color: [f32; 4], } +fn rgb2rgba(c: [f32; 3]) -> [f32; 4] { [c[0], c[1], c[2], 1.0] } + impl<'a> App<'a> { pub fn new(settings: &'a Settings) -> App<'a> { App { @@ -34,96 +37,103 @@ impl<'a> App<'a> { comment1: None, comment2: None, window_background_color: [1.0, 1.0, 1.0, 1.0], - - gl: Gl::new(), } } - fn render_ui(&mut self, c: &Context) { - // logo - c.trans(self.settings.board_padding, self.settings.board_padding) - .image(self.logo.get_ref()) - .rgb(self.settings.text_dark_color[0], - self.settings.text_dark_color[1], - self.settings.text_dark_color[2]) - .draw(&mut self.gl); + fn render_ui(&self, c: &Context, gl: &mut GlGraphics) { + use graphics::*; - c.view() - .rect(self.settings.best_rect[0], - self.settings.best_rect[1], - self.settings.best_rect[2], - self.settings.best_rect[3]) - .rgba(self.settings.label_color[0], - self.settings.label_color[1], - self.settings.label_color[2], - 1.0) - .draw(&mut self.gl); + Image::new_colored(rgb2rgba(self.settings.text_dark_color)) + .draw(self.logo.iter().next().unwrap(), + default_draw_state(), + c.trans(self.settings.board_padding,self.settings.board_padding).transform, + gl); + + Rectangle::new(rgb2rgba(self.settings.label_color)) + .draw(self.settings.best_rect, + default_draw_state(), + c.transform, + gl); let comment1_offset_y = self.settings.comment1_offset_y; let comment1 = self.comment1.as_ref().unwrap(); - App::render_comment(self.settings, comment1, comment1_offset_y, c, &mut self.gl); + App::render_comment(self.settings, comment1, comment1_offset_y, c, gl); let comment2_offset_y = self.settings.comment2_offset_y; let comment2 = self.comment2.as_ref().unwrap(); - App::render_comment(self.settings, comment2, comment2_offset_y, c, &mut self.gl); + App::render_comment(self.settings, comment2, comment2_offset_y, c, gl); } - fn render_comment(settings: &Settings, comment: &Texture, y: f64, c: &Context, gl: &mut Gl) { + fn render_comment(settings: &Settings, comment: &Texture, y: f64, c: &Context, gl: &mut GlGraphics) { let (width, height) = comment.get_size(); let w = settings.window_size[0] as f64 - 2.0 * settings.board_padding; let h = height as f64 * w / width as f64; - c.rect(settings.board_padding, y, w, h) - .image(comment) - .rgb(settings.text_dark_color[0], - settings.text_dark_color[1], - settings.text_dark_color[2]) - .draw(gl); + + Image::new_colored(rgb2rgba(settings.text_dark_color)) + .rect([settings.board_padding, y, w, h]) + .draw( comment, + default_draw_state(), + c.transform, + gl); } pub fn load(&mut self) { - let asset_store = Path::new(self.settings.asset_folder.as_slice()); - self.number_renderer = Some(NumberRenderer::new(&asset_store)); + let mut asset_root = PathBuf::new(); + asset_root.push(Path::new(&self.settings.asset_folder)); - self.logo = Some(Texture::from_path(&asset_store.path("logo.png").unwrap()).unwrap()); - self.comment1 = Some(Texture::from_path(&asset_store.path("comment1.png").unwrap()).unwrap()); - self.comment2 = Some(Texture::from_path(&asset_store.path("comment2.png").unwrap()).unwrap()); + let mut logo_path = asset_root.clone(); + logo_path.push(Path::new("logo.png")); + let mut comment1_path = asset_root.clone(); + comment1_path.push(Path::new("comment1.png")); + let mut comment2_path = asset_root.clone(); + comment2_path.push(Path::new("comment2.png")); + + self.number_renderer = Some(NumberRenderer::new()); + self.logo = Some(Texture::from_path(&logo_path).unwrap()); + self.comment1 = Some(Texture::from_path(&comment1_path).unwrap()); + self.comment2 = Some(Texture::from_path(&comment2_path).unwrap()); } - pub fn render(&mut self, args: &RenderArgs) { - self.gl.viewport(0, 0, args.width as i32, args.height as i32); - let ref c = Context::abs(args.width as f64, args.height as f64); - c.color(self.window_background_color).draw(&mut self.gl); + pub fn render(&mut self, args: &RenderArgs, gl: &mut GlGraphics) { + + let ref c = Context::abs(args.width as f64, args.height as f64); + + let w_bg_col = self.window_background_color; + let ref nr = self.number_renderer; + + gl.draw([0,0,args.width as i32, args.height as i32], |_, gl| { + clear(w_bg_col, gl); + self.render_ui(c, gl); + self.board.render(nr.iter().next().unwrap(), c, gl); + }); - self.render_ui(c); - self.board.render(self.number_renderer.get_ref(), c, &mut self.gl); } pub fn update(&mut self, args: &UpdateArgs) { self.board.update(args.dt); } - pub fn key_press(&mut self, args: &KeyPressArgs) { + pub fn key_press(&mut self, args: &Button) { use piston::input::Button::Keyboard; use piston::input::keyboard::Key; - if args.key == Keyboard(Key::Left) { + if *args == Keyboard(Key::Left) { self.board.merge_from_right_to_left(); } - - if args.key == Keyboard(Key::Right) { + + if *args == Keyboard(Key::Right) { self.board.merge_from_left_to_right(); } - - if args.key == Keyboard(Key::Up) { + + if *args == Keyboard(Key::Up) { self.board.merge_from_bottom_to_top(); } - - if args.key == Keyboard(Key::Down) { + + if *args == Keyboard(Key::Down) { self.board.merge_from_top_to_bottom(); } - - if args.key == Keyboard(Key::Space) { + + if *args == Keyboard(Key::Space) { self.board = Board::new(self.settings); } } } - diff --git a/src/board.rs b/src/board.rs index 8af8f20..18ceb08 100644 --- a/src/board.rs +++ b/src/board.rs @@ -1,9 +1,8 @@ -use std::iter::range_step; use std::collections::HashSet; -use std::rand::random; +use std::iter::range_step_inclusive; +use rand::random; use graphics::*; -use piston::*; use opengl_graphics::{ Gl, }; @@ -14,9 +13,11 @@ use tile::{ TileState, }; +fn rgb2rgba(c: [f32; 3]) -> [f32; 4] { [c[0], c[1], c[2], 1.0] } + pub struct Board<'a> { tiles: Vec>, - score: int, + score: i32, settings: &'a Settings, } @@ -34,13 +35,13 @@ impl<'a> Board<'a> { } pub fn generate_tile(&mut self) { - if self.tiles.len() == (self.settings.tile_width * self.settings.tile_height) as uint { + if self.tiles.len() == (self.settings.tile_width * self.settings.tile_height) as usize { return; } loop { - let x = (random::() % self.settings.tile_width as uint) as int; - let y = (random::() % self.settings.tile_height as uint) as int; + let x = (random::() % self.settings.tile_width as u32) as i32; + let y = (random::() % self.settings.tile_height as u32) as i32; if self.get_tile(x, y).is_none() { self.tiles.push(Tile::new(self.settings, 2, x, y)); break; @@ -49,7 +50,7 @@ impl<'a> Board<'a> { } pub fn update(&mut self, dt: f64) { - for tile in self.tiles.mut_iter() { + for tile in self.tiles.iter_mut() { tile.update(dt); } @@ -57,14 +58,14 @@ impl<'a> Board<'a> { return; } let mut score_to_added = 0; - let mut tiles_need_removed = HashSet::::new(); + let mut tiles_need_removed = HashSet::::new(); let mut tiles_need_added = Vec::::new(); - for i in range(0, self.tiles.len()) { + for i in 0..self.tiles.len() { let tile1 = &self.tiles[i]; if tile1.status != TileState::TileStatic { continue; } - for j in range(i + 1, self.tiles.len()) { + for j in i+1..self.tiles.len() { let tile2 = &self.tiles[j]; if tile2.status != TileState::TileStatic || tile1.tile_x != tile2.tile_x @@ -82,17 +83,18 @@ impl<'a> Board<'a> { if tiles_need_removed.len() > 0 { let mut tiles = Vec::::new(); - for i in range(0, self.tiles.len()) { + for i in 0..self.tiles.len() { if !tiles_need_removed.contains(&i) { - tiles.push(self.tiles[i]); + tiles.push(self.tiles[i].clone()); } } - self.tiles = tiles.append(tiles_need_added.as_slice()); + tiles.append(&mut tiles_need_added); + self.tiles = tiles; self.add_score(score_to_added); } } - pub fn render(&mut self, number_renderer: &NumberRenderer, c: &Context, gl: &mut Gl) { + pub fn render(&self, number_renderer: &NumberRenderer, c: &Context, gl: &mut Gl) { number_renderer.render( self.score as u32, self.settings.best_rect[0] + self.settings.best_rect[2] / 2.0, @@ -114,7 +116,7 @@ impl<'a> Board<'a> { self.merge_col(height - 1, -1, -1); } - fn merge_col(&mut self, y_start: int, y_end: int, y_step: int) { + fn merge_col(&mut self, y_start: i32, y_end: i32, y_step: i32) { if self.is_locking() { return; } @@ -122,8 +124,8 @@ impl<'a> Board<'a> { let mut need_generate = false; loop { // move all tiles to right place - for col in range(0, self.settings.tile_width) { - for row in range_step(y_start, y_end, y_step) { + for col in 0..self.settings.tile_width { + for row in range_step_inclusive(y_start, y_end, y_step) { match self.get_mut_tile(col, row) { None => { match self.get_mut_next_tile(col, row, 0, y_step) { @@ -142,13 +144,13 @@ impl<'a> Board<'a> { } let mut did_merged = false; - for col in range(0, self.settings.tile_width) { + for col in 0..self.settings.tile_width { let mut found = false; let mut sx = 0; let mut sy = 0; let mut dx = 0; let mut dy = 0; - for row in range_step(y_start, y_end, y_step) { + for row in range_step_inclusive(y_start, y_end, y_step) { match self.get_tile(col, row) { Some(ref d_tile) => { match self.get_next_tile(col, row, 0, y_step) { @@ -173,8 +175,7 @@ impl<'a> Board<'a> { if found { need_generate = true; did_merged = true; - let mut tile = self.get_mut_tile(sx, sy); - let tile = tile.get_mut_ref(); + let tile = self.get_mut_tile(sx, sy).unwrap(); tile.start_moving(dx, dy); println!("merge ({}, {}) to ({}, {})", sx, sy, dx, dy); } @@ -199,7 +200,7 @@ impl<'a> Board<'a> { self.merge_row(0, width, 1); } - fn merge_row(&mut self, x_start: int, x_end: int, x_step: int) { + fn merge_row(&mut self, x_start: i32, x_end: i32, x_step: i32) { if self.is_locking() { return; } @@ -207,8 +208,8 @@ impl<'a> Board<'a> { let mut need_generate = false; loop { // move all tiles to right place - for row in range(0, self.settings.tile_height) { - for col in range_step(x_start, x_end, x_step) { + for row in 0..self.settings.tile_height { + for col in range_step_inclusive(x_start, x_end, x_step) { match self.get_mut_tile(col, row) { None => { match self.get_mut_next_tile(col, row, x_step, 0) { @@ -227,13 +228,13 @@ impl<'a> Board<'a> { // merge let mut did_merged = false; - for row in range(0, self.settings.tile_height) { + for row in 0..self.settings.tile_height { let mut found = false; let mut sx = 0; let mut sy = 0; let mut dx = 0; let mut dy = 0; - for col in range_step(x_start, x_end, x_step) { + for col in range_step_inclusive(x_start, x_end, x_step) { match self.get_tile(col, row) { Some(ref d_tile) => { match self.get_next_tile(col, row, x_step, 0) { @@ -258,8 +259,7 @@ impl<'a> Board<'a> { if found { need_generate = true; did_merged = true; - let mut tile = self.get_mut_tile(sx, sy); - let tile = tile.get_mut_ref(); + let tile = self.get_mut_tile(sx, sy).unwrap(); tile.start_moving(dx, dy); println!("merge ({}, {}) to ({}, {})", sx, sy, dx, dy); } @@ -285,7 +285,7 @@ impl<'a> Board<'a> { } /// Returns next tile right besides (x, y) - fn get_next_tile<'b>(&'b self, x: int, y: int, step_x: int, step_y: int) -> Option<&'b Tile<'a>> { + fn get_next_tile<'b>(&'b self, x: i32, y: i32, step_x: i32, step_y: i32) -> Option<&'b Tile<'a>> { let mut x = x + step_x; let mut y = y + step_y; while x >= 0 && x < self.settings.tile_width @@ -300,7 +300,7 @@ impl<'a> Board<'a> { None } - fn get_mut_next_tile<'b>(&'b mut self, x: int, y: int, step_x: int, step_y: int) -> Option<&'b mut Tile<'a>> { + fn get_mut_next_tile<'b>(&'b mut self, x: i32, y: i32, step_x: i32, step_y: i32) -> Option<&'b mut Tile<'a>> { let mut x = x + step_x; let mut y = y + step_y; let mut found = false; @@ -322,7 +322,7 @@ impl<'a> Board<'a> { } } - fn get_tile<'b>(&'b self, x: int, y: int) -> Option<&'b Tile<'a>> { + fn get_tile<'b>(&'b self, x: i32, y: i32) -> Option<&'b Tile<'a>> { for tile in self.tiles.iter() { if tile.tile_x == x && tile.tile_y == y { return Some(tile); @@ -331,8 +331,8 @@ impl<'a> Board<'a> { None } - fn get_mut_tile<'b>(&'b mut self, x: int, y: int) -> Option<&'b mut Tile<'a>> { - for tile in self.tiles.mut_iter() { + fn get_mut_tile<'b>(&'b mut self, x: i32, y: i32) -> Option<&'b mut Tile<'a>> { + for tile in self.tiles.iter_mut() { if tile.tile_x == x && tile.tile_y == y { return Some(tile); } @@ -340,7 +340,7 @@ impl<'a> Board<'a> { None } - fn get_tile_count(&self, x: int, y: int) -> int { + fn get_tile_count(&self, x: i32, y: i32) -> i32 { let mut count = 0; for tile in self.tiles.iter() { if tile.tile_x == x && tile.tile_y == y { @@ -351,28 +351,24 @@ impl<'a> Board<'a> { } fn render_board(&self, c: &Context, gl: &mut Gl) { - c.view() - .rect(self.settings.board_padding, - self.settings.board_padding + self.settings.board_offset_y, - self.settings.board_size[0], - self.settings.board_size[1]) - .rgba(self.settings.tile_background_color[0], - self.settings.tile_background_color[1], - self.settings.tile_background_color[2], - 1.0) - .draw(gl); + Rectangle::new(rgb2rgba(self.settings.label_color)) + .draw([self.settings.board_padding, + self.settings.board_padding + self.settings.board_offset_y, + self.settings.board_size[0], + self.settings.board_size[1]], + default_draw_state(), + c.transform, + gl); let mut x = self.settings.board_padding + self.settings.tile_padding; let mut y = self.settings.board_padding + self.settings.board_offset_y + self.settings.tile_padding; - for _ in range(0, self.settings.tile_height) { - for _ in range(0, self.settings.tile_width) { - c.view() - .rect(x, y, self.settings.tile_size, self.settings.tile_size) - .rgba(self.settings.tiles_colors[0][0], - self.settings.tiles_colors[0][1], - self.settings.tiles_colors[0][2], - 1.0) - .draw(gl); + for _ in 0..self.settings.tile_height { + for _ in 0..self.settings.tile_width { + Rectangle::new(rgb2rgba(self.settings.tiles_colors[0])) + .draw([x, y, self.settings.tile_size, self.settings.tile_size], + default_draw_state(), + c.transform, + gl); x += self.settings.tile_padding + self.settings.tile_size; } @@ -387,9 +383,8 @@ impl<'a> Board<'a> { } } - fn add_score(&mut self, score: int) { + fn add_score(&mut self, score: i32) { self.score += score; println!("Score: {}", self.score); } } - diff --git a/src/main.rs b/src/main.rs index 58b5043..db51236 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,18 @@ -#![feature(globs)] +#![feature(core,collections,path_ext)] -extern crate serialize; +extern crate rustc_serialize; +extern crate rand; extern crate graphics; extern crate piston; extern crate opengl_graphics; extern crate sdl2_window; -use piston::*; +use piston::event::*; +use piston::window::{WindowSettings, Size}; use sdl2_window::Sdl2Window; +use opengl_graphics::{GlGraphics,OpenGL}; mod app; mod board; @@ -20,13 +23,12 @@ mod tile; fn main() { let settings = settings::Settings::load(); - let mut window = Sdl2Window::new( - Sdl2Window { - title: "Rust-2048".to_string(), - size: settings.window_size, - fullscreen: false, - exit_on_esc: true, - } + let window = Sdl2Window::new( + OpenGL::_3_2, + WindowSettings::new( + "Rust-2048".to_string(), + Size { width: settings.window_size[0], height: settings.window_size[1] }) + .exit_on_esc(true) ); let mut app = app::App::new(&settings); @@ -39,26 +41,27 @@ fn main() { max_frames_per_second: 60, }; */ + let mut gl = GlGraphics::new(OpenGL::_3_2); - for e in piston::events(&window) { + for e in window.events() { use piston::event::{ RenderEvent, PressEvent }; - if let Some(args) = e.render_args() { - app.render(args); + if let Some(ref args) = e.render_args() { + app.render(args, &mut gl); } - if let Some(args) = e.update_args() { + if let Some(ref args) = e.update_args() { app.update(args); } - if let Some(args) = e.press_args() { + if let Some(ref 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); @@ -74,4 +77,3 @@ fn main() { } */ } - diff --git a/src/number_renderer.rs b/src/number_renderer.rs index f0b6a5c..13660c6 100644 --- a/src/number_renderer.rs +++ b/src/number_renderer.rs @@ -1,4 +1,5 @@ +use std::path::Path; use graphics::*; use opengl_graphics::{ Gl, @@ -15,12 +16,12 @@ pub struct NumberRenderer { impl NumberRenderer { pub fn new() -> NumberRenderer { NumberRenderer { - image: Texture::from_path(Path::new("digits.png")).unwrap(), + image: Texture::from_path(Path::new("bin/assets/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 { @@ -34,11 +35,13 @@ impl NumberRenderer { let y = center_y - height / 2.0; for digit in digits.iter() { - c.rect(x, y, width, height) - .image(&self.image) - .src_rect((*digit * DIGITS_WIDTH as u32) as i32, 0, DIGITS_WIDTH as i32, DIGITS_HEIGHT as i32) - .rgba(color[0], color[1], color[2], 1.0) - .draw(gl); + Image::new_colored([color[0], color[1], color[2], 1.0]) + .src_rect([(*digit * DIGITS_WIDTH as u32) as i32, 0, (*digit * DIGITS_WIDTH as u32) as i32 + DIGITS_WIDTH as i32, DIGITS_HEIGHT as i32]) + .rect([x, y, width, height]) + .draw(&self.image, + default_draw_state(), + c.transform, + gl); x += width; } } @@ -58,4 +61,3 @@ fn number_to_digits(number: u32) -> Vec { } digits } - diff --git a/src/settings.rs b/src/settings.rs index b52057b..d80c98d 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -1,8 +1,9 @@ -use std::os::self_exe_path; -use std::old_io::{BufferedWriter, BufferedReader}; -use std::fs::File; -use serialize::{ +use std::env::current_exe; +use std::io::{BufWriter, BufReader, Write}; +use std::fs::{File,PathExt}; +use std::path::Path; +use rustc_serialize::{ json, Encodable, Decodable, @@ -12,29 +13,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_width: i32, + pub tile_height: i32, 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 +50,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 +128,7 @@ impl Settings { } } -#[derive(Encodable, Decodable)] +#[derive(RustcEncodable, RustcDecodable)] struct SettingsInJson { asset_folder: String, @@ -140,8 +141,8 @@ struct SettingsInJson { board_padding: f64, board_offset_y: f64, - tile_width: int, - tile_height: int, + tile_width: i32, + tile_height: i32, tile_size: f64, tile_padding: f64, tile_background_color: Vec, @@ -185,7 +186,7 @@ impl SettingsInJson { // 512 color tiles_colors.push(vec![237.0, 200.0, 80.0]); SettingsInJson { - asset_folder: "assets".to_string(), + asset_folder: "bin/assets".to_string(), window_background_color: vec![255.0, 248.0, 239.0], comment1_offset_y: 72.0, comment2_offset_y: 100.0, @@ -211,37 +212,48 @@ impl SettingsInJson { } pub fn load() -> SettingsInJson { - let exe_path = self_exe_path(); - if exe_path.is_none() { + let exe_path = current_exe(); + if exe_path.is_err() { return SettingsInJson::default_settings(); } - let exe_path = exe_path.unwrap(); + let mut exe_path = exe_path.unwrap(); + exe_path.pop(); let path = exe_path.join(Path::new(SETTING_FILENAME)); - if !path.exists() || !path.is_file() { + if !path.as_path().exists() || !path.is_file() { let default = SettingsInJson::default_settings(); default.save(); return default; } let file = File::open(&path).unwrap(); - let mut reader = BufferedReader::new(file); - let mut decoder = json::Decoder::new(json::from_reader(&mut reader).unwrap()); + let mut reader = BufReader::new(file); + let mut decoder = json::Decoder::new(json::Json::from_reader(&mut reader).unwrap()); Decodable::decode(&mut decoder).unwrap() } pub fn save(&self) { - let exe_path = self_exe_path(); - if exe_path.is_none() { + let exe_path = current_exe(); + if exe_path.is_err() { println!("WARNING: Failed to save settings: can't find exe path."); return; } - let path = exe_path.unwrap().join(Path::new(SETTING_FILENAME)); - let file = File::create(&path).unwrap(); - let mut writer = BufferedWriter::new(file); - let mut encoder = json::Encoder::new(&mut writer); - match self.encode(&mut encoder) { - Ok(()) => (), - Err(e) => { println!("WARNING: Failed to save settings: {}", e); }, + let mut path = exe_path.unwrap(); + path.pop(); + let file = File::create(&path.with_file_name(SETTING_FILENAME)).unwrap(); + let mut writer = BufWriter::new(file); + + match json::encode(self) { + Ok(encoded) => { + if let Err(e) = writer.write(encoded.as_bytes()) { + println!("WARNING: Failed to save settings: {}", e); + } + }, Err(e) => { + println!("WARNING: Failed to save settings: {}", e); + } } + + // match self.encode(&mut encoder) { + // Ok(()) => (), + // Err(e) => { println!("WARNING: Failed to save settings: {}", e); }, + // } } } - diff --git a/src/tile.rs b/src/tile.rs index eb9d66d..f522550 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -1,6 +1,5 @@ use graphics::*; -use piston::*; use opengl_graphics::Gl; use number_renderer::NumberRenderer; use settings::Settings; @@ -9,7 +8,7 @@ use settings::Settings; pub enum TileState { TileStatic, /// (t, x, y, origin_x, origin_x) - TileMoving(f64, f64, f64, int, int), + TileMoving(f64, f64, f64, i32, i32), /// (t, size) TileNew(f64, f64), /// (t, size) @@ -18,16 +17,16 @@ pub enum TileState { #[derive(Clone)] pub struct Tile<'a> { - pub score: int, - pub tile_x: int, - pub tile_y: int, + pub score: i32, + pub tile_x: i32, + pub tile_y: i32, pub status: TileState, settings: &'a Settings, } impl<'a> Tile<'a> { - pub fn new(settings: &'a Settings, score: int, tile_x: int, tile_y: int) -> Tile<'a> { + pub fn new(settings: &'a Settings, score: i32, tile_x: i32, tile_y: i32) -> Tile<'a> { Tile { score: score, tile_x: tile_x, @@ -38,7 +37,7 @@ impl<'a> Tile<'a> { } } - pub fn new_combined(settings: &'a Settings, score: int, tile_x: int, tile_y: int) -> Tile<'a> { + pub fn new_combined(settings: &'a Settings, score: i32, tile_x: i32, tile_y: i32) -> Tile<'a> { Tile { score: score, tile_x: tile_x, @@ -49,13 +48,13 @@ impl<'a> Tile<'a> { } } - fn tile_to_pos(&self, tile_x: int, tile_y: int) -> (f64, f64) { + fn tile_to_pos(&self, tile_x: i32, tile_y: i32) -> (f64, f64) { let x = self.settings.board_padding + tile_x as f64 * self.settings.tile_size + (tile_x + 1) as f64 * self.settings.tile_padding; let y = self.settings.board_padding + self.settings.board_offset_y + tile_y as f64 * self.settings.tile_size + (tile_y + 1) as f64 * self.settings.tile_padding; (x, y) } - pub fn start_moving(&mut self, destination_tile_x: int, destination_tile_y: int) { + pub fn start_moving(&mut self, destination_tile_x: i32, destination_tile_y: i32) { match self.status { TileState::TileMoving(_, _, _, ox, oy) => { let (x, y) = self.tile_to_pos(ox, oy); @@ -122,11 +121,14 @@ impl<'a> Tile<'a> { let (x, y) = pos; let (w, h) = size; let color = self.get_color(); - c.view() - .rect_centered(x + self.settings.tile_size / 2.0, - y + self.settings.tile_size / 2.0, - w / 2.0, h / 2.0) - .rgba(color[0], color[1], color[2], 1.0).draw(gl); + + Rectangle::new([color[0], color[1], color[2], 1.0]) + .draw(rectangle::centered([x + self.settings.tile_size / 2.0, + y + self.settings.tile_size / 2., + w/2.0, h/2.0]), + default_draw_state(), + c.transform, + gl); let color = if self.score >= 8 { self.settings.text_light_color @@ -136,8 +138,8 @@ 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] { - let i = (self.score as f64).log2() as uint; + fn get_color(&self) -> [f32; 3] { + let i = (self.score as f64).log2() as usize; if i > 0 && i < self.settings.tiles_colors.len() { self.settings.tiles_colors[i] } else { @@ -145,4 +147,3 @@ impl<'a> Tile<'a> { } } } -