2011-09-04 23:30:48 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <GL/freeglut.h>
|
|
|
|
|
2012-07-23 16:52:55 +03:00
|
|
|
int nWindow, nChildWindow = -1;
|
2011-09-04 23:30:48 +03:00
|
|
|
int nLoopMain = 0;
|
2012-07-23 16:52:55 +03:00
|
|
|
GLboolean bChildPosDone = GL_FALSE, bChildSizeDone = GL_FALSE;
|
|
|
|
|
2011-09-04 23:30:48 +03:00
|
|
|
void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY );
|
|
|
|
void Redisplay();
|
2012-11-23 10:21:24 +02:00
|
|
|
void Reshape(int width, int height);
|
|
|
|
void Position(int x, int y);
|
2013-03-01 12:31:34 +02:00
|
|
|
void WindowStatus(int state);
|
2011-09-04 23:30:48 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void DrawQuad()
|
|
|
|
{
|
2013-04-04 04:56:39 +03:00
|
|
|
int width = glutGet(GLUT_WINDOW_WIDTH);
|
|
|
|
int height = glutGet(GLUT_WINDOW_HEIGHT);
|
2012-07-23 16:52:55 +03:00
|
|
|
|
2011-09-04 23:30:48 +03:00
|
|
|
glBegin(GL_QUADS);
|
2013-04-04 04:56:39 +03:00
|
|
|
glVertex2d(width*.25, height*.75);
|
|
|
|
glVertex2d(width*.75, height*.75);
|
|
|
|
glVertex2d(width*.75, height*.25);
|
|
|
|
glVertex2d(width*.25, height*.25);
|
2011-09-04 23:30:48 +03:00
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
|
2013-03-01 12:31:34 +02:00
|
|
|
void UnhideTimer(int window)
|
|
|
|
{
|
|
|
|
glutSetWindow(window);
|
|
|
|
glutShowWindow();
|
|
|
|
}
|
|
|
|
|
2013-04-04 04:56:39 +03:00
|
|
|
void ChangeTitleTimer(int unused)
|
|
|
|
{
|
|
|
|
glutSetIconTitle("new icon title");
|
|
|
|
glutSetWindowTitle("new test title");
|
|
|
|
}
|
|
|
|
|
2011-09-04 23:30:48 +03:00
|
|
|
void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY )
|
|
|
|
{
|
2012-07-23 16:52:55 +03:00
|
|
|
switch (cChar)
|
|
|
|
{
|
|
|
|
case 27:
|
2011-09-04 23:30:48 +03:00
|
|
|
glutLeaveMainLoop();
|
|
|
|
|
2012-07-23 16:52:55 +03:00
|
|
|
break;
|
|
|
|
|
2011-09-04 23:30:48 +03:00
|
|
|
|
2012-07-23 16:52:55 +03:00
|
|
|
case 'f':
|
|
|
|
case 'F':
|
|
|
|
printf("main window toggle fullscreen\n");
|
2011-09-04 23:30:48 +03:00
|
|
|
glutFullScreenToggle();
|
|
|
|
|
2012-07-23 16:52:55 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 'r':
|
|
|
|
case 'R':
|
2012-11-23 10:03:20 +02:00
|
|
|
if (nChildWindow!=-1 && cChar=='r') /* Capital R always resizes the main window*/
|
2012-07-23 16:52:55 +03:00
|
|
|
{
|
|
|
|
glutSetWindow(nChildWindow);
|
|
|
|
printf("child window resize\n");
|
|
|
|
if (!bChildSizeDone)
|
|
|
|
glutReshapeWindow(glutGet(GLUT_WINDOW_WIDTH)+50,glutGet(GLUT_WINDOW_HEIGHT)+50);
|
|
|
|
else
|
|
|
|
glutReshapeWindow(glutGet(GLUT_WINDOW_WIDTH)-50,glutGet(GLUT_WINDOW_HEIGHT)-50);
|
|
|
|
bChildSizeDone = !bChildSizeDone;
|
|
|
|
}
|
2011-09-04 23:30:48 +03:00
|
|
|
else
|
2012-07-23 16:52:55 +03:00
|
|
|
{
|
2012-11-23 10:03:20 +02:00
|
|
|
glutSetWindow(nWindow);
|
2012-07-23 16:52:55 +03:00
|
|
|
printf("main window resize\n");
|
2013-04-04 04:56:39 +03:00
|
|
|
if (glutGet(GLUT_WINDOW_WIDTH)<400)
|
2012-07-23 16:52:55 +03:00
|
|
|
glutReshapeWindow(600,300);
|
|
|
|
else
|
|
|
|
glutReshapeWindow(300,300);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 'm':
|
|
|
|
case 'M':
|
2012-11-23 10:03:20 +02:00
|
|
|
if (nChildWindow!=-1 && cChar=='m') /* Capital M always moves the main window*/
|
2012-07-23 16:52:55 +03:00
|
|
|
{
|
|
|
|
glutSetWindow(nChildWindow);
|
|
|
|
/* The window position you request is relative to the top-left
|
|
|
|
* corner of the client area of the parent window.
|
|
|
|
*/
|
|
|
|
if (!bChildPosDone)
|
|
|
|
glutPositionWindow(glutGet(GLUT_WINDOW_X)+50,glutGet(GLUT_WINDOW_Y)+50);
|
|
|
|
else
|
|
|
|
glutPositionWindow(glutGet(GLUT_WINDOW_X)-50,glutGet(GLUT_WINDOW_Y)-50);
|
|
|
|
bChildPosDone = !bChildPosDone;
|
|
|
|
}
|
2011-09-04 23:30:48 +03:00
|
|
|
else
|
2012-07-23 16:52:55 +03:00
|
|
|
{
|
2012-11-23 10:03:20 +02:00
|
|
|
glutSetWindow(nWindow);
|
2012-07-23 16:52:55 +03:00
|
|
|
printf("main window position\n");
|
|
|
|
/* The window position you request is the outer top-left of the window,
|
|
|
|
* the client area is at a different position if the window has borders
|
|
|
|
* and/or a title bar.
|
|
|
|
*/
|
2013-04-04 04:56:39 +03:00
|
|
|
if (glutGet(GLUT_WINDOW_X)<400)
|
2012-07-23 16:52:55 +03:00
|
|
|
glutPositionWindow(600,300);
|
|
|
|
else
|
|
|
|
glutPositionWindow(300,300);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
2013-04-04 04:56:39 +03:00
|
|
|
case 'd':
|
|
|
|
case 'D':
|
|
|
|
if (nChildWindow!=-1 && cChar=='d') /* Capital D always moves+resizes the main window*/
|
|
|
|
{
|
|
|
|
glutSetWindow(nChildWindow);
|
|
|
|
if (!bChildPosDone)
|
|
|
|
glutPositionWindow(glutGet(GLUT_WINDOW_X)+50,glutGet(GLUT_WINDOW_Y)+50);
|
|
|
|
else
|
|
|
|
glutPositionWindow(glutGet(GLUT_WINDOW_X)-50,glutGet(GLUT_WINDOW_Y)-50);
|
|
|
|
bChildPosDone = !bChildPosDone;
|
|
|
|
if (!bChildSizeDone)
|
|
|
|
glutReshapeWindow(glutGet(GLUT_WINDOW_WIDTH)+50,glutGet(GLUT_WINDOW_HEIGHT)+50);
|
|
|
|
else
|
|
|
|
glutReshapeWindow(glutGet(GLUT_WINDOW_WIDTH)-50,glutGet(GLUT_WINDOW_HEIGHT)-50);
|
|
|
|
bChildSizeDone = !bChildSizeDone;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (glutGet(GLUT_WINDOW_X)<400)
|
|
|
|
glutPositionWindow(600,300);
|
|
|
|
else
|
|
|
|
glutPositionWindow(300,300);
|
|
|
|
if (glutGet(GLUT_WINDOW_WIDTH)<400)
|
|
|
|
glutReshapeWindow(600,300);
|
|
|
|
else
|
|
|
|
glutReshapeWindow(300,300);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
2012-07-23 16:52:55 +03:00
|
|
|
case 'c':
|
|
|
|
case 'C':
|
|
|
|
if (nChildWindow==-1)
|
|
|
|
{
|
2013-04-04 04:56:39 +03:00
|
|
|
int width = glutGet(GLUT_WINDOW_WIDTH);
|
|
|
|
int height = glutGet(GLUT_WINDOW_HEIGHT);
|
|
|
|
|
2012-07-23 16:52:55 +03:00
|
|
|
/* open child window */
|
|
|
|
printf("open child window\n");
|
|
|
|
|
2013-04-04 04:56:39 +03:00
|
|
|
nChildWindow = glutCreateSubWindow(nWindow,(int)(width*.35),(int)(height*.35),(int)(width*.3),(int)(height*.3));
|
2012-07-23 16:52:55 +03:00
|
|
|
glutKeyboardFunc( SampleKeyboard );
|
|
|
|
glutDisplayFunc( Redisplay );
|
|
|
|
glutReshapeFunc( Reshape );
|
2012-11-23 10:21:24 +02:00
|
|
|
glutPositionFunc( Position );
|
2013-03-01 12:31:34 +02:00
|
|
|
glutWindowStatusFunc( WindowStatus );
|
2012-07-23 16:52:55 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* close child window */
|
|
|
|
printf("close child window\n");
|
|
|
|
glutSetWindow(nWindow);
|
|
|
|
glutDestroyWindow(nChildWindow);
|
|
|
|
nChildWindow = -1;
|
|
|
|
bChildSizeDone = GL_FALSE;
|
|
|
|
bChildPosDone = GL_FALSE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
2013-03-01 12:31:34 +02:00
|
|
|
case 'i':
|
|
|
|
case 'I':
|
|
|
|
glutIconifyWindow();
|
2013-04-04 04:56:39 +03:00
|
|
|
glutTimerFunc(1500, ChangeTitleTimer, 0);
|
2013-03-01 12:31:34 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 'h':
|
|
|
|
case 'H':
|
|
|
|
if (nChildWindow!=-1 && cChar=='h') /* Capital H always hides the main window*/
|
|
|
|
{
|
|
|
|
glutSetWindow(nChildWindow);
|
|
|
|
glutTimerFunc(2000, UnhideTimer, nChildWindow);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glutSetWindow(nWindow);
|
|
|
|
glutTimerFunc(2000, UnhideTimer, nWindow);
|
|
|
|
}
|
|
|
|
glutHideWindow();
|
2014-08-27 08:47:05 +03:00
|
|
|
break;
|
2013-04-04 14:04:17 +03:00
|
|
|
|
|
|
|
case 'p':
|
|
|
|
case 'P':
|
|
|
|
if (nChildWindow!=-1 && cChar=='p') /* Capital P always changes pointer for the main window*/
|
|
|
|
{
|
|
|
|
glutSetWindow(nChildWindow);
|
2017-06-05 21:33:52 +03:00
|
|
|
if (glutGet(GLUT_WINDOW_CURSOR)==GLUT_CURSOR_TOP_SIDE)
|
|
|
|
{
|
|
|
|
glutSetCursor(GLUT_CURSOR_RIGHT_ARROW);
|
|
|
|
printf("reverting child window cursor\n");
|
|
|
|
}
|
2013-04-04 14:04:17 +03:00
|
|
|
else
|
2017-06-05 21:33:52 +03:00
|
|
|
{
|
|
|
|
glutSetCursor(GLUT_CURSOR_TOP_SIDE);
|
|
|
|
printf("changing child window cursor\n");
|
|
|
|
}
|
2013-04-04 14:04:17 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glutSetWindow(nWindow);
|
|
|
|
if (glutGet(GLUT_WINDOW_CURSOR)==GLUT_CURSOR_CYCLE)
|
2017-06-05 21:33:52 +03:00
|
|
|
{
|
|
|
|
glutSetCursor(GLUT_CURSOR_RIGHT_ARROW);
|
|
|
|
printf("reverting main window cursor\n");
|
|
|
|
}
|
2013-04-04 14:04:17 +03:00
|
|
|
else
|
2017-06-05 21:33:52 +03:00
|
|
|
{
|
|
|
|
glutSetCursor(GLUT_CURSOR_CYCLE);
|
|
|
|
printf("changing main window cursor\n");
|
|
|
|
}
|
2013-04-04 14:04:17 +03:00
|
|
|
}
|
2014-08-27 08:47:05 +03:00
|
|
|
break;
|
2013-04-04 14:04:17 +03:00
|
|
|
|
2012-07-23 16:52:55 +03:00
|
|
|
default:
|
|
|
|
break;
|
2011-09-04 23:30:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Idle(void)
|
|
|
|
{
|
|
|
|
glutPostRedisplay();
|
|
|
|
}
|
|
|
|
|
2012-11-23 09:41:39 +02:00
|
|
|
void Reshape(int width, int height)
|
2011-09-04 23:30:48 +03:00
|
|
|
{
|
2012-07-23 16:52:55 +03:00
|
|
|
int win = glutGetWindow();
|
|
|
|
|
2013-04-04 04:56:39 +03:00
|
|
|
printf("reshape %s, client area: %dx%d\n",win==nWindow?"main":"child",
|
2012-11-23 09:41:39 +02:00
|
|
|
width, height);
|
2011-09-04 23:30:48 +03:00
|
|
|
|
2012-11-23 09:41:39 +02:00
|
|
|
glViewport(0,0,width,height);
|
2011-09-04 23:30:48 +03:00
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity();
|
2012-11-23 09:41:39 +02:00
|
|
|
gluOrtho2D(0,width,0,height);
|
2012-07-23 16:52:55 +03:00
|
|
|
|
|
|
|
if (win==nWindow && nChildWindow!=-1)
|
|
|
|
{
|
2013-04-04 04:56:39 +03:00
|
|
|
/* Put child window in right place */
|
|
|
|
int x = (int)(width*.35), y=(int)(height*.35), w=(int)(width*.3), h = (int)(height*.3);
|
|
|
|
if (bChildPosDone)
|
|
|
|
{
|
|
|
|
x += 50;
|
|
|
|
y += 50;
|
|
|
|
}
|
|
|
|
if (bChildSizeDone)
|
|
|
|
{
|
|
|
|
w += 50;
|
|
|
|
h += 50;
|
|
|
|
}
|
2012-07-23 16:52:55 +03:00
|
|
|
glutSetWindow(nChildWindow);
|
2013-04-04 04:56:39 +03:00
|
|
|
glutPositionWindow(x,y);
|
|
|
|
glutReshapeWindow(w,h);
|
2012-07-23 16:52:55 +03:00
|
|
|
glutSetWindow(nWindow);
|
|
|
|
}
|
2011-09-04 23:30:48 +03:00
|
|
|
}
|
|
|
|
|
2012-11-23 09:41:39 +02:00
|
|
|
void Position(int x, int y)
|
|
|
|
{
|
|
|
|
int win = glutGetWindow();
|
|
|
|
|
2013-04-04 04:56:39 +03:00
|
|
|
printf("position, %s: (%d,%d)\n",win==nWindow?"top-left (non-client) of main":"top-left of child relative to parent",
|
2012-11-23 09:41:39 +02:00
|
|
|
x, y);
|
|
|
|
}
|
|
|
|
|
2013-03-01 12:31:34 +02:00
|
|
|
void WindowStatus(int state)
|
|
|
|
{
|
|
|
|
int win = glutGetWindow();
|
|
|
|
printf("windowstatus (win %i): %i\n",win,state);
|
|
|
|
}
|
|
|
|
|
2011-09-04 23:30:48 +03:00
|
|
|
void Redisplay(void)
|
|
|
|
{
|
2012-07-23 16:52:55 +03:00
|
|
|
int win = glutGetWindow();
|
2014-08-27 08:47:05 +03:00
|
|
|
int viewport[4];
|
2012-07-23 16:52:55 +03:00
|
|
|
|
|
|
|
if (win==nWindow)
|
|
|
|
{
|
|
|
|
glClearColor(.2f,0.f,0.f,0.f);
|
|
|
|
glColor3f(1,1,1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* child window */
|
|
|
|
glClearColor(.0f,.2f,0.f,0.f);
|
|
|
|
glColor3f(.5,.5,.5);
|
|
|
|
glutPostWindowRedisplay(nWindow);
|
|
|
|
}
|
2011-09-04 23:30:48 +03:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
DrawQuad();
|
|
|
|
|
2014-08-27 08:47:05 +03:00
|
|
|
if (win==nWindow)
|
|
|
|
{
|
|
|
|
glColor3f(1, 1, 0);
|
|
|
|
glGetIntegerv(GL_VIEWPORT, viewport);
|
|
|
|
glRasterPos2i(2, -glutBitmapHeight(GLUT_BITMAP_9_BY_15)+3+viewport[3]);
|
|
|
|
glutBitmapString(GLUT_BITMAP_9_BY_15, (unsigned char*)"press f/r/m/d/c/i/h/p");
|
|
|
|
}
|
|
|
|
|
2011-09-04 23:30:48 +03:00
|
|
|
glutSwapBuffers();
|
2012-07-23 16:52:55 +03:00
|
|
|
glutPostWindowRedisplay(win);
|
2011-09-04 23:30:48 +03:00
|
|
|
}
|
|
|
|
|
2013-03-19 18:34:21 +02:00
|
|
|
void Timer(int unused)
|
|
|
|
{
|
|
|
|
int win = glutGetWindow();
|
2013-04-04 04:56:39 +03:00
|
|
|
int x, y;
|
|
|
|
int width, height;
|
2012-11-23 10:03:20 +02:00
|
|
|
int border, caption;
|
|
|
|
|
2013-04-04 04:56:39 +03:00
|
|
|
x = glutGet(GLUT_WINDOW_X);
|
|
|
|
y = glutGet(GLUT_WINDOW_Y);
|
|
|
|
width = glutGet(GLUT_WINDOW_WIDTH);
|
|
|
|
height = glutGet(GLUT_WINDOW_HEIGHT);
|
2012-11-23 10:03:20 +02:00
|
|
|
border = glutGet(GLUT_WINDOW_BORDER_WIDTH);
|
|
|
|
caption = glutGet(GLUT_WINDOW_HEADER_HEIGHT);
|
|
|
|
/* returned position is top-left of client area, to get top-left of
|
|
|
|
* of window you'll need to add the size of the border and caption
|
|
|
|
* of the current window (can be 0).
|
|
|
|
* Note that the window position is not necessarily positive (e.g.
|
|
|
|
* when the window is on a monitor to the left of the primary monitor
|
|
|
|
* or simply when maximized--try pressing the maximize button).
|
|
|
|
* the returned size is the size of the client area
|
|
|
|
* Note that the top-left of a child window is relative to the
|
|
|
|
* top-left of the client area of the parent.
|
|
|
|
*/
|
|
|
|
/* printf("window border: %dpx, caption: %dpx\n",border,caption); */
|
|
|
|
if (win==nWindow)
|
|
|
|
printf("main window %dx%d, top-left of client at: (%d,%d), of window at: (%d,%d)\n",
|
2013-04-04 04:56:39 +03:00
|
|
|
width, height,
|
|
|
|
x ,y,
|
|
|
|
x-border,
|
2014-08-27 13:14:44 +03:00
|
|
|
y-caption);
|
2012-11-23 10:03:20 +02:00
|
|
|
else
|
|
|
|
printf("child window %dx%d, top-left of client at: (%d,%d), relative to parent\n",
|
2013-04-04 04:56:39 +03:00
|
|
|
width, height,
|
|
|
|
x ,y);
|
2013-03-19 18:34:21 +02:00
|
|
|
|
|
|
|
/* (re)set the timer callback and ask glut to call it in 500 ms */
|
|
|
|
glutTimerFunc(500, Timer, 0);
|
2012-11-23 10:03:20 +02:00
|
|
|
}
|
|
|
|
|
2011-09-04 23:30:48 +03:00
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2012-11-16 13:02:06 +02:00
|
|
|
int border, caption;
|
2011-09-04 23:30:48 +03:00
|
|
|
glutInit( &argc, argv );
|
|
|
|
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE /*| GLUT_BORDERLESS*/); // do try as well with GLUT_BORDERLESS and GLUT_CAPTIONLESS
|
|
|
|
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS);
|
2012-11-16 13:02:06 +02:00
|
|
|
|
|
|
|
/* Get border and caption size of default window style */
|
|
|
|
border = glutGet(GLUT_WINDOW_BORDER_WIDTH);
|
|
|
|
caption = glutGet(GLUT_WINDOW_HEADER_HEIGHT);
|
|
|
|
printf("default window style border: %dpx, caption: %dpx\n",border,caption);
|
|
|
|
|
|
|
|
/* NB: The window position you request is the outer top-left of the
|
|
|
|
* window, the client area is at a different position if the window has
|
|
|
|
* borders and/or a title bar.
|
2011-09-04 23:30:48 +03:00
|
|
|
*/
|
|
|
|
glutInitWindowPosition(150,250);
|
|
|
|
glutInitWindowSize(200,200);
|
|
|
|
|
|
|
|
nWindow = glutCreateWindow("test");
|
2013-04-04 04:56:39 +03:00
|
|
|
glutSetIconTitle("test icon title");
|
2011-09-04 23:30:48 +03:00
|
|
|
printf("main window id: %d\n", nWindow);
|
|
|
|
|
|
|
|
glutKeyboardFunc( SampleKeyboard );
|
|
|
|
glutDisplayFunc( Redisplay );
|
|
|
|
glutReshapeFunc( Reshape );
|
2012-11-23 09:41:39 +02:00
|
|
|
glutPositionFunc( Position );
|
2013-03-01 12:31:34 +02:00
|
|
|
glutWindowStatusFunc( WindowStatus );
|
2011-09-04 23:30:48 +03:00
|
|
|
|
2012-11-23 10:03:20 +02:00
|
|
|
glutTimerFunc(300, Timer, 0);
|
|
|
|
|
2011-09-04 23:30:48 +03:00
|
|
|
glutMainLoop();
|
|
|
|
printf("glutMainLoop returned\n");
|
|
|
|
|
2012-07-23 16:52:55 +03:00
|
|
|
return EXIT_SUCCESS;
|
2014-08-27 19:02:03 +03:00
|
|
|
}
|