aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 2b08c5ec663e22799ffff4b5b8365ce1fb227f5e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
extern crate sdl2;

use sdl2::pixels::Color;
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use std::time::Duration;
use chip8::Chip;
use std::env;


pub fn main() {
    //new chip struct
    let args: Vec<String> = env::args().collect();
    if args.len() != 2 { panic!("Incorrect format; please pass a single rom file as a parameter"); }
    let mut chip = Chip::new();
    chip.init();
    chip.read_rom(args[1].as_str());


    //SDL initalizationa and window creation
    let sdl_context = sdl2::init().unwrap();
    let video_subsystem = sdl_context.video().unwrap();
    let window = video_subsystem.window("CHIP-8", chip8::WIDTH * chip8::SCALE, chip8::HEIGHT * chip8::SCALE)
        .position_centered()
        .build()
        .unwrap();

    //Canvas to interact with
    let mut canvas = window.into_canvas().build().unwrap();

    //Keyboard input handler
    let mut event_pump = sdl_context.event_pump().unwrap();
    //Main loop called 'running', checks for keyboard input and renders the display grid
    'running: loop {
        for event in event_pump.poll_iter() {
            match event {
                Event::Quit {..} |
                Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
                    break 'running
                },
                _ => {}
            }
        }
        // The rest of the game loop goes here...
        //Draw black bg
        canvas.set_draw_color(Color::RGB(0, 0, 0));
        //clear the screen with black bg
        canvas.clear();
        //choose white color 
        canvas.set_draw_color(Color::RGB(255, 255, 255));
        //render all the rectangles as white pixels on the canvas
        chip.render(&mut canvas);
        //display canvas
        canvas.present();

        std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
    }
}