]> git.jsancho.org Git - lugaru.git/commitdiff
No more thread for text field, SDLPollEvent is used instead. There is still a thread...
authorCôme BERNIGAUD <come.bernigaud@gmail.com>
Fri, 4 Jun 2010 17:27:05 +0000 (19:27 +0200)
committerCôme BERNIGAUD <come.bernigaud@gmail.com>
Fri, 4 Jun 2010 17:27:05 +0000 (19:27 +0200)
Chatting and Console are fixed (in debug mode)
Useless functions removed from WinInput.h

Source/Game.cpp
Source/Game.h
Source/GameTick.cpp
Source/WinInput.cpp
Source/WinInput.h

index 776a0e77899ecda9854457c6684d098d2b157d7b..524009d2500b504846f927c4223147d5fba8fe47 100644 (file)
@@ -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<displaychars[0]){
-                                       displayselected++;
-                               } else if(evenement.key.keysym.sym==SDLK_RETURN) {
-                                       if(displaychars[0]){
-                                               accountactive = Account::add(string(displaytext[0]));
-
-                                               mainmenu=8;
-
-                                               flash();
-
-                                               fireSound(firestartsound);
-
-                                               for(i=0;i<255;i++){
-                                                       displaytext[0][i]=' ';
-                                               }
-                                               displaychars[0]=0;
+       int i;
+       
+       if(!waiting) {
+               waiting=true;
+               SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,SDL_DEFAULT_REPEAT_INTERVAL);
+               SDL_EnableUNICODE(true);
+       }
 
-                                               displayselected=0;
-                                       }
-                                       waiting=false;
-                               } else if(evenement.key.keysym.unicode&&displaychars[0]<60) {
-                                       for(i=255;i>=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;
index 8846493e07be874e29a4c9a3a83d09c8cfa8de53..fab60c8a0c7f7850ad744a1ecc234987bb5fa490 100644 (file)
@@ -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;
index dca83bc0ea4997b738e8afe592c339e307a9428b..e1ea0433fa973fb4d3bb962e31458ee5d770a2a8 100644 (file)
@@ -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<displaychars[0]){
-                                                       displayselected++;
-                                               }
-                                               if(i==SDLK_RETURN){
-                                                       if(displaychars[0]){
-                                                               /*for(j=0;j<displaychars[0];j++){
-                                                               talkname[j]=displaytext[0][j];
-                                                               }
-                                                               talkname[displaychars[0]]='\0';
-                                                               sprintf (chatname, "%s: %s",playerName,talkname);
-                                                               //NetworkSendInformation(chatname);
-                                                               */
-                                                               for(j=0;j<255;j++){
-                                                                       displaytext[0][j]=' ';
-                                                               }
-                                                               displaychars[0]=0;
-                                                               displayselected=0;
-                                                               chatting=0;
-                                                       }
-                                               }
-                                       }
-                                       togglekey[i]=1;
-                               }
-                               else {
-                                       togglekey[i]=0;
-                                       togglekeydelay[i]=0;
-                               }
+                                       displaychars[0]=0;
+                                       displayselected=0;
+                               }       
+                               chatting=0;             
                        }
 
                        displayblinkdelay-=multiplier;
@@ -3149,9 +3123,11 @@ void     Game::Tick()
 
                if(Input::isKeyDown(SDLK_BACKQUOTE)&&!consoletogglekeydown&&debugmode){
                        console=1-console;
-                       if(!console)freeze=0;
                        if(console){
                                OPENAL_SetFrequency(OPENAL_ALL, 0.001);
+                       } else {
+                               freeze=0;
+                               waiting=false;
                        }
                        consoletogglekeydown=1;
                }
@@ -3163,85 +3139,23 @@ void    Game::Tick()
                if(console)freeze=1;
 
                if(console&&!Input::isKeyDown(SDLK_LMETA)){
-                       for(i=0;i<140;i++){
-                               if(Input::isKeyDown(i)){
-                                       togglekeydelay[i]+=multiplier;
-                                       if(togglekeydelay[i]>.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&&consoleselected<consolechars[0]){
-                                                       consoleselected++;
-                                               }
-                                               if(i==SDLK_RETURN){
-                                                       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];
-                                                               }
-                                                               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;
                                }
                        }
 
index c5a59c349e653415d097c32f10bbb512c52aa602..87268544ab57ed8f0ee62290934de451c2cefa7d 100644 (file)
@@ -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;
-}
index 561d593709276604f7d4d91a43b0f25b5d0c41c8..1145eb319067a9c6aafe2d880dbf4b83ddedbcd6 100644 (file)
@@ -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();