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