added 3dengfx into the repo, probably not the correct version for this
[summerhack] / src / 3dengfx / src / fxwt / fxwt_sdl.cpp
1 /*
2 This file is part of fxwt, the window system toolkit of 3dengfx.
3
4 Copyright (C) 2004, 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 SDL.
22  *
23  * Author: John Tsiombikas 2004
24  */
25
26 #include "3dengfx_config.h"
27
28 #if GFX_LIBRARY == SDL
29
30 #include "gfx_library.h"
31 #include "fxwt.hpp"
32 #include "3dengfx/3denginefx.hpp"
33 #include "common/err_msg.h"
34
35 #ifdef __unix__
36 #include <unistd.h>
37 #ifdef _POSIX_PRIORITY_SCHEDULING
38 #include <sched.h>
39 #endif  // _POSIX_PRIORITY_SCHEDULING
40 #endif  // __unix__
41
42 #ifdef WIN32
43 #include <windows.h>
44 #endif  // WIN32
45
46 using std::list;
47 using namespace fxwt;
48
49 static void handle_event(const SDL_Event &event);
50
51 Vector2 fxwt::get_mouse_pos_normalized() {
52         int x, y;
53         SDL_GetMouseState(&x, &y);
54
55         return Vector2((scalar_t)x / (scalar_t)screenx, (scalar_t)y / (scalar_t)screeny);
56 }
57
58 void fxwt::set_window_title(const char *title) {
59         SDL_WM_SetCaption(title, 0);
60 }
61
62 void fxwt::swap_buffers() {
63         SDL_GL_SwapBuffers();
64 #if defined(_POSIX_PRIORITY_SCHEDULING)
65         sched_yield();
66 #elif defined(WIN32)
67         Sleep(0);
68 #endif
69 }
70
71 int fxwt::main_loop() {
72         set_verbosity(3);
73         
74         SDL_EnableKeyRepeat(300, 20);
75
76         while(1) {
77                 SDL_Event event;
78
79                 if(!idle_handlers.empty()) {
80                         while(SDL_PollEvent(&event)) {
81                                 handle_event(event);
82                         }
83                         
84                         list<void (*)()>::iterator iter = idle_handlers.begin();
85                         while(iter != idle_handlers.end()) {
86                                 (*iter++)();
87                         }
88                 } else {
89                         SDL_WaitEvent(&event);
90                         handle_event(event);
91                 }
92         }
93
94         return 0;
95 }
96
97 static void handle_event(const SDL_Event &event) {
98         switch(event.type) {
99         case SDL_KEYDOWN:
100                 {
101                         list<void (*)(int)>::iterator iter = keyb_handlers.begin();
102                         while(iter != keyb_handlers.end()) {
103                                 (*iter++)(event.key.keysym.sym);
104                         }
105                 }
106                 break;
107
108         case SDL_VIDEOEXPOSE:
109                 {
110                         list<void (*)()>::iterator iter = disp_handlers.begin();
111                         while(iter != disp_handlers.end()) {
112                                 (*iter++)();
113                         }
114                 }
115                 break;
116
117         case SDL_MOUSEMOTION:
118                 {
119                         list<void (*)(int, int)>::iterator iter = motion_handlers.begin();
120                         while(iter != motion_handlers.end()) {
121                                 (*iter++)(event.motion.x, event.motion.y);
122                         }
123                 }
124                 break;
125
126         case SDL_MOUSEBUTTONDOWN:
127         case SDL_MOUSEBUTTONUP:
128                 {
129                         button_state[event.button.button] = event.button.state;
130                         list<void (*)(int, int, int, int)>::iterator iter = button_handlers.begin();
131                         while(iter != button_handlers.end()) {
132                                 (*iter++)(event.button.button, event.button.state == SDL_PRESSED, event.button.x, event.button.y);
133                         }
134                 }
135                 break;
136
137         case SDL_QUIT:
138                 exit(0);
139
140         default:
141                 break;
142         }
143 }
144
145
146 #endif  // GFX_LIBRARY == SDL