Skip to content

Commit

Permalink
Add ScreenScaler::from_canvas (fixes #269)
Browse files Browse the repository at this point in the history
  • Loading branch information
17cupsofcoffee committed Mar 17, 2023
1 parent 96ce305 commit d9cafa0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This project adheres to Semantic Versioning.
behaviour. ([@fililip](https://github.com/fililip) in [#321](https://github.com/17cupsofcoffee/tetra/pull/321))
* `ScreenScaler` now has `scale_factor` and sizing utility methods. ([@timerertim](https://github.com/timerertim)
in [#327](https://github.com/17cupsofcoffee/tetra/pull/327))
* `ScreenScaler` now has a `from_canvas` constructor. This can be used for greater control over the canvas that is used for scaling.

### Changed

Expand Down
24 changes: 24 additions & 0 deletions src/graphics/scaling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,30 @@ impl ScreenScaler {
})
}

/// Returns a new `ScreenScaler`, using the given canvas for drawing.
/// The mode will determine how the image is scaled to fit the screen.
pub fn from_canvas(
canvas: Canvas,
outer_width: i32,
outer_height: i32,
mode: ScalingMode,
) -> Result<ScreenScaler> {
let (inner_width, inner_height) = canvas.size();

let screen_rect =
get_screen_rect(mode, inner_width, inner_height, outer_width, outer_height);

Ok(ScreenScaler {
canvas,
mode,
screen_rect,
inner_width,
inner_height,
outer_width,
outer_height,
})
}

/// Returns a new `ScreenScaler`, with the specified inner width and height, and the outer
/// size set to the current dimensions of the window.
pub fn with_window_size(
Expand Down

0 comments on commit d9cafa0

Please sign in to comment.