Moved rendering to ActiveState
authorTomas Wenström <tomas.wenstrom@gmail.com>
Wed, 6 Jan 2021 15:58:02 +0000 (16:58 +0100)
committerTomas Wenström <tomas.wenstrom@gmail.com>
Wed, 6 Jan 2021 15:58:02 +0000 (16:58 +0100)
src/game/app.rs

index 88f8e88..f6adba0 100644 (file)
@@ -2,16 +2,13 @@ use boll::*;
 use common::{Point2D, Rect};
 use point; // defined in common, but loaded from main...
 use rand::Rng;
-use sdl2::event::Event;
-use sdl2::event::WindowEvent;
+use sdl2::event::{Event, WindowEvent};
 use sdl2::gfx::primitives::DrawRenderer;
 use sdl2::keyboard::Keycode;
 use sdl2::pixels::Color;
 use sdl2::rect::Rect as SDLRect;
-use sdl2::render::BlendMode;
-use sdl2::render::Canvas;
-use sdl2::video::FullscreenType;
-use sdl2::video::{SwapInterval, Window};
+use sdl2::render::{BlendMode, Canvas};
+use sdl2::video::{FullscreenType, SwapInterval, Window};
 use sdl2::{EventPump, VideoSubsystem};
 use sprites::SpriteManager;
 use std::f32::consts::PI;
@@ -149,189 +146,18 @@ 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;
 
         'running: loop {
-            self.canvas.set_draw_color(Color::RGB(0, 0, 0));
-            self.canvas.clear();
-            {
-                let blocks = 20;
-                let size = 32;
-                let offset = point!(
-                    (screen.width - (blocks + 1) * size) / 2,
-                    (screen.height - (blocks + 1) * size) / 2
-                );
-                let block = self.sprites.get("block");
-                for i in 0..blocks {
-                    self.canvas
-                        .copy(
-                            block,
-                            None,
-                            SDLRect::new((i) * size + offset.x, offset.y, size as u32, size as u32),
-                        )
-                        .unwrap();
-                    self.canvas
-                        .copy(
-                            block,
-                            None,
-                            SDLRect::new(
-                                (blocks - i) * size + offset.x,
-                                (blocks) * size + offset.y,
-                                size as u32,
-                                size as u32,
-                            ),
-                        )
-                        .unwrap();
-                    self.canvas
-                        .copy(
-                            block,
-                            None,
-                            SDLRect::new(
-                                offset.x,
-                                (blocks - i) * size + offset.y,
-                                size as u32,
-                                size as u32,
-                            ),
-                        )
-                        .unwrap();
-                    self.canvas
-                        .copy(
-                            block,
-                            None,
-                            SDLRect::new(
-                                (blocks) * size + offset.x,
-                                (i) * size + offset.y,
-                                size as u32,
-                                size as u32,
-                            ),
-                        )
-                        .unwrap();
-                }
-            }
-            {
-                let size = 64;
-                let offset = point!(
-                    (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;
-                let offset2 = point!((angle.cos() * radius) as i32, (angle.sin() * radius) as i32);
-                self.canvas
-                    .copy_ex(
-                        self.sprites.get("mario"),
-                        None,
-                        SDLRect::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);
-                self.canvas
-                    .circle(p.x, p.y, 100, Color::RGB(255, 255, 255))
-                    .unwrap();
-                self.canvas
-                    .aa_circle(p.x, p.y, 110, Color::RGB(255, 255, 255))
-                    .unwrap();
-                self.canvas
-                    .ellipse(p.x, p.y, 50, 100, Color::RGB(255, 255, 255))
-                    .unwrap();
-                self.canvas
-                    .aa_ellipse(p.x, p.y, 110, 55, Color::RGB(255, 255, 255))
-                    .unwrap();
-            }
-
-            //        window.gl_swap_window();
-            for event in self.event_pump.poll_iter() {
-                match event {
-                    Event::Quit { .. }
-                    | Event::KeyDown {
-                        keycode: Some(Keycode::Escape),
-                        ..
-                    } => {
-                        break 'running;
-                    }
-                    Event::KeyDown {
-                        keycode: Some(Keycode::F11),
-                        ..
-                    } => {
-                        match self.canvas.window().fullscreen_state() {
-                            FullscreenType::Off => self
-                                .canvas
-                                .window_mut()
-                                .set_fullscreen(FullscreenType::Desktop),
-                            _ => self.canvas.window_mut().set_fullscreen(FullscreenType::Off),
-                        }
-                        .unwrap();
-                    }
-                    Event::Window {
-                        win_event: WindowEvent::Resized(x, y),
-                        ..
-                    } => {
-                        println!("window resized({}, {})", x, y)
-                    }
-                    Event::Window {
-                        win_event: WindowEvent::Maximized,
-                        ..
-                    } => {
-                        println!("window maximized")
-                    }
-                    Event::Window {
-                        win_event: WindowEvent::Restored,
-                        ..
-                    } => {
-                        println!("window restored")
-                    }
-                    Event::Window {
-                        win_event: WindowEvent::Enter,
-                        ..
-                    } => {
-                        println!("window enter")
-                    }
-                    Event::Window {
-                        win_event: WindowEvent::Leave,
-                        ..
-                    } => {
-                        println!("window leave")
-                    }
-                    Event::Window {
-                        win_event: WindowEvent::FocusGained,
-                        ..
-                    } => {
-                        println!("window focus gained")
-                    }
-                    Event::Window {
-                        win_event: WindowEvent::FocusLost,
-                        ..
-                    } => {
-                        println!("window focus lost")
-                    }
-                    _ => self.state.on_event(event),
-                }
-            }
+           if let Err(_) = self.handle_events() {
+               break 'running;
+           }
 
             let duration =
                 last_time.to(PreciseTime::now()).num_nanoseconds().unwrap() as Nanoseconds;
             last_time = PreciseTime::now();
             self.state.update(duration);
-            self.state.render(&mut self.canvas);
-            self.canvas.present();
+
+           self.render();
 
             frame_count += 1;
             if frame_count == FPS as u64 {
@@ -345,21 +171,101 @@ impl App {
 
         self.state.leave();
     }
+
+    fn handle_events(&mut self) -> Result<(), ()> {
+        for event in self.event_pump.poll_iter() {
+            match event {
+                Event::Quit { .. }
+                | Event::KeyDown {
+                    keycode: Some(Keycode::Escape),
+                    ..
+                } => {
+                   return Err(())
+                }
+                Event::KeyDown {
+                    keycode: Some(Keycode::F11),
+                    ..
+                } => {
+                    match self.canvas.window().fullscreen_state() {
+                        FullscreenType::Off => self
+                            .canvas
+                            .window_mut()
+                            .set_fullscreen(FullscreenType::Desktop),
+                        _ => self.canvas.window_mut().set_fullscreen(FullscreenType::Off),
+                    }
+                    .unwrap();
+                }
+                Event::Window {
+                    win_event: WindowEvent::Resized(x, y),
+                    ..
+                } => {
+                    println!("window resized({}, {})", x, y)
+                }
+                Event::Window {
+                    win_event: WindowEvent::Maximized,
+                    ..
+                } => {
+                    println!("window maximized")
+                }
+                Event::Window {
+                    win_event: WindowEvent::Restored,
+                    ..
+                } => {
+                    println!("window restored")
+                }
+                Event::Window {
+                    win_event: WindowEvent::Enter,
+                    ..
+                } => {
+                    println!("window enter")
+                }
+                Event::Window {
+                    win_event: WindowEvent::Leave,
+                    ..
+                } => {
+                    println!("window leave")
+                }
+                Event::Window {
+                    win_event: WindowEvent::FocusGained,
+                    ..
+                } => {
+                    println!("window focus gained")
+                }
+                Event::Window {
+                    win_event: WindowEvent::FocusLost,
+                    ..
+                } => {
+                    println!("window focus lost")
+                }
+                _ => self.state.on_event(event),
+            }
+        }
+       Ok(())
+    }
+
+    fn render(&mut self) {
+        self.canvas.set_draw_color(Color::RGB(0, 0, 0));
+        self.canvas.clear();
+        self.state.render(&mut self.canvas, &mut self.sprites);
+        self.canvas.present();
+    }
 }
 
 pub trait AppState {
     fn update(&mut self, dt: Nanoseconds);
-    fn render(&self, canvas: &mut Canvas<Window>);
+    fn render(&mut self, canvas: &mut Canvas<Window>, sprites: &mut SpriteManager);
     fn leave(&self);
     fn on_event(&mut self, event: Event);
 }
 
 type Bollar = Vec<Box<dyn Boll>>;
 
+#[derive(Default)]
 pub struct ActiveState {
     screen: Rect<u32>,
     bolls: Bollar,
     boll_size: u32,
+    mario_angle: f64,
 }
 
 impl ActiveState {
@@ -368,6 +274,7 @@ impl ActiveState {
             bolls: Bollar::new(),
             boll_size: 1,
            screen: Rect::from(screen),
+           ..Default::default()
         }
     }
 
@@ -409,7 +316,106 @@ impl AppState for ActiveState {
         }
     }
 
-    fn render(&self, canvas: &mut Canvas<Window>) {
+    fn render(&mut self, canvas: &mut Canvas<Window>, sprites: &mut SpriteManager) {
+        /* draw square of blocks */ {
+            let blocks = 20;
+            let size = 32;
+            let offset = point!(
+                (self.screen.width as i32 - (blocks + 1) * size) / 2,
+                (self.screen.height as i32 - (blocks + 1) * size) / 2
+            );
+            let block = sprites.get("block");
+            for i in 0..blocks {
+                canvas
+                    .copy(
+                        block,
+                        None,
+                        SDLRect::new((i) * size + offset.x, offset.y, size as u32, size as u32),
+                    )
+                    .unwrap();
+                canvas
+                    .copy(
+                        block,
+                        None,
+                        SDLRect::new(
+                            (blocks - i) * size + offset.x,
+                            (blocks) * size + offset.y,
+                            size as u32,
+                            size as u32,
+                        ),
+                    )
+                    .unwrap();
+                canvas
+                    .copy(
+                        block,
+                        None,
+                        SDLRect::new(
+                            offset.x,
+                            (blocks - i) * size + offset.y,
+                            size as u32,
+                            size as u32,
+                        ),
+                    )
+                    .unwrap();
+                canvas
+                    .copy(
+                        block,
+                        None,
+                        SDLRect::new(
+                            (blocks) * size + offset.x,
+                            (i) * size + offset.y,
+                            size as u32,
+                            size as u32,
+                        ),
+                    )
+                    .unwrap();
+            }
+        }
+
+        /* draw mario */ {
+            let size = 64;
+            let offset = point!(
+                (self.screen.width as i32 - size) / 2,
+                (self.screen.height as i32 - size) / 2
+            );
+            let radius = 110.0 + size as f32 * 0.5;
+            let angle = (self.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,
+                    SDLRect::new(
+                        offset.x + offset2.x,
+                        offset.y + offset2.y,
+                        size as u32,
+                        size as u32,
+                    ),
+                    self.mario_angle,
+                    sdl2::rect::Point::new(size / 2, size / 2),
+                    false,
+                    false,
+                )
+                .unwrap();
+           self.mario_angle = (self.mario_angle + 1.0) % 360.0;
+        }
+
+        /* draw circles and ellipses*/ {
+            let p = point!((self.screen.width / 2) as i16, (self.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();
+        }
+
         for b in &self.bolls {
             b.draw(canvas, self.boll_size);
         }