gluie 0.2.0

A 2d easy to use graphics engine for application development (GPU-accelerated)
Documentation
use std::sync::atomic::{AtomicU32, Ordering};

static FONT_ATLAS_X: AtomicU32 = AtomicU32::new(0);
static FONT_ATLAS_Y: AtomicU32 = AtomicU32::new(0);

mod load;
use load as lad;

use crate::texture;

//rectangle shape
pub struct Rec {
    pub mw: u32,
    pub mh: u32,
    pub mr: u8,
    pub mg: u8,
    pub mb: u8,
    pub mx: u32,
    pub my: u32,
    structure: Vec<(u32, u32)>,

    pub vao: u32,
    pub vbo: u32,
}

impl Rec {
    pub fn new(x: u32, y: u32, w: u32, h: u32, r: u8, g: u8, b: u8) -> Rec {
        Rec {
            mw: w,
            mh: h,
            structure: Vec::new(),
            vao: 0,
            vbo: 0,
            mx: x,
            my: y,
            mr: r,
            mg: g,
            mb: b,
        }
    }

    pub fn create_struct(&mut self) {
        self.structure.push((0, 0));
        self.structure.push((self.mw, 0));
        self.structure.push((0, self.mh));
        self.structure.push((self.mw, self.mh));

        (self.vao, self.vbo) = lad::load_rec(&self.structure);
    }

    pub fn change_size(&mut self, w: u32, h: u32) {
        (self.mw, self.mh) = (w, h);

        self.structure.clear();
        self.structure.push((0, 0));
        self.structure.push((self.mw, 0));
        self.structure.push((0, self.mh));
        self.structure.push((self.mw, self.mh));

        lad::change_size(&self.structure, &mut self.vbo);
    }
    
    pub fn change_color(&mut self, r: u8, g: u8, b: u8) {
        (self.mr, self.mg, self.mb) = (r, g, b);
    }

    pub fn change_cord(&mut self, x: u32, y: u32){
        (self.mx, self.my) = (x, y);
    }
}

//image object
pub struct Image {
    pub mw: u32,
    pub mh: u32,
    pub mx: u32,
    pub my: u32,
    pub image: u32,

    structure: Vec<(u32, u32)>,
    pub vao: u32,
    pub str_vbo: u32,
    pub uv_vbo: u32,
}

impl Image {
    pub fn new(
        w: u32,
        h: u32,
        x: u32,
        y: u32,
        (image_data, image_width, image_height): (Vec<u8>, u32, u32),
    ) -> Image {
        let mut image_texture = 0u32;
        
        texture::gen_texture(&mut image_texture);

        unsafe {
            gl::TexImage2D(
                gl::TEXTURE_2D,
                0,
                gl::RGBA as i32,
                image_width as i32,
                image_height as i32,
                0,
                gl::RGBA,
                gl::UNSIGNED_BYTE,
                image_data.as_ptr() as *const _,
            );
        }

        Image {
            mw: w,
            mh: h,
            mx: x,
            my: y,
            structure: Vec::new(),
            image: image_texture,
            vao: 0,
            str_vbo: 0,
            uv_vbo: 0,
        }
    }

    pub fn create_struct(&mut self) {
        self.structure.push((0, 0));
        self.structure.push((self.mw, 0));
        self.structure.push((0, self.mh));
        self.structure.push((self.mw, self.mh));

        (self.vao, self.str_vbo, self.uv_vbo) = lad::load_image(&self.structure, ((0.0, 0.0), (1.0, 1.0)));
    }

    pub fn change_size(&mut self, w: u32, h: u32){
        (self.mw, self.mh) = (w, h);

        self.structure.clear();
        self.structure.push((0, 0));
        self.structure.push((self.mw, 0));
        self.structure.push((0, self.mh));
        self.structure.push((self.mw, self.mh));

        lad::change_size(&self.structure, &mut self.str_vbo);
    }

    pub fn change_img(&mut self, (image_data, image_width, image_height): (Vec<u8>, u32, u32)){
        unsafe{
            gl::BindTexture(gl::TEXTURE_2D, self.image);

            gl::TexImage2D(
                gl::TEXTURE_2D,
                0,
                gl::RGBA as i32,
                image_width as i32,
                image_height as  i32,
                0,
                gl::RGBA,
                gl::UNSIGNED_BYTE,
                image_data.as_ptr() as *const _,
            );
        }
    }
    
    pub fn change_cord(&mut self, x: u32, y: u32){
        (self.mx, self.my) = (x, y);
    }
}

pub struct Text {
    pub mw: u32,
    pub mh: u32,
    pub maw: u32,
    pub mah: u32,
    pub mx: u32,
    pub my: u32,
    pub mr: u8,
    pub mg: u8,
    pub mb: u8,
    pub mch: char,

    structure: Vec<(u32, u32)>,

    pub vao: u32,
    pub str_vbo: u32,
    pub uv_vbo: u32,
}

impl Text {
    pub fn new(
        w: u32,
        h: u32,
        aw: u32,
        ah: u32,
        x: u32,
        y: u32,
        image_data: &[u8],
        r: u8,
        g: u8,
        b: u8,
        ch: char,
        texture_atlas: u32,
        font_size: u32,
    ) -> (Text, (f32, f32), (f32, f32)) {
        unsafe {
            gl::BindTexture(gl::TEXTURE_2D, texture_atlas);
            gl::PixelStorei(gl::UNPACK_ALIGNMENT, 1);

            gl::TexSubImage2D(
                gl::TEXTURE_2D,
                0,
                FONT_ATLAS_X.load(Ordering::Relaxed) as i32,
                FONT_ATLAS_Y.load(Ordering::Relaxed) as i32,
                w as i32,
                h as i32,
                gl::RED,
                gl::UNSIGNED_BYTE,
                image_data.as_ptr() as *const _,
            );
        }

        let font_atlas_x_before = FONT_ATLAS_X.load(Ordering::Relaxed) as f32;
        let font_atlas_y_before = FONT_ATLAS_Y.load(Ordering::Relaxed) as f32;

        FONT_ATLAS_X.fetch_add(w, Ordering::Relaxed);
        if FONT_ATLAS_X.load(Ordering::Relaxed) >= 2048{
            FONT_ATLAS_X.store(0, Ordering::Relaxed);
            FONT_ATLAS_Y.fetch_add(font_size, Ordering::Relaxed);
        }

        (
            Text {
                mw: w,
                mh: h,
                maw: aw,
                mah: ah,
                mx: x,
                my: y,
                mr: r,
                mg: g,
                mb: b,
                mch: ch,

                structure: Vec::new(),
                vao: 0,
                str_vbo: 0,
                uv_vbo: 0,
            },

            (
                font_atlas_x_before / 2048.0,
                font_atlas_y_before / 2048.0,
            ),
            (
                (font_atlas_x_before + w as f32) / 2048.0,
                (font_atlas_y_before + h as f32) / 2048.0,
            ),        
        )
    }

    pub fn create_struct(&mut self, uv: ((f32, f32), (f32, f32))) {
        self.structure.push((0, 0));
        self.structure.push((self.mw, 0));
        self.structure.push((0, self.mh));
        self.structure.push((self.mw, self.mh));

        (self.vao, self.str_vbo, self.uv_vbo) = lad::load_image(&self.structure, uv);
    }

    pub fn chage_cord(&mut self, x: u32, y: u32){
        (self.mx, self.my) = (x, y);
    }

    pub fn change_color(&mut self, r: u8, g: u8, b: u8){
        (self.mr, self.mg, self.mb) = (r, g, b);
    }
}