colour_cycle/
colour_cycle.rs

1use std::error::Error;
2
3use simple_game_engine::{self as sge, prelude::*};
4
5const CYCLE_SPEED: f32 = 130.0;
6const SCREEN_WIDTH: u32 = 480;
7const SCREEN_HEIGHT: u32 = 360;
8
9struct App {
10    col: f32,
11    flipper: bool,
12}
13
14impl App {
15    pub fn new() -> Self {
16        Self {
17            col: 255.0,
18            flipper: true,
19        }
20    }
21}
22
23impl sge::Application for App {
24    fn on_update(
25        &mut self,
26        canvas: &mut WindowCanvas,
27        input: &InputState,
28        elapsed_time: f64,
29    ) -> sge::ApplicationResult {
30        // Handle keyboard input
31        if input.keyboard.pressed(Scancode::Q) {
32            return Ok(false);
33        }
34        // If we're at the bounds for a colour value, change direction
35        if self.col <= 0.0 || self.col >= 255.0 {
36            self.flipper = !self.flipper;
37        }
38        // Fill the screen with the current colour
39        canvas.set_draw_color(Color::RGB(self.col as u8, 0, 255 - self.col as u8));
40        canvas.clear();
41        // Change the colour
42        if !self.flipper {
43            self.col -= CYCLE_SPEED * elapsed_time as f32;
44        } else {
45            self.col += CYCLE_SPEED * elapsed_time as f32;
46        }
47        Ok(true)
48    }
49
50    fn on_quit(&mut self) -> Result<(), Box<dyn Error>> {
51        println!("Quitting ...");
52        Ok(())
53    }
54}
55
56fn main() -> Result<(), Box<dyn Error>> {
57    let mut app = App::new();
58    let mut engine = sge::Engine::new(&mut app, "Test App", SCREEN_WIDTH, SCREEN_HEIGHT)?;
59    engine.start(false)
60}