Upgraded to latest Piston

This commit is contained in:
Coeuvre 2014-06-04 08:41:43 +08:00
parent 6f9cbae0a8
commit b12e3c751e
6 changed files with 34 additions and 37 deletions

View file

@ -11,9 +11,9 @@ pub struct App<'a> {
number_renderer: Option<NumberRenderer>,
settings: &'a Settings,
logo: Option<Image>,
comment1: Option<Image>,
comment2: Option<Image>,
logo: Option<Texture>,
comment1: Option<Texture>,
comment2: Option<Texture>,
}
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) {

View file

@ -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<Tile<'a>>,
score: int,
highest_score: int,
settings: &'a Settings,
}
@ -24,7 +23,6 @@ impl<'a> Board<'a> {
let mut board = Board {
tiles: Vec::<Tile>::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);
}
}

View file

@ -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],
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,],
)
1.0,
],
}
);
let mut asset_store = AssetStore::from_folder(settings.asset_folder.as_slice());

View file

@ -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;
}

View file

@ -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,

View file

@ -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)