|
1 | | -use sdl2; |
2 | | -use sdl2::pixels; |
3 | | -use sdl2::rect::Rect; |
4 | | -use sdl2::render::Canvas; |
5 | | -use sdl2::video::Window; |
6 | | - |
7 | | -use CHIP8_WIDTH; |
8 | | -use CHIP8_HEIGHT; |
9 | | - |
10 | | -const SCALE_FACTOR: u32 = 20; |
11 | | -const SCREEN_WIDTH: u32 = (CHIP8_WIDTH as u32) * SCALE_FACTOR; |
12 | | -const SCREEN_HEIGHT: u32 = (CHIP8_HEIGHT as u32) * SCALE_FACTOR; |
13 | | - |
14 | | -pub struct DisplayDriver { |
15 | | - canvas: Canvas<Window>, |
16 | | -} |
17 | | - |
18 | | -impl DisplayDriver { |
19 | | - pub fn new(sdl_context: &sdl2::Sdl) -> Self { |
20 | | - let video_subsys = sdl_context.video().unwrap(); |
21 | | - let window = video_subsys |
22 | | - .window( |
23 | | - "rust-sdl2_gfx: draw line & FPSManager", |
24 | | - SCREEN_WIDTH, |
25 | | - SCREEN_HEIGHT, |
26 | | - ) |
27 | | - .position_centered() |
28 | | - .opengl() |
29 | | - .build() |
30 | | - .unwrap(); |
31 | | - |
32 | | - let mut canvas = window.into_canvas().build().unwrap(); |
33 | | - |
34 | | - canvas.set_draw_color(pixels::Color::RGB(0, 0, 0)); |
35 | | - canvas.clear(); |
36 | | - canvas.present(); |
37 | | - |
38 | | - DisplayDriver { canvas: canvas } |
39 | | - } |
40 | | - |
41 | | - pub fn draw(&mut self, pixels: &[u64; CHIP8_HEIGHT]) { |
42 | | - |
43 | | - for y in 0usize..CHIP8_HEIGHT { |
44 | | - for x in 0usize..CHIP8_WIDTH { |
45 | | - let _x = (x as u32) * SCALE_FACTOR; |
46 | | - let _y = (y as u32) * SCALE_FACTOR; |
47 | | - |
48 | | - self.canvas.set_draw_color( color( (pixels[y] >> (63 - x) ) & 1 )); |
49 | | - |
50 | | - let _ = self.canvas |
51 | | - .fill_rect( Rect::new( _x as i32, _y as i32, SCALE_FACTOR, SCALE_FACTOR ) ); |
52 | | - } |
53 | | - } |
54 | | - self.canvas.present(); |
55 | | - } |
56 | | -} |
57 | | - |
58 | | -fn color(value: u64) -> pixels::Color { |
59 | | - if value == 0 { |
60 | | - pixels::Color::RGB(0, 0,100) |
61 | | - } else { |
62 | | - pixels::Color::RGB(0, 150, 0) |
63 | | - } |
64 | | -} |
65 | | - |
66 | | -// pub fn run() { |
67 | | - |
68 | | - |
69 | | - |
70 | | -// let mut lastx = 0; |
71 | | -// let mut lasty = 0; |
72 | | - |
73 | | -// let mut events = sdl_context.event_pump().unwrap(); |
74 | | - |
75 | | -// 'main: loop { |
76 | | -// for event in events.poll_iter() { |
77 | | - |
78 | | -// match event { |
79 | | - |
80 | | -// Event::Quit {..} => break 'main, |
81 | | - |
82 | | -// Event::KeyDown {keycode: Some(keycode), ..} => { |
83 | | -// if keycode == Keycode::Escape { |
84 | | -// break 'main |
85 | | -// } else if keycode == Keycode::Space { |
86 | | -// println!("space down"); |
87 | | -// for i in 0..400 { |
88 | | -// driver.canvas.pixel(i as i16, i as i16, 0xFF000FFu32).unwrap(); |
89 | | -// } |
90 | | -// driver.canvas.present(); |
91 | | - |
92 | | -// } |
93 | | -// } |
94 | | - |
95 | | -// Event::MouseButtonDown {x, y, ..} => { |
96 | | -// let color = pixels::Color::RGB(x as u8, y as u8, 255); |
97 | | -// let _ = driver.canvas.line(lastx, lasty, x as i16, y as i16, color); |
98 | | -// let _ = driver.canvas.fill_rect(Rect::new(0,0,100,100)); |
99 | | - |
100 | | -// lastx = x as i16; |
101 | | -// lasty = y as i16; |
102 | | -// println!("mouse btn down at ({},{})", x, y); |
103 | | -// driver.canvas.present(); |
104 | | -// } |
105 | | - |
106 | | -// _ => {} |
107 | | -// } |
108 | | -// } |
109 | | -// } |
110 | | -// } |
| 1 | +use sdl2; |
| 2 | +use sdl2::pixels; |
| 3 | +use sdl2::rect::Rect; |
| 4 | +use sdl2::render::Canvas; |
| 5 | +use sdl2::video::Window; |
| 6 | + |
| 7 | +use CHIP8_HEIGHT; |
| 8 | +use CHIP8_WIDTH; |
| 9 | + |
| 10 | +const SCALE_FACTOR: u32 = 20; |
| 11 | +const SCREEN_WIDTH: u32 = (CHIP8_WIDTH as u32) * SCALE_FACTOR; |
| 12 | +const SCREEN_HEIGHT: u32 = (CHIP8_HEIGHT as u32) * SCALE_FACTOR; |
| 13 | + |
| 14 | +pub struct DisplayDriver { |
| 15 | + canvas: Canvas<Window>, |
| 16 | +} |
| 17 | + |
| 18 | +impl DisplayDriver { |
| 19 | + pub fn new(sdl_context: &sdl2::Sdl) -> Self { |
| 20 | + let video_subsys = sdl_context.video().unwrap(); |
| 21 | + let window = video_subsys |
| 22 | + .window( |
| 23 | + "rust-sdl2_gfx: draw line & FPSManager", |
| 24 | + SCREEN_WIDTH, |
| 25 | + SCREEN_HEIGHT, |
| 26 | + ) |
| 27 | + .position_centered() |
| 28 | + .opengl() |
| 29 | + .build() |
| 30 | + .unwrap(); |
| 31 | + |
| 32 | + let mut canvas = window.into_canvas().build().unwrap(); |
| 33 | + |
| 34 | + canvas.set_draw_color(pixels::Color::RGB(0, 0, 0)); |
| 35 | + canvas.clear(); |
| 36 | + canvas.present(); |
| 37 | + |
| 38 | + DisplayDriver { canvas: canvas } |
| 39 | + } |
| 40 | + |
| 41 | + pub fn draw(&mut self, pixels: &[u64; CHIP8_HEIGHT]) { |
| 42 | + pixels.iter().enumerate().for_each(|(y_index, y)| { |
| 43 | + (0usize..CHIP8_WIDTH).for_each(|x_index| { |
| 44 | + let _x_start = (x_index as u32) * SCALE_FACTOR; |
| 45 | + let _y_start = (y_index as u32) * SCALE_FACTOR; |
| 46 | + |
| 47 | + self.canvas |
| 48 | + .set_draw_color(color((*y >> (63 - x_index)) & 1)); |
| 49 | + |
| 50 | + let _ = self.canvas.fill_rect(Rect::new( |
| 51 | + _x_start as i32, |
| 52 | + _y_start as i32, |
| 53 | + SCALE_FACTOR, |
| 54 | + SCALE_FACTOR, |
| 55 | + )); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + self.canvas.present(); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +fn color(value: u64) -> pixels::Color { |
| 64 | + if value == 0 { |
| 65 | + pixels::Color::RGB(0, 0, 100) |
| 66 | + } else { |
| 67 | + pixels::Color::RGB(0, 150, 0) |
| 68 | + } |
| 69 | +} |
0 commit comments