Loading files in Windows

I'm using SDL, so if I'm not meant to be asking for help with that here let me know I'll post somewhere else.

I'm not sure why this isn't working; it compiles fine on my mac but then when I try it on windows it doesn't seem to load the images properly.
The error message which I get from the function displays the correct filepath (and the file is definately there), so I can't understand why it won't load.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
SDL_Surface* load_image(char* filename){
    SDL_Surface* loadedImage = NULL;
    SDL_Surface* optimizedImage = NULL;
	
	stringstream filepath;
	filepath << get_directory() << filename;
    loadedImage = IMG_Load(filepath.str().c_str());
    
    if(loadedImage == NULL) error_message(const_cast<char*> (filepath.str().c_str()));
    
    if(loadedImage != NULL){
        optimizedImage = SDL_DisplayFormat(loadedImage);
		SDL_FreeSurface(loadedImage);
		if(optimizedImage != NULL){
			Uint32 colorkey = SDL_MapRGB(optimizedImage->format, transparency.r, transparency.g, transparency.b); 
			SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, colorkey); 
		}
    }
    return optimizedImage;
}

char* get_directory(){
	char path[260], c;
#if defined (__WIN32__)
    c = 0x5C;
#else
    c = 0x2F;
#endif
    getcwd(path, 260);
	stringstream fullpath;
	fullpath << path << c;
	return const_cast<char*> (fullpath.str().c_str());
}


stderr.txt has this line in it:
C:\Users\Joe\Documents\Bubbles\spritesheet.png

And I'm calling the function with this:
spritesheet = load_image("spritesheet.png");

Any help would be greatly appreciated.
Loading (non-BMP) images is an additional SDL library (SDL_image, I think). Are you sure that library is present on your Windows target?
Last edited on
You got lucky (or unlucky, depending on how you look at it) running it on the Mac, since there is an error. You're returning a local variable. You want something more like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void get_directory( char* path ) {
    char* c = "/";
#if defined (__WIN32__)
    c = "\\";
#endif
    getcwd( path, 260 );
    strcat( path, c );
}

// Call it like this:
    char path[260];
    get_directory( path );
    strcat( path, filename );
// Then just use filename, no need for a sstream object 


Yeah, IMG_Load is the SDL_Image function. The SDL one is SDL_LoadBitmap or something.
I have the libraries set up properly.

Ah, thanks.
I'll give that a go. I had some problems using strcat before.

EDIT:
Well, I tried doing it like that but I got the same problem.
path seems to contain the correct string rather than filename, but the image loading still doesn't work.
Last edited on
Well, this is the first I've looked directly at your code.

I'm concerned about line 31. Take a look at Hammurabi's code to see the order things are concatenated into the string. Or:
1
2
3
4
5
6
#ifdef __WIN32__
char sep = '\\';
#else
char sep = '/';
#endif
fullpath << sep << path;

Notice also that I used character literals instead of coding the ASCII value directly.

Hope this helps.
Topic archived. No new replies allowed.