SDL Toggling Windowed/Fullscreen Mode

Started by
3 comments, last by Xorcist 22 years, 2 months ago
Okay I've got a quick question... I have used this code to toggle the screen:
      
SDL_Surface *ToggleScreenMode(SDL_Surface *screen)
  {//Begin ToggleScreenMode


    Uint32 flags;
    flags = screen->flags;
    flags ^= SDL_FULLSCREEN;

    printf("Toggle: %s",(screen->flags & SDL_FULLSCREEN)
      ? "Fullscreen -> Windowed\n"
      : "Windowed -> Fullscreen\n");

    screen = CreateScreen(screen->w, screen->h, screen->format->BitsPerPixel, flags);
    return(screen);

  }//End ToggleScreenMode

  
and it seems to work okay when used in the format: screen = ToggleScreenMode(screen); *note CreateScreen is a custom function that returns a SDL_Surface. what I don't understand is why when the code is modified as such:
              
void ToggleScreenMode(SDL_Surface *screen)
  {//Begin ToggleScreenMode


    Uint32 flags;
    flags = screen->flags;
    flags ^= SDL_FULLSCREEN;

    printf("Toggle: %s",(screen->flags & SDL_FULLSCREEN)
      ? "Fullscreen -> Windowed\n"
      : "Windowed -> Fullscreen\n");

    screen = CreateScreen(screen->w, screen->h, screen->format->BitsPerPixel, flags);

  }//End ToggleScreenMode

      
and called like this: ToggleScreenMode(screen); it fails when trying to toggle back to windowed mode, after being toggled to fullscreen. According to my log it seems to try to toggle the fullscreen into fullscreen and then I get a segmentation error. It's probably some stupid mistake I'm overlooking, but I just don't get why it's not working... it should shouldn't it? Any help would be greatly appreciated. Edited by - Xorcist on February 6, 2002 12:36:52 AM Edited by - Xorcist on February 7, 2002 4:14:21 AM
Advertisement
Firstly, have you searched the SDL website and mailing list archives. Go to Google, enter "site:libsdl.org" and follow that with some relevant keywords. I guarantee you that this topic has come up several times before, and I'm pretty sure the way you're doing it is wrong.

Secondly, your code doesn't do what you expect it to. All you're doing in that modified version is changing the local variable called 'screen'. That has no effect on the variable that you passed to the ToggleScreenMode function. What you want is to pass the pointer or the surface by reference.

Edited by - Kylotan on February 7, 2002 12:36:33 PM
See that''s just it, I already did a search, and I found this:

http://archives.seul.org/pygame/users/Apr-2001/msg00009.html

which if you don''t feel like reading it says the built in toggle function is a bit unreliable and the best way to toggle is to just create a new surface with or without the FULLSCREEN flag. That''s what I was trying to do, I just created a new surface and set the old one equal to the new one and it seems to work fine.

P.S. Don''t worry about my modified code, I musta not bun thunkin''

I remember reading on the SDL list that you''re not supposed to mess with the screen surface because it is managed internally. But I dunno - if it works, fair enough.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]
Correct me if I''m wrong, but I think the second version, the "void ToggleScreenMode(SDL_Surface *screen)" version, is because you are only changing the temporary pointer ''screen''. Just change the function''s parameter to (SDL_Surface* &screen). That way you will modify the pointer that is passed to the function and should correct your problems.

This topic is closed to new replies.

Advertisement