use crate::data::Color;
use crate::shader::{Shader, ShaderProgram, ShaderType};
use crate::vertex::VertexBuffer;
#[expect(
clippy::unwrap_used,
reason = "This should be replaced with proper error returns on the next major release."
)]
#[expect(
clippy::module_name_repetitions,
reason = "Will be renamed to shader_2d in the next major release."
)]
#[inline]
#[must_use]
pub fn shape2d_shader() -> ShaderProgram {
ShaderProgram::new(vec![
Shader::load_str(
ShaderType::Vertex,
include_str!("builtin_shaders/shape2d.vert.glsl"),
)
.unwrap(),
Shader::load_str(
ShaderType::Fragment,
include_str!("builtin_shaders/shape2d.frag.glsl"),
)
.unwrap(),
])
.unwrap()
}
#[expect(
clippy::module_name_repetitions,
reason = "Will be renamed to Triangle in the next major release."
)]
#[non_exhaustive]
pub struct TriangleShape {
vertex_buffer: VertexBuffer,
}
impl TriangleShape {
#[inline]
#[must_use]
pub fn new(vertices: &[f32; 15]) -> TriangleShape {
let vertex_buffer = VertexBuffer::new(vertices, &[0, 1, 2]);
vertex_buffer.set_layout(&[2_i32, 3_i32]);
TriangleShape { vertex_buffer }
}
#[inline]
#[must_use]
pub fn new_solid(vertices: &[f32; 6], color: &Color) -> TriangleShape {
let (r, g, b, _) = color.gl();
TriangleShape::new(&[
vertices[0],
vertices[1],
r,
g,
b,
vertices[2],
vertices[3],
r,
g,
b,
vertices[4],
vertices[5],
r,
g,
b,
])
}
#[inline]
#[must_use]
pub fn new_flat_isosceles(
x: f32,
y: f32,
width: f32,
height: f32,
color: &Color,
) -> TriangleShape {
TriangleShape::new_solid(
&[x, y, x + width, y, width.mul_add(0.5, x), y + height],
color,
)
}
#[inline]
pub fn draw(&self, shader_program: &ShaderProgram) {
self.vertex_buffer.draw(shader_program);
}
}
#[expect(
clippy::module_name_repetitions,
reason = "Will be renamed to Triangle in the next major release."
)]
#[non_exhaustive]
pub struct RectangleShape {
vertex_buffer: VertexBuffer,
}
impl RectangleShape {
#[inline]
#[must_use]
pub fn new(vertices: &[f32; 20]) -> RectangleShape {
let vertex_buffer = VertexBuffer::new(vertices, &[0, 1, 2, 2, 3, 0]);
vertex_buffer.set_layout(&[2_i32, 3_i32]);
RectangleShape { vertex_buffer }
}
#[inline]
#[must_use]
pub fn new_solid(vertices: &[f32; 8], color: &Color) -> RectangleShape {
let (r, g, b, _) = color.gl();
RectangleShape::new(&[
vertices[0],
vertices[1],
r,
g,
b,
vertices[2],
vertices[3],
r,
g,
b,
vertices[4],
vertices[5],
r,
g,
b,
vertices[6],
vertices[7],
r,
g,
b,
])
}
#[inline]
#[must_use]
pub fn new_flat(x: f32, y: f32, width: f32, height: f32, color: &Color) -> RectangleShape {
RectangleShape::new_solid(
&[x, y, x + width, y, x + width, y + height, x, y + height],
color,
)
}
#[inline]
pub fn draw(&self, shader_program: &ShaderProgram) {
self.vertex_buffer.draw(shader_program);
}
}