Built an app builder and other minor things
[kaka/rust-sdl-test.git] / src / game / app.rs
index 50acaa1..b37e8fd 100644 (file)
@@ -9,44 +9,83 @@ use sdl2::video::Window;
 
 use ::{SCREEN_HEIGHT, SCREEN_WIDTH};
 use boll::*;
-use common::Point2D;
+use common::{Point2D, Rect};
 use sprites::SpriteManager;
 use NS_PER_FRAME;
 use point; // defined in common, but loaded from main...
 
 pub type Nanoseconds = u64;
 
-pub struct App {
-    pub canvas: Canvas<Window>,
-    pub event_pump: EventPump,
-    pub sprites: SpriteManager,
-    pub state: Box<dyn AppState>,
+#[derive(Default)]
+pub struct AppBuilder {
+    resolution: Rect<u16>,
+    fps: u8,
+    state: Option<Box<dyn AppState>>,
+    title: Option<String>,
 }
 
-impl App {
-    pub fn new() -> App {
+impl AppBuilder {
+    pub fn with_resolution(mut self, width: u16, height: u16) -> Self {
+       self.resolution = Rect { width, height };
+       self
+    }
+
+    pub fn with_fps(mut self, fps: u8) -> Self {
+       unimplemented!()
+       // self.fps = fps;
+       // self
+    }
+
+    pub fn with_state(mut self, state: Box<dyn AppState>) -> Self {
+       self.state = Some(state);
+       self
+    }
+
+    pub fn with_title(mut self, title: &str) -> Self {
+       self.title = Some(title.to_string());
+       self
+    }
+
+    pub fn start(self) -> App {
         let context = sdl2::init().unwrap();
         sdl2::image::init(sdl2::image::InitFlag::PNG).unwrap();
-        let window = context.video().unwrap().window("SDL test", SCREEN_WIDTH, SCREEN_HEIGHT)
+        let window = context
+           .video().unwrap()
+           .window(&self.title.unwrap(), self.resolution.width.into(), self.resolution.height.into())
             .position_centered()
             .opengl()
-            .build()
-            .unwrap();
+            .build().unwrap();
         context.mouse().show_cursor(false);
+
         let mut canvas = window.into_canvas().build().unwrap();
         canvas.set_blend_mode(BlendMode::Add);
         canvas.set_draw_color(Color::RGB(0, 0, 0));
         canvas.clear();
         canvas.present();
+
         let event_pump = context.event_pump().unwrap();
         let sprites = SpriteManager::new(canvas.texture_creator());
+
         App {
             canvas,
             event_pump,
             sprites,
-            state: Box::new(ActiveState::new()),
+            state: self.state.unwrap_or(Box::new(ActiveState::new())),
         }
     }
+}
+
+pub struct App {
+    pub canvas: Canvas<Window>,
+    pub event_pump: EventPump,
+    pub sprites: SpriteManager,
+    pub state: Box<dyn AppState>,
+}
+
+impl App {
+    pub fn new() -> AppBuilder {
+       Default::default()
+    }
 
     pub fn load_sprites(&mut self, sprites: &[(&str, &str)]) {
         for (name, file) in sprites {
@@ -70,7 +109,7 @@ pub struct ActiveState {
 }
 
 impl ActiveState {
-    fn new() -> ActiveState {
+    pub fn new() -> ActiveState {
         ActiveState {
             bolls: Bollar::new(),
             boll_size: 1,