Your handleEvents method is invalid — instead of if, use while and process all pending events. By the way, you can load the file into texture directly (without touching surfaces), using for example IMG_LoadTexture function. All you need are the renderer and filename.
I made the changes you mentioned, it did return a null and the problem was that sdl wasn’t able to open the .png file; So, I gave it the absolute image path and it works now. Thanks a ton!
Thanks for the suggestion! I’m actually a novice and am learning from a book, I’ll definitely implement IMG_LoadTexture when I level-up my grasp on SDL.
That’s solid advice, It works! Thank you!!
Could you also suggest a book/source for learning SDL…I’m currently reading “SDL Game Development” but there’s some sections that I can’t figure out. Thanks again.
I’m not familiar with SDL books, especially since I use it in conjunction with Free Pascal, which is not very popular.
The first thing to do is to read the documentation of this library — just go to the documentation site, check what functions this library provides, what these functions do, what parameters they require, how to use them. So first look around and find out what SDL can do, and then try to imagine what you can use these functions for (what you can build from them). SDL is low-level, but very easy to use, which is its great advantage.
Links to documentation and well-known tutorials are provided above, but if you want to know how to do something, always use Google — there is a lot of sensible material to help you. If you need anything, you can ask here on the forum, as well as on SDL’s Discord.
I called the loadTexture func in the while loop so, it created a texture every second resulting in memory leak. I placed it outside the game loop and the problem’s solved. Thank you for your replies!
Remember, you’re allowed to declare a variable as many times as you like but your are (usually) not allowed to define a variable more than once.
If you declare a variable outside of any function or class without using static then you’re defining a variable with external linkage.
int foo; // defines a variables named foo with external linkage
If you do this inside a .cpp file then it’s usually not a problem because the content of that file will only be compiled once.
But if you do this inside a header file then it will get defined in all the .cpp files that includes it which will lead to multiple definitions.
What you could do is declare it in a header file using extern
extern int foo; // declares a variables named foo with external linkage (without defining it)
and define it in a .cpp file as normal. Then the .cpp files could just include the header file if they want to use the variable.
Since C++17 there is also another alternative. You could define it as an inline variable. Then multiple definitions will be allowed.
inline int foo; // defines a variables named foo with external linkage (multiple definitions allowed)
If you do this you must make sure that you always define the variable the same way. You should for example not define it as an int in one .cpp file and as float in another .cpp file. That could lead to trouble and the compiler might not complain. This is not a problem if you only define it once in one of your header files. This is no different from how it works with inline functions.
If you do what you did, using static, then it will define a variable with internal linkage.
static int foo; // defines a variables named foo with internal linkage
This means that if you put this into a header file then each .cpp file that includes it will get it’s own copy of the variable. This is probably not what you want.
I just want to declare my renderer once like: SDL_Renderer* renderer = nullptr; renderer = SDL_CreateRenderer(window);
And, use this same renderer in different .cpp files. What should I do?
If I understand you correctly, you want me to use extern right?