maylib 0.2.1

A rust-native raylib alternative with multiple window support
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
use rand::{rng, Rng};
use rodio::{OutputStream, OutputStreamHandle};
use sdl2::clipboard::ClipboardUtil;
use sdl2::image::InitFlag;
use sdl2::keyboard::Scancode;
use sdl2::mouse::{MouseButton, MouseUtil};
use sdl2::render::TextureCreator;
use sdl2::video::FullscreenType;
use sdl2::{event::{Event, WindowEvent}, pixels, render::Canvas, video, EventPump, Sdl, TimerSubsystem, VideoSubsystem};
use std::{collections::HashMap, str};

/// Defines a color
#[derive(Clone, Copy)]
pub struct Color {
    r: u8,
    g: u8,
    b: u8,
    a: u8,
}
#[allow(non_upper_case_globals)]
impl Color {
    /// Creates a new solid color
    pub fn new(r: u8, g: u8, b: u8) -> Color {
        Color { r, g, b, a: 255 }
    }

    /// Creates a new color with transparency
    pub fn new_alpha(r: u8, g: u8, b: u8, a: u8) -> Color {
        Color { r, g, b, a }
    }

    pub const White: Color = Color {
        r: 0xFF,
        g: 0xFF,
        b: 0xFF,
        a: 0xFF,
    };

    /// Raylib logo white
    pub const RayWhite: Color = Color {
        r: 0xF5,
        g: 0xF5,
        b: 0xF5,
        a: 0xFF,
    };

    pub const Silver: Color = Color {
        r: 0xC0,
        g: 0xC0,
        b: 0xC0,
        a: 0xFF,
    };

    pub const Gray: Color = Color {
        r: 0x80,
        g: 0x80,
        b: 0x80,
        a: 0xFF,
    };

    /// Maylib logo gray
    pub const MayGray: Color = Color {
        r: 0x28,
        g: 0x28,
        b: 0x28,
        a: 0xFF,
    };

    pub const Black: Color = Color {
        r: 0x00,
        g: 0x00,
        b: 0x00,
        a: 0xFF,
    };

    pub const Red: Color = Color {
        r: 0xFF,
        g: 0x00,
        b: 0x00,
        a: 0xFF,
    };

    pub const Maroon: Color = Color {
        r: 0x80,
        g: 0x00,
        b: 0x00,
        a: 0xFF,
    };

    pub const Yellow: Color = Color {
        r: 0xFF,
        g: 0xFF,
        b: 0x00,
        a: 0xFF,
    };

    pub const Olive: Color = Color {
        r: 0x80,
        g: 0x80,
        b: 0x00,
        a: 0xFF,
    };

    pub const Lime: Color = Color {
        r: 0x00,
        g: 0xFF,
        b: 0x00,
        a: 0xFF,
    };

    pub const Green: Color = Color {
        r: 0x00,
        g: 0x80,
        b: 0x00,
        a: 0xFF,
    };

    pub const Aqua: Color = Color {
        r: 0x00,
        g: 0xFF,
        b: 0xFF,
        a: 0xFF,
    };

    pub const Teal: Color = Color {
        r: 0x00,
        g: 0x80,
        b: 0x80,
        a: 0xFF,
    };

    pub const Blue: Color = Color {
        r: 0x00,
        g: 0x00,
        b: 0xFF,
        a: 0xFF,
    };

    pub const Navy: Color = Color {
        r: 0x00,
        g: 0x00,
        b: 0x80,
        a: 0xFF,
    };

    pub const Fuchsia: Color = Color {
        r: 0xFF,
        g: 0x00,
        b: 0xFF,
        a: 0xFF,
    };

    pub const Purple: Color = Color {
        r: 0x80,
        g: 0x00,
        b: 0x80,
        a: 0xFF,
    };
}
impl From<Color> for pixels::Color {
    /// Yield a sdl2::pixels::Color from a maylib::core::Color
    fn from(value: Color) -> Self {
        pixels::Color {
            r: value.r,
            g: value.g,
            b: value.b,
            a: value.a,
        }
    }
}

// A window
pub(crate) struct Window {
    /// The actual window
    pub(crate) window: video::Window,
    /// The canvas to draw on
    pub(crate) canvas: Canvas<video::Window>,
    /// The texture creator
    pub(crate) texture: TextureCreator<video::WindowContext>,
    /// Window readiness
    ready: bool,
    /// Should window close
    should_close: bool,
    /// Is window fullscreen
    fullscreen: bool,
    /// Is window hidden
    hidden: bool,
    /// Is window minimized
    minimized: bool,
    /// Is window maximized
    maximized: bool,
    /// Is window focused
    focused: bool,
    /// Is window bordered
    bordered: bool,
    /// Has window been resized
    resized: bool,
    /// Previous frame time
    previous_time: f64,
    /// Current time
    current_time: f64,
    /// Time window was opened
    start_time: f64,
}

/// The maylib state and logic
pub struct Maylib {
    /// SDL's video subsystem
    video: VideoSubsystem,
    /// The event pump
    event_pump: EventPump,
    /// The mouse
    mouse: MouseUtil,
    /// The timer/clock
    timer: TimerSubsystem,
    /// Clipboard access
    clipboard: ClipboardUtil,
    /// The currently open window
    pub(crate) current_window: u32,
    /// all windows
    pub(crate) windows: HashMap<u32, Window>,
    /// The frame rate to run at
    frame_rate: i32,
    /// The delay between each frame
    frame_time: f32,
    /// The audio stream. Unused, but needs to stay loaded
    _audio_stream: OutputStream,
    /// The audio stream handle
    pub(crate) audio: OutputStreamHandle
}
impl Maylib {
    /// Initialize maylib
    pub fn init() -> Result<Maylib, String> {
        let sdl: Sdl = match sdl2::init() {
            Ok(s) => s,
            Err(e) => {
                return Err(e);
            }
        };
        let video: VideoSubsystem = match sdl.video() {
            Ok(v) => v,
            Err(e) => {
                return Err(e);
            }
        };
        let event_pump: EventPump = match sdl.event_pump() {
            Ok(p) => p,
            Err(e) => {
                return Err(e);
            }
        };
        let clipboard: ClipboardUtil = video.clipboard();
        let mouse: MouseUtil = sdl.mouse();
        let timer: TimerSubsystem = match sdl.timer() {
            Ok(t) => t,
            Err(e) => {
                return Err(e);
            }
        };
        sdl2::image::init(InitFlag::PNG | InitFlag::JPG | InitFlag::TIF | InitFlag::WEBP)
            .expect("Image should init successfully");
        let (_audio_stream, audio) = match OutputStream::try_default() {
            Ok(s) => {
                s
            }
            Err(e) => {
                return Err(e.to_string().to_owned());
            }
        };
        Ok(Maylib {
            video,
            event_pump,
            clipboard,
            mouse,
            timer,
            current_window: 4294967295,
            windows: HashMap::new(),
            frame_rate: 60,
            frame_time: 1.0 / 60f32,
            _audio_stream,
            audio
        })
    }

    /// Initialize a window
    pub fn init_window(&mut self, title: &str, width: u32, height: u32) -> Result<u32, String> {
        let winctx: video::Window = match self
            .video
            .window(title, width, height)
            .position_centered()
            .build()
        {
            Ok(w) => w,
            Err(e) => return Err(e.to_string()),
        };
        let id = winctx.id();
        let start_time = self.timer.ticks64() as f64 / 1000f64;
        let canvas: Canvas<video::Window> = match winctx.clone().into_canvas().build() {
            Ok(c) => c,
            Err(e) => {
                return Err(e.to_string());
            }
        };
        let window = Window {
            texture: canvas.texture_creator(),
            canvas,
            ready: true,
            should_close: false,
            fullscreen: false,
            hidden: false,
            minimized: false,
            maximized: false,
            focused: false,
            resized: false,
            bordered: true,
            window: winctx,
            previous_time: 0.0,
            current_time: start_time,
            start_time,
        };
        self.windows.insert(id, window);
        Ok(id)
    }

    /// Close the current window
    pub fn close_window(&mut self) {
        let window = self.windows.remove(&self.current_window);
        drop(window);
    }

    /// Should the current window close
    pub fn window_should_close(&self) -> bool {
        self.windows
            .get(&self.current_window)
            .expect("Window should be valid if loaded from switch_window")
            .should_close
    }

    /// Set the framerate of all windows
    pub fn set_frame_rate(&mut self, rate: i32) {
        self.frame_rate = rate;
        self.frame_time = 1.0 / rate as f32;
    }

    /// Checks if all windows are closed
    pub fn all_windows_closed(&self) -> bool {
        for win in self.windows.iter() {
            if win.1.ready {
                return false;
            }
        }
        true
    }

    /// checks if current window is ready
    pub fn is_window_ready(&self) -> bool {
        self.windows
            .get(&self.current_window)
            .expect("Window should be valid if loaded from switch_window")
            .ready
    }

    /// checks if current window
    pub fn is_window_fullscreen(&self) -> bool {
        self.windows
            .get(&self.current_window)
            .expect("Window should be valid if loaded from switch_window")
            .fullscreen
    }

    /// checks if current window is hidden
    pub fn is_window_hidden(&self) -> bool {
        self.windows
            .get(&self.current_window)
            .expect("Window should be valid if loaded from switch_window")
            .hidden
    }

    /// checks if current window is minimized
    pub fn is_window_minimized(&self) -> bool {
        self.windows
            .get(&self.current_window)
            .expect("Window should be valid if loaded from switch_window")
            .minimized
    }

    /// checks if current window is maximized
    pub fn is_window_maximized(&self) -> bool {
        self.windows
            .get(&self.current_window)
            .expect("Window should be valid if loaded from switch_window")
            .maximized
    }

    /// checks if current window is focused
    pub fn is_window_focused(&self) -> bool {
        self.windows
            .get(&self.current_window)
            .expect("Window should be valid if loaded from switch_window")
            .focused
    }

    /// checks if current window is resized
    pub fn is_window_resized(&self) -> bool {
        self.windows
            .get(&self.current_window)
            .expect("Window should be valid if loaded from switch_window")
            .resized
    }

    /// toggle fullscreen for current window
    pub fn toggle_fullscreen(&mut self) {
        match self.windows.get_mut(&self.current_window) {
            Some(w) => {
                w.fullscreen = !w.fullscreen;
            }
            None => {
                panic!("Window should be valid if loaded from switch_window")
            }
        }
        if self.is_window_fullscreen() {
            self.windows
                .get_mut(&self.current_window)
                .expect("Window should be valid if loaded from switch_window")
                .window
                .set_fullscreen(FullscreenType::Off)
                .expect("Window should exit fullscreen");
        } else {
            self.windows
                .get_mut(&self.current_window)
                .expect("Window should be valid if loaded from switch_window")
                .window
                .set_fullscreen(FullscreenType::True)
                .expect("Window should enter fullscreen");
        }
    }

    /// toggle borderless for current window
    pub fn toggle_borderless_windowed(&mut self) {
        match self.windows.get_mut(&self.current_window) {
            Some(w) => {
                w.bordered = !w.bordered;
            }
            None => {
                panic!("Window should be valid if loaded from switch_window");
            }
        }
        if self
            .windows
            .get_mut(&self.current_window)
            .expect("Window should be valid if loaded from switch_window")
            .bordered
        {
            self.windows
                .get_mut(&self.current_window)
                .expect("Window should be valid if loaded from switch_window")
                .window
                .set_bordered(false);
        } else {
            self.windows
                .get_mut(&self.current_window)
                .expect("Window should be valid if loaded from switch_window")
                .window
                .set_bordered(true);
        }
    }

    /// get time since current window opened
    pub fn time_since_open(&self) -> f64 {
        let start_time = self
            .windows
            .get(&self.current_window)
            .expect("Window should be valid if loaded from switch_window")
            .start_time;
        self.windows
            .get(&self.current_window)
            .expect("Window should be valid if loaded from switch_window")
            .current_time
            - start_time
    }

    /// maximize current window
    pub fn maximize_window(&mut self) {
        self.windows
            .get_mut(&self.current_window)
            .expect("Window should be valid if loaded from switch_window")
            .maximized = true;
        self.windows
            .get_mut(&self.current_window)
            .expect("Window should be valid if loaded from switch_window")
            .window
            .maximize();
    }

    /// minimize current window
    pub fn minimize_window(&mut self) {
        self.windows
            .get_mut(&self.current_window)
            .expect("Window should be valid if loaded from switch_window")
            .maximized = true;
        self.windows
            .get_mut(&self.current_window)
            .expect("Window should be valid if loaded from switch_window")
            .window
            .minimize();
    }

    /// restore current window
    pub fn restore_window(&mut self) {
        self.windows
            .get_mut(&self.current_window)
            .expect("Window should be valid if loaded from switch_window")
            .window
            .restore();
    }

    /// set the title of the current window
    pub fn set_window_title(&mut self, title: &str) {
        self.windows
            .get_mut(&self.current_window)
            .expect("Window should be valid if loaded from switch_window")
            .window
            .set_title(title)
            .expect("Title should be valid. Does it contain invalid text?")
    }

    /// set the position of the current window
    pub fn set_window_position(&mut self, x: i32, y: i32) {
        self.windows
            .get_mut(&self.current_window)
            .expect("Window should be valid if loaded from switch_window")
            .window
            .set_position(
                video::WindowPos::Positioned(x),
                video::WindowPos::Positioned(y),
            );
    }

    /// get the size of the current window
    pub fn get_window_size(&mut self) -> (u32, u32) {
        self.windows
            .get_mut(&self.current_window)
            .expect("Window should be valid if loaded from switch_window")
            .window
            .size()
    }

    /// set the size of the current window
    pub fn set_window_size(&mut self, width: u32, height: u32) {
        self.windows
            .get_mut(&self.current_window)
            .expect("Window should be valid if loaded from switch_window")
            .window
            .set_size(width, height)
            .expect("Size should be valid. Are any parameters 0?")
    }

    /// get the screen width
    pub fn get_screen_width(&self) -> i32 {
        self.windows
            .get(&self.current_window)
            .expect("Window should be valid if loaded from switch_window")
            .window
            .display_mode()
            .expect("There should be a valid display mode")
            .w
    }

    /// get the screen height
    pub fn get_screen_height(&self) -> i32 {
        self.windows
            .get(&self.current_window)
            .expect("Window should be valid if loaded from switch_window")
            .window
            .display_mode()
            .expect("There should be a valid display mode")
            .h
    }

    /// get the current window x
    pub fn get_window_x(&self) -> i32 {
        self.windows
            .get(&self.current_window)
            .expect("Window should be valid if loaded from switch_window")
            .window
            .position()
            .0
    }

    /// get the current window y
    pub fn get_window_y(&self) -> i32 {
        self.windows
            .get(&self.current_window)
            .expect("Window should be valid if loaded from switch_window")
            .window
            .position()
            .1
    }

    /// get the clipboard text
    pub fn get_clipboard_text(&self) -> Option<String> {
        match self.clipboard.clipboard_text() {
            Ok(text) => Some(text),
            Err(_) => None,
        }
    }

    /// set the clipboard text
    pub fn set_clipboard_text(&mut self, text: &str) {
        self.clipboard
            .set_clipboard_text(text)
            .expect("Clipboard should be valid");
    }

    /// show the cursor
    pub fn show_cursor(&mut self) {
        self.mouse.show_cursor(true);
    }

    /// hide the cursor
    pub fn hide_cursor(&mut self) {
        self.mouse.show_cursor(false);
    }

    /// check if the cursor is hidden
    pub fn cursor_hidden(&mut self) -> bool {
        self.mouse.is_cursor_showing()
    }

    /// clear the background of the current window
    pub fn clear_background(&mut self, color: Color) {
        self.windows
            .get_mut(&self.current_window)
            .expect("Window should be valid if loaded from switch_window")
            .canvas
            .set_draw_color(pixels::Color::from(color));
        self.windows
            .get_mut(&self.current_window)
            .expect("Window should be valid if loaded from switch_window")
            .canvas
            .clear();
    }

    /// begin drawing
    pub fn begin_drawing(&mut self) {
        for window in self.windows.values_mut() {
            window.previous_time = window.current_time;
            window.previous_time = self.timer.ticks64() as f64 / 1000f64;
        }
        for event in self.event_pump.poll_iter() {
            match event {
                Event::Window {
                    timestamp: _,
                    window_id,
                    win_event,
                } => match win_event {
                    WindowEvent::Close => {
                        self.windows
                            .get_mut(&window_id)
                            .expect("Window should exist if SDL passes it to us")
                            .should_close = true;
                    }
                    WindowEvent::FocusLost => {
                        self.windows
                            .get_mut(&window_id)
                            .expect("Window should exist if SDL passes it to us")
                            .focused = false;
                    }
                    WindowEvent::FocusGained => {
                        self.windows
                            .get_mut(&window_id)
                            .expect("Window should exist if SDL passes it to us")
                            .focused = true;
                    }
                    _ => {}
                },
                Event::AppTerminating { timestamp: _ } => {
                    // Just die peacefully
                    #[allow(clippy::empty_loop)]
                    loop {}
                }
                _ => {}
            }
        }
        self.wait(self.frame_time as f64);
    }

    /// end drawing
    pub fn end_drawing(&mut self) {
        for window in self.windows.values_mut() {
            window.canvas.present();
        }
    }

    /// switch window
    pub fn switch_window(&mut self, id: u32) {
        self.current_window = id;
    }

    /// get the time since opening sdl
    pub fn get_time(&self) -> f64 {
        self.windows
            .get(&self.current_window)
            .expect("Window should be valid if loaded from switch_window")
            .current_time
    }

    /// wait an amount of time
    pub fn wait(&mut self, time: f64) {
        let start: f64 = self.get_time();
        let mut current: f64 = self.get_time();

        while current < start + time {
            self.windows
                .get_mut(&self.current_window)
                .expect("Window should be valid if loaded from switch_window")
                .previous_time = self
                .windows
                .get(&self.current_window)
                .expect("Window should be valid if loaded from switch_window")
                .current_time;
            self.windows
                .get_mut(&self.current_window)
                .expect("Window should be valid if loaded from switch_window")
                .current_time = self.timer.ticks64() as f64 / 1000f64;
            current = self.get_time();
        }
    }

    /// get a random i32
    pub fn get_random_i32(min: i32, max: i32) -> i32 {
        let mut rand = rng();
        rand.random_range(min..max)
    }

    /// get a random i64
    pub fn get_random_i64(min: i64, max: i64) -> i64 {
        let mut rand = rng();
        rand.random_range(min..max)
    }

    /// get a random f64
    pub fn get_random_f64(min: f64, max: f64) -> f64 {
        let mut rand = rng();
        rand.random_range(min..max)
    }


    /// open a URL in the browser
    pub fn open_url(&mut self, url: &str) {
        open::that(url).expect("Should be able to open URL");
    }

    /// checks if key is pressed
    pub fn key_pressed(&mut self, key: Scancode) -> bool {
        self.event_pump.keyboard_state().is_scancode_pressed(key)
    }

    /// checks if mouse button is pressed
    pub fn mouse_button_pressed(&mut self, button: MouseButton) -> bool {
        self.event_pump
            .mouse_state()
            .is_mouse_button_pressed(button)
    }

    /// get the mouse x
    pub fn get_mouse_x(&self) -> i32 {
        self.event_pump.mouse_state().x()
    }

    /// get the mouse y
    pub fn get_mouse_y(&self) -> i32 {
        self.event_pump.mouse_state().y()
    }
}