Skip to content

Commit

Permalink
add dimensions, and sprite dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDavenport committed Apr 14, 2024
1 parent cf1fcab commit 645ce22
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 21 deletions.
4 changes: 4 additions & 0 deletions gamercade_console/src/api/data_api.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub trait DataApi {
fn height(&self) -> i32;
fn width(&self) -> i32;
fn dimensions(&self) -> (i32, i32);
fn fps(&self) -> i32;
fn frame_time(&self) -> f32;

Expand All @@ -10,6 +11,7 @@ pub trait DataApi {
fn sprite_height(&self, sheet_index: i32) -> i32;
fn sprite_width(&self, sheet_index: i32) -> i32;
fn sprite_count(&self, sheet_index: i32) -> i32;
fn sprite_dimensions(&self, sheet_index: i32) -> (i32, i32);

fn bgm_length_secs(&self, bgm_index: i32) -> f32;
fn bgm_length_frames(&self, bgm_index: i32) -> i32;
Expand All @@ -32,12 +34,14 @@ macro_rules! derive_bind_data_api {
derive_bind_data_api! {
bind_height,
bind_width,
bind_dimensions,
bind_fps,
bind_frame_time,
bind_sprite_sheet_count,
bind_palette_count,
bind_sprite_height,
bind_sprite_width,
bind_sprite_dimensions,
bind_sprite_count,
bind_bgm_length_secs,
bind_bgm_length_frames,
Expand Down
2 changes: 2 additions & 0 deletions gamercade_console/src/console/bindings/data_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ macro_rules! derive_data_api_binding {
derive_data_api_binding! {
height(),
width(),
dimensions(),
fps(),
frame_time(),
sprite_sheet_count(),
palette_count(),
sprite_height(sprite_sheet: i32),
sprite_width(sprite_sheet: i32),
sprite_dimensions(sprite_sheet: i32),
sprite_count(sprite_sheet: i32),
bgm_length_secs(bgm_index: i32),
bgm_length_frames(bgm_index: i32),
Expand Down
10 changes: 10 additions & 0 deletions gamercade_console/src/console/contexts/data_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ impl DataApi for DataContext {
self.rom.width()
}

fn dimensions(&self) -> (i32, i32) {
(self.rom.width(), self.rom.height())
}

fn fps(&self) -> i32 {
self.rom.frame_rate.frames_per_second() as i32
}
Expand Down Expand Up @@ -53,6 +57,12 @@ impl DataApi for DataContext {
.unwrap_or(-1)
}

fn sprite_dimensions(&self, sheet_index: i32) -> (i32, i32) {
self.get_sprite_sheet(sheet_index)
.map(|sheet| (sheet.width as i32, sheet.height as i32))
.unwrap_or((-1, -1))
}

fn sprite_count(&self, sheet_index: i32) -> i32 {
self.get_sprite_sheet(sheet_index)
.map(|sheet| sheet.count as i32)
Expand Down
20 changes: 20 additions & 0 deletions gamercade_rs/src/api/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ pub fn width() -> usize {
unsafe { raw::width() as usize }
}

/// Returns the width and height of the screen, in pixels
pub fn dimensions() -> (usize, usize) {
unsafe {
let (width, height) = raw::dimensions();
(width as usize, height as usize)
}
}

/// Returns the target frame rate, in frames per second.
pub fn fps() -> usize {
unsafe { raw::fps() as usize }
Expand Down Expand Up @@ -46,6 +54,18 @@ pub fn sprite_width(sprite_sheet: usize) -> Option<u32> {
i32_u32_to_option(val)
}

/// Returns the width and height of each image for the request sprite sheet.
/// If the index is invalid, will return None.
pub fn sprite_dimensions(sprite_sheet: usize) -> Option<(u32, u32)> {
let (width, height) = unsafe { raw::sprite_dimensions(sprite_sheet as i32) };

if width.is_negative() {
None
} else {
Some((width as u32, height as u32))
}
}

/// Returns the number of sprites for the requsted sprite sheet.
/// If the index is invalid, will return None.
pub fn sprite_count(sprite_sheet: usize) -> Option<u32> {
Expand Down
31 changes: 10 additions & 21 deletions gamercade_rs/src/raw.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![allow(missing_docs)]
#![allow(improper_ctypes)]

// Audio
extern "C" {
Expand All @@ -8,27 +9,25 @@ extern "C" {
pub fn stop_channel(channel: i32);
pub fn play_note(note_id: i32, instrument_index: i32, channel: i32);
pub fn play_frequency(frequency: f32, instrument_index: i32, channel: i32);
}

// Data
extern "C" {
// Data
pub fn height() -> i32;
pub fn width() -> i32;
pub fn dimensions() -> (i32, i32);
pub fn fps() -> i32;
pub fn frame_time() -> f32;
pub fn sprite_sheet_count() -> i32;
pub fn palette_count() -> i32;
pub fn sprite_height(sprite_sheet: i32) -> i32;
pub fn sprite_width(sprite_sheet: i32) -> i32;
pub fn sprite_dimensions(sprite_sheet: i32) -> (i32, i32);
pub fn sprite_count(sprite_sheet: i32) -> i32;
pub fn bgm_length_secs(bgm_index: i32) -> f32;
pub fn bgm_length_frames(bgm_index: i32) -> i32;
pub fn sfx_length_secs(sfx_index: i32) -> f32;
pub fn sfx_length_frames(sfx_index: i32) -> i32;
}

// Graphics Params
extern "C" {
// Graphics Params
pub fn palette_index(palette_index: i32) -> i32;
pub fn sprite_sheet_index(sprite_sheet_index: i32) -> i32;
pub fn sprite_index(sprite_index: i32) -> i32;
Expand All @@ -43,10 +42,8 @@ extern "C" {
flip_x: i32,
flip_y: i32,
) -> i32;
}

// Draw
extern "C" {
// Draw
pub fn clear_screen(graphics_parameters: i32);
pub fn set_pixel(graphics_parameters: i32, x: i32, y: i32);
pub fn circle(graphics_parameters: i32, x: i32, y: i32, radius: i32);
Expand All @@ -56,24 +53,18 @@ extern "C" {
pub fn line(graphics_parameters: i32, x0: i32, y0: i32, x1: i32, y1: i32);
pub fn sprite(graphics_parameters: i32, transparency_mask: i64, x: i32, y: i32);
pub fn write_pixel_buffer(start_index: i32, parameters_ptr: i32, len: i32);
}

// Text
extern "C" {
// Text
pub fn console_log(text_ptr: i32, len: i32);
pub fn console_log_utf16(text_ptr: i32, len: i32);
}

// Random
extern "C" {
// Random
pub fn set_seed(seed: i32);
pub fn random_int_range(min: i32, max: i32) -> i32;
pub fn random_float() -> f32;
pub fn random_float_range(min: f32, max: f32) -> f32;
}

// Input
extern "C" {
// Input
pub fn button_a_pressed(player_id: i32) -> i32;
pub fn button_a_released(player_id: i32) -> i32;
pub fn button_a_held(player_id: i32) -> i32;
Expand Down Expand Up @@ -152,10 +143,8 @@ extern "C" {

pub fn raw_input_state(player_id: i32) -> i64;
pub fn raw_mouse_state(player_id: i32) -> i64;
}

// Multiplayer
extern "C" {
// Multiplayer
pub fn num_players() -> i32;
pub fn is_local_player(player_id: i32) -> i32;
pub fn is_remote_player(player_id: i32) -> i32;
Expand Down

0 comments on commit 645ce22

Please sign in to comment.