gluie 0.2.0

A 2d easy to use graphics engine for application development (GPU-accelerated)
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
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
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
use glfw::{Action, Context, MouseButton};
use std::collections::HashSet;

//used for shader processing
mod shader;
use shader as sdr;

//use for texture managment 
mod texture;

//used for things we are actualy using
mod objects;
use objects as obj;

//type of objects we will have
pub type Rec = obj::Rec;
pub type Img = obj::Image;
pub type Text = obj::Text;

//types that will be used for io managing
pub type KeyBoard = glfw::Key;
pub type Font = freetype::Face;

//main structure for this project
pub struct Gluie {
    pub window: glfw::PWindow,
    pub events: glfw::GlfwReceiver<(f64, glfw::WindowEvent)>,
    pub glfw_init: glfw::Glfw,
    event_buffer: Vec<glfw::WindowEvent>,

    //shaders
    pub shape_shader: Option<u32>,
    pub image_shader: Option<u32>,
    pub font_shader: Option<u32>,

    //recs uniform
    pub rec_uniform_window_size: i32,
    pub rec_uniform_color: i32,
    pub rec_uniform_cord: i32,

    //image uniform
    pub image_uniform_window_size: i32,
    pub image_uniform_img: i32,
    pub image_uniform_cord: i32,

    //font uniform
    pub font_uniform_window_size: i32,
    pub font_uniform_color: i32,
    pub font_uniform_cord: i32,

    //lib
    lib: freetype::Library,

    //io
    input: std::rc::Rc<std::cell::RefCell<String>>,

    //font meta data
    pub font_size: u32,
    pub font_texture_atlas: u32,

    //global variable
    called_draw_rec: bool,
    called_draw_img: bool,
    called_draw_text: bool,

    have_rec_shader: bool,
    have_img_shader: bool,
    have_text_shader: bool,
}

//implimentation for this
impl Gluie {
    ///init for everything like window create and init variable and load openGL
    pub fn new(size: (u32, u32), name: &str) -> Gluie {
        let mut glfw_init = glfw::init(glfw::fail_on_errors).unwrap();

        let (mut window, events) = glfw_init
            .create_window(size.0, size.1, name, glfw::WindowMode::Windowed)
            .unwrap();

        window.make_current();

        glfw_init.set_swap_interval(glfw::SwapInterval::None);

        window.set_key_polling(true);

        gl::load_with(|symbol| {
            window
                .get_proc_address(symbol)
                .map_or(std::ptr::null(), |p| p as *const _)
        });

        unsafe {
            gl::Enable(gl::BLEND);
            gl::BlendFunc(gl::SRC_ALPHA, gl::ONE_MINUS_SRC_ALPHA);
        }

        Gluie {
            window: window,
            events: events,
            event_buffer: Vec::new(),
            glfw_init: glfw_init,

            shape_shader: None,
            image_shader: None,
            font_shader: None,

            rec_uniform_window_size: 0,
            rec_uniform_color: 0,
            rec_uniform_cord: 0,

            image_uniform_window_size: 0,
            image_uniform_img: 0,
            image_uniform_cord: 0,

            font_uniform_window_size: 0,
            font_uniform_color: 0,
            font_uniform_cord: 0,

            lib: freetype::Library::init().unwrap(),

            input: std::rc::Rc::new(std::cell::RefCell::new(String::new())),

            font_size: 0,
            font_texture_atlas: 0,

            called_draw_rec: false,
            called_draw_img: false,
            called_draw_text: false,
            have_rec_shader: false,
            have_img_shader: false,
            have_text_shader: false,
        }
    }

    /* .---------------------------------.
     * | ======= Window managing ======= |
     * *---------------------------------*
     */
    ///undate screen
    pub fn show(&mut self) {
        self.window.swap_buffers();
    }

    ///if the exit button is clicked
    pub fn click_exit(&self) -> bool {
        for event in &self.event_buffer {
            match event {
                glfw::WindowEvent::Close => return true,
                _ => {}
            }
        }
        false
    }

    ///if the window should close
    pub fn should_close(&self) -> bool{
        self.window.should_close()
    }

    ///set to the window should close
    pub fn set_should_close(&mut self){
        self.window.set_should_close(true);
    }

    ///check if window size changed
    pub fn resized(&self) -> bool {
        for event in &self.event_buffer {
            match event {
                glfw::WindowEvent::Size(_, _) => return false,
                _ => {}
            }
        }
        true
    }

    ///flush events so we can get window input
    pub fn flush_event(&mut self) {
        self.event_buffer.clear();
        self.glfw_init.poll_events();
        for (_, event) in glfw::flush_messages(&self.events) {
            self.event_buffer.push(event);
        }
    }

    ///set a color for the screen
    pub fn set_screen_color(&self, r: u8, g: u8, b: u8) {
        unsafe {
            gl::ClearColor(
                (r as f32) / 255.0,
                (g as f32) / 255.0,
                (b as f32) / 255.0,
                1.0,
            );
            gl::Clear(gl::COLOR_BUFFER_BIT);
        };
    }

    ///return window's size
    pub fn window_size(&self) -> (u32, u32) {
        let (x, y) = self.window.get_size();
        (x as u32, y as u32)
    }

    ///set window's size into an other size
    pub fn set_window_size(&mut self, w: u32, h: u32) {
        self.window.set_size(w as i32, h as i32);
    }

    ///update viewport (used for resizing)
    pub fn update_view(&mut self) {
        if self.resized() {
            let (x, y) = self.window_size();
            unsafe { gl::Viewport(0, 0, x as i32, y as i32) };

            unsafe {
                match self.shape_shader {
                    Some(d) => {
                        gl::UseProgram(d);
                        gl::Uniform2f(self.rec_uniform_window_size, x as f32, y as f32);
                    }
                    None => {}
                }

                match self.image_shader {
                    Some(d) => {
                        gl::UseProgram(d);
                        gl::Uniform2f(self.image_uniform_window_size, x as f32, y as f32);
                    }
                    None => {}
                }

                match self.font_shader {
                    Some(d) => {
                        gl::UseProgram(d);
                        gl::Uniform2f(self.font_uniform_window_size, x as f32, y as f32);
                    }
                    None => {}
                }
            };
        }
    }

    /* .------------------------------.
     * | ======= Mouse inputs ======= |
     * *------------------------------*
     */
    ///get mouse's pos
    pub fn mouse_cord(&self) -> (u32, u32) {
        let (x, y) = self.window.get_cursor_pos();
        (x as u32, y as u32)
    }

    ///check if a button is pressed support up to 1-8 buttons
    pub fn mouse_button(&self, button: u8) -> bool {
        let button = match button {
            1 => MouseButton::Button1,
            2 => MouseButton::Button2,
            3 => MouseButton::Button3,
            4 => MouseButton::Button4,
            5 => MouseButton::Button5,
            6 => MouseButton::Button6,
            7 => MouseButton::Button7,
            8 => MouseButton::Button8,
            _ => return false,
        };
        self.window.get_mouse_button(button) == Action::Press
    }

    ///check scroll direction
    pub fn mouse_scroll(&self, x: &mut f32, y: &mut f32) {
        for event in &self.event_buffer {
            match event {
                glfw::WindowEvent::Scroll(xoff, yoff) => {
                    *x = *xoff as f32;
                    *y = *yoff as f32;
                    return;
                }
                _ => {}
            }
        }
        *x = 0.0;
        *y = 0.0;
    }
    
    /* .---------------------------------.
     * | ======= Keyboard inputs ======= |
     * *---------------------------------*
     */
    ///get keyboard input down when just click down
    pub fn key_down(&self, keys: &mut HashSet<KeyBoard>) {
        keys.clear();
        for event in &self.event_buffer {
            if let glfw::WindowEvent::Key(key, _, glfw::Action::Press, _) = *event {
                keys.insert(key as KeyBoard);
            }
        }
    }

    ///get keyboard input down and repeat
    pub fn key_repeat(&self, keys: &mut HashSet<KeyBoard>) {
        keys.clear();
        for event in &self.event_buffer {
            if let glfw::WindowEvent::Key(key, _, glfw::Action::Repeat, _) = *event {
                keys.insert(key as KeyBoard);
            }
        }
    }

    ///get keyboard input down and floud
    pub fn key_floud(&self, keys: &mut HashSet<KeyBoard>) {
        for event in &self.event_buffer {
            if let glfw::WindowEvent::Key(key, _, glfw::Action::Press, _) = *event {
                keys.insert(key as KeyBoard);
            }
        }
    }

    ///get keyboard input down and user repeat
    pub fn key_user(&self, keys: &mut HashSet<KeyBoard>) {
        keys.clear();
        for event in &self.event_buffer {
            if let glfw::WindowEvent::Key(key, _, glfw::Action::Press | glfw::Action::Repeat, _) =
                *event
            {
                keys.insert(key as KeyBoard);
            }
        }
    }

    /// init keyboard input as chars
    pub fn key_char_init(&mut self) {
        let input_clone = std::rc::Rc::clone(&self.input);
        self.window.set_char_callback(move |_, ch| {
            input_clone.borrow_mut().push(ch);
        });
    }

    ///get keyboard input as chars
    pub fn key_char(&self) -> String {
        let data = self.input.borrow().to_string();
        self.input.borrow_mut().clear();
        data
    }

    /* .-------------------------------------------------.
     * | ======= shader and actual gpu things :D ======= |
     * *-------------------------------------------------*
     */

    /*
     *
     * <---- Rec ---->
     *
     */
    /// loads the shader for shapes return a None of success String if error
    pub fn link_rec_shader(&mut self, vertex_source: &str, fragment_source: &str) -> Option<String> {
        self.shape_shader = match sdr::create_program(vertex_source, fragment_source) {
            Ok(data) => Some(data),
            Err(e) => return Some(e),
        };

        self.rec_uniform_window_size = unsafe {
            gl::GetUniformLocation(self.shape_shader.unwrap_unchecked(), c"winsize".as_ptr())
        };

        self.rec_uniform_color = unsafe {
            gl::GetUniformLocation(self.shape_shader.unwrap_unchecked(), c"color".as_ptr())
        };

        self.rec_uniform_cord = unsafe {
            gl::GetUniformLocation(self.shape_shader.unwrap_unchecked(), c"cord".as_ptr())
        };

        self.have_rec_shader = true;

        None
    }

    ///create a rectangle object
    pub fn build_rec(&self, x: u32, y: u32, w: u32, h: u32, r: u8, g: u8, b: u8) -> Rec {
        let mut rec = obj::Rec::new(x, y, w, h, r, g, b);
        rec.create_struct();
        rec
    }

    ///change the size of the rec
    pub fn change_rec_size(&self, w: u32, h: u32, rec: &mut Rec) {
        rec.change_size(w, h);
    }

    //change the color of the rec
    pub fn change_rec_color(&self, r: u8, g: u8, b: u8, rec: &mut Rec){
        rec.change_color(r, g, b);
    }

    ///change rec's cord
    pub fn change_rec_cords(&self, x: u32, y: u32, rec: &mut Rec){
        rec.change_cord(x, y);
    }

    ///drawing rec shape
    pub fn draw_rec(&mut self, object: &Rec) {
        if !self.have_rec_shader{
            return;
        }

        unsafe {
            if !self.called_draw_rec{
                gl::UseProgram(self.shape_shader.unwrap());
                self.called_draw_img = false;
                self.called_draw_text = false;
                self.called_draw_rec = true;
            }

            gl::Uniform3ui(
                self.rec_uniform_color,
                object.mr as u32,
                object.mg as u32,
                object.mb as u32,
            );

            gl::Uniform2ui(
                self.rec_uniform_cord,
                object.mx,
                object.my
            );

            gl::BindVertexArray(object.vao);
            gl::DrawArrays(gl::TRIANGLE_STRIP, 0, 4);
        }
    }

    ///mouse hover rec
    pub fn mouse_hover_rec(&self, rec: &Rec) -> bool {
        let (x, y) = self.mouse_cord();
        if rec.mx <= x && rec.mx + rec.mw >= x && rec.my <= y && rec.my + rec.mh >= y {
            return true;
        }
        false
    }

    ///mouse click on rec with any button
    pub fn mouse_click_rec(&self, rec: &Rec, button: u8) -> bool {
        if self.mouse_hover_rec(rec) && self.mouse_button(button) {
            return true;
        }
        false
    }

    /*
     *
     * <---- Image ---->
     *
     */
    /// loads the shader for image return a None of success String if error
    pub fn link_img_shader(&mut self, vertex_source: &str, fragment_source: &str) -> Option<String> {
        self.image_shader = match sdr::create_program(vertex_source, fragment_source) {
            Ok(data) => Some(data),
            Err(e) => return Some(e),
        };

        self.image_uniform_window_size = unsafe {
            gl::GetUniformLocation(self.image_shader.unwrap_unchecked(), c"winsize".as_ptr())
        };

        self.image_uniform_img = unsafe {
            gl::GetUniformLocation(self.image_shader.unwrap_unchecked(), c"image".as_ptr())
        };

        unsafe { gl::Uniform1i(self.image_uniform_img, 0); }
        
        self.image_uniform_cord = unsafe {
            gl::GetUniformLocation(self.image_shader.unwrap_unchecked(), c"cord".as_ptr())
        };

        self.have_img_shader = true;
        
        None
    }

    ///create a image object
    pub fn build_img(
        &self,
        w: u32,
        h: u32,
        x: u32,
        y: u32,
        image: (Vec<u8>, u32, u32),
    ) -> Img {
        let mut img = Img::new(w, h, x, y, image);
        img.create_struct();
        img
    }

    ///change teh size of the img
    pub fn change_img_size(&self, w: u32, h: u32, img: &mut Img){
        img.change_size(w, h);
    }

    ///change image's image
    pub fn change_img_img(&self, image: (Vec<u8>, u32, u32), img: &mut Img){
        img.change_img(image);
    }
    
    ///change image's cord
    pub fn change_img_cords(&self, x: u32, y: u32, img: &mut Img){
        img.change_cord(x, y);
    }

    ///drawing img
    pub fn draw_img(&mut self, object: &Img) {
        if !self.have_img_shader{
            return;
        }

        unsafe {
            if !self.called_draw_img{
                gl::UseProgram(self.image_shader.unwrap_unchecked());
                self.called_draw_img = true;
                self.called_draw_text = false;
                self.called_draw_rec = false;
            }

            gl::ActiveTexture(gl::TEXTURE0);
            gl::BindTexture(gl::TEXTURE_2D, object.image);
            
            gl::Uniform2ui(
                self.image_uniform_cord,
                object.mx,
                object.my
            );

            gl::BindVertexArray(object.vao);
            gl::DrawArrays(gl::TRIANGLE_STRIP, 0, 4);
        }
    }

    //io with image
    ///mouse hover image
    pub fn mouse_hover_img(&self, img: &Img) -> bool {
        let (x, y) = self.mouse_cord();
        if img.mx <= x && img.mx + img.mw >= x && img.my <= y && img.my + img.mh >= y {
            return true;
        }
        false
    }

    ///mouse click on image with any button
    pub fn mouse_click_img(&self, img: &Img, button: u8) -> bool {
        if self.mouse_hover_img(img) && self.mouse_button(button) {
            return true;
        }
        false
    }
    
    /*
     *
     * <---- Text ---->
     *
     */
    /// loads the shader for image return a None of success String if error
    pub fn link_font_shader(&mut self, vertex_source: &str, fragment_source: &str) -> Option<String> {
        self.font_shader = match sdr::create_program(vertex_source, fragment_source) {
            Ok(data) => Some(data),
            Err(e) => return Some(e),
        };
        
        unsafe { gl::UseProgram(self.font_shader.unwrap_unchecked()) };

        self.font_uniform_window_size = unsafe {
            gl::GetUniformLocation(self.font_shader.unwrap_unchecked(), c"winsize".as_ptr())
        };

        self.font_uniform_color = unsafe {
            gl::GetUniformLocation(self.font_shader.unwrap_unchecked(), c"color".as_ptr())
        };
        
        self.font_uniform_cord = unsafe {
            gl::GetUniformLocation(self.font_shader.unwrap_unchecked(), c"cord".as_ptr())
        };

        let font_uniform_atlas = unsafe {
            gl::GetUniformLocation(self.font_shader.unwrap_unchecked(), c"atlas".as_ptr())
        };

        self.have_text_shader = true;
        
        texture::gen_texture(&mut self.font_texture_atlas);

        unsafe{
            gl::TexImage2D(
                gl::TEXTURE_2D, 
                0, 
                gl::R8 as i32, 
                2048, 
                2048, 
                0, 
                gl::RED, 
                gl::UNSIGNED_BYTE, 
                std::ptr::null(),
            );
            gl::BindTexture(gl::TEXTURE_2D, self.font_texture_atlas);

            gl::TexParameteri(
                gl::TEXTURE_2D,
                gl::TEXTURE_MIN_FILTER,
                gl::NEAREST as i32,
            );

            gl::TexParameteri(
                gl::TEXTURE_2D,
                gl::TEXTURE_MAG_FILTER,
                gl::NEAREST as i32,
            );
        }
        
        unsafe { gl::Uniform1i(font_uniform_atlas, 0); }

        None
    }

    ///create a Font object
    pub fn load_font(&self, font_path: &str, size: u32) -> Font {
        let face = self.lib.new_face(font_path, 0).unwrap();
        face.set_pixel_sizes(0, size).unwrap();
        face
    }

    ///load in characters to texture text
    pub fn build_char(&self, ch: char, font: &Font, x: u32, y: u32, r: u8, g: u8, b: u8) -> Text {
        font.load_char(ch as usize, freetype::face::LoadFlag::RENDER)
            .unwrap();

        let glyph = font.glyph();
        let bitmap = glyph.bitmap();

        let aw = (glyph.advance().x >> 6) as u32;
        let ah = (glyph.advance().y >> 6) as u32;
        let w = bitmap.width() as usize;
        let h = bitmap.rows() as usize;
        let pitch = bitmap.pitch();
        let stride = pitch.unsigned_abs() as usize;
        let buffer = bitmap.buffer();

        let mut pixels = vec![0u8; w * h];

        for y in 0..h {
            let src_y = if pitch >= 0 { y } else { h - 1 - y };
            let src = src_y * stride;

            pixels[y * w..(y + 1) * w].copy_from_slice(&buffer[src..src + w]);
        }

        let left = glyph.bitmap_left();
        let size_metric = font.size_metrics();
        let (mut text, uv1, uv2) = Text::new(
            w as u32,
            h as u32,
            aw as u32,
            ah as u32,
            (x as i32 + left).max(0) as u32,
            (y as i32 + (size_metric.unwrap().ascender >> 6) as i32 - glyph.bitmap_top()) as u32,
            &pixels,
            r,
            g,
            b,
            ch,
            self.font_texture_atlas,
            (size_metric.unwrap().height >> 6) as u32 
        );

        text.create_struct((uv1, uv2));
        text
    }

    ///Get the char size without building the characters (way faster than build then get)
    pub fn get_char_size(&self, ch: char, font: &Font) -> (u32, u32){
        font.load_char(ch as usize, freetype::face::LoadFlag::RENDER)
            .unwrap();

        let glyph = font.glyph();

        ((glyph.advance().x >> 6) as u32, (glyph.advance().y >> 6) as u32)
    }

    ///change the color of text
    pub fn change_text_color(&self, r: u8, g: u8, b: u8, text: &mut Text){
        text.change_color(r, g, b);
    }

    ///change the position of the text
    pub fn change_text_cord(&self, x: u32, y: u32, text: &mut Text){
        text.chage_cord(x, y);
    }

    ///drawing rec shape
    pub fn draw_char(&mut self, object: &Text) {
        if !self.have_text_shader{
            return;
        }

        unsafe {
            if !self.called_draw_text{
                gl::UseProgram(self.font_shader.unwrap());
                self.called_draw_img = false;
                self.called_draw_text = true;
                self.called_draw_rec = false;

            }

            gl::Uniform3ui(
                self.font_uniform_color,
                object.mr as u32,
                object.mg as u32,
                object.mb as u32,
            );

            gl::ActiveTexture(gl::TEXTURE0);
            gl::BindTexture(gl::TEXTURE_2D, self.font_texture_atlas);

            gl::Uniform2ui(
                self.font_uniform_cord,
                object.mx,
                object.my
            );

            gl::BindVertexArray(object.vao);
            gl::DrawArrays(gl::TRIANGLE_STRIP, 0, 4);
        }
    }
}
///get text size if you have the Text value (way faster than the before build one)
pub fn get_text_size(ch: &Text) -> (u32, u32) {
    (ch.maw, ch.mah)
}

///load image data
pub fn load_image(path: &str) -> Result<(Vec<u8>, u32, u32), String> {
    let image = match image::open(path) {
        Ok(data) => data,
        Err(err) => return Err(err.to_string()),
    }
    .to_rgba8();

    Ok((image.clone().into_raw(), image.width(), image.height()))
}

///generate the basic vertex shader for shape that GLUIE need
///
///#Example
///
///```
///fn main(){
///    let vertex_shader_source: &str = gluie::rec_basic_vertex();
///}
///```
pub fn rec_basic_vertex() -> &'static str {
    "
#version 330 core

layout(location = 0) in uvec2 structure;

uniform uvec2 cord;
uniform uvec3 color;
uniform vec2 winsize;

out vec3 aColor;

void main() {
    uvec2 point = structure + cord;

    vec2 pos = vec2(point) / winsize;
    pos.y = 1.0 - pos.y;

    gl_Position = vec4(pos * 2.0 - 1.0, 0.0, 1.0);

    aColor = vec3(color) / 255.0;
}
    "
}

///Generates the basic fragment shader for shape that GLUIE need
///
///#Example
///
///```
///fn main(){
///    let fragment_shader_source: &str = gluie::rec_basic_fragment();
///}
///```
pub fn rec_basic_fragment() -> &'static str {
    "
#version 330 core

in vec3 aColor;
out vec4 color;
void main(){
    color = vec4(aColor, 1);
}
    "
}

///Generate the basic vertex shader for image that GLUIE need
///
///#Example
///
///```
///fn main(){
///    let vertex_shader_source: &str = gluie::img_basic_vertex();
///}
///```
pub fn img_basic_vertex() -> &'static str {
    "
#version 330 core

layout(location = 0) in uvec2 structure;
layout(location = 1) in uvec2 uv;

uniform vec2 winsize;
uniform uvec2 cord;

out vec2 aUV;

void main(){
    uvec2 point = cord + structure;
    vec2 pos = vec2(point) / winsize;
    pos.y = 1 - pos.y;

    gl_Position = vec4(pos * vec2(2, 2) - vec2(1, 1), 0, 1);
    aUV = vec2(uv);
}
    "
}

///Generate the basic fragment shader for image that GLUIE need
///
///#Example
///
///```
///fn main(){
///    let fragmnet_shader_source: &str = gluie::img_basic_fragment();
///}
///```
pub fn img_basic_fragment() -> &'static str {
    "
#version 330 core

in vec2 aUV;

uniform sampler2D image;

out vec4 color;
void main(){
    color = texture(image, aUV);
}
    "
}

///Generate the basic vertex shader for font that GLUIE need
///
///#Example
///
///```
///fn main(){
///    let vertex_shader_source: &str = gluie::font_basic_vertex();
///}
///```
pub fn font_basic_vertex() -> &'static str {
    "
#version 330 core

layout(location = 0) in uvec2 structure;
layout(location = 1) in vec2 uv;

uniform vec2 winsize;
uniform uvec2 cord;

out vec2 aUV;

void main(){
    uvec2 point = cord + structure;
    vec2 pos = vec2(point) / winsize;
    pos.y = 1 - pos.y;

    gl_Position = vec4(pos * vec2(2, 2) - vec2(1, 1), 0, 1);
    aUV = vec2(uv);
}
    "
}

///Generate the basic fragment shader for image that GLUIE need
///
///#Example
///
///```
///fn main(){
///    let fragmnet_shader_source: &str = gluie::font_basic_fragment();
///}
///```
pub fn font_basic_fragment() -> &'static str {
    "
#version 330 core

in vec2 aUV;

uniform sampler2D atlas;
uniform uvec3 color;

out vec4 acolor;

void main() {
    float alpha = texture(atlas, aUV).r;
    vec3 rgb = vec3(color) / 255.0;

    acolor = vec4(rgb, alpha);
}
    "
}