1
\$\begingroup\$

When loading a texture in SDL2 the general procedure is this:

SDL_Texture* texture = IMG_LoadTexture(renderer, "resources/images/image.png);

However this doesn't work and I have to supply the WHOLE path to the specific image. The code that I have to use is:

SDL_Texture* texture = IMG_LoadTexture(renderer, "/home/user/programs/game/resources/images/image.png);

This issue doesn't occur in a Windows Mingw32 environment.

Any suggestion on how to fix this issue?

\$\endgroup\$
2
\$\begingroup\$

It's possible that you're running the executable in a different directory than the one that contains your resources folder.

If you need to change the directory from, say, a makefile, just cd to the desired base directory and run the executable from there.

\$\endgroup\$
  • \$\begingroup\$ Instead of makefile, the recommended way is to use SDL_GetBasePath() to access resources. \$\endgroup\$ – Milan Babuškov Feb 10 '15 at 0:05
1
\$\begingroup\$

You can get this information with the following code:

char buffer[256];    
char *getcwd( buffer, 256 );

One hint: when the working directory differs from the place where the binary is stored: do you start the app from an IDE like QtCreator. If yes: please change the working directory for the run in your IDE.

\$\endgroup\$
  • \$\begingroup\$ Your hint helped also but the other answer was better \$\endgroup\$ – saloomi2012 Aug 12 '14 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.