diff options
Diffstat (limited to 'src/client')
| -rw-r--r-- | src/client/chat.c | 67 | ||||
| -rw-r--r-- | src/client/game.c | 1 | ||||
| -rw-r--r-- | src/client/hotKey.c | 158 | ||||
| -rw-r--r-- | src/client/hotKey.h | 16 | ||||
| -rwxr-xr-x | src/client/interface.c | 57 | ||||
| -rw-r--r-- | src/client/pauza.c | 38 | ||||
| -rw-r--r-- | src/client/saveDialog.c | 34 |
7 files changed, 261 insertions, 110 deletions
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); |