Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - add_texture returns index to texture #2864

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion crates/bevy_sprite/src/texture_atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,15 @@ impl TextureAtlas {
}

/// Add a sprite to the list of textures in the `TextureAtlas`
/// returns an index to the texture which can be used with `TextureAtlasSprite`
///
/// # Arguments
///
/// * `rect` - The section of the atlas that contains the texture to be added,
/// from the top-left corner of the texture to the bottom-right corner
pub fn add_texture(&mut self, rect: Rect) {
pub fn add_texture(&mut self, rect: Rect) -> u32 {
Copy link
Member

@NiklasEi NiklasEi Sep 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taking a look at get_texture_index we are returning the index as usize. I think we should do the same here.

Copy link
Contributor Author

@hymm hymm Sep 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My preference would actually be to change get_texture_index to return a u32 too. TextureAtlasSprite and the shader for it takes a u32.

Copy link
Member

@NiklasEi NiklasEi Sep 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should come up with a guideline to keep Bevy consistent. My opinion would be to use usize for indices and cast them to u32 when passing them to shaders. Initial responses on Discord suggest the same. Making all this consistent should be another PR though, so I wouldn't block this one based on the inconsistency.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now, this needs to be u32 because we directly use the TextureAtlasSprite type when generating shader uniform buffers. In the renderer rework, this is no longer the case. And I agree that we should favor usize in user facing types for more natural indexing.

I think we should merge this as-is. Can you create another PR for the pipelined-rendering branch and make the relevant types in bevy_sprites2 use usize?

self.textures.push(rect);
(self.textures.len() - 1) as u32
}

/// How many textures are in the `TextureAtlas`
Expand Down