Move app to module
authorTomas Wenström <tomas.wenstrom@gmail.com>
Fri, 15 Feb 2019 19:33:41 +0000 (20:33 +0100)
committerTomas Wenström <tomas.wenstrom@gmail.com>
Fri, 15 Feb 2019 20:50:32 +0000 (21:50 +0100)
src/app.rs [new file with mode: 0644]
src/main.rs

diff --git a/src/app.rs b/src/app.rs
new file mode 100644 (file)
index 0000000..746f98d
--- /dev/null
@@ -0,0 +1,45 @@
+use sdl2::EventPump;
+use sdl2::pixels::Color;
+use sdl2::render::BlendMode;
+use sdl2::render::Canvas;
+use sdl2::video::Window;
+
+use {SCREEN_HEIGHT, SCREEN_WIDTH};
+use sprites::SpriteManager;
+
+pub struct App {
+    pub canvas: Canvas<Window>,
+    pub event_pump: EventPump,
+    pub sprites: SpriteManager,
+}
+
+impl App {
+    pub fn new() -> 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)
+            .position_centered()
+            .opengl()
+            .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,
+        }
+    }
+
+    pub fn load_sprites(&mut self, sprites: &[(&'static str, &str)]) {
+        for (name, file) in sprites {
+            self.sprites.load(name, file);
+        }
+    }
+}
index be22448..8b10341 100644 (file)
@@ -7,21 +7,18 @@ use std::f32::consts::PI;
 use rand::Rng;
 use sdl2::event::Event;
 use sdl2::event::WindowEvent;
-use sdl2::EventPump;
 use sdl2::gfx::primitives::DrawRenderer;
 use sdl2::keyboard::Keycode;
 use sdl2::pixels::Color;
 use sdl2::rect::Rect;
-use sdl2::render::BlendMode;
-use sdl2::render::Canvas;
 use sdl2::video::FullscreenType;
-use sdl2::video::Window;
 use time::PreciseTime;
 
+use app::*;
 use boll::{Boll, CircleBoll, SquareBoll};
 use common::Point2D;
-use sprites::SpriteManager;
 
+mod app;
 #[macro_use] mod common;
 mod boll;
 mod sprites;
@@ -33,43 +30,6 @@ const NS_PER_FRAME: u32 = 1_000_000_000 / FPS;
 
 type Bollar = Vec<Box<dyn Boll>>;
 
-struct App {
-    canvas: Canvas<Window>,
-    event_pump: EventPump,
-    sprites: SpriteManager,
-}
-
-impl App {
-    fn new() -> 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)
-            .position_centered()
-            .opengl()
-            .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,
-        }
-    }
-
-    fn load_sprites(&mut self, sprites: &[(&'static str, &str)]) {
-        for (name, file) in sprites {
-            self.sprites.load(name, file);
-        }
-    }
-}
-
 fn main() {
     println!("starting...");
     let mut app = App::new();