-
Notifications
You must be signed in to change notification settings - Fork 50
/
lib.rs
153 lines (128 loc) · 4.36 KB
/
lib.rs
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
extern crate nes;
extern crate emumisc;
#[macro_use]
extern crate libretro_backend;
use libretro_backend::{CoreInfo, AudioVideoInfo, PixelFormat, GameData, LoadGameResult, Region, RuntimeHandle, JoypadButton};
use nes::{Palette, ControllerPort, Button};
struct PinkyCore {
state: nes::State,
palette: [u32; 512],
framebuffer: [u32; 256 * 240],
audio_buffer: Vec< i16 >,
game_data: Option< GameData >
}
impl nes::Context for PinkyCore {
#[inline]
fn state_mut( &mut self ) -> &mut nes::State {
&mut self.state
}
#[inline]
fn state( &self ) -> &nes::State {
&self.state
}
#[inline]
fn on_audio_sample( &mut self, sample: f32 ) {
let value = if sample >= 1.0 {
32767
} else if sample <= -1.0 {
-32768
} else {
(sample * 32767.0) as i16
};
self.audio_buffer.push( value );
self.audio_buffer.push( value );
}
}
fn palette_to_argb( palette: &Palette ) -> [u32; 512] {
let mut output = [0; 512];
for (index, out) in output.iter_mut().enumerate() {
let (r, g, b) = palette.get_rgb( index as u16 );
*out = ((r as u32) << 16) |
((g as u32) << 8) |
((b as u32) );
}
output
}
impl PinkyCore {
fn new() -> PinkyCore {
PinkyCore {
state: nes::State::new(),
palette: palette_to_argb( &Palette::default() ),
framebuffer: [0; 256 * 240],
audio_buffer: Vec::with_capacity( 44100 ),
game_data: None
}
}
}
impl Default for PinkyCore {
fn default() -> Self {
Self::new()
}
}
impl libretro_backend::Core for PinkyCore {
fn info() -> CoreInfo {
CoreInfo::new( "Pinky", env!( "CARGO_PKG_VERSION" ) )
.supports_roms_with_extension( "nes" )
}
fn on_load_game( &mut self, game_data: GameData ) -> LoadGameResult {
if game_data.is_empty() {
return LoadGameResult::Failed( game_data );
}
let result = if let Some( data ) = game_data.data() {
nes::Interface::load_rom( self, data )
} else if let Some( path ) = game_data.path() {
let data = match std::fs::read( path ) {
Ok( data ) => data,
Err( _ ) => {
return LoadGameResult::Failed( game_data );
}
};
nes::Interface::load_rom( self, &data )
} else {
unreachable!();
};
match result {
Ok( _ ) => {
self.game_data = Some( game_data );
let av_info = AudioVideoInfo::new()
.video( 256, 240, 60.0, PixelFormat::ARGB8888 )
.audio( 44100.0 )
.region( Region::NTSC );
LoadGameResult::Success( av_info )
},
Err( _ ) => {
LoadGameResult::Failed( game_data )
}
}
}
fn on_unload_game( &mut self ) -> GameData {
self.game_data.take().unwrap()
}
fn on_run( &mut self, handle: &mut RuntimeHandle ) {
macro_rules! update_controllers {
( $( $button:ident ),+ ) => (
$(
nes::Interface::set_button_state( self, ControllerPort::First, Button::$button, handle.is_joypad_button_pressed( 0, JoypadButton::$button ) );
nes::Interface::set_button_state( self, ControllerPort::Second, Button::$button, handle.is_joypad_button_pressed( 1, JoypadButton::$button ) );
)+
)
}
update_controllers!( A, B, Start, Select, Left, Up, Right, Down );
if let Err( error ) = nes::Interface::execute_for_a_frame( self ) {
println!( "Execution error: {}", error );
return;
}
let framebuffer = self.state.framebuffer();
for (pixel_in, pixel_out) in framebuffer.iter().zip( self.framebuffer.iter_mut() ) {
*pixel_out = self.palette[ pixel_in.full_color_index() as usize ];
}
let video_frame = emumisc::as_bytes( &self.framebuffer[..] );
handle.upload_video_frame( video_frame );
handle.upload_audio_frame( &self.audio_buffer[..] );
self.audio_buffer.clear();
}
fn on_reset( &mut self ) {
nes::Interface::soft_reset( self );
}
}
libretro_core!( PinkyCore );