diff options
| author | oroborus <oroborus@0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e> | 2008-08-03 18:46:22 +0000 |
|---|---|---|
| committer | oroborus <oroborus@0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e> | 2008-08-03 18:46:22 +0000 |
| commit | b5723cbd822b2359850c121ca69e682a607d91bd (patch) | |
| tree | c731a5c07d6188b1bdce6e90b5ac02e35800716d /src/client | |
| parent | 00c9cebac684c42741e924d11038d5591e87dacd (diff) | |
- pridany kapov patch na klavesy
- widget na nastavovanie klaves sa rozsvieti, ked je aktivny
git-svn-id: http://opensvn.csie.org/tuxanci_ng@189 0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e
Diffstat (limited to 'src/client')
| -rw-r--r-- | src/client/chat.c | 226 | ||||
| -rw-r--r-- | src/client/chat.h | 2 | ||||
| -rwxr-xr-x | src/client/interface.c | 22 | ||||
| -rwxr-xr-x | src/client/interface.h | 5 | ||||
| -rw-r--r-- | src/client/keyboardBuffer.c | 138 | ||||
| -rw-r--r-- | src/client/keyboardBuffer.h | 34 |
6 files changed, 298 insertions, 129 deletions
diff --git a/src/client/chat.c b/src/client/chat.c index ade5452..f0bf7a3 100644 --- a/src/client/chat.c +++ b/src/client/chat.c @@ -1,11 +1,13 @@ #include <stdlib.h> #include <assert.h> +#include <SDL.h> #include "main.h" #include "list.h" #include "myTimer.h" #include "image.h" +#include "serverSendMsg.h" #include "net_multiplayer.h" #include "screen_world.h" @@ -13,7 +15,7 @@ #include "interface.h" #include "chat.h" #include "font.h" -#include "serverSendMsg.h" +#include "keyboardBuffer.h" static image_t *g_chat; @@ -25,7 +27,7 @@ static int line_time, line_atime; static int timeBlick; static bool_t chat_active; -static bool_t revicedNewMsg; +static bool_t receivedNewMsg; static my_time_t lastActive; @@ -36,7 +38,7 @@ void initChat() listText = newList(); strcpy(line, ""); chat_active = FALSE; - revicedNewMsg = FALSE; + receivedNewMsg = FALSE; line_time = 0; line_atime = 0; timeBlick = 0; @@ -62,7 +64,7 @@ void drawChat() char *s; s = (char *)listText->list[i]; - + drawFontMaxSize(s, CHAT_LOCATION_X+5, CHAT_LOCATION_Y+5 + i*20, CHAT_SIZE_X-10, 20, COLOR_WHITE); @@ -85,109 +87,46 @@ void drawChat() drawFont(str, CHAT_LOCATION_X+5, CHAT_LOCATION_Y+5 + CHAT_MAX_LINES*20, COLOR_WHITE); } -static void readKey() +static void processMessageKey(SDL_keysym keysym) { - Uint8 *mapa; int w, h; int len; - int i; - - const int len_shift_map = 20; - - char shift_map[] = - { - '-', '_', - '=', '+', - '[', '{', - ']', '}', - ';', ':', - '/', '\"', - '\\', '|', - ',', '<', - '.', '>', - '/', '?' - }; + //int i; if (line_atime < 100) line_atime ++; - mapa = SDL_GetKeyState(NULL); len = strlen(line); getTextSize(line, &w, &h); - for( i = SDLK_FIRST ; i <= SDLK_F15 ; i++ ) + if( w > CHAT_SIZE_X-40 ) { - if( mapa[i] == SDL_PRESSED && len < STR_SIZE ) - { - char *name = (char *) SDL_GetKeyName(i); - char c = '\0'; - - if( name == NULL )continue; - - if( strlen(name) == 1 )c = name[0]; - if( strlen(name) == 3 )c = name[1]; - if( strcmp(name, "space") == 0 )c = ' '; - - //printf("name %s\n", name); - - if( strcmp(name, "backspace") == 0 && len > 0 ) - { - line_time = 0; - line[len-1]='\0'; - return; - } - - if( c == '\0' || w > CHAT_SIZE_X-40 ) - { - continue; - } - - if( len > 0 ) - { - if( line[len-1] == c ) - { - line_time++; - - if( line_time < CHAT_TIME_MULTIPLE ) - { - if( line_atime < 3 )return; - } - } - else - { - line_time = 0; - } - } - - line_atime = 0; - - line[len] = c; - - if( mapa[SDLK_LSHIFT] == SDL_PRESSED || - mapa[SDLK_RSHIFT] == SDL_PRESSED) - { - int i; - - for( i = 0 ; i < len_shift_map ; i+= 2 ) - { - if( c == shift_map[i] ) - { - line[len] = shift_map[i+1]; - } - } + return; + } - return; - } + /* zpracovani backspace */ + if (keysym.sym == SDLK_BACKSPACE){ + line[len-1]='\0'; + return; + } - if( mapa[SDLK_LSHIFT] == SDL_PRESSED || - mapa[SDLK_RSHIFT] == SDL_PRESSED) - { - line[len] -= 32; - } + /* zpracovani tisknutelnych paznaku */ + if ( (keysym.sym >= SDLK_SPACE && keysym.sym <= SDLK_AT) + || (keysym.sym >= SDLK_LEFTBRACKET && keysym.sym <= SDLK_BACKQUOTE) ){ + line[len] = keysym.sym; /* tady by bylo daleko lepsi dat strcat, + predpoklada se, ze line je vyplnen 0 az dokonce */ + } - return; - } + /* zpracovani pismenek */ + if ( keysym.sym >= SDLK_a && keysym.sym <= SDLK_z){ + char c = keysym.sym; + if (keysym.mod == KMOD_SHIFT) + c = toupper(c); + line[len] = c; /* tady by bylo daleko lepsi dat strcat, + predpoklada se, ze line je vyplnen 0 az dokonce */ } + + return; } static void switchChatActive() @@ -202,52 +141,87 @@ static void switchChatActive() } } -void eventChat() +static void sendNewMessage() { - Uint8 *mapa; - my_time_t currentTime; + if( getNetTypeGame() == NET_GAME_TYPE_CLIENT ) + { + proto_send_chat_client(line); + } - mapa = SDL_GetKeyState(NULL); - currentTime = getMyTime(); - - if( mapa[SDLK_RETURN] == SDL_PRESSED && - strcmp(line, "") == 0 && - currentTime - lastActive > CHAT_LAST_ENTER_INTERVAL ) + if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) { - lastActive = getMyTime(); - revicedNewMsg = FALSE; + char out[STR_PROTO_SIZE]; + + snprintf(out, STR_PROTO_SIZE, "chat %s:%s\n", + getControlTux(TUX_CONTROL_KEYBOARD_RIGHT)->name, line); + + proto_send_chat_server(PROTO_SEND_ALL, NULL, out); + } +} + +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); - return; + /* zapneme zachytavani klaves do bufferu a vycistime buffer */ + enableKeyboardBuffer(); + clearKeyboardBuffer(); } +} - if( chat_active == FALSE ) - { - return; - } +static void eventChatEnable() +{ + /* Mame zobrazene okno chatu. Je potreba zpracovat vsechny klavesy v bufferu + * s tim, ze pri klavese RETURN provedeme odeslani radku chatu + * na server + */ - if( mapa[SDLK_RETURN] == SDL_PRESSED && strcmp(line, "") != 0 ) + while( isAnyKeyInKeyboardBuffer() == TRUE ) { - if( getNetTypeGame() == NET_GAME_TYPE_CLIENT ) - { - proto_send_chat_client(line); - } + SDL_keysym key; + key = popKeyFromKeyboardBuffer(); - if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) + if ( key.sym == SDLK_RETURN ) { - char out[STR_PROTO_SIZE]; - - snprintf(out, STR_PROTO_SIZE, "chat %s:%s\n", - getControlTux(TUX_CONTROL_KEYBOARD_RIGHT)->name, line); - - proto_send_chat_server(PROTO_SEND_ALL, NULL, out); + if ( strcmp(line, "") != 0 ) + { /* mame napsany radek: je potreba jej odeslat */ + sendNewMessage(); + memset(line, '\0', STR_SIZE); + continue; /* pokracovat s dalsimi klavesami */ + } + else + { /* radek je prazdny, je potreba vypnout okno chatu */ + switchChatActive(); + memset(line, '\0', STR_SIZE); + /* vypneme zachytavani klaves do bufferu a vycistime buffer */ + disableKeyboardBuffer(); + clearKeyboardBuffer(); + } } - memset(line, '\0', STR_SIZE); - return; + processMessageKey(key); } +} - readKey(); +/** + * Zpracovani "udalosti" chatu? + */ +void eventChat() +{ + if (isChatActive() == FALSE) + { /* okno chatu neni aktivni */ + eventChatDisable(); + } + else + { + eventChatEnable(); + } } bool_t isChatActive() @@ -257,13 +231,13 @@ bool_t isChatActive() bool_t isRecivedNewMsg() { - return revicedNewMsg; + return receivedNewMsg; } void addToChat(char *s) { addList(listText, strdup(s) ); - + if( listText->count > CHAT_MAX_LINES ) { delListItem(listText, 0, free); @@ -271,7 +245,7 @@ void addToChat(char *s) if( chat_active == FALSE ) { - revicedNewMsg = TRUE; + receivedNewMsg = TRUE; } } diff --git a/src/client/chat.h b/src/client/chat.h index f2e1770..fc6abc3 100644 --- a/src/client/chat.h +++ b/src/client/chat.h @@ -14,8 +14,6 @@ #define CHAT_LOCATION_X (WINDOW_SIZE_X/2 - CHAT_SIZE_X/2) #define CHAT_LOCATION_Y (WINDOW_SIZE_Y/2 - CHAT_SIZE_Y/2) -#define CHAT_LAST_ENTER_INTERVAL 50 - extern void initChat(); extern void drawChat(); extern void eventChat(); diff --git a/src/client/interface.c b/src/client/interface.c index 2c45110..37aa361 100755 --- a/src/client/interface.c +++ b/src/client/interface.c @@ -6,6 +6,7 @@ #include "interface.h" #include "screen.h" +#include "keyboardBuffer.h" // window surface static SDL_Surface *screen; @@ -19,6 +20,9 @@ static Uint32 g_win_flags = SDL_HWSURFACE|SDL_DOUBLEBUF; static bool_t isInterfaceInit = FALSE; +// flag, kterym se ridi zarazovani klaves do bufferu +bool_t keyboardBufferEnabled = FALSE; + bool_t isInterfaceInicialized() { return isInterfaceInit; @@ -37,6 +41,14 @@ static Uint32 TimerCallback(Uint32 interval, void *param) return interval; } +void enableKeyboardBuffer(){ + keyboardBufferEnabled=TRUE; +} + +void disableKeyboardBuffer(){ + keyboardBufferEnabled=FALSE; +} + int initSDL() { printf("init SDL..\n"); @@ -70,9 +82,11 @@ int initSDL() SDL_WM_SetCaption(WINDOW_TITLE, NULL); //SDL_ShowCursor(0); - SDL_EnableKeyRepeat(1,1); + SDL_EnableKeyRepeat(0,1); timer = SDL_AddTimer(INTERVAL, TimerCallback, NULL); + initKeyboardBuffer(KEYBOARD_BUFFER_SIZE); + srand((unsigned)time(NULL)); isInterfaceInit = TRUE; @@ -199,6 +213,9 @@ int eventAction() break; default: + //neni potreba vyuzivat frontu stale + if (keyboardBufferEnabled==TRUE) + pushKeyToKeyboardBuffer(event.key.keysym); break; } break; @@ -252,6 +269,9 @@ void eventSDL() void quitSDL() { printf("quit SDL..\n"); + quitKeyboardBuffer(); + SDL_Quit(); + isInterfaceInit = FALSE; } diff --git a/src/client/interface.h b/src/client/interface.h index cabc2a7..29d51af 100755 --- a/src/client/interface.h +++ b/src/client/interface.h @@ -26,7 +26,12 @@ // interval (in ms) of triggering akcia(); #define INTERVAL 50 +// velikost klavesoveho bufferu (ve znacich) +#define KEYBOARD_BUFFER_SIZE 256 + extern bool_t isInterfaceInicialized(); +extern void enableKeyboardBuffer(); +extern void disableKeyboardBuffer(); extern int initSDL(); extern SDL_Surface* getSDL_Screen(); extern void getMousePosition(int *x, int *y); diff --git a/src/client/keyboardBuffer.c b/src/client/keyboardBuffer.c new file mode 100644 index 0000000..29cd9d4 --- /dev/null +++ b/src/client/keyboardBuffer.c @@ -0,0 +1,138 @@ +/* + * keyboardBuffer.c + * + * Created on: 1.8.2008 + * Author: Karel Podvolecky + * + * Jednoducha fronta stisknutych klaves. + * + */ + +#include <stdio.h> +#include <assert.h> +#include <SDL.h> + +#include "keyboardBuffer.h" + +static keyboardBuffer_t *keyboardBuffer=NULL; + +static SDL_keysym emptyKey; + +/* + * Inicializuje klavesovy buffer. Parametr udava pocet klaves. + */ +void initKeyboardBuffer(uint32_t size){ + assert(size>0); + assert(keyboardBuffer==NULL); + + keyboardBuffer = malloc(sizeof(keyboardBuffer_t)); + assert(keyboardBuffer!=NULL); + memset(keyboardBuffer, 0, sizeof(keyboardBuffer_t)); + + keyboardBuffer->buff = malloc(size * sizeof(SDL_keysym)); + assert(keyboardBuffer->buff!=NULL); + memset(keyboardBuffer->buff, 0, size * sizeof(SDL_keysym)); + + keyboardBuffer->begin=0; + keyboardBuffer->end=0; + keyboardBuffer->count=0; + keyboardBuffer->size=size; + + emptyKey.sym = SDLK_UNKNOWN; +} + +/* + * Vyprazdni buffer. + */ +void clearKeyboardBuffer(){ + assert(keyboardBuffer!=NULL); + + keyboardBuffer->begin=0; + keyboardBuffer->end=0; + keyboardBuffer->count=0; + memset(keyboardBuffer->buff, 0, keyboardBuffer->size * sizeof(SDL_keysym)); +} + +/* + * Prida klavesu na konec bufferu. Pokud je buffer preplnen, + * vypise chybu na chybovy vystup a klavesu zahodi. + */ +bool_t pushKeyToKeyboardBuffer(SDL_keysym key){ + assert(keyboardBuffer!=NULL); + + if (keyboardBuffer->count >= keyboardBuffer->size){ + fprintf(stderr, "Preteceni klavesoveho bufferu! Zahazuji klavesu: %02x\n", key.sym); + return FALSE; + } + + keyboardBuffer->buff[keyboardBuffer->end] = key; + + keyboardBuffer->end++; + if (keyboardBuffer->end>=keyboardBuffer->size) + keyboardBuffer->end=0; + + keyboardBuffer->count++; + + return TRUE; +} + +/* + * Vyjme prvni klavesu z bufferu a vrati ji. Pokud je buffer prazdny, + * vypise chybu na chybovy vystup a vrati SDLK_UNKNOWN + */ +SDL_keysym popKeyFromKeyboardBuffer(){ + assert(keyboardBuffer!=NULL); + + if (keyboardBuffer->count <= 0){ + fprintf(stderr, "Podteceni klavesoveho bufferu!\n"); + return emptyKey; + } + + SDL_keysym key = keyboardBuffer->buff[keyboardBuffer->begin]; + keyboardBuffer->buff[keyboardBuffer->begin]=emptyKey; + + keyboardBuffer->begin++; + if (keyboardBuffer->begin>=keyboardBuffer->size) + keyboardBuffer->begin=0; + + keyboardBuffer->count--; + + return key; +} + +/* + * Velikost bufferu. + */ +uint32_t getKeyboardBufferSize(){ + assert(keyboardBuffer!=NULL); + return keyboardBuffer->size; +} + +/* + * Pocet klaves v bufferu. + */ +uint32_t KeyboardBufferCount(){ + assert(keyboardBuffer!=NULL); + return keyboardBuffer->count; +} + + +bool_t isAnyKeyInKeyboardBuffer(){ + return KeyboardBufferCount()>0 ? TRUE : FALSE; +} + +/* + * Uvolni buffer. Pokud neni prazdny, vypise predtim + * info o zaplneni na chybovy vystup. + */ +void quitKeyboardBuffer(){ + assert(keyboardBuffer!=NULL); + + if (keyboardBuffer->count>0){ + fprintf(stderr, "Klavesovy buffer neni prazdny!\n"); + } + + free(keyboardBuffer->buff); + free(keyboardBuffer); + keyboardBuffer=NULL; +} diff --git a/src/client/keyboardBuffer.h b/src/client/keyboardBuffer.h new file mode 100644 index 0000000..13b1183 --- /dev/null +++ b/src/client/keyboardBuffer.h @@ -0,0 +1,34 @@ +/* + * keyboardBuffer.h + * + * Created on: 1.8.2008 + * Author: Karel Podvolecky + * + * Jednoducha fronta stisknutych klaves. + */ + +#ifndef KEYBOARD_BUFFER_H + +#define KEYBOARD_BUFFER_H + +#include <SDL.h> +#include "main.h" + +typedef struct { + SDL_keysym *buff; /* buffer klaves (kod dle SDLKey enumu) */ + uint32_t size; /* aktualni velikost alokovaneho bufferu (v poctu uchovatelnych klaves) */ + uint32_t count; /* pocet klaves aktualne v bufferu */ + uint32_t begin; /* ukazatel na zacatek bufferu (tj. kam se pridavaji klavesy) */ + uint32_t end; /* ukazatel na konec bufferu (tj. odkud se odebiraji klavesy) */ +} keyboardBuffer_t; + +extern void initKeyboardBuffer(uint32_t size); +extern void clearKeyboardBuffer(); +extern bool_t pushKeyToKeyboardBuffer(SDL_keysym key); +extern SDL_keysym popKeyFromKeyboardBuffer(); +extern uint32_t getKeyboardBufferSize(); +extern uint32_t KeyboardBufferCount(); +extern bool_t isAnyKeyInKeyboardBuffer(); +extern void quitKeyboardBuffer(); + +#endif /* KEYBOARDBUFFER_H_ */ |