Setting SDL environment variable in code

Started by
4 comments, last by VicTex 16 years, 10 months ago
Hello. I was interested in using DirectX as my video driver instead of the default windib. I understand that I can do this by setting the environment variable "SDL_VIDEODRIVER" to "directx", but I do not understand how to do this in my C++ code. I've tried _putenv("SDL_VIDEODRIVER=directx"); (after initializing SDL and its systems), but that doesn't seem to work. Here is a view of some of my code.

int main(int argc, char* args[])
{
	bool quit = false;
	if(Init() == false)
		return 1;

	_putenv("$SDL_VIDEODRIVER=directx"); // Attempt at setting environment variable

	SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF));
	User* player = new User();
        // Load starting files for player
	if(LoadBegFiles(player) == false)
		return 1;
	ApplySurface(100, 100, image, screen, NULL);
	if(SDL_Flip(screen) == -1)
		return 1;
	while(quit == false)
	{
		while(SDL_PollEvent(&event))
                {
                    if(event.type == SDL_QUIT)
                    {
                         // Quit the program.
                         quit = true;
                    }
	            if(event.type == SDL_KEYDOWN)
                    {
		         quit = true;
                    }
	}
	delete player;

        // To check video driver being used.
	char* driv = new char[20];
	driv = SDL_VideoDriverName(driv, 20);
	printf(driv);

	CleanUp();
	return 0;
}

bool Init()
{
    if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
        return false;
    screen = SDL_SetVideoMode(Screen_Width, Screen_Height, Screen_BPP,
                                SDL_HWSURFACE|SDL_FULLSCREEN);
    if(screen == NULL)
        return false;
    
    return true;
}


I am not sure if I need to link with DirectX in order to use the DirectX video driver. So far, I am only linking with the needed SDL lib's. And of course, I am just starting this project. Its just that I want to take care of this in the beginning instead of a million lines later. [wink] Could you please tell me how to do this in code (if at all possible)?
Advertisement
I *THINK* this might involve actually rebuilding SDL from source. If you have to temporarily set environment variables, I recommend you put them into a batch file or something. There's something very ugly about the exe doing that kind of stuff... but then, I come from the scripty world of Linux ;-).
Shouldn't it be _putenv("SDL_VIDEODRIVER=directx");? Note the lack of "$" in front of the variable name. I'm not a Windows type of guy so I'm really not sure about it, but I'd give it a try. To this UN*X-head, a "$" means "reading the value", not "setting it".

Also, you'll probably want to use SDL_putenv (defined in SDL_stdinc.h). _putenv is definitely not portable.
I've tried it without the "$" and I've also tried the function SDL_putenv without any luck. I am looking for information on using batch files to attempt the other solution recommended and if needed, I'll try to rebuild SDL from the source.
I don't think setting SDL_VIDEODRIVER is going to work after you've already SDL_Init( SDL_INIT_EVERYTHING )ed like you have, and definately not after you've already set up a window with SDL_SetScreenMode (again like you have).
Thank you MaulingMonkey. I've tried setting the environment variable before initilizing SDL and it worked. Thank you all for helping me with this problem.

This topic is closed to new replies.

Advertisement