Changeset 6475138 in network-game for client/Client


Ignore:
Timestamp:
Nov 25, 2012, 1:13:45 AM (12 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
2488852
Parents:
9a3e6b1
Message:

Added a chat class to encapsulate the server messages and text input

Location:
client/Client
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • client/Client/Client.vcxproj

    r9a3e6b1 r6475138  
    6565  <ItemGroup>
    6666    <ClCompile Include="..\..\common\message.cpp" />
     67    <ClCompile Include="chat.cpp" />
    6768    <ClCompile Include="main.cpp" />
    6869  </ItemGroup>
     
    7071    <ClInclude Include="..\..\common\compiler.h" />
    7172    <ClInclude Include="..\..\common\message.h" />
     73    <ClInclude Include="chat.h" />
    7274  </ItemGroup>
    7375  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
  • client/Client/Client.vcxproj.filters

    r9a3e6b1 r6475138  
    1717      <UniqueIdentifier>{9ecfb491-9e8e-4a29-bc07-dd887204d590}</UniqueIdentifier>
    1818    </Filter>
     19    <Filter Include="Header Files\common">
     20      <UniqueIdentifier>{6c9ad2f2-bc8d-4e25-9c5c-c4440c60991c}</UniqueIdentifier>
     21    </Filter>
    1922  </ItemGroup>
    2023  <ItemGroup>
     
    2528      <Filter>Source Files\common</Filter>
    2629    </ClCompile>
     30    <ClCompile Include="chat.cpp">
     31      <Filter>Source Files</Filter>
     32    </ClCompile>
    2733  </ItemGroup>
    2834  <ItemGroup>
    29     <ClInclude Include="..\..\common\message.h">
    30       <Filter>Source Files\common</Filter>
     35    <ClInclude Include="chat.h">
     36      <Filter>Header Files</Filter>
    3137    </ClInclude>
    3238    <ClInclude Include="..\..\common\compiler.h">
    33       <Filter>Source Files\common</Filter>
     39      <Filter>Header Files\common</Filter>
     40    </ClInclude>
     41    <ClInclude Include="..\..\common\message.h">
     42      <Filter>Header Files\common</Filter>
    3443    </ClInclude>
    3544  </ItemGroup>
  • client/Client/main.cpp

    r9a3e6b1 r6475138  
    2626#include "../../common/message.h"
    2727
     28#include "chat.h"
     29
    2830#ifdef WINDOWS
    29         #pragma comment(lib, "ws2_32.lib")
     31   #pragma comment(lib, "ws2_32.lib")
    3032#endif
    3133
     
    5658   bool doexit = false;
    5759
    58    vector<string> vctChat;
    59    string strPrompt;
    60    string strEnteredInput;
     60   chat chatConsole;
    6161
    6262   if(!al_init()) {
     
    161161      error("receiveMessage");
    162162       
    163    vctChat.push_back(string(msgFrom.buffer));
     163   chatConsole.addLine(msgFrom.buffer);
    164164
    165165   al_start_timer(timer);
     
    193193      }
    194194      else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
    195          switch(ev.keyboard.keycode) {
    196             case ALLEGRO_KEY_UP:
    197                key[KEY_UP] = true;
    198                break;
    199  
    200             case ALLEGRO_KEY_DOWN:
    201                key[KEY_DOWN] = true;
    202                break;
    203  
    204             case ALLEGRO_KEY_LEFT:
    205                key[KEY_LEFT] = true;
    206                break;
    207  
    208             case ALLEGRO_KEY_RIGHT:
    209                key[KEY_RIGHT] = true;
    210                break;
    211 
    212                         case ALLEGRO_KEY_ENTER:
    213                            strEnteredInput = strPrompt;
    214                            strPrompt.clear();
    215 
    216                            if (strEnteredInput.compare("q") == 0) {
    217                               doexit = true;
    218                        }
    219                            else
    220                            {
    221                   strcpy(msgTo.buffer, strEnteredInput.c_str());
    222                           n=sendMessage(&msgTo, sock, &server);
    223                           if (n < 0)
    224                                  error("sendMessage");
    225 
    226                           n = receiveMessage(&msgFrom, sock, &from);
    227                           if (n < 0)
    228                                  error("receiveMessage");
    229 
    230                           vctChat.push_back(string(msgFrom.buffer));
    231                            }
    232 
    233                            break;
    234          }
    235 
    236                  if(ALLEGRO_KEY_A <= ev.keyboard.keycode && ev.keyboard.keycode <= ALLEGRO_KEY_Z)
    237                  {
    238             char newChar = 'a'+ev.keyboard.keycode-ALLEGRO_KEY_A;
    239             strPrompt.append(1, newChar);
    240                  }
    241 
    242                  if(ALLEGRO_KEY_0 <= ev.keyboard.keycode && ev.keyboard.keycode <= ALLEGRO_KEY_9)
    243                  {
    244             char newChar = '0'+ev.keyboard.keycode-ALLEGRO_KEY_0;
    245             strPrompt.append(1, newChar);
    246                  }
     195         bool eventConsumed = chatConsole.processEvent(ev);
     196
     197         if (eventConsumed) {
     198            string input = chatConsole.getInput();
     199            if (!input.empty()) {
     200               cout << "input: " << input << endl;
     201               strcpy(msgTo.buffer, input.c_str());
     202               
     203               n=sendMessage(&msgTo, sock, &server);
     204               if (n < 0)
     205                  error("sendMessage");
     206
     207               n = receiveMessage(&msgFrom, sock, &from);
     208               if (n < 0)
     209                  error("receiveMessage");
     210
     211               chatConsole.addLine(string(msgFrom.buffer));
     212               cout << "Added new line" << endl;
     213            }
     214         }else {
     215            switch(ev.keyboard.keycode) {
     216               case ALLEGRO_KEY_UP:
     217                  key[KEY_UP] = true;
     218                  break;
     219 
     220               case ALLEGRO_KEY_DOWN:
     221                  key[KEY_DOWN] = true;
     222                  break;
     223 
     224               case ALLEGRO_KEY_LEFT:
     225                  key[KEY_LEFT] = true;
     226                  break;
     227 
     228               case ALLEGRO_KEY_RIGHT:
     229                  key[KEY_RIGHT] = true;
     230                  break;
     231            }
     232         }
    247233      }
    248234      else if(ev.type == ALLEGRO_EVENT_KEY_UP) {
     
    277263         al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0);
    278264
    279                  for(int x=0; x<vctChat.size(); x++)
    280             al_draw_text(font, al_map_rgb(255,255,255), 10, 10+x*15, ALLEGRO_ALIGN_LEFT, vctChat[x].c_str());
    281 
    282                  al_draw_text(font, al_map_rgb(255,255,255), 10, 460, ALLEGRO_ALIGN_LEFT, strPrompt.c_str());
    283          
     265         chatConsole.draw(font, al_map_rgb(255,255,255));
     266
    284267         al_flip_display();
    285268      }
Note: See TracChangeset for help on using the changeset viewer.