1
\$\begingroup\$

I need to create a texture from a surface with indexed color palette and alter the colors. However, after I set new colors, the rendered texture gets blank.

int main(int argc, char** argv)
{
    init();

    gSurface = TTF_RenderText_Solid(gFont, "text", fg1);

    // text is rendered properly, without following line
    SDL_SetPaletteColors(gSurface->format->palette, &fg2, 1, 1);

    gTexture = SDL_CreateTextureFromSurface(gRenderer, gSurface);

    SDL_SetRenderDrawColor(gRenderer, 255, 255, 255, 255);
    SDL_RenderClear(gRenderer);

    SDL_RenderCopy(gRenderer, gTexture, NULL, NULL);
    SDL_RenderPresent(gRenderer);

    SDL_Delay(1000);

    quit();
}

I tested this code with software rendering using window surface instead of renderer and pallete changing worked.
Could someone explain me why does it happed and how to fix it?

\$\endgroup\$
2
  • \$\begingroup\$ What are fg1 and fg2? \$\endgroup\$
    – user35344
    Commented Feb 3, 2016 at 16:00
  • \$\begingroup\$ they are SDL_Color structures \$\endgroup\$
    – warownia1
    Commented Feb 5, 2016 at 18:24

2 Answers 2

0
\$\begingroup\$

I think your problem is the fact that SDL_SetPaletteColors wants an array of SDL_Colors as an argument instead of pointer to a single color. I cannot guarantee that this is the problem as you didn't post the code where you initialize the code, and you haven't told if SDL_GetError() returns an error message.

Otherwise, make sure to always check what SDL_GetError() returns on a SDL function you think might be the cause of the problem.

\$\endgroup\$
2
  • \$\begingroup\$ I tried both an array and a single color and both approaches doesn't work with renderer and does work with window surface. In principle, pointer to the object is no different to a one element array. I'll post more code and SDL_GetError() message if any. \$\endgroup\$
    – warownia1
    Commented Feb 13, 2016 at 16:42
  • \$\begingroup\$ Thinking about it that makes sense. Please add more code and the error message if there is one. \$\endgroup\$
    – user35344
    Commented Feb 13, 2016 at 16:56
0
\$\begingroup\$

Some days ago I have encountered a similar issue with a short example SDL2 app. There were no mainloop as in your sample code. After mainloop added the image began to display.

Quick fix for your code seems to be: Put SDL_Delay(10); or SDL_Delay(100); between SDL_RenderCopy and SDL_RenderPresent. Just like that

SDL_SetRenderDrawColor(gRenderer, 255, 255, 255, 255);
SDL_RenderClear(gRenderer);

SDL_RenderCopy(gRenderer, gTexture, NULL, NULL);
SDL_Delay(10);
SDL_RenderPresent(gRenderer);

SDL_Delay(1000);
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .