Include and link physfs properly.
[tuxanci.git] / src / client / interface.c
blob62d7f96dca3d71d1b24e685178b187a312010f66
1 #include <stdlib.h>
3 #include "main.h"
4 #include "myTimer.h"
6 #include "interface.h"
7 #include "screen.h"
8 #include "keyboardBuffer.h"
9 #include "hotKey.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()
23 return use_open_gl;
26 bool_t interface_is_inicialized()
28 return isInterfaceInit;
31 static Uint32 TimerCallback(Uint32 interval, void *param)
33 SDL_Event event;
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);
42 return interval;
45 void interface_enable_keyboard_buffer()
47 keyboardBufferEnabled = TRUE;
50 void interface_disable_keyboard_buffer()
52 keyboardBufferEnabled = FALSE;
55 void hotkey_screen()
57 if (g_win_flags & SDL_FULLSCREEN) {
58 g_win_flags &= ~SDL_FULLSCREEN;
59 } else {
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);
69 if (screen == NULL) {
70 error("Unable to switch to the window mode: %s", SDL_GetError());
71 return;
76 #ifdef SUPPORT_OPENGL
77 /**
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);
89 glClearDepth(1.0);
90 glDisable(GL_DEPTH_TEST);
92 glEnable(GL_BLEND);
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);
100 glLoadIdentity();
102 glOrtho(0.0f, width, height, 0.0f, -1.0f, 1.0f);
104 glMatrixMode(GL_MODELVIEW);
105 glLoadIdentity();
107 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
108 glEnable(GL_TEXTURE_2D);
110 return rval;
112 #endif /* SUPPORT_OPENGL */
114 int interface_init()
116 debug("Initializing SDL");
118 /* initialization of SDL */
119 if (SDL_Init(SDL_SUBSYSTEMS) == -1) {
120 fatal("Unable to initialize SDL: %s", SDL_GetError());
121 SDL_Quit();
122 exit(0);
123 return -1;
126 #ifndef SUPPORT_OPENGL
127 use_open_gl = FALSE;
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) {
139 use_open_gl = FALSE;
140 g_win_flags = SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_ANYFORMAT;
142 screen = SDL_SetVideoMode(WINDOW_SIZE_X, WINDOW_SIZE_Y, 0, g_win_flags);
144 } else {
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());
152 SDL_Quit();
153 return -1;
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);
168 hot_key_init();
169 hot_key_register(SDLK_F1, hotkey_screen);
171 srand((unsigned) time(NULL));
172 isInterfaceInit = TRUE;
174 return 0;
177 SDL_Surface *interface_get_screen()
179 /*return my_surface;*/
180 return screen;
183 void interface_refresh()
185 #ifndef SUPPORT_OPENGL
186 SDL_Flip(screen);
187 #else /* SUPPORT_OPENGL */
188 if (interface_is_use_open_gl()) {
189 SDL_GL_SwapBuffers();
190 } else {
191 SDL_Flip(screen);
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()
208 Uint8 *mapa;
209 int i;
211 mapa = SDL_GetKeyState(NULL);
213 for (i = SDLK_BACKSPACE; i < SDLK_KP9; i++) {
214 if (mapa[i] == SDL_PRESSED) {
215 return 1;
219 return 0;
222 void printPressAnyKey()
224 Uint8 *mapa;
225 int i;
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);
237 int hack_slow()
239 static time_t lastTime = 0;
240 static bool_t isSlowHack = FALSE;
241 time_t currentTime;
243 currentTime = timer_get_current_time();
245 if (lastTime == 0) {
246 lastTime = currentTime;
247 return 0;
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);*/
254 isSlowHack = TRUE;
255 lastTime = timer_get_current_time();
256 return 1;
259 if (isSlowHack == TRUE && currentTime - lastTime >= 50) {
260 /*printf("stop slow hack (%d)\n", currentTime - lastTime);*/
261 isSlowHack = FALSE;
262 lastTime = timer_get_current_time();
263 return 0;
266 if (isSlowHack == TRUE) {
267 /*printf("hack time interval %d\n", currentTime - lastTime);*/
270 lastTime = timer_get_current_time();
272 return isSlowHack;
275 int eventAction()
277 SDL_Event event;
279 while (SDL_WaitEvent(&event)) {
280 switch (event.type) {
281 case SDL_MOUSEBUTTONDOWN:
282 mouse_buffer_event(&event.button);
283 break;
285 case SDL_KEYDOWN:
286 switch (event.key.keysym.sym) {
287 default:
288 /* it is not necessary to always use the buffer */
289 if (keyboardBufferEnabled == TRUE) {
290 keyboard_buffer_push(event.key.keysym);
292 break;
294 break;
296 case SDL_USEREVENT:
297 switch (event.user.code) {
298 case USR_EVT_TIMER:
299 if (hack_slow()) {
300 break;
303 screen_draw();
304 screen_event();
305 hot_key_event();
306 screen_switch();
307 mouse_buffer_clean();
308 break;
310 default:
311 break;
313 break;
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());
322 return 0;
324 break;
326 /* termination request */
327 case SDL_QUIT:
328 return -1;
329 break;
331 default:
332 break;
336 return 0;
339 void interface_event()
341 while (1) {
342 if (eventAction() == -1) {
343 return;
348 void interface_quit()
350 debug("Shutting down SDL");
352 hot_key_quit();
353 keyboard_buffer_quit();
355 SDL_Quit();
357 isInterfaceInit = FALSE;