Only enable verbose NoOp dispatch if DEBUG is defined
[mesa.git] / src / glut / dos / window.c
1 /*
2 * DOS/DJGPP Mesa Utility Toolkit
3 * Version: 1.0
4 *
5 * Copyright (C) 2005 Daniel Borca All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * DANIEL BORCA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 #include <stdio.h>
27
28 #include "internal.h"
29
30
31 static GLuint swaptime, swapcount;
32
33 static DMesaVisual visual = NULL;
34
35 GLUTwindow *_glut_current, *_glut_windows[MAX_WINDOWS];
36
37
38 static void
39 clean (void)
40 {
41 int i;
42
43 for (i=1; i<=MAX_WINDOWS; i++) {
44 glutDestroyWindow(i);
45 }
46 if (visual) DMesaDestroyVisual(visual);
47
48 pc_close_stdout();
49 pc_close_stderr();
50 }
51
52
53 static GLUTwindow *
54 _glut_window (int win)
55 {
56 if (win > 0 && --win < MAX_WINDOWS) {
57 return _glut_windows[win];
58 }
59 return NULL;
60 }
61
62
63 int APIENTRY
64 glutCreateWindow (const char *title)
65 {
66 int i;
67 int m8width = (_glut_default.width + 7) & ~7;
68
69 if (!(_glut_default.mode & GLUT_DOUBLE)) {
70 return 0;
71 }
72
73 /* We set the Visual once. This will be our desktop (graphic mode).
74 * We should do this in the `glutInit' code, but we don't have any idea
75 * about its geometry. Supposedly, when we are about to create one
76 * window, we have a slight idea about resolution.
77 */
78 if (!visual) {
79 if ((visual=DMesaCreateVisual(_glut_default.x + m8width, _glut_default.y + _glut_default.height, _glut_visual.bpp, _glut_visual.refresh,
80 GLUT_SINGLE,
81 !(_glut_default.mode & GLUT_INDEX),
82 (_glut_default.mode & GLUT_ALPHA ) ? _glut_visual.alpha : 0,
83 (_glut_default.mode & GLUT_DEPTH ) ? _glut_visual.depth : 0,
84 (_glut_default.mode & GLUT_STENCIL) ? _glut_visual.stencil : 0,
85 (_glut_default.mode & GLUT_ACCUM ) ? _glut_visual.accum : 0))==NULL) {
86 return 0;
87 }
88
89 DMesaGetIntegerv(DMESA_GET_SCREEN_SIZE, _glut_visual.geometry);
90 DMesaGetIntegerv(DMESA_GET_DRIVER_CAPS, &_glut_visual.flags);
91
92 /* Also hook stdio/stderr once */
93 pc_open_stdout();
94 pc_open_stderr();
95 pc_atexit(clean);
96 }
97
98 /* Search for an empty slot.
99 * Each window has its own rendering Context and its own Buffer.
100 */
101 for (i=0; i<MAX_WINDOWS; i++) {
102 if (_glut_windows[i] == NULL) {
103 DMesaContext c;
104 DMesaBuffer b;
105 GLUTwindow *w;
106
107 if ((w = (GLUTwindow *)calloc(1, sizeof(GLUTwindow))) == NULL) {
108 return 0;
109 }
110
111 /* Allocate the rendering Context. */
112 if ((c = DMesaCreateContext(visual, NULL)) == NULL) {
113 free(w);
114 return 0;
115 }
116
117 /* Allocate the Buffer (displayable area).
118 * We have to specify buffer size and position (inside the desktop).
119 */
120 if ((b = DMesaCreateBuffer(visual, _glut_default.x, _glut_default.y, m8width, _glut_default.height)) == NULL) {
121 DMesaDestroyContext(c);
122 free(w);
123 return 0;
124 }
125
126 /* Bind Buffer to Context and make the Context the current one. */
127 if (!DMesaMakeCurrent(c, b)) {
128 DMesaDestroyBuffer(b);
129 DMesaDestroyContext(c);
130 free(w);
131 return 0;
132 }
133
134 _glut_current = _glut_windows[i] = w;
135
136 w->num = ++i;
137 w->xpos = _glut_default.x;
138 w->ypos = _glut_default.y;
139 w->width = m8width;
140 w->height = _glut_default.height;
141 w->context = c;
142 w->buffer = b;
143
144 return i;
145 }
146 }
147
148 return 0;
149 }
150
151
152 int APIENTRY
153 glutCreateSubWindow (int win, int x, int y, int width, int height)
154 {
155 return GL_FALSE;
156 }
157
158
159 void APIENTRY
160 glutDestroyWindow (int win)
161 {
162 GLUTwindow *w = _glut_window(win);
163 if (w != NULL) {
164 if (w->destroy) {
165 w->destroy();
166 }
167 DMesaMakeCurrent(NULL, NULL);
168 DMesaDestroyBuffer(w->buffer);
169 DMesaDestroyContext(w->context);
170 free(w);
171 _glut_windows[win - 1] = NULL;
172 }
173 }
174
175
176 void APIENTRY
177 glutPostRedisplay (void)
178 {
179 _glut_current->redisplay = GL_TRUE;
180 }
181
182
183 void APIENTRY
184 glutSwapBuffers (void)
185 {
186 if (_glut_current->show_mouse) {
187 /* XXX scare mouse */
188 DMesaSwapBuffers(_glut_current->buffer);
189 /* XXX unscare mouse */
190 } else {
191 DMesaSwapBuffers(_glut_current->buffer);
192 }
193
194 if (_glut_fps) {
195 GLint t = glutGet(GLUT_ELAPSED_TIME);
196 swapcount++;
197 if (swaptime == 0)
198 swaptime = t;
199 else if (t - swaptime > _glut_fps) {
200 double time = 0.001 * (t - swaptime);
201 double fps = (double)swapcount / time;
202 fprintf(stderr, "GLUT: %d frames in %.2f seconds = %.2f FPS\n", swapcount, time, fps);
203 swaptime = t;
204 swapcount = 0;
205 }
206 }
207 }
208
209
210 int APIENTRY
211 glutGetWindow (void)
212 {
213 return _glut_current->num;
214 }
215
216
217 void APIENTRY
218 glutSetWindow (int win)
219 {
220 GLUTwindow *w = _glut_window(win);
221 if (w != NULL) {
222 _glut_current = w;
223 DMesaMakeCurrent(_glut_current->context, _glut_current->buffer);
224 }
225 }
226
227
228 void APIENTRY
229 glutSetWindowTitle (const char *title)
230 {
231 }
232
233
234 void APIENTRY
235 glutSetIconTitle (const char *title)
236 {
237 }
238
239
240 void APIENTRY
241 glutPositionWindow (int x, int y)
242 {
243 if (DMesaMoveBuffer(x, y)) {
244 _glut_current->xpos = x;
245 _glut_current->ypos = y;
246 }
247 }
248
249
250 void APIENTRY
251 glutReshapeWindow (int width, int height)
252 {
253 if (DMesaResizeBuffer(width, height)) {
254 _glut_current->width = width;
255 _glut_current->height = height;
256 if (_glut_current->reshape) {
257 _glut_current->reshape(width, height);
258 } else {
259 glViewport(0, 0, width, height);
260 }
261 }
262 }
263
264
265 void APIENTRY
266 glutFullScreen (void)
267 {
268 }
269
270
271 void APIENTRY
272 glutPopWindow (void)
273 {
274 }
275
276
277 void APIENTRY
278 glutPushWindow (void)
279 {
280 }
281
282
283 void APIENTRY
284 glutIconifyWindow (void)
285 {
286 }
287
288
289 void APIENTRY
290 glutShowWindow (void)
291 {
292 }
293
294
295 void APIENTRY
296 glutHideWindow (void)
297 {
298 }
299
300
301 void APIENTRY
302 glutCloseFunc (GLUTdestroyCB destroy)
303 {
304 _glut_current->destroy = destroy;
305 }
306
307
308 void APIENTRY
309 glutPostWindowRedisplay (int win)
310 {
311 GLUTwindow *w = _glut_window(win);
312 if (w != NULL) {
313 w->redisplay = GL_TRUE;
314 }
315 }
316
317
318 void * APIENTRY
319 glutGetWindowData (void)
320 {
321 return _glut_current->data;
322 }
323
324
325 void APIENTRY
326 glutSetWindowData (void *data)
327 {
328 _glut_current->data = data;
329 }