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

Getting PixelFormat from PixelFormatEnum? #840

Closed
sarracht opened this issue Jan 14, 2019 · 5 comments
Closed

Getting PixelFormat from PixelFormatEnum? #840

sarracht opened this issue Jan 14, 2019 · 5 comments

Comments

@sarracht
Copy link

Color::to_u32() requires a &PixelFormat, but I've got a PixelFormatEnum and I can't figure out how to convert it. There's a From implementation for PixelFormat to PixelFormatEnum, but nothing the other way around as far as I can tell.

The overall project here is drawing text with rusttype. Best I've figured is to get a Color for each pixel, ram them all in a Vec<u32>, pass that data as a &[u8] to Surface::from_data(), then TextureCreator::create_texture_from_surface(). If there's a better/simpler way, I'm all ears.

@khernyo
Copy link

khernyo commented Mar 2, 2019

The following seems to work:

PixelFormat::from_ll(sdl2::sys::SDL_AllocFormat(pixel_format_enum as u32))

Swiped from https://stackoverflow.com/a/32000743/203487

@Cobrand
Copy link
Member

Cobrand commented Mar 3, 2019

Seems like there could be a helper in rust-sdl2 to help you do that without unsafe. I'm tagging it as functionality, if someone sees this and wants to make a PR, feel free!

@ishanjain28
Copy link

@Cobrand Hi, Is an Into trait implementation to go from PixelFormatEnum to PixelFormat that uses unsafe okay?

@Cobrand
Copy link
Member

Cobrand commented Mar 7, 2019

I think it's fine to use unsafe here, as long as we guarantee ourselves that the code cannot fail according to the sdl2 doc.

@mnmaita
Copy link
Contributor

mnmaita commented Sep 29, 2019

@Cobrand I guess this issue is closeable as per #898

@Cobrand Cobrand closed this as completed Sep 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants