0

When loading a PNG image on my SDL program I used the SDL_CreateTextureFromSurface() function, however it returns a NULL texture and the error returned is "Invalid renderer".

The renderer was created successfully, I did test what it could be putting an SDL_GetError() after the CreateRenderer and it returned the error "That operation is not supported".

#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>

int main() {
  if (SDL_Init(SDL_INIT_VIDEO) < 0) {...} //Print error and return 1.

  SDL_Window * window = SDL_CreateWindow("test", 0, 0, 100, 100, 0);
  if (window == NULL) {...} //Print error and return 1.

  SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0);
  if (renderer == NULL) {...} //Print error and return 1.

  printf("%s\n", SDL_GetError); //Returns "That operation is not supported".

  int imageFlags = IMG_INIT_PNG;
  if (!(IMG_Init(imageFlags) & imageFlags)) {...} //Print error and return 1.
  
  SDL_Texture * texture = NULL;
  SDL_Surface * surface = IMG_Load("Test.png"); //The file does exist on the folder.
  if (surface == NULL) {...} //Print error and return 1.
  texture = SDL_CreateTextureFromSurface(renderer, surface);
  if (texture == NULL) {
    printf("%s\n", SDL_GetError()); //Returns "Invalid Renderer".
    return 1; 
  } 
  SDL_FreeSurface(surface);

  SDL_Delay(100);
  SDL_DestroyRenderer(renderer);
  SDL_DestroyWindow(window);
  IMG_Quit();
  SDL_Quit();
  return 0;
}
Troloze
  • 23
  • 8
  • Is `surface` non-null? – HolyBlackCat Nov 15 '20 at 21:35
  • Yes. I forgot to put the NULL check on the example, just editted it in. But yeah, not NULL – Troloze Nov 15 '20 at 21:52
  • This probably isn't the issue, but technically you should call `SDL_GetError()` if there's an error in `SDL_CreateTextureFromSurface()`, not `IMG_GetError()`. See https://wiki.libsdl.org/SDL_CreateTextureFromSurface Also note that if you call GetError before there is any error, the output is not meaningful. – Nick ODell Nov 15 '20 at 21:56
  • @NickODell thanks for the reminder, just swapped them on my main program, but the problem is still there lol. Gonna edit the code example as well. – Troloze Nov 15 '20 at 22:00
  • @Troloze "running it on a Virtual Machine" probably that is your problem. What happens if you do `SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE)` instead? – keltar Nov 16 '20 at 11:58
  • @keltar just tried it out, still, same problem: "Invalid renderer". – Troloze Nov 16 '20 at 13:00
  • @Troloze could you edit in absolultely 100% the same code you use in your test, as well as your build command? It is possible the problem is in a part we don't see. – keltar Nov 16 '20 at 16:19
  • @keltar The program is kinda big already, also, this program in the example has the same problem, i've tried it. I'm using `gcc Test.c -o prog -lSDL2 -lSDL2_image` to compile it and `./prog` to run it. – Troloze Nov 16 '20 at 18:47

0 Answers0