Draws all render commands to the screen using macroquad.
(
commands: Vec<RenderCommand<CustomElementData>>,
handle_custom_command: impl Fn(&RenderCommand<CustomElementData>),
)
| 1580 | |
| 1581 | /// Draws all render commands to the screen using macroquad. |
| 1582 | pub async fn render<CustomElementData: Clone + Default + std::fmt::Debug>( |
| 1583 | commands: Vec<RenderCommand<CustomElementData>>, |
| 1584 | handle_custom_command: impl Fn(&RenderCommand<CustomElementData>), |
| 1585 | ) { |
| 1586 | let mut state = RenderState::new(); |
| 1587 | for command in commands { |
| 1588 | let current_clip = state.clip_stack.last().copied(); |
| 1589 | match &command.config { |
| 1590 | RenderCommandConfig::Image(image) => { |
| 1591 | let bb = command.bounding_box; |
| 1592 | let cr = &image.corner_radii; |
| 1593 | let mut tint = ply_to_macroquad_color(&image.background_color); |
| 1594 | if tint == Color::new(0.0, 0.0, 0.0, 0.0) { |
| 1595 | tint = Color::new(1.0, 1.0, 1.0, 1.0); |
| 1596 | } |
| 1597 | |
| 1598 | match &image.data { |
| 1599 | ImageSource::Texture(tex) => { |
| 1600 | // Direct GPU texture — draw immediately, no TextureManager |
| 1601 | let has_corner_radii = cr.top_left > 0.0 || cr.top_right > 0.0 || cr.bottom_left > 0.0 || cr.bottom_right > 0.0; |
| 1602 | if !has_corner_radii { |
| 1603 | draw_texture_ex( |
| 1604 | tex, |
| 1605 | bb.x, |
| 1606 | bb.y, |
| 1607 | tint, |
| 1608 | DrawTextureParams { |
| 1609 | dest_size: Some(Vec2::new(bb.width, bb.height)), |
| 1610 | ..Default::default() |
| 1611 | }, |
| 1612 | ); |
| 1613 | } else { |
| 1614 | let mut manager = TEXTURE_MANAGER.lock().unwrap(); |
| 1615 | // Use texture raw pointer as a unique key for the corner-radii variant |
| 1616 | let key = format!( |
| 1617 | "tex-proc:{:?}:{}:{}:{}:{}:{}:{}:{:?}", |
| 1618 | tex.raw_miniquad_id(), |
| 1619 | bb.width, bb.height, |
| 1620 | cr.top_left, cr.top_right, cr.bottom_left, cr.bottom_right, |
| 1621 | current_clip |
| 1622 | ); |
| 1623 | let texture = manager.get_or_create(key, || { |
| 1624 | let mut resized_image: Image = resize(tex, bb.height, bb.width, ¤t_clip).get_texture_data(); |
| 1625 | let rounded_rect: Image = rounded_rectangle_texture(cr, &bb, ¤t_clip).get_texture_data(); |
| 1626 | for i in 0..resized_image.bytes.len()/4 { |
| 1627 | let this_alpha = resized_image.bytes[i * 4 + 3] as f32 / 255.0; |
| 1628 | let mask_alpha = rounded_rect.bytes[i * 4 + 3] as f32 / 255.0; |
| 1629 | resized_image.bytes[i * 4 + 3] = (this_alpha * mask_alpha * 255.0) as u8; |
| 1630 | } |
| 1631 | Texture2D::from_image(&resized_image) |
| 1632 | }); |
| 1633 | draw_texture_ex( |
| 1634 | texture, |
| 1635 | bb.x, |
| 1636 | bb.y, |
| 1637 | tint, |
| 1638 | DrawTextureParams { |
| 1639 | dest_size: Some(Vec2::new(bb.width, bb.height)), |
no test coverage detected