Skip to content

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

Open

Description

@ilyapopov

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?

Activity

  1. ooxi commented on Dec 30, 2015

    @ooxi
    Contributor

    👍

  2. AMDmi3 commented on Dec 30, 2015

    @AMDmi3
    Member

    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 :)

  3. ilyapopov commented on Dec 30, 2015

    @ilyapopov
    ContributorAuthor

    Is this a correct workaround?

    SDL_PixelFormat fmt* SDL_AllocFormat(surface.Format());
    surface.FillRect(rect, SDL_MapRGB(fmt, r, g, b));
    SDL_FreeFormat(fmt);
    
  4. ilyapopov commented on Dec 30, 2015

    @ilyapopov
    ContributorAuthor

    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));
    
  5. AMDmi3 commented on Dec 30, 2015

    @AMDmi3
    Member

    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));
    
  6. ilyapopov commented on Dec 30, 2015

    @ilyapopov
    ContributorAuthor

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

  7. self-assigned this
    on Jan 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions