• Advertisement
Sign in to follow this  

Switching from Fullscreen to Window

This topic is 1923 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

Hi guys,
I'm new in OGL, I found how to Enter and Leave Fullscreen in two different solutions:
1. glutFullScreen() and glutLeaveFullScreen() (doesn't work ... maybe old dll)
2. glutEnterGameMode and glutLeaveGameMode
Now I have maybe badly constructed code, but works for me without fullscreen, if I want Fullscreen, I must change variables "width" and "height" and then run glutFullSreen method.
I want fullscreen on key F when I draw ogl window with menu (simply switch fullscreen/window on key "F").

Now I have:
[CODE]
// MAIN.cpp - MAIN PROGRAM
#include "main.h"
// Variables
float cam1=5; // Camera position
float x=4250,y=-30,z=-7390; // Map position
float carx=-4220,cary=0,carz=7210; // Car position
float width=800.0, height=600.0; // Window size
bool h1=false,k1=false,l1=false,f1=false; // F key for Help menu
// Font settings
const int font1=(int)GLUT_BITMAP_HELVETICA_12;
const int font2=(int)GLUT_BITMAP_HELVETICA_18;
// Light settings
GLfloat lightAmb[3] = {0.1f, 0.1f, 0.1f};
GLfloat lightDiff[3] = {1.0f, 1.0f, 1.0f};
GLfloat lightPos[4]={0.0f, 0.0f, 0.0f, 1.0f};
// Models variables
GLMmodel* car = NULL;
GLMmodel* map = NULL;
// Keyboard Function
void Keyboard(unsigned char key, int xx, int yy) {
glutPostRedisplay();
switch(key) {
case 'h': // Show/hide help menu
if(h1==false) h1=true;
else h1=false;
break;
case 'k': // Left-side camera
k1=true;
cam1=-103;
x=4400;
break;
case 'l': // Right-side camera
l1=true;
cam1=113;
x=4100;
break;
case 'f' : // Fullscreen ON/OFF
if(f1==false) {
f1=true;
}
else {
f1=false;
}
break;
case 27 : // Stop program
exit(0);
}
}
// Keyboard UP Function
void KeyboardUp(unsigned char key, int xx, int yy) {
switch(key) {
case 'k': // K key up
k1=false;
cam1=5;
x=4250;
break;
case 'l': // L key up
l1=false;
cam1=5;
x=4250;
break;
}
}
// Keyboard HOLD DOWN Function
void myIdleFunc(void) {
glutPostRedisplay();
if(k1) { // K key pressed
cam1=-103;
x=4400;
}
if(l1) { // L key pressed
cam1=113;
x=4100;
}
}
// Initializing models
void initModels(void) {
// Set car model
car = glmReadOBJ("Models/audi-tt.obj");
if (!car) exit(0);
glmFacetNormals(car);
glmVertexNormals(car, 90.0);
// Set map model
map = glmReadOBJ("Models/map.obj");
if (!map) exit(0);
glmFacetNormals(map);
glmVertexNormals(map, 90.0);
}
// Text Function
void renderBitmapString(float x, float y, void *font,string str) {
const char *c;
glRasterPos2f(x, y); // Text position
for (c=str.c_str(); *c != '\0'; c++) glutBitmapCharacter(font, *c);
}
// Display Function
void Display(void) {
glClearColor(0.3f, 0.5f, 0.9f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Set 3D
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, (int)width, (int)height);
gluPerspective(30, width/height, 0.1, 10000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Set camera
gluLookAt(cam1,27,130, 5,11,0, 0,1,0);
glutPostRedisplay();
// Light and Z-buffer
glPushAttrib(GL_ALL_ATTRIB_BITS);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmb);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiff);
// Draw Map and Car
glPushMatrix();
// load map obj
glPushMatrix();
glTranslatef(x,y,z);
glRotatef(-180, 0.0, 1.0, 0.0);
glmDraw(map, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
// load car obj
glPushMatrix();
glTranslatef(x+carx, y+cary, z+carz);
glRotatef(-180, 0.0, 1.0, 0.0);
glmDraw(car, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glPopAttrib();
glFlush();
// Set 2D
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,width,height,0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
// Draw text
glPushMatrix();
glColor3f(1, 1, 1);
renderBitmapString(10,20,(void *)font2,"Press 'H' for show/hide help");
glEnd();
glPopMatrix();
// Show help menu
if(h1==true) {
glTranslatef((float)((width-500)/2),(float)(500+((height-500)/2)),0);
glPushMatrix();
glColor3f(1, 1, 1);
glBegin(GL_QUADS);
glVertex3f(0.0f, -500.0f, 0.0f); // The bottom left corner
glVertex3f(0.0f, 0.0f, 0.0f); // The top left corner
glVertex3f(500.0f, 0.0f, 0.0f); // The top right corner
glVertex3f(500.0f, -500.0f, 0.0f); // The bottom right corner
glEnd();
glPopMatrix();
glPushMatrix();
glColor3f(0, 0, 0);
renderBitmapString(130,-440,(void *)font1,"P H Y S X C A R S I M U L A T O R");
renderBitmapString(190,-410,(void *)font2,"HELP MENU:");
renderBitmapString(60,-350,(void *)font2,"'K' = change to left-side camera");
renderBitmapString(60,-320,(void *)font2,"'L' = change to right-side camera");
renderBitmapString(60,-290,(void *)font2,"'ESC' = exit program");
renderBitmapString(60,-50,(void *)font1,"Author: Son Nguyen (NGU0013) Version: 1.0 , 2012");
glEnd();
glPopMatrix();
}
glutSwapBuffers();
}
// Main Function
int main(int argc, char **argv) {
cout << "Loading models ... ";
initModels();
cout << "DONE" << endl;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize((int)width, (int)height);
glutInitWindowPosition(0, 0);
glutCreateWindow("PhysX Car Simulator");
glutFullScreen();
initPhysX();
glutDisplayFunc(Display);
glutKeyboardFunc(Keyboard);
glutKeyboardUpFunc(KeyboardUp);
glutIdleFunc(myIdleFunc);
glutMainLoop();
deinitPhysX();
return 0;
}
[/CODE]

Thanks for help.

Share this post


Link to post
Share on other sites
Advertisement
After some googling I found solution:
on keyboard pressed "F":
[CODE]
if(fkey==false) {
fkey=true;
width=1366.0;
height=768.0;
glutFullScreen();
}
else {
fkey=false;
width=800.0;
height=600.0;
glutPositionWindow(0,0);
glutReshapeWindow(800,600);
}
[/CODE]
I have some troubles with reshape function that reshaped fullscreen to window, but instead of size 800x600 was same as fullscreen 1366x768 and when I moved glutPositionWindow before Reshape method it worked :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement