2

I have recently started working on a small program with SDL2 and c++. In this program I used IMG_LoadTexture() to load a texture however it just would not want to work, the program could not find the image. So I chanced my program to use SDL_loadBMP(), did not work. After hours of endlessly looking for bugs I still could not find anything.

When I started my project I saved it to my D: disk and not on my C: disk. So when I was about to give up on the whole project I copied my exe, image and libraries to my C: disk and Boom it worked.

So I have no idea why this happens. I refer to my image with a relative path but it just doesn't want to work on my D: disk. Is this a bug? or might it have something to do with settings related to my disk? And how can I make it work on my D: disk?

P.S. I am using windows 7
P.S.S. My apologies in advance for any horrible grammar and spelling mistakes.

Skayn
  • 23
  • 4
  • I had the same errors: in the file where I've written my SDL1.2 texture loading function with IMG_Load() (OpenGL), I included (normal). When I changed to SDL2.0, I've forgotten to change to . It was compiling, but not working (normal). Could it be the same error ? – Pierre Emmanuel Lallemant Jan 15 '14 at 18:13
  • Can we see a little snippet of code and en error message ? It could be really useful.. – jordsti Jan 15 '14 at 18:39

1 Answers1

2

Is the working directory of you executable the same you assume it is? On windows happened more than one time that working directory was not the path I believed. With certain compiler configurations the working directory is not the same on wich the executable is, you have to change working directory looking at documentation of your IDE.

What frequently happened to me

CURRENT WORKING DIR

D:/projectdir/

EXPECTED WORKING DIR

D:/projectdir/bin/debug/

where files really are:

D:/projectdir/bin/debug/app.exe
D:/projectdir/bin/debug/texture.png

which files app is trying to loading when launched from IDE:

D:/projectdir/app.exe
D:/projectdir/texture.png

You can quickly test wich is current working directory by calling equivalent on windows of getcwd (answer from here )

#include <WinBase.h>

int main() {
    TCHAR pwd[MAX_PATH];
    GetCurrentDirectory(MAX_PATH,pwd);
    MessageBox(NULL,pwd,pwd,0);
}
Community
  • 1
  • 1
CoffeDeveloper
  • 7,961
  • 3
  • 35
  • 69
  • 1
    @DarioOO: I would not suggest changing the working directory in the IDE... it is set to be the project's root directory by default for a reason. Otherwise, you have to duplicate all resources for each build configuration (e.g. Release/, Debug/, Release-x64/, Debug-x64/). It is great having the .exe stored in the build configuration folder and all other resources and dependencies stored in a central location. – Andon M. Coleman Jan 15 '14 at 18:49
  • So the program runs in projectdir? How does this work? – Skayn Jan 15 '14 at 18:49
  • when you try to open a file named "test.txt" for example OS will append for you the complete path (working dir) and it becomes an absolute path "d:/projectdir/bin/test.txt" wich is what you goind to really open. The working dir is provided externally so it is not necessarily be the same in wich executable is – CoffeDeveloper Jan 15 '14 at 18:52
  • No problem, by the way keep in mind also Andon M. Coleman suggestion;). Once you get confindent on how working dir and relative paths works you should start thinking a better way for doin things – CoffeDeveloper Jan 15 '14 at 18:57