From 7406ce9252d917dddd1b0650354c3def391732fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= <joamag@gmail.com> Date: Mon, 7 Aug 2023 10:18:51 +0100 Subject: [PATCH] chore: improved structure of info constants Using static methods instead of global functions. --- README.md | 1 + frontends/libretro/src/lib.rs | 6 ++--- frontends/sdl/src/main.rs | 6 ++--- frontends/web/ts/gb.ts | 18 ++++++------- src/gb.rs | 24 ++---------------- src/info.rs | 48 +++++++++++++++++++++++++---------- src/state.rs | 7 ++--- 7 files changed, 54 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index 7b8c7221..8f3c0e98 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ A Game Boy emulator that is written in Rust 🦀. * Serial Data Transfer ([Link Cable](https://en.wikipedia.org/wiki/Game_Link_Cable)) support * Game Boy Printer emulation * Support for multiple MBCs: MBC1, MBC2, MBC3, and MBC5 +* Save state using the [BESS Specification](https://github.com/LIJI32/SameBoy/blob/master/BESS.md) for cross-compatibility with other emulators * Cheat support using [Game Genie](https://en.wikipedia.org/wiki/Game_Genie) codes * Variable CPU clock speed * Accurate PPU - passes [dmg-acid2](https://github.com/mattcurrie/dmg-acid2) and [cgb-acid2](https://github.com/mattcurrie/cgb-acid2) tests diff --git a/frontends/libretro/src/lib.rs b/frontends/libretro/src/lib.rs index a3df43e3..fd4cdcdc 100644 --- a/frontends/libretro/src/lib.rs +++ b/frontends/libretro/src/lib.rs @@ -5,7 +5,7 @@ pub mod consts; use boytacean::{ debugln, gb::{AudioProvider, GameBoy}, - info::{name, version}, + info::Info, pad::PadKey, ppu::{DISPLAY_HEIGHT, DISPLAY_WIDTH, FRAME_BUFFER_SIZE, XRGB8888_SIZE}, rom::Cartridge, @@ -173,8 +173,8 @@ pub extern "C" fn retro_reset() { #[no_mangle] pub unsafe extern "C" fn retro_get_system_info(info: *mut RetroSystemInfo) { debugln!("retro_get_system_info()"); - (*info).library_name = format!("{}\0", name()).as_ptr() as *const c_char; - (*info).library_version = format!("v{}\0", version()).as_ptr() as *const c_char; + (*info).library_name = format!("{}\0", Info::name()).as_ptr() as *const c_char; + (*info).library_version = format!("v{}\0", Info::version()).as_ptr() as *const c_char; (*info).valid_extensions = "gb|gbc\0".as_ptr() as *const c_char; (*info).need_fullpath = false; (*info).block_extract = false; diff --git a/frontends/sdl/src/main.rs b/frontends/sdl/src/main.rs index 3c4a4971..0483f147 100644 --- a/frontends/sdl/src/main.rs +++ b/frontends/sdl/src/main.rs @@ -9,7 +9,7 @@ use audio::Audio; use boytacean::{ devices::{printer::PrinterDevice, stdout::StdoutDevice}, gb::{AudioProvider, GameBoy, GameBoyMode}, - info::{name, version}, + info::Info, pad::PadKey, ppu::PaletteInfo, rom::Cartridge, @@ -100,7 +100,7 @@ impl Emulator { unlimited: options.unlimited.unwrap_or(false), sdl: None, audio: None, - title: format!("{} v{}", name(), version()), + title: format!("{} v{}", Info::name(), Info::version()), rom_path: String::from("invalid"), ram_path: String::from("invalid"), logic_frequency: GameBoy::CPU_FREQ, @@ -862,7 +862,7 @@ fn main() { game_boy.load(!args.no_boot); // prints the current version of the emulator (informational message) - println!("========= {} =========\n{}", name(), game_boy); + println!("========= {} =========\n{}", Info::name(), game_boy); // creates a new generic emulator structure then starts // both the video and audio sub-systems, loads default diff --git a/frontends/web/ts/gb.ts b/frontends/web/ts/gb.ts index 38f9a00b..d4902c10 100644 --- a/frontends/web/ts/gb.ts +++ b/frontends/web/ts/gb.ts @@ -31,13 +31,11 @@ import { import { Cartridge, default as _wasm, - name, GameBoy, PadKey, GameBoyMode, GameBoySpeed, - version, - system + Info } from "../lib/boytacean"; import info from "../package.json"; @@ -493,12 +491,12 @@ export class GameboyEmulator extends EmulatorBase implements Emulator { } get name(): string { - return name() ?? info.name; + return Info.name() ?? info.name; } get device(): Entry { return { - text: system(), + text: Info.system(), url: "https://en.wikipedia.org/wiki/Game_Boy" }; } @@ -509,7 +507,7 @@ export class GameboyEmulator extends EmulatorBase implements Emulator { get version(): Entry | undefined { return { - text: version() ?? info.version, + text: Info.version() ?? info.version, url: "https://github.com/joamag/boytacean/blob/master/CHANGELOG.md" }; } @@ -665,16 +663,16 @@ export class GameboyEmulator extends EmulatorBase implements Emulator { get compiler(): Compiler | null { if (!this.gameBoy) return null; return { - name: this.gameBoy.compiler(), - version: this.gameBoy.compiler_version() + name: Info.compiler(), + version: Info.compiler_version() }; } get compilation(): Compilation | null { if (!this.gameBoy) return null; return { - date: this.gameBoy.compilation_date(), - time: this.gameBoy.compilation_time() + date: Info.compilation_date(), + time: Info.compilation_time() }; } diff --git a/src/gb.rs b/src/gb.rs index 0e8d51fd..88e5f71d 100644 --- a/src/gb.rs +++ b/src/gb.rs @@ -13,9 +13,8 @@ use crate::{ data::{BootRom, CGB_BOOT, DMG_BOOT, DMG_BOOTIX, MGB_BOOTIX, SGB_BOOT}, devices::{printer::PrinterDevice, stdout::StdoutDevice}, dma::Dma, - gen::{COMPILATION_DATE, COMPILATION_TIME, COMPILER, COMPILER_VERSION}, genie::{GameGenie, GameGenieCode}, - info::version, + info::Info, mmu::Mmu, pad::{Pad, PadKey}, ppu::{ @@ -704,25 +703,6 @@ impl GameBoy { tile.palette_buffer(self.ppu().palette_bg()) } - /// Obtains the name of the compiler that has been - /// used in the compilation of the base Boytacean - /// library. Can be used for diagnostics. - pub fn compiler(&self) -> String { - String::from(COMPILER) - } - - pub fn compiler_version(&self) -> String { - String::from(COMPILER_VERSION) - } - - pub fn compilation_date(&self) -> String { - String::from(COMPILATION_DATE) - } - - pub fn compilation_time(&self) -> String { - String::from(COMPILATION_TIME) - } - pub fn is_dmg(&self) -> bool { self.mode == GameBoyMode::Dmg } @@ -867,7 +847,7 @@ impl GameBoy { format!( "{} {}\n{} {}\n{} {}\n{} {}\n{} {}\n{} {}", version_l, - version(), + Info::version(), mode_l, self.mode(), clock_l, diff --git a/src/info.rs b/src/info.rs index dc5249f0..91fc262e 100644 --- a/src/info.rs +++ b/src/info.rs @@ -1,27 +1,49 @@ //! General information about the crate and the emulator. use crate::{ - gen::{NAME, VERSION}, + gen::{COMPILATION_DATE, COMPILATION_TIME, COMPILER, COMPILER_VERSION, NAME, VERSION}, util::capitalize, }; #[cfg(feature = "wasm")] use wasm_bindgen::prelude::*; -/// Obtains the name of the emulator. #[cfg_attr(feature = "wasm", wasm_bindgen)] -pub fn name() -> String { - capitalize(NAME) -} +pub struct Info; -/// Obtains the version of the emulator. #[cfg_attr(feature = "wasm", wasm_bindgen)] -pub fn version() -> String { - String::from(VERSION) -} +impl Info { + /// Obtains the name of the emulator. + pub fn name() -> String { + capitalize(NAME) + } -/// Obtains the system this emulator is emulating. -#[cfg_attr(feature = "wasm", wasm_bindgen)] -pub fn system() -> String { - String::from("Game Boy") + /// Obtains the version of the emulator. + pub fn version() -> String { + String::from(VERSION) + } + + /// Obtains the system this emulator is emulating. + pub fn system() -> String { + String::from("Game Boy") + } + + /// Obtains the name of the compiler that has been + /// used in the compilation of the base Boytacean + /// library. Can be used for diagnostics. + pub fn compiler() -> String { + String::from(COMPILER) + } + + pub fn compiler_version() -> String { + String::from(COMPILER_VERSION) + } + + pub fn compilation_date() -> String { + String::from(COMPILATION_DATE) + } + + pub fn compilation_time() -> String { + String::from(COMPILATION_TIME) + } } diff --git a/src/state.rs b/src/state.rs index 12b55d83..65b9a750 100644 --- a/src/state.rs +++ b/src/state.rs @@ -6,10 +6,7 @@ use std::{ mem::size_of, }; -use crate::{ - gb::GameBoy, - info::{name, version}, -}; +use crate::{gb::GameBoy, info::Info}; pub trait Serialize { fn save(&self, buffer: &mut Vec<u8>); @@ -268,7 +265,7 @@ impl Serialize for BeesName { impl State for BeesName { fn from_gb(_gb: &GameBoy) -> Self { - Self::new(format!("{} v{}", name(), version())) + Self::new(format!("{} v{}", Info::name(), Info::version())) } fn to_gb(&self, _gb: &mut GameBoy) {} -- GitLab