Skip to content

Commit

Permalink
Merge pull request #4 from InquisitiveCoder/no-init
Browse files Browse the repository at this point in the history
Move core initialization to load_game for better ergonomics
  • Loading branch information
adam-becker committed Nov 23, 2022
2 parents 91817ec + 6995d66 commit faff74a
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 22 deletions.
2 changes: 1 addition & 1 deletion example/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "example"
version = "0.1.0"
authors = ["Adam Becker <im.adam.becker@gmail.com>"]
authors = ["Adam Becker <apbecker@protonmail.com>"]
edition = "2021"

[dependencies]
Expand Down
14 changes: 5 additions & 9 deletions example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@ use libretro_rs::*;
pub struct Emulator;

impl RetroCore for Emulator {
fn init(env: &RetroEnvironment) -> Self {
let system_dir = env.get_system_directory().unwrap_or("~/.config/emulator");

println!("[libretro_rs] new(). system_dir={}", system_dir);

Emulator
}

fn get_system_info() -> RetroSystemInfo {
println!("[libretro_rs] get_system_info()");

Expand All @@ -29,7 +21,10 @@ impl RetroCore for Emulator {
println!("[libretro_rs] run()");
}

fn load_game(&mut self, _: &RetroEnvironment, game: RetroGame) -> RetroLoadGameResult {
fn load_game(env: &RetroEnvironment, game: RetroGame) -> RetroLoadGameResult<Self> {
let system_dir = env.get_system_directory().unwrap_or("~/.config/emulator");
println!("[libretro_rs] load_game(). system_dir={}", system_dir);

match game {
RetroGame::None { .. } => {
println!("[libretro_rs] load_game()");
Expand All @@ -47,6 +42,7 @@ impl RetroCore for Emulator {
region: RetroRegion::NTSC,
audio: RetroAudioInfo::new(44_100.0),
video: RetroVideoInfo::new(60.0, 256, 240),
core: Emulator,
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions libretro-rs-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
[package]
name = "libretro-rs-sys"
repository = "https://github.com/libretro-rs/libretro-rs"
license = "MIT/Apache-2.0"
version = "0.1.0"
authors = ["Adam Becker <apbecker@protonmail.com>"]
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion libretro-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ readme = "README.md"
repository = "https://github.com/libretro-rs/libretro-rs"
license = "MIT/Apache-2.0"
version = "0.1.3"
authors = ["Adam Becker <im.adam.becker@gmail.com>"]
authors = ["Adam Becker <apbecker@protonmail.com>"]
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion libretro-rs/src/core_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ macro_rules! libretro_core {

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

#[no_mangle]
Expand Down
24 changes: 14 additions & 10 deletions libretro-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use std::ffi::{CStr, CString};
use sys::*;

#[allow(unused_variables)]
pub trait RetroCore {
pub trait RetroCore: Sized {
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 during `retro_init()`. This function is provided for the sake of completeness; it's generally redundant
/// with [load_game].
fn init(env: &RetroEnvironment) {}

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

fn cheat_set(&mut self, env: &RetroEnvironment, index: u32, enabled: bool, code: &str) {}

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>;

fn load_game_special(&mut self, env: &RetroEnvironment, game_type: u32, info: RetroGame, num_info: usize) -> bool {
false
Expand Down Expand Up @@ -336,12 +338,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 @@ -586,9 +589,9 @@ impl<T: RetroCore> RetroInstance<T> {
}

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

/// Invoked by a `libretro` frontend, with the `retro_deinit` API call.
Expand Down Expand Up @@ -718,12 +721,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 Down

0 comments on commit faff74a

Please sign in to comment.