File: input.c

package info (click to toggle)
freedroidrpg 0.16.1-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 260,740 kB
  • sloc: ansic: 63,915; sh: 4,588; makefile: 503; python: 178; xml: 54; sed: 16
file content (283 lines) | stat: -rw-r--r-- 6,360 bytes parent folder | download | duplicates (3)
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
/* 
 *
 *   Copyright (c) 1994, 2002, 2003 Johannes Prix
 *   Copyright (c) 1994, 2002 Reinhard Prix
 *   Copyright (c) 2004-2007 Arthur Huillet 
 *
 *
 *  This file is part of Freedroid
 *
 *  Freedroid is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  Freedroid is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Freedroid; see the file COPYING. If not, write to the 
 *  Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
 *  MA  02111-1307  USA
 *
 */
/**
 * This file contains functions for keyboard and mouse events.
 */

#define _input_c

#include "struct.h"
#include "global.h"
#include "proto.h"
#include "widgets/widgets.h"

#ifndef SDL_BUTTON_WHEELUP
#define SDL_BUTTON_WHEELUP 4
#endif
#ifndef SDL_BUTTON_WHEELDOWN
#define SDL_BUTTON_WHEELDOWN 5
#endif

static Uint8 *key_state_array;		/* SDL keyboard state internal array */
static int key_state_array_size;

static Uint8 mouse_state_last_frame;	/* mouse states reference */
static Uint8 mouse_state_this_frame;	/* current mouse state */

static int MouseWheelUpMovesRecorded;
static int MouseWheelDownMovesRecorded;

void init_keyboard_input_array()
{
	key_state_array = SDL_GetKeyState(&key_state_array_size);
}

int GetMousePos_x(void)
{
	int x;
	SDL_GetMouseState(&x, NULL);
	return x;
};

int GetMousePos_y(void)
{
	int y;
	SDL_GetMouseState(NULL, &y);
	return y;
};

/**
 * Save the current mouse state to mouse_state_last_frame, so
 * the *Clicked functions have a point of comparison.
 */
void save_mouse_state()
{
	mouse_state_last_frame = mouse_state_this_frame;
	mouse_state_this_frame = SDL_GetMouseState(NULL, NULL);
}

static void input_mouse_motion(SDL_Event * event)
{
	if (game_status != INSIDE_LVLEDITOR) {
		input_axis.x = event->motion.x - UserCenter_x;
		input_axis.y = event->motion.y - UserCenter_y;
	}
}

static void input_mouse_button(SDL_Event * event)
{
	if (game_status != INSIDE_LVLEDITOR) {
		if (event->type == SDL_MOUSEBUTTONUP)
			return;
		input_axis.x = event->button.x - UserCenter_x;
		input_axis.y = event->button.y - UserCenter_y;

		if (event->button.button == SDL_BUTTON_WHEELUP)
			MouseWheelUpMovesRecorded++;

		if (event->button.button == SDL_BUTTON_WHEELDOWN)
			MouseWheelDownMovesRecorded++;
	}
}

int input_handle(void)
{
	SDL_Event event;

	while (SDL_PollEvent(&event)) {

		switch (event.type) {
		case SDL_QUIT:
			printf("\n\nUser requested termination...\n\nTerminating...");
			Terminate(EXIT_SUCCESS);
			break;

		case SDL_KEYDOWN:
			input_key_press(&event);
			break;

		case SDL_MOUSEMOTION:
			input_mouse_motion(&event);
			break;

		case SDL_MOUSEBUTTONDOWN:
		case SDL_MOUSEBUTTONUP:
			input_mouse_button(&event);
			break;

		default:
			break;
		}

		handle_widget_event(&event);
	}
	return 0;
}

int MouseWheelUpPressed(void)
{
	if (MouseWheelUpMovesRecorded) {
		MouseWheelUpMovesRecorded--;
		return (TRUE);
	} else
		return (FALSE);
}

int MouseWheelDownPressed(void)
{
	if (MouseWheelDownMovesRecorded) {
		MouseWheelDownMovesRecorded--;
		return (TRUE);
	} else
		return (FALSE);
}

static int key_is_pressed(int key)
{
	SDL_PumpEvents();
	return key_state_array[key];
}

/* *Pressed functions return the current state of the keyboard/mouse button in question */
int LeftPressed()
{
	return key_is_pressed(SDLK_LEFT);
}

int RightPressed()
{
	return key_is_pressed(SDLK_RIGHT);
}

int UpPressed()
{
	return key_is_pressed(SDLK_UP);
}

int DownPressed()
{
	return key_is_pressed(SDLK_DOWN);
}

int SpacePressed()
{
	return key_is_pressed(SDLK_SPACE);
}

int EnterPressed()
{
	return key_is_pressed(SDLK_RETURN) || key_is_pressed(SDLK_KP_ENTER);
}

int EscapePressed()
{
	return key_is_pressed(SDLK_ESCAPE);
}

int LeftCtrlPressed()
{
	return key_is_pressed(SDLK_LCTRL);
}

int CtrlPressed()
{
	return key_is_pressed(SDLK_LCTRL) || key_is_pressed(SDLK_RCTRL);
}

int ShiftPressed()
{
	return key_is_pressed(SDLK_RSHIFT) || key_is_pressed(SDLK_LSHIFT);
}

int APressed()
{
	return key_is_pressed(SDLK_a);
}

int QPressed()
{
	return key_is_pressed(SDLK_q);
}

int XPressed()
{
	return key_is_pressed(SDLK_x);
}

int MouseRightPressed()
{
	SDL_PumpEvents();
	return ((SDL_GetMouseState(NULL, NULL)) & (SDL_BUTTON(3)));
}

int MouseLeftPressed()
{
	SDL_PumpEvents();
	return (SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(1));
}

/* *Clicked functions return non zero if the mouse button is question has just been clicked, i.e. it is pressed now and wasn't at last frame.
 * "last frame" is whenever you called save_mouse_state
 */
int MouseRightClicked()
{
	return (!(mouse_state_last_frame & SDL_BUTTON(3)) && (mouse_state_this_frame & SDL_BUTTON(3)));
}

int MouseLeftClicked()
{
	return (!(mouse_state_last_frame & SDL_BUTTON(1)) && (mouse_state_this_frame & SDL_BUTTON(1)));
}

/**
 * \brief Wait until there is no more interaction event pending.
 *
 * \details Some UI screens (the dialog screen, for instance) have their own
 * interaction loop, and do not use the main input_handle() function.
 * When such a UI screen is closed, the state of the input devices (pressed
 * keys, cursor position and so on) can be incoherent with the interaction
 * state internally computed and stored by input_handle().
 * This function waits for a 'neutral' interaction state, and is to be used
 * before to return to the main game screen.
 */
void WaitNoEvent()
{
	SDL_Event event;
	int mouse_x, mouse_y;

	// Because we are still using a mix of event-based and state-based
	// interaction, it is not easy to define what a 'neutral' state is.
	// We at least wait for the release of the 'common' used keys, and
	// set the cursor position to the current mouse position.

	while ( (SDL_PumpEvents(), SDL_GetMouseState(&mouse_x, &mouse_y) & (SDL_BUTTON(1) | SDL_BUTTON(3))) ||
			EnterPressed() || EscapePressed() || SDL_PollEvent(&event))
		SDL_Delay(1);
	input_axis.x = mouse_x - UserCenter_x;
	input_axis.y = mouse_y - UserCenter_y;
}

#undef _input_c