use tnl_emit_func, it's safer
[mesa.git] / src / glut / dos / init.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 4.0
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 <string.h>
31
32 #include "glutint.h"
33
34 #define DEFAULT_WIDTH 300
35 #define DEFAULT_HEIGHT 300
36 #define DEFAULT_BPP 16
37
38 #define ALPHA_SIZE 8
39 #define DEPTH_SIZE 16
40 #define STENCIL_SIZE 8
41 #define ACCUM_SIZE 16
42
43
44
45 GLuint g_bpp = DEFAULT_BPP;
46 GLuint g_alpha = ALPHA_SIZE;
47 GLuint g_depth = DEPTH_SIZE;
48 GLuint g_stencil = STENCIL_SIZE;
49 GLuint g_accum = ACCUM_SIZE;
50 GLuint g_refresh = 0;
51 GLuint g_screen_w, g_screen_h;
52 GLint g_driver_caps;
53
54 GLuint g_fps = 0;
55
56 GLuint g_display_mode = 0;
57 int g_init_x = 0, g_init_y = 0;
58 GLuint g_init_w = DEFAULT_WIDTH, g_init_h = DEFAULT_HEIGHT;
59
60 char *__glutProgramName = NULL;
61
62
63
64 void APIENTRY glutInit (int *argc, char **argv)
65 {
66 char *str;
67 const char *env;
68
69 if ((env = getenv("DMESA_GLUT_BPP")) != NULL) {
70 g_bpp = atoi(env);
71 }
72 if ((env = getenv("DMESA_GLUT_ALPHA")) != NULL) {
73 g_alpha = atoi(env);
74 }
75 if ((env = getenv("DMESA_GLUT_DEPTH")) != NULL) {
76 g_depth = atoi(env);
77 }
78 if ((env = getenv("DMESA_GLUT_STENCIL")) != NULL) {
79 g_stencil = atoi(env);
80 }
81 if ((env = getenv("DMESA_GLUT_ACCUM")) != NULL) {
82 g_accum = atoi(env);
83 }
84 if ((env = getenv("DMESA_GLUT_REFRESH")) != NULL) {
85 g_refresh = atoi(env);
86 }
87
88 /* Determine program name. */
89 str = strrchr(argv[0], '/');
90 if (str == NULL) {
91 str = argv[0];
92 } else {
93 str++;
94 }
95 __glutProgramName = __glutStrdup(str);
96
97 /* check if GLUT_FPS env var is set */
98 if ((env = getenv("GLUT_FPS")) != NULL) {
99 if ((g_fps = atoi(env)) <= 0) {
100 g_fps = 5000; /* 5000 milliseconds */
101 }
102 }
103
104 /* Initialize timer */
105 glutGet(GLUT_ELAPSED_TIME);
106 }
107
108
109
110 void APIENTRY glutInitDisplayMode (unsigned int mode)
111 {
112 g_display_mode = mode;
113 }
114
115
116
117 void APIENTRY glutInitWindowPosition (int x, int y)
118 {
119 g_init_x = x;
120 g_init_y = y;
121 }
122
123
124
125 void APIENTRY glutInitWindowSize (int width, int height)
126 {
127 g_init_w = width;
128 g_init_h = height;
129 }
130
131
132
133 #define DO_REDISPLAY(w, ccin, ccout) \
134 do { \
135 if (w->redisplay && w->display) { \
136 int rv = GL_TRUE; \
137 \
138 idle = GL_FALSE; \
139 w->redisplay = GL_FALSE; \
140 \
141 /* test IN condition (whether we need to `MakeCurrent') */\
142 if (ccin) { \
143 rv = DMesaMakeCurrent(w->context, w->buffer); \
144 } \
145 \
146 /* do the display only if `MakeCurrent' didn't failed */ \
147 if (rv) { \
148 if (w->show_mouse && !(g_display_mode & GLUT_DOUBLE)) {\
149 /* XXX scare mouse */ \
150 w->display(); \
151 /* XXX unscare mouse */ \
152 } else { \
153 w->display(); \
154 } \
155 \
156 /* update OUT condition */ \
157 ccout; \
158 } \
159 } \
160 } while (0)
161
162
163
164 void APIENTRY glutMainLoop (void)
165 {
166 int i, n;
167 GLUTwindow *w;
168 GLboolean idle;
169 static int old_mouse_x = 0;
170 static int old_mouse_y = 0;
171 static int old_mouse_b = 0;
172
173 {
174 GLint screen_size[2];
175 DMesaGetIntegerv(DMESA_GET_SCREEN_SIZE, screen_size);
176 g_screen_w = screen_size[0];
177 g_screen_h = screen_size[1];
178 DMesaGetIntegerv(DMESA_GET_DRIVER_CAPS, &g_driver_caps);
179 }
180
181 pc_install_keyb();
182 __glutInitMouse();
183
184 for (i = 0; i < MAX_WINDOWS; i++) {
185 w = g_windows[i];
186 if (w != NULL) {
187 glutSetWindow(w->num);
188 glutPostRedisplay();
189 if (w->reshape) {
190 w->reshape(w->width, w->height);
191 }
192 if (w->visibility) {
193 w->visibility(GLUT_VISIBLE);
194 }
195 }
196 }
197
198 while (GL_TRUE) {
199 idle = GL_TRUE;
200
201 n = 0;
202 for (i = 0; i < MAX_WINDOWS; i++) {
203 w = g_windows[i];
204 if ((w != NULL) && (w != g_curwin)) {
205 /* 1) redisplay `w'
206 * 2) `MakeCurrent' always
207 * 3) update number of non-default windows
208 */
209 DO_REDISPLAY(w, GL_TRUE, n++);
210 }
211 }
212 /* 1) redisplay `g_curwin'
213 * 2) `MakeCurrent' only if we previously did non-default windows
214 * 3) don't update anything
215 */
216 DO_REDISPLAY(g_curwin, n, n);
217
218 if (g_mouse) {
219 int mouse_x;
220 int mouse_y;
221 int mouse_z;
222 int mouse_b;
223
224 /* query mouse */
225 mouse_b = pc_query_mouse(&mouse_x, &mouse_y, &mouse_z);
226
227 /* relative to window coordinates */
228 g_mouse_x = mouse_x - g_curwin->xpos;
229 g_mouse_y = mouse_y - g_curwin->ypos;
230
231 /* mouse was moved? */
232 if ((mouse_x != old_mouse_x) || (mouse_y != old_mouse_y)) {
233 idle = GL_FALSE;
234 old_mouse_x = mouse_x;
235 old_mouse_y = mouse_y;
236
237 if (mouse_b) {
238 /* any button pressed */
239 if (g_curwin->motion) {
240 g_curwin->motion(g_mouse_x, g_mouse_y);
241 }
242 } else {
243 /* no button pressed */
244 if (g_curwin->passive) {
245 g_curwin->passive(g_mouse_x, g_mouse_y);
246 }
247 }
248 }
249
250 /* button state changed? */
251 if (mouse_b != old_mouse_b) {
252 GLUTmouseCB mouse_func;
253
254 if ((mouse_func = g_curwin->mouse)) {
255 if ((old_mouse_b & 1) && !(mouse_b & 1))
256 mouse_func(GLUT_LEFT_BUTTON, GLUT_UP, g_mouse_x, g_mouse_y);
257 else if (!(old_mouse_b & 1) && (mouse_b & 1))
258 mouse_func(GLUT_LEFT_BUTTON, GLUT_DOWN, g_mouse_x, g_mouse_y);
259
260 if ((old_mouse_b & 2) && !(mouse_b & 2))
261 mouse_func(GLUT_RIGHT_BUTTON, GLUT_UP, g_mouse_x, g_mouse_y);
262 else if (!(old_mouse_b & 2) && (mouse_b & 2))
263 mouse_func(GLUT_RIGHT_BUTTON, GLUT_DOWN, g_mouse_x, g_mouse_y);
264
265 if ((old_mouse_b & 4) && !(mouse_b & 4))
266 mouse_func(GLUT_MIDDLE_BUTTON, GLUT_UP, g_mouse_x, g_mouse_y);
267 else if (!(old_mouse_b & 3) && (mouse_b & 4))
268 mouse_func(GLUT_MIDDLE_BUTTON, GLUT_DOWN, g_mouse_x, g_mouse_y);
269 }
270
271 idle = GL_FALSE;
272 old_mouse_b = mouse_b;
273 }
274 }
275
276 if (pc_keypressed()) {
277 int key;
278 int glut_key;
279
280 idle = GL_FALSE;
281 key = pc_readkey();
282
283 switch (key>>16) {
284 case KEY_F1: glut_key = GLUT_KEY_F1; goto special;
285 case KEY_F2: glut_key = GLUT_KEY_F2; goto special;
286 case KEY_F3: glut_key = GLUT_KEY_F3; goto special;
287 case KEY_F4: glut_key = GLUT_KEY_F4; goto special;
288 case KEY_F5: glut_key = GLUT_KEY_F5; goto special;
289 case KEY_F6: glut_key = GLUT_KEY_F6; goto special;
290 case KEY_F7: glut_key = GLUT_KEY_F7; goto special;
291 case KEY_F8: glut_key = GLUT_KEY_F8; goto special;
292 case KEY_F9: glut_key = GLUT_KEY_F9; goto special;
293 case KEY_F10: glut_key = GLUT_KEY_F10; goto special;
294 case KEY_F11: glut_key = GLUT_KEY_F11; goto special;
295 case KEY_F12: glut_key = GLUT_KEY_F12; goto special;
296 case KEY_LEFT: glut_key = GLUT_KEY_LEFT; goto special;
297 case KEY_UP: glut_key = GLUT_KEY_UP; goto special;
298 case KEY_RIGHT: glut_key = GLUT_KEY_RIGHT; goto special;
299 case KEY_DOWN: glut_key = GLUT_KEY_DOWN; goto special;
300 case KEY_PGUP: glut_key = GLUT_KEY_PAGE_UP; goto special;
301 case KEY_PGDN: glut_key = GLUT_KEY_PAGE_DOWN; goto special;
302 case KEY_HOME: glut_key = GLUT_KEY_HOME; goto special;
303 case KEY_END: glut_key = GLUT_KEY_END; goto special;
304 case KEY_INSERT: glut_key = GLUT_KEY_INSERT; goto special;
305 special:
306 if (g_curwin->special) {
307 g_curwin->special(glut_key, g_mouse_x, g_mouse_y);
308 }
309 break;
310 default:
311 if (g_curwin->keyboard) {
312 g_curwin->keyboard(key & 0xFF, g_mouse_x, g_mouse_y);
313 }
314 }
315 }
316
317 if (idle && g_idle_func)
318 g_idle_func();
319 }
320 }