22e28b03c6826d64ebfccdad3521f5db2d0d275c
[freeglut] / progs / demos / subwin / subwin.c
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 */
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
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
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             shapesPrintf (2, 3, "Move The mouse into different windows");
108             shapesPrintf (3, 3, "pressing keys will add to the string");
109     }
110     shapesPrintf (5, 3, "Window: %d", win);
111     shapesPrintf (6, 3, "String: %s", strings[win]);
112
113     glutSwapBuffers();
114 }
115
116
117 static void
118 key(unsigned char key, int x, int y)
119 {
120         char *s,str[2];
121         int win = glutGetWindow();
122         
123     switch (key)
124     {
125     case 27 :
126     case 'Q':
127     case 'q': glutLeaveMainLoop () ;      break;
128
129     default:
130         s=strings[win];
131         if (strlen(s)+1>MAXSTR) {
132                 s[0]=0;
133         }
134         str[0]=key;
135         str[1]=0;
136         strcat(s,str);
137         break;
138     }
139
140     glutPostRedisplay();
141 }
142
143 static void special (int key, int x, int y)
144 {
145     switch (key)
146     {
147     default:
148         break;
149     }
150     glutPostRedisplay();
151 }
152
153
154 static void
155 entry(int state)
156 {
157     int win = glutGetWindow();
158     printf("Win: %d, state: %d\n",win,state);
159 }
160
161 /* Program entry point */
162
163 int
164 main(int argc, char *argv[])
165 {
166         int winmax,sw1,sw2,i;
167         
168     glutInitWindowSize(640,480);
169     glutInitWindowPosition(40,40);
170     glutInit(&argc, argv);
171     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
172
173     glutCreateWindow("FreeGLUT Sub Windows");
174
175     glutReshapeFunc(resize);
176     glutDisplayFunc(display);
177     glutKeyboardFunc(key);
178     glutSpecialFunc(special);
179     glutEntryFunc(entry);
180
181     glutSetOption ( GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION ) ;
182
183     glClearColor(1,1,1,1);
184
185         mainwin = glutGetWindow();
186         winmax=mainwin;
187         
188         sw1=glutCreateSubWindow(mainwin,4,240,314,236);
189     glutReshapeFunc(resize);
190     glutDisplayFunc(display);
191     glutKeyboardFunc(key);
192     glutSpecialFunc(special);
193     glutEntryFunc(entry);
194     glClearColor(0.7f,0.7f,0.7f,1);
195         winmax = sw1 > winmax ? sw1 : winmax;
196
197         sw2=glutCreateSubWindow(mainwin,322,240,314,236);
198     glutReshapeFunc(resize);
199     glutDisplayFunc(display);
200     glutKeyboardFunc(key);
201     glutSpecialFunc(special);
202     glutEntryFunc(entry);
203     glClearColor(0.7f,0.7f,0.7f,1);
204         winmax = sw2 > winmax ? sw2 : winmax;
205
206         strings = malloc(sizeof(char *)*(winmax+1));
207         for (i=0;i<winmax+1;i++) {
208                 strings[i] = malloc(sizeof(char)*MAXSTR+1);
209                 strings[i][0]=0;
210         }
211
212     glutMainLoop();
213
214 #ifdef _MSC_VER
215     /* DUMP MEMORY LEAK INFORMATION */
216     _CrtDumpMemoryLeaks () ;
217 #endif
218
219     return EXIT_SUCCESS;
220 }