flo_draw 0.3.1

Hassle-free windowed 2D graphics rendering
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use flo_draw::*;
use flo_canvas::*;
use flo_stream::*;

use rand::*;

use futures::prelude::*;
use futures::stream;
use futures::executor;
use futures_timer::*;

use std::f64;
use std::time::*;

///
/// Demonstration of `flo_draw` that illustrates a way to implement a simple game
///
pub fn main() {
    with_2d_graphics(|| {
        executor::block_on(async {
            // Set up
            let (canvas, events) = create_drawing_window_with_events("Vectoroids");

            // Create a tick generator
            let tick_stream = tick_stream();

            // Combine the tick generator into the events stream
            let events      = events.map(|evt| VectorEvent::DrawEvent(evt));
            let mut events  = stream::select(events, tick_stream);

            // Set up the canvas by declaring the sprites and the background
            let ship_sprite     = SpriteId(0);
            let bullet_sprite   = SpriteId(1);
            let roid_sprite     = SpriteId(2);
            canvas.draw(|gc| {
                gc.clear_canvas(Color::Rgba(0.1, 0.1, 0.1, 1.0));

                // Game area is a 1000x1000 square
                gc.canvas_height(1000.0);
                gc.center_region(0.0, 0.0, 1000.0, 1000.0);

                // Ship is just a triangle
                gc.sprite(ship_sprite);
                gc.clear_sprite();

                gc.new_path();
                gc.move_to(-10.0, -8.0);
                gc.line_to(0.0, 12.0);
                gc.line_to(10.0, -8.0);
                gc.line_to(8.0, -6.0);
                gc.line_to(-8.0, -6.0);
                gc.line_to(-10.0, -8.0);

                gc.line_width(2.0);
                gc.stroke_color(Color::Rgba(0.8, 0.7, 0.0, 1.0));
                gc.stroke();

                // Bullet is a square
                gc.sprite(bullet_sprite);
                gc.clear_sprite();

                gc.new_path();
                gc.rect(-1.0, -1.0, 1.0, 1.0);
                gc.fill_color(Color::Rgba(1.0, 0.8, 0.0, 1.0));
                gc.fill();

                // 'roids are an irregular shape
                gc.sprite(roid_sprite);
                gc.clear_sprite();

                gc.new_path();
                gc.move_to(0.0, -15.0);
                gc.line_to(-15.0, -25.0);
                gc.line_to(-35.0, -15.0);
                gc.line_to(-20.0, 0.0);
                gc.line_to(-25.0, 5.0);
                gc.line_to(-10.0, 20.0);
                gc.line_to(5.0, 25.0);
                gc.line_to(30.0, 5.0);
                gc.line_to(20.0, -15.0);
                gc.line_to(10.0, -25.0);
                gc.line_to(0.0, -15.0);

                gc.line_width(3.0);
                gc.stroke_color(Color::Rgba(0.6, 0.5, 0.0, 1.0));
                gc.stroke();

                // Background is a random starscape made of squares
                gc.layer(LayerId(0));
                gc.clear_layer();

                gc.fill_color(Color::Rgba(0.7, 0.7, 0.8, 1.0));
                for _star in 0..200 {
                    let x = random::<f32>() * 1000.0;
                    let y = random::<f32>() * 1000.0;

                    gc.rect(x-1.0, y-1.0, x+1.0, y+1.0);
                    gc.fill();
                }
            });

            // Run the main game loop
            let mut game_state = GameState::new(ship_sprite, roid_sprite);

            while let Some(event) = events.next().await {
                match event {
                    VectorEvent::Tick   => {
                        // Update the game state
                        game_state.tick();

                        // Draw the game on layer 1
                        canvas.draw(|gc| {
                            gc.layer(LayerId(1));
                            gc.clear_layer();

                            game_state.draw(gc);
                        });
                    }

                    VectorEvent::DrawEvent(DrawEvent::KeyDown(_, Some(Key::KeyLeft))) => {
                        game_state.ship.rotation = (360.0) / 60.0;
                    }
                    VectorEvent::DrawEvent(DrawEvent::KeyDown(_, Some(Key::KeyRight))) => {
                        game_state.ship.rotation = -(360.0) / 60.0;
                    }
                    VectorEvent::DrawEvent(DrawEvent::KeyUp(_, Some(Key::KeyLeft))) |
                    VectorEvent::DrawEvent(DrawEvent::KeyUp(_, Some(Key::KeyRight))) => {
                        game_state.ship.rotation = 0.0;
                    }

                    VectorEvent::DrawEvent(DrawEvent::KeyDown(_, Some(Key::KeyUp))) => {
                        game_state.ship.thrust = 0.3;
                    }
                    VectorEvent::DrawEvent(DrawEvent::KeyUp(_, Some(Key::KeyUp))) => {
                        game_state.ship.thrust = 0.0;
                    }

                    VectorEvent::DrawEvent(DrawEvent::KeyDown(_, Some(Key::KeySpace))) => {
                        game_state.bullets.push(Bullet::new(bullet_sprite, game_state.ship.x, game_state.ship.y, game_state.ship.vel_x, game_state.ship.vel_y, game_state.ship.angle));
                    }

                    _ => { /* Other events are ignored */ }
                }
            }
        });
    });
}

///
/// Events processed by the game
///
enum VectorEvent {
    Tick,
    DrawEvent(DrawEvent)
}

///
/// Represents the state of a game
///
struct GameState {
    ship:           Ship,
    roids:          Vec<Roid>,
    bullets:        Vec<Bullet>
}

///
/// Represents the state of the player's ship
///
struct Ship {
    sprite:     SpriteId,

    x:          f64,
    y:          f64,
    angle:      f64,
    vel_x:      f64,
    vel_y:      f64,
    rotation:   f64,
    thrust:     f64
}

///
/// Represents the state of a 'roid
///
struct Roid {
    sprite:     SpriteId,

    x:          f64,
    y:          f64,
    angle:      f64,
    rotation:   f64,
    vel_x:      f64,
    vel_y:      f64,
}

///
/// Represents the state of a bullet
///
struct Bullet {
    sprite:     SpriteId,
    x:          f64,
    y:          f64,
    vel_x:      f64,
    vel_y:      f64,
    time_left:  usize
}

impl GameState {
    ///
    /// Creates a new game state
    ///
    pub fn new(ship_sprite: SpriteId, roid_sprite: SpriteId) -> GameState {
        GameState {
            ship:           Ship::new(ship_sprite),
            roids:          (0..20).into_iter().map(|_| Roid::new(roid_sprite)).collect(),
            bullets:        vec![]
        }
    }

    ///
    /// Updates the game state after a tick
    ///
    pub fn tick(&mut self) {
        self.ship.tick();
        self.roids.iter_mut().for_each(|roid| roid.tick());
        self.bullets.iter_mut().for_each(|bullet| bullet.tick());

        self.bullets.retain(|bullet| bullet.time_left > 0);
    }

    pub fn draw(&self, gc: &mut dyn GraphicsContext) {
        self.roids.iter().for_each(|roid| roid.draw(gc));
        self.ship.draw(gc);
        self.bullets.iter().for_each(|bullet| bullet.draw(gc));
    }
}

impl Ship {
    ///
    /// Creates a new ship state
    ///
    pub fn new(sprite: SpriteId) -> Ship {
        Ship {
            sprite:     sprite,
            x:          500.0,
            y:          500.0,
            vel_x:      0.0,
            vel_y:      0.0,
            angle:      0.0,
            rotation:   0.0,
            thrust:     0.0
        }        
    }

    ///
    /// Updates the ship state after a tick
    ///
    pub fn tick(&mut self) {
        // Move the ship
        self.x      += self.vel_x;
        self.y      += self.vel_y;
        self.angle  += self.rotation;
        self.angle  = self.angle % 360.0;

        // Clip to the play area
        if self.x < 0.0 { self.x = 1000.0 };
        if self.y < 0.0 { self.y = 1000.0 };

        if self.x > 1000.0 { self.x = 0.0 };
        if self.y > 1000.0 { self.y = 0.0 };

        // Apply thrust
        let (acc_x, acc_y) = Transform2D::rotate_degrees(self.angle as _).transform_point(0.0, self.thrust as _);
        self.vel_x += acc_x as f64;
        self.vel_y += acc_y as f64;

        // Friction
        self.vel_x *= 0.99;
        self.vel_y *= 0.99;
    }

    pub fn draw(&self, gc: &mut dyn GraphicsContext) {
        gc.sprite_transform(SpriteTransform::Identity);
        gc.sprite_transform(SpriteTransform::Translate(self.x as _, self.y as _));
        gc.sprite_transform(SpriteTransform::Rotate(self.angle as _));
        gc.draw_sprite(self.sprite);
    }
}

impl Roid {
    ///
    /// Creates a new 'roid state
    ///
    pub fn new(sprite: SpriteId) -> Roid {
        Roid {
            sprite:     sprite,
            x:          random::<f64>() * 1000.0,
            y:          random::<f64>() * 1000.0,
            vel_x:      random::<f64>() * 3.0 - 1.5,
            vel_y:      random::<f64>() * 3.0 - 1.5,
            angle:      random::<f64>() * 360.0,
            rotation:   random::<f64>() * 8.0 - 4.0
        }        
    }

    ///
    /// Updates the 'roid state after a tick
    ///
    pub fn tick(&mut self) {
        // Move the 'roid
        self.x      += self.vel_x;
        self.y      += self.vel_y;
        self.angle  += self.rotation;
        self.angle  = self.angle % 360.0;

        // Clip to the play area
        if self.x < 0.0 { self.x = 1000.0 };
        if self.y < 0.0 { self.y = 1000.0 };

        if self.x > 1000.0 { self.x = 0.0 };
        if self.y > 1000.0 { self.y = 0.0 };
    }

    pub fn draw(&self, gc: &mut dyn GraphicsContext) {
        gc.sprite_transform(SpriteTransform::Identity);
        gc.sprite_transform(SpriteTransform::Translate(self.x as _, self.y as _));
        gc.sprite_transform(SpriteTransform::Rotate(self.angle as _));
        gc.draw_sprite(self.sprite);
    }
}

impl Bullet {
    ///
    /// Creates a new bullet state
    ///
    pub fn new(sprite: SpriteId, x: f64, y: f64, ship_vel_x: f64, ship_vel_y: f64, ship_angle: f64) -> Bullet {
        let transform               = Transform2D::rotate_degrees(ship_angle as _);
        let (offset_x, offset_y)    = transform.transform_point(0.0, 11.0);
        let (x, y)                  = (x+offset_x as f64, y+offset_y as f64);

        let (vel_x, vel_y)          = transform.transform_point(0.0, 4.0);
        let (vel_x, vel_y)          = (ship_vel_x+vel_x as f64, ship_vel_y+vel_y as f64);

        Bullet {
            sprite:     sprite,
            x:          x,
            y:          y,
            vel_x:      vel_x,
            vel_y:      vel_y,
            time_left:  90
        }        
    }

    ///
    /// Updates the bullet state after a tick
    ///
    pub fn tick(&mut self) {
        // Move the bullet
        self.x          += self.vel_x;
        self.y          += self.vel_y;
        self.time_left  -= 1;

        // Clip to the play area
        if self.x < 0.0 { self.x = 1000.0 };
        if self.y < 0.0 { self.y = 1000.0 };

        if self.x > 1000.0 { self.x = 0.0 };
        if self.y > 1000.0 { self.y = 0.0 };
    }

    pub fn draw(&self, gc: &mut dyn GraphicsContext) {
        gc.sprite_transform(SpriteTransform::Identity);
        gc.sprite_transform(SpriteTransform::Translate(self.x as _, self.y as _));
        gc.draw_sprite(self.sprite);
    }
}

///
/// A stream that generates a 'tick' event every time the game state should update
///
fn tick_stream() -> impl Send+Unpin+Stream<Item=VectorEvent> {
    generator_stream(|yield_value| async move {
        // Set up the clock
        let start_time          = Instant::now();
        let mut last_time       = Duration::from_millis(0);

        // We limit to a certain number of ticks per callback (in case the task is suspended or stuck for a prolonged period of time)
        let max_ticks_per_call  = 5;

        // Ticks are generated 60 times a second
        let tick_length         = Duration::from_nanos(1_000_000_000 / 60);

        loop {
            // Time that has elapsed since the last tick
            let elapsed         = start_time.elapsed() - last_time;

            // Time remaining
            let mut remaining   = elapsed;
            let mut num_ticks   = 0;
            while remaining >= tick_length {
                if num_ticks < max_ticks_per_call {
                    // Generate the tick
                    yield_value(VectorEvent::Tick).await;
                    num_ticks += 1;
                }

                // Remove from the remaining time, and update the last tick time
                remaining -= tick_length;
                last_time += tick_length;
            }

            // Wait for half a tick before generating more ticks
            let next_time = tick_length - remaining;
            let wait_time = Duration::min(tick_length / 2, next_time);

            Delay::new(wait_time).await;
        }
    }.boxed())
}