4

I don't understand why when I press 'f' it enters into fullscreen but does not exit out of full screen. In the beginning of this method I have set bool fullscreen = false;

Here is the code for my toggle:

case 'f': //toggle screenmode
    if(!fullscreen){
        glutFullScreen();
        fullscreen = true;
    } else if(fullscreen){
        glutReshapeWindow(1200, 900);
        glutPositionWindow(0,0);
        fullscreen = false;
    }
    break;
genpfault
  • 51,148
  • 11
  • 85
  • 139
DorkMonstuh
  • 819
  • 2
  • 19
  • 38
  • "does not exit out of full screen at the top of this method" - what do you mean by this? – sashoalm Oct 26 '12 at 09:37
  • When I press f it goes into full screen but can't go back to its original windowed state? if that makes sense – DorkMonstuh Oct 26 '12 at 09:39
  • 1
    You can switch to freeglut and use [LeaveFullScreen/FullscreenToggle](http://freeglut.sourceforge.net/docs/api.php#Window). – Cfr Oct 26 '12 at 09:42
  • 1
    Btw: you don't need `else if(fullscreen)` but just `else` – Sdra Oct 26 '12 at 09:42

7 Answers7

5

at the top of this method I have set bool fullscreen = false;

Every time you press a key, GLUT will call your keyboard handler. And at the top of your keyboard handler, you create a bool variable named fullscreen and set its value to false. This happens regardless of whether you're in full-screen mode or not. Every time you press a key, this will happen.

If you want to retain a boolean variable that actually tracks whether you're currently fullscreen, then you need to use a global. And you need to not set it at the start of the function. You set it once when you create the window, and you only set it again when you change the fullscreen status of the window.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
3

To restore the original window size

... switch the calling order of ReshapeWindow and PositionWindow to

glutPositionWindow(0,0);
glutReshapeWindow(1200, 900);

Otherwise it will go back to windowed mode, but not adapt to the window size you specified!

euphrat
  • 146
  • 7
1

The problem is not in the code you posted above as according to glutFullScreen specification the window should exit fullscreen mode once glutReshapeWindow or glutPositionWindow is being called.

at the top of this method I have set bool fullscreen = false;

I bet you set this inside the same function (not as a global variable) rendering the variable always being false when you press f

Stacker
  • 1,080
  • 14
  • 20
0

Instead of defining the bool at the beginning of the method, you have to define a global variable. Otherwise each time that method is called, it will set the fullscreen bool to 0, and think that it's not in fullscreen mode. Also, you may want to take not of euphrat's answer, he makes a valid point about the method organization.

Jordan LaPrise
  • 1,088
  • 1
  • 11
  • 20
0

Try this:

case 'f': //toggle screenmode
    fullScreen = !fullScreen;
    if (fullScreen) {
        glutFullScreen();
    } else {
        glutReshapeWindow(1200, 900);
        glutPositionWindow(0,0);
    }
    break;
Meyer
  • 1,662
  • 7
  • 21
  • 20
Esau
  • 21
  • 3
0

While it might not answer the question directly, I found it an excellent place to post a go-to full-screen and exit source code.

Switch and restore full screen with <GL/glut.h>

myGame.c

...
glutSpecialFunc(handleSpecial);

void handleSpecial(int key, int x, int y) {
  oglHandleFullScreen(key, x, y);
}
...

If you are looking forward to responding to a keyboard event instead (glutKeyboardFunc), make sure to change the signature of the below oglHandleFullScreen to (unsigned char key, int x, int y).

fullscreen.h

void oglHandleFullScreen(int key, int x, int y);
void oglWindowed(int positionX, int positionY, int width, int height);
void oglFullScreen();

fullscreen.c

#include <GL/glut.h>
#include "fullscreen.h"

int isFullScreen = 0;
int previousPosition[2] = { 0, 0 };
int previousSize[2] = { 100, 100 };

void oglHandleFullScreen(int key, int x, int y) {
  if (key != GLUT_KEY_F11) { // Respond to F11 key (glutSpecialFunc).
    return;
  }

  if (isFullScreen) {
    oglWindowed(previousPosition[0], previousPosition[1],
                previousSize[0], previousSize[1]);
  } else {
    previousPosition[0] = glutGet(GLUT_WINDOW_X);
    previousPosition[1] = glutGet(GLUT_WINDOW_Y);
    previousSize[0] = glutGet(GLUT_WINDOW_WIDTH);
    previousSize[1] = glutGet(GLUT_WINDOW_HEIGHT);
    oglFullScreen();
  }
  isFullScreen = !isFullScreen;
}

void oglWindowed(int positionX, int positionY, int width, int height) {
  glutReshapeWindow(width, height);
  glutPositionWindow(positionX, positionY);
}

void oglFullScreen() {
  glutFullScreen();
}
zurfyx
  • 31,043
  • 20
  • 111
  • 145
0

In this case, you may want to add a static keyword before the bool to make this variable initialized only once.

A safer approach would be to make fullscreen global, since static local variables may be implemented differently across compilers and c++ standards.

Edit to add code snippet:

static bool fullscreen = false;

This should fix the problem of initializing the variable every call to the function, and by c++11 standard should be thread-safe

(Many compilers may say otherwise. Visual Studio only supported this feature of c++11 in VS2015 according to another question's accepted answer: Is static init thread-safe with VC2010?)

Better option may be using a global variable (Wrapping into a class is not good for this purpose).

Edit: Also, despite being simple in syntax, this c++11 feature is costly when called frequently.(See Cost of thread-safe local static variable initialization in C++11?)

(A key down is not that frequent as drawing vertices at high FPS, though... Do NOT use this when performance-critical...)

shangjiaxuan
  • 205
  • 1
  • 10