diff options
author | Matthew Michelotti <michelotti.matthew@gmail.com> | 2018-05-05 23:15:27 -0500 |
---|---|---|
committer | Matthew Michelotti <michelotti.matthew@gmail.com> | 2018-05-05 23:18:33 -0500 |
commit | 774ccc13e42553dc50b3f29f6e6df6efa901a577 (patch) | |
tree | 190a062376f11cbaaac3984ee66b3ad272c6d4ab | |
parent | 96a28c69a7ecc0c165b10969429d4acc452942f6 (diff) |
removed builder from AppInfo
-rw-r--r-- | example/src/main.rs | 2 | ||||
-rw-r--r-- | gate/src/app_info.rs | 63 | ||||
-rw-r--r-- | gate/src/lib.rs | 6 |
3 files changed, 31 insertions, 40 deletions
diff --git a/example/src/main.rs b/example/src/main.rs index db7b45a..042da6a 100644 --- a/example/src/main.rs +++ b/example/src/main.rs @@ -121,6 +121,6 @@ impl App<AssetId> for TowerGame { } fn main() { - let info = AppInfo::with_app_height(48.).title("Tower").build(); + let info = AppInfo::with_app_height(48.).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 e23d625..8f3dac6 100644 --- a/gate/src/app_info.rs +++ b/gate/src/app_info.rs @@ -17,19 +17,20 @@ /// A struct for specifying initialization information for running an `App`. /// +/// Methods for setting fields in `AppInfo` are intended to be chained together like +/// the builder pattern. +/// /// # Example /// /// ```rust -/// use gate::app_info::AppInfo; +/// use gate::AppInfo; /// /// let info = AppInfo::with_app_height(100.) /// .title("My Game") /// .target_fps(30.) /// .print_workload_info() -/// .print_gl_info() -/// .build(); +/// .print_gl_info(); /// ``` -#[derive(Clone)] pub struct AppInfo { pub(crate) app_height: f64, pub(crate) window_pixels: (u32, u32), @@ -42,71 +43,59 @@ pub struct AppInfo { } impl AppInfo { - /// Returns a builder, initialized with the required value `app_height`. + /// Returns a new AppInfo, initialized with the required value `app_height`. /// /// The `app_height` is the height of the screen in conceptual "app pixels", /// which defines the units used by the renderers. /// Even if the window is resized and the aspect ratio changed, /// the app height will always remain the same. /// The choice of this is important for the `TiledRenderer` in particular. - pub fn with_app_height(app_height: f64) -> Builder { + pub fn with_app_height(app_height: f64) -> AppInfo { assert!(app_height >= 1e-30 && app_height <= 3000., "unrealistic app height {}", app_height); - Builder { - info: AppInfo { - app_height, - window_pixels: (800, 600), - min_aspect_ratio: 4. / 3., - max_aspect_ratio: 16. / 9., - title: "untitled app", - target_fps: 60., - print_workload_info: false, - print_gl_info: false, - } + AppInfo { + app_height, + window_pixels: (800, 600), + min_aspect_ratio: 4. / 3., + max_aspect_ratio: 16. / 9., + title: "untitled app", + target_fps: 60., + print_workload_info: false, + print_gl_info: false, } } -} -/// Builder for `AppInfo`, created by `AppInfo::builder()`. -pub struct Builder { - info: AppInfo -} - -impl Builder { /// Specifies the minimum and maximum aspect ratio for the game, enforced by /// letterboxing/pillarboxing if necessary (default is `4/3` to `16/9`). - pub fn aspect_ratio_range(&mut self, min_ratio: f64, max_ratio: f64) -> &mut Self { + pub fn aspect_ratio_range(mut self, min_ratio: f64, max_ratio: f64) -> Self { assert!(0.2 < min_ratio && min_ratio < max_ratio && max_ratio < 5.0, "invalid aspect ratios"); // TODO ensure there is a large enough gap between min_ratio and max_ratio, so that pixel rounding isn't an issue - self.info.min_aspect_ratio = min_ratio; - self.info.max_aspect_ratio = max_ratio; + self.min_aspect_ratio = min_ratio; + self.max_aspect_ratio = max_ratio; self } /// Specifies a window title (default is "untitled app"). - pub fn title(&mut self, title: &'static str) -> &mut Self { self.info.title = title; self } + pub fn title(mut self, title: &'static str) -> Self { self.title = title; self } /// Specifies the intial width and height of the window (default is width `800` height `600`). - pub fn window_pixels(&mut self, width: u32, height: u32) -> &mut Self { + pub fn window_pixels(mut self, width: u32, height: u32) -> Self { assert!(width >= 10 && width <= 3000, "unrealistic window width {}", width); assert!(height >= 10 && height <= 3000, "unrealistic window height {}", height); - self.info.window_pixels = (width, height); + self.window_pixels = (width, height); self } /// Specifies the target frames-per-second (default is `60.`). - pub fn target_fps(&mut self, target_fps: f64) -> &mut Self { + pub fn target_fps(mut self, target_fps: f64) -> Self { assert!(target_fps > 10. && target_fps < 200., "unrealistic target_fps: {}", target_fps); - self.info.target_fps = target_fps; + self.target_fps = target_fps; self } /// If invoked, workload info will be printed to standard output periodically. - pub fn print_workload_info(&mut self) -> &mut Self { self.info.print_workload_info = true; self } + pub fn print_workload_info(mut self) -> Self { self.print_workload_info = true; self } /// If invoked, the OpenGL version info will be printed out at the start of the application. - pub fn print_gl_info(&mut self) -> &mut Self { self.info.print_gl_info = true; self } - - /// Returns an `AppInfo` made from this builder. - pub fn build(&mut self) -> AppInfo { self.info.clone() } + pub fn print_gl_info(mut self) -> Self { self.print_gl_info = true; self } } diff --git a/gate/src/lib.rs b/gate/src/lib.rs index b46009a..72b4e40 100644 --- a/gate/src/lib.rs +++ b/gate/src/lib.rs @@ -58,7 +58,7 @@ extern crate byteorder; pub mod asset_id; pub mod renderer; -pub mod app_info; +mod app_info; mod app_context; mod input; mod core; @@ -77,7 +77,9 @@ use renderer::Renderer; /// /// Will panic if this method is called more than once. /// The `AppInfo` is used to specify intiailization parameters for the application. -pub fn run<AS: 'static + AppAssetId, AP: 'static + App<AS>>(info: AppInfo, app: AP) { core::run(info, app); } +pub fn run<AS: 'static + AppAssetId, AP: 'static + App<AS>>(info: AppInfo, app: AP) { + core::run(info, app); +} /// Trait that a user can implement to specify application behavior, passed into `gate::run(...)`. pub trait App<A: AppAssetId> { |