rain2d 1.0.0

Simple 2D game engine
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#![warn(missing_docs)]
//! rain2d core functionality

use minifb::{Window, WindowOptions, KeyRepeat, MouseMode};
use bresenham::Bresenham;
use std::{
    time::Duration,
    time::Instant,
    mem::swap
};

pub use crate::core::color::*;

/// Reexported from minifb
///
pub use minifb::Key as Key;

/// Reexported from minifb
///
pub use minifb::MouseButton as MouseButton;

use crate::core::rendertarget::*;

mod color;
mod rendertarget;

#[allow(unused_variables)]
/// Trait used to call event functions from main loop
///
/// It's not required to implement any of these functions
/// although you probably want to or nothing will happen
///
/// ### Example
/// ```no_run
/// use rain2d::core::*;
///
/// struct App;
///
/// impl RainApp for App {
///     // setup
///     fn on_start(&mut self) {}
///
///     // main loop
///     fn on_update(&mut self, rain: &mut RainCore, dt: std::time::Duration) {}
///
///     // cleanup
///     fn on_exit(&mut self) {}
/// }
///
/// let mut core = RainCore::init("example app",
///     640,
///     360,
///     true);
///
/// core.run(&mut App {});
/// ```
pub trait RainApp {
    /// Called once when the application starts
    ///
    /// Used to do any setup required by the main application
    fn on_start(&mut self) {}

    /// Called every frame
    ///
    /// `dt` is the time since the last update
    fn on_update(&mut self, rain: &mut RainCore, dt: Duration) {}

    /// Called before the application exits
    ///
    /// Used to clean up before exiting the main application
    fn on_exit(&mut self) {}
}

/// Engine state
pub struct RainCore {
    /// Sets if the application should exit when the escape key is pressed
    pub exit_on_esc: bool,

    active: bool,
    window_title: String,
    window: Option<Window>,
    screen_width: usize,
    screen_height: usize,
    render_target: RenderTarget,
    frame_timer: f32,
    frame_count: u32,
}

impl RainCore {
    /// Initializes the engine and opens a window with the specified dimensions
    ///
    /// Set `exit_on_esc` to true if you want the application to close when pressing escape
    ///
    /// Alternatively, you can control when to exit yourself by setting
    ///
    /// ### Example
    /// ```no_run
    /// use rain2d::core::*;
    ///
    /// let mut core = RainCore::init("example app",
    ///         640,
    ///         360,
    ///         true);
    /// ```
    pub fn init(window_title: &str, width: usize, height: usize, exit_on_esc: bool) -> Self {
        RainCore {
            exit_on_esc,
            active: true,
            window_title: window_title.to_string(),
            window: None,
            render_target: RenderTarget::new(width, height),
            screen_width: width,
            screen_height: height,
            frame_timer: 1.0,
            frame_count: 0,
        }
    }

    /// Starts the main loop
    ///
    /// This function won't return until the application is closed, use [`on_update`]
    /// to update the application state
    ///
    /// [`on_update`]: trait.RainApp.html#method.on_update
    ///
    /// ### Example
    /// ```no_run
    /// # use rain2d::core::*;
    /// #
    /// struct ExampleApp;
    ///
    /// impl RainApp for ExampleApp {}
    ///
    /// # let mut core = RainCore::init("example app", 640, 360, true);
    /// core.run(&mut ExampleApp {});
    /// ```
    pub fn run(&mut self, app: &mut dyn RainApp) {
        self.window = Some(Window::new(&self.window_title,
                                 self.screen_width,
                                 self.screen_height,
                                 WindowOptions::default()).unwrap());

        app.on_start();

        let mut last_time = Instant::now();
        while self.active {
            let current_time = Instant::now();
            let elapsed = current_time - last_time;
            last_time = current_time;

            // update state
            app.on_update(self, elapsed);

            // draw to screen
            if let Some(window) = &mut self.window {
                window.update_with_buffer(&self.render_target.data,
                                               self.render_target.width,
                                               self.render_target.height).unwrap();

            }

            // update frame count
            self.frame_timer += elapsed.as_secs_f32();
            self.frame_count += 1;
            if self.frame_timer >= 1.0 {
                self.frame_timer -= 1.0;

                if let Some(window) = &mut self.window {
                    let title = format!("{} - FPS: {}", self.window_title, self.frame_count);
                    window.set_title(&title);
                }

                self.frame_count = 0;
            }

            if let Some(window) = &self.window {
                // check window status
                if !window.is_open() {
                    self.active = false;
                }

                if self.exit_on_esc && window.is_key_down(Key::Escape) {
                    self.active = false;
                }
            }
        }

        app.on_exit();
    }

    /// Stops the main loop after the current frame has been drawn and calls [`on_exit`]
    ///
    /// ### Example
    ///```no_run
    ///# use rain2d::core::*;
    ///#
    ///# let mut core = RainCore::init("example app",
    ///#     640,
    ///#     360,
    ///#     true);
    ///#
    ///# core.run(&mut App {});
    ///#
    ///# struct App;
    ///#
    ///# impl RainApp for App {
    ///  fn on_update(&mut self, rain: &mut RainCore, dt: std::time::Duration) {
    ///      rain.exit();
    ///      // exit doesn't immediately stop the application so anything here will get executed
    ///  }
    ///# }
    ///```
    /// [`on_exit`]: trait.RainApp.html#method.on_exit
    pub fn exit(&mut self) {
        self.active = false;
    }

    /// Checks if the key is currently down
    ///
    /// ### Example
    /// ```no_run
    /// # use rain2d::core::*;
    /// # let core = RainCore::init("example app", 640, 360, true);
    /// if core.key_down(Key::Space) {
    ///     println!("Spacebar down");
    /// }
    /// ```
    pub fn key_down(&self, key: Key) -> bool {
        if let Some(window) = &self.window {
            return window.is_key_down(key);
        }
        false
    }

    /// Checks if the key was pressed (not held) since the last update
    pub fn key_pressed(&self, key: Key) -> bool {
        if let Some(window) = &self.window {
            return window.is_key_pressed(key, KeyRepeat::No);
        }
        false
    }

    /// Checks if the key was released since the last update
    pub fn key_released(&self, key: Key) -> bool {
        if let Some(window) = &self.window {
            window.is_key_released(key);
        }
        false
    }

    /// Gets all keys that are currently down
    ///
    /// ### Example
    /// ```no_run
    /// # use rain2d::core::*;
    /// # let mut core = RainCore::init("example app", 640, 360, true);
    /// core.get_keys().map(|keys| {
    ///     for key in keys {
    ///         match key {
    ///             Key::A => println!("A key down"),
    ///             Key::Escape => core.exit(),
    ///             _ => (),
    ///         }
    ///     }
    /// });
    /// ```
    pub fn get_keys(&self) -> Option<Vec<Key>> {
        if let Some(window) = &self.window {
            return window.get_keys();
        }
        None
    }

    /// Get mouse position relative to the window, (0, 0) in upper left corner
    ///
    /// ### Example
    /// ```no_run
    /// # use rain2d::core::*;
    /// # let core = RainCore::init("example app", 640, 360, true);
    /// if let Some((x, y)) = core.get_mouse_pos() {
    ///     println!("x: {}, y: {}", x, y);
    /// }
    /// ```
    pub fn get_mouse_pos(&self) -> Option<(f32, f32)> {
        if let Some(window) = &self.window {
            return window.get_mouse_pos(MouseMode::Pass);
        }
        None
    }

    /// Checks if the button is currently down
    ///
    /// ### Example
    /// ```no_run
    /// # use rain2d::core::*;
    /// # let core = RainCore::init("example app", 640, 360, true);
    /// if core.mouse_button_down(MouseButton::Left) {
    ///     println!("Left mouse button down");
    /// }
    /// ```
    pub fn mouse_button_down(&self, button: MouseButton) -> bool {
        if let Some(window) = &self.window {
            return window.get_mouse_down(button);
        }
        false
    }

    /// Get current scroll wheel movement
    ///
    /// ### Example
    /// ```no_run
    /// # use rain2d::core::*;
    /// # let core = RainCore::init("example app", 640, 360, true);
    /// if let Some((x, y)) = core.get_scroll_wheel() {
    ///     println!("x: {}, y: {}", x, y);
    /// }
    /// ```
    pub fn get_scroll_wheel(&self) -> Option<(f32,f32)> {
        if let Some(window) = &self.window {
            return window.get_scroll_wheel();
        }
        None
    }

    /// Clears the screen with the provided color
    ///
    /// ### Example
    /// ```no_run
    /// # use rain2d::core::*;
    /// # let mut core = RainCore::init("example app", 640, 360, true);
    /// core.clear(NONE);
    /// ```
    pub fn clear(&mut self, color: Color) {
        self.render_target.clear(color);
    }

    /// Draws a pixel if the location is in bounds
    ///
    /// ### Example
    /// ```no_run
    /// # use rain2d::core::*;
    /// # let mut core = RainCore::init("example app", 640, 360, true);
    /// core.draw(10, 10, WHITE);
    /// ```
    pub fn draw(&mut self, x: i32, y: i32, color: Color) {
        self.render_target.set_pixel(x, y, color);
    }

    /// Draws a line from `(x1, y1)` to `(x2, y2)`
    ///
    /// ### Example
    /// ```no_run
    /// # use rain2d::core::*;
    /// # let mut core = RainCore::init("example app", 640, 360, true);
    /// core.draw_line(10, 10, 100, 50, WHITE);
    /// ```
    pub fn draw_line(&mut self, mut x1: i32, mut y1: i32, mut x2: i32, mut y2: i32, color: Color) {

        // vertical line
        if x2 - x1 == 0 {
            if y2 < y1 { swap(&mut y1, &mut y2); }
            for y in y1..y2 {
                self.draw(x1, y, color);
            }
            return;
        }

        // horizontal line
        if y2 - y1 == 0 {
            if x2 < x1 { swap(&mut x1, &mut x2); }
            for x in x1..x2 {
                self.draw(x, y1, color);
            }
            return;
        }

        for (x,y) in Bresenham::new((x1 as isize, y1 as isize), (x2 as isize, y2 as isize)) {
            self.draw(x as i32, y as i32, color);
        }
    }

    /// Draws a circle at `(x, y)` with radius `r`
    ///
    /// ### Example
    /// ```no_run
    /// # use rain2d::core::*;
    /// # let mut core = RainCore::init("example app", 640, 360, true);
    /// core.draw_circle(100, 100, 10, WHITE);
    /// ```
    pub fn draw_circle(&mut self, x: i32, y: i32, r: i32, color: Color) {
        let mut x0 = 0;
        let mut y0 = r;
        let mut d = 3 - 2 * r;
        if r <= 0 { return; }

        while y0 >= x0 {
            self.draw(x + x0, y - y0, color);
            self.draw(x + y0, y - x0, color);
            self.draw(x + y0, y + x0, color);
            self.draw(x + x0, y + y0, color);
            self.draw(x - x0, y - y0, color);
            self.draw(x - y0, y - x0, color);
            self.draw(x - y0, y + x0, color);
            self.draw(x - x0, y + y0, color);
            if d < 0 { d += 4 * x0 + 6; x0 += 1; }
            else { x0 += 1; y0 -= 1; d += 4 * (x0 - y0) + 10; }
        }
    }

    /// Draws a filled in circle at `(x, y)` with radius `r`
    ///
    /// ### Example
    /// ```no_run
    /// # use rain2d::core::*;
    /// # let mut core = RainCore::init("example app", 640, 360, true);
    /// core.fill_circle(100, 100, 10, WHITE);
    /// ```
    pub fn fill_circle(&mut self, x: i32, y: i32, r: i32, color: Color) {
        let mut x0 = 0;
        let mut y0 = r;
        let mut d = 3 - 2 * r;
        if r <= 0 { return; }

        while y0 >= x0 {
            self.draw_line(x - x0, y - y0, x + x0, y - y0, color);
            self.draw_line(x - y0, y - x0, x + y0, y - x0, color);
            self.draw_line(x - x0, y + y0, x + x0, y + y0, color);
            self.draw_line(x - y0, y + x0, x + y0, y + x0, color);
            if d < 0 { d += 4 * x0 + 6; x0 += 1; }
            else { x0 += 1; y0 -= 1; d += 4 * (x0 - y0) + 10; }
        }
    }

    /// Draws a rectangle at `(x, y)` with specified dimensions
    ///
    /// ### Example
    /// ```no_run
    /// # use rain2d::core::*;
    /// # let mut core = RainCore::init("example app", 640, 360, true);
    /// core.draw_rect(100, 100, 50, 50, WHITE);
    /// ```
    pub fn draw_rect(&mut self, x: i32, y: i32, width: i32, height: i32, color: Color) {
        self.draw_line(x, y, x + width, y, color);
        self.draw_line(x + width, y, x + width, y + height, color);
        self.draw_line(x + width, y + height, x, y + height, color);
        self.draw_line(x, y + height, x, y, color);
    }

    /// Draws a filled in rectangle at `(x, y)` with specified dimensions
    ///
    /// ### Example
    /// ```no_run
    /// # use rain2d::core::*;
    /// # let mut core = RainCore::init("example app", 640, 360, true);
    /// core.fill_rect(100, 100, 50, 50, WHITE);
    /// ```
    pub fn fill_rect(&mut self, x: i32, y: i32, width: i32, height: i32, color: Color) {
        let x_max = x + width;
        let y_max = y + height;
        for i in x..x_max {
            for j in y..y_max {
                self.draw(i, j, color);
            }
        }
    }

    /// Draws a triangle with vertices `(x1, y1)`, `(x2, y2)` and `(x3, y3)`
    ///
    /// ### Example
    /// ```no_run
    /// # use rain2d::core::*;
    /// # let mut core = RainCore::init("example app", 640, 360, true);
    /// core.draw_triangle(25, 100, 75, 100, 50, 0, WHITE);
    /// ```
    pub fn draw_triangle(&mut self, x1: i32, y1: i32, x2: i32, y2: i32, x3: i32, y3: i32, color: Color) {
        self.draw_line(x1, y1, x2, y2, color);
        self.draw_line(x2, y2, x3, y3, color);
        self.draw_line(x3, y3, x1, y1, color);
    }

    /// Draws a filled in triangle with vertices `(x1, y1)`, `(x2, y2)` and `(x3, y3)`
    ///
    /// ### Example
    /// ```no_run
    /// # use rain2d::core::*;
    /// # let mut core = RainCore::init("example app", 640, 360, true);
    /// core.fill_triangle(25, 100, 75, 100, 50, 0, WHITE);
    /// ```
    // http://www.sunshine2k.de/coding/java/TriangleRasterization/TriangleRasterization.html
    pub fn fill_triangle(&mut self, mut x1: i32, mut y1: i32, mut x2: i32, mut y2: i32, mut x3: i32, mut y3: i32, color: Color) {
        // sort vertices
        if y1 > y2 { swap(&mut x1, &mut x2); swap(&mut y1, &mut y2); }
        if y1 > y3 { swap(&mut x1, &mut x3); swap(&mut y1, &mut y3); }
        if y2 > y3 { swap(&mut x2, &mut x3); swap(&mut y2, &mut y3); }

        // flat bottom triangle
        if y2 == y3 {
            self.fill_triangle_bottom(x1, y1, x2, y2, x3, y3, color);
        }
        // flat top triangle
        else if y1 == y2 {
            self.fill_triangle_top(x1, y1, x2, y2, x3, y3, color);
        }
        // split triangle and fill sides
        else {
            let x4 = x1 + f32::round(((y2 - y1) as f32 / (y3 - y1) as f32) * (x3 - x1) as f32) as i32;
            self.fill_triangle_bottom(x1, y1, x2, y2, x4, y2, color);
            self.fill_triangle_top(x2, y2, x4, y2, x3, y3, color);
        }
    }

    fn fill_triangle_bottom(&mut self, x1: i32, y1: i32, x2: i32, y2: i32, x3: i32, y3: i32, color: Color) {
        // calculate slope
        let s1 = (x2 - x1) as f32 / (y2 - y1) as f32;
        let s2 = (x3 - x1) as f32 / (y3 - y1) as f32;

        let mut x1 = x1 as f32;
        let mut x2 = x1 as f32;

        // draw scanlines, adjust ends of lines according to slopes
        for y in y1..=y2 {
            self.draw_line(f32::round(x1) as i32, y, f32::round(x2) as i32, y, color);
            x1 += s1;
            x2 += s2;
        }
    }

    fn fill_triangle_top(&mut self, x1: i32, y1: i32, x2: i32, y2: i32, x3: i32, y3: i32, color: Color) {
        // calculate slopes
        let s1 = (x3 - x1) as f32 / (y3 - y1) as f32;
        let s2 = (x3 - x2) as f32 / (y3 - y2) as f32;

        let mut x1 = x3 as f32;
        let mut x2 = x3 as f32;

        // draw scanlines, adjust ends of lines according to slopes
        for y in (y1..=y3).rev() {
            self.draw_line(f32::round(x1) as i32, y, f32::round(x2) as i32, y, color);
            x1 -= s1;
            x2 -= s2;
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    fn create_core(w: usize, h: usize) -> RainCore {
        RainCore::init("", w, h, false)
    }

    #[test]
    fn test_clear() {
        let mut core = create_core(10, 10);

        core.clear(WHITE);
        for p in core.render_target.data {
            assert_eq!(p, WHITE.into());
        }
    }

    #[test]
    fn test_draw() {
        let mut core = create_core(10, 10);

        core.draw(5, 3, WHITE);
        assert_eq!(core.render_target.get_pixel(5, 3), Some(WHITE));
    }
}