mod circle;
mod line;
mod mesh;
mod point;
mod quad;
mod rect;
mod sprite;
use crate::geometry::*;
use crate::math::*;
use crate::*;
use core::mem::size_of;
use image;
use image::*;
use std::path::Path;
const BYTES_POINTS: usize = size_of::<DrawPoint>() * MAX_POINTS;
const BYTES_LINES: usize = size_of::<DrawLine>() * MAX_LINES;
pub fn load_img(ctx: &mut Ctx, filename: &str) -> Texture {
let path = Path::new(filename);
let img = image::open(path).unwrap();
gl_register_img(ctx, img)
}
pub use circle::draw_circ;
pub use line::draw_line;
pub use mesh::draw_mesh;
pub use point::draw_point;
pub use rect::draw_rect;
pub use sprite::*;
pub fn draw_shape(ctx: &mut Ctx, shape: Shape, color: V4) {
match shape {
Shape::Point(p) => draw_point(ctx, p, color),
Shape::Rect(r) => draw_rect(ctx, r, color),
Shape::Circle(c) => draw_circ(ctx, c, color),
}
}
pub fn aspect(_: &mut Ctx) -> f32 {
(sapp_width() as f32) / (sapp_height() as f32)
}
pub fn window_width(_: &Ctx) -> u32 {
(sapp_width() as u32)
}
pub fn window_height(_: &Ctx) -> u32 {
(sapp_height() as u32)
}
pub fn default_projection(ctx: &mut Ctx) {
let half_w = window_width(ctx) as f32 / 2.0;
let half_h = window_height(ctx) as f32 / 2.0;
let camera_pos = v3(0.0, 0.0, 6.0);
ctx.gl.proj = M4::ortho(-half_w, half_w, -half_h, half_h, -500.0, 500.0);
ctx.gl.view = M4::look_at(camera_pos, V3::ZERO, V3::Y);
}
fn gl_register_img(ctx: &mut Ctx, img: DynamicImage) -> Texture {
let id = ctx.gl.images.count;
ctx.gl.images.count += 1;
let img = img.into_rgba();
let (w, h) = img.dimensions();
let width = w as i32;
let height = h as i32;
let img_data = img.into_raw();
let img_ptr: *const u8 = img_data.as_ptr();
let size: i32 = width * height * 8 ;
let e = sg_make_image(
Some(&[(img_ptr, size)]),
&SgImageDesc {
width,
height,
pixel_format: SgPixelFormat::RGBA8,
min_filter: SgFilter::Nearest,
mag_filter: SgFilter::Nearest,
wrap_u: SgWrap::ClampToEdge,
wrap_v: SgWrap::ClampToEdge,
..Default::default()
},
);
ctx.gl.images.e[id] = Image { e, w, h };
return Texture { id, w, h };
}
fn std_uniform_block<'a>() -> SgShaderUniformBlockDesc<'a> {
SgShaderUniformBlockDesc {
size: size_of::<M4>() as i32,
uniforms: vec![SgShaderUniformDesc {
name: "projection",
uniform_type: SgUniformType::Mat4,
array_count: 0,
}],
}
}
pub fn init(ctx: &mut Ctx) {
sg_setup(&SgDesc {
..Default::default()
});
ctx.gl.proj = M4::IDENTITY;
ctx.gl.view = M4::IDENTITY;
ctx.gl.pass_action = SgPassAction {
colors: vec![SgColorAttachmentAction {
action: SgAction::Clear,
val: [0.2, 0.2, 0.2, 1.0],
}],
..Default::default()
};
mesh::init(ctx);
line::init(ctx);
point::init(ctx);
quad::init(ctx);
}
pub fn present(ctx: &mut Ctx) {
sg_begin_default_pass(&ctx.gl.pass_action, sapp_width(), sapp_height());
mesh::present(ctx);
quad::present(ctx);
point::present(ctx);
line::present(ctx);
ctx.gl.quads.count = 0;
ctx.gl.points.count = 0;
ctx.gl.lines.count = 0;
ctx.gl.mesh.count = 0;
sg_end_pass();
sg_commit();
}