...am i getting the wrong object back in my Handler-dispatch code? i probably am.

This commit is contained in:
lif 2019-11-15 02:05:37 -08:00
parent 33b3270c10
commit d3f83c48c1
1 changed files with 14 additions and 60 deletions

View File

@ -1,62 +1,3 @@
/*extern crate sdl2;
use sdl2::pixels::PixelFormatEnum;
use sdl2::rect::Rect;
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
pub fn main() -> Result<(), String> {
let sdl_context = sdl2::init()?;
let video_subsystem = sdl_context.video()?;
let window = video_subsystem.window("rust-sdl2 demo: Video", 800, 600)
.position_centered()
.opengl()
.build()
.map_err(|e| e.to_string())?;
let mut canvas = window.into_canvas().build().map_err(|e| e.to_string())?;
let texture_creator = canvas.texture_creator();
let mut texture = texture_creator.create_texture_streaming(PixelFormatEnum::RGB24, 256, 256)
.map_err(|e| e.to_string())?;
// Create a red-green gradient
texture.with_lock(None, |buffer: &mut [u8], pitch: usize| {
for y in 0..256 {
for x in 0..256 {
let offset = y*pitch + x*3;
buffer[offset] = x as u8;
buffer[offset + 1] = y as u8;
buffer[offset + 2] = 0;
}
}
})?;
canvas.clear();
canvas.copy(&texture, None, Some(Rect::new(100, 100, 256, 256)))?;
canvas.copy_ex(&texture, None,
Some(Rect::new(450, 100, 256, 256)), 30.0, None, false, false)?;
canvas.present();
let mut event_pump = sdl_context.event_pump()?;
'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...
}
Ok(())
}
*/
extern crate rustro;
extern crate sdl2;
@ -117,6 +58,7 @@ impl MyEmulator {
impl retro::convert::Handler for MyEmulator {
fn video_refresh(&mut self, data: &[u8], width: u32, height: u32, pitch: u32) {
println!("self ref: {:?}", self as *mut Self);
//println!("video_refresh {}x{}", width, height);
//let rect = Rect::new(0, 0, width, height);
if let Some(tex) = self.texture.as_mut() {
@ -159,7 +101,9 @@ pub fn main() -> failure::Fallible<()> {
let mut canvas = window.into_canvas().build()?;
let texture_creator = canvas.texture_creator();
emu.texture = texture_creator.create_texture_streaming(emu.pixel_format, width, height).ok();
println!("{:?}", emu.texture.is_some());
let mut x: u8 = 1;
println!("emu ref: {:?}", &emu as *const MyEmulator);
let mut event_pump = sdl_context.event_pump().map_err(failure::err_msg)?;
'running: loop {
@ -172,6 +116,16 @@ pub fn main() -> failure::Fallible<()> {
_ => {}
}
}
if let Some(tex) = emu.texture.as_mut() {
tex.with_lock(None, |tex_buffer: &mut [u8], tex_pitch: usize| {
for i in 0..tex_buffer.len() {
tex_buffer[i] = x;
x = (x + 1) % 255;
}
}).unwrap();
//tex.update(None, data, pitch as usize).unwrap();
}
// The rest of the game loop goes here...
emu.run();
canvas.clear();