maylib 0.3.0

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
use crate::types;
use crate::types::Maylib;
use rand::{rng, Rng};
use sdl2::event::{Event, WindowEvent};
use sdl2::render::Canvas;
use sdl2::video::FullscreenType;
use sdl2::{pixels, video};
use std::sync::Mutex;
use lazy_static::lazy_static;

lazy_static! {
    pub static ref MAYLIB: Mutex<Maylib> = {
        let mutex = Mutex::new(Maylib::init().expect("Can't init Maylib"));
        mutex.clear_poison();
        mutex
    };
}

pub fn init_maylib() {
    lazy_static::initialize(&MAYLIB);
}

pub fn init_window(title: &str, width: u32, height: u32) -> Result<u32, String> {
    let mut get = MAYLIB.lock().expect("Should be able to lock");
    let winctx: video::Window = match get
        .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 = get.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 = types::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,
    };
    get.windows.insert(id, window);
    Ok(id)
}

pub fn close_window() {
    let mut get = MAYLIB.lock().expect("Should be able to lock");
    let current_window = get.current_window;
    let window = get.windows.remove(&current_window);
    drop(window);
}

pub fn window_should_close() -> bool {
    let get = MAYLIB.lock().expect("Should be able to lock");
    get.windows
        .get(&get.current_window)
        .expect("Window should be valid if loaded from switch_window")
        .should_close
}

pub fn set_frame_rate(rate: i32) {
    let mut get = MAYLIB.lock().expect("Should be able to lock");
    get.frame_rate = rate;
    get.frame_time = 1.0 / rate as f32;
}

pub fn all_windows_closed() -> bool {
    let get = MAYLIB.lock().expect("Should be able to lock");
    for win in get.windows.iter() {
        if win.1.ready {
            return false;
        }
    }
    true
}

pub fn is_window_ready() -> bool {
    let get = MAYLIB.lock().expect("Should be able to lock");
    get.windows
        .get(&get.current_window)
        .expect("Window should be valid if loaded from switch_window")
        .ready
}

pub fn is_window_fullscreen() -> bool {
    let get = MAYLIB.lock().expect("Should be able to lock");
    get.windows
        .get(&get.current_window)
        .expect("Window should be valid if loaded from switch_window")
        .fullscreen
}

pub fn is_window_hidden() -> bool {
    let get = MAYLIB.lock().expect("Should be able to lock");
    get.windows
        .get(&get.current_window)
        .expect("Window should be valid if loaded from switch_window")
        .hidden
}

pub fn is_window_minimized() -> bool {
    let get = MAYLIB.lock().expect("Should be able to lock");
    get.windows
        .get(&get.current_window)
        .expect("Window should be valid if loaded from switch_window")
        .minimized
}

pub fn is_window_maximized() -> bool {
    let get = MAYLIB.lock().expect("Should be able to lock");
    get.windows
        .get(&get.current_window)
        .expect("Window should be valid if loaded from switch_window")
        .maximized
}

pub fn is_window_focused() -> bool {
    let get = MAYLIB.lock().expect("Should be able to lock");
    get.windows
        .get(&get.current_window)
        .expect("Window should be valid if loaded from switch_window")
        .focused
}

pub fn is_window_resized() -> bool {
    let get = MAYLIB.lock().expect("Should be able to lock");
    get.windows
        .get(&get.current_window)
        .expect("Window should be valid if loaded from switch_window")
        .resized
}

pub fn toggle_fullscreen() {
    let mut get = MAYLIB.lock().expect("Should be able to lock");
    let current_window = get.current_window;
    let window = get.windows.get_mut(&current_window);
    match window {
        Some(w) => {
            if w.fullscreen {
                w.window
                    .set_fullscreen(FullscreenType::Off)
                    .expect("Window should exit fullscreen");
            } else {
                w.window
                    .set_fullscreen(FullscreenType::True)
                    .expect("Window should enter fullscreen");
            }
            w.fullscreen = !w.fullscreen;
        }
        None => { 
            panic!("Window should be valid if loaded from switch_window")
        }
    }
}

pub fn toggle_borderless_windowed() {
    let mut get = MAYLIB.lock().expect("Should be able to lock");
    let current_window = get.current_window;
    match get.windows.get_mut(&current_window) {
        Some(w) => {
            w.bordered = !w.bordered;
        }
        None => {
            panic!("Window should be valid if loaded from switch_window");
        }
    }
    let window = get
        .windows
        .get_mut(&current_window)
        .expect("Window should be valid if loaded from switch_window");
    if window.bordered {
        window.window.set_bordered(false);
    } else {
        window.window.set_bordered(true);
    }
}

pub fn maximize_window() {
    let mut get = MAYLIB.lock().expect("Should be able to lock");
    let current_window = get.current_window;
    let window = get.windows.get_mut(&current_window).expect("Window should exist if loaded from switch_window");
    window.maximized = true;
    window.window.maximize();
}

pub fn minimize_window() {
    let mut get = MAYLIB.lock().expect("Should be able to lock");
    let current_window = get.current_window;
    let window = get.windows.get_mut(&current_window).expect("Window should exist if loaded from switch_window");
    window.minimized = true;
    window.window.minimize();
}

pub fn restore_window() {
    let mut get = MAYLIB.lock().expect("Should be able to lock");
    let current_window = get.current_window;
    let window = get.windows.get_mut(&current_window).expect("Window should exist if loaded from switch_window");
    window.window.restore();
}

pub fn set_window_title(title: &str) {
    let mut get = MAYLIB.lock().expect("Should be able to lock");
    let current_window = get.current_window;
    get.windows
        .get_mut(&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?")
}

pub fn set_window_position(x: i32, y: i32) {
    let mut get = MAYLIB.lock().expect("Should be able to lock");
    let current_window = get.current_window;
    get.windows
        .get_mut(&current_window)
        .expect("Window should be valid if loaded from switch_window")
        .window
        .set_position(
            video::WindowPos::Positioned(x),
            video::WindowPos::Positioned(y),
        );
}

pub fn get_window_size() -> (u32, u32) {
    let mut get = MAYLIB.lock().expect("Should be able to lock");
    let current_window = get.current_window;
    get.windows
        .get_mut(&current_window)
        .expect("Window should be valid if loaded from switch_window")
        .window
        .size()
}

pub fn set_window_size(width: u32, height: u32) {
    let mut get = MAYLIB.lock().expect("Should be able to lock");
    let current_window = get.current_window;
    get.windows
        .get_mut(&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?")
}

pub fn get_screen_width() -> i32 {
    let get = MAYLIB.lock().expect("Should be able to lock");
    get.windows
        .get(&get.current_window)
        .expect("Window should be valid if loaded from switch_window")
        .window
        .display_mode()
        .expect("There should be a valid display mode")
        .w
}

pub fn get_screen_height() -> i32 {
    let get = MAYLIB.lock().expect("Should be able to lock");
    get.windows
        .get(&get.current_window)
        .expect("Window should be valid if loaded from switch_window")
        .window
        .display_mode()
        .expect("There should be a valid display mode")
        .h
}

pub fn get_window_x() -> i32 {
    let get = MAYLIB.lock().expect("Should be able to lock");
    get.windows
        .get(&get.current_window)
        .expect("Window should be valid if loaded from switch_window")
        .window
        .position()
        .0
}

pub fn get_window_y() -> i32 {
    let get = MAYLIB.lock().expect("Should be able to lock");
    get.windows
        .get(&get.current_window)
        .expect("Window should be valid if loaded from switch_window")
        .window
        .position()
        .1
}

pub fn get_clipboard_text() -> Option<String> {
    let get = MAYLIB.lock().expect("Should be able to lock");
    Some(get.clipboard.clipboard_text().expect("TEXT SHOULD EXIST"))
}

pub fn set_clipboard_text(text: &str) {
    let get = MAYLIB.lock().expect("Should be able to lock");
    get.clipboard
        .set_clipboard_text(text)
        .expect("Clipboard should be valid");
}

pub fn show_cursor() {
    let get = MAYLIB.lock().expect("Should be able to lock");
    get.mouse.show_cursor(true);
}

/// hide the cursor
pub fn hide_cursor() {
    let get = MAYLIB.lock().expect("Should be able to lock");
    get.mouse.show_cursor(false);
}

/// check if the cursor is hidden
pub fn cursor_hidden() -> bool {
    let get = MAYLIB.lock().expect("Should be able to lock");
    get.mouse.is_cursor_showing()
}

/// clear the background of the current window
pub fn clear_background(color: types::Color) {
    let mut get = MAYLIB.lock().expect("Should be able to lock");
    let current_window = get.current_window;
    get.windows
        .get_mut(&current_window)
        .expect("Window should be valid if loaded from switch_window")
        .canvas
        .set_draw_color(pixels::Color::from(color));
    get.windows
        .get_mut(&current_window)
        .expect("Window should be valid if loaded from switch_window")
        .canvas
        .clear();
}

pub fn begin_drawing() {
    let frame_time;
    {
        let mut get = MAYLIB.lock().expect("Should be able to lock");
        let time = get.timer.ticks64() as f64 / 1000f64;
        let keys: Vec<u32> = get.windows.keys().copied().collect();
        let windows= &mut get.windows;
        for key in keys {
            let current_time = windows.get_mut(&key).expect("Window should exist if in keys").current_time;
            windows.get_mut(&key).expect("Window should exist if in keys").previous_time = current_time;
            windows.get_mut(&key).expect("Window should exist if in keys").current_time = time;
        }
        let events: Vec<_> = get.event_pump.poll_iter().collect();
        let windows = &mut get.windows;
        for event in events {
            match event {
                Event::Window {
                    timestamp: _,
                    window_id,
                    win_event,
                } => match win_event {
                    WindowEvent::Close => {
                        windows
                            .get_mut(&window_id)
                            .expect("Window should exist if SDL passes it to us")
                            .should_close = true;
                    }
                    WindowEvent::FocusLost => {
                        windows
                            .get_mut(&window_id)
                            .expect("Window should exist if SDL passes it to us")
                            .focused = false;
                    }
                    WindowEvent::FocusGained => {
                        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 {}
                }
                _ => {}
            }
        }
        frame_time = get.frame_time;
    }
    wait(frame_time as f64);
}

/// end drawing
pub fn end_drawing() {
    let mut get = MAYLIB.lock().expect("Should be able to lock");
    for window in get.windows.values_mut() {
        window.canvas.present();
    }
}

/// switch window
pub fn switch_window(id: u32) {
    let mut get = MAYLIB.lock().expect("Should be able to lock");
    get.current_window = id;
}

/// get the time since opening sdl
pub fn get_time() -> f64 {
    let get = MAYLIB.lock().expect("Should be able to lock");
    get.windows
        .get(&get.current_window)
        .expect("Window should be valid if loaded from switch_window")
        .current_time
}

/// wait an amount of time
pub fn wait(time: f64) {
    let mut get = MAYLIB.lock().expect("Should be able to lock");
    let current_window = get.current_window;
    let start: f64 = get.timer.ticks64() as f64 / 1000f64;
    let mut current: f64 = get.timer.ticks64() as f64 / 1000f64;

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

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

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

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

pub fn open_url(url: &str) {
    open::that(url).expect("Should be able to open URL");
}

//TODO: Move to custom Scancode struct
pub fn key_pressed(key: types::Keycode) -> bool {
    let get = MAYLIB.lock().expect("Should be able to lock");
    get.event_pump
        .keyboard_state()
        .is_scancode_pressed(key.into())
}

pub fn mouse_button_pressed(button: types::MouseButton) -> bool {
    let get = MAYLIB.lock().expect("Should be able to lock");
    get.event_pump
        .mouse_state()
        .is_mouse_button_pressed(sdl2::mouse::MouseButton::from(button))
}

/// get the mouse x
pub fn get_mouse_x() -> i32 {
    let get = MAYLIB.lock().expect("Should be able to lock");
    get.event_pump.mouse_state().x()
}

/// get the mouse y
pub fn get_mouse_y() -> i32 {
    let get = MAYLIB.lock().expect("Should be able to lock");
    get.event_pump.mouse_state().y()
}