Skip to content
This repository was archived by the owner on Jan 31, 2023. It is now read-only.

Commit ae3e4bd

Browse files
committed
Draw canvas
Using `canvas.draw_point`.
1 parent 684057d commit ae3e4bd

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

src/nes/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ pub fn run(ctx: &mut Context, key_state: u8) {
8686
}
8787
}
8888

89+
pub fn get_render_buf(ctx: &mut Context) -> &Vec<u8> {
90+
ctx.renderer.get_buf()
91+
}
92+
8993
impl Context {
9094
pub fn new(buf: &mut [Data]) -> Self {
9195
let cassette = parser::parse(buf);

src/nes/renderer/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ impl Renderer {
2828
}
2929
}
3030

31+
pub fn get_buf(&self) -> &Vec<u8> {
32+
&self.buf
33+
}
34+
3135
fn should_pixel_hide(&self, x: usize, y: usize, background: &BackgroundField) -> bool {
3236
let tile_x = x / 8;
3337
let tile_y = y / 8;
@@ -119,4 +123,4 @@ impl Renderer {
119123
}
120124
}
121125
}
122-
}
126+
}

standalone/src/main.rs

+21-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use sdl2::event::{Event};
66
use sdl2::keyboard::{Keycode};
77
use sdl2::pixels::{Color};
88
use sdl2::render::{WindowCanvas};
9+
use sdl2::rect::{Point};
910

1011
use std::time::{Duration};
1112

@@ -48,7 +49,6 @@ impl App {
4849

4950
pub fn run(&mut self) {
5051
let mut event_pump = self.sdl_context.event_pump().unwrap();
51-
let mut i = 0;
5252
'running: loop {
5353
for event in event_pump.poll_iter() {
5454
match event {
@@ -61,11 +61,7 @@ impl App {
6161
}
6262

6363
self.update();
64-
65-
i = (i + 1) % 255;
66-
self.canvas.set_draw_color(Color::RGB(i, 64, 255 - i));
67-
self.canvas.clear();
68-
64+
self.render();
6965
self.canvas.present();
7066
::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
7167
}
@@ -81,6 +77,25 @@ impl App {
8177
None => (),
8278
}
8379
}
80+
81+
fn render(&mut self) {
82+
match &mut self.ctx {
83+
Some(ctx) => {
84+
let buf = nes::get_render_buf(ctx);
85+
for i in 0..HEIGHT {
86+
for j in 0..WIDTH {
87+
let base = ((i * WIDTH + j) * 4) as usize;
88+
let r = buf[base + 0];
89+
let g = buf[base + 1];
90+
let b = buf[base + 2];
91+
self.canvas.set_draw_color(Color::RGB(r, g, b));
92+
let _ = self.canvas.draw_point(Point::new(j as i32, i as i32));
93+
}
94+
}
95+
},
96+
None => (),
97+
}
98+
}
8499
}
85100

86101
#[no_mangle]

0 commit comments

Comments
 (0)