Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ name = "rust-2048"
path = "src/main.rs"

[dependencies]
rustc-serialize = "0.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
rand = "0.3.7"
piston_window = "0.127.0"
pistoncore-sdl2_window = "0.68.0"
Expand Down
Binary file added SDL2.lib
Binary file not shown.
Binary file added SDL2main.lib
Binary file not shown.
Binary file added SDL2test.lib
Binary file not shown.
27 changes: 13 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
extern crate rustc_serialize;
extern crate rand;
extern crate piston_window;
extern crate opengl_graphics;
extern crate piston_window;
extern crate rand;
extern crate sdl2_window;
extern crate serde;
extern crate serde_json;

use piston_window::*;
use sdl2_window::Sdl2Window;
Expand All @@ -14,18 +15,16 @@ mod settings;
mod tile;

fn main() {
use opengl_graphics::GlGraphics;
use opengl_graphics::GlGraphics;
let settings = settings::Settings::load();

let (width, height) = (settings.window_size[0],
settings.window_size[1]);
let (width, height) = (settings.window_size[0], settings.window_size[1]);

// according to piston WindowSettings documentation, OpenGL::V3_2 is the default version
let mut window: PistonWindow<Sdl2Window> =
WindowSettings::new("Rust-2048", [width, height])
.exit_on_esc(true)
.build()
.unwrap_or_else(|e| { panic!("Failed to build PistonWindow: {}", e) });
let mut window: PistonWindow<Sdl2Window> = WindowSettings::new("Rust-2048", [width, height])
.exit_on_esc(true)
.build()
.unwrap_or_else(|e| panic!("Failed to build PistonWindow: {}", e));

let mut app = app::App::new(&settings);

Expand All @@ -39,9 +38,9 @@ fn main() {
}

if let Some(ref args) = e.update_args() {
// TODO: only update if necessary
// println!("update");
app.update(args);
// TODO: only update if necessary
// println!("update");
app.update(args);
}

if let Some(ref args) = e.press_args() {
Expand Down
77 changes: 31 additions & 46 deletions src/settings.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@

use serde::{Deserialize, Serialize};
use serde_json::{from_reader, to_writer};
use std::env::current_exe;
use std::io::{BufWriter, BufReader, Write};
use std::fs::{File};
use std::fs::File;
use std::io::{BufReader, BufWriter};
use std::path::Path;
use rustc_serialize::{ json, Encodable, Decodable };

static SETTING_FILENAME: &'static str = "settings.json";

#[derive(Serialize, Deserialize)]
pub struct Settings {
pub asset_folder: String,
pub window_size: [u32; 2],
Expand Down Expand Up @@ -48,11 +49,7 @@ impl Settings {
let mut tiles_colors = Vec::<[f32; 3]>::new();

for color in s.tiles_colors.iter() {
tiles_colors.push([
color[0] / 255.0,
color[1] / 255.0,
color[2] / 255.0,
]);
tiles_colors.push([color[0] / 255.0, color[1] / 255.0, color[2] / 255.0]);
}

Settings {
Expand Down Expand Up @@ -125,7 +122,7 @@ impl Settings {
}
}

#[derive(RustcEncodable, RustcDecodable)]
#[derive(Serialize, Deserialize)]
struct SettingsInJson {
asset_folder: String,

Expand Down Expand Up @@ -199,7 +196,7 @@ impl SettingsInJson {
tile_move_time: 0.1,
tile_new_time: 0.1,
tile_combine_time: 0.1,
best_rect: vec![284.0, 12.0, 96.0, 48.0,],
best_rect: vec![284.0, 12.0, 96.0, 48.0],
score_rect: vec![176.0, 12.0, 96.0, 48.0],
label_color: vec![187.0, 173.0, 160.0],
button_color: vec![142.0, 122.0, 102.0],
Expand All @@ -219,33 +216,28 @@ impl SettingsInJson {
exe_path.pop();
let path = exe_path.join(Path::new(SETTING_FILENAME));

// FIXME: use this if possible (.exists() is unstable in Rust 1.0.0)
/* if !path.as_path().exists() || !path.is_file() {
println!("Configuration file not found. Generating a default one.");
let default = SettingsInJson::default_settings();
default.save();
return default;
}
let file = File::open(&path).unwrap();
let mut reader = BufReader::new(file);
*/
let file = File::open(&path);
let file = File::open(&path);

match file {
Err(e) => {
println!("Configuration file can't be open ({}). Try to generate a default one.", e);
match file {
Err(e) => {
println!(
"Configuration file can't be open ({}). Try to generate a default one.",
e
);
let default = SettingsInJson::default_settings();
default.save();
return default;
}
Ok(file) => {
let reader = BufReader::new(file);
serde_json::from_reader(reader).unwrap_or_else(|_| {
println!("Failed to parse settings, using default.");
let default = SettingsInJson::default_settings();
default.save();
return default;
},
_ => {}
default
})
}

let mut reader = BufReader::new(file.unwrap());
// End FIXME

let mut decoder = json::Decoder::new(json::Json::from_reader(&mut reader).unwrap());
Decodable::decode(&mut decoder).unwrap()
}
}

pub fn save(&self) {
Expand All @@ -256,19 +248,12 @@ impl SettingsInJson {
return;
}

let path = exe_path.unwrap();
let file = File::create(&path.with_file_name(SETTING_FILENAME)).unwrap();
let mut writer = BufWriter::new(file);
let path = exe_path.unwrap().with_file_name(SETTING_FILENAME);
let file = File::create(&path).unwrap();
let 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);
}
if let Err(e) = serde_json::to_writer(writer, self) {
println!("WARNING: Failed to save settings: {}", e);
}
}
}