8 #include "keyboardBuffer.h"
10 #include "mouse_buffer.h"
12 static SDL_Surface
*screen
; /* window surface */
13 /*static SDL_Surface *my_surface;*/
14 static SDL_TimerID timer
; /* timer */
15 static Uint32 g_win_flags
; /* window flags */
17 static bool_t isInterfaceInit
= FALSE
;
18 static bool_t keyboardBufferEnabled
= FALSE
; /* flag that controls organising keys into the buffer */
19 static bool_t use_open_gl
;
21 bool_t
interface_is_use_open_gl()
26 bool_t
interface_is_inicialized()
28 return isInterfaceInit
;
31 static Uint32
TimerCallback(Uint32 interval
, void *param
)
35 event
.type
= SDL_USEREVENT
;
36 event
.user
.code
= USR_EVT_TIMER
;
37 event
.user
.data1
= NULL
;
38 event
.user
.data2
= NULL
;
40 SDL_PushEvent(&event
);
45 void interface_enable_keyboard_buffer()
47 keyboardBufferEnabled
= TRUE
;
50 void interface_disable_keyboard_buffer()
52 keyboardBufferEnabled
= FALSE
;
57 if (g_win_flags
& SDL_FULLSCREEN
) {
58 g_win_flags
&= ~SDL_FULLSCREEN
;
60 g_win_flags
|= SDL_FULLSCREEN
;
63 if (SDL_WM_ToggleFullScreen(screen
) == 0) {
64 error("Unable to switch to the fullscreen mode");
66 SDL_FreeSurface(screen
);
67 screen
= SDL_SetVideoMode(WINDOW_SIZE_X
, WINDOW_SIZE_Y
, WIN_BPP
, g_win_flags
);
70 error("Unable to switch to the window mode: %s", SDL_GetError());
78 * Sets video mode and sets up OpenGL for this change
80 SDL_Surface
*SetVideoMode(int width
, int height
, int bpp
, Uint32 flags
)
82 SDL_Surface
*rval
= 0;
84 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER
, 1);
85 rval
= SDL_SetVideoMode(width
, height
, bpp
, flags
);
87 glClearColor(0.0f
, 0.0f
, 0.0f
, 0.5f
);
88 glColor4f(1.0f
, 1.0f
, 1.0f
, 1.0f
);
90 glDisable(GL_DEPTH_TEST
);
93 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
94 #ifdef SDL_GL_SWAP_CONTROL
95 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL
, 1);
96 #endif /* SDL_GL_SWAP_CONTROL */
98 glViewport(0, 0, width
, height
);
99 glMatrixMode(GL_PROJECTION
);
102 glOrtho(0.0f
, width
, height
, 0.0f
, -1.0f
, 1.0f
);
104 glMatrixMode(GL_MODELVIEW
);
107 glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
108 glEnable(GL_TEXTURE_2D
);
112 #endif /* SUPPORT_OPENGL */
116 debug("Initializing SDL");
118 /* initialization of SDL */
119 if (SDL_Init(SDL_SUBSYSTEMS
) == -1) {
120 fatal("Unable to initialize SDL: %s", SDL_GetError());
126 #ifndef SUPPORT_OPENGL
128 g_win_flags
= SDL_SWSURFACE
| SDL_DOUBLEBUF
| SDL_ANYFORMAT
;
130 screen
= SDL_SetVideoMode(WINDOW_SIZE_X
, WINDOW_SIZE_Y
, 0, g_win_flags
);
131 #else /* SUPPORT_OPENGL */
132 use_open_gl
= !isParamFlag("--disable-opengl");
134 if (interface_is_use_open_gl()) {
135 g_win_flags
= SDL_OPENGL
;
136 screen
= SetVideoMode(WINDOW_SIZE_X
, WINDOW_SIZE_Y
, 0, g_win_flags
);
138 if (screen
== NULL
) {
140 g_win_flags
= SDL_HWSURFACE
| SDL_DOUBLEBUF
| SDL_ANYFORMAT
;
142 screen
= SDL_SetVideoMode(WINDOW_SIZE_X
, WINDOW_SIZE_Y
, 0, g_win_flags
);
145 g_win_flags
= SDL_HWSURFACE
| SDL_DOUBLEBUF
| SDL_ANYFORMAT
;
146 screen
= SDL_SetVideoMode(WINDOW_SIZE_X
, WINDOW_SIZE_Y
, 0, g_win_flags
);
148 #endif /* SUPPORT_OPENGL */
150 if (screen
== NULL
) {
151 error("Unable to create interface: %s", SDL_GetError());
156 * enable unicode by default for keyboard support - it is a bit more
157 * consuming than the nonsdl but can draw more characters
159 SDL_EnableUNICODE(1);
160 SDL_WM_SetCaption(WINDOW_TITLE
, NULL
);
162 /* keyboard repeating */
163 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY
, SDL_DEFAULT_REPEAT_INTERVAL
);
164 timer
= SDL_AddTimer(INTERVAL
, TimerCallback
, NULL
);
166 keyboard_buffer_init(KEYBOARD_BUFFER_SIZE
);
169 hot_key_register(SDLK_F1
, hotkey_screen
);
171 srand((unsigned) time(NULL
));
172 isInterfaceInit
= TRUE
;
177 SDL_Surface
*interface_get_screen()
179 /*return my_surface;*/
183 void interface_refresh()
185 #ifndef SUPPORT_OPENGL
187 #else /* SUPPORT_OPENGL */
188 if (interface_is_use_open_gl()) {
189 SDL_GL_SwapBuffers();
193 #endif /* SUPPORT_OPENGL */
196 void interface_get_mouse_position(int *x
, int *y
)
198 SDL_GetMouseState(x
, y
);
201 int interface_is_mouse_clicket()
203 return SDL_GetMouseState(NULL
, NULL
) & SDL_BUTTON(SDL_BUTTON_LEFT
);
206 int interface_is_press_any_key()
211 mapa
= SDL_GetKeyState(NULL
);
213 for (i
= SDLK_BACKSPACE
; i
< SDLK_KP9
; i
++) {
214 if (mapa
[i
] == SDL_PRESSED
) {
222 void printPressAnyKey()
227 mapa
= SDL_GetKeyState(NULL
);
229 for (i
= SDLK_BACKSPACE
; i
< SDLK_KP9
; i
++) {
230 if (mapa
[i
] == SDL_PRESSED
) {
231 debug("Pressed key [SDL: %d]", i
);
239 static time_t lastTime
= 0;
240 static bool_t isSlowHack
= FALSE
;
243 currentTime
= timer_get_current_time();
246 lastTime
= currentTime
;
250 /*printf("DEBUG: time interval %d\n", currentTime - lastTime);*/
252 if (isSlowHack
== FALSE
&& currentTime
- lastTime
>= 100) {
253 /*printf("start slow hack (%d)\n", currentTime - lastTime);*/
255 lastTime
= timer_get_current_time();
259 if (isSlowHack
== TRUE
&& currentTime
- lastTime
>= 50) {
260 /*printf("stop slow hack (%d)\n", currentTime - lastTime);*/
262 lastTime
= timer_get_current_time();
266 if (isSlowHack
== TRUE
) {
267 /*printf("hack time interval %d\n", currentTime - lastTime);*/
270 lastTime
= timer_get_current_time();
279 while (SDL_WaitEvent(&event
)) {
280 switch (event
.type
) {
281 case SDL_MOUSEBUTTONDOWN
:
282 mouse_buffer_event(&event
.button
);
286 switch (event
.key
.keysym
.sym
) {
288 /* it is not necessary to always use the buffer */
289 if (keyboardBufferEnabled
== TRUE
) {
290 keyboard_buffer_push(event
.key
.keysym
);
297 switch (event
.user
.code
) {
307 mouse_buffer_clean();
315 /* change the window size */
316 case SDL_VIDEORESIZE
:
317 screen
= SDL_SetVideoMode(event
.resize
.w
, event
.resize
.h
,
318 WIN_BPP
, g_win_flags
);
320 if (screen
== NULL
) {
321 error("Unable to change resolution of the window: %s", SDL_GetError());
326 /* termination request */
339 void interface_event()
342 if (eventAction() == -1) {
348 void interface_quit()
350 debug("Shutting down SDL");
353 keyboard_buffer_quit();
357 isInterfaceInit
= FALSE
;