Skip to content

Commit

Permalink
Move core initialization to load_game for better ergonomics.
Browse files Browse the repository at this point in the history
  • Loading branch information
InquisitiveCoder committed Oct 11, 2022
1 parent 90b72a8 commit 86ab0c3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 29 deletions.
2 changes: 1 addition & 1 deletion libretro-rs/Cargo.toml
Expand Up @@ -4,7 +4,7 @@ description = "High-level abstractions for the libretro API"
readme = "README.md"
repository = "https://github.com/libretro-rs/libretro-rs"
license = "MIT/Apache-2.0"
version = "0.1.3"
version = "0.2.0"
authors = ["Adam Becker <im.adam.becker@gmail.com>"]
edition = "2021"

Expand Down
2 changes: 0 additions & 2 deletions libretro-rs/src/core_macro.rs
Expand Up @@ -34,12 +34,10 @@ macro_rules! libretro_core {

#[no_mangle]
extern "C" fn retro_init() {
instance_mut(|instance| instance.on_init())
}

#[no_mangle]
extern "C" fn retro_deinit() {
instance_mut(|instance| instance.on_deinit())
}

#[no_mangle]
Expand Down
42 changes: 16 additions & 26 deletions libretro-rs/src/lib.rs
Expand Up @@ -9,10 +9,6 @@ use sys::*;
pub trait RetroCore {
const SUPPORT_NO_GAME: bool = false;

/// Called when a new instance of the core is needed. The `env` parameter can be used to set-up and/or query values
/// required for the core to function properly.
fn init(env: &RetroEnvironment) -> Self;

/// Called to get information about the core. This information can then be displayed in a frontend, or used to
/// construct core-specific paths.
fn get_system_info() -> RetroSystemInfo;
Expand Down Expand Up @@ -49,7 +45,9 @@ pub trait RetroCore {

fn cheat_set(&mut self, env: &RetroEnvironment, index: u32, enabled: bool, code: *const libc::c_char) {}

fn load_game(&mut self, env: &RetroEnvironment, game: RetroGame) -> RetroLoadGameResult;
/// Called when a new instance of the core is needed. The `env` parameter can be used to set-up and/or query values
/// required for the core to function properly.
fn load_game(env: &RetroEnvironment, game: RetroGame) -> RetroLoadGameResult<Self> where Self: Sized;

fn load_game_special(&mut self, env: &RetroEnvironment, game_type: u32, info: RetroGame, num_info: usize) -> bool {
false
Expand Down Expand Up @@ -330,12 +328,13 @@ impl Into<u32> for RetroJoypadButton {
}

#[must_use]
pub enum RetroLoadGameResult {
pub enum RetroLoadGameResult<T> {
Failure,
Success {
region: RetroRegion,
audio: RetroAudioInfo,
video: RetroVideoInfo,
core: T,
},
}

Expand Down Expand Up @@ -570,23 +569,6 @@ impl<T: RetroCore> RetroInstance<T> {
info.timing.sample_rate = audio.sample_rate;
}

/// Invoked by a `libretro` frontend, with the `retro_init` API call.
pub fn on_init(&mut self) {
let env = self.environment();
self.system = Some(T::init(&env))
}

/// Invoked by a `libretro` frontend, with the `retro_deinit` API call.
pub fn on_deinit(&mut self) {
self.system = None;
self.audio_sample = None;
self.audio_sample_batch = None;
self.environment = None;
self.input_poll = None;
self.input_state = None;
self.video_refresh = None;
}

/// Invoked by a `libretro` frontend, with the `retro_set_environment` API call.
pub fn on_set_environment(&mut self, cb: retro_environment_t) {
self.environment = Some(RetroEnvironment::new(cb));
Expand Down Expand Up @@ -684,12 +666,13 @@ impl<T: RetroCore> RetroInstance<T> {
pub fn on_load_game(&mut self, game: &retro_game_info) -> bool {
let env = self.environment();

match self.core_mut(|core| core.load_game(&env, game.into())) {
match T::load_game(&env, game.into()) {
RetroLoadGameResult::Failure => {
self.system_av_info = None;
false
}
RetroLoadGameResult::Success { region, audio, video } => {
RetroLoadGameResult::Success { region, audio, video, core } => {
self.system = Some(core);
self.system_region = Some(region);
self.system_av_info = Some(RetroSystemAvInfo { audio, video });
true
Expand All @@ -706,7 +689,14 @@ impl<T: RetroCore> RetroInstance<T> {
/// Invoked by a `libretro` frontend, with the `retro_unload_game` API call.
pub fn on_unload_game(&mut self) {
let env = self.environment();
self.core_mut(|core| core.unload_game(&env))
self.core_mut(|core| core.unload_game(&env));
self.system = None;
self.audio_sample = None;
self.audio_sample_batch = None;
self.environment = None;
self.input_poll = None;
self.input_state = None;
self.video_refresh = None;
}

/// Invoked by a `libretro` frontend, with the `retro_get_region` API call.
Expand Down

0 comments on commit 86ab0c3

Please sign in to comment.