get component reference without rc<refcell> business

This commit is contained in:
lifning 2021-10-17 21:47:24 -07:00
parent 68d8657131
commit faf517ab98
1 changed files with 10 additions and 14 deletions

View File

@ -1,11 +1,9 @@
extern crate ferretro_components;
extern crate sdl2;
use std::cell::RefCell;
use std::ffi::CStr;
use std::path::PathBuf;
use std::pin::Pin;
use std::rc::Rc;
use sdl2::render::{WindowCanvas, BlendMode};
use sdl2::image::ImageRWops;
@ -18,8 +16,8 @@ use rand_xoshiro::Xoshiro128Plus;
use structopt::StructOpt;
use ferretro_components::base::{ControlFlow, RetroComponentBase};
use ferretro_components::prelude::LibretroWrapperAccess;
use ferretro_components::base::ControlFlow;
use ferretro_components::prelude::*;
use ferretro_components::provided::{
sdl2::{Sdl2SurfaceComponent, SimpleSdl2AudioComponent},
stdlib::{SleepFramerateLimitComponent, PathBufComponent},
@ -47,7 +45,7 @@ struct Zretro {
rng: Xoshiro128Plus,
ui_bg: Option<Surface<'static>>,
snowflakes: Vec<Rect>,
sdl2surf_comp: Rc<RefCell<Sdl2SurfaceComponent>>,
sdl2surf_comp_id: RetroComponentId,
}
const FONT_PNG: &'static [u8] = include_bytes!("lifont.png");
@ -72,23 +70,21 @@ impl Zretro {
.build()?;
let canvas = window.into_canvas().build()?;
let sdl2surf_comp = Rc::new(RefCell::new(
Sdl2SurfaceComponent::new(emu.libretro_core())?
));
emu.register_component(sdl2surf_comp.clone());
let sdl2surf_comp = Sdl2SurfaceComponent::new(emu.libretro_core())?;
let sdl2surf_comp_id = emu.register_component(sdl2surf_comp)?;
let sdl2_audio = SimpleSdl2AudioComponent::new(&mut sdl_context, emu.libretro_core());
emu.register_component(sdl2_audio);
emu.register_component(sdl2_audio)?;
let sleep_fps = SleepFramerateLimitComponent::new(emu.libretro_core());
emu.register_component(sleep_fps);
emu.register_component(sleep_fps)?;
emu.register_component(PathBufComponent {
sys_path: opt.system.clone(),
libretro_path: Some(opt.core.to_path_buf()),
core_assets_path: None,
save_path: Some(std::env::temp_dir()),
});
})?;
emu.init().unwrap();
emu.load_game(&opt.rom).unwrap();
@ -119,7 +115,7 @@ impl Zretro {
rng,
ui_bg: Some(ui_bg),
snowflakes,
sdl2surf_comp,
sdl2surf_comp_id,
})
}
@ -148,7 +144,7 @@ impl Zretro {
font_rect.set_height(64);
while let ControlFlow::Continue = self.emu.run() {
self.update_snow()?;
let ref_mut = RefCell::try_borrow_mut(&self.sdl2surf_comp)?;
let ref_mut = self.emu.component_mut::<Sdl2SurfaceComponent>(self.sdl2surf_comp_id)?;
let surface = ref_mut.surface();
let emu_tx = tc.create_texture_from_surface(surface)?;