Skip to content

Commit

Permalink
improve dpi scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
griffi-gh committed Sep 29, 2023
1 parent 8f3bfc9 commit 8291aa1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 36 deletions.
5 changes: 3 additions & 2 deletions yarge-frontend-sdl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ anyhow = "1.0"
dirs = "5.0"

[target.'cfg(windows)'.dependencies]
windows = { version = "0.51", features = ["Win32_UI_HiDpi"] }
windows = { version = "0.51", features = ["Win32_UI_HiDpi"], optional = true }

[features]
default = ["global_config"]
default = ["global_config", "hidpi"]
production = []
global_config = []
hidpi = ["dep:windows"]
2 changes: 1 addition & 1 deletion yarge-frontend-sdl/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl Default for Configuration {
speed: 1,
save_slot: 0,
dpi_scaling: true,
dpi_scaling_frac: false,
dpi_scaling_frac: true,
}
}
}
Expand Down
53 changes: 31 additions & 22 deletions yarge-frontend-sdl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct Args {

fn main() {
//Set dpi aware flag on windows
#[cfg(windows)] {
#[cfg(all(windows, feature = "hidpi"))] {
use windows::Win32::UI::HiDpi::{SetProcessDpiAwareness, PROCESS_PER_MONITOR_DPI_AWARE};
if unsafe { SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE) }.is_err() {
println!("[ERR] Failed to set DPI awareness");
Expand Down Expand Up @@ -186,30 +186,39 @@ fn main() {

println!("[INIT/INFO] Initialization done");

//Main loop
#[cfg(feature = "hidpi")]
let mut dpi_prev = 1.;

//Main loop
'run: loop {
//Update dpi scale
let mut display_dpi_scale = if config.dpi_scaling {
video_subsystem.display_dpi(canvas.window().display_index().unwrap()).unwrap().0 / 96.
} else {
1.
//Figure out dpi stuff
let display_dpi_scale = {
#[cfg(feature = "hidpi")] {
let mut display_dpi_scale = if config.dpi_scaling {
video_subsystem.display_dpi(canvas.window().display_index().unwrap()).unwrap().0 / 96.
} else {
1.
};
if !config.dpi_scaling_frac {
display_dpi_scale = display_dpi_scale.ceil();
}
if dpi_prev != display_dpi_scale {
println!("[INFO/DPI] dpi scale changed from {} to {}", dpi_prev, display_dpi_scale);
dpi_prev = display_dpi_scale;
let s = (
GB_WIDTH as u32 * config.scale.scale_or_default(),
GB_HEIGHT as u32 * config.scale.scale_or_default()
);
canvas.window_mut().set_size(
(display_dpi_scale * s.0 as f32) as u32,
(display_dpi_scale * s.1 as f32) as u32
).unwrap();
}
text_renderer.set_render_dpi_scale(display_dpi_scale);
display_dpi_scale
}
#[cfg(not(feature = "hidpi"))] { 1. }
};
if !config.dpi_scaling_frac {
display_dpi_scale = display_dpi_scale.ceil();
}
if dpi_prev != display_dpi_scale {
dpi_prev = display_dpi_scale;
let s = (
GB_WIDTH as u32 * config.scale.scale_or_default(),
GB_HEIGHT as u32 * config.scale.scale_or_default()
);
canvas.window_mut().set_size(
(display_dpi_scale * s.0 as f32) as u32,
(display_dpi_scale * s.1 as f32) as u32
).unwrap();
}
text_renderer.set_render_dpi_scale(display_dpi_scale);

//Process SDL2 events
for event in event_pump.poll_iter() {
Expand Down
22 changes: 11 additions & 11 deletions yarge-frontend-sdl/src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,14 +667,14 @@ impl Menu {
// Update scroll
//TODO rewrite!!
if let Some(cursor_y) = x_cursor_y {
let cursor_underscroll = cursor_y - (res.1 as i32 - (text.char_size(1.).1 as i32 + 2) - 2 * MENU_ITEM_HEIGHT as i32);
let cursor_underscroll = cursor_y - ((res.1 as f32 / dpi_scale) as i32 - (text.char_size(1.).1 as i32 + 2) - 2 * MENU_ITEM_HEIGHT as i32);
let cursor_overscroll = cursor_y - list_start_y_noscroll - MENU_ITEM_HEIGHT as i32;
self.scroll += cursor_underscroll.max(0) + cursor_overscroll.min(0);
}
self.scroll = self.scroll.max(0);

// Draw scroll bar
let viewport_height = res.1 as f32 - list_start_y_noscroll as f32 - (text.char_size(1.).1 as f32 + 2.);
let viewport_height = (res.1 as f32 / dpi_scale) - list_start_y_noscroll as f32 - (text.char_size(1.).1 as f32 + 2.);
let scrollbar_visible = (x_position.1 - list_start_y) as f32 > viewport_height;
if scrollbar_visible {
//Compute stuff
Expand All @@ -684,16 +684,16 @@ impl Menu {
let scrollbar_h = viewport_height_ratio * viewport_height;
let y_correction: f32 = - (scrollbar_h * progress);
let scrollbar_rect = Rect::from((
res.0 as i32 - SCROLLBAR_WIDTH as i32,
list_start_y_noscroll + ((progress * viewport_height) + y_correction) as i32,
SCROLLBAR_WIDTH,
scrollbar_h as u32
res.0 as i32 - m(SCROLLBAR_WIDTH as i32, dpi_scale),
m(list_start_y_noscroll + ((progress * viewport_height) + y_correction) as i32, dpi_scale),
mu(SCROLLBAR_WIDTH, dpi_scale),
mu(scrollbar_h as u32, dpi_scale)
));
let scrollbar_bg_rect = Rect::from((
res.0 as i32 - SCROLLBAR_WIDTH as i32,
list_start_y_noscroll,
SCROLLBAR_WIDTH,
viewport_height as u32
res.0 as i32 - m(SCROLLBAR_WIDTH as i32, dpi_scale),
m(list_start_y_noscroll, dpi_scale),
mu(SCROLLBAR_WIDTH, dpi_scale),
mu(viewport_height as u32, dpi_scale)
));
canvas.set_draw_color(Color::RGBA(0, 0, 0, 96));
canvas.fill_rects(&[scrollbar_rect, scrollbar_rect, scrollbar_bg_rect]).unwrap();
Expand All @@ -713,7 +713,7 @@ impl Menu {
let h = text.char_size(1.).1 + 2;
//animation
let opa = (self.activation_anim_state.value * 255.) as u32 as u8;
let offst = h as i32 - (self.activation_anim_state.value * h as f32) as i32;
let offst = -(h as i32 - (self.activation_anim_state.value * h as f32) as i32);
//box
canvas.set_draw_color(Color::RGBA(0, 0, 0, opa / 4));
canvas.fill_rects(&[
Expand Down

0 comments on commit 8291aa1

Please sign in to comment.