Skip to content

Commit a6df131

Browse files
author
A
committed
Now displays ARGB8888 pixel buffers correctly and shows the interface for 2048
1 parent 440cbc2 commit a6df131

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

README.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2211,7 +2211,7 @@ I will need to test on other platforms and with other controllers to figure out
22112211
We have all the required functionality for playing Game Boy games now but we aren't really a LibRetro frontend if we only support one core, so lets start implementing some setting that will enable more libRetro cores to run in our frontend.
22122212

22132213

2214-
Fiest of all we need to refactor the core setup a little so we can get the requires FPS that the core want to run at, otherwise the core will either run too fast or too slow on our frontend. We can do this using the `av_info` object we got from the core earlier:
2214+
First of all we need to refactor the core setup a little so we can get the requires FPS that the core want to run at, otherwise the core will either run too fast or too slow on our frontend. We can do this using the `av_info` object we got from the core earlier:
22152215

22162216
```rust
22172217
let mut av_info = SystemAvInfo {
@@ -2244,4 +2244,38 @@ let mut av_info = SystemAvInfo {
22442244

22452245

22462246

2247+
We could try another emulator core but first lets try a standalong core, you can download the 2048 core from [LibRetro Nightly](https://buildbot.libretro.com/nightly/apple/osx/x86_64/latest/) and since we are going to be testing a bunch of different cores lets put them all in a ./cores directory.
2248+
2249+
We can now run 2048 with the following command:
2250+
2251+
```rust
2252+
cargo build --release && ./target/release/rustro_arch Tetris.gb -L ./cores/2048_libretro.dylib # Change the .dylib to .dll on windows and .so on Linux
2253+
```
2254+
2255+
Note that we still pass the GameBoy ROM in as our parameter parsing gives an error if it is not there.
2256+
2257+
When you run you will notice that something has gone horribly wrong:
2258+
2259+
![2048 with wrong Piel Format](./screenshots/2048_Wrong_PixelFormat.jpeg)
2260+
2261+
This is caused by us presuming the Pixel Format is `RGB565` which it was in GamBatte, but the `2048` core uses `ARGB8888` which is the same as `minifb` uses so no conversion is nessecary, so lets stop auto converting the frame buffer to `RBG565` by modifying the code inside the `libretro_set_video_refresh_callback` function like so:
2262+
2263+
```rust
2264+
let result = match CURRENT_EMULATOR_STATE.pixel_format {
2265+
PixelFormat::RGB565 => Vec::from(convert_pixel_array_from_rgb565_to_xrgb8888(buffer_slice)),
2266+
PixelFormat::ARGB8888 => std::slice::from_raw_parts(buffer_slice.as_ptr() as *const u32, buffer_slice.len()).to_vec(),
2267+
_ => panic!("Unknown Pixel Format {:?}", CURRENT_EMULATOR_STATE.pixel_format)
2268+
};
2269+
2270+
// Create a Vec<u8> from the slice
2271+
2272+
// Wrap the Vec<u8> in an Option and assign it to the frame_buffer field
2273+
CURRENT_EMULATOR_STATE.frame_buffer = Some(result);
2274+
```
2275+
2276+
If you run the code it will provide a much more reasonable interface:
2277+
2278+
![2048 Working](./screenshots/2048_working.jpeg)
2279+
2280+
22472281
# Step ? - Cheating
419 KB
Loading

screenshots/2048_working.jpeg

50.7 KB
Loading

src/main.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,16 @@ unsafe extern "C" fn libretro_set_video_refresh_callback(
129129
frame_buffer_data as *const u8,
130130
length_of_frame_buffer as usize,
131131
);
132-
let result = convert_pixel_array_from_rgb565_to_xrgb8888(buffer_slice);
132+
let result = match CURRENT_EMULATOR_STATE.pixel_format {
133+
PixelFormat::RGB565 => Vec::from(convert_pixel_array_from_rgb565_to_xrgb8888(buffer_slice)),
134+
PixelFormat::ARGB8888 => std::slice::from_raw_parts(buffer_slice.as_ptr() as *const u32, buffer_slice.len()).to_vec(),
135+
_ => panic!("Unknown Pixel Format {:?}", CURRENT_EMULATOR_STATE.pixel_format)
136+
};
133137

134138
// Create a Vec<u8> from the slice
135-
let buffer_vec = Vec::from(result);
136139

137-
// Wrap the Vec<u8> in an Some Option and assign it to the frame_buffer field
138-
CURRENT_EMULATOR_STATE.frame_buffer = Some(buffer_vec);
140+
// Wrap the Vec<u8> in an Option and assign it to the frame_buffer field
141+
CURRENT_EMULATOR_STATE.frame_buffer = Some(result);
139142
CURRENT_EMULATOR_STATE.screen_height = height;
140143
CURRENT_EMULATOR_STATE.screen_width = width;
141144
CURRENT_EMULATOR_STATE.screen_pitch = pitch as u32;
@@ -758,6 +761,7 @@ fn main() {
758761
if slice_of_pixel_buffer.len() < width * height * 4 {
759762
// The frame buffer isn't big enough so lets add additional pixels just so we can display it
760763
let mut vec: Vec<u32> = slice_of_pixel_buffer.to_vec();
764+
println!("Frame Buffer wasn't big enough");
761765
vec.resize((width * height * 4) as usize, 0x0000FFFF); // Add any missing pixels with colour blue
762766
window.update_with_buffer(&vec, width, height).unwrap();
763767
} else {

0 commit comments

Comments
 (0)