1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
#include <stdlib.h>
#include "main.h"
#include "myTimer.h"
#include "interface.h"
#include "screen.h"
#include "keyboardBuffer.h"
#include "hotKey.h"
// window surface
static SDL_Surface *screen;
//static SDL_Surface *my_surface;
// timer
static SDL_TimerID timer;
// window flags
static Uint32 g_win_flags;
static bool_t isInterfaceInit = FALSE;
// flag, kterym se ridi zarazovani klaves do bufferu
static bool_t keyboardBufferEnabled = FALSE;
static bool_t use_open_gl;
bool_t interface_is_use_open_gl()
{
return use_open_gl;
}
bool_t interface_is_inicialized()
{
return isInterfaceInit;
}
static Uint32 TimerCallback(Uint32 interval, void *param)
{
SDL_Event event;
event.type = SDL_USEREVENT;
event.user.code = USR_EVT_TIMER;
event.user.data1 = NULL;
event.user.data2 = NULL;
SDL_PushEvent(&event);
return interval;
}
void interface_enable_keyboard_buffer()
{
keyboardBufferEnabled = TRUE;
}
void interface_disable_keyboard_buffer()
{
keyboardBufferEnabled = FALSE;
}
void hotkey_screen()
{
if (g_win_flags & SDL_FULLSCREEN)
g_win_flags &= ~SDL_FULLSCREEN;
else
g_win_flags |= SDL_FULLSCREEN;
if (SDL_WM_ToggleFullScreen(screen) == 0) {
fprintf(stderr, _("Unable to switch to FULLSCREEN mode!\n"));
SDL_FreeSurface(screen);
screen = SDL_SetVideoMode(WINDOW_SIZE_X, WINDOW_SIZE_Y, WIN_BPP, g_win_flags);
if (screen == NULL) {
fprintf(stderr, _("Unable to switch to WINDOW mode! Error: %s\n"),
SDL_GetError());
return;
}
}
}
#ifdef SUPPORT_OPENGL
/*
* Sets video mode and sets up OpenGL for this change
*/
SDL_Surface * SetVideoMode(int width, int height, int bpp, Uint32 flags)
{
SDL_Surface *rval = 0;
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
rval = SDL_SetVideoMode(width, height, bpp, flags);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glClearDepth(1.0);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
#ifdef SDL_GL_SWAP_CONTROL
SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
#endif
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f,width, height,0.0f,-1.0f,1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glEnable(GL_TEXTURE_2D);
return rval;
}
#endif
int interface_init()
{
#ifdef DEBUG
DEBUG_MSG(_("Initializing SDL system\n"));
#endif
// initialization of SDL
if (SDL_Init(SDL_SUBSYSTEMS) == -1) {
fprintf(stderr, "SDL_Init %s\n", SDL_GetError());
SDL_Quit();
return -1;
}
#ifndef SUPPORT_OPENGL
use_open_gl = FALSE;
g_win_flags = SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_ANYFORMAT;
screen = SDL_SetVideoMode(WINDOW_SIZE_X, WINDOW_SIZE_Y, 0, g_win_flags);
#endif
#ifdef SUPPORT_OPENGL
use_open_gl = ! isParamFlag("--disable-opengl");
if( interface_is_use_open_gl() )
{
g_win_flags = SDL_OPENGL;
screen = SetVideoMode(WINDOW_SIZE_X, WINDOW_SIZE_Y, 0, g_win_flags);
if( screen == NULL )
{
use_open_gl = FALSE;
g_win_flags = SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_ANYFORMAT;
screen = SDL_SetVideoMode(WINDOW_SIZE_X, WINDOW_SIZE_Y, 0, g_win_flags);
}
}
else
{
g_win_flags = SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_ANYFORMAT;
screen = SDL_SetVideoMode(WINDOW_SIZE_X, WINDOW_SIZE_Y, 0, g_win_flags);
}
#endif
if (screen == NULL) {
fprintf(stderr, "SDL_SetVideoMode %s\n", SDL_GetError());
SDL_Quit();
return -1;
}
// Enable unicode by default for keyboard support.
// It is bit more consuming than the nonsdl but can draw more characters.
SDL_EnableUNICODE(1);
SDL_WM_SetCaption(WINDOW_TITLE, NULL);
// Keyboard repeating
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
timer = SDL_AddTimer(INTERVAL, TimerCallback, NULL);
keyboard_buffer_init(KEYBOARD_BUFFER_SIZE);
hot_key_init();
hot_key_register(SDLK_F1, hotkey_screen);
srand((unsigned) time(NULL));
isInterfaceInit = TRUE;
return 0;
}
SDL_Surface *interface_get_screen()
{
//return my_surface;
return screen;
}
void interface_refresh()
{
#ifndef SUPPORT_OPENGL
SDL_Flip(screen);
#endif
#ifdef SUPPORT_OPENGL
if( interface_is_use_open_gl() )
{
SDL_GL_SwapBuffers();
}
else
{
SDL_Flip(screen);
}
#endif
}
void interface_get_mouse_position(int *x, int *y)
{
SDL_GetMouseState(x, y);
}
int interface_is_mouse_clicket()
{
return SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT);
}
int interface_is_press_any_key()
{
Uint8 *mapa;
int i;
mapa = SDL_GetKeyState(NULL);
for (i = SDLK_BACKSPACE; i < SDLK_KP9; i++)
if (mapa[i] == SDL_PRESSED) {
return 1;
}
return 0;
}
void printPressAnyKey()
{
Uint8 *mapa;
int i;
mapa = SDL_GetKeyState(NULL);
for (i = SDLK_BACKSPACE; i < SDLK_KP9; i++)
if (mapa[i] == SDL_PRESSED) {
printf(_("Pressed key with SDL value: %d\n"), i);
}
}
int hack_slow()
{
static time_t lastTime = 0;
static bool_t isSlowHack = FALSE;
time_t currentTime;
currentTime = timer_get_current_time();
if( lastTime == 0 )
{
lastTime = currentTime;
return 0;
}
//printf("DEBUG: time interval %d\n", currentTime - lastTime);
if( isSlowHack == FALSE && currentTime - lastTime >= 100 )
{
//printf("start slow hack (%d)\n", currentTime - lastTime);
isSlowHack = TRUE;
lastTime = timer_get_current_time();
return 1;
}
if( isSlowHack == TRUE && currentTime - lastTime >= 50 )
{
//printf("stop slow hack (%d)\n", currentTime - lastTime);
isSlowHack = FALSE;
lastTime = timer_get_current_time();
return 0;
}
if( isSlowHack == TRUE )
{
//printf("hack time interval %d\n", currentTime - lastTime);
}
lastTime = timer_get_current_time();
return isSlowHack;
}
int eventAction()
{
SDL_Event event;
while (SDL_WaitEvent(&event)) {
switch (event.type) {
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
default:
//neni potreba vyuzivat frontu stale
if (keyboardBufferEnabled == TRUE)
keyboard_buffer_push(event.key.keysym);
break;
}
break;
case SDL_USEREVENT:
switch (event.user.code) {
case USR_EVT_TIMER:
if( hack_slow() )
{
break;
}
screen_draw();
screen_event();
hot_key_event();
screen_switch();
break;
default:
break;
}
break;
// change of window size
case SDL_VIDEORESIZE:
screen =
SDL_SetVideoMode(event.resize.w, event.resize.h, WIN_BPP,
g_win_flags);
if (screen == NULL) {
fprintf(stderr,
_("Unable to change WINDOW resolution! Error: %s\n"),
SDL_GetError());
return 0;
}
break;
// termination request
case SDL_QUIT:
return -1;
break;
default:
break;
}
}
return 0;
}
void interface_event()
{
while (1) {
if (eventAction() == -1)
return;
}
}
void interface_quit()
{
DEBUG_MSG(_("Quitting SDL\n"));
hot_key_quit();
keyboard_buffer_quit();
SDL_Quit();
isInterfaceInit = FALSE;
}
|