use gl::types::*;
use crate::core::*;
pub mod rect;
pub mod texture;
pub mod text;
pub mod circle;
pub struct Buffer {
pub id: GLuint,
target: GLuint,
usage: GLuint,
}
impl Buffer {
pub fn new(target: GLuint, usage: GLuint) -> Self {
let mut id: GLuint = 0;
unsafe {
gl::GenBuffers(1, &mut id);
}
Self { id, target, usage }
}
pub fn bind(&self) {
unsafe {
gl::BindBuffer(self.target, self.id);
}
}
pub fn set_data<DataType>(&self, data: &Vec<DataType>) {
self.bind();
let size = data.len() * std::mem::size_of::<DataType>();
let data = data.as_ptr();
unsafe {
gl::BufferData(
self.target,
size as gl::types::GLsizeiptr,
data as *const gl::types::GLvoid,
self.usage
);
}
}
}
impl Default for Buffer {
fn default() -> Self {
Self {
id: 0,
target: 0,
usage: 0,
}
}
}
impl Drop for Buffer {
fn drop(&mut self) {
unsafe {
gl::DeleteBuffers(1, [self.id].as_ptr());
}
}
}
pub struct VertexArray {
pub id: GLuint,
}
impl VertexArray {
pub fn new() -> Self {
let mut id: GLuint = 0;
unsafe {
gl::GenVertexArrays(1, &mut id);
}
Self { id }
}
pub fn bind(&self) {
unsafe {
gl::BindVertexArray(self.id);
}
}
}
impl Default for VertexArray {
fn default() -> Self {
Self {
id: 0,
}
}
}
impl Drop for VertexArray {
fn drop(&mut self) {
unsafe {
gl::DeleteVertexArrays(1, [self.id].as_ptr());
}
}
}
pub struct TextureBuffer {
pub id: GLuint,
}
impl TextureBuffer {
pub fn new() -> Self {
let mut id: GLuint = 0;
unsafe {
gl::GenTextures(1, &mut id);
}
Self { id }
}
pub fn bind(&self) {
unsafe {
gl::BindTexture(gl::TEXTURE_2D, self.id);
}
}
pub fn set_data(&self, image: &image::RgbaImage) {
self.bind();
let (width, height) = image.dimensions();
unsafe {
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);
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_S, gl::CLAMP_TO_EDGE as i32);
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_T, gl::CLAMP_TO_EDGE as i32);
gl::TexImage2D(
gl::TEXTURE_2D,
0,
gl::RGBA as i32,
width as i32,
height as i32,
0,
gl::RGBA,
gl::UNSIGNED_BYTE,
image.as_ptr() as *const gl::types::GLvoid,
);
gl::GenerateMipmap(gl::TEXTURE_2D);
}
}
}
impl Default for TextureBuffer {
fn default() -> Self {
Self {
id: 0,
}
}
}
impl Drop for TextureBuffer {
fn drop(&mut self) {
unsafe {
gl::DeleteTextures(1, [self.id].as_ptr());
}
}
}
pub trait Object {
fn add(&mut self, component_data: &ObjectData);
fn set(&mut self, i: usize, component_data: &ObjectData);
fn load(&mut self) -> Result<(), String>;
fn reload(&mut self);
fn remove(&mut self, i: usize);
fn remove_all(&mut self);
fn draw(&mut self, draw: &Draw, camera: &Transform, model_transform: &Transform) -> Result<(), String>;
fn set_state(&mut self, object_state: ObjectState);
}
pub enum ObjectState {
Ok,
Reload
}
pub type TextureCoordinate = [f32; 8];