Hello,
I’ve written a small program (actual Archlinux on x86_64 with SDL2 v. 2.0.5). Here is the code in abbreviated form:
if (SDL_Init(SDL_INIT_EVERYTHING))
{
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Could not initialize SDL: %s\n", SDL_GetError());
return(ERROR);
}
if ((mainWindow = SDL_CreateWindow(basename(argv[0]),
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
320, 240,
SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL)) == NULL)
{
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Could not create window: %s\n", SDL_GetError());
close_display();
return(ERROR);
}
mainRenderer = SDL_CreateRenderer(mainWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
mainSurface = SDL_GetWindowSurface(mainWindow);
imgFlags = IMG_INIT_PNG;
if (!(IMG_Init(imgFlags) & imgFlags))
{
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Could not initialize SDL_image: %s\n", IMG_GetError());
close_display();
return(ERROR);
}
if ((gImage = IMG_Load("my_bg.png")) ==NULL)
{
SDL_LogError(SDL_LOG_CATEGORY_ERROR,
"Unable to load image %s!\nSDL_image Error: %s\n",
basename(filename), IMG_GetError());
return(NULL);
}
if ((frameSurface = SDL_ConvertSurface(gImage, mainSurface->format, 0)) == NULL)
SDL_LogError(SDL_LOG_CATEGORY_ERROR,
"Unable to optimize image %s!\nSDL_image Error: %s\n",
basename(filename), IMG_GetError());
SDL_FreeSurface(gImage);
SDL_RenderClear(mainRenderer);
SDL_BlitSurface(frameSurface, NULL, mainSurface, NULL);
SDL_RenderPresent(mainRenderer);
All steps run without errors returned, but it doesn’t display the PNG image.
It works when I replace the call to SDL_BlitSurface by following lines:
frameTexture = SDL_CreateTextureFromSurface(mainRenderer, frameSurface);
SDL_RenderCopy(mainRenderer, frameTexture, NULL, NULL);
The same behavior results when I try to output a text string by following code:
textSurface = TTF_RenderText_Blended(font, string, color);
SDL_BlitSurface(textSurface, NULL, mainSurface, &dstRect);
I don’t understand, why the call to SDL_BlitSurface doesn’t work!
Can somebody help me?
Thanks in advance,
Michael