From b12e3c751ed61376db22d7ca55d8ca1ea02c42dc Mon Sep 17 00:00:00 2001 From: Coeuvre Date: Wed, 4 Jun 2014 08:41:43 +0800 Subject: [PATCH] Upgraded to latest Piston --- src/app.rs | 21 +++++++++++---------- src/board.rs | 9 ++------- src/main.rs | 23 ++++++++++++----------- src/number_renderer.rs | 14 +++++++------- src/settings.rs | 2 +- src/tile.rs | 2 +- 6 files changed, 34 insertions(+), 37 deletions(-) diff --git a/src/app.rs b/src/app.rs index 7abad63..177aea7 100644 --- a/src/app.rs +++ b/src/app.rs @@ -11,9 +11,9 @@ pub struct App<'a> { number_renderer: Option, settings: &'a Settings, - logo: Option, - comment1: Option, - comment2: Option, + logo: Option, + comment1: Option, + comment2: Option, } impl<'a> App<'a> { @@ -34,7 +34,7 @@ impl<'a> App<'a> { fn render_ui(&self, c: &Context, gl: &mut Gl) { // logo c.trans(self.settings.board_padding, self.settings.board_padding) - .image(self.logo.unwrap()) + .image(self.logo.get_ref()) .rgb(self.settings.text_dark_color[0], self.settings.text_dark_color[1], self.settings.text_dark_color[2]) @@ -55,11 +55,12 @@ impl<'a> App<'a> { self.render_comment(self.comment2.get_ref(), self.settings.comment2_offset_y, c, gl); } - fn render_comment(&self, comment: &Image, y: f64, c: &Context, gl: &mut Gl) { + fn render_comment(&self, comment: &Texture, y: f64, c: &Context, gl: &mut Gl) { + let (width, height) = comment.get_size(); let w = self.settings.window_size[0] as f64 - 2.0 * self.settings.board_padding; - let h = comment.texture_height as f64 * w / comment.texture_width as f64; + let h = height as f64 * w / width as f64; c.rect(self.settings.board_padding, y, w, h) - .image(*comment) + .image(comment) .rgb(self.settings.text_dark_color[0], self.settings.text_dark_color[1], self.settings.text_dark_color[2]) @@ -71,9 +72,9 @@ impl<'a> Game for App<'a> { fn load(&mut self, asset_store: &mut AssetStore) { self.number_renderer = Some(NumberRenderer::new(asset_store)); - self.logo = Some(asset_store.load_image("logo.png").unwrap()); - self.comment1 = Some(asset_store.load_image("comment1.png").unwrap()); - self.comment2 = Some(asset_store.load_image("comment2.png").unwrap()); + 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()); } fn render(&self, _ext_dt: f64, c: &Context, gl: &mut Gl) { diff --git a/src/board.rs b/src/board.rs index 98cd604..c606d5c 100644 --- a/src/board.rs +++ b/src/board.rs @@ -1,7 +1,7 @@ use std::iter::range_step; use collections::hashmap::HashSet; -use rand::random; +use std::rand::random; use graphics::*; use piston::*; use number_renderer::NumberRenderer; @@ -14,7 +14,6 @@ use tile::{ pub struct Board<'a> { tiles: Vec>, score: int, - highest_score: int, settings: &'a Settings, } @@ -24,7 +23,6 @@ impl<'a> Board<'a> { let mut board = Board { tiles: Vec::::new(), score: 0, - highest_score: 0, settings: settings, }; board.generate_tile(); @@ -384,10 +382,7 @@ impl<'a> Board<'a> { fn add_score(&mut self, score: int) { self.score += score; - if self.score > self.highest_score { - self.highest_score = self.score; - } - println!("Score: {}, Highest Score: {}", self.score, self.highest_score); + println!("Score: {}", self.score); } } diff --git a/src/main.rs b/src/main.rs index 64f2718..9d6daa0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,6 @@ #![feature(globs)] extern crate collections; -extern crate rand; extern crate serialize; extern crate graphics; @@ -22,16 +21,18 @@ fn main() { let settings = settings::Settings::load(); let mut game_window: GameWindowBackEnd = GameWindow::new( - GameWindowSettings::new ( - "Rust-2048".to_owned(), - settings.window_size, - false, - true, - [settings.window_background_color[0], - settings.window_background_color[1], - settings.window_background_color[2], - 1.0,], - ) + GameWindowSettings { + title: "Rust-2048".to_string(), + size: settings.window_size, + fullscreen: false, + exit_on_esc: true, + background_color: [ + settings.window_background_color[0], + settings.window_background_color[1], + settings.window_background_color[2], + 1.0, + ], + } ); let mut asset_store = AssetStore::from_folder(settings.asset_folder.as_slice()); diff --git a/src/number_renderer.rs b/src/number_renderer.rs index 5972878..6a8a1b1 100644 --- a/src/number_renderer.rs +++ b/src/number_renderer.rs @@ -3,19 +3,20 @@ use graphics::*; use piston::{ AssetStore, Gl, + Texture, }; static DIGITS_WIDTH: f64 = 20.0; static DIGITS_HEIGHT: f64 = 26.0; pub struct NumberRenderer { - image: Image, + image: Texture, } impl NumberRenderer { pub fn new(asset_store: &mut AssetStore) -> NumberRenderer { NumberRenderer { - image: asset_store.load_image("digits.png").unwrap(), + image: Texture::from_path(&asset_store.path("digits.png").unwrap()).unwrap(), } } @@ -33,12 +34,11 @@ impl NumberRenderer { let height = width / DIGITS_WIDTH * DIGITS_HEIGHT; let y = center_y - height / 2.0; - let mut image = self.image; - image.source_rect[2] = DIGITS_WIDTH as u32; for digit in digits.iter() { - image.source_rect[0] = DIGITS_WIDTH as u32 * *digit; - c.view().rect(x, y, width, height) - .image(image).rgba(color[0], color[1], color[2], 1.0) + c.rect(x, y, width, height) + .image(&self.image) + .src_rect(*digit * DIGITS_WIDTH as u32, 0, DIGITS_WIDTH as u32, DIGITS_HEIGHT as u32) + .rgba(color[0], color[1], color[2], 1.0) .draw(gl); x += width; } diff --git a/src/settings.rs b/src/settings.rs index 363e3c6..707c7b0 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -185,7 +185,7 @@ impl SettingsInJson { // 512 color tiles_colors.push(vec![237.0, 200.0, 80.0]); SettingsInJson { - asset_folder: "assets".to_strbuf(), + asset_folder: "assets".to_string(), window_background_color: vec![255.0, 248.0, 239.0], comment1_offset_y: 72.0, comment2_offset_y: 100.0, diff --git a/src/tile.rs b/src/tile.rs index 64ed3c7..a5d055a 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -4,7 +4,7 @@ use piston::*; use number_renderer::NumberRenderer; use settings::Settings; -#[deriving(Clone, Eq)] +#[deriving(Clone, PartialEq)] pub enum TileState { TileStatic, /// (t, x, y, origin_x, origin_x)