From 77034de98f1182979c74c6ccc81944ef74db35b4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tomas=20Wenstr=C3=B6m?= Date: Wed, 6 Jan 2021 13:17:55 +0100 Subject: [PATCH] Removed global use of SCREEN_WIDTH/HEIGHT --- src/game/app.rs | 24 ++++++++++++++---------- src/main.rs | 8 ++++---- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/game/app.rs b/src/game/app.rs index 0ec34ee..3e11b5d 100644 --- a/src/game/app.rs +++ b/src/game/app.rs @@ -16,7 +16,6 @@ use sdl2::{EventPump, VideoSubsystem}; use sprites::SpriteManager; use std::f32::consts::PI; use time::PreciseTime; -use {SCREEN_HEIGHT, SCREEN_WIDTH}; pub type Nanoseconds = u64; @@ -77,12 +76,13 @@ impl AppBuilder { let event_pump = context.event_pump()?; let sprites = SpriteManager::new(canvas.texture_creator()); + let screen = canvas.output_size().unwrap(); Ok(App { canvas, event_pump, sprites, - state: self.state.unwrap_or_else(|| Box::new(ActiveState::new())), + state: self.state.unwrap_or_else(|| Box::new(ActiveState::new(screen))), }) } @@ -146,6 +146,8 @@ impl App { let mut frame_count: u64 = 0; let mut fps_time = PreciseTime::now(); let mut last_time = PreciseTime::now(); + let screen = self.canvas.output_size().unwrap(); + let screen = Rect::from((screen.0 as i32, screen.1 as i32)); let mut mario_angle = 0.0; @@ -156,8 +158,8 @@ impl App { 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 + (screen.width - (blocks + 1) * size) / 2, + (screen.height - (blocks + 1) * size) / 2 ); let block = self.sprites.get("block"); for i in 0..blocks { @@ -209,8 +211,8 @@ impl App { { let size = 64; let offset = point!( - (SCREEN_WIDTH as i32 - size) / 2, - (SCREEN_HEIGHT as i32 - size) / 2 + (screen.width - size) / 2, + (screen.height - size) / 2 ); let radius = 110.0 + size as f32 * 0.5; let angle = (mario_angle as f32 - 90.0) * PI / 180.0; @@ -237,7 +239,7 @@ impl App { } } { - let p = point!((SCREEN_WIDTH / 2) as i16, (SCREEN_HEIGHT / 2) as i16); + let p = point!((screen.width / 2) as i16, (screen.height / 2) as i16); self.canvas .circle(p.x, p.y, 100, Color::RGB(255, 255, 255)) .unwrap(); @@ -352,15 +354,17 @@ pub trait AppState { type Bollar = Vec>; pub struct ActiveState { + screen: Rect, bolls: Bollar, boll_size: u32, } impl ActiveState { - pub fn new() -> ActiveState { + pub fn new(screen: (u32, u32)) -> ActiveState { ActiveState { bolls: Bollar::new(), boll_size: 1, + screen: Rect::from(screen), } } @@ -381,8 +385,8 @@ impl ActiveState { let mut rng = rand::thread_rng(); self.bolls.push(Box::new(SquareBoll { pos: point!( - rng.gen_range(0, SCREEN_WIDTH) as f64, - rng.gen_range(0, SCREEN_HEIGHT) as f64 + rng.gen_range(0, self.screen.width) as f64, + rng.gen_range(0, self.screen.height) as f64 ), vel: point!(rng.gen_range(-2.0, 2.0), rng.gen_range(-2.0, 2.0)), })); diff --git a/src/main.rs b/src/main.rs index 877d367..256cc3c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,14 +10,14 @@ mod common; mod boll; mod sprites; -const SCREEN_WIDTH: u32 = 1280; -const SCREEN_HEIGHT: u32 = (SCREEN_WIDTH as f64 * (1440.0 / 2560.0)) as u32; +const SCREEN_WIDTH: u16 = 1280; +const SCREEN_HEIGHT: u16 = (SCREEN_WIDTH as f64 * (1440.0 / 2560.0)) as u16; fn main() { println!("starting..."); let mut app = App::new() - .with_resolution(SCREEN_WIDTH as u16, SCREEN_HEIGHT as u16) - .with_state(Box::new(ActiveState::new())) + .with_resolution(SCREEN_WIDTH, SCREEN_HEIGHT) + .with_state(Box::new(ActiveState::new((SCREEN_WIDTH as u32, SCREEN_HEIGHT as u32)))) .with_title("SDL test") .build() .unwrap(); -- 2.11.0