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

Provide a convenient way to convert to Surface's pixel format #61

Open
ilyapopov opened this issue Dec 29, 2015 · 6 comments
Open

Provide a convenient way to convert to Surface's pixel format #61

ilyapopov opened this issue Dec 29, 2015 · 6 comments

Comments

@ilyapopov
Copy link
Contributor

Suppose I want to fill a rectangle with some color. FillRect member function expects a color in surface's format. There is conversion function, SDL_MapRGB, but it expects SDL_PixelFormat*. Surface::GetFormat, however, returns only SDL_PixelFormat::format, not the whole structure.

What is the preferred way to convert RGB values to surface's color that FillRect expects?

surface.FillRect(rect, SDL_MapRGB(???, r, g, b));

Shall a method be provided to convert RGB to color value:

Surface::MapRGB(Uint8 r, Uint8 g, Uint8 b);

Or even a FillRect overload taking RGB values?

@ooxi
Copy link
Contributor

ooxi commented Dec 30, 2015

👍

@AMDmi3
Copy link
Member

AMDmi3 commented Dec 30, 2015

Yes, I've had that in plans. Not yet figured out a consistent way to handle both integer pixelformat and SDL_PixelFormat structure everywhere (renderer, texture, surface). Regarding the color, MapRGB should likely be a method of a SDL_PixelFormat wrapper. I'll try to handle this next year :)

@ilyapopov
Copy link
Contributor Author

Is this a correct workaround?

SDL_PixelFormat fmt* SDL_AllocFormat(surface.Format());
surface.FillRect(rect, SDL_MapRGB(fmt, r, g, b));
SDL_FreeFormat(fmt);

@ilyapopov
Copy link
Contributor Author

Or better this:

std::unique_ptr<SDL_PixelFormat, void (*)(SDL_PixelFormat *)> fmt(
    SDL_AllocFormat(surface.Format()), SDL_FreeFormat);
surface.FillRect(rect, SDL_MapRGB(fmt.get(), r, g, b));

@AMDmi3
Copy link
Member

AMDmi3 commented Dec 30, 2015

You don't have to allocate a new pixel format as Surface already contains one.

Probably something like this:

surface.FillRect(rect, SDL_MapRGB(surface.Get()->format, r, g, b));

@ilyapopov
Copy link
Contributor Author

Thanks! I did not realize i can access format through Get().

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

3 participants