I have built a space invaders clone using SDL and compiled with MSVC11. The code loads 4 separate PNG files and a TTF font for use in rendering. The program works perfectly fine when given an absolute file path eg "C:/spaceinvader.png".
I tried changing this to a relative file path, ie "./res/spaceinvader.png
The program is then compiled it in release mode, and placed it in a folder of its own.
Directory structure:
game\game.exe
game\(all the required SDL dlls)
game\res\image1.png (and the other 3 images)
game\res\font.ttf
The oddest thing happens when you try to run the game. The first time you attempt to run this, the program console loads up, the SDL window opens, and then it crashes to the desktop. (I have ensured there are no premature return codes in main()). All subsequent times however, the program loads and works, although one of the sprites does not display on the screen. The only error appearing in the console relates to two of the PNG files having an incorrect sRGB profile.
If the entire folder is moved to a different location the program doesn't load again the first time, but then loads subsequently
So my question is
1) why does the program not load the first time, but subsequently? 2) why is there a failure of just one sprite to display, when the files are all identical (created in photoshop) and the code to load them is identical?
(apologies for not including any code here as I didn't feel that this was a coding issue - happy to paste specific code segments on request)
Thanks! K
EDIT:
Image loading code (loadTexture function cribbed from here:
SDL_Texture* loadTexture(const std::string &file, SDL_Renderer *ren)
{
SDL_Texture *texture = IMG_LoadTexture(ren, file.c_str());
log_CheckSDLPointer(std::cout, texture, "LoadTexture");
return texture;
}
int Gamestate::initialiseSprites(SDL_Renderer *Renderer)
{
enemytexture = loadTexture("./res/invader.png", Renderer);
if (enemytexture == nullptr) {return 1;}
playertexture = loadTexture("./res/player.png", Renderer);
if (playertexture == nullptr) {return 1;}
missiletexture = loadTexture("./res/bullet.png", Renderer);
if (missiletexture == nullptr) {return 1;}
background = loadTexture("./res/background.png", Renderer);
if (background == nullptr) {return 1;}
return 0;
}
This function is called in main:
if (Active_Gamestate.initialiseSprites(mainRenderer) != 0)
{
return 1; //quit game if sprites could not be initialised
}