minor gitignore additions
[summerhack] / src / 3dengfx / src / fxwt / fxwt_glut.cpp
1 /*
2 This file is part of fxwt, the window system toolkit of 3dengfx.
3
4 Copyright (C) 2005 John Tsiombikas <nuclear@siggraph.org>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program 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
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 /* main fxwt event handling and window system abstraction through GLUT.
22  *
23  * Author: John Tsiombikas 2005
24  */
25
26 #include "3dengfx_config.h"
27
28 #if GFX_LIBRARY == GLUT
29
30 #include "gfx_library.h"
31 #include "fxwt.hpp"
32 #include "3dengfx/3denginefx.hpp"
33 #include "common/err_msg.h"
34
35 #if defined(__unix__) || defined(unix)
36 #include <unistd.h>
37 #ifdef _POSIX_PRIORITY_SCHEDULING
38 #include <sched.h>
39 #endif  // _POSIX_PRIORITY_SCHEDULING
40 #endif  // __unix__
41
42 #if defined(WIN32) || defined(__WIN32__)
43 #include <windows.h>
44 #endif  // WIN32
45
46 using std::list;
47 using namespace fxwt;
48
49 static void glut_disp_handler();
50 static void glut_idle_handler();
51 static void glut_keyb_handler(unsigned char key, int x, int y);
52 static void glut_motion_handler(int x, int y);
53 static void glut_button_handler(int bn, int state, int x, int y);
54
55 extern int fxwt_glut_win;
56
57 Vector2 fxwt::get_mouse_pos_normalized() {
58         // TODO: implement this
59         return Vector2(0, 0);
60 }
61
62 void fxwt::set_window_title(const char *title) {
63         glutSetWindow(fxwt_glut_win);
64         glutSetWindowTitle(title);
65 }
66
67 void fxwt::show_cursor(bool show)
68 {
69         glutSetCursor(show ? GLUT_CURSOR_LEFT_ARROW : GLUT_CURSOR_NONE);
70 }
71
72 void fxwt::swap_buffers() {
73         glutSetWindow(fxwt_glut_win);
74         glutSwapBuffers();
75 #if defined(_POSIX_PRIORITY_SCHEDULING)
76         sched_yield();
77 #elif defined(WIN32)
78         Sleep(0);
79 #endif
80
81 }
82
83 int fxwt::main_loop() {
84         set_verbosity(3);
85
86         // register glut event handlers
87         glutSetWindow(fxwt_glut_win);
88         glutDisplayFunc(glut_disp_handler);
89         if(!idle_handlers.empty()) glutIdleFunc(glut_idle_handler);
90         glutKeyboardFunc(glut_keyb_handler);
91         if(!motion_handlers.empty()) {
92                 glutMotionFunc(glut_motion_handler);
93                 glutPassiveMotionFunc(glut_motion_handler);
94         }
95         glutMouseFunc(glut_button_handler);
96
97         glutMainLoop();
98         return 0;
99 }
100
101 static void glut_disp_handler() {
102         list<void (*)()>::iterator iter = disp_handlers.begin();
103         while(iter != disp_handlers.end()) {
104                 (*iter++)();
105         }
106 }
107 static void glut_idle_handler() {
108         list<void (*)()>::iterator iter = idle_handlers.begin();
109         while(iter != idle_handlers.end()) {
110                 (*iter++)();
111         }
112 }
113
114 static void glut_keyb_handler(unsigned char key, int x, int y) {
115         list<void (*)(int)>::iterator iter = keyb_handlers.begin();
116         while(iter != keyb_handlers.end()) {
117                 (*iter++)(key);
118         }
119 }
120
121 static void glut_motion_handler(int x, int y) {
122         list<void (*)(int, int)>::iterator iter = motion_handlers.begin();
123         while(iter != motion_handlers.end()) {
124                 (*iter++)(x, y);
125         }
126 }
127
128 static void glut_button_handler(int bn, int state, int x, int y) {
129         bn++;
130         button_state[bn] = state == GLUT_DOWN;
131         list<void (*)(int, int, int, int)>::iterator iter = button_handlers.begin();
132         while(iter != button_handlers.end()) {
133                 (*iter++)(bn, state == GLUT_DOWN, x, y);
134         }
135 }
136
137 #endif  // GFX_LIBRARY == GLUT