Skip to content

Commit

Permalink
update to use png
Browse files Browse the repository at this point in the history
  • Loading branch information
raphamorim committed Feb 12, 2025
1 parent 420a20d commit 8cca458
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ crate-type = ["cdylib", "rlib", "staticlib"]

[features]
default = ["libc", "glutin", "gl" ,"ratatui" ,"icy_sixel", "image", "ratatui-image"]
ffi = []
ffi = ["image"]

[profile.release]
opt-level = "s"
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ web-build:
cd web && npm run build

ffi-build:
cargo install cbindgen
# cargo install cbindgen
cargo build --release --no-default-features --features ffi
cbindgen . -o gameboy.h --lang c

ffi-size:
du -k ./target/release/libgameboy.a
du -h ./target/release/libgameboy.a

ffi:
cp target/release/libgameboy.a examples/ffi-go/
Expand Down
6 changes: 1 addition & 5 deletions gameboy.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ typedef enum KeypadKey {
Start,
} KeypadKey;

typedef struct String String;

typedef struct ImageBuffer {
int32_t len;
const uint8_t *data;
Expand All @@ -42,6 +40,4 @@ void keyup(enum KeypadKey key);

struct ImageBuffer image(void);

extern void log(struct String s);

extern void log_u32(uint32_t a);
struct ImageBuffer scaled_image_png(uint8_t scale);
49 changes: 48 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Mutex;
use std::sync::OnceLock;

#[cfg(all(target_arch = "wasm32", feature = "ffi"))]
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

pub mod cpu;
Expand Down Expand Up @@ -93,3 +93,50 @@ pub extern "C" fn image() -> ImageBuffer {
data: std::ptr::null_mut(),
}
}

#[no_mangle]
pub extern "C" fn scaled_image_png(scale: u8) -> ImageBuffer {
if let Some(gb) = GAMEBOY.get() {
if let Ok(mut locked_gb) = gb.lock() {
let width = 160;
let height = 144;

let image: &[u8] = locked_gb.as_mut().unwrap().image();
let len = image.len() as i32;
// std::mem::forget(image);

// Allocate a new buffer for the RGB image, 3 bytes per pixel
let mut output_data = vec![0u8; width as usize * height as usize * 3];

let mut i = 0;
// Iterate through 4-byte chunks of the image data (RGBA bytes)
for chunk in image.chunks(4) {
// ... and copy each of them to output, leaving out the A byte
output_data[i..i + 3].copy_from_slice(&chunk[0..3]);
i += 3;
}

let mut buffer =
image::ImageBuffer::from_raw(width, height, output_data).unwrap();
if scale > 1 {
buffer = image::imageops::resize(
&buffer,
width * (scale as u32),
height * (scale as u32),
image::imageops::FilterType::Nearest,
);
}
let result = image::DynamicImage::ImageRgb8(buffer);

return ImageBuffer {
len,
data: result.as_bytes().as_ptr(),
};
}
}

ImageBuffer {
len: 0,
data: std::ptr::null_mut(),
}
}
18 changes: 10 additions & 8 deletions src/screen/web.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(not(feature = "ffi"))]

extern crate console_error_panic_hook;

use crate::gameboy::Gameboy;
Expand All @@ -12,13 +14,13 @@ use web_sys::{CanvasRenderingContext2d, ImageData};

use std::panic;

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
fn log(s: String);
#[wasm_bindgen(js_namespace = console, js_name = log)]
fn log_u32(a: u32);
}
// #[wasm_bindgen]
// extern "C" {
// #[wasm_bindgen(js_namespace = console)]
// fn log(s: String);
// #[wasm_bindgen(js_namespace = console, js_name = log)]
// fn log_u32(a: u32);
// }

fn window() -> web_sys::Window {
web_sys::window().expect("no global `window` exists")
Expand Down Expand Up @@ -121,7 +123,7 @@ pub async fn render(rom: Vec<u8>) -> Result<(), JsValue> {
context.put_image_data(&d, 0.0, 0.0).ok();
}
Err(err) => {
log(format!("{:?}", err));
// log(format!("{:?}", err));
}
};

Expand Down

0 comments on commit 8cca458

Please sign in to comment.