Reputation: 934
I'm trying to create a SDL_Surface for pixel manipulation, however something is going really wrong when setting the color masks of the surface as the colors are incorrect when filling the color buffer (see the comments, I'm trying to interpret u8vec4 as a RGBA color):
const int win_width = 640;
const int win_height = 480;
std::vector<glm::u8vec4> colors{ win_width * win_height, glm::u8vec4{ 0, 0, 0, 0 } };
SDL_Surface* render_surface = SDL_CreateRGBSurfaceFrom(&colors[0], win_width, win_height, 32,
sizeof(glm::u8vec4) * win_width, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
SDL_Texture* render_texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_STREAMING, win_width, win_height);
// { 255, 0, 0, 255 } = Red
// { 255, 0, 0, 100 } = Darker red, alpha seems to be working
// { 0, 255, 0, 255 } = Purple!? Should be green?
// { 0, 0, 255, 255 } = Yellow, expecting blue
std::fill(colors.begin(), colors.end(), glm::u8vec4{ 0, 0, 0, 255 }); // Red
SDL_UpdateTexture(render_texture, nullptr, render_surface->pixels, render_surface->pitch);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, render_texture, nullptr, nullptr);
SDL_RenderPresent(renderer);
Upvotes: 0
Views: 3550
Reputation: 5532
You should try setting your bitmaks according to the endian. Here's how I do it :
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
const uint32_t R_MASK = 0xff000000;
const uint32_t G_MASK = 0x00ff0000;
const uint32_t B_MASK = 0x0000ff00;
const uint32_t A_MASK = 0x000000ff;
#else
const uint32_t R_MASK = 0x000000ff;
const uint32_t G_MASK = 0x0000ff00;
const uint32_t B_MASK = 0x00ff0000;
const uint32_t A_MASK = 0xff000000;
#endif
In your case the you've gotten the colormasks the other way around.
I think Jongware is correct, that the format is ABGR.
// { 255, 0, 0, 100 } = Darker red, alpha seems to be working
I'm guessing here the alpha seems correct because the background is black, and by reducing the amount of red to 100, it will naturally blend in with the background.
I also think you haven't set SDL_BlendMode
Upvotes: 4