shapes/shapes.rs
1//! Demonstrates common shape drawing.
2
3use qilin::game::context::GameContext;
4use qilin::game::game::Game;
5use qilin::render::canvas::Canvas;
6use qilin::render::color::Color;
7use qilin::render::sketch::Sketch;
8use qilin::scene::Scene;
9use qilin::simplified::vec2;
10use qilin::types::{GameConfig, FPS30};
11use qilin::ScaleMode;
12use qilin::WindowOptions;
13
14struct ShapeScene;
15
16impl Scene for ShapeScene {
17 // create new empty scene
18 fn new() -> Self
19 where
20 Self: Sized,
21 {
22 Self
23 }
24
25 // gets called when game enters current scene
26 fn enter(&mut self) { println!("What do you call a fake noodle?") }
27
28 // gets called when window requests draw updates
29 fn update(&mut self, canvas: &mut Canvas, _ctx: &mut GameContext) {
30 // draw a sketch containing a single shape/line
31 canvas.draw(
32 Sketch::new()
33 // draw line from (0, 0) to (300, 600) with color green
34 .line(vec2(0, 0), vec2(300, 600), Color::GREEN),
35 );
36
37 // draw a sketch containing multiple shapes
38 canvas.draw(
39 Sketch::new()
40 // draw circle at (100, 100) with radius of 30 and color red
41 .circle(vec2(100, 100), 30, Color::RED)
42 // draw rectangle at (300, 300) with width of 100 and height of 100 and color blue
43 .rect(vec2(300, 300), 100, 100, Color::BLUE)
44 .oval(vec2(400, 200), 120, 60, Color::CYAN),
45 );
46 }
47
48 fn fixed_update(&mut self, _canvas: &mut Canvas, _ctx: &mut GameContext) {
49 // Will be called X times per second.
50 // This ensures, physics are applied independent of frame-rate.
51 // See https://docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html for FixedUpdate() in Unity.
52 }
53
54 // gets called when game exits current scene
55 fn exit(&mut self) { println!("An impasta!") }
56}
57
58fn main() {
59 Game::new::<ShapeScene>() // create game object with ShapeScene as entry scene
60 .with_config(GameConfig {
61 title: "My Shapes".to_string(), // set window title
62 update_rate_limit: FPS30, // limit update rate to 30 fps, default is 60 fps
63 width: 800, // set initial width
64 height: 600, // set initial height
65 fixed_time_step: Default::default(), // for better docs, see GameConfig or examples/move.
66 window: WindowOptions {
67 scale_mode: ScaleMode::AspectRatioStretch, // scale pixels to fit in aspect ratio
68 resize: true, // make window resizeable
69 ..Default::default()
70 },
71 })
72 .play()
73 .expect("Failed to play game");
74}