Changeset 929b4e0 in network-game for client/Client/main.cpp


Ignore:
Timestamp:
Sep 25, 2013, 2:20:46 AM (11 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
bbebe9c
Parents:
3eac3b3
Message:

The lobby screen has some gui elements and basic functionality

File:
1 edited

Legend:

Unmodified
Added
Removed
  • client/Client/main.cpp

    r3eac3b3 r929b4e0  
    5656POSITION mapToScreen(POSITION pos);
    5757int getRefreshRate(int width, int height);
    58 
    59 // callbacks
     58void drawMessageStatus(ALLEGRO_FONT* font);
     59
     60// Callback declarations
    6061void goToLoginScreen();
    6162void goToRegisterScreen();
     
    6667void sendChatMessage();
    6768void toggleDebugging();
    68 void drawMessageStatus(ALLEGRO_FONT* font);
     69void joinGame();
     70void createGame();
    6971
    7072void error(const char *);
     
    101103RadioButtonList* rblClasses;
    102104TextLabel* lblRegisterStatus;
     105
     106// wndLobby
     107Textbox* txtJoinGame;
     108Textbox* txtCreateGame;
    103109
    104110// wndGame
     
    241247
    242248   wndLobby = new Window(0, 0, SCREEN_W, SCREEN_H);
     249   wndLobby->addComponent(new Button(920, 10, 80, 20, font, "Logout", logout));
     250   wndLobby->addComponent(new TextLabel(SCREEN_W*1/4-112, 40, 110, 20, font, "Game Name:", ALLEGRO_ALIGN_RIGHT));
     251   wndLobby->addComponent(new Textbox(SCREEN_W*1/4+4, 40, 100, 20, font));
     252   wndLobby->addComponent(new Button(SCREEN_W*1/4-100, 80, 200, 20, font, "Join Existing Game", joinGame));
     253   wndLobby->addComponent(new TextLabel(SCREEN_W*3/4-112, 40, 110, 20, font, "Game Name:", ALLEGRO_ALIGN_RIGHT));
     254   wndLobby->addComponent(new Textbox(SCREEN_W*3/4+4, 40, 100, 20, font));
     255   wndLobby->addComponent(new Button(SCREEN_W*3/4-100, 80, 200, 20, font, "Create New Game", createGame));
     256
     257   txtJoinGame = (Textbox*)wndLobby->getComponent(2);
     258   txtCreateGame = (Textbox*)wndLobby->getComponent(5);
    243259
    244260   cout << "Created lobby screen" << endl;
     
    363379         if(wndCurrent == wndLobby) {
    364380            if (ev.mouse.button == 1) { // left click
     381               txtJoinGame->clear();
     382               txtCreateGame->clear();
    365383               state = STATE_GAME;
    366384               wndCurrent = wndGame;
     
    922940}
    923941
     942int getRefreshRate(int width, int height)
     943{
     944   int numRefreshRates = al_get_num_display_modes();
     945   ALLEGRO_DISPLAY_MODE displayMode;
     946
     947   for(int i=0; i<numRefreshRates; i++) {
     948      al_get_display_mode(i, &displayMode);
     949
     950      if (displayMode.width == width && displayMode.height == height)
     951         return displayMode.refresh_rate;
     952   }
     953
     954   return 0;
     955}
     956
     957void drawMessageStatus(ALLEGRO_FONT* font)
     958{
     959   int clientMsgOffset = 5;
     960   int serverMsgOffset = 950;
     961
     962   al_draw_text(font, al_map_rgb(0, 255, 255), 0+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "ID");
     963   al_draw_text(font, al_map_rgb(0, 255, 255), 20+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "Type");
     964   al_draw_text(font, al_map_rgb(0, 255, 255), 240+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "Acked?");
     965
     966   al_draw_text(font, al_map_rgb(0, 255, 255), serverMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "ID");
     967
     968   map<unsigned int, map<unsigned long, MessageContainer> >& sentMessages = msgProcessor.getSentMessages();
     969   int id, type;
     970   bool acked;
     971   ostringstream ossId, ossAcked;
     972
     973   map<unsigned int, map<unsigned long, MessageContainer> >::iterator it;
     974
     975   int msgCount = 0;
     976   for (it = sentMessages.begin(); it != sentMessages.end(); it++) {
     977      map<unsigned long, MessageContainer> playerMessage = it->second;
     978      map<unsigned long, MessageContainer>::iterator it2;
     979      for (it2 = playerMessage.begin(); it2 !=  playerMessage.end(); it2++) {
     980
     981         id = it->first;
     982         ossId.str("");;
     983         ossId << id;
     984
     985         type = it2->second.getMessage()->type;
     986         string typeStr = MessageContainer::getMsgTypeString(type);
     987
     988         acked = it2->second.getAcked();
     989         ossAcked.str("");;
     990         ossAcked << boolalpha << acked;
     991
     992         al_draw_text(font, al_map_rgb(0, 255, 0), clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossId.str().c_str());
     993         al_draw_text(font, al_map_rgb(0, 255, 0), 20+clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, typeStr.c_str());
     994         al_draw_text(font, al_map_rgb(0, 255, 0), 240+clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossAcked.str().c_str());
     995
     996         msgCount++;
     997      }
     998   }
     999
     1000   if (msgProcessor.getAckedMessages().size() > 0) {
     1001      map<unsigned int, unsigned long long> ackedMessages = msgProcessor.getAckedMessages()[0];
     1002      map<unsigned int, unsigned long long>::iterator it3;
     1003
     1004      msgCount = 0;
     1005      for (it3 = ackedMessages.begin(); it3 != ackedMessages.end(); it3++) {
     1006         ossId.str("");;
     1007         ossId << it3->first;
     1008
     1009         al_draw_text(font, al_map_rgb(255, 0, 0), 25+serverMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossId.str().c_str());
     1010
     1011         msgCount++;
     1012      }
     1013   }
     1014}
     1015
     1016// Callback definitions
     1017
    9241018void goToRegisterScreen()
    9251019{
     
    9971091void logout()
    9981092{
     1093   switch(state) {
     1094   case STATE_LOBBY:
     1095      txtJoinGame->clear();
     1096      txtCreateGame->clear();
     1097      break;
     1098   case STATE_GAME:
     1099      txtChat->clear();
     1100      chatConsole.clear();
     1101      break;
     1102   default:
     1103      cout << "Logout called from invalid state: " << state << endl;
     1104      break;
     1105   }
     1106
     1107   msgTo.type = MSG_TYPE_LOGOUT;
     1108
     1109   strcpy(msgTo.buffer, username.c_str());
     1110
     1111   msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
     1112}
     1113
     1114void quit()
     1115{
     1116   doexit = true;
     1117}
     1118
     1119void sendChatMessage()
     1120{
     1121   string msg = txtChat->getStr();
     1122
    9991123   txtChat->clear();
    1000    chatConsole.clear();
    1001 
    1002    msgTo.type = MSG_TYPE_LOGOUT;
    1003 
    1004    strcpy(msgTo.buffer, username.c_str());
     1124
     1125   msgTo.type = MSG_TYPE_CHAT;
     1126
     1127   strcpy(msgTo.buffer, msg.c_str());
    10051128
    10061129   msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
    10071130}
    10081131
    1009 void quit()
    1010 {
    1011    doexit = true;
    1012 }
    1013 
    1014 void sendChatMessage()
    1015 {
    1016    string msg = txtChat->getStr();
    1017 
    1018    txtChat->clear();
    1019 
    1020    msgTo.type = MSG_TYPE_CHAT;
    1021 
    1022    strcpy(msgTo.buffer, msg.c_str());
    1023 
    1024    msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
    1025 }
    1026 
    10271132void toggleDebugging()
    10281133{
     
    10301135}
    10311136
    1032 int getRefreshRate(int width, int height)
    1033 {
    1034    int numRefreshRates = al_get_num_display_modes();
    1035    ALLEGRO_DISPLAY_MODE displayMode;
    1036 
    1037    for(int i=0; i<numRefreshRates; i++) {
    1038       al_get_display_mode(i, &displayMode);
    1039 
    1040       if (displayMode.width == width && displayMode.height == height)
    1041          return displayMode.refresh_rate;
    1042    }
    1043 
    1044    return 0;
    1045 }
    1046 
    1047 void drawMessageStatus(ALLEGRO_FONT* font)
    1048 {
    1049    int clientMsgOffset = 5;
    1050    int serverMsgOffset = 950;
    1051 
    1052    al_draw_text(font, al_map_rgb(0, 255, 255), 0+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "ID");
    1053    al_draw_text(font, al_map_rgb(0, 255, 255), 20+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "Type");
    1054    al_draw_text(font, al_map_rgb(0, 255, 255), 240+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "Acked?");
    1055 
    1056    al_draw_text(font, al_map_rgb(0, 255, 255), serverMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "ID");
    1057 
    1058    map<unsigned int, map<unsigned long, MessageContainer> >& sentMessages = msgProcessor.getSentMessages();
    1059    int id, type;
    1060    bool acked;
    1061    ostringstream ossId, ossAcked;
    1062 
    1063    map<unsigned int, map<unsigned long, MessageContainer> >::iterator it;
    1064 
    1065    int msgCount = 0;
    1066    for (it = sentMessages.begin(); it != sentMessages.end(); it++) {
    1067       map<unsigned long, MessageContainer> playerMessage = it->second;
    1068       map<unsigned long, MessageContainer>::iterator it2;
    1069       for (it2 = playerMessage.begin(); it2 !=  playerMessage.end(); it2++) {
    1070 
    1071          id = it->first;
    1072          ossId.str("");;
    1073          ossId << id;
    1074 
    1075          type = it2->second.getMessage()->type;
    1076          string typeStr = MessageContainer::getMsgTypeString(type);
    1077 
    1078          acked = it2->second.getAcked();
    1079          ossAcked.str("");;
    1080          ossAcked << boolalpha << acked;
    1081 
    1082          al_draw_text(font, al_map_rgb(0, 255, 0), clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossId.str().c_str());
    1083          al_draw_text(font, al_map_rgb(0, 255, 0), 20+clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, typeStr.c_str());
    1084          al_draw_text(font, al_map_rgb(0, 255, 0), 240+clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossAcked.str().c_str());
    1085 
    1086          msgCount++;
    1087       }
    1088    }
    1089 
    1090    if (msgProcessor.getAckedMessages().size() > 0) {
    1091       map<unsigned int, unsigned long long> ackedMessages = msgProcessor.getAckedMessages()[0];
    1092       map<unsigned int, unsigned long long>::iterator it3;
    1093 
    1094       msgCount = 0;
    1095       for (it3 = ackedMessages.begin(); it3 != ackedMessages.end(); it3++) {
    1096          ossId.str("");;
    1097          ossId << it3->first;
    1098 
    1099          al_draw_text(font, al_map_rgb(255, 0, 0), 25+serverMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossId.str().c_str());
    1100 
    1101          msgCount++;
    1102       }
    1103    }
    1104 }
     1137void joinGame()
     1138{
     1139   cout << "Joining game" << endl;
     1140}
     1141
     1142void createGame()
     1143{
     1144   cout << "Creating game" << endl;
     1145}
Note: See TracChangeset for help on using the changeset viewer.