3 * Copyright (C) 1998 Brainchild Design - http://brainchilddesign.com/
5 * Copyright (C) 2001 Chuck Mason <cemason@users.sourceforge.net>
7 * Copyright (C) 2002 Florian Schulze <crow@icculus.org>
9 * Portions of this code are from the MPEG software simulation group
10 * idct implementation. This code will be replaced with a new
11 * implementation soon.
13 * This file is part of Jump'n'Bump.
15 * Jump'n'Bump is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * Jump'n'Bump is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 #include "SDL_thread.h"
40 #include "SDL_mutex.h"
41 #include <kailleraclient.h>
44 #endif /* USE_KAILLERA */
51 /* information about the party in this session */
52 static int my_player = -1;
53 static int my_numplayers = -1;
55 /* semaphore for controlling kaillera thread */
56 static SDL_sem *game_start_sem = NULL;
58 /* keys supported on my end */
59 static int my_player_up = -1;
60 static int my_player_left = -1;
61 static int my_player_right = 1;
63 /* values for the kaillera client interface */
64 static char kaillera_app_name[] = "Jump 'n Bump";
65 static char kaillera_game_name[] = "Jump 'n Bump\0\0";
67 static int player_keys[4][3] = {
90 static int WINAPI kaillera_game_callback(char *game, int player, int numplayers)
94 unsigned char random[8];
96 if (strcmp(game, kaillera_game_name) != 0) {
97 printf("unknown game selected: %s\n", game);
103 printf("start network game with %d players\n", numplayers);
104 printf("I am player %d\n", player);
107 my_numplayers = numplayers;
109 my_player_up = player_keys[player-1][0] & 0xff;
110 my_player_left = player_keys[player-1][1] & 0xff;
111 my_player_right = player_keys[player-1][2] & 0xff;
113 /* initialize randomizer agreed by all players */
114 random[0] = time(0) & 0xff;
115 random[1] = random[2] = random[3] = 0x00;
116 length = kailleraModifyPlayValues(&random, sizeof(random[0]));
121 urand = random[3] << 24 | random[2] << 16 | random[1] << 8 | random[0];
126 SDL_SemPost(game_start_sem);
130 static kailleraInfos kaillera_data = {
133 kaillera_game_callback,
139 static void print_version()
143 kailleraGetVersion(version);
144 printf("using kaillera version %s\n", version);
147 static int kaillera_thread(void *arg)
151 /* print_version(); */
153 kailleraSetInfos(&kaillera_data);
155 kailleraSelectServerDialog(0);
156 if (SDL_SemValue(game_start_sem) == 0) {
157 /* server dialog returned and game didnt start */
159 /* release blocking thread */
161 SDL_SemPost(game_start_sem);
167 static int start_kaillera_thread(void)
171 game_start_sem = SDL_CreateSemaphore(0);
173 thread = SDL_CreateThread(kaillera_thread, NULL);
175 printf("SDL_CreateThread failed\n");
182 int addkey(unsigned int key)
184 /* it doesnt matter if a player presses keys
185 * that control other bunnies. whatever is sent
186 * is packed by pack_keys()
188 if (!(key & 0x8000)) {
189 local_keyb[key & 0x7fff] = 1;
191 local_keyb[key & 0x7fff] = 0;
195 void remove_keyb_handler(void)
204 rv = local_keyb[my_player_up];
205 rv |= local_keyb[my_player_left] << 1;
206 rv |= local_keyb[my_player_right] << 2;
207 rv |= local_keyb[1] << 3;
211 void unpack_keys(int player, char value)
213 keyb[player_keys[player][0] & 0xff] = (value >> 0) & 1;
214 keyb[player_keys[player][1] & 0xff] = (value >> 1) & 1;
215 keyb[player_keys[player][2] & 0xff] = (value >> 2) & 1;
217 /* escape key is shared among all users */
218 keyb[1] |= (value >> 3) & 1;
221 int update_kaillera_keys(void)
227 keys[0] = pack_keys();
228 length = kailleraModifyPlayValues(&keys, sizeof(keys[0]));
231 /* terminate session */
232 printf("** LOST CONNECTION **\n");
238 for (player=0; player<length; player++) {
239 unpack_keys(player, keys[player]);
245 int hook_keyb_handler(void)
247 SDL_EnableUNICODE(1);
248 memset((void *) last_keys, 0, sizeof(last_keys));
250 start_kaillera_thread();
251 SDL_SemWait(game_start_sem);
253 printf("GAME ABORTED!\n");
257 printf("GAME STARTS!\n");
261 int key_pressed(int key)
263 if (key == 1 && my_player < 0) {
264 /* if game completed or aborted, post ESC */
268 return keyb[(unsigned char) key];
271 #else /* USE_KAILLERA */
273 int addkey(unsigned int key)
277 if (!(key & 0x8000)) {
278 keyb[key & 0x7fff] = 1;
279 for (c1 = 48; c1 > 0; c1--)
280 last_keys[c1] = last_keys[c1 - 1];
281 last_keys[0] = key & 0x7fff;
283 keyb[key & 0x7fff] = 0;
287 void remove_keyb_handler(void)
291 int hook_keyb_handler(void)
293 SDL_EnableUNICODE(1);
294 memset((void *) last_keys, 0, sizeof(last_keys));
299 int key_pressed(int key)
301 return keyb[(unsigned char) key];
305 #endif /* USE_KAILLERA */
311 static int last_time = 0;
314 while (SDL_PollEvent(&e)) {
316 case SDL_MOUSEBUTTONDOWN:
320 switch (e.key.keysym.sym) {
322 if (e.type == SDL_KEYDOWN) {
328 if (e.type == SDL_KEYDOWN) {
333 if (e.type == SDL_KEYUP)
339 e.key.keysym.sym &= 0x7f;
340 if (e.type == SDL_KEYUP)
341 e.key.keysym.sym |= 0x8000;
342 addkey(e.key.keysym.sym);
354 now = SDL_GetTicks();
355 time_diff = now - last_time;
357 i = time_diff / (1000 / 60);
363 tmp = (1000/60) - i - 10;
372 then = (1000 / 60) - (now - then);
373 if (then > 0 && then < 1000)
380 if (my_player >= 0) {
381 update_kaillera_keys();
384 #endif /* USE_KAILLERA */