From b4cb68756603ec3f7edfcf94107fd05a1fc5a8f6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?C=C3=B4me=20BERNIGAUD?= Date: Fri, 4 Jun 2010 19:27:05 +0200 Subject: [PATCH] No more thread for text field, SDLPollEvent is used instead. There is still a thread for key configuration. (I do not plan to remove it) Chatting and Console are fixed (in debug mode) Useless functions removed from WinInput.h --- Source/Game.cpp | 153 ++++++++++------------- Source/Game.h | 7 +- Source/GameTick.cpp | 184 ++++++++-------------------- Source/WinInput.cpp | 290 -------------------------------------------- Source/WinInput.h | 2 - 5 files changed, 117 insertions(+), 519 deletions(-) diff --git a/Source/Game.cpp b/Source/Game.cpp index 776a0e7..524009d 100644 --- a/Source/Game.cpp +++ b/Source/Game.cpp @@ -181,8 +181,8 @@ Game::Game() consoleblinkdelay = 0; consoleblink = 0; consoleselected = 0; - memset(togglekey, 0, sizeof(togglekey)); - memset(togglekeydelay, 0, sizeof(togglekeydelay)); +// memset(togglekey, 0, sizeof(togglekey)); +// memset(togglekeydelay, 0, sizeof(togglekeydelay)); registernow = 0; autocam = 0; @@ -229,88 +229,65 @@ void Game::fireSound(int sound) { OPENAL_Sample_SetMinMaxDistance(samp[sound], 8.0f, 2000.0f); } -void Game::inputText() { - waiting=true; - params_thread* data = new params_thread; - data->game = this; - data->method = &Game::inputText_thread; - SDL_Thread* thread = SDL_CreateThread(Game::thread, data); - if ( thread == NULL ) { - fprintf(stderr, "Unable to create thread: %s\n", SDL_GetError()); - waiting=false; - return; - } -} - -void Game::inputText_thread() { +void Game::inputText(char* str, int* charselected, int* nb_chars) { SDL_Event evenement; - SDL_EnableKeyRepeat(400, 40); - SDL_EnableUNICODE(true); - - while(waiting) { - int i; - SDL_WaitEvent(&evenement); - - switch(evenement.type) { - case SDL_KEYDOWN: - if(evenement.key.keysym.sym == SDLK_ESCAPE) { - for(i=0;i<255;i++){ - displaytext[0][i]=' '; - } - displaychars[0]=0; - displayselected=0; - mainmenutogglekeydown=1; - waiting=false; - } else if(evenement.key.keysym.sym==SDLK_BACKSPACE&&displayselected!=0){ - for(i=displayselected-1;i<255;i++){ - displaytext[0][i]=displaytext[0][i+1]; - } - displaytext[0][255]=' '; - displayselected--; - displaychars[0]--; - } else if(evenement.key.keysym.sym==SDLK_DELETE&&displayselected!=0){ - for(i=displayselected;i<255;i++){ - displaytext[0][i]=displaytext[0][i+1]; - } - displaytext[0][255]=' '; - displaychars[0]--; - } else if(evenement.key.keysym.sym==SDLK_LEFT&&displayselected!=0){ - displayselected--; - } else if(evenement.key.keysym.sym==SDLK_RIGHT&&displayselected=displayselected+1;i--){ - displaytext[0][i]=displaytext[0][i-1]; + SDL_PollEvent(&evenement); + + switch(evenement.type) { + case SDL_KEYDOWN: + if(evenement.key.keysym.sym == SDLK_ESCAPE) { + for(i=0;i<255;i++){ + str[i]=' '; + } + *nb_chars=0; + *charselected=0; + waiting=false; + } else if(evenement.key.keysym.sym==SDLK_BACKSPACE){ + if((*charselected)!=0) { + for(i=(*charselected)-1;i<255;i++){ + str[i]=str[i+1]; } - displaytext[0][displayselected]=evenement.key.keysym.unicode; - displayselected++; - displaychars[0]++; - printf("%c\n",evenement.key.keysym.unicode); + str[255]=' '; + (*charselected)--; + (*nb_chars)--; } - break; - } + } else if(evenement.key.keysym.sym==SDLK_DELETE){ + for(i=(*charselected);i<255;i++){ + str[i]=str[i+1]; + } + str[255]=' '; + (*nb_chars)--; + } else if(evenement.key.keysym.sym==SDLK_LEFT){ + if((*charselected)!=0) + (*charselected)--; + } else if(evenement.key.keysym.sym==SDLK_RIGHT){ + if((*charselected)<(*nb_chars)) + (*charselected)++; + } else if(evenement.key.keysym.sym==SDLK_RETURN) { + waiting=false; + } else if((evenement.key.keysym.unicode<127)&&((*nb_chars)<60)&&(evenement.key.keysym.sym!=SDLK_LSHIFT)&&(evenement.key.keysym.sym!=SDLK_RSHIFT)) { + for(i=255;i>=(*charselected)+1;i--){ + str[i]=str[i-1]; + } + str[*charselected]=evenement.key.keysym.unicode; + (*charselected)++; + (*nb_chars)++; + } + break; + } + + if(!waiting) { + SDL_EnableKeyRepeat(0,0); // disable key repeat + SDL_EnableUNICODE(false); } - entername=0; - SDL_EnableKeyRepeat(0,0); // disable key repeat - SDL_EnableUNICODE(false); } void Game::setKeySelected() { @@ -346,23 +323,23 @@ void Game::setKeySelected_thread() { if(keycode != SDLK_ESCAPE) { fireSound(); switch(keyselect) { - case 0:forwardkey=keycode; + case 0: forwardkey=keycode; break; - case 1:backkey=keycode; + case 1: backkey=keycode; break; - case 2:leftkey=keycode; + case 2: leftkey=keycode; break; - case 3:rightkey=keycode; + case 3: rightkey=keycode; break; - case 4:crouchkey=keycode; + case 4: crouchkey=keycode; break; - case 5:jumpkey=keycode; + case 5: jumpkey=keycode; break; - case 6:drawkey=keycode; + case 6: drawkey=keycode; break; - case 7:throwkey=keycode; + case 7: throwkey=keycode; break; - case 8:attackkey=keycode; + case 8: attackkey=keycode; break; default: break; diff --git a/Source/Game.h b/Source/Game.h index 8846493..fab60c8 100644 --- a/Source/Game.h +++ b/Source/Game.h @@ -229,8 +229,8 @@ class Game float consoleblinkdelay; bool consoleblink; int consoleselected; - int togglekey[140]; - float togglekeydelay[140]; + //int togglekey[140]; + //float togglekeydelay[140]; bool registernow; bool autocam; @@ -301,8 +301,7 @@ class Game private: void setKeySelected_thread(); static int thread(void *data); - void inputText(); - void inputText_thread(); + void inputText(char* str, int* charselected, int* nb_chars); void flash(); bool waiting; bool mainmenutogglekeydown; diff --git a/Source/GameTick.cpp b/Source/GameTick.cpp index dca83bc..e1ea043 100644 --- a/Source/GameTick.cpp +++ b/Source/GameTick.cpp @@ -3036,7 +3036,28 @@ void Game::Tick() OPENAL_SetFrequency(channels[stream_music3], 22050); if(entername) { - if(!waiting) inputText(); + inputText(displaytext[0],&displayselected,&displaychars[0]); + if(!waiting) { // the input as finished + if(displaychars[0]){ // with enter + accountactive = Account::add(string(displaytext[0])); + + mainmenu=8; + + flash(); + + fireSound(firestartsound); + + for(i=0;i<255;i++){ + displaytext[0][i]=' '; + } + displaychars[0]=0; + + displayselected=0; + } else { // with escape or empty + mainmenutogglekeydown=1; + } + entername=0; + } displayblinkdelay-=multiplier; if(displayblinkdelay<=0){ @@ -3060,8 +3081,6 @@ void Game::Tick() if(Input::isKeyDown(chatkey)&&!chattogglekeydown&&!console&&!chatting&&debugmode){ chatting=1; chattogglekeydown=1; - togglekey[chatkey]=1; - togglekeydelay[chatkey]=-20; } if(!Input::isKeyDown(chatkey)){ @@ -3069,61 +3088,16 @@ void Game::Tick() } if(chatting){ - for(i=0;i<140;i++){ - if(Input::isKeyDown(i)){ - togglekeydelay[i]+=multiplier; - if(togglekeydelay[i]>.4){ - togglekey[i]=0; - togglekeydelay[i]=.36; + inputText(displaytext[0],&displayselected,&displaychars[0]); + if(!waiting) { + if(displaychars[0]){ + for(int j=0;j<255;j++){ + displaytext[0][j]=' '; } - if(!togglekey[i]){ - if(KeyToSingleChar(i)!='\0'&&displaychars[0]<60){ - for(j=255;j>=displayselected+1;j--){ - displaytext[0][j]=displaytext[0][j-1]; - } - displaytext[0][displayselected]=KeyToSingleChar(i); - if(Input::isKeyDown(SDLK_LSHIFT))displaytext[0][displayselected]=Shift(displaytext[0][displayselected]); - displayselected++; - displaychars[0]++; - } - if(i==SDLK_DELETE&&displayselected!=0){ - for(j=displayselected-1;j<255;j++){ - displaytext[0][j]=displaytext[0][j+1]; - } - displaytext[0][255]=' '; - displayselected--; - displaychars[0]--; - } - if(i==SDLK_LEFT&&displayselected!=0){ - displayselected--; - } - if(i==SDLK_RIGHT&&displayselected.4){ - togglekey[i]=0; - togglekeydelay[i]=.36; - } - if(!togglekey[i]){ - if(KeyToSingleChar(i)!='\0'&&consolechars[0]<255){ - for(j=255;j>=consoleselected+1;j--){ - consoletext[0][j]=consoletext[0][j-1]; - } - consoletext[0][consoleselected]=KeyToSingleChar(i); - if(Input::isKeyDown(SDLK_LSHIFT))consoletext[0][consoleselected]=Shift(consoletext[0][consoleselected]); - consoleselected++; - consolechars[0]++; - } - else if(i==SDLK_RETURN){ - for(j=255;j>=consoleselected+1;j--){ - consoletext[0][j]=consoletext[0][j-1]; - } - consoletext[0][consoleselected]='\n'; - consoleselected++; - consolechars[0]++; - } - if(i==SDLK_DELETE&&consoleselected!=0){ - for(j=consoleselected-1;j<255;j++){ - consoletext[0][j]=consoletext[0][j+1]; - } - consoletext[0][255]=' '; - consoleselected--; - consolechars[0]--; - } - if(i==SDLK_UP){ - if(archiveselected<14)archiveselected++; - for(j=0;j<255;j++){ - consolechars[0]=consolechars[archiveselected]; - consoletext[0][j]=consoletext[archiveselected][j]; - consoleselected=consolechars[0]; - } - } - if(i==SDLK_DOWN){ - if(archiveselected>0)archiveselected--; - for(j=0;j<255;j++){ - consolechars[0]=consolechars[archiveselected]; - consoletext[0][j]=consoletext[archiveselected][j]; - consoleselected=consolechars[0]; - } - } - if(i==SDLK_LEFT&&consoleselected!=0){ - consoleselected--; - } - if(i==SDLK_RIGHT&&consoleselected0){ - for(k=14;k>=1;k--){ - for(j=0;j<255;j++){ - consoletext[k][j]=consoletext[k-1][j]; - } - consolechars[k]=consolechars[k-1]; - } - for(j=0;j<255;j++){ - consoletext[0][j]=' '; - } - consolechars[0]=0; - consoleselected=0; - } + inputText(consoletext[0],&consoleselected,&consolechars[0]); + if(!waiting) { + archiveselected=0; + cmd_dispatch(this, consoletext[0]); + if(consolechars[0]>0){ + + for(k=14;k>=1;k--){ + for(j=0;j<255;j++){ + consoletext[k][j]=consoletext[k-1][j]; } + consolechars[k]=consolechars[k-1]; } - togglekey[i]=1; - } - else { - togglekey[i]=0; - togglekeydelay[i]=0; + for(j=0;j<255;j++){ + consoletext[0][j]=' '; + } + consolechars[0]=0; + consoleselected=0; } } diff --git a/Source/WinInput.cpp b/Source/WinInput.cpp index c5a59c3..8726854 100644 --- a/Source/WinInput.cpp +++ b/Source/WinInput.cpp @@ -48,293 +48,3 @@ unsigned short CharToKey(const char* which) } return SDLK_LAST; } - -char KeyToSingleChar(unsigned short which) -{ - static int i; - - if(which==SDLK_a){ - return 'a'; - } - if(which==SDLK_b){ - return 'b'; - } - if(which==SDLK_c){ - return 'c'; - } - if(which==SDLK_d){ - return 'd'; - } - if(which==SDLK_e){ - return 'e'; - } - if(which==SDLK_f){ - return 'f'; - } - if(which==SDLK_g){ - return 'g'; - } - if(which==SDLK_h){ - return 'h'; - } - if(which==SDLK_i){ - return 'i'; - } - if(which==SDLK_j){ - return 'j'; - } - if(which==SDLK_k){ - return 'k'; - } - if(which==SDLK_m){ - return 'm'; - } - if(which==SDLK_n){ - return 'n'; - } - if(which==SDLK_o){ - return 'o'; - } - if(which==SDLK_p){ - return 'p'; - } - if(which==SDLK_q){ - return 'q'; - } - if(which==SDLK_s){ - return 's'; - } - if(which==SDLK_u){ - return 'u'; - } - if(which==SDLK_v){ - return 'v'; - } - if(which==SDLK_x){ - return 'x'; - } - if(which==SDLK_z){ - return 'z'; - } - if(which==SDLK_KP1){ - return '1'; - } - if(which==SDLK_KP2){ - return '2'; - } - if(which==SDLK_KP3){ - return '3'; - } - if(which==SDLK_KP4){ - return '4'; - } - if(which==SDLK_KP5){ - return '5'; - } - if(which==SDLK_KP6){ - return '6'; - } - if(which==SDLK_KP7){ - return '7'; - } - if(which==SDLK_KP8){ - return '8'; - } - if(which==SDLK_KP9){ - return '9'; - } - if(which==SDLK_KP0){ - return '0'; - } - if(which==SDLK_1){ - return '1'; - } - if(which==SDLK_2){ - return '2'; - } - if(which==SDLK_3){ - return '3'; - } - if(which==SDLK_4){ - return '4'; - } - if(which==SDLK_5){ - return '5'; - } - if(which==SDLK_6){ - return '6'; - } - if(which==SDLK_7){ - return '7'; - } - if(which==SDLK_8){ - return '8'; - } - if(which==SDLK_9){ - return '9'; - } - if(which==SDLK_0){ - return '0'; - } - if(which==SDLK_SPACE){ - return ' '; - } - return '\0'; -} - -char Shift(char which) -{ - static int i; - - if(which=='a'){ - return 'A'; - } - if(which=='b'){ - return 'B'; - } - if(which=='c'){ - return 'C'; - } - if(which=='d'){ - return 'D'; - } - if(which=='e'){ - return 'E'; - } - if(which=='f'){ - return 'F'; - } - if(which=='g'){ - return 'G'; - } - if(which=='h'){ - return 'H'; - } - if(which=='e'){ - return 'E'; - } - if(which=='f'){ - return 'F'; - } - if(which=='g'){ - return 'G'; - } - if(which=='h'){ - return 'H'; - } - if(which=='i'){ - return 'I'; - } - if(which=='j'){ - return 'J'; - } - if(which=='k'){ - return 'K'; - } - if(which=='l'){ - return 'L'; - } - if(which=='m'){ - return 'M'; - } - if(which=='n'){ - return 'N'; - } - if(which=='o'){ - return 'O'; - } - if(which=='p'){ - return 'P'; - } - if(which=='q'){ - return 'Q'; - } - if(which=='r'){ - return 'R'; - } - if(which=='s'){ - return 'S'; - } - if(which=='t'){ - return 'T'; - } - if(which=='u'){ - return 'U'; - } - if(which=='v'){ - return 'V'; - } - if(which=='w'){ - return 'W'; - } - if(which=='x'){ - return 'X'; - } - if(which=='y'){ - return 'Y'; - } - if(which=='z'){ - return 'Z'; - } - if(which=='1'){ - return '!'; - } - if(which=='2'){ - return '@'; - } - if(which=='3'){ - return '#'; - } - if(which=='4'){ - return '$'; - } - if(which=='5'){ - return '%'; - } - if(which=='6'){ - return '^'; - } - if(which=='7'){ - return '&'; - } - if(which=='8'){ - return '*'; - } - if(which=='9'){ - return '('; - } - if(which=='0'){ - return ')'; - } - if(which=='-'){ - return '_'; - } - if(which=='='){ - return '+'; - } - if(which=='['){ - return '{'; - } - if(which==']'){ - return '}'; - } - if(which=='\\'){ - return '|'; - } - if(which=='.'){ - return '>'; - } - if(which==','){ - return '<'; - } - if(which=='/'){ - return '?'; - } - if(which==';'){ - return ':'; - } - if(which=='\''){ - return '\"'; - } - return which; -} diff --git a/Source/WinInput.h b/Source/WinInput.h index 561d593..1145eb3 100644 --- a/Source/WinInput.h +++ b/Source/WinInput.h @@ -38,8 +38,6 @@ void MoveMouse(int xcoord, int ycoord, Point *mouseloc); void RefreshMouse(Point *mouseloc); void DisposeMouse(); unsigned short CharToKey(const char* which); -char KeyToSingleChar(unsigned short which); -char Shift(char which); Boolean Button(); -- 2.39.5