\r
#include <GL/freeglut.h>\r
\r
-int nWindow;\r
+int nWindow, nChildWindow = -1;\r
int nLoopMain = 0;\r
\r
int nPosX, nPosY;\r
int nWidth, nHeight;\r
\r
+GLboolean bChildPosDone = GL_FALSE, bChildSizeDone = GL_FALSE;\r
+\r
void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY );\r
void Redisplay();\r
-\r
+void Reshape(int x, int y);\r
\r
\r
\r
\r
void DrawQuad()\r
{\r
+ nWidth = glutGet(GLUT_WINDOW_WIDTH);\r
+ nHeight = glutGet(GLUT_WINDOW_HEIGHT);\r
+\r
glBegin(GL_QUADS);\r
glVertex2d(nWidth*.25, nHeight*.75);\r
glVertex2d(nWidth*.75, nHeight*.75);\r
\r
void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY )\r
{\r
- if (cChar == 27)\r
+ switch (cChar)\r
+ {\r
+ case 27:\r
glutLeaveMainLoop();\r
\r
- else if (cChar=='f')\r
- {\r
- printf("main window toggle fullscreen\n");\r
+ break;\r
+\r
\r
+ case 'f':\r
+ case 'F':\r
+ printf("main window toggle fullscreen\n");\r
glutFullScreenToggle();\r
- }\r
- else if (cChar=='r')\r
- {\r
- printf("main window resize\n");\r
\r
- if (nWidth<400)\r
- glutReshapeWindow(600,300);\r
+ break;\r
+\r
+\r
+ case 'r':\r
+ case 'R':\r
+ if (nChildWindow!=-1)\r
+ {\r
+ glutSetWindow(nChildWindow);\r
+ printf("child window resize\n");\r
+ if (!bChildSizeDone)\r
+ glutReshapeWindow(glutGet(GLUT_WINDOW_WIDTH)+50,glutGet(GLUT_WINDOW_HEIGHT)+50);\r
+ else\r
+ glutReshapeWindow(glutGet(GLUT_WINDOW_WIDTH)-50,glutGet(GLUT_WINDOW_HEIGHT)-50);\r
+ bChildSizeDone = !bChildSizeDone;\r
+ }\r
else\r
- glutReshapeWindow(300,300);\r
- }\r
- else if (cChar=='m')\r
- {\r
- printf("main window position\n");\r
-\r
- /* The window position you request is the outer top-left of the window,\r
- * the client area is at a different position if the window has borders\r
- * and/or a title bar.\r
- */\r
- if (nPosX<400)\r
- glutPositionWindow(600,300);\r
+ {\r
+ printf("main window resize\n");\r
+ if (nWidth<400)\r
+ glutReshapeWindow(600,300);\r
+ else\r
+ glutReshapeWindow(300,300);\r
+ }\r
+\r
+ break;\r
+\r
+\r
+ case 'm':\r
+ case 'M':\r
+ if (nChildWindow!=-1)\r
+ {\r
+ glutSetWindow(nChildWindow);\r
+\r
+ /* The window position you request is relative to the top-left\r
+ * corner of the client area of the parent window.\r
+ */\r
+ if (!bChildPosDone)\r
+ glutPositionWindow(glutGet(GLUT_WINDOW_X)+50,glutGet(GLUT_WINDOW_Y)+50);\r
+ else\r
+ glutPositionWindow(glutGet(GLUT_WINDOW_X)-50,glutGet(GLUT_WINDOW_Y)-50);\r
+ bChildPosDone = !bChildPosDone;\r
+ }\r
else\r
- glutPositionWindow(300,300);\r
+ {\r
+ printf("main window position\n");\r
+\r
+ /* The window position you request is the outer top-left of the window,\r
+ * the client area is at a different position if the window has borders\r
+ * and/or a title bar.\r
+ */\r
+ if (nPosX<400)\r
+ glutPositionWindow(600,300);\r
+ else\r
+ glutPositionWindow(300,300);\r
+ }\r
+\r
+ break;\r
+\r
+\r
+ case 'c':\r
+ case 'C':\r
+ if (nChildWindow==-1)\r
+ {\r
+ /* open child window */\r
+ printf("open child window\n");\r
+ nWidth = glutGet(GLUT_WINDOW_WIDTH);\r
+ nHeight = glutGet(GLUT_WINDOW_HEIGHT);\r
+\r
+ nChildWindow = glutCreateSubWindow(nWindow,(int)(nWidth*.35),(int)(nHeight*.35),(int)(nWidth*.3),(int)(nHeight*.3));\r
+ glutKeyboardFunc( SampleKeyboard );\r
+ glutDisplayFunc( Redisplay );\r
+ glutReshapeFunc( Reshape );\r
+ }\r
+ else\r
+ {\r
+ /* close child window */\r
+ printf("close child window\n");\r
+ glutSetWindow(nWindow);\r
+ glutDestroyWindow(nChildWindow);\r
+ nChildWindow = -1;\r
+ bChildSizeDone = GL_FALSE;\r
+ bChildPosDone = GL_FALSE;\r
+ }\r
+ break;\r
+\r
+\r
+ default:\r
+ break;\r
}\r
}\r
\r
\r
void Reshape(int x, int y)\r
{\r
+ int win = glutGetWindow();\r
+\r
nWidth = glutGet(GLUT_WINDOW_WIDTH);\r
nHeight = glutGet(GLUT_WINDOW_HEIGHT);\r
+ printf("reshape %s, %dx%d\n",win==nWindow?"main":"child",\r
+ nWidth, nHeight);\r
\r
glViewport(0,0,nWidth,nHeight);\r
glMatrixMode(GL_PROJECTION);\r
glLoadIdentity();\r
gluOrtho2D(0,nWidth,0,nHeight);\r
+\r
+ if (win==nWindow && nChildWindow!=-1)\r
+ {\r
+ glutSetWindow(nChildWindow);\r
+ glutPositionWindow((int)(nWidth*.35),(int)(nHeight*.35));\r
+ glutReshapeWindow((int)(nWidth*.3),(int)(nHeight*.3));\r
+ glutSetWindow(nWindow);\r
+ }\r
}\r
\r
void Redisplay(void)\r
{\r
- if (nLoopMain++%6==0)\r
+ int win = glutGetWindow();\r
+\r
+ if (nLoopMain++%20==0)\r
{\r
int border, caption;\r
\r
* or simply when maximized--try pressing the maximize button).\r
* the returned size is the size of the client area\r
*/\r
- printf("window now %dx%d, top-left of client at: (%d,%d), of window at: (%d,%d)\n",\r
+ if (win==nWindow)\r
+ printf("main window %dx%d, top-left of client at: (%d,%d), of window at: (%d,%d)\n",\r
+ nWidth, nHeight,\r
+ nPosX ,nPosY,\r
+ nPosX-border,\r
+ nPosY-border-caption);\r
+ else\r
+ printf("child window %dx%d, top-left of client at: (%d,%d), relative to parent\n",\r
nWidth, nHeight,\r
- nPosX ,nPosY,\r
- nPosX-border,\r
- nPosY-border-caption);\r
+ nPosX ,nPosY);\r
}\r
\r
- glClearColor(.2f,0.f,0.f,0.f);\r
+ if (win==nWindow)\r
+ {\r
+ glClearColor(.2f,0.f,0.f,0.f);\r
+ glColor3f(1,1,1);\r
+ }\r
+ else\r
+ {\r
+ /* child window */\r
+ glClearColor(.0f,.2f,0.f,0.f);\r
+ glColor3f(.5,.5,.5);\r
+ glutPostWindowRedisplay(nWindow);\r
+ }\r
glClear(GL_COLOR_BUFFER_BIT);\r
- glColor3f(1,1,1);\r
DrawQuad();\r
\r
glutSwapBuffers();\r
- glutPostRedisplay();\r
+ glutPostWindowRedisplay(win);\r
}\r
\r
\r
glutMainLoop();\r
printf("glutMainLoop returned\n");\r
\r
- return 1;\r
+ return EXIT_SUCCESS;\r
}
\ No newline at end of file