76d55f77320adb3fdaccb337c849e7873aba501f
[freeglut] / progs / demos / Resizer / Resizer.cpp
1 #include <stdio.h>\r
2 \r
3 #include <GL/freeglut.h>\r
4 \r
5 int nWindow, nChildWindow = -1;\r
6 int nLoopMain = 0;\r
7 \r
8 int nPosX,  nPosY;\r
9 int nWidth, nHeight;\r
10 \r
11 GLboolean bChildPosDone = GL_FALSE, bChildSizeDone = GL_FALSE;\r
12 \r
13 void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY );\r
14 void Redisplay();\r
15 void Reshape(int x, int y);\r
16 \r
17 \r
18 \r
19 \r
20 void DrawQuad()\r
21 {\r
22     nWidth  = glutGet(GLUT_WINDOW_WIDTH);\r
23     nHeight = glutGet(GLUT_WINDOW_HEIGHT);\r
24 \r
25     glBegin(GL_QUADS);\r
26         glVertex2d(nWidth*.25, nHeight*.75);\r
27         glVertex2d(nWidth*.75, nHeight*.75);\r
28         glVertex2d(nWidth*.75, nHeight*.25);\r
29         glVertex2d(nWidth*.25, nHeight*.25);\r
30     glEnd();\r
31 }\r
32 \r
33 void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY )\r
34 {\r
35     switch (cChar)\r
36     {\r
37     case 27:\r
38         glutLeaveMainLoop();\r
39 \r
40         break;\r
41 \r
42 \r
43     case 'f':\r
44     case 'F':\r
45         printf("main window toggle fullscreen\n");\r
46         glutFullScreenToggle();\r
47 \r
48         break;\r
49 \r
50 \r
51     case 'r':\r
52     case 'R':\r
53         if (nChildWindow!=-1)\r
54         {\r
55             glutSetWindow(nChildWindow);\r
56             printf("child window resize\n");\r
57             if (!bChildSizeDone)\r
58                 glutReshapeWindow(glutGet(GLUT_WINDOW_WIDTH)+50,glutGet(GLUT_WINDOW_HEIGHT)+50);\r
59             else\r
60                 glutReshapeWindow(glutGet(GLUT_WINDOW_WIDTH)-50,glutGet(GLUT_WINDOW_HEIGHT)-50);\r
61             bChildSizeDone = !bChildSizeDone;\r
62         }\r
63         else\r
64         {\r
65             printf("main window resize\n");\r
66             if (nWidth<400)\r
67                 glutReshapeWindow(600,300);\r
68             else\r
69                 glutReshapeWindow(300,300);\r
70         }\r
71 \r
72         break;\r
73 \r
74 \r
75     case 'm':\r
76     case 'M':\r
77         if (nChildWindow!=-1)\r
78         {\r
79             glutSetWindow(nChildWindow);\r
80 \r
81             /* The window position you request is relative to the top-left\r
82              * corner of the client area of the parent window.\r
83              */\r
84             if (!bChildPosDone)\r
85                 glutPositionWindow(glutGet(GLUT_WINDOW_X)+50,glutGet(GLUT_WINDOW_Y)+50);\r
86             else\r
87                 glutPositionWindow(glutGet(GLUT_WINDOW_X)-50,glutGet(GLUT_WINDOW_Y)-50);\r
88             bChildPosDone = !bChildPosDone;\r
89         }\r
90         else\r
91         {\r
92             printf("main window position\n");\r
93 \r
94             /* The window position you request is the outer top-left of the window,\r
95              * the client area is at a different position if the window has borders\r
96              * and/or a title bar.\r
97              */\r
98             if (nPosX<400)\r
99                 glutPositionWindow(600,300);\r
100             else\r
101                 glutPositionWindow(300,300);\r
102         }\r
103 \r
104         break;\r
105 \r
106 \r
107     case 'c':\r
108     case 'C':\r
109         if (nChildWindow==-1)\r
110         {\r
111             /* open child window */\r
112             printf("open child window\n");\r
113             nWidth  = glutGet(GLUT_WINDOW_WIDTH);\r
114             nHeight = glutGet(GLUT_WINDOW_HEIGHT);\r
115 \r
116             nChildWindow = glutCreateSubWindow(nWindow,(int)(nWidth*.35),(int)(nHeight*.35),(int)(nWidth*.3),(int)(nHeight*.3));\r
117             glutKeyboardFunc( SampleKeyboard );\r
118             glutDisplayFunc( Redisplay );\r
119             glutReshapeFunc( Reshape );\r
120         }\r
121         else\r
122         {\r
123             /* close child window */\r
124             printf("close child window\n");\r
125             glutSetWindow(nWindow);\r
126             glutDestroyWindow(nChildWindow);\r
127             nChildWindow = -1;\r
128             bChildSizeDone = GL_FALSE;\r
129             bChildPosDone  = GL_FALSE;\r
130         }\r
131         break;\r
132 \r
133 \r
134     default:\r
135         break;\r
136     }\r
137 }\r
138 \r
139 void Idle(void)\r
140 {\r
141     glutPostRedisplay();\r
142 }\r
143 \r
144 void Reshape(int x, int y)\r
145 {\r
146     int win = glutGetWindow();\r
147 \r
148     nWidth  = glutGet(GLUT_WINDOW_WIDTH);\r
149     nHeight = glutGet(GLUT_WINDOW_HEIGHT);\r
150     printf("reshape %s, %dx%d\n",win==nWindow?"main":"child",\r
151         nWidth, nHeight);\r
152 \r
153     glViewport(0,0,nWidth,nHeight);\r
154     glMatrixMode(GL_PROJECTION);\r
155     glLoadIdentity();\r
156     gluOrtho2D(0,nWidth,0,nHeight);\r
157 \r
158     if (win==nWindow && nChildWindow!=-1)\r
159     {\r
160         glutSetWindow(nChildWindow);\r
161         glutPositionWindow((int)(nWidth*.35),(int)(nHeight*.35));\r
162         glutReshapeWindow((int)(nWidth*.3),(int)(nHeight*.3));\r
163         glutSetWindow(nWindow);\r
164     }\r
165 }\r
166 \r
167 void Redisplay(void)\r
168 {\r
169     int win = glutGetWindow();\r
170 \r
171     if (nLoopMain++%20==0)\r
172     {\r
173         int border, caption;\r
174 \r
175         nPosX   = glutGet(GLUT_WINDOW_X);\r
176         nPosY   = glutGet(GLUT_WINDOW_Y);\r
177         nWidth  = glutGet(GLUT_WINDOW_WIDTH);\r
178         nHeight = glutGet(GLUT_WINDOW_HEIGHT);\r
179         border  = glutGet(GLUT_WINDOW_BORDER_WIDTH);\r
180         caption = glutGet(GLUT_WINDOW_HEADER_HEIGHT);\r
181         /* returned position is top-left of client area, to get top-left of\r
182          * of window you'll need to add the size of the border and caption\r
183          * of the current window (can be 0).\r
184          * Note that the window position is not necessarily positive (e.g.\r
185          * when the window is on a monitor to the left of the primary monitor\r
186          * or simply when maximized--try pressing the maximize button).\r
187          * the returned size is the size of the client area\r
188          */\r
189         if (win==nWindow)\r
190             printf("main  window %dx%d, top-left of client at: (%d,%d), of window at: (%d,%d)\n",\r
191                 nWidth, nHeight,\r
192                 nPosX ,nPosY,\r
193                 nPosX-border,\r
194                 nPosY-border-caption);\r
195         else\r
196             printf("child window %dx%d, top-left of client at: (%d,%d), relative to parent\n",\r
197             nWidth, nHeight,\r
198             nPosX ,nPosY);\r
199     }\r
200 \r
201     if (win==nWindow)\r
202     {\r
203         glClearColor(.2f,0.f,0.f,0.f);\r
204         glColor3f(1,1,1);\r
205     }\r
206     else\r
207     {\r
208         /* child window */\r
209         glClearColor(.0f,.2f,0.f,0.f);\r
210         glColor3f(.5,.5,.5);\r
211         glutPostWindowRedisplay(nWindow);\r
212     }\r
213     glClear(GL_COLOR_BUFFER_BIT);\r
214     DrawQuad();\r
215 \r
216     glutSwapBuffers();\r
217     glutPostWindowRedisplay(win);\r
218 }\r
219 \r
220 \r
221 int main(int argc, char* argv[])\r
222 {\r
223     glutInit( &argc, argv );\r
224     glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE /*| GLUT_BORDERLESS*/); // do try as well with GLUT_BORDERLESS and GLUT_CAPTIONLESS\r
225     glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS);\r
226 \r
227     /* The window position you request is the outer top-left of the window,\r
228      * the client area is at a different position if the window has borders\r
229      * and/or a title bar.\r
230      */\r
231     glutInitWindowPosition(150,250);\r
232     glutInitWindowSize(200,200);\r
233 \r
234     nWindow = glutCreateWindow("test");\r
235     printf("main window id: %d\n", nWindow);\r
236 \r
237     glutKeyboardFunc( SampleKeyboard );\r
238     glutDisplayFunc( Redisplay );\r
239     glutReshapeFunc( Reshape );\r
240 \r
241     glutMainLoop();\r
242     printf("glutMainLoop returned\n");\r
243 \r
244     return EXIT_SUCCESS;\r
245 }