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
pub mod key_assign;
use anyhow::Result;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use crate::key_assign::{InputState, KeyAssign};
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) {
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)]
pub struct Pixel {
pub r: u8,
pub g: u8,
pub b: u8,
}
impl Pixel {
pub fn new(r: u8, g: u8, b: u8) -> Self {
Self { r, g, b }
}
}
#[derive(Default)]
pub struct AudioBuffer {
pub samples: Vec<AudioSample>,
}
#[derive(Default, Clone)]
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 keys: Vec<(String, KeyAssign)>,
}
impl KeyConfig {
pub fn input(&self, input_state: &InputState) -> InputData {
let mut inputs = Vec::with_capacity(self.keys.len());
for (key, assign) in &self.keys {
inputs.push((key.clone(), assign.pressed(input_state)));
}
InputData { inputs }
}
}
#[derive(Default)]
pub struct InputData {
pub inputs: Vec<(String, bool)>,
}
impl InputData {
pub fn get(&self, key: &str) -> bool {
self.inputs
.iter()
.find_map(|(k, v)| (k == key).then(|| *v))
.unwrap()
}
}
pub trait ConfigUi {
fn ui(&mut self, ui: &mut egui::Ui);
}
pub trait EmulatorCore {
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>
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<()>;
}