summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/CMakeLists.txt1
-rw-r--r--src/base/arena.c20
-rw-r--r--src/base/arena.h1
-rw-r--r--src/client/chat.c67
-rw-r--r--src/client/game.c1
-rw-r--r--src/client/hotKey.c158
-rw-r--r--src/client/hotKey.h16
-rwxr-xr-xsrc/client/interface.c57
-rw-r--r--src/client/pauza.c38
-rw-r--r--src/client/saveDialog.c34
-rwxr-xr-xsrc/net/tcp.c3
-rw-r--r--src/screen/screen_world.c26
12 files changed, 298 insertions, 124 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index d912e00..cdc636b 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -64,6 +64,7 @@ SET ( SRC_client
${WORKDIR}/client/radar ${WORKDIR}/client/panel
${WORKDIR}/client/image ${WORKDIR}/client/keyboardBuffer
${WORKDIR}/client/pauza ${WORKDIR}/client/saveDialog
+ ${WORKDIR}/client/hotKey
${WORKDIR}/screen/screen_choiceArena
${WORKDIR}/screen/screen_settingKeys ${WORKDIR}/screen/screen_table
diff --git a/src/base/arena.c b/src/base/arena.c
index 3a72eba..bab0ad3 100644
--- a/src/base/arena.c
+++ b/src/base/arena.c
@@ -11,6 +11,7 @@
#include "item.h"
#include "myTimer.h"
#include "modules.h"
+#include "hotKey.h"
#ifndef PUBLIC_SERVER
#include "screen_world.h"
@@ -30,6 +31,18 @@ arena_t* getCurrentArena()
return currentArena;
}
+void hotkey_splitArena()
+{
+ if( splitType == SCREEN_SPLIT_VERTICAL )
+ {
+ splitType = SCREEN_SPLIT_HORIZONTAL;
+ }
+ else
+ {
+ splitType = SCREEN_SPLIT_VERTICAL;
+ }
+}
+
void initArena()
{
splitType = SCREEN_SPLIT_VERTICAL;
@@ -45,6 +58,13 @@ void initArena()
splitType = SCREEN_SPLIT_HORIZONTAL;
printf("SCREEN_SPLIT_HORIZONTAL\n");
}
+
+ registerHotKey(SDLK_F3, hotkey_splitArena);
+}
+
+void quitArena()
+{
+ unregisterHotKey(SDLK_F3);
}
arena_t* newArena(int w, int h)
diff --git a/src/base/arena.h b/src/base/arena.h
index 1a326c3..5730bea 100644
--- a/src/base/arena.h
+++ b/src/base/arena.h
@@ -42,6 +42,7 @@ typedef struct arena_struct
extern void setCurrentArena(arena_t *p);
extern arena_t* getCurrentArena();
extern void initArena();
+extern void quitArena();
extern arena_t* newArena(int w, int h);
extern int conflictSpace(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2);
extern int isFreeSpace(arena_t *arena, int x, int y, int w, int h);
diff --git a/src/client/chat.c b/src/client/chat.c
index f0bf7a3..2b2805c 100644
--- a/src/client/chat.c
+++ b/src/client/chat.c
@@ -16,6 +16,7 @@
#include "chat.h"
#include "font.h"
#include "keyboardBuffer.h"
+#include "hotKey.h"
static image_t *g_chat;
@@ -29,7 +30,19 @@ static int timeBlick;
static bool_t chat_active;
static bool_t receivedNewMsg;
-static my_time_t lastActive;
+static void hotkey_chat();
+
+static void hotkey_chat()
+{
+ chat_active = TRUE;
+ receivedNewMsg = FALSE;
+
+ disableHotKey(SDLK_RETURN);
+ disableHotKey(SDLK_p);
+
+ enableKeyboardBuffer();
+ clearKeyboardBuffer();
+}
void initChat()
{
@@ -38,11 +51,12 @@ void initChat()
listText = newList();
strcpy(line, "");
chat_active = FALSE;
+ registerHotKey(SDLK_RETURN, hotkey_chat);
+
receivedNewMsg = FALSE;
line_time = 0;
line_atime = 0;
timeBlick = 0;
- lastActive = 0;
}
void drawChat()
@@ -105,7 +119,7 @@ static void processMessageKey(SDL_keysym keysym)
}
/* zpracovani backspace */
- if (keysym.sym == SDLK_BACKSPACE){
+ if (keysym.sym == SDLK_BACKSPACE && len > 0){
line[len-1]='\0';
return;
}
@@ -129,18 +143,6 @@ static void processMessageKey(SDL_keysym keysym)
return;
}
-static void switchChatActive()
-{
- if( chat_active == TRUE )
- {
- chat_active = FALSE;
- }
- else
- {
- chat_active = TRUE;
- }
-}
-
static void sendNewMessage()
{
if( getNetTypeGame() == NET_GAME_TYPE_CLIENT )
@@ -161,18 +163,14 @@ static void sendNewMessage()
static void eventChatDisable()
{
- Uint8 *mapa;
- mapa = SDL_GetKeyState(NULL);
-
- if( mapa[SDLK_RETURN] == SDL_PRESSED )
- { /* chat neni aktivni, tak jej aktivujeme */
- receivedNewMsg = FALSE;
- switchChatActive();
- memset(line, '\0', STR_SIZE);
- /* zapneme zachytavani klaves do bufferu a vycistime buffer */
- enableKeyboardBuffer();
- clearKeyboardBuffer();
- }
+ chat_active = FALSE;
+ memset(line, '\0', STR_SIZE);
+
+ enableHotKey(SDLK_RETURN);
+ enableHotKey(SDLK_p);
+
+ disableKeyboardBuffer();
+ clearKeyboardBuffer();
}
static void eventChatEnable()
@@ -197,11 +195,10 @@ static void eventChatEnable()
}
else
{ /* radek je prazdny, je potreba vypnout okno chatu */
- switchChatActive();
- memset(line, '\0', STR_SIZE);
+ eventChatDisable();
/* vypneme zachytavani klaves do bufferu a vycistime buffer */
- disableKeyboardBuffer();
- clearKeyboardBuffer();
+ //disableKeyboardBuffer();
+ //clearKeyboardBuffer();
}
}
@@ -214,11 +211,7 @@ static void eventChatEnable()
*/
void eventChat()
{
- if (isChatActive() == FALSE)
- { /* okno chatu neni aktivni */
- eventChatDisable();
- }
- else
+ if( isChatActive() )
{
eventChatEnable();
}
@@ -252,5 +245,7 @@ void addToChat(char *s)
void quitChat()
{
assert( listText != NULL );
+
+ unregisterHotKey(SDLK_RETURN);
destroyListItem(listText, free);
}
diff --git a/src/client/game.c b/src/client/game.c
index faf89bb..b0d2322 100644
--- a/src/client/game.c
+++ b/src/client/game.c
@@ -48,7 +48,6 @@ static void initGame()
initSDL();
initFont(FONT_FILE , FONT_SIZE);
initLayer();
- initArena();
initImageData();
#ifndef NO_SOUND
initAudio();
diff --git a/src/client/hotKey.c b/src/client/hotKey.c
new file mode 100644
index 0000000..43a13ca
--- /dev/null
+++ b/src/client/hotKey.c
@@ -0,0 +1,158 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+
+#include "main.h"
+#include "list.h"
+#include "myTimer.h"
+
+#include "interface.h"
+#include "hotKey.h"
+
+typedef struct hotKey_struct
+{
+ SDLKey key;
+ bool_t active;
+ void (*handler)();
+} hotKey_t;
+
+static list_t *listHotKey;
+static my_time_t lastActive;
+
+static hotKey_t* newHotKey(SDLKey key, void (*handler)())
+{
+ hotKey_t *new;
+
+ new = malloc( sizeof(hotKey_t) );
+ new->key = key;
+ new->active = TRUE;
+ new->handler = handler;
+
+ return new;
+}
+
+static void destroyHotKey(hotKey_t *hotkey)
+{
+ free(hotkey);
+}
+
+static hotKey_t* findHotkey(SDLKey key)
+{
+ int i;
+
+ for( i = 0 ; i < listHotKey->count ; i++ )
+ {
+ hotKey_t *this;
+
+ this = (hotKey_t *)listHotKey->list[i];
+
+ if( this->key == key )
+ {
+ return this;
+ }
+ }
+
+ return NULL;
+}
+
+void initHotKey()
+{
+ listHotKey = newList();
+ lastActive = getMyTime();
+}
+
+void registerHotKey(SDLKey key, void (*handler)())
+{
+ hotKey_t *hotkey;
+
+ if( findHotkey(key) != NULL )
+ {
+ assert( ! "Conflict with register key");
+ }
+
+ hotkey = newHotKey(key, handler);
+ addList(listHotKey, hotkey);
+}
+
+void unregisterHotKey(SDLKey key)
+{
+ hotKey_t *hotkey;
+ int index;
+
+ hotkey = findHotkey(key);
+
+ if( hotkey == NULL )
+ {
+ assert( ! "This hotkey not register");
+ }
+
+ index = searchListItem(listHotKey, hotkey);
+ assert( index != -1 );
+ delListItem(listHotKey, index, destroyHotKey);
+}
+
+void enableHotKey(SDLKey key)
+{
+ hotKey_t *hotkey;
+
+ hotkey = findHotkey(key);
+
+ if( hotkey == NULL )
+ {
+ assert( ! "This hotkey not register");
+ }
+
+ lastActive = getMyTime();
+ hotkey->active = TRUE;
+}
+
+void disableHotKey(SDLKey key)
+{
+ hotKey_t *hotkey;
+
+ hotkey = findHotkey(key);
+
+ if( hotkey == NULL )
+ {
+ assert( ! "This hotkey not register");
+ }
+
+ lastActive = getMyTime();
+ hotkey->active = FALSE;
+}
+
+void eventHotKey()
+{
+ my_time_t currentTime;
+ Uint8 *mapa;
+ int i;
+
+ currentTime = getMyTime();
+
+ if( currentTime - lastActive < HOTKEY_ACTIVE_INTERVAL )
+ {
+ return;
+ }
+
+ mapa = SDL_GetKeyState(NULL);
+
+ for( i = 0 ; i < listHotKey->count ; i++ )
+ {
+ hotKey_t *this;
+
+ this = (hotKey_t *)listHotKey->list[i];
+
+ if( mapa[this->key] == SDL_PRESSED && this->active == TRUE )
+ {
+ lastActive = getMyTime();
+ //printf("hotKey = %d\n", this->key);
+ this->handler();
+ }
+ }
+}
+
+void quitHotKey()
+{
+ destroyListItem(listHotKey, destroyHotKey);
+}
diff --git a/src/client/hotKey.h b/src/client/hotKey.h
new file mode 100644
index 0000000..1e15c1c
--- /dev/null
+++ b/src/client/hotKey.h
@@ -0,0 +1,16 @@
+
+#ifndef HOTKEY_H
+
+#define HOTKEY_H
+
+#define HOTKEY_ACTIVE_INTERVAL 500
+
+extern void initHotKey();
+extern void registerHotKey(SDLKey key, void (*handler)());
+extern void unregisterHotKey(SDLKey key);
+extern void enableHotKey(SDLKey key);
+extern void disableHotKey(SDLKey key);
+extern void eventHotKey();
+extern void quitHotKey();
+
+#endif
diff --git a/src/client/interface.c b/src/client/interface.c
index eb8168f..0a0b005 100755
--- a/src/client/interface.c
+++ b/src/client/interface.c
@@ -7,6 +7,7 @@
#include "interface.h"
#include "screen.h"
#include "keyboardBuffer.h"
+#include "hotKey.h"
// window surface
static SDL_Surface *screen;
@@ -49,6 +50,28 @@ void disableKeyboardBuffer(){
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;
+ }
+ }
+}
+
int initSDL()
{
#ifdef DEBUG
@@ -87,7 +110,10 @@ int initSDL()
timer = SDL_AddTimer(INTERVAL, TimerCallback, NULL);
initKeyboardBuffer(KEYBOARD_BUFFER_SIZE);
-
+
+ initHotKey();
+ registerHotKey(SDLK_F1, hotkey_screen);
+
srand((unsigned)time(NULL));
isInterfaceInit = TRUE;
@@ -110,30 +136,6 @@ void interfaceRefresh()
//SDL_UpdateRect(screen, 400, 0, 400, 600);
}
-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, _("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 0;
- }
- }
-
- return 1;
-}
-
void getMousePosition(int *x, int *y)
{
SDL_GetMouseState(x, y);
@@ -210,10 +212,11 @@ int eventAction()
return 0;
break;
*/
+/*
case SDLK_F1:
if(!toggleFullscreen())return 0;
break;
-
+*/
default:
//neni potreba vyuzivat frontu stale
if (keyboardBufferEnabled==TRUE)
@@ -228,6 +231,7 @@ int eventAction()
case USR_EVT_TIMER:
drawScreen();
eventScreen();
+ eventHotKey();
switchScreen();
break;
@@ -273,6 +277,7 @@ void quitSDL()
#ifdef DEBUG
printf(_("Quitting SDL\n"));
#endif
+ quitHotKey();
quitKeyboardBuffer();
SDL_Quit();
diff --git a/src/client/pauza.c b/src/client/pauza.c
index 97bade5..a4a9a4c 100644
--- a/src/client/pauza.c
+++ b/src/client/pauza.c
@@ -10,17 +10,10 @@
#include "interface.h"
#include "image.h"
#include "pauza.h"
+#include "hotKey.h"
static image_t *g_pauza;
static bool_t activePauza;
-static my_time_t lastActive;
-
-void initPauza()
-{
- g_pauza = addImageData("pauza.png", IMAGE_ALPHA, "pauza", IMAGE_GROUP_USER);
- activePauza = FALSE;
- lastActive = getMyTime();
-}
static void switchPauzed()
{
@@ -34,6 +27,19 @@ static void switchPauzed()
}
}
+static void hotkey_pauze()
+{
+ switchPauzed();
+}
+
+void initPauza()
+{
+ g_pauza = addImageData("pauza.png", IMAGE_ALPHA, "pauza", IMAGE_GROUP_USER);
+ activePauza = FALSE;
+
+ registerHotKey(SDLK_p, hotkey_pauze);
+}
+
void drawPauza()
{
if( activePauza )
@@ -48,21 +54,6 @@ void drawPauza()
void eventPauza()
{
- Uint8 *mapa;
- mapa = SDL_GetKeyState(NULL);
-
- if( mapa[SDLK_p] == SDL_PRESSED )
- {
- my_time_t currentTime;
-
- currentTime = getMyTime();
-
- if( currentTime - lastActive > PAUZE_ACTIVE_TIME_INTERVAL )
- {
- lastActive = currentTime;
- switchPauzed();
- }
- }
}
bool_t isPauzeActive()
@@ -72,5 +63,6 @@ bool_t isPauzeActive()
void quitPauza()
{
+ unregisterHotKey(SDLK_p);
}
diff --git a/src/client/saveDialog.c b/src/client/saveDialog.c
index 462c219..37a3d8f 100644
--- a/src/client/saveDialog.c
+++ b/src/client/saveDialog.c
@@ -4,7 +4,6 @@
#include "main.h"
#include "list.h"
-#include "myTimer.h"
#include "tux.h"
#include "space.h"
#include "arena.h"
@@ -15,6 +14,7 @@
#include "image.h"
#include "font.h"
#include "saveLoad.h"
+#include "hotKey.h"
#include "widget.h"
#include "widget_label.h"
@@ -23,7 +23,6 @@
static image_t *g_background;
static bool_t activeSaveDialog;
-static my_time_t lastActive;
static widget_t *widgetLabelMsg;
static widget_t *widgetTextFieldName;
@@ -61,10 +60,14 @@ static void eventWidget(void *p)
}
}
+static void hotkey_saveDialog()
+{
+ switchTerm();
+}
+
void initSaveDialog()
{
activeSaveDialog = FALSE;
- lastActive = getMyTime();
g_background = getImage(IMAGE_GROUP_BASE, "screen_main");
@@ -91,6 +94,8 @@ void initSaveDialog()
SAVE_DIALOG_LOCATIN_X+20+WIDGET_BUTTON_WIDTH+20,
SAVE_DIALOG_LOCATIN_Y+60,
eventWidget);
+
+ registerHotKey(SDLK_F2, hotkey_saveDialog);
}
bool_t isSaveDialogActive()
@@ -120,27 +125,6 @@ void drawSaveDialog()
void eventSaveDialog()
{
- my_time_t currentTime;
- Uint8 *mapa;
-
- if( getNetTypeGame() != NET_GAME_TYPE_NONE )
- {
- return;
- }
-
- mapa = SDL_GetKeyState(NULL);
-
- currentTime = getMyTime();
-
- if( mapa[SDLK_F2] == SDL_PRESSED )
- {
- if( currentTime - lastActive > SAVEDIALOG_ACTIVE_TIME_INTERVAL )
- {
- lastActive = currentTime;
- switchTerm();
- }
- }
-
if( activeSaveDialog == FALSE )
{
return;
@@ -154,6 +138,8 @@ void eventSaveDialog()
void quitSaveDialog()
{
+ unregisterHotKey(SDLK_F2);
+
destroyWidgetLabel(widgetLabelMsg);
destroyWidgetTextfield(widgetTextFieldName);
destroyWidgetButton(widgetButtonSave);
diff --git a/src/net/tcp.c b/src/net/tcp.c
index 0d5d03c..766fdbe 100755
--- a/src/net/tcp.c
+++ b/src/net/tcp.c
@@ -41,6 +41,7 @@ void destroySockTcp(sock_tcp_t *p)
sock_tcp_t* bindTcpSocket(char *address, int port, int proto)
{
sock_tcp_t *new;
+ unsigned long param_setsock = 1;
int len;
int ret;
@@ -70,6 +71,8 @@ sock_tcp_t* bindTcpSocket(char *address, int port, int proto)
return NULL;
}
+ setsockopt(new->sock, SOL_SOCKET, SO_REUSEADDR, (char *) &param_setsock, sizeof (param_setsock));
+
if( new->proto == PROTO_TCPv4 )
{
new->sockAddr.sin_family = AF_INET;
diff --git a/src/screen/screen_world.c b/src/screen/screen_world.c
index cbfca38..8019c86 100644
--- a/src/screen/screen_world.c
+++ b/src/screen/screen_world.c
@@ -22,6 +22,8 @@
#include "layer.h"
#include "image.h"
#include "font.h"
+
+#include "hotKey.h"
#include "panel.h"
#include "radar.h"
#include "chat.h"
@@ -371,18 +373,6 @@ static void control_keyboard_left(tux_t *tux)
}
}
-static void eventEsc()
-{
- Uint8 *mapa;
- mapa = SDL_GetKeyState(NULL);
-
- if( mapa[(SDLKey)SDLK_ESCAPE] == SDL_PRESSED )
- {
- setWorldEnd();
- return;
- }
-}
-
static void eventEnd()
{
if( isEndWorld == TRUE )
@@ -459,7 +449,6 @@ void eventWorld()
{
eventNetMultiplayer();
eventEnd();
- eventEsc();
return;
}
@@ -494,7 +483,11 @@ void eventWorld()
eventTerm();
eventSaveDialog();
- eventEsc();
+}
+
+static void hotkey_escape()
+{
+ setWorldEnd();
}
void startWorld()
@@ -502,6 +495,8 @@ void startWorld()
arena = NULL;
isEndWorld = FALSE;
+ registerHotKey(SDLK_ESCAPE, hotkey_escape);
+ initArena();
initListID();
initRadar();
initPauza();
@@ -560,6 +555,9 @@ void stoptWorld()
setAnalyze();
}
+ unregisterHotKey(SDLK_ESCAPE);
+ quitArena();
+
quitNetMultiplayer();
if( arena != NULL )