diff options
author | Matthew Michelotti <michelotti.matthew@gmail.com> | 2018-05-24 17:53:37 -0500 |
---|---|---|
committer | Matthew Michelotti <michelotti.matthew@gmail.com> | 2018-05-24 17:53:37 -0500 |
commit | 663198b64432ab72d46a6cc4188693865695093c (patch) | |
tree | 3ebdec1b710887032ea22eeb3b0375d30f569af3 | |
parent | 257c8412d478cbbfae4668acb71f60848bdc4ba1 (diff) |
added tile_width option
-rw-r--r-- | example/src/main.rs | 1 | ||||
-rw-r--r-- | gate/src/app_info.rs | 9 | ||||
-rw-r--r-- | gate/src/renderer/render_buffer.rs | 18 |
3 files changed, 22 insertions, 6 deletions
diff --git a/example/src/main.rs b/example/src/main.rs index c06f1ce..78b4a29 100644 --- a/example/src/main.rs +++ b/example/src/main.rs @@ -119,6 +119,7 @@ impl App<AssetId> for TowerGame { fn main() { let info = AppInfo::with_max_dims(86., 48.) .min_dims(64., 44.) + .tile_width(16) .title("Tower"); gate::run(info, TowerGame { pillars: vec![vec![4, 3, 2, 1, 0], vec![], vec![]], held: None }); } diff --git a/gate/src/app_info.rs b/gate/src/app_info.rs index e1c48bc..e9ecaa6 100644 --- a/gate/src/app_info.rs +++ b/gate/src/app_info.rs @@ -27,6 +27,7 @@ /// /// let info = AppInfo::with_max_dims(160., 90.) /// .min_dims(120., 86.) +/// .tile_width(16) /// .title("My Game") /// .target_fps(30.) /// .print_workload_info() @@ -36,6 +37,7 @@ pub struct AppInfo { pub(crate) window_pixels: (u32, u32), pub(crate) min_dims: (f64, f64), pub(crate) max_dims: (f64, f64), + pub(crate) tile_width: Option<u32>, pub(crate) title: &'static str, pub(crate) target_fps: f64, pub(crate) print_workload_info: bool, @@ -52,6 +54,7 @@ impl AppInfo { window_pixels: (800, 600), min_dims: (0., 0.), max_dims: (max_width, max_height), + tile_width: None, title: "untitled app", target_fps: 60., print_workload_info: false, @@ -65,6 +68,12 @@ impl AppInfo { self } + pub fn tile_width(mut self, tile_width: u32) -> Self { + assert!(tile_width > 0 && tile_width <= 10000, "unrealistic tile_width {}", tile_width); + self.tile_width = Some(tile_width); + self + } + /// Specifies a window title (default is "untitled app"). pub fn title(mut self, title: &'static str) -> Self { self.title = title; self } diff --git a/gate/src/renderer/render_buffer.rs b/gate/src/renderer/render_buffer.rs index 916863a..61b3daa 100644 --- a/gate/src/renderer/render_buffer.rs +++ b/gate/src/renderer/render_buffer.rs @@ -25,6 +25,7 @@ pub(super) enum Mode { Sprite } pub(super) struct RenderDims { pub min_dims: (f64, f64), pub max_dims: (f64, f64), + pub tile_width: Option<u32>, pub native_dims: (u32, u32), pub pixel_scalar: f64, pub native_pre_pad: (u32, u32), @@ -33,9 +34,14 @@ pub(super) struct RenderDims { } impl RenderDims { - fn new(min_dims: (f64, f64), max_dims: (f64, f64), native_dims: (u32, u32)) -> RenderDims { - let scalar_from_min = (native_dims.0 as f64 / min_dims.0).min(native_dims.1 as f64 / min_dims.1); - let scalar_from_max = (native_dims.0 as f64 / max_dims.0).max(native_dims.1 as f64 / max_dims.1); + fn new(min_dims: (f64, f64), max_dims: (f64, f64), tile_width: Option<u32>, native_dims: (u32, u32)) -> RenderDims { + let mut scalar_from_min = (native_dims.0 as f64 / min_dims.0).min(native_dims.1 as f64 / min_dims.1); + let mut scalar_from_max = (native_dims.0 as f64 / max_dims.0).max(native_dims.1 as f64 / max_dims.1); + if let Some(tile_width) = tile_width { + let tile_width = tile_width as f64; + scalar_from_min = (scalar_from_min * tile_width).floor() / tile_width; + scalar_from_max = (scalar_from_max * tile_width).ceil() / tile_width; + } let pixel_scalar = scalar_from_min.min(scalar_from_max).max(1.0); let used_native_dims = ( native_dims.0.min((max_dims.0 * pixel_scalar).floor() as u32), @@ -44,11 +50,11 @@ impl RenderDims { let native_pad = (native_dims.0 - used_native_dims.0, native_dims.1 - used_native_dims.1); let native_pre_pad = (native_pad.0 / 2, native_pad.1 / 2); let dims = (used_native_dims.0 as f64 / pixel_scalar, used_native_dims.1 as f64 / pixel_scalar); - RenderDims { min_dims, max_dims, native_dims, pixel_scalar, native_pre_pad, used_native_dims, dims } + RenderDims { min_dims, max_dims, tile_width, native_dims, pixel_scalar, native_pre_pad, used_native_dims, dims } } pub fn set_native_dims(&mut self, native_dims: (u32, u32)) { - *self = RenderDims::new(self.min_dims, self.max_dims, native_dims); + *self = RenderDims::new(self.min_dims, self.max_dims, self.tile_width, native_dims); } pub fn to_app_pos(&self, raw_x: i32, raw_y: i32) -> (f64, f64) { @@ -73,7 +79,7 @@ impl RenderBuffer { sprite_atlas, mode: Mode::Sprite, vbo_data: Vec::new(), - dims: RenderDims::new(info.min_dims, info.max_dims, native_dims), + dims: RenderDims::new(info.min_dims, info.max_dims, info.tile_width, native_dims), } } |