darthdeus / comfy

Comfy is a fun 2D game engine built in Rust. It's designed to be opinionated, productive, and easy to use.
https://comfyengine.org
Apache License 2.0
717 stars 35 forks source link

Add new draw_sprite_pro function with a more user-friendly API #65

Closed setzer22 closed 11 months ago

setzer22 commented 11 months ago

This brings a more intuitive (IMO, but also as we discussed) API to draw sprites. It supports sprite alignment, scaling (rather, set the sprite size) as well as setting a rotation pivot.

The API is based on this params struct:

pub struct DrawTextureProParams {
    /// The source rectangle in the texture to draw, uses pixel coordinates. If
    /// `None`, the entire texture is drawn.
    pub source_rect: Option<IRect>,
    /// The alignment of the sprite. The sprite's origin (its position) will be
    /// aligned according to this value. E.g. if `align` is `BottomRight`, at
    /// the draw position the bottom right corner of the sprite will be drawn.
    pub align: SpriteAlign,
    /// Defined as an offset from the sprite position. The point around which
    /// the sprite rotates. None means the pivot is the sprite's position.
    pub pivot: Option<Vec2>,
    /// The desired size of the sprite in world units.
    pub size: Vec2,
    /// The rotation to apply to the sprite, in radians.
    pub rotation: f32,
    /// Whether to flip the sprite horizontally.
    pub flip_x: bool,
    /// Whether to flip the sprite vertically.
    pub flip_y: bool,
    /// The blend mode to use when drawing the sprite.
    pub blend_mode: BlendMode,
}

and this function

pub fn draw_sprite_pro(
    texture: TextureHandle,
    position: Vec2,
    tint: Color,
    z_index: i32,
    params: DrawTextureProParams,
) {

I didn't change anything from the previous API, and I also kind of avoided the rotated_rectangle method because it didn't kinda do what I wanted, so the method looks a bit more boilerplatey because of that. But I have tested everything on my end and it seems to be working properly.

setzer22 commented 11 months ago

I've noticed there was an issue with flipping the sprites when sprites have animation (and thus define a source_rect different than the full image). It is now fixed.

As discussed, I'll also be adding some examples :+1:

setzer22 commented 11 months ago

Alright, finally found some time to take care of this :smile: Here's the new example!

https://github.com/darthdeus/comfy/assets/7241990/aeaf59dd-6341-4e5e-ae58-cd91b64c1f19

darthdeus commented 11 months ago

thanks!