8252d7deaabc16346b41dea39cf79151f826c8ac
[freeglut] / progs / demos / Resizer / Resizer.cpp
1 #include <stdio.h>\r
2 \r
3 #include <GL/freeglut.h>\r
4 \r
5 int nWindow;\r
6 int nLoopMain = 0;\r
7 \r
8 int nPosX,  nPosY;\r
9 int nWidth, nHeight;\r
10 \r
11 void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY );\r
12 void Redisplay();\r
13 \r
14 \r
15 \r
16 \r
17 \r
18 void DrawQuad()\r
19 {\r
20     glBegin(GL_QUADS);\r
21         glVertex2d(nWidth*.25, nHeight*.75);\r
22         glVertex2d(nWidth*.75, nHeight*.75);\r
23         glVertex2d(nWidth*.75, nHeight*.25);\r
24         glVertex2d(nWidth*.25, nHeight*.25);\r
25     glEnd();\r
26 }\r
27 \r
28 void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY )\r
29 {\r
30     if (cChar == 27)\r
31         glutLeaveMainLoop();\r
32 \r
33     else if (cChar=='f')\r
34     {\r
35         printf("main window toggle fullscreen\n");\r
36 \r
37         glutFullScreenToggle();\r
38     }\r
39     else if (cChar=='r')\r
40     {\r
41         printf("main window resize\n");\r
42 \r
43         if (nWidth<400)\r
44             glutReshapeWindow(600,300);\r
45         else\r
46             glutReshapeWindow(300,300);\r
47     }\r
48     else if (cChar=='m')\r
49     {\r
50         printf("main window position\n");\r
51 \r
52         /* The window position you request is the outer top-left of the window,\r
53          * the client area is at a different position if the window has borders\r
54          * and/or a title bar.\r
55          */\r
56         if (nPosX<400)\r
57             glutPositionWindow(600,300);\r
58         else\r
59             glutPositionWindow(300,300);\r
60     }\r
61 }\r
62 \r
63 void Idle(void)\r
64 {\r
65     glutPostRedisplay();\r
66 }\r
67 \r
68 void Reshape(int x, int y)\r
69 {\r
70     nWidth  = glutGet(GLUT_WINDOW_WIDTH);\r
71     nHeight = glutGet(GLUT_WINDOW_HEIGHT);\r
72 \r
73     glViewport(0,0,nWidth,nHeight);\r
74     glMatrixMode(GL_PROJECTION);\r
75     glLoadIdentity();\r
76     gluOrtho2D(0,nWidth,0,nHeight);\r
77 }\r
78 \r
79 void Redisplay(void)\r
80 {\r
81     if (nLoopMain++%6==0)\r
82     {\r
83         int border, caption;\r
84 \r
85         nPosX   = glutGet(GLUT_WINDOW_X);\r
86         nPosY   = glutGet(GLUT_WINDOW_Y);\r
87         nWidth  = glutGet(GLUT_WINDOW_WIDTH);\r
88         nHeight = glutGet(GLUT_WINDOW_HEIGHT);\r
89         border  = glutGet(GLUT_WINDOW_BORDER_WIDTH);\r
90         caption = glutGet(GLUT_WINDOW_HEADER_HEIGHT);\r
91         /* returned position is top-left of client area, to get top-left of\r
92          * of window you'll need to add the size of the border and caption\r
93          * of the current window (can be 0).\r
94          * Note that the window position is not necessarily positive (e.g.\r
95          * when the window is on a monitor to the left of the primary monitor\r
96          * or simply when maximized--try pressing the maximize button).\r
97          * the returned size is the size of the client area\r
98          */\r
99         printf("window now %dx%d, top-left of client at: (%d,%d), of window at: (%d,%d)\n",\r
100             nWidth, nHeight,\r
101             nPosX ,nPosY,\r
102             nPosX-border,\r
103             nPosY-border-caption);\r
104     }\r
105 \r
106     glClearColor(.2f,0.f,0.f,0.f);\r
107     glClear(GL_COLOR_BUFFER_BIT);\r
108     glColor3f(1,1,1);\r
109     DrawQuad();\r
110 \r
111     glutSwapBuffers();\r
112     glutPostRedisplay();\r
113 }\r
114 \r
115 \r
116 int main(int argc, char* argv[])\r
117 {\r
118     glutInit( &argc, argv );\r
119     glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE /*| GLUT_BORDERLESS*/); // do try as well with GLUT_BORDERLESS and GLUT_CAPTIONLESS\r
120     glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS);\r
121 \r
122     /* The window position you request is the outer top-left of the window,\r
123      * the client area is at a different position if the window has borders\r
124      * and/or a title bar.\r
125      */\r
126     glutInitWindowPosition(150,250);\r
127     glutInitWindowSize(200,200);\r
128 \r
129     nWindow = glutCreateWindow("test");\r
130     printf("main window id: %d\n", nWindow);\r
131 \r
132     glutKeyboardFunc( SampleKeyboard );\r
133     glutDisplayFunc( Redisplay );\r
134     glutReshapeFunc( Reshape );\r
135 \r
136     glutMainLoop();\r
137     printf("glutMainLoop returned\n");\r
138 \r
139     return 1;\r
140 }