From: Tomas Wenström Date: Fri, 15 Feb 2019 19:33:41 +0000 (+0100) Subject: Move app to module X-Git-Url: http://git.dolda2000.com/gitweb/?a=commitdiff_plain;h=3bfea951f8059b01b1671be7dd78f2dc5a7ac4ee;p=kaka%2Frust-sdl-test.git Move app to module --- diff --git a/src/app.rs b/src/app.rs new file mode 100644 index 0000000..746f98d --- /dev/null +++ b/src/app.rs @@ -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, + 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); + } + } +} diff --git a/src/main.rs b/src/main.rs index be22448..8b10341 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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>; -struct App { - canvas: Canvas, - 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();