sgl

annotate src/wsys_glut.c @ 8:0b07dd867b2f

added empty-file warning silencers in wsys_* files
author John Tsiombikas <nuclear@siggraph.org>
date Sat, 14 May 2011 08:33:41 +0300
parents edbfc96fe80d
children 1532e43bf858
rev   line source
nuclear@7 1 /* SimplyGL window system module for GLUT */
nuclear@7 2 /* link-with: -lglut */
nuclear@7 3
nuclear@7 4 #include "config.h"
nuclear@7 5
nuclear@7 6 #ifdef USE_WSYS_MODULE_GLUT
nuclear@7 7
nuclear@7 8 #include <setjmp.h>
nuclear@7 9 #ifndef __APPLE__
nuclear@7 10 #include <GL/glut.h>
nuclear@7 11 #ifdef FREEGLUT
nuclear@7 12 #include <GL/freeglut_ext.h>
nuclear@7 13 #endif /* freeglut */
nuclear@7 14
nuclear@7 15 #else /* apple */
nuclear@7 16 #include <GLUT/glut.h>
nuclear@7 17 #endif
nuclear@7 18
nuclear@7 19 #include "sgl.h"
nuclear@7 20 #include "wsys.h"
nuclear@7 21
nuclear@7 22 struct window {
nuclear@7 23 int id;
nuclear@7 24 struct window *next;
nuclear@7 25 };
nuclear@7 26
nuclear@7 27 static int init(void);
nuclear@7 28 static void shutdown(void);
nuclear@7 29
nuclear@7 30 /* video mode switching */
nuclear@7 31 static int set_vidmode(int xsz, int ysz);
nuclear@7 32 static int get_vidmode(int *xsz, int *ysz);
nuclear@7 33
nuclear@7 34 /* create/destroy windows */
nuclear@7 35 static int create_window(int xsz, int ysz, unsigned int flags);
nuclear@7 36 static void close_window(int id);
nuclear@7 37
nuclear@7 38 /* window management */
nuclear@7 39 static int set_active(int id);
nuclear@7 40 static int set_title(const char *str);
nuclear@7 41 static void redisplay(void);
nuclear@7 42 static void swap_buffers(void);
nuclear@7 43
nuclear@7 44 static int get_modifiers(void);
nuclear@7 45
nuclear@7 46 /* event handling and friends */
nuclear@7 47 static void set_event(int idx, int enable);
nuclear@7 48 static int process_events(void);
nuclear@7 49
nuclear@7 50 /* callbacks */
nuclear@7 51 static void disp_cb(void);
nuclear@7 52 static void reshape_cb(int x, int y);
nuclear@7 53 static void keyb_down_cb(unsigned char c, int x, int y);
nuclear@7 54 static void keyb_up_cb(unsigned char c, int x, int y);
nuclear@7 55 static void special_down_cb(int c, int x, int y);
nuclear@7 56 static void special_up_cb(int c, int x, int y);
nuclear@7 57 static void mouse_cb(int bn, int state, int x, int y);
nuclear@7 58 static void motion_cb(int x, int y);
nuclear@7 59 static void passive_cb(int x, int y);
nuclear@7 60 static void idle_cb(void);
nuclear@7 61
nuclear@7 62
nuclear@7 63 static struct wsys_module ws = {
nuclear@7 64 "glut", 1,
nuclear@7 65 init,
nuclear@7 66 shutdown,
nuclear@7 67 set_vidmode,
nuclear@7 68 get_vidmode,
nuclear@7 69 create_window,
nuclear@7 70 close_window,
nuclear@7 71 set_active,
nuclear@7 72 set_title,
nuclear@7 73 redisplay,
nuclear@7 74 swap_buffers,
nuclear@7 75 get_modifiers,
nuclear@7 76 set_event,
nuclear@7 77 process_events,
nuclear@7 78 0
nuclear@7 79 };
nuclear@7 80
nuclear@7 81 #ifndef FREEGLUT
nuclear@7 82 static jmp_buf jbuf;
nuclear@7 83 #endif
nuclear@7 84
nuclear@7 85 static struct window *winlist;
nuclear@7 86
nuclear@7 87
nuclear@7 88 /* this is the only exported function, everything else should be static */
nuclear@7 89 void sgl_register_glut(void)
nuclear@7 90 {
nuclear@7 91 sgl_register_module(&ws);
nuclear@7 92 }
nuclear@7 93
nuclear@7 94 static int init(void)
nuclear@7 95 {
nuclear@7 96 char *argv[] = { "simplygl", 0 };
nuclear@7 97 int argc = 1;
nuclear@7 98
nuclear@7 99 glutInit(&argc, argv);
nuclear@7 100 return 0;
nuclear@7 101 }
nuclear@7 102
nuclear@7 103 static void shutdown(void)
nuclear@7 104 {
nuclear@7 105 struct window *win = winlist;
nuclear@7 106
nuclear@7 107 while(win) {
nuclear@7 108 int id = win->id;
nuclear@7 109 win = win->next;
nuclear@7 110
nuclear@7 111 close_window(id);
nuclear@7 112 }
nuclear@7 113 winlist = 0;
nuclear@7 114 }
nuclear@7 115
nuclear@7 116 static int set_vidmode(int xsz, int ysz)
nuclear@7 117 {
nuclear@7 118 /* TODO */
nuclear@7 119 return 0;
nuclear@7 120 }
nuclear@7 121
nuclear@7 122 static int get_vidmode(int *xsz, int *ysz)
nuclear@7 123 {
nuclear@7 124 /* TODO */
nuclear@7 125 return 0;
nuclear@7 126 }
nuclear@7 127
nuclear@7 128 static int create_window(int xsz, int ysz, unsigned int flags)
nuclear@7 129 {
nuclear@7 130 struct window *win;
nuclear@7 131 unsigned int glut_flags = GLUT_RGBA;
nuclear@7 132
nuclear@7 133 if(flags & SGL_DOUBLE) {
nuclear@7 134 glut_flags |= GLUT_DOUBLE;
nuclear@7 135 }
nuclear@7 136 if(flags & SGL_DEPTH) {
nuclear@7 137 glut_flags |= GLUT_DEPTH;
nuclear@7 138 }
nuclear@7 139 if(flags & SGL_STENCIL) {
nuclear@7 140 glut_flags |= GLUT_STENCIL;
nuclear@7 141 }
nuclear@7 142 if(flags & SGL_STEREO) {
nuclear@7 143 glut_flags |= GLUT_STEREO;
nuclear@7 144 }
nuclear@7 145 if(flags & SGL_MULTISAMPLE) {
nuclear@7 146 glut_flags |= GLUT_MULTISAMPLE;
nuclear@7 147 }
nuclear@7 148
nuclear@7 149 if(!(win = malloc(sizeof *win))) {
nuclear@7 150 return -1;
nuclear@7 151 }
nuclear@7 152
nuclear@7 153 glutInitDisplayMode(glut_flags);
nuclear@7 154 glutInitWindowSize(xsz, ysz);
nuclear@7 155 if((win->id = glutCreateWindow("OpenGL/GLUT")) <= 0) {
nuclear@7 156 free(win);
nuclear@7 157 return -1;
nuclear@7 158 }
nuclear@7 159
nuclear@7 160 win->next = winlist;
nuclear@7 161 winlist = win;
nuclear@7 162 return win->id;
nuclear@7 163 }
nuclear@7 164
nuclear@7 165 static void close_window(int id)
nuclear@7 166 {
nuclear@7 167 struct window dummy, *win, *prev;
nuclear@7 168
nuclear@7 169 dummy.next = win = winlist;
nuclear@7 170 prev = &dummy;
nuclear@7 171
nuclear@7 172 while(win) {
nuclear@7 173 if(win->id == id) {
nuclear@7 174 prev->next = win->next;
nuclear@7 175 free(win);
nuclear@7 176 break;
nuclear@7 177 }
nuclear@7 178 win = win->next;
nuclear@7 179 }
nuclear@7 180
nuclear@7 181 glutDestroyWindow(id);
nuclear@7 182 }
nuclear@7 183
nuclear@7 184 static int set_active(int id)
nuclear@7 185 {
nuclear@7 186 glutSetWindow(id);
nuclear@7 187 return 0;
nuclear@7 188 }
nuclear@7 189
nuclear@7 190 static int set_title(const char *str)
nuclear@7 191 {
nuclear@7 192 glutSetWindowTitle(str);
nuclear@7 193 glutSetIconTitle(str);
nuclear@7 194 return 0;
nuclear@7 195 }
nuclear@7 196
nuclear@7 197 static void redisplay(void)
nuclear@7 198 {
nuclear@7 199 glutPostRedisplay();
nuclear@7 200 }
nuclear@7 201
nuclear@7 202 static void swap_buffers(void)
nuclear@7 203 {
nuclear@7 204 glutSwapBuffers();
nuclear@7 205 }
nuclear@7 206
nuclear@7 207 static int get_modifiers(void)
nuclear@7 208 {
nuclear@7 209 return glutGetModifiers();
nuclear@7 210 }
nuclear@7 211
nuclear@7 212 static void set_event(int idx, int enable)
nuclear@7 213 {
nuclear@7 214 switch(idx) {
nuclear@7 215 case SGL_DISPLAY:
nuclear@7 216 glutDisplayFunc(enable ? disp_cb : 0);
nuclear@7 217 break;
nuclear@7 218 case SGL_RESHAPE:
nuclear@7 219 glutReshapeFunc(enable ? reshape_cb : 0);
nuclear@7 220 break;
nuclear@7 221 case SGL_KEYBOARD:
nuclear@7 222 glutKeyboardFunc(enable ? keyb_down_cb : 0);
nuclear@7 223 glutKeyboardUpFunc(enable ? keyb_up_cb : 0);
nuclear@7 224 glutSpecialFunc(enable ? special_down_cb : 0);
nuclear@7 225 glutSpecialUpFunc(enable ? special_up_cb : 0);
nuclear@7 226 break;
nuclear@7 227 case SGL_MOUSE:
nuclear@7 228 glutMouseFunc(enable ? mouse_cb : 0);
nuclear@7 229 break;
nuclear@7 230 case SGL_MOTION:
nuclear@7 231 glutMotionFunc(enable ? motion_cb : 0);
nuclear@7 232 break;
nuclear@7 233 case SGL_PASSIVE:
nuclear@7 234 glutPassiveMotionFunc(enable ? passive_cb : 0);
nuclear@7 235 break;
nuclear@7 236 case SGL_IDLE:
nuclear@7 237 glutIdleFunc(enable ? idle_cb : 0);
nuclear@7 238 break;
nuclear@7 239 default:
nuclear@7 240 break;
nuclear@7 241 }
nuclear@7 242 }
nuclear@7 243
nuclear@7 244 static int process_events(void)
nuclear@7 245 {
nuclear@7 246 #ifdef FREEGLUT
nuclear@7 247 glutMainLoopEvent();
nuclear@7 248 #else
nuclear@7 249 if(setjmp(jbuf) == 0) {
nuclear@7 250 glutMainLoop();
nuclear@7 251 }
nuclear@7 252 /* ok ... what happens is any callback that kicks in will set the idle func
nuclear@7 253 * if it's not set, and then the idle func will longjmp right back here...
nuclear@7 254 */
nuclear@7 255 #endif
nuclear@7 256 return 0;
nuclear@7 257 }
nuclear@7 258
nuclear@7 259 static void disp_cb(void)
nuclear@7 260 {
nuclear@7 261 sgl_display_callback_t func = sgl_get_callback(SGL_DISPLAY);
nuclear@7 262 func();
nuclear@7 263
nuclear@7 264 #ifndef FREEGLUT
nuclear@7 265 glutIdleFunc(idle_cb);
nuclear@7 266 #endif
nuclear@7 267 }
nuclear@7 268
nuclear@7 269 static void reshape_cb(int x, int y)
nuclear@7 270 {
nuclear@7 271 sgl_reshape_callback_t func = sgl_get_callback(SGL_RESHAPE);
nuclear@7 272 func(x, y);
nuclear@7 273
nuclear@7 274 #ifndef FREEGLUT
nuclear@7 275 glutIdleFunc(idle_cb);
nuclear@7 276 #endif
nuclear@7 277 }
nuclear@7 278
nuclear@7 279 static void keyb_down_cb(unsigned char c, int x, int y)
nuclear@7 280 {
nuclear@7 281 sgl_keyboard_callback_t func = sgl_get_callback(SGL_KEYBOARD);
nuclear@7 282 func((int)c, 1);
nuclear@7 283
nuclear@7 284 #ifndef FREEGLUT
nuclear@7 285 glutIdleFunc(idle_cb);
nuclear@7 286 #endif
nuclear@7 287 }
nuclear@7 288
nuclear@7 289 static void keyb_up_cb(unsigned char c, int x, int y)
nuclear@7 290 {
nuclear@7 291 sgl_keyboard_callback_t func = sgl_get_callback(SGL_KEYBOARD);
nuclear@7 292 func((int)c, 0);
nuclear@7 293
nuclear@7 294 #ifndef FREEGLUT
nuclear@7 295 glutIdleFunc(idle_cb);
nuclear@7 296 #endif
nuclear@7 297 }
nuclear@7 298
nuclear@7 299 static void special_down_cb(int c, int x, int y)
nuclear@7 300 {
nuclear@7 301 sgl_keyboard_callback_t func = sgl_get_callback(SGL_KEYBOARD);
nuclear@7 302 func(c, 1);
nuclear@7 303
nuclear@7 304 #ifndef FREEGLUT
nuclear@7 305 glutIdleFunc(idle_cb);
nuclear@7 306 #endif
nuclear@7 307 }
nuclear@7 308
nuclear@7 309 static void special_up_cb(int c, int x, int y)
nuclear@7 310 {
nuclear@7 311 sgl_keyboard_callback_t func = sgl_get_callback(SGL_KEYBOARD);
nuclear@7 312 func(c, 0);
nuclear@7 313
nuclear@7 314 #ifndef FREEGLUT
nuclear@7 315 glutIdleFunc(idle_cb);
nuclear@7 316 #endif
nuclear@7 317 }
nuclear@7 318
nuclear@7 319 static void mouse_cb(int bn, int state, int x, int y)
nuclear@7 320 {
nuclear@7 321 sgl_mouse_callback_t func = sgl_get_callback(SGL_MOUSE);
nuclear@7 322 func(0, bn, state, x, y);
nuclear@7 323
nuclear@7 324 #ifndef FREEGLUT
nuclear@7 325 glutIdleFunc(idle_cb);
nuclear@7 326 #endif
nuclear@7 327 }
nuclear@7 328
nuclear@7 329 static void motion_cb(int x, int y)
nuclear@7 330 {
nuclear@7 331 sgl_motion_callback_t func = sgl_get_callback(SGL_MOTION);
nuclear@7 332 func(0, x, y);
nuclear@7 333
nuclear@7 334 #ifndef FREEGLUT
nuclear@7 335 glutIdleFunc(idle_cb);
nuclear@7 336 #endif
nuclear@7 337 }
nuclear@7 338
nuclear@7 339 static void passive_cb(int x, int y)
nuclear@7 340 {
nuclear@7 341 sgl_passive_callback_t func = sgl_get_callback(SGL_PASSIVE);
nuclear@7 342 func(0, x, y);
nuclear@7 343
nuclear@7 344 #ifndef FREEGLUT
nuclear@7 345 glutIdleFunc(idle_cb);
nuclear@7 346 #endif
nuclear@7 347 }
nuclear@7 348
nuclear@7 349 static void idle_cb(void)
nuclear@7 350 {
nuclear@7 351 sgl_idle_callback_t func = sgl_get_callback(SGL_IDLE);
nuclear@7 352 if(func) {
nuclear@7 353 func();
nuclear@7 354 #ifndef FREEGLUT
nuclear@7 355 } else {
nuclear@7 356 /* this was just the longjmp trick so restore the lack of idle func */
nuclear@7 357 glutIdleFunc(0);
nuclear@7 358 #endif
nuclear@7 359 }
nuclear@7 360
nuclear@7 361 #ifndef FREEGLUT
nuclear@7 362 longjmp(jbuf, 0);
nuclear@7 363 #endif
nuclear@7 364 }
nuclear@7 365
nuclear@8 366 #else
nuclear@8 367 int sgl_wsys_glut_silence_the_fucking_empty_file_warnings;
nuclear@7 368 #endif /* USE_WSYS_MODULE_GLUT */