added 3dengfx into the repo, probably not the correct version for this
[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::swap_buffers() {
68         glutSetWindow(fxwt_glut_win);
69         glutSwapBuffers();
70 #if defined(_POSIX_PRIORITY_SCHEDULING)
71         sched_yield();
72 #elif defined(WIN32)
73         Sleep(0);
74 #endif
75
76 }
77
78 int fxwt::main_loop() {
79         set_verbosity(3);
80
81         // register glut event handlers
82         glutSetWindow(fxwt_glut_win);
83         glutDisplayFunc(glut_disp_handler);
84         if(!idle_handlers.empty()) glutIdleFunc(glut_idle_handler);
85         glutKeyboardFunc(glut_keyb_handler);
86         if(!motion_handlers.empty()) {
87                 glutMotionFunc(glut_motion_handler);
88                 glutPassiveMotionFunc(glut_motion_handler);
89         }
90         glutMouseFunc(glut_button_handler);
91
92         glutMainLoop();
93         return 0;
94 }
95
96 static void glut_disp_handler() {
97         list<void (*)()>::iterator iter = disp_handlers.begin();
98         while(iter != disp_handlers.end()) {
99                 (*iter++)();
100         }
101 }
102 static void glut_idle_handler() {
103         list<void (*)()>::iterator iter = idle_handlers.begin();
104         while(iter != idle_handlers.end()) {
105                 (*iter++)();
106         }
107 }
108
109 static void glut_keyb_handler(unsigned char key, int x, int y) {
110         list<void (*)(int)>::iterator iter = keyb_handlers.begin();
111         while(iter != keyb_handlers.end()) {
112                 (*iter++)(key);
113         }
114 }
115
116 static void glut_motion_handler(int x, int y) {
117         list<void (*)(int, int)>::iterator iter = motion_handlers.begin();
118         while(iter != motion_handlers.end()) {
119                 (*iter++)(x, y);
120         }
121 }
122
123 static void glut_button_handler(int bn, int state, int x, int y) {
124         bn++;
125         button_state[bn] = state == GLUT_DOWN;
126         list<void (*)(int, int, int, int)>::iterator iter = button_handlers.begin();
127         while(iter != button_handlers.end()) {
128                 (*iter++)(bn, state == GLUT_DOWN, x, y);
129         }
130 }
131
132 #endif  // GFX_LIBRARY == GLUT