(cocks shotgun) code's haunted
This commit is contained in:
parent
50b1649315
commit
41a4a016f3
|
@ -10,7 +10,7 @@ failure = "^0.1"
|
|||
libloading = "^0.5"
|
||||
num_enum = "^0.4"
|
||||
structopt = "^0.3"
|
||||
sdl2 = { version = "*", optional = true }
|
||||
sdl2 = { version = "*", optional = true, features = ["unsafe_textures"] }
|
||||
|
||||
[features]
|
||||
with-sdl2 = ["sdl2"]
|
||||
|
|
|
@ -1,8 +1,68 @@
|
|||
/*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;
|
||||
|
||||
use rustro::retro;
|
||||
use rustro::retro::ffi::GameGeometry;
|
||||
use rustro::retro::ffi::SystemAvInfo;
|
||||
|
||||
use std::ffi::CStr;
|
||||
use std::path::{PathBuf, Path};
|
||||
|
@ -10,16 +70,18 @@ use std::time::Duration;
|
|||
|
||||
use structopt::StructOpt;
|
||||
|
||||
use sdl2::render::{WindowCanvas, Texture};
|
||||
use sdl2::render::{WindowCanvas, Texture, TextureCreator};
|
||||
use sdl2::rect::Rect;
|
||||
use sdl2::event::Event;
|
||||
use sdl2::keyboard::Keycode;
|
||||
use sdl2::video::WindowContext;
|
||||
use std::io::Write;
|
||||
|
||||
struct MyEmulator {
|
||||
retro: retro::wrapper::LibretroWrapper,
|
||||
|
||||
sdl_context: sdl2::Sdl,
|
||||
video_subsystem: sdl2::VideoSubsystem,
|
||||
canvas: WindowCanvas,
|
||||
av_info: SystemAvInfo,
|
||||
pixel_format: sdl2::pixels::PixelFormatEnum,
|
||||
texture: Option<Texture>,
|
||||
}
|
||||
|
||||
impl MyEmulator {
|
||||
|
@ -30,31 +92,22 @@ impl MyEmulator {
|
|||
println!("api version: {}", retro.as_ref().api_version());
|
||||
println!("name: {}", unsafe { CStr::from_ptr(retro.as_ref().get_system_info().library_name) }.to_string_lossy());
|
||||
|
||||
let av = retro.as_ref().get_system_av_info();
|
||||
|
||||
let sdl_context = sdl2::init().unwrap();
|
||||
let video_subsystem = sdl_context.video().unwrap();
|
||||
|
||||
let window = video_subsystem.window(
|
||||
"rust libretro",
|
||||
av.geometry.base_width,
|
||||
av.geometry.base_height
|
||||
).position_centered().build().unwrap();
|
||||
|
||||
let mut canvas = window.into_canvas().build().unwrap();
|
||||
let av_info = retro.as_ref().get_system_av_info();
|
||||
|
||||
let pixel_format = sdl2::pixels::PixelFormatEnum::ABGR1555;
|
||||
let mut result = MyEmulator { retro, av_info, pixel_format, texture: None };
|
||||
|
||||
let mut result = MyEmulator { retro, sdl_context, video_subsystem, canvas, pixel_format };
|
||||
retro::wrapper::register_handler(&mut result);
|
||||
result.retro.as_ref().init();
|
||||
result
|
||||
}
|
||||
|
||||
pub fn run(&self) {
|
||||
println!("run?");
|
||||
self.retro.as_ref().run();
|
||||
println!("run.");
|
||||
}
|
||||
|
||||
pub fn base_dimensions(&self) -> (u32, u32) {
|
||||
(self.av_info.geometry.base_width, self.av_info.geometry.base_height)
|
||||
}
|
||||
|
||||
pub fn load_game(&self, rom: impl AsRef<Path>) {
|
||||
|
@ -64,23 +117,12 @@ impl MyEmulator {
|
|||
|
||||
impl retro::convert::Handler for MyEmulator {
|
||||
fn video_refresh(&mut self, data: &[u8], width: u32, height: u32, pitch: u32) {
|
||||
println!("video_refresh {}x{}", width, height);
|
||||
let rect = Rect::new(0, 0, width, height);
|
||||
println!("0");
|
||||
let tex_creator = self.canvas.texture_creator();
|
||||
println!("a");
|
||||
let mut tex: Texture = tex_creator.create_texture_streaming(
|
||||
self.pixel_format,
|
||||
width,
|
||||
height,
|
||||
).unwrap();
|
||||
println!("b");
|
||||
tex.update(None, data, pitch as usize);
|
||||
println!("c");
|
||||
self.canvas.copy(&tex, rect, rect);
|
||||
println!("d");
|
||||
self.canvas.present();
|
||||
println!("e");
|
||||
//println!("video_refresh {}x{}", width, height);
|
||||
//let rect = Rect::new(0, 0, width, height);
|
||||
println!("{:?}", self.texture.is_some());
|
||||
if let Some(ref mut tex) = self.texture {
|
||||
tex.update(None, data, pitch as usize).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
fn set_pixel_format(&mut self, pix_fmt: retro::ffi::PixelFormat) -> bool {
|
||||
|
@ -93,15 +135,44 @@ impl retro::convert::Handler for MyEmulator {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
pub fn main() -> failure::Fallible<()> {
|
||||
let opt: Opt = Opt::from_args();
|
||||
let mut emu = MyEmulator::with_core(&opt.core);
|
||||
emu.load_game(&opt.rom);
|
||||
|
||||
loop {
|
||||
let sdl_context = sdl2::init().map_err(failure::err_msg)?;
|
||||
let video_subsystem = sdl_context.video().map_err(failure::err_msg)?;
|
||||
|
||||
let (width, height) = emu.base_dimensions();
|
||||
let window = video_subsystem.window("rust libretro", width, height)
|
||||
.position_centered()
|
||||
.opengl()
|
||||
.build()?;
|
||||
|
||||
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 event_pump = sdl_context.event_pump().map_err(failure::err_msg)?;
|
||||
'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...
|
||||
emu.run();
|
||||
canvas.clear();
|
||||
canvas.copy(emu.texture.as_ref().unwrap(), None, Some(Rect::new(0, 0, width, height))).map_err(failure::err_msg)?;
|
||||
canvas.present();
|
||||
::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(StructOpt)]
|
||||
|
|
Loading…
Reference in New Issue