Sign in to follow this  

Setting SDL environment variable in code

This topic is 3841 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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)?

Share this post


Link to post
Share on other sites
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 ;-).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 3841 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this