I'm currently following Lets Make Games' tutorial series for making a game in C++ with SDL2. In the episode, he uses this method to create a texture:
SDL_Renderer* renderer;
SDL_Texture* tex;
SDL_Surface* tmpSurface = IMG_LOAD("name")
tex = SDL_CreateTextureFromSurface(renderer, tmp);
SDL_FreeSurface(tmpSurface);
I was a little confused about this when I saw it in the video, as I feel like you should make the texture directly? This doesn't feel very memory safe to me, so I was just wondering what the best solution would be.
Im on Windows 11 using Visual Studio 2022, with C++ 11.
I used the method in the video but I don't know the impact on performance yet.
Using an intermediate surface gives you the opportunity to manipulate the image data if needed before creating the texture.
Also, separating the image loading step allows you to handle potential errors that might occur during loading before attempting to create a texture
The approach demonstrated in the tutorial is a reasonable and commonly used way to create textures in SDL2-based games. It offers flexibility and error handling. If you encounter performance issues in your game, it's more likely to be related to other aspects of your code or game design rather than this specific texture creation approach.