feat(renderer2d): added a simple draw order to the Renderer2D which can be defined by the draw_index in the Render2D component

This commit is contained in:
lisk77 2025-10-25 01:43:34 +02:00
parent dab38c1e32
commit ca47efba42
2 changed files with 33 additions and 3 deletions

View file

@ -46,6 +46,7 @@ pub struct Render2D {
is_visible: bool,
texture_name: &'static str,
scale: v2,
draw_index: u32,
}
#[derive(Component)]
@ -62,6 +63,7 @@ pub struct Text {
font_size: f32,
color: Color,
is_visible: bool,
bounds: v2,
}
#[derive(Component)]
@ -274,11 +276,21 @@ impl Collider for Rectangle2D {
}
impl Render2D {
pub fn new(texture: &'static str, is_visible: bool, scale: v2, draw_index: u32) -> Self {
Self {
is_visible,
texture_name: texture,
scale,
draw_index,
}
}
pub fn with_texture(texture: &'static str) -> Self {
Self {
is_visible: true,
texture_name: texture,
scale: v2::new(1.0, 1.0),
draw_index: 0,
}
}
@ -289,6 +301,14 @@ impl Render2D {
pub fn set_scale(&mut self, scale: v2) {
self.scale = scale;
}
pub fn draw_index(&self) -> u32 {
self.draw_index
}
pub fn set_draw_index(&mut self, index: u32) {
self.draw_index = index
}
}
impl Render for Render2D {
@ -449,6 +469,7 @@ impl Text {
font_size,
color: Color::from_wgpu_color(color.to_wgpu()),
is_visible,
bounds: v2::ZERO,
}
}

View file

@ -686,7 +686,15 @@ impl<'a> Renderer2D<'a> {
return;
}
let entities = scene.get_entities_with(vec![Transform2D::type_id(), Render2D::type_id()]);
let mut entities =
scene.get_entities_with(vec![Transform2D::type_id(), Render2D::type_id()]);
entities.sort_by(|&a, &b| {
let ra = scene.get_component::<Render2D>(a).unwrap();
let rb = scene.get_component::<Render2D>(b).unwrap();
ra.draw_index().cmp(&rb.draw_index())
});
let texts =
scene.get_entities_with(vec![Transform2D::type_id(), comet_ecs::Text::type_id()]);
@ -713,8 +721,9 @@ impl<'a> Renderer2D<'a> {
let region = t_region.unwrap();
let (dim_x, dim_y) = region.dimensions();
let half_width = dim_x as f32 * 0.5;
let half_height = dim_y as f32 * 0.5;
let scale = renderer_component.scale();
let half_width = dim_x as f32 * 0.5 * scale.x();
let half_height = dim_y as f32 * 0.5 * scale.y();
let buffer_size = vertex_buffer.len() as u16;