PDA

View Full Version : glut gamemode - strange behaviour



McBain
11-11-2001, 01:50 PM
I have strange problems using the glut gamemode
i start in windowed mode, and go to fullscreen mode
everything ok
back to window mode
ok
back to fullscreen mode -> crash
the video mode is switched but i dont see the opengl output, i do see
the part of the windows desktop that is visible in that mode
it seems the window has lost focus or so, rebooting is the onlmy
solution

i'm trying to solve the problem for 2 days now, withouth succes
i would be nice if someone could find the error or give me some pointers
where to look
included is the code + code of a working screenshot routine (tga files)
as reward for u guys taking a look at my code http://www.opengl.org/discussion_boards/ubb/smile.gif


(i can also be reached @ ares.lagae@cs.st.kuleuven.ac.be)

// GLApplication.cpp

#include <GL/glut.h>
#include "glapplication.h"
#include "gltools.h"
#include <stdio.h>

// -----------------------------
// General stuff
// -----------------------------

// application name
char *GLApplication::_appName;

char *GLApplication::getAppName(void) {
return _appName;
}

void GLApplication::setAppName(char *appName) {
_appName = appName;
}

// width, height, pos
int GLApplication::_width;

int GLApplication::getWidth(void) {
return _width;
}

void GLApplication::setWidth(int width) {
_width = width;
}

int GLApplication::_height;

int GLApplication::getHeight(void) {
return _height;
}

int GLApplication::_xPos;

int GLApplication::_yPos;

void GLApplication::setHeight(int height) {
_height = height;
}

// keyboard
bool GLApplication::_handlesKeyboardEvents;

bool GLApplication::handlesKeyboardEvents(void) {
return _handlesKeyboardEvents;
}

void GLApplication::handleKeyboardEvents(bool value) {
_handlesKeyboardEvents = value;
}

// mouse
bool GLApplication::_handlesMouseMotionEvents;

bool GLApplication::handlesMouseMotionEvents(void) {
return _handlesMouseMotionEvents;
}

void GLApplication::handleMouseMotionEvents(bool value) {
_handlesMouseMotionEvents = value;
}

bool GLApplication::_handlesMouseButtonEvents;

bool GLApplication::handlesMouseButtonEvents(void) {
return _handlesMouseButtonEvents;
}

void GLApplication::handleMouseButtonEvents(bool value) {
_handlesMouseButtonEvents = value;
}

// animation
bool GLApplication::_doesAnimation;

bool GLApplication::doesAnimation(void) {
return _doesAnimation;
}

void GLApplication::doAnimation(bool value) {
_doesAnimation = value;
}

// fullscreen
bool GLApplication::_fullscreen;

int GLApplication::_oldHeight;

int GLApplication::_oldWidth;

// fps
float GLApplication::_fps;
int GLApplication::_fpsFrame;
int GLApplication::_fpsTime;
int GLApplication::_fpsTimeBase;

// -----------------------------
// Init and run
// -----------------------------


// init
void GLApplication::init() {
_mouseState = MOUSE_UP;
_mouseOldXPos = -13;
_mouseOldYPos = -13;
_mouseRotX = 0;
_mouseRotY = 0;
_mouseRotZ = 0;
_fullscreen = 0;
_fpsFrame = 0;
_fpsTime = 0;
_fpsTimeBase = 0;
}

// run
void GLApplication::run(void) {
init();
glRun();
}

// -----------------------------
// GL init
// -----------------------------


// gl init function
void GLApplication::glInit(void) {
glEnable (GL_DEPTH_TEST);
//glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
}


// -----------------------------
// Display function
// -----------------------------


// gl scene display function
void GLApplication::glDisplayFunc(void) {
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
/* move our eye position 50 units away */
glTranslatef (0, 0, -50);
/* apply the rotation from the mouse */
glRotatef (_mouseRotX, 1.0, 0.0, 0.0);
glRotatef (_mouseRotY, 0.0, 1.0, 0.0);
glColor3f (0.5, 0.5, 0.5);
glBegin (GL_LINES);
glColor3f (0.5, 0.0, 0.0);
glVertex3f (-20.0, 0.0, 0.0);
glVertex3f (20.0, 0.0, 0.0);

/*glVertex3f (19.0, -1.0, 0.0);
glVertex3f (20.0, 0.0, 0.0);
glVertex3f (19.0, 1.0, 0.0);
glVertex3f (20.0, 0.0, 0.0);
glVertex3f (19.0, 0.0, -1.0);
glVertex3f (20.0, 0.0, 0.0);
glVertex3f (19.0, 0.0, 1.0);
glVertex3f (20.0, 0.0, 0.0);*/

glColor3f (0.0, 0.5, 0.0);
glVertex3f (0.0, 20.0, 0.0);
glVertex3f (0.0, -20.0, 0.0);

/*glVertex3f (-1.0, 19.0, 0.0);
glVertex3f (0.0, 20.0, 0.0);
glVertex3f (1.0, 19.0, 0.0);
glVertex3f (0.0, 20.0, 0.0);
glVertex3f (0.0, 19.0, -1.0);
glVertex3f (0.0, 20.0, 0.0);
glVertex3f (0.0, 19.0, 1.0);
glVertex3f (0.0, 20.0, 0.0);*/

glColor3f (0.0, 0.0, 0.5);
glVertex3f (0.0, 0.0, -20.0);
glVertex3f (0.0, 0.0, 20.0);

/*glVertex3f (-1.0, 0.0, 19.0);
glVertex3f (0.0, 0.0, 20.0);
glVertex3f (1.0, 0.0, 19.0);
glVertex3f (0.0, 0.0, 20.0);
glVertex3f (0.0, -1.0, 19.0);
glVertex3f (0.0, 0.0, 20.0);
glVertex3f (0.0, 1.0, 19.0);
glVertex3f (0.0, 0.0, 20.0);*/

glEnd ();

/*glPushMatrix ();
glRotatef (_animationRotate, 1, 1, 0);
glTranslatef (-5.0, 10.0, 5.0);
glColor3f (1.0, 0.0, 0.0);
glutWireSphere (5.0, 6.0, 6.0);
glPopMatrix ();*/

/*glPushMatrix ();
glRotatef (_animationRotate, -1, 1, 0);
glTranslatef (5.0, 10.0, -5.0);
glColor3f (0.0, 1.0, 0.0);
glutWireSphere (5.0, 6.0, 6.0);
glPopMatrix ();*/

/*char fpsString[80]; // make sure the array can hold the string,
otherwise the application will crash
sprintf(fpsString, "fps: %f", _fps);
GLTools::drawText(10, getHeight() - 10 - 8, fpsString,
GLUT_BITMAP_8_BY_13, 1.0f, 1.0f, 1.0f);

GLTools::drawText(10, 10, "controls: mouse, a(nimation), q(uit),
f(ullscreen), s(screenshot)", GLUT_BITMAP_8_BY_13, 1.0f, 1.0f, 1.0f);*/


glutSwapBuffers();
}


// -----------------------------
// Resize function
// -----------------------------

// gl window resize function
void GLApplication::glResizeFunc(int width, int height) {
printf("GLApplication::glResizeFunc(): resizing to %dx%d...\n", width,
height);
if (height == 0) {
height = 1;
}
_width = width;
_height = height;

glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glViewport (0, 0, _width, _height);
gluPerspective (90, 1.0f * _width / _height, 1, 9999);

//glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
//glutPostRedisplay ();
}

// -----------------------------
// Keyboard stuff
// -----------------------------

// gl keyboard event handler
void GLApplication::glKeyboardFunc(unsigned char key, int x, int y) {
printf("GLApplication::glKeyboardFunc(): key %d pressed...\n", key);
if (_handlesKeyboardEvents) {
switch (key) {
case 'f':
_fullscreen = !_fullscreen;
if (_fullscreen) {
printf("beware... there is no return from the fullscreen
world...\n");
// save parameters
/*_xPos = glutGet((GLenum)GLUT_WINDOW_X);
_yPos = glutGet((GLenum)GLUT_WINDOW_Y);
_oldWidth = glutGet((GLenum)GLUT_WINDOW_WIDTH);
_oldHeight = glutGet((GLenum)GLUT_WINDOW_HEIGHT);*/
// go to fullscreen
glutGameModeString("800x600:16@60");
if (glutGameModeGet(GLUT_GAME_MODE_POSSIBLE)) {
glutEnterGameMode();
test();
}
else {
_fullscreen = !_fullscreen;
printf("fullscreen mode not available - buy new hardware...\n");
}
//glutFullScreen();
}
else {
/*_width = _oldWidth; _height = _oldHeight;*/
if (glutGameModeGet(GLUT_GAME_MODE_ACTIVE) != 0)
glutLeaveGameMode();
//glutInitWindowSize (getWidth(), getHeight());
//glutInitWindowPosition(_xPos, _yPos);
//init();

}
break;
case 'q':
case 'Q':
// this is a good point to call cleanup methods and free memory
// all code below GLApplication::run(); will not be executed
// if the window is closed instead of the q button, this will not be
executed
printf("GLApplication::glKeyboardFunc(): application is about to
exit...\n");
exit (1);
break;
case 'a':
case 'A':
_doesAnimation = !_doesAnimation;
break;
case 's':
case 'S':
GLTools::screenShot(_width, _height);
break;
}
//glutPostRedisplay ();
}
}

// -----------------------------
// Mouse stuff
// -----------------------------

// variables for mouse navigation
int GLApplication::_mouseState;
int GLApplication::_mouseOldXPos;
int GLApplication::_mouseOldYPos;
float GLApplication::_mouseRotX;
float GLApplication::_mouseRotY;
float GLApplication::_mouseRotZ;


// gl mouse motion event handler
void GLApplication::glMouseMotionFunc(int x, int y) {
/* if (_handlesMouseMotionEvents) {
if (_mouseState == MOUSE_DOWN) {
_mouseRotX -= ((_mouseOldYPos - y) * 180.0f) / 100.0f;
if (_mouseRotX > 360 &#0124; &#0124; _mouseRotX < -360) _mouseRotX = 0.0f;
_mouseRotY -= ((_mouseOldXPos - x) * 180.0f) / 100.0f;
if (_mouseRotY > 360 &#0124; &#0124; _mouseRotY < -360) _mouseRotY = 0.0f;
}
_mouseOldXPos = x;
_mouseOldYPos = y;
//glutPostRedisplay ();
}*/
}

// gl mouse button event handler
void GLApplication::glMouseButtonFunc(int button, int state, int x, int
y) {
/* if (_handlesMouseButtonEvents) {
if(_mouseState == GLUT_DOWN) {
switch(button) {
case GLUT_LEFT_BUTTON:
case GLUT_RIGHT_BUTTON:
_mouseState = MOUSE_DOWN;
_mouseOldXPos = x;
_mouseOldYPos = y;
break;
}
}
else if (_mouseState == GLUT_UP) {
_mouseState = MOUSE_UP;
}
}*/
}


// -----------------------------
// Animation stuff
// -----------------------------

// variables for animation
float GLApplication::_animationRotate;


// gl animation function
void GLApplication::glAnimationFunc(void) {
/*if (_doesAnimation) {
_animationRotate += 1.0f;
if (_animationRotate > 360) {
_animationRotate = 0.0f;
}
//glutPostRedisplay ();
}*/
/*_fpsFrame++;
_fpsTime = glutGet(GLUT_ELAPSED_TIME);
if (_fpsTime - _fpsTimeBase > 1000) {
_fps = _fpsFrame * 1000.0 / (_fpsTime - _fpsTimeBase);
_fpsTimeBase = _fpsTime;
_fpsFrame = 0;
}*/
}


// -----------------------------
// Set up window
// -----------------------------

void GLApplication::test(void) {
glutIgnoreKeyRepeat(1);
glutKeyboardFunc(glKeyboardFunc);
glutDisplayFunc(glDisplayFunc);
glutReshapeFunc(glResizeFunc);
glutMotionFunc(glMouseMotionFunc);
glutMouseFunc(glMouseButtonFunc);
glutIdleFunc(glAnimationFunc);
glInit();
}

// gl run the application
void GLApplication::glRun(void) {

glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

glutInitWindowPosition (100, 100);
glutInitWindowSize (getWidth(), getHeight());
glutCreateWindow (getAppName());
//glutSetCursor(GLUT_CURSOR_CROSSHAIR);

test();

glutMainLoop();
}