Skip to content

Commit

Permalink
gui: make screen integer scaling optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodrigodd committed Sep 13, 2023
1 parent 7df36c2 commit 8755617
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
4 changes: 4 additions & 0 deletions gameroy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ sort_list = "+File"
# the initial size of the window in format WIDTHxHEIGHT
screen_size = "768x400"

# if true, the game screen will be scaled only by integer values, to keep
# perfect pixel sizes.
only_integer_scaling = true

# if rewiding is enabled. This causes a small performance hit and higher memory
# usge due to the constant doing of save states.
rewinding = true
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ pub struct Config {
pub jit: bool,
#[serde(deserialize_with = "screen_size_deser")]
pub screen_size: Option<(u32, u32)>,
pub only_integer_scaling: bool,
pub keymap: KeyMap,
}

Expand Down Expand Up @@ -272,6 +273,7 @@ const DEFAULT_CONFIG: Config = Config {
frame_skip: false,
jit: true,
screen_size: None,
only_integer_scaling: false,
keymap: DEFAULT_KEYMAP,
};

Expand Down
16 changes: 14 additions & 2 deletions src/widget/pixel_perfect_layout.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use giui::{Id, Layout, LayoutContext, MinSizeContext};

use crate::config::config;

pub struct ScreenLayout {
size: (u32, u32),
pub foward_button: Option<Id>,
Expand Down Expand Up @@ -43,10 +45,20 @@ impl Layout for ScreenLayout {
let ratio = self.size.0 as f32 / self.size.1 as f32;

if width / height < ratio {
des_width = self.size.0 as f32 * (width / self.size.0 as f32).floor();
let mut fraction = width / self.size.0 as f32;
if config().only_integer_scaling {
fraction = fraction.floor();
}

des_width = self.size.0 as f32 * fraction;
des_height = (des_width / ratio).round();
} else {
des_height = self.size.1 as f32 * (height / self.size.1 as f32).floor();
let mut fraction = height / self.size.1 as f32;
if config().only_integer_scaling {
fraction = fraction.floor();
}

des_height = self.size.1 as f32 * fraction;
des_width = des_height * ratio;
}

Expand Down

0 comments on commit 8755617

Please sign in to comment.