The problem is in your LoadImage function in resources.hpp/.cpp.
The LoadImage function takes in a pointer to an Image.
When you’re passing in an image to set the loaded image data to, the function won’t actually store the loaded data to the image being passed in.
This means that every time the LoadImage function is called, the loaded data is not stored properly to the Image being passed in.
So your BG and GameIcon (among other data) isn’t properly set.
I have two suggestions on how you can fix this.
Suggestion 1:
Have the LoadImage function take in a double pointer, so a pointer to a pointer to an image to set the loaded image data to.
The image that you then pass in will have to be passed in as a reference.
Example code:
void Resources::LoadImage(Image** to_var, SDL_Renderer* rend, const char* filename)
{
Image* img = Birdy::LoadImage(rend, filename);
*to_var = img;
allImgs.push_back(img);
}
The function would then be used like this:
LoadImage(&GameIcon, rend, "../icon.svg");
Suggestion 2:
Have the LoadImage function return the image data instead of setting the image data to a passed in image.
Example code:
Image* Resources::LoadImage(SDL_Renderer* rend, const char* filename)
{
Image* img = Birdy::LoadImage(rend, filename);
allImgs.push_back(img);
return img;
}
The function would then be used like this:
BG = LoadImage(rend, std::string(IMAGES_FOLDER + "main_menu/background.svg").c_str());
GameIcon = LoadImage(rend, "../icon.svg");
I would personally use the second suggestion.
You have the same issue with most images that’s being loaded in the constructor of Resources. Most of them are nullptr upon loading, except the images that resides inside the multiple std::vector’s that you’re using in your code.
You’ll notice later during development that your LoadSound function is having the same issue as your LoadImage function. The LoadSound function takes in a pointer to a Mix_Chunk and the function won’t store the loaded data to the Mix_Chunk being passed in.
So you’ll need to fix that function too, for the footStepSnds->grass, for example, to be set properly.
Another thing that I want to mention.
You have a lot of memory leaks in your code.
When I shutdown the game, I get a report of around 49.000 memory leaks. You have a lot of new allocations in your code, which is not being cleaned up upon game shutdown.
Remember that for every new in your code, there should be a pairing delete for the data being allocated using new to be cleaned up properly.
Also don’t forget to call SDL_FreeSurface for every created SDL_Surface, SDL_DestroyTexture for every created SDL_Texture and the same for other SDL-specific data types, like fonts, sounds etc.