resolution don't care option, heuristic to avoid multimon sizes
[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::show_cursor(bool show)
63 {
64         SDL_ShowCursor(show ? 1 : 0);
65 }
66
67 void fxwt::swap_buffers() {
68         SDL_GL_SwapBuffers();
69 #if defined(_POSIX_PRIORITY_SCHEDULING)
70         sched_yield();
71 #elif defined(WIN32)
72         Sleep(0);
73 #endif
74 }
75
76 int fxwt::main_loop() {
77         set_verbosity(3);
78
79         SDL_EnableKeyRepeat(300, 20);
80
81         while(1) {
82                 SDL_Event event;
83
84                 if(!idle_handlers.empty()) {
85                         while(SDL_PollEvent(&event)) {
86                                 handle_event(event);
87                         }
88                         
89                         list<void (*)()>::iterator iter = idle_handlers.begin();
90                         while(iter != idle_handlers.end()) {
91                                 (*iter++)();
92                         }
93                 } else {
94                         SDL_WaitEvent(&event);
95                         handle_event(event);
96                 }
97         }
98
99         return 0;
100 }
101
102 static void handle_event(const SDL_Event &event) {
103         switch(event.type) {
104         case SDL_KEYDOWN:
105                 {
106                         list<void (*)(int)>::iterator iter = keyb_handlers.begin();
107                         while(iter != keyb_handlers.end()) {
108                                 (*iter++)(event.key.keysym.sym);
109                         }
110                 }
111                 break;
112
113         case SDL_VIDEOEXPOSE:
114                 {
115                         list<void (*)()>::iterator iter = disp_handlers.begin();
116                         while(iter != disp_handlers.end()) {
117                                 (*iter++)();
118                         }
119                 }
120                 break;
121
122         case SDL_MOUSEMOTION:
123                 {
124                         list<void (*)(int, int)>::iterator iter = motion_handlers.begin();
125                         while(iter != motion_handlers.end()) {
126                                 (*iter++)(event.motion.x, event.motion.y);
127                         }
128                 }
129                 break;
130
131         case SDL_MOUSEBUTTONDOWN:
132         case SDL_MOUSEBUTTONUP:
133                 {
134                         button_state[event.button.button] = event.button.state;
135                         list<void (*)(int, int, int, int)>::iterator iter = button_handlers.begin();
136                         while(iter != button_handlers.end()) {
137                                 (*iter++)(event.button.button, event.button.state == SDL_PRESSED, event.button.x, event.button.y);
138                         }
139                 }
140                 break;
141
142         case SDL_QUIT:
143                 exit(0);
144
145         default:
146                 break;
147         }
148 }
149
150
151 #endif  // GFX_LIBRARY == SDL