SDL Application problems

Hi again,

My SDL Application, when run, seems to work OK, but it never actually appears. My IDE (Code::Blocks) says it's running, and it appears as a running process in Task Manager, but it doesn't appear (neither does it modify a file, as it is supposed to during its run, but that's irrelevant).

I believe the problem may be this:
SDL_Surface *man[12];
Not only am I unsure how to initialize the entire vector to NULL, but I remember reading that arrays of pointers are "bad" somewhere...

I later set their values by using a for loop that gives them each a path to a file, like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    int x=0;
    std::stringstream ss;
    char path[30];
    std::string partialPath="resources/sprites/man/";
    for (x=0; x<12; x++) {
        ss<<partialPath<<"man"<<(x+1)<<".png";
        ss>>path;
        man[x]=IMG_Load(path);
        ss.clear();
    }

    for (x=0; x<12; x++) {
        if (man[x]==NULL) {
            return false;
        }
    }


This seemed the most space efficient method of doing it: feeding an std::stringstream the path in segments, then using that path to load the image.

Is there anything wrong with this?

If so, how do I rectify my error?

Thanks,
hnefatl
Oops, seems when i declared path to be 30 chars long (I chose 30 as a random number that I guessed would be enough spaces to fit the path), the path size was actually 30 or 31 chars long. What are the chances?

Just checking it works now.

hnefatl
Yup, it works. Sorry to bother you all!

Thanks,
hnefatl
You should avoid char arrays. And actually, there is no need to extract the string from a stringstream with the >> operator. The 'str' function is for just that.

You could do this without the extra char array and possibly without any memory copies with this:

1
2
3
4
5
    for (x=0; x<12; x++) {
        ss<<partialPath<<"man"<<(x+1)<<".png";
        man[x]=IMG_Load( ss.str().c_str() );
        ss.clear();
    }
I do try to avoid them, but IMG_Load() only takes char*, so I can't feed it a string...

Thanks, I'd forgotten about the str() and c_str() functions.

Thanks,
hnefatl
Topic archived. No new replies allowed.