SDL libraries & .c_str()

When SDL libraries are being used(compared to me commenting them out and testing stuff non-SDL components via the console) the portion of code that converts strings to const char* 's seems to be ignored and not convert them.

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <SDL/SDL.h>

const int Width = 70, Height = 95, Depth = 32, Max = 10;
SDL_Surface *screen = NULL, *bitmap[Max];

class SDL_Controls
{
	public:
		const char* cardPath;
		void SetPath(std::string);
};

int main(int argc, char*args[])
{
	SDL_Controls card[Max];
	SDL_Rect offset;
	SDL_Init(SDL_INIT_VIDEO);
	screen = SDL_SetVideoMode(Width*5, Height*3, Depth, SDL_SWSURFACE);
	SDL_Rect cardPlacement[Max];

	for (int x = 0; x<Max;x++)
	{
		bitmap[x] = NULL;
		if ((x == 0) || (x == 5))
			cardPlacement[x].x = 0;
		else if ((x == 1) || (x == 6))
			cardPlacement[x].x = Width;
		else if ((x == 2) || (x == 7))
			cardPlacement[x].x = Width*2;
		else if ((x == 3) || (x == 8))
			cardPlacement[x].x = Width*3;
		else
			cardPlacement[x].x = Width*4;
		if (x>4)
			cardPlacement[x].y = Height*2;
		else
			cardPlacement[x].y = 0;
		cardPlacement[x].h = Height;
		cardPlacement[x].w = Width;
		card[x].SetPath("card-BMPs/404.bmp");
	 	bitmap[x]= SDL_LoadBMP(card[x].cardpath);
		SDL_BlitSurface(bitmap[x],NULL,screen,&cardPlacement[x]);
		SDL_Flip(screen);
	}

	SDL_Delay(6000);
	SDL_Quit();
	return 0;
}

void SDL_Controls::SetPath(std::string path)
{
	cardPath=path.c_str();
}


I only get a black screen whereas i should get 10 of the "card-BMPs/404.bmp"s on the screen. It works properly if i put the link directly into the SDL_LoadBMP step however.

http://www.ehu.es/~mtpalezp/magia/cesta/4C.GIF is the used card, aside from me using a BMP file.
Are you sure path is valid? Try doing something basic to make sure like print it to std::cout.
cardPath=path.c_str(); You are copying a pointer of a temporary, use strcpy to copy cstrings.
EDIT: And allocate memory to cardPath.
(or just use std::strings)
Last edited on
cardPath=path.c_str(); You are copying a pointer of a temporary, use strcpy to copy cstrings.
EDIT: And allocate memory to cardPath.
(or just use std::strings)

Ah, for some reason I felt that it was absolutely necessary that I use const char* throughout this, but minor reworking with strings fixed it, thanks for the replies
Topic archived. No new replies allowed.