Skip to content

Commit d14b7ab

Browse files
committed
boing-ggez: Make viewport computations naming more precise (and intuitive)
1 parent 9bc0c20 commit d14b7ab

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

boing-ggez/src/main.rs

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -52,42 +52,43 @@ fn compute_viewport(context: &Context) -> (Rect, Rect) {
5252
// Assume that the pixels are square.
5353
//
5454
let PhysicalSize {
55-
width: screen_width,
56-
height: screen_height,
55+
width: screen_physical_width,
56+
height: screen_physical_height,
5757
} = context.gfx.window().inner_size();
5858

59-
let game_ratio = WINDOW_WIDTH / WINDOW_HEIGHT;
60-
let screen_ratio = screen_width as f32 / screen_height as f32;
61-
62-
let (viewport_width, viewport_height, scaling_ratio) = if screen_ratio >= game_ratio {
63-
(
64-
WINDOW_HEIGHT * screen_ratio,
65-
WINDOW_HEIGHT,
66-
screen_height as f32 / WINDOW_HEIGHT,
67-
)
68-
} else {
69-
(
70-
WINDOW_WIDTH,
71-
WINDOW_WIDTH / screen_ratio,
72-
screen_width as f32 / WINDOW_WIDTH,
73-
)
74-
};
75-
76-
let tot_border_width = viewport_width - WINDOW_WIDTH;
77-
let tot_border_height = viewport_height - WINDOW_HEIGHT;
59+
let window_ratio = WINDOW_WIDTH / WINDOW_HEIGHT;
60+
let screen_physical_ratio = screen_physical_width as f32 / screen_physical_height as f32;
61+
62+
let (screen_logical_width, screen_logical_height, logical_scale) =
63+
if screen_physical_ratio >= window_ratio {
64+
(
65+
WINDOW_HEIGHT * screen_physical_ratio,
66+
WINDOW_HEIGHT,
67+
screen_physical_height as f32 / WINDOW_HEIGHT,
68+
)
69+
} else {
70+
(
71+
WINDOW_WIDTH,
72+
WINDOW_WIDTH / screen_physical_ratio,
73+
screen_physical_width as f32 / WINDOW_WIDTH,
74+
)
75+
};
76+
77+
let horizontal_bar_width = (screen_logical_width - WINDOW_WIDTH) / 2.;
78+
let vertical_bar_height = (screen_logical_height - WINDOW_HEIGHT) / 2.;
7879

7980
let viewport_rect = Rect::new(
80-
-tot_border_width / 2.,
81-
-tot_border_height / 2.,
82-
viewport_width,
83-
viewport_height,
81+
-horizontal_bar_width,
82+
-vertical_bar_height,
83+
screen_logical_width,
84+
screen_logical_height,
8485
);
8586

8687
let scissors_rect = Rect::new(
87-
(screen_width as f32 - WINDOW_WIDTH * scaling_ratio) / 2.,
88-
(screen_height as f32 - WINDOW_HEIGHT * scaling_ratio) / 2.,
89-
viewport_rect.w * scaling_ratio,
90-
viewport_rect.h * scaling_ratio,
88+
(screen_physical_width as f32 - WINDOW_WIDTH * logical_scale) / 2.,
89+
(screen_physical_height as f32 - WINDOW_HEIGHT * logical_scale) / 2.,
90+
viewport_rect.w * logical_scale,
91+
viewport_rect.h * logical_scale,
9192
);
9293

9394
(viewport_rect, scissors_rect)

0 commit comments

Comments
 (0)