1 /*! \file subwin.c
2 \ingroup demos
3
4 This program is a test harness for the subwindows
5 in OpenGLUT. Based Originally on shape.c demo.
6
7 \author Written by Evan Felix February 2011
8
9 \author Portions Copyright (C) 2004, the OpenGLUT project contributors. <br>
10 OpenGLUT branched from freeglut in February, 2004.
11
12 \image html openglut_subwin.png OpenGLUT Sub Window Demonstration
13 \include demos/subwin/subwin.c
14 */
15
16 #include <GL/freeglut.h>
17
18 #include <stdarg.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #ifdef _MSC_VER
23 /* DUMP MEMORY LEAKS */
24 #include <crtdbg.h>
25 #endif
26
27 #define MAXSTR 16
28 char **strings;
29 int mainwin;
30
31
32 /*!
33 Does printf()-like work using freeglut/OpenGLUT
34 glutBitmapString(). Uses a fixed font. Prints
35 at the indicated row/column position.
36
37 Limitation: Cannot address pixels.
38 Limitation: Renders in screen coords, not model coords.
39 */
shapesPrintf(int row,int col,const char * fmt,...)40 static void shapesPrintf (int row, int col, const char *fmt, ...)
41 {
42 static char buf[256];
43 int viewport[4];
44 void *font = GLUT_BITMAP_9_BY_15;
45 va_list args;
46
47 va_start(args, fmt);
48 #if defined(WIN32) && !defined(__CYGWIN__)
49 (void) _vsnprintf (buf, sizeof(buf), fmt, args);
50 #else
51 (void) vsnprintf (buf, sizeof(buf), fmt, args);
52 #endif
53 va_end(args);
54
55 glGetIntegerv(GL_VIEWPORT,viewport);
56
57 glPushMatrix();
58 glLoadIdentity();
59
60 glMatrixMode(GL_PROJECTION);
61 glPushMatrix();
62 glLoadIdentity();
63
64 glOrtho(0,viewport[2],0,viewport[3],-1,1);
65
66 glRasterPos2i
67 (
68 glutBitmapWidth(font, ' ') * col,
69 - glutBitmapHeight(font) * (row+2) + viewport[3]
70 );
71 glutBitmapString (font, (unsigned char*)buf);
72
73 glPopMatrix();
74 glMatrixMode(GL_MODELVIEW);
75 glPopMatrix();
76 }
77
78 /* GLUT callback Handlers */
79
80 static void
resize(int width,int height)81 resize(int width, int height)
82 {
83
84 glViewport(0, 0, width, height);
85
86 glMatrixMode(GL_PROJECTION);
87 glLoadIdentity();
88 /*gluOrtho2D(0, width, 0, height);*/
89
90 glMatrixMode(GL_MODELVIEW);
91 glLoadIdentity() ;
92 }
93
display(void)94 static void display(void)
95 {
96
97 int win = glutGetWindow();
98
99 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
100
101 glColor3d(1,0,0);
102
103 glDisable(GL_LIGHTING);
104 glColor3d(0.1,0.1,0.4);
105
106 if (win == mainwin)
107 {
108 shapesPrintf (2, 3, "Move The mouse into different windows");
109 shapesPrintf (3, 3, "pressing keys will add to the string");
110 shapesPrintf (5, 3, "Window: %d", win);
111 shapesPrintf (6, 3, "String: %s", strings[win]);
112 }
113 else
114 {
115 shapesPrintf (1, 3, "Window: %d", win);
116 shapesPrintf (2, 3, "String: %s", strings[win]);
117 }
118
119 glutSwapBuffers();
120 }
121
122
123 static void
key(unsigned char key,int x,int y)124 key(unsigned char key, int x, int y)
125 {
126 char *s,str[2];
127 int win = glutGetWindow();
128
129 switch (key)
130 {
131 case 27 :
132 case 'Q':
133 case 'q': glutLeaveMainLoop () ; break;
134
135 default:
136 s=strings[win];
137 if (strlen(s)+1>MAXSTR) {
138 s[0]=0;
139 }
140 str[0]=key;
141 str[1]=0;
142 strcat(s,str);
143 break;
144 }
145
146 glutPostRedisplay();
147 }
148
special(int key,int x,int y)149 static void special (int key, int x, int y)
150 {
151 switch (key)
152 {
153 default:
154 break;
155 }
156 glutPostRedisplay();
157 }
158
159
160 static void
entry(int state)161 entry(int state)
162 {
163 int win = glutGetWindow();
164 printf("Win: %d, state: %d\n",win,state);
165 }
166
167 /* Program entry point */
168
169 int
main(int argc,char * argv[])170 main(int argc, char *argv[])
171 {
172 int winmax,sw1,sw2,sw2sw,i;
173
174 glutInitWindowSize(640,480);
175 glutInitWindowPosition(40,40);
176 glutInit(&argc, argv);
177 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
178
179 glutCreateWindow("FreeGLUT Sub Windows");
180
181 glutReshapeFunc(resize);
182 glutDisplayFunc(display);
183 glutKeyboardFunc(key);
184 glutSpecialFunc(special);
185 glutEntryFunc(entry);
186
187 glutSetOption ( GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION ) ;
188
189 glClearColor(1,1,1,1);
190
191 mainwin = glutGetWindow();
192 winmax=mainwin;
193
194 sw1=glutCreateSubWindow(mainwin,4,240,314,236);
195 glutReshapeFunc(resize);
196 glutDisplayFunc(display);
197 glutKeyboardFunc(key);
198 glutSpecialFunc(special);
199 glutEntryFunc(entry);
200 glClearColor(0.7f,0.7f,0.7f,1);
201 winmax = sw1 > winmax ? sw1 : winmax;
202
203 sw2=glutCreateSubWindow(mainwin,322,240,314,236);
204 glutReshapeFunc(resize);
205 glutDisplayFunc(display);
206 glutKeyboardFunc(key);
207 glutSpecialFunc(special);
208 glutEntryFunc(entry);
209 glClearColor(0.7f,0.7f,0.7f,1);
210 winmax = sw2 > winmax ? sw2 : winmax;
211
212 sw2sw=glutCreateSubWindow(sw2,10,128,294,98);
213 glutReshapeFunc(resize);
214 glutDisplayFunc(display);
215 glutKeyboardFunc(key);
216 glutSpecialFunc(special);
217 glutEntryFunc(entry);
218 glClearColor(0.4f,0.4f,0.4f,1);
219 winmax = sw2sw > winmax ? sw2sw : winmax;
220
221 strings = malloc(sizeof(char *)*(winmax+1));
222 for (i=0;i<winmax+1;i++) {
223 strings[i] = malloc(sizeof(char)*MAXSTR+1);
224 strings[i][0]=0;
225 }
226
227 glutMainLoop();
228
229 #ifdef _MSC_VER
230 /* DUMP MEMORY LEAK INFORMATION */
231 _CrtDumpMemoryLeaks () ;
232 #endif
233
234 return EXIT_SUCCESS;
235 }
236