I spent a few hours testing and experimenting different solutions, but at the end what worked was:
/* Maximize window using Windows API after glutCreateWindow() has been called */
HWND win_handle = FindWindow(0, L"Stackoverflow");
if (!win_handle)
{
printf("!!! Failed FindWindow\n");
return -1;
}
SetWindowLong(win_handle, GWL_STYLE, (GetWindowLong(win_handle, GWL_STYLE) | WS_MAXIMIZE));
ShowWindowAsync(win_handle, SW_SHOWMAXIMIZED);
/* Activate GLUT main loop */
glutMainLoop();
It's pretty simple, and I just want to empathize that you can only call glutMainLoop()
at the end.
Now, since you are looking for a cross-platform solution I must say that this approach is a terrible idea. Let me share a few other approaches:
Consider using Qt (a cross-platform C++ framework to build applications with GUI). Here's an example;
Consider writing individual platform code to retrieve the current monitor resolution. When glutInitWindowSize()
is called, you can pass the right values instead of trying to hack the window to maximize it;
Here's the source code to a minimal, complete and verifiable example (for Windows):
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <GL/glut.h>
int WIDTH = 480;
int HEIGHT = 480;
void initializeGL()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLfloat aspect = (GLfloat) WIDTH / HEIGHT;
gluPerspective(45, aspect, 1.0f, 500.0f);
glViewport(0, 0, WIDTH, HEIGHT);
glMatrixMode(GL_MODELVIEW);
glShadeModel( GL_SMOOTH );
glClearDepth( 1.0f );
glEnable( GL_DEPTH_TEST );
glDepthFunc( GL_LEQUAL );
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
glClearColor(0.0, 0.0, 0.0, 1.0);
}
void displayGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-3.0f);
glBegin(GL_TRIANGLES);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glEnd();
glutSwapBuffers();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(640, 480);
glutCreateWindow("Stackoverflow");
glutDisplayFunc(displayGL);
initializeGL();
/* Maximize window using Windows API */
HWND win_handle = FindWindow(0, L"Stackoverflow");
if (!win_handle)
{
printf("!!! Failed FindWindow\n");
return -1;
}
SetWindowLong(win_handle, GWL_STYLE, (GetWindowLong(win_handle, GWL_STYLE) | WS_MAXIMIZE));
ShowWindowAsync(win_handle, SW_SHOWMAXIMIZED);
/* Activate GLUT main loop */
glutMainLoop();
return 0;
}