updates from Daniel Borca
[mesa.git] / src / glut / dos / window.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 4.1
4 * Copyright (C) 1995-1998 Brian Paul
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 /*
22 * DOS/DJGPP glut driver v1.4 for Mesa
23 *
24 * Copyright (C) 2002 - Borca Daniel
25 * Email : dborca@users.sourceforge.net
26 * Web : http://www.geocities.com/dborca
27 */
28
29
30 #include <stdio.h>
31
32 #include "glutint.h"
33 #include "GL/dmesa.h"
34
35
36
37 GLUTwindow *g_curwin;
38 static GLuint swaptime, swapcount;
39
40 static DMesaVisual visual = NULL;
41 GLUTwindow *g_windows[MAX_WINDOWS];
42
43
44
45 static void clean (void)
46 {
47 int i;
48
49 for (i=1; i<=MAX_WINDOWS; i++) {
50 glutDestroyWindow(i);
51 }
52 if (visual) DMesaDestroyVisual(visual);
53
54 pc_close_stdout();
55 pc_close_stderr();
56 }
57
58
59
60 int APIENTRY glutCreateWindow (const char *title)
61 {
62 int i;
63 int m8width = (g_init_w + 7) & ~7;
64
65 /* We set the Visual once. This will be our desktop (graphic mode).
66 * We should do this in the `glutInit' code, but we don't have any idea
67 * about its geometry. Supposedly, when we are about to create one
68 * window, we have a slight idea about resolution.
69 */
70 if (!visual) {
71 if ((visual=DMesaCreateVisual(g_init_x + m8width, g_init_y + g_init_h, g_bpp, g_refresh,
72 g_display_mode & GLUT_DOUBLE,
73 !(g_display_mode & GLUT_INDEX),
74 (g_display_mode & GLUT_ALPHA ) ? g_alpha : 0,
75 (g_display_mode & GLUT_DEPTH ) ? g_depth : 0,
76 (g_display_mode & GLUT_STENCIL) ? g_stencil : 0,
77 (g_display_mode & GLUT_ACCUM ) ? g_accum : 0))==NULL) {
78 return 0;
79 }
80
81 /* Also hook stdio/stderr once */
82 pc_open_stdout();
83 pc_open_stderr();
84 pc_atexit(clean);
85 }
86
87 /* Search for an empty slot.
88 * Each window has its own rendering Context and its own Buffer.
89 */
90 for (i=0; i<MAX_WINDOWS; i++) {
91 if (g_windows[i] == NULL) {
92 DMesaContext c;
93 DMesaBuffer b;
94 GLUTwindow *w;
95
96 if ((w = (GLUTwindow *)calloc(1, sizeof(GLUTwindow))) == NULL) {
97 return 0;
98 }
99
100 /* Allocate the rendering Context. */
101 if ((c = DMesaCreateContext(visual, NULL)) == NULL) {
102 free(w);
103 return 0;
104 }
105
106 /* Allocate the Buffer (displayable area).
107 * We have to specify buffer size and position (inside the desktop).
108 */
109 if ((b = DMesaCreateBuffer(visual, g_init_x, g_init_y, m8width, g_init_h)) == NULL) {
110 DMesaDestroyContext(c);
111 free(w);
112 return 0;
113 }
114
115 /* Bind Buffer to Context and make the Context the current one. */
116 if (!DMesaMakeCurrent(c, b)) {
117 DMesaDestroyBuffer(b);
118 DMesaDestroyContext(c);
119 free(w);
120 return 0;
121 }
122
123 g_curwin = g_windows[i] = w;
124
125 w->num = ++i;
126 w->xpos = g_init_x;
127 w->ypos = g_init_y;
128 w->width = m8width;
129 w->height = g_init_h;
130 w->context = c;
131 w->buffer = b;
132
133 return i;
134 }
135 }
136
137 return 0;
138 }
139
140
141
142 int APIENTRY glutCreateSubWindow (int win, int x, int y, int width, int height)
143 {
144 return GL_FALSE;
145 }
146
147
148
149 void APIENTRY glutDestroyWindow (int win)
150 {
151 if (g_windows[--win]) {
152 GLUTwindow *w = g_windows[win];
153 DMesaMakeCurrent(NULL, NULL);
154 DMesaDestroyBuffer(w->buffer);
155 DMesaDestroyContext(w->context);
156 free(w);
157 g_windows[win] = NULL;
158 }
159 }
160
161
162
163 void APIENTRY glutPostRedisplay (void)
164 {
165 g_curwin->redisplay = GL_TRUE;
166 }
167
168
169
170 void APIENTRY glutSwapBuffers (void)
171 {
172 if (g_curwin->show_mouse) {
173 /* XXX scare mouse */
174 DMesaSwapBuffers(g_curwin->buffer);
175 /* XXX unscare mouse */
176 } else {
177 DMesaSwapBuffers(g_curwin->buffer);
178 }
179
180 if (g_fps) {
181 GLint t = glutGet(GLUT_ELAPSED_TIME);
182 swapcount++;
183 if (swaptime == 0)
184 swaptime = t;
185 else if (t - swaptime > g_fps) {
186 double time = 0.001 * (t - swaptime);
187 double fps = (double)swapcount / time;
188 fprintf(stderr, "GLUT: %d frames in %.2f seconds = %.2f FPS\n", swapcount, time, fps);
189 swaptime = t;
190 swapcount = 0;
191 }
192 }
193 }
194
195
196
197 int APIENTRY glutGetWindow (void)
198 {
199 return g_curwin->num;
200 }
201
202
203
204 void APIENTRY glutSetWindow (int win)
205 {
206 g_curwin = g_windows[win - 1];
207 DMesaMakeCurrent(g_curwin->context, g_curwin->buffer);
208 }
209
210
211
212 void APIENTRY glutSetWindowTitle (const char *title)
213 {
214 }
215
216
217
218 void APIENTRY glutSetIconTitle (const char *title)
219 {
220 }
221
222
223
224 void APIENTRY glutPositionWindow (int x, int y)
225 {
226 if (DMesaMoveBuffer(x, y)) {
227 g_curwin->xpos = x;
228 g_curwin->ypos = y;
229 }
230 }
231
232
233
234 void APIENTRY glutReshapeWindow (int width, int height)
235 {
236 if (DMesaResizeBuffer(width, height)) {
237 g_curwin->width = width;
238 g_curwin->height = height;
239 if (g_curwin->reshape) {
240 g_curwin->reshape(width, height);
241 } else {
242 glViewport(0, 0, width, height);
243 }
244 }
245 }
246
247
248
249 void APIENTRY glutFullScreen (void)
250 {
251 }
252
253
254
255 void APIENTRY glutPopWindow (void)
256 {
257 }
258
259
260
261 void APIENTRY glutPushWindow (void)
262 {
263 }
264
265
266
267 void APIENTRY glutIconifyWindow (void)
268 {
269 }
270
271
272
273 void APIENTRY glutShowWindow (void)
274 {
275 }
276
277
278
279 void APIENTRY glutHideWindow (void)
280 {
281 }