From fcbc5786908a32f68597961203d8c30e8a70c9ba Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tomas=20Wenstr=C3=B6m?= Date: Fri, 15 Feb 2019 18:19:04 +0100 Subject: [PATCH] Use unsafe textures and move some things into an app object --- Cargo.toml | 2 +- src/main.rs | 102 ++++++++++++++++++++++++++++++++------------------------- src/sprites.rs | 10 +++--- 3 files changed, 63 insertions(+), 51 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ce2f5ee..4486569 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,4 +9,4 @@ time = "0.1.42" [dependencies.sdl2] version = "0.32.1" -features = ["gfx", "image"] +features = ["gfx", "image", "unsafe_textures"] diff --git a/src/main.rs b/src/main.rs index d474fd6..be22448 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ 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; @@ -20,9 +21,6 @@ use time::PreciseTime; use boll::{Boll, CircleBoll, SquareBoll}; use common::Point2D; use sprites::SpriteManager; -use sdl2::video::WindowContext; -use sdl2::render::TextureCreator; -use sdl2::event::WindowEvent; #[macro_use] mod common; mod boll; @@ -35,35 +33,50 @@ const NS_PER_FRAME: u32 = 1_000_000_000 / FPS; type Bollar = Vec>; -fn init() -> (Canvas, EventPump) { - let context = sdl2::init().unwrap(); - let video = context.video().unwrap(); - let _image_context = sdl2::image::init(sdl2::image::InitFlag::PNG).unwrap(); - let window = video.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(); - (canvas, event_pump) +struct App { + canvas: Canvas, + event_pump: EventPump, + sprites: SpriteManager, } -fn load_sprites(texture_creator: &TextureCreator) -> SpriteManager { - let mut texman = SpriteManager::new(texture_creator); - texman.load("block", "res/block.bmp"); - texman.load("mario", "res/mario-trans.png"); - texman +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 canvas, mut event_pump) = init(); + let mut app = App::new(); + app.load_sprites(&[ + ("block", "res/block.bmp"), + ("mario", "res/mario-trans.png"), + ]); let mut frame_count: u64 = 0; let mut fps_time = PreciseTime::now(); @@ -71,23 +84,22 @@ fn main() { let mut bolls: Bollar = Bollar::new(); let mut boll_size = 1; - let texture_creator = canvas.texture_creator(); - let sprites = load_sprites(&texture_creator); let mut mario_angle = 0.0; 'running: loop { let loop_start = PreciseTime::now(); - canvas.set_draw_color(Color::RGB(0, 0, 0)); - canvas.clear(); + app.canvas.set_draw_color(Color::RGB(0, 0, 0)); + app.canvas.clear(); { let blocks = 20; let size = 32; let offset = point!((SCREEN_WIDTH as i32 - (blocks + 1) * size) / 2, (SCREEN_HEIGHT as i32 - (blocks + 1) * size) / 2); + let block = app.sprites.get("block"); for i in 0..blocks { - canvas.copy(sprites.get("block"), None, Rect::new((i) * size + offset.x, (0) * size + offset.y, size as u32, size as u32)).unwrap(); - canvas.copy(sprites.get("block"), None, Rect::new((blocks - i) * size + offset.x, (blocks) * size + offset.y, size as u32, size as u32)).unwrap(); - canvas.copy(sprites.get("block"), None, Rect::new((0) * size + offset.x, (blocks - i) * size + offset.y, size as u32, size as u32)).unwrap(); - canvas.copy(sprites.get("block"), None, Rect::new((blocks) * size + offset.x, (i) * size + offset.y, size as u32, size as u32)).unwrap(); + app.canvas.copy(block, None, Rect::new((i) * size + offset.x, (0) * size + offset.y, size as u32, size as u32)).unwrap(); + app.canvas.copy(block, None, Rect::new((blocks - i) * size + offset.x, (blocks) * size + offset.y, size as u32, size as u32)).unwrap(); + app.canvas.copy(block, None, Rect::new((0) * size + offset.x, (blocks - i) * size + offset.y, size as u32, size as u32)).unwrap(); + app.canvas.copy(block, None, Rect::new((blocks) * size + offset.x, (i) * size + offset.y, size as u32, size as u32)).unwrap(); } } { @@ -96,33 +108,33 @@ fn main() { let radius = 110.0 + size as f32 * 0.5; let angle = (mario_angle as f32 - 90.0) * PI / 180.0; let offset2 = point!((angle.cos() * radius) as i32, (angle.sin() * radius) as i32); - canvas.copy_ex(sprites.get("mario"), None, Rect::new(offset.x + offset2.x, offset.y + offset2.y, size as u32, size as u32), mario_angle, sdl2::rect::Point::new(size / 2, size / 2), false, false).unwrap(); + app.canvas.copy_ex(app.sprites.get("mario"), None, Rect::new(offset.x + offset2.x, offset.y + offset2.y, size as u32, size as u32), mario_angle, sdl2::rect::Point::new(size / 2, size / 2), false, false).unwrap(); mario_angle += 1.0; if mario_angle >= 360.0 { mario_angle -= 360.0 } } { let p = point!((SCREEN_WIDTH / 2) as i16, (SCREEN_HEIGHT / 2) as i16); - canvas.circle(p.x, p.y, 100, Color::RGB(255, 255, 255)).unwrap(); - canvas.aa_circle(p.x, p.y, 110, Color::RGB(255, 255, 255)).unwrap(); - canvas.ellipse(p.x, p.y, 50, 100, Color::RGB(255, 255, 255)).unwrap(); - canvas.aa_ellipse(p.x, p.y, 110, 55, Color::RGB(255, 255, 255)).unwrap(); + app.canvas.circle(p.x, p.y, 100, Color::RGB(255, 255, 255)).unwrap(); + app.canvas.aa_circle(p.x, p.y, 110, Color::RGB(255, 255, 255)).unwrap(); + app.canvas.ellipse(p.x, p.y, 50, 100, Color::RGB(255, 255, 255)).unwrap(); + app.canvas.aa_ellipse(p.x, p.y, 110, 55, Color::RGB(255, 255, 255)).unwrap(); } for b in &mut bolls { b.update(); - b.draw(&mut canvas, boll_size); + b.draw(&mut app.canvas, boll_size); } // window.gl_swap_window(); - for event in event_pump.poll_iter() { + for event in app.event_pump.poll_iter() { match event { Event::Quit { .. } | Event::KeyDown { keycode: Some(Keycode::Escape), .. } => { break 'running; } Event::KeyDown { keycode: Some(Keycode::F11), .. } => { - match canvas.window().fullscreen_state() { - FullscreenType::Off => canvas.window_mut().set_fullscreen(FullscreenType::Desktop), - _ => canvas.window_mut().set_fullscreen(FullscreenType::Off) + match app.canvas.window().fullscreen_state() { + FullscreenType::Off => app.canvas.window_mut().set_fullscreen(FullscreenType::Desktop), + _ => app.canvas.window_mut().set_fullscreen(FullscreenType::Off) }.unwrap(); } Event::KeyDown { keycode: Some(Keycode::KpPlus), .. } => { boll_size = std::cmp::min(boll_size + 1, 32) } @@ -143,7 +155,7 @@ fn main() { _ => {} } } - canvas.present(); + app.canvas.present(); match loop_start.to(PreciseTime::now()).num_nanoseconds() { Some(ns) if ns < (NS_PER_FRAME - 50_0000) as i64 => { change_boll_count(&mut bolls, 100) } diff --git a/src/sprites.rs b/src/sprites.rs index 82dd6dc..57fc0a4 100644 --- a/src/sprites.rs +++ b/src/sprites.rs @@ -5,13 +5,13 @@ use sdl2::render::Texture; use sdl2::render::TextureCreator; use sdl2::video::WindowContext; -pub struct SpriteManager<'canvas> { - texture_creator: &'canvas TextureCreator, // can't make the lifetimes work when this is owned instead of borrowed - textures: HashMap<&'static str, Texture<'canvas>>, +pub struct SpriteManager { + texture_creator: TextureCreator, // can't make the lifetimes work when this is owned instead of borrowed + textures: HashMap<&'static str, Texture>, } -impl<'canvas> SpriteManager<'canvas> { - pub fn new(texture_creator: &'canvas TextureCreator) -> SpriteManager<'canvas> { +impl SpriteManager { + pub fn new(texture_creator: TextureCreator) -> SpriteManager { SpriteManager { texture_creator, textures: HashMap::new(), -- 2.11.0