Browse Source

Added configuration loading

Chris Konstad 3 years ago
parent
commit
691f137d3d
5 changed files with 52 additions and 6 deletions
  1. 1 0
      Cargo.lock
  2. 1 0
      Cargo.toml
  3. 7 0
      config.json
  4. 1 0
      src/graphics.rs
  5. 42 6
      src/main.rs

+ 1 - 0
Cargo.lock

@ -7,6 +7,7 @@ dependencies = [
7 7
 "env_logger 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
8 8
 "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
9 9
 "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
10
 "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
10 11
 "sdl2 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)",
11 12
]
12 13

													

+ 1 - 0
Cargo.toml

@ -21,4 +21,5 @@ clap = "*"
21 21
env_logger = "0.3"
22 22
log = "0.3"
23 23
rand = "0.3"
24
rustc-serialize = "0.3"
24 25
sdl2 = "0.19"

+ 7 - 0
config.json

@ -0,0 +1,7 @@
1
{
2
  "color": {
3
    "red": 0,
4
    "green": 255,
5
    "blue": 255
6
  }
7
}

+ 1 - 0
src/graphics.rs

@ -8,6 +8,7 @@ use std::time::{Duration, Instant};
8 8
use std::thread::sleep;
9 9

													
10 10
/// Represents a color to paint the display with
11
#[derive(RustcDecodable, RustcEncodable)]
11 12
pub struct Color {
12 13
    pub red: u8,
13 14
    pub green: u8,

+ 42 - 6
src/main.rs

@ -1,8 +1,9 @@
1 1
extern crate chip8;
2 2
extern crate clap;
3
extern crate env_logger;
3 4
#[macro_use]
4 5
extern crate log;
5
extern crate env_logger;
6
extern crate rustc_serialize;
6 7
extern crate sdl2;
7 8

													
8 9
mod audio;
@ -12,6 +13,9 @@ mod loader;
12 13

													
13 14
use chip8::Chip8;
14 15
use clap::{Arg, App};
16
use rustc_serialize::json;
17
use std::fs::File;
18
use std::io::Read;
15 19
use std::time::Duration;
16 20

													
17 21
/// Adjust the scaling factor of the chip8's display.  The larger the number,
@ -24,6 +28,24 @@ const WIDTH : u32 = chip8::WIDTH * SCALE;
24 28
/// The scaled height of the display.
25 29
const HEIGHT : u32 = chip8::HEIGHT * SCALE;
26 30

													
31
#[derive(RustcDecodable, RustcEncodable)]
32
// TODO let config fields be optional?
33
struct Config {
34
    color: graphics::Color,
35
}
36

													
37
impl Default for Config {
38
    fn default() -> Self {
39
        Config {
40
            color: graphics::Color {
41
                red: 255,
42
                green: 255,
43
                blue: 0,
44
            },
45
        }
46
    }
47
}
48

													
27 49
fn main() {
28 50
    // Init the logger
29 51
    env_logger::init().unwrap();
@ -33,6 +55,12 @@ fn main() {
33 55
        .version(chip8::version())
34 56
        .author("Chris Konstad <chriskon149@gmail.com>")
35 57
        .about("Runs Chip8 games.")
58
        .arg(Arg::with_name("config")
59
             .short("c")
60
             .long("config")
61
             .value_name("FILE")
62
             .help("Sets a custom config file")
63
             .takes_value(true))
36 64
        .arg(Arg::with_name("ROM")
37 65
             .help("Sets the path to the ROM to play")
38 66
             .required(true))
@ -40,6 +68,17 @@ fn main() {
40 68

													
41 69
    println!("Chip8 emulator starting...");
42 70

													
71
    // Load configuration file
72
    let config: Config = match matches.value_of("config") {
73
        None => Config::default(),
74
        Some(path) => {
75
            let mut file = File::open(path).unwrap();
76
            let mut data = String::new();
77
            file.read_to_string(&mut data).unwrap();
78
            json::decode(&data).unwrap()
79
        },
80
    };
81

													
43 82
    // Initialize the emulator and load the game
44 83
    let mut chip = Chip8::default();
45 84
    chip.load_hex(&loader::load_file(matches.value_of("ROM").unwrap()));
@ -57,11 +96,8 @@ fn main() {
57 96
                                            WIDTH,
58 97
                                            HEIGHT,
59 98
                                            Duration::from_millis(2),
60
                                            graphics::Color {
61
                                                red: 255,
62
                                                green: 0,
63
                                                blue: 0,
64
                                            });
99
                                            config.color,
100
                                            );
65 101

													
66 102
    // Emulation loop
67 103
    'running: loop {