Changeset e607c0f in network-game


Ignore:
Timestamp:
Dec 9, 2012, 8:56:45 PM (12 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
cdb4bec
Parents:
581058c
Message:

The client uses nonblocking calls to check for server messages and textboxes can now process all the standard keyboard keys

Files:
6 edited

Legend:

Unmodified
Added
Removed
  • client/Client/Client.vcxproj

    r581058c re607c0f  
    7373  </ItemGroup>
    7474  <ItemGroup>
     75    <ClInclude Include="..\..\common\Common.h" />
    7576    <ClInclude Include="..\..\common\Compiler.h" />
    7677    <ClInclude Include="..\..\common\Message.h" />
  • client/Client/Client.vcxproj.filters

    r581058c re607c0f  
    7272      <Filter>Header Files\common</Filter>
    7373    </ClInclude>
     74    <ClInclude Include="..\..\common\Common.h">
     75      <Filter>Header Files\common</Filter>
     76    </ClInclude>
    7477  </ItemGroup>
    7578</Project>
  • client/Client/Textbox.cpp

    r581058c re607c0f  
    1010   str = "";
    1111   selected = false;
     12   shiftPressed = false;
     13   
     14   // populate the shift map
     15   for(int i=0; i<26; i++)
     16      shiftMap['a'+i] = 'A'+i;
     17
     18   shiftMap['1'] = '!';
     19   shiftMap['2'] = '@';
     20   shiftMap['3'] = '#';
     21   shiftMap['4'] = '$';
     22   shiftMap['5'] = '%';
     23   shiftMap['6'] = '^';
     24   shiftMap['7'] = '&';
     25   shiftMap['8'] = '*';
     26   shiftMap['9'] = '(';
     27   shiftMap['0'] = ')';
     28
     29   shiftMap['`'] = '~';
     30   shiftMap['-'] = '_';
     31   shiftMap['='] = '+';
     32   shiftMap['['] = '{';
     33   shiftMap[']'] = '}';
     34   shiftMap['\\'] = '|';
     35   shiftMap[';'] = ':';
     36   shiftMap['\''] = '\"';
     37   shiftMap[','] = '<';
     38   shiftMap['.'] = '>';
     39   shiftMap['/'] = '?';
     40   shiftMap[' '] = ' ';
    1241}
    1342
     
    7099      char newChar = 0;
    71100
    72       if (ALLEGRO_KEY_A <= e.keyboard.keycode && e.keyboard.keycode <= ALLEGRO_KEY_Z) {
     101      if (ALLEGRO_KEY_A <= e.keyboard.keycode && e.keyboard.keycode <= ALLEGRO_KEY_Z)
    73102         newChar = 'a'+e.keyboard.keycode-ALLEGRO_KEY_A;
    74          if (al_key_down(&keys, ALLEGRO_KEY_LSHIFT) || al_key_down(&keys, ALLEGRO_KEY_RSHIFT))
    75             newChar -= 32;
    76       }
    77103      else if (ALLEGRO_KEY_0 <= e.keyboard.keycode && e.keyboard.keycode <= ALLEGRO_KEY_9)
    78104         newChar = '0'+e.keyboard.keycode-ALLEGRO_KEY_0;
    79       else if (e.keyboard.keycode = ALLEGRO_KEY_BACKSPACE && str.size() > 0)
    80          str = str.substr(0, str.size()-1);
     105      else {
     106         switch(e.keyboard.keycode)
     107         {
     108         case ALLEGRO_KEY_TILDE:
     109            newChar = '`';
     110            break;
     111         case ALLEGRO_KEY_MINUS:
     112            newChar = '-';
     113            break;
     114         case ALLEGRO_KEY_EQUALS:
     115            newChar = '=';
     116            break;
     117         case ALLEGRO_KEY_OPENBRACE:
     118            newChar = '[';
     119            break;
     120         case ALLEGRO_KEY_CLOSEBRACE:
     121            newChar = ']';
     122            break;
     123         case ALLEGRO_KEY_SEMICOLON:
     124            newChar = ';';
     125            break;
     126         case ALLEGRO_KEY_QUOTE:
     127            newChar = '\'';
     128            break;
     129         case ALLEGRO_KEY_BACKSLASH:
     130            newChar = '\\';
     131            break;
     132         case ALLEGRO_KEY_COMMA:
     133            newChar = ',';
     134            break;
     135         case ALLEGRO_KEY_FULLSTOP:
     136            newChar = '.';
     137            break;
     138         case ALLEGRO_KEY_SLASH:
     139            newChar = '/';
     140            break;
     141         case ALLEGRO_KEY_SPACE:
     142            newChar = ' ';
     143            break;
     144         case  ALLEGRO_KEY_BACKSPACE:
     145            if (str.size() > 0)
     146            {
     147               str = str.substr(0, str.size()-1);
     148               return true;
     149            }
     150            else
     151               return false;
     152            break;
     153         case  ALLEGRO_KEY_LSHIFT:
     154         case  ALLEGRO_KEY_RSHIFT:
     155            shiftPressed = true;
     156            break;
     157         default:
     158            cout << "unknown keycode: " << e.keyboard.keycode << endl;
     159            break;
     160         }
     161      }
    81162
    82163      if (newChar != 0) {
     164         if (al_key_down(&keys, ALLEGRO_KEY_LSHIFT) || al_key_down(&keys, ALLEGRO_KEY_RSHIFT))
     165            newChar = shiftMap[newChar];
     166
    83167         str.append(1, newChar);
    84168         return true;
     169      }
     170   }
     171   else if (e.type == ALLEGRO_EVENT_KEY_UP) {
     172      switch(e.keyboard.keycode)
     173      {
     174      case ALLEGRO_KEY_LSHIFT:
     175      case ALLEGRO_KEY_RSHIFT:
     176         shiftPressed = false;
     177         break;
    85178      }
    86179   }
  • client/Client/Textbox.h

    r581058c re607c0f  
    55
    66#include <string>
     7#include <map>
    78
    89using namespace std;
     
    1415   string str;
    1516   bool selected;
     17   bool shiftPressed;
     18   map<char, char> shiftMap;
    1619
    1720public:
  • client/Client/main.cpp

    r581058c re607c0f  
    2323#include "../../common/Compiler.h"
    2424#include "../../common/Message.h"
     25#include "../../common/Common.h"
    2526
    2627#include "Window.h"
     
    200201      error("socket");
    201202
     203   set_nonblock(sock);
     204
    202205   server.sin_family = AF_INET;
    203206   hp = gethostbyname(argv[1]);
     
    209212
    210213   al_start_timer(timer);
    211  
     214
    212215   while(!doexit)
    213216   {
     
    282285         }
    283286      }
    284  
    285       if(redraw && al_is_event_queue_empty(event_queue)) {
     287
     288      if (receiveMessage(&msgFrom, sock, &from) >= 0)
     289      {
     290         processMessage(msgFrom, state, chatConsole);
     291         cout << "state: " << state << endl;
     292      }
     293 
     294      if (redraw && al_is_event_queue_empty(event_queue))
     295      {
    286296         redraw = false;
    287297 
     
    364374      case STATE_START:
    365375      {
     376         cout << "In STATE_START" << endl;
     377
    366378         chatConsole.addLine(response);
    367379
     
    448460
    449461   sendMessage(&msgTo, sock, &server);
    450    receiveMessage(&msgFrom, sock, &from);
    451    processMessage(msgFrom, state, chatConsole);
    452    cout << "state: " << state << endl;
    453462}
    454463
     
    468477
    469478   sendMessage(&msgTo, sock, &server);
    470    receiveMessage(&msgFrom, sock, &from);
    471    processMessage(msgFrom, state, chatConsole);
    472    cout << "state: " << state << endl;
    473479}
    474480
     
    482488
    483489   sendMessage(&msgTo, sock, &server);
    484    receiveMessage(&msgFrom, sock, &from);
    485    processMessage(msgFrom, state, chatConsole);
    486490}
    487491
     
    502506
    503507   sendMessage(&msgTo, sock, &server);
    504    receiveMessage(&msgFrom, sock, &from);
    505    processMessage(msgFrom, state, chatConsole);
    506 }
     508}
  • common/Common.h

    r581058c re607c0f  
    44void set_nonblock(int sock)
    55{
    6     int flags;
    7     flags = fcntl(sock, F_GETFL,0);
    8     assert(flags != -1);
    9     fcntl(sock, F_SETFL, flags | O_NONBLOCK);
     6   #ifdef WIN32
     7      unsigned long mode = 1;
     8      ioctlsocket(sock, FIONBIO, &mode);
     9   #else
     10      int flags;
     11      flags = fcntl(sock, F_GETFL,0);
     12      assert(flags != -1);
     13      fcntl(sock, F_SETFL, flags | O_NONBLOCK);
     14   #endif
    1015}
    1116
Note: See TracChangeset for help on using the changeset viewer.