1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
pub mod key_assign;
use std::path::PathBuf;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
pub use crate::key_assign::{
Gamepad, GamepadAxis, GamepadAxisType, GamepadButton, GamepadButtonType, InputState, KeyAssign,
KeyCode, MultiKey, SingleKey,
};
pub struct CoreInfo {
pub system_name: &'static str,
pub abbrev: &'static str,
pub file_extensions: &'static [&'static str],
}
#[derive(Default)]
pub struct FrameBuffer {
pub width: usize,
pub height: usize,
pub buffer: Vec<Pixel>,
}
impl FrameBuffer {
pub fn new(width: usize, height: usize) -> Self {
let mut ret = Self::default();
ret.resize(width, height);
ret
}
pub fn resize(&mut self, width: usize, height: usize) {
if (width, height) == (self.width, self.height) {
return;
}
self.width = width;
self.height = height;
self.buffer.resize(width * height, Pixel::default());
}
pub fn pixel(&self, x: usize, y: usize) -> &Pixel {
&self.buffer[y * self.width + x]
}
pub fn pixel_mut(&mut self, x: usize, y: usize) -> &mut Pixel {
&mut self.buffer[y * self.width + x]
}
}
#[derive(Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Pixel {
pub r: u8,
pub g: u8,
pub b: u8,
}
impl Pixel {
pub const fn new(r: u8, g: u8, b: u8) -> Self {
Self { r, g, b }
}
}
pub struct AudioBuffer {
pub sample_rate: u32,
pub channels: u16,
pub samples: Vec<AudioSample>,
}
impl Default for AudioBuffer {
fn default() -> Self {
Self {
sample_rate: 48000,
channels: 2,
samples: vec![],
}
}
}
impl AudioBuffer {
pub fn new(sample_rate: u32, channels: u16) -> Self {
Self {
sample_rate,
channels,
samples: vec![],
}
}
}
#[derive(Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AudioSample {
pub left: i16,
pub right: i16,
}
impl AudioSample {
pub fn new(left: i16, right: i16) -> Self {
Self { left, right }
}
}
#[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)]
pub struct KeyConfig {
pub controllers: Vec<Vec<(String, KeyAssign)>>,
}
impl KeyConfig {
pub fn input(&self, input_state: &impl InputState) -> InputData {
let controllers = self
.controllers
.iter()
.map(|keys| {
keys.iter()
.map(|(key, assign)| (key.clone(), assign.pressed(input_state)))
.collect()
})
.collect();
InputData { controllers }
}
}
#[derive(Default)]
pub struct InputData {
pub controllers: Vec<Vec<(String, bool)>>,
}
pub trait ConfigUi {
fn ui(&mut self, ui: &mut impl Ui);
}
pub trait Ui {
fn horizontal(&mut self, f: impl FnOnce(&mut Self));
fn enabled(&mut self, enabled: bool, f: impl FnOnce(&mut Self));
fn label(&mut self, text: &str);
fn checkbox(&mut self, value: &mut bool, text: &str);
fn file(&mut self, label: &str, value: &mut Option<PathBuf>, filter: &[(&str, &[&str])]);
fn color(&mut self, value: &mut Pixel);
fn radio<T: PartialEq + Clone>(&mut self, value: &mut T, choices: &[(&str, T)]);
fn combo_box<T: PartialEq + Clone>(&mut self, value: &mut T, choices: &[(&str, T)]);
}
pub trait EmulatorCore {
type Error: std::error::Error + Send + Sync + 'static;
type Config: ConfigUi + Serialize + DeserializeOwned + Default;
fn core_info() -> &'static CoreInfo;
fn try_from_file(
data: &[u8],
backup: Option<&[u8]>,
config: &Self::Config,
) -> Result<Self, Self::Error>
where
Self: Sized;
fn game_info(&self) -> Vec<(String, String)>;
fn set_config(&mut self, config: &Self::Config);
fn exec_frame(&mut self, render_graphics: bool);
fn reset(&mut self);
fn frame_buffer(&self) -> &FrameBuffer;
fn audio_buffer(&self) -> &AudioBuffer;
fn default_key_config() -> KeyConfig;
fn set_input(&mut self, input: &InputData);
fn backup(&self) -> Option<Vec<u8>>;
fn save_state(&self) -> Vec<u8>;
fn load_state(&mut self, data: &[u8]) -> Result<(), Self::Error>;
}