summaryrefslogtreecommitdiff
path: root/src/interface.c
blob: 42ced59fbcec7fad14462e965c2a090f1604f3ea (plain)
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

#include "main.h"
#include "interface.h"
#include "screen.h"

//povrch okna
static SDL_Surface *screen;
static SDL_Surface *fake_screen;

//casovac
static SDL_TimerID timer;

//priznaky okna
static Uint32 g_win_flags = SDL_HWSURFACE|SDL_DOUBLEBUF;

static bool_t isInterfaceInit = FALSE;

bool_t isInterfaceInicialized()
{
	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;
}

int initSDL()
{
	printf("init SDL..\n");

	//inicializujem SDL
	if( SDL_Init(SDL_SUBSYSTEMS) == -1 )
	{
		fprintf(stderr, "%s\n", SDL_GetError());
		SDL_Quit();
		return -1;
	}

	//inicializujem okno
	//screen = SDL_SetVideoMode(WINDOW_SIZE_X, WINDOW_SIZE_Y, 0, g_win_flags);
	screen = SDL_SetVideoMode(WINDOW_SIZE_X, WINDOW_SIZE_Y, 0, g_win_flags);

        fake_screen = SDL_CreateRGBSurface(screen->flags, WINDOW_SIZE_X, WINDOW_SIZE_Y,
		screen->format->BitsPerPixel,
		screen->format->Rmask,	screen->format->Gmask,
		screen->format->Bmask,	screen->format->Amask);

	if (screen == NULL )
	{
		fprintf(stderr, "%s\n", SDL_GetError());
		SDL_Quit();
		return -1;
	}

	SDL_WM_SetCaption(WINDOW_TITLE, NULL);
	//SDL_ShowCursor(0);
	//SDL_EnableKeyRepeat(1,1);
	timer = SDL_AddTimer(INTERVAL, TimerCallback, NULL);

	srand((unsigned)time(NULL));
	isInterfaceInit = TRUE;

	return 0;
}

SDL_Surface* getSDL_Screen()
{
	//return fake_screen;
	return screen;
}

void interfaceRefresh()
{
	//SDL_BlitSurface(fake_screen, NULL, screen, NULL);
	SDL_UpdateRect(screen, 0, 0, screen->w, screen->h);
	//SDL_Flip(screen);
}

int toggleFullscreen()
{
	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, "Nemozem prepnut do fullscreenu!\n");

		SDL_FreeSurface(screen);
		screen = SDL_SetVideoMode(WINDOW_SIZE_X, WINDOW_SIZE_Y, WIN_BPP, g_win_flags);

		if(screen == NULL)
		{
			fprintf(stderr, "Nemozem premnut do okna:%s\n", SDL_GetError());
			return 0;
		}
	}

	return 1;
}

void getMousePosition(int *x, int *y)
{
	SDL_GetMouseState(x, y);
}

int isMouseClicked()
{
	return SDL_GetMouseState(NULL,NULL) & SDL_BUTTON(SDL_BUTTON_LEFT);
}

/*
static void action_esc()
{
	if( strcmp(getScreen(), "world") == 0 )
	{
		setScreen("analyze");
	}

	if( strcmp(getScreen(), "mainMenu") == 0 )
	{
		quit();
	}

}
*/

int eventAction()
{
	SDL_Event event;

	while(SDL_WaitEvent(&event))
	{
		switch(event.type)
		{
		case SDL_KEYDOWN:
			switch(event.key.keysym.sym)
			{
/*
				case SDLK_ESCAPE:
					action_esc();
					return 0;
				break;
*/
				case SDLK_F1:
					if(!toggleFullscreen())return 0;
				break;

				default:
				break;
			}
		break;

		case SDL_USEREVENT:
			switch(event.user.code)
			{
				case USR_EVT_TIMER:
					drawScreen();
					eventScreen();
				break;

				default:
				break;
			}
		break;

		// Zmena velikosti okna
		case SDL_VIDEORESIZE:
			screen = SDL_SetVideoMode(event.resize.w,event.resize.h, WIN_BPP, g_win_flags);

			if(screen == NULL)
			{
				fprintf(stderr, "Nemozem zmenit rozlisene okna: %s\n",SDL_GetError());
				return 0;
			}
		break;

		// Pozadavek na ukonceni
		case SDL_QUIT:
			return -1;
		break;

		default:
		break;
		}
	}

	return 0;
}

void eventSDL()
{
	while(1)
	{
		if ( eventAction() == -1 )return;
	}
}

void quitSDL()
{
	printf("quit SDL..\n");
	SDL_Quit();
	isInterfaceInit = FALSE;
}