resizer demo now uses timer to display window information
[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 && cChar=='r') /* Capital R always resizes the main window*/\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             glutSetWindow(nWindow);\r
66             printf("main window resize\n");\r
67             if (nWidth<400)\r
68                 glutReshapeWindow(600,300);\r
69             else\r
70                 glutReshapeWindow(300,300);\r
71         }\r
72 \r
73         break;\r
74 \r
75 \r
76     case 'm':\r
77     case 'M':\r
78         if (nChildWindow!=-1 && cChar=='m') /* Capital M always moves the main window*/\r
79         {\r
80             glutSetWindow(nChildWindow);\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             glutSetWindow(nWindow);\r
93             printf("main window position\n");\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 width, int height)\r
145 {\r
146     int win = glutGetWindow();\r
147 \r
148     printf("reshape %s, %dx%d\n",win==nWindow?"main":"child",\r
149         width, height);\r
150 \r
151     glViewport(0,0,width,height);\r
152     glMatrixMode(GL_PROJECTION);\r
153     glLoadIdentity();\r
154     gluOrtho2D(0,width,0,height);\r
155 \r
156     if (win==nWindow && nChildWindow!=-1)\r
157     {\r
158         glutSetWindow(nChildWindow);\r
159         glutPositionWindow((int)(width*.35),(int)(height*.35));\r
160         glutReshapeWindow((int)(width*.3),(int)(height*.3));\r
161         glutSetWindow(nWindow);\r
162     }\r
163 }\r
164 \r
165 void Position(int x, int y)\r
166 {\r
167     int win = glutGetWindow();\r
168 \r
169     printf("position %s, %dx%d\n",win==nWindow?"main":"child",\r
170         x, y);\r
171 }\r
172 \r
173 void Redisplay(void)\r
174 {\r
175     int win = glutGetWindow();\r
176 \r
177     if (win==nWindow)\r
178     {\r
179         glClearColor(.2f,0.f,0.f,0.f);\r
180         glColor3f(1,1,1);\r
181     }\r
182     else\r
183     {\r
184         /* child window */\r
185         glClearColor(.0f,.2f,0.f,0.f);\r
186         glColor3f(.5,.5,.5);\r
187         glutPostWindowRedisplay(nWindow);\r
188     }\r
189     glClear(GL_COLOR_BUFFER_BIT);\r
190     DrawQuad();\r
191 \r
192     glutSwapBuffers();\r
193     glutPostWindowRedisplay(win);\r
194 }\r
195 \r
196 void Timer(int unused)
197 {
198     int win = glutGetWindow();
199     int border, caption;\r
200 \r
201     nPosX   = glutGet(GLUT_WINDOW_X);\r
202     nPosY   = glutGet(GLUT_WINDOW_Y);\r
203     nWidth  = glutGet(GLUT_WINDOW_WIDTH);\r
204     nHeight = glutGet(GLUT_WINDOW_HEIGHT);\r
205     border  = glutGet(GLUT_WINDOW_BORDER_WIDTH);\r
206     caption = glutGet(GLUT_WINDOW_HEADER_HEIGHT);\r
207     /* returned position is top-left of client area, to get top-left of\r
208      * of window you'll need to add the size of the border and caption\r
209      * of the current window (can be 0).\r
210      * Note that the window position is not necessarily positive (e.g.\r
211      * when the window is on a monitor to the left of the primary monitor\r
212      * or simply when maximized--try pressing the maximize button).\r
213      * the returned size is the size of the client area\r
214      * Note that the top-left of a child window is relative to the\r
215      * top-left of the client area of the parent.\r
216      */\r
217     /* printf("window border: %dpx, caption: %dpx\n",border,caption); */\r
218     if (win==nWindow)\r
219         printf("main  window %dx%d, top-left of client at: (%d,%d), of window at: (%d,%d)\n",\r
220             nWidth, nHeight,\r
221             nPosX ,nPosY,\r
222             nPosX-border,\r
223             nPosY-border-caption);\r
224     else\r
225         printf("child window %dx%d, top-left of client at: (%d,%d), relative to parent\n",\r
226         nWidth, nHeight,\r
227         nPosX ,nPosY);
228
229     /* (re)set the timer callback and ask glut to call it in 1 second */
230     glutTimerFunc(300, Timer, 0);
231 }\r
232 \r
233 \r
234 int main(int argc, char* argv[])\r
235 {\r
236     int border, caption;\r
237     glutInit( &argc, argv );\r
238     glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE /*| GLUT_BORDERLESS*/); // do try as well with GLUT_BORDERLESS and GLUT_CAPTIONLESS\r
239     glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS);\r
240     \r
241     /* Get border and caption size of default window style */\r
242     border  = glutGet(GLUT_WINDOW_BORDER_WIDTH);\r
243     caption = glutGet(GLUT_WINDOW_HEADER_HEIGHT);\r
244     printf("default window style border: %dpx, caption: %dpx\n",border,caption);\r
245 \r
246     /* NB: The window position you request is the outer top-left of the\r
247      * window, the client area is at a different position if the window has\r
248      * borders and/or a title bar.\r
249      */\r
250     glutInitWindowPosition(150,250);\r
251     glutInitWindowSize(200,200);\r
252 \r
253     nWindow = glutCreateWindow("test");\r
254     printf("main window id: %d\n", nWindow);\r
255 \r
256     glutKeyboardFunc( SampleKeyboard );\r
257     glutDisplayFunc( Redisplay );\r
258     glutReshapeFunc( Reshape );\r
259     glutPositionFunc( Position );\r
260 \r
261     glutTimerFunc(300, Timer, 0);\r
262 \r
263     glutMainLoop();\r
264     printf("glutMainLoop returned\n");\r
265 \r
266     return EXIT_SUCCESS;\r
267 }