Correction pour la version rustc 1.0.0-nightly de Rust (une erreur restante)

This commit is contained in:
Yannick Armand (PifyZ) 2015-03-21 22:07:51 +01:00
parent ae34d5c5a0
commit 25553529ff
7 changed files with 90 additions and 70 deletions

View file

@ -9,20 +9,15 @@ authors = ["coeuvre <coeuvre@gmail.com>"]
name = "rust-2048" name = "rust-2048"
path = "src/main.rs" path = "src/main.rs"
[dependencies.graphics] [dependencies.piston2d-graphics]
git = "https://github.com/pistondevelopers/graphics"
git = "https://github.com/pistondevelopers/rust-graphics"
[dependencies.piston] [dependencies.piston]
git = "https://github.com/pistondevelopers/piston"
git = "https://github.com/pistondevelopers/piston/" [dependencies.piston2d-opengl_graphics]
[dependencies.opengl_graphics]
git = "https://github.com/pistondevelopers/opengl_graphics" git = "https://github.com/pistondevelopers/opengl_graphics"
[dependencies.sdl2_window] [dependencies.pistoncore-sdl2_window]
git = "https://github.com/pistondevelopers/sdl2_window" git = "https://github.com/pistondevelopers/sdl2_window"

View file

@ -1,6 +1,6 @@
use graphics::*; use graphics::*;
use piston::*; use piston::*;
use piston::events::{ RenderArgs, UpdateArgs };
use board::Board; use board::Board;
use number_renderer::NumberRenderer; use number_renderer::NumberRenderer;
@ -18,7 +18,7 @@ pub struct App<'a> {
logo: Option<Texture>, logo: Option<Texture>,
comment1: Option<Texture>, comment1: Option<Texture>,
comment2: Option<Texture>, comment2: Option<Texture>,
window_background_color: [f32, ..4], window_background_color: [f32; ..4],
gl: Gl, gl: Gl,
} }
@ -80,7 +80,7 @@ impl<'a> App<'a> {
} }
pub fn load(&mut self) { 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.number_renderer = Some(NumberRenderer::new(&asset_store));
self.logo = Some(Texture::from_path(&asset_store.path("logo.png").unwrap()).unwrap()); 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) { 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(); 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(); 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(); 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(); self.board.merge_from_top_to_bottom();
} }
if args.key == keyboard::Space {
if args.key == Keyboard(Key::Space) {
self.board = Board::new(self.settings); self.board = Board::new(self.settings);
} }
} }

View file

@ -1,6 +1,6 @@
use std::iter::range_step; use std::iter::range_step;
use std::collections::hashmap::HashSet; use std::collections::HashSet;
use std::rand::random; use std::rand::random;
use graphics::*; use graphics::*;
use piston::*; use piston::*;
@ -11,7 +11,7 @@ use number_renderer::NumberRenderer;
use settings::Settings; use settings::Settings;
use tile::{ use tile::{
Tile, Tile,
TileStatic, TileState,
}; };
pub struct Board<'a> { pub struct Board<'a> {
@ -61,12 +61,12 @@ impl<'a> Board<'a> {
let mut tiles_need_added = Vec::<Tile>::new(); let mut tiles_need_added = Vec::<Tile>::new();
for i in range(0, self.tiles.len()) { for i in range(0, self.tiles.len()) {
let tile1 = &self.tiles[i]; let tile1 = &self.tiles[i];
if tile1.status != TileStatic { if tile1.status != TileState::TileStatic {
continue; continue;
} }
for j in range(i + 1, self.tiles.len()) { for j in range(i + 1, self.tiles.len()) {
let tile2 = &self.tiles[j]; let tile2 = &self.tiles[j];
if tile2.status != TileStatic if tile2.status != TileState::TileStatic
|| tile1.tile_x != tile2.tile_x || tile1.tile_x != tile2.tile_x
|| tile1.tile_y != tile2.tile_y { || tile1.tile_y != tile2.tile_y {
continue; continue;
@ -277,7 +277,7 @@ impl<'a> Board<'a> {
fn is_locking(&self) -> bool { fn is_locking(&self) -> bool {
for tile in self.tiles.iter() { for tile in self.tiles.iter() {
if tile.status != TileStatic { if tile.status != TileState::TileStatic {
return true; return true;
} }
} }

View file

@ -6,10 +6,10 @@ extern crate serialize;
extern crate graphics; extern crate graphics;
extern crate piston; extern crate piston;
extern crate opengl_graphics; extern crate opengl_graphics;
extern crate sdl2_game_window; extern crate sdl2_window;
use piston::*; use piston::*;
use sdl2_game_window::GameWindowSDL2; use sdl2_window::Sdl2Window;
mod app; mod app;
mod board; mod board;
@ -20,8 +20,8 @@ mod tile;
fn main() { fn main() {
let settings = settings::Settings::load(); let settings = settings::Settings::load();
let mut window = GameWindowSDL2::new( let mut window = Sdl2Window::new(
GameWindowSettings { Sdl2Window {
title: "Rust-2048".to_string(), title: "Rust-2048".to_string(),
size: settings.window_size, size: settings.window_size,
fullscreen: false, fullscreen: false,
@ -33,12 +33,32 @@ fn main() {
app.load(); app.load();
/*
let game_iter_settings = GameIteratorSettings { let game_iter_settings = GameIteratorSettings {
updates_per_second: 120, updates_per_second: 120,
max_frames_per_second: 60, 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) { for e in GameIterator::new(&mut window, &game_iter_settings) {
match e { match e {
Render(ref args) => { Render(ref args) => {
app.render(args); app.render(args);
@ -52,5 +72,6 @@ fn main() {
_ => {}, _ => {},
} }
} }
*/
} }

View file

@ -1,8 +1,5 @@
use graphics::*; use graphics::*;
use piston::{
AssetStore,
};
use opengl_graphics::{ use opengl_graphics::{
Gl, Gl,
Texture, Texture,
@ -16,14 +13,14 @@ pub struct NumberRenderer {
} }
impl NumberRenderer { impl NumberRenderer {
pub fn new(asset_store: &AssetStore) -> NumberRenderer { pub fn new() -> NumberRenderer {
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, 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 digits = number_to_digits(number);
let total_width = DIGITS_WIDTH * digits.len() as f64; let total_width = DIGITS_WIDTH * digits.len() as f64;
let total_width = if total_width > max_width { let total_width = if total_width > max_width {

View file

@ -1,7 +1,7 @@
use std::os::self_exe_path; use std::os::self_exe_path;
use std::io::{BufferedWriter, BufferedReader}; use std::old_io::{BufferedWriter, BufferedReader};
use std::io::fs::File; use std::fs::File;
use serialize::{ use serialize::{
json, json,
Encodable, Encodable,
@ -12,29 +12,29 @@ static SETTING_FILENAME: &'static str = "settings.json";
pub struct Settings { pub struct Settings {
pub asset_folder: String, pub asset_folder: String,
pub window_size: [u32, ..2], pub window_size: [u32; ..2],
pub window_background_color: [f32, ..3], pub window_background_color: [f32; ..3],
pub comment1_offset_y: f64, pub comment1_offset_y: f64,
pub comment2_offset_y: f64, pub comment2_offset_y: f64,
pub board_padding: f64, pub board_padding: f64,
pub board_size: [f64, ..2], pub board_size: [f64; ..2],
pub board_offset_y: f64, pub board_offset_y: f64,
pub tile_width: int, pub tile_width: int,
pub tile_height: int, pub tile_height: int,
pub tile_size: f64, pub tile_size: f64,
pub tile_padding: f64, pub tile_padding: f64,
pub tile_background_color: [f32, ..3], pub tile_background_color: [f32; ..3],
pub tiles_colors: Vec<[f32, ..3]>, pub tiles_colors: Vec<[f32; ..3]>,
pub tile_unknow_color: [f32, ..3], pub tile_unknow_color: [f32; ..3],
pub tile_move_time: f64, pub tile_move_time: f64,
pub tile_new_time: f64, pub tile_new_time: f64,
pub tile_combine_time: f64, pub tile_combine_time: f64,
pub best_rect: [f64, ..4], pub best_rect: [f64; ..4],
pub score_rect: [f64, ..4], pub score_rect: [f64; ..4],
pub label_color: [f32, ..3], pub label_color: [f32; ..3],
pub button_color: [f32, ..3], pub button_color: [f32; ..3],
pub text_dark_color: [f32, ..3], pub text_dark_color: [f32; ..3],
pub text_light_color: [f32, ..3], pub text_light_color: [f32; ..3],
} }
impl Settings { 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() { for color in s.tiles_colors.iter() {
tiles_colors.push([ tiles_colors.push([
color[0] / 255.0, color[0] / 255.0,
@ -127,7 +127,7 @@ impl Settings {
} }
} }
#[deriving(Encodable, Decodable)] #[derive(Encodable, Decodable)]
struct SettingsInJson { struct SettingsInJson {
asset_folder: String, asset_folder: String,

View file

@ -5,7 +5,7 @@ use opengl_graphics::Gl;
use number_renderer::NumberRenderer; use number_renderer::NumberRenderer;
use settings::Settings; use settings::Settings;
#[deriving(Clone, PartialEq)] #[derive(Clone, PartialEq)]
pub enum TileState { pub enum TileState {
TileStatic, TileStatic,
/// (t, x, y, origin_x, origin_x) /// (t, x, y, origin_x, origin_x)
@ -16,7 +16,7 @@ pub enum TileState {
TileCombine(f64, f64), TileCombine(f64, f64),
} }
#[deriving(Clone)] #[derive(Clone)]
pub struct Tile<'a> { pub struct Tile<'a> {
pub score: int, pub score: int,
pub tile_x: int, pub tile_x: int,
@ -32,7 +32,7 @@ impl<'a> Tile<'a> {
score: score, score: score,
tile_x: tile_x, tile_x: tile_x,
tile_y: tile_y, tile_y: tile_y,
status: TileNew(settings.tile_new_time, 0.0), status: TileState::TileNew(settings.tile_new_time, 0.0),
settings: settings, settings: settings,
} }
@ -43,7 +43,7 @@ impl<'a> Tile<'a> {
score: score, score: score,
tile_x: tile_x, tile_x: tile_x,
tile_y: tile_y, 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, settings: settings,
} }
@ -57,15 +57,15 @@ impl<'a> Tile<'a> {
pub fn start_moving(&mut self, destination_tile_x: int, destination_tile_y: int) { pub fn start_moving(&mut self, destination_tile_x: int, destination_tile_y: int) {
match self.status { match self.status {
TileMoving(_, _, _, ox, oy) => { TileState::TileMoving(_, _, _, ox, oy) => {
let (x, y) = self.tile_to_pos(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_x = destination_tile_x;
self.tile_y = destination_tile_y; self.tile_y = destination_tile_y;
}, },
TileStatic => { TileState::TileStatic => {
let (x, y) = self.tile_to_pos(self.tile_x, self.tile_y); 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_x = destination_tile_x;
self.tile_y = destination_tile_y; self.tile_y = destination_tile_y;
}, },
@ -75,29 +75,29 @@ impl<'a> Tile<'a> {
pub fn update(&mut self, dt: f64) { pub fn update(&mut self, dt: f64) {
match self.status { match self.status {
TileMoving(t, x, y, ox, oy) => { TileState::TileMoving(t, x, y, ox, oy) => {
if dt >= t { if dt >= t {
self.status = TileStatic; self.status = TileState::TileStatic;
} else { } else {
let (dx, dy) = self.tile_to_pos(self.tile_x, self.tile_y); let (dx, dy) = self.tile_to_pos(self.tile_x, self.tile_y);
let factor = dt / t; 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 { if dt >= t {
self.status = TileStatic; self.status = TileState::TileStatic;
} else { } else {
let factor = dt / t; 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 { if dt >= t {
self.status = TileStatic; self.status = TileState::TileStatic;
} else { } else {
let factor = dt / t; 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 pos = self.tile_to_pos(self.tile_x, self.tile_y);
let mut size = (self.settings.tile_size, self.settings.tile_size); let mut size = (self.settings.tile_size, self.settings.tile_size);
match self.status { match self.status {
TileMoving(_, x, y, _, _) => { TileState::TileMoving(_, x, y, _, _) => {
pos = (x, y); pos = (x, y);
}, },
TileNew(_, s) => { TileState::TileNew(_, s) => {
size = (s, s); size = (s, s);
}, },
TileCombine(_, s) => { TileState::TileCombine(_, s) => {
size = (s, 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); 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; let i = (self.score as f64).log2() as uint;
if i > 0 && i < self.settings.tiles_colors.len() { if i > 0 && i < self.settings.tiles_colors.len() {
self.settings.tiles_colors[i] self.settings.tiles_colors[i]