Changeset 87b3ee2 in network-game for client/Client


Ignore:
Timestamp:
Dec 3, 2012, 12:24:23 AM (12 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
41ad8ed
Parents:
439f7bc
Message:

Created a simple gui for the client

Location:
client/Client
Files:
8 added
5 edited

Legend:

Unmodified
Added
Removed
  • client/Client/Client.vcxproj

    r439f7bc r87b3ee2  
    6666    <ClCompile Include="..\..\common\message.cpp" />
    6767    <ClCompile Include="chat.cpp" />
     68    <ClCompile Include="Button.cpp" />
     69    <ClCompile Include="GuiComponent.cpp" />
    6870    <ClCompile Include="main.cpp" />
     71    <ClCompile Include="Textbox.cpp" />
     72    <ClCompile Include="Window.cpp" />
    6973  </ItemGroup>
    7074  <ItemGroup>
     
    7276    <ClInclude Include="..\..\common\message.h" />
    7377    <ClInclude Include="chat.h" />
     78    <ClInclude Include="Button.h" />
     79    <ClInclude Include="GuiComponent.h" />
     80    <ClInclude Include="Textbox.h" />
     81    <ClInclude Include="Window.h" />
    7482  </ItemGroup>
    7583  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
  • client/Client/Client.vcxproj.filters

    r439f7bc r87b3ee2  
    2020      <UniqueIdentifier>{6c9ad2f2-bc8d-4e25-9c5c-c4440c60991c}</UniqueIdentifier>
    2121    </Filter>
     22    <Filter Include="Header Files\gui">
     23      <UniqueIdentifier>{c6c812ef-0533-42bc-9d71-aa40428b5ca8}</UniqueIdentifier>
     24    </Filter>
     25    <Filter Include="Source Files\gui">
     26      <UniqueIdentifier>{10ae8361-2a07-4085-b595-761b47868bad}</UniqueIdentifier>
     27    </Filter>
    2228  </ItemGroup>
    2329  <ItemGroup>
     
    2935    </ClCompile>
    3036    <ClCompile Include="chat.cpp">
     37      <Filter>Source Files</Filter>
     38    </ClCompile>
     39    <ClCompile Include="GuiComponent.cpp">
     40      <Filter>Source Files\gui</Filter>
     41    </ClCompile>
     42    <ClCompile Include="Textbox.cpp">
     43      <Filter>Source Files\gui</Filter>
     44    </ClCompile>
     45    <ClCompile Include="Button.cpp">
     46      <Filter>Source Files\gui</Filter>
     47    </ClCompile>
     48    <ClCompile Include="Window.cpp">
    3149      <Filter>Source Files</Filter>
    3250    </ClCompile>
     
    4260      <Filter>Header Files\common</Filter>
    4361    </ClInclude>
     62    <ClInclude Include="GuiComponent.h">
     63      <Filter>Header Files\gui</Filter>
     64    </ClInclude>
     65    <ClInclude Include="Textbox.h">
     66      <Filter>Header Files\gui</Filter>
     67    </ClInclude>
     68    <ClInclude Include="Button.h">
     69      <Filter>Header Files\gui</Filter>
     70    </ClInclude>
     71    <ClInclude Include="Window.h">
     72      <Filter>Header Files</Filter>
     73    </ClInclude>
    4474  </ItemGroup>
    4575</Project>
  • client/Client/chat.cpp

    r439f7bc r87b3ee2  
    1919{
    2020   for(int x=0; x<vctChat.size(); x++)
    21       al_draw_text(font, color, 10, 10+x*15, ALLEGRO_ALIGN_LEFT, vctChat[x].c_str());
     21      al_draw_text(font, color, 10, 140+x*15, ALLEGRO_ALIGN_LEFT, vctChat[x].c_str());
    2222
    2323   al_draw_text(font, color, 10, 460, ALLEGRO_ALIGN_LEFT, strPrompt.c_str());
     
    3030
    3131// returns true if the event was consumed, false if it should be passed on
    32 bool chat::processEvent(ALLEGRO_EVENT ev)
     32bool chat::handleEvent(ALLEGRO_EVENT e)
    3333{
    3434   ALLEGRO_KEYBOARD_STATE keys;
    3535   al_get_keyboard_state(&keys);
    3636
    37    if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
     37   if (e.type == ALLEGRO_EVENT_KEY_DOWN) {
    3838      char newChar = 0;
    3939
    40       if (ALLEGRO_KEY_A <= ev.keyboard.keycode && ev.keyboard.keycode <= ALLEGRO_KEY_Z) {
    41          newChar = 'a'+ev.keyboard.keycode-ALLEGRO_KEY_A;
     40      if (ALLEGRO_KEY_A <= e.keyboard.keycode && e.keyboard.keycode <= ALLEGRO_KEY_Z) {
     41         newChar = 'a'+e.keyboard.keycode-ALLEGRO_KEY_A;
    4242         if (al_key_down(&keys, ALLEGRO_KEY_LSHIFT) || al_key_down(&keys, ALLEGRO_KEY_RSHIFT))
    4343            newChar -= 32;
    4444      }
    45       if (ALLEGRO_KEY_0 <= ev.keyboard.keycode && ev.keyboard.keycode <= ALLEGRO_KEY_9)
    46          newChar = '0'+ev.keyboard.keycode-ALLEGRO_KEY_0;
     45      if (ALLEGRO_KEY_0 <= e.keyboard.keycode && e.keyboard.keycode <= ALLEGRO_KEY_9)
     46         newChar = '0'+e.keyboard.keycode-ALLEGRO_KEY_0;
    4747
    4848      if (newChar != 0) {
     
    5151      }
    5252
    53       if (ev.keyboard.keycode == ALLEGRO_KEY_ENTER) {
     53      if (e.keyboard.keycode == ALLEGRO_KEY_ENTER) {
    5454         strEnteredInput = strPrompt;
    5555         strPrompt.clear();
  • client/Client/chat.h

    r439f7bc r87b3ee2  
    2424   void addLine(string s);
    2525
    26    bool processEvent(ALLEGRO_EVENT ev);
     26   bool handleEvent(ALLEGRO_EVENT e);
    2727};
    2828
  • client/Client/main.cpp

    r439f7bc r87b3ee2  
    1818#include <string>
    1919#include <iostream>
    20 #include <vector>
    2120
    2221#include <allegro5/allegro.h>
     
    2625#include "../../common/message.h"
    2726
     27#include "Window.h"
     28#include "Textbox.h"
     29#include "Button.h"
    2830#include "chat.h"
    2931
     
    3638void initWinSock();
    3739void shutdownWinSock();
    38 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, string &username);
     40void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole);
     41
     42// callbacks
     43void registerAccount();
     44void login();
     45void logout();
     46void quit();
     47void sendChatMessage();
    3948
    4049void error(const char *);
     
    5362enum STATE {
    5463   STATE_START,
    55    STATE_LOGIN,
    56    STATE_LOGOUT
     64   STATE_LOGIN // this means you're already logged in
    5765};
     66
     67int state;
     68
     69bool doexit;
     70
     71Window* wndLogin;
     72Window* wndMain;
     73Window* wndCurrent;
     74
     75Textbox* txtUsername;
     76Textbox* txtPassword;
     77Textbox* txtChat;
     78
     79int sock;
     80struct sockaddr_in server, from;
     81struct hostent *hp;
     82NETWORK_MSG msgTo, msgFrom;
     83string username;
     84chat chatConsole;
    5885 
    5986int main(int argc, char **argv)
     
    6390   ALLEGRO_TIMER *timer = NULL;
    6491   ALLEGRO_BITMAP *bouncer = NULL;
     92   bool key[4] = { false, false, false, false };
     93   bool redraw = true;
     94   doexit = false;
     95
    6596   float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0;
    6697   float bouncer_y = SCREEN_H / 2.0 - BOUNCER_SIZE / 2.0;
    67    bool key[4] = { false, false, false, false };
    68    bool redraw = true;
    69    bool doexit = false;
    70 
    71    int state = STATE_START;
    72 
    73    chat chatConsole;
     98
     99   state = STATE_START;
    74100
    75101   if(!al_init()) {
     
    78104   }
    79105
     106   al_init_primitives_addon();
    80107   al_init_font_addon();
    81108   al_init_ttf_addon();
     
    93120      return -1;
    94121   }
     122
     123    if(!al_install_mouse()) {
     124      fprintf(stderr, "failed to initialize the mouse!\n");
     125      return -1;
     126   }
    95127 
    96128   timer = al_create_timer(1.0 / FPS);
     
    106138      return -1;
    107139   }
    108  
     140
     141   wndLogin = new Window(0, 0, SCREEN_W, SCREEN_H);
     142   wndLogin->addComponent(new Textbox(104, 40, 100, 20, font));
     143   wndLogin->addComponent(new Textbox(104, 70, 100, 20, font));
     144   wndLogin->addComponent(new Button(22, 100, 90, 20, font, "Register", registerAccount));
     145   wndLogin->addComponent(new Button(122, 100, 60, 20, font, "Login", login));
     146   wndLogin->addComponent(new Button(540, 10, 80, 20, font, "Quit", quit));
     147
     148   txtUsername = (Textbox*)wndLogin->getComponent(0);
     149   txtPassword = (Textbox*)wndLogin->getComponent(1);
     150
     151   wndMain = new Window(0, 0, SCREEN_W, SCREEN_H);
     152   wndMain->addComponent(new Textbox(95, 40, 525, 20, font));
     153   wndMain->addComponent(new Button(95, 70, 160, 20, font, "Send Message", sendChatMessage));
     154   wndMain->addComponent(new Button(540, 10, 80, 20, font, "Logout", logout));
     155
     156   txtChat = (Textbox*)wndMain->getComponent(0);
     157
     158   wndCurrent = wndLogin;
     159
    109160   bouncer = al_create_bitmap(BOUNCER_SIZE, BOUNCER_SIZE);
    110161   if(!bouncer) {
     
    131182 
    132183   al_register_event_source(event_queue, al_get_display_event_source(display));
    133  
    134184   al_register_event_source(event_queue, al_get_timer_event_source(timer));
    135  
    136185   al_register_event_source(event_queue, al_get_keyboard_event_source());
     186   al_register_event_source(event_queue, al_get_mouse_event_source());
    137187 
    138188   al_clear_to_color(al_map_rgb(0,0,0));
    139189 
    140190   al_flip_display();
    141 
    142    int sock;
    143    struct sockaddr_in server, from;
    144    struct hostent *hp;
    145    NETWORK_MSG msgTo, msgFrom;
    146    string username;
    147191
    148192   if (argc != 3) {
     
    171215      ALLEGRO_EVENT ev;
    172216      al_wait_for_event(event_queue, &ev);
    173  
    174       if(ev.type == ALLEGRO_EVENT_TIMER) {
     217
     218      if(wndCurrent->handleEvent(ev)) {
     219         // do nothing
     220      }
     221      else if(ev.type == ALLEGRO_EVENT_TIMER) {
    175222         if(key[KEY_UP] && bouncer_y >= 4.0) {
    176223            bouncer_y -= 4.0;
     
    195242      }
    196243      else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
    197          bool eventConsumed = chatConsole.processEvent(ev);
    198 
    199          if (eventConsumed) {
    200             string input = chatConsole.getInput();
    201             if (!input.empty()) {
    202                cout << "input: " << input << endl;
    203                strcpy(msgTo.buffer, input.c_str());
    204 
    205                switch(state)
    206                {
    207                   case STATE_START:
    208                   {
    209                      username = input;
    210                      strcpy(msgTo.buffer+input.size()+1, "MyPassword");
    211                      msgTo.type = MSG_TYPE_REGISTER;
    212                      //msgTo.type = MSG_TYPE_LOGIN;
    213 
    214                      sendMessage(&msgTo, sock, &server);
    215                      receiveMessage(&msgFrom, sock, &from);
    216                      processMessage(msgFrom, state, chatConsole, username);
    217                      cout << "state: " << state << endl;
    218 
    219                      break;
    220                   }
    221                   case STATE_LOGIN:
    222                   {
    223                      if (input.compare("quit") == 0 ||
    224                          input.compare("exit") == 0 ||
    225                          input.compare("logout") == 0)
    226                      {
    227                         strcpy(msgTo.buffer, username.c_str());
    228                         msgTo.type = MSG_TYPE_LOGOUT;
    229                      }
    230                      else
    231                         msgTo.type = MSG_TYPE_CHAT;
    232 
    233                      sendMessage(&msgTo, sock, &server);
    234                      receiveMessage(&msgFrom, sock, &from);
    235                      processMessage(msgFrom, state, chatConsole, username);
    236                      cout << "state: " << state << endl;
    237 
    238                      break;
    239                   }
    240                   case STATE_LOGOUT:
    241                   {
    242                      chatConsole.addLine("You're logged out, so you can't send any messages to the server.");
    243 
    244                      cout << "You're logged out, so you can't send any messages to the server." << endl;
    245                  
    246                      break;
    247                   }
    248                   default:
    249                   {
    250                      cout << "The state has an invalid value: " << state << endl;
    251                  
    252                      break;
    253                   }
    254                }
    255             }
    256          }else {
    257             switch(ev.keyboard.keycode) {
    258                case ALLEGRO_KEY_UP:
    259                   key[KEY_UP] = true;
    260                   break;
    261  
    262                case ALLEGRO_KEY_DOWN:
    263                   key[KEY_DOWN] = true;
    264                   break;
    265  
    266                case ALLEGRO_KEY_LEFT:
    267                   key[KEY_LEFT] = true;
    268                   break;
    269  
    270                case ALLEGRO_KEY_RIGHT:
    271                   key[KEY_RIGHT] = true;
    272                   break;
    273             }
     244         switch(ev.keyboard.keycode) {
     245            case ALLEGRO_KEY_UP:
     246               key[KEY_UP] = true;
     247               break;
     248 
     249            case ALLEGRO_KEY_DOWN:
     250               key[KEY_DOWN] = true;
     251               break;
     252 
     253            case ALLEGRO_KEY_LEFT:
     254               key[KEY_LEFT] = true;
     255               break;
     256 
     257            case ALLEGRO_KEY_RIGHT:
     258               key[KEY_RIGHT] = true;
     259               break;
    274260         }
    275261      }
     
    301287         redraw = false;
    302288 
    303          al_clear_to_color(al_map_rgb(0,0,0));
     289         wndCurrent->draw(display);
    304290 
    305291         al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0);
    306292
    307293         chatConsole.draw(font, al_map_rgb(255,255,255));
     294
     295         if(wndCurrent == wndLogin) {
     296            al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Username:");
     297            al_draw_text(font, al_map_rgb(0, 255, 0), 1, 73, ALLEGRO_ALIGN_LEFT, "Password:");
     298         }
     299         else if(wndCurrent == wndMain) {
     300            al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Message:");
     301         }
    308302
    309303         al_flip_display();
     
    319313   shutdownWinSock();
    320314   
     315   delete wndLogin;
     316   delete wndMain;
     317
    321318   al_destroy_event_queue(event_queue);
    322319   al_destroy_bitmap(bouncer);
     
    360357}
    361358
    362 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, string &username)
     359void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole)
    363360{
    364361   string response = string(msg.buffer);
     
    372369         chatConsole.addLine(response);
    373370
    374          /*
    375          if (response.compare("Player has already logged in.") == 0)
     371         switch(msg.type)
    376372         {
    377             cout << "User login failed" << endl;
    378             username.clear();
    379          }
    380          else
    381          {
    382             cout << "User login successful" << endl;
    383             state = STATE_LOGIN;
    384          }
    385          */
     373            case MSG_TYPE_REGISTER:
     374            {
     375               break;
     376            }
     377            case MSG_TYPE_LOGIN:
     378            {
     379               if (response.compare("Player has already logged in.") == 0)
     380               {
     381                  username.clear();
     382                  cout << "User login failed" << endl;
     383               }
     384               else if (response.compare("Incorrect username or password") == 0)
     385               {
     386                  username.clear();
     387                  cout << "User login failed" << endl;
     388               }
     389               else
     390               {
     391                  state = STATE_LOGIN;
     392                  wndCurrent = wndMain;
     393                  cout << "User login successful" << endl;
     394               }
     395               break;
     396            }
     397         }
    386398
    387399         break;
     
    391403         chatConsole.addLine(response);
    392404
    393          if (response.compare("You have successfully logged out. You may quit the game.") == 0)
     405          switch(msg.type)
    394406         {
    395             cout << "Logged out" << endl;
    396             state = STATE_LOGOUT;
    397          }
    398          else
    399          {
    400             cout << "Added new line" << endl;
     407            case MSG_TYPE_REGISTER:
     408            {
     409               break;
     410            }
     411            case MSG_TYPE_LOGIN:
     412            {
     413               if (response.compare("You have successfully logged out.") == 0)
     414               {
     415                  cout << "Logged out" << endl;
     416                  state = STATE_START;
     417                  wndCurrent = wndLogin;
     418               }
     419               else
     420               {
     421                  cout << "Added new line" << endl;
     422               }
     423
     424               break;
     425            }
    401426         }
    402427                     
    403          break;
    404       }
    405       case STATE_LOGOUT:
    406       {
    407          cout << "Bug: You're logged out, so you shouldn't be receiving any messages." << endl;
    408                  
    409428         break;
    410429      }
     
    417436   }
    418437}
     438
     439void registerAccount()
     440{
     441   string username = txtUsername->getStr();
     442   string password = txtPassword->getStr();
     443
     444   txtUsername->clear();
     445   txtPassword->clear();
     446
     447   msgTo.type = MSG_TYPE_REGISTER;
     448
     449   strcpy(msgTo.buffer, username.c_str());
     450   strcpy(msgTo.buffer+username.size()+1, password.c_str());
     451
     452   sendMessage(&msgTo, sock, &server);
     453   receiveMessage(&msgFrom, sock, &from);
     454   processMessage(msgFrom, state, chatConsole);
     455   cout << "state: " << state << endl;
     456}
     457
     458void login()
     459{
     460   string strUsername = txtUsername->getStr();
     461   string strPassword = txtPassword->getStr();
     462   username = strUsername;
     463
     464   txtUsername->clear();
     465   txtPassword->clear();
     466
     467   msgTo.type = MSG_TYPE_LOGIN;
     468
     469   strcpy(msgTo.buffer, strUsername.c_str());
     470   strcpy(msgTo.buffer+username.size()+1, strPassword.c_str());
     471
     472   sendMessage(&msgTo, sock, &server);
     473   receiveMessage(&msgFrom, sock, &from);
     474   processMessage(msgFrom, state, chatConsole);
     475   cout << "state: " << state << endl;
     476}
     477
     478void logout()
     479{
     480   txtChat->clear();
     481
     482   msgTo.type = MSG_TYPE_LOGOUT;
     483
     484   strcpy(msgTo.buffer, username.c_str());
     485
     486   sendMessage(&msgTo, sock, &server);
     487   receiveMessage(&msgFrom, sock, &from);
     488   processMessage(msgFrom, state, chatConsole);
     489}
     490
     491void quit()
     492{
     493   doexit = true;
     494}
     495
     496void sendChatMessage()
     497{
     498   string msg = txtChat->getStr();
     499
     500   txtChat->clear();
     501
     502   msgTo.type = MSG_TYPE_CHAT;
     503
     504   strcpy(msgTo.buffer, msg.c_str());
     505
     506   sendMessage(&msgTo, sock, &server);
     507   receiveMessage(&msgFrom, sock, &from);
     508   processMessage(msgFrom, state, chatConsole);
     509}
Note: See TracChangeset for help on using the changeset viewer.