Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Fastest way to load a texture in C++ with SDL2

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I used the method in the video but I don’t know the impact on performance yet.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading