Changeset f63aa57 in network-game


Ignore:
Timestamp:
Dec 27, 2013, 5:57:25 PM (11 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
9bfc1cb
Parents:
cbc70eb
Message:

The old Game window has been completely removed from the client and the client frees all gui components on its own, instead of relying on the Window destructor

Location:
client/Client
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • client/Client/Window.cpp

    rcbc70eb rf63aa57  
    88Window::~Window(void)
    99{
    10    for(unsigned int x=0; x<this->vctGui.size(); x++)
    11       delete this->vctGui[x];
    1210}
    1311
    14 void Window::addComponent(GuiComponent *comp)
     12GuiComponent* Window::addComponent(GuiComponent* comp)
    1513{
    1614   this->vctGui.push_back(comp);
     15   return comp;
    1716}
    1817
  • client/Client/Window.h

    rcbc70eb rf63aa57  
    1818   ~Window(void);
    1919
    20    void addComponent(GuiComponent *comp);
     20   GuiComponent* addComponent(GuiComponent* comp);
    2121   GuiComponent* getComponent(int x);
    2222   void draw(ALLEGRO_DISPLAY *display);
  • client/Client/main.cpp

    rcbc70eb rf63aa57  
    2222#include <fstream>
    2323#include <map>
     24#include <vector>
    2425
    2526#include <allegro5/allegro.h>
     
    8889bool doexit;
    8990
     91vector<GuiComponent*> vctComponents;
     92
    9093Window* wndLogin;
    9194Window* wndRegister;
    9295Window* wndLobby;
    93 Window* wndGame;
     96Window* wndLobbyDebug;
    9497Window* wndNewGame;
    95 Window* wndGameDebug;
    9698Window* wndGameSummary;
    9799Window* wndCurrent;
     
    111113Textbox* txtJoinGame;
    112114Textbox* txtCreateGame;
    113 
    114 // wndGame
    115115Textbox* txtChat;
    116116
     
    213213
    214214   wndLogin = new Window(0, 0, SCREEN_W, SCREEN_H);
    215    wndLogin->addComponent(new Textbox(516, 40, 100, 20, font));
    216    wndLogin->addComponent(new Textbox(516, 70, 100, 20, font));
    217    wndLogin->addComponent(new TextLabel(410, 40, 100, 20, font, "Username:", ALLEGRO_ALIGN_RIGHT));
    218    wndLogin->addComponent(new TextLabel(410, 70, 100, 20, font, "Password:", ALLEGRO_ALIGN_RIGHT));
    219    wndLogin->addComponent(new TextLabel((SCREEN_W-600)/2, 100, 600, 20, font, "", ALLEGRO_ALIGN_CENTRE));
    220    wndLogin->addComponent(new Button(SCREEN_W/2-100, 130, 90, 20, font, "Register", goToRegisterScreen));
    221    wndLogin->addComponent(new Button(SCREEN_W/2+10, 130, 90, 20, font, "Login", login));
    222    wndLogin->addComponent(new Button(920, 10, 80, 20, font, "Quit", quit));
    223    wndLogin->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging));
     215   vctComponents.push_back(wndLogin->addComponent(new Textbox(516, 40, 100, 20, font)));
     216   vctComponents.push_back(wndLogin->addComponent(new Textbox(516, 70, 100, 20, font)));
     217   vctComponents.push_back(wndLogin->addComponent(new TextLabel(410, 40, 100, 20, font, "Username:", ALLEGRO_ALIGN_RIGHT)));
     218   vctComponents.push_back(wndLogin->addComponent(new TextLabel(410, 70, 100, 20, font, "Password:", ALLEGRO_ALIGN_RIGHT)));
     219   vctComponents.push_back(wndLogin->addComponent(new TextLabel((SCREEN_W-600)/2, 100, 600, 20, font, "", ALLEGRO_ALIGN_CENTRE)));
     220   vctComponents.push_back(wndLogin->addComponent(new Button(SCREEN_W/2-100, 130, 90, 20, font, "Register", goToRegisterScreen)));
     221   vctComponents.push_back(wndLogin->addComponent(new Button(SCREEN_W/2+10, 130, 90, 20, font, "Login", login)));
     222   vctComponents.push_back(wndLogin->addComponent(new Button(920, 10, 80, 20, font, "Quit", quit)));
     223   vctComponents.push_back(wndLogin->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging)));
    224224
    225225   txtUsername = (Textbox*)wndLogin->getComponent(0);
     
    230230
    231231   wndRegister = new Window(0, 0, SCREEN_W, SCREEN_H);
    232    wndRegister->addComponent(new Textbox(516, 40, 100, 20, font));
    233    wndRegister->addComponent(new Textbox(516, 70, 100, 20, font));
    234    wndRegister->addComponent(new TextLabel(410, 40, 100, 20, font, "Username:", ALLEGRO_ALIGN_RIGHT));
    235    wndRegister->addComponent(new TextLabel(410, 70, 100, 20, font, "Password:", ALLEGRO_ALIGN_RIGHT));
    236    wndRegister->addComponent(new RadioButtonList(432, 100, "Pick a class", font));
    237    wndRegister->addComponent(new TextLabel((SCREEN_W-600)/2, 190, 600, 20, font, "", ALLEGRO_ALIGN_CENTRE));
    238    wndRegister->addComponent(new Button(SCREEN_W/2-100, 220, 90, 20, font, "Back", goToLoginScreen));
    239    wndRegister->addComponent(new Button(SCREEN_W/2+10, 220, 90, 20, font, "Submit", registerAccount));
    240    wndRegister->addComponent(new Button(920, 10, 80, 20, font, "Quit", quit));
    241    wndRegister->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging));
     232   vctComponents.push_back(wndRegister->addComponent(new Textbox(516, 40, 100, 20, font)));
     233   vctComponents.push_back(wndRegister->addComponent(new Textbox(516, 70, 100, 20, font)));
     234   vctComponents.push_back(wndRegister->addComponent(new TextLabel(410, 40, 100, 20, font, "Username:", ALLEGRO_ALIGN_RIGHT)));
     235   vctComponents.push_back(wndRegister->addComponent(new TextLabel(410, 70, 100, 20, font, "Password:", ALLEGRO_ALIGN_RIGHT)));
     236   vctComponents.push_back(wndRegister->addComponent(new RadioButtonList(432, 100, "Pick a class", font)));
     237   vctComponents.push_back(wndRegister->addComponent(new TextLabel((SCREEN_W-600)/2, 190, 600, 20, font, "", ALLEGRO_ALIGN_CENTRE)));
     238   vctComponents.push_back(wndRegister->addComponent(new Button(SCREEN_W/2-100, 220, 90, 20, font, "Back", goToLoginScreen)));
     239   vctComponents.push_back(wndRegister->addComponent(new Button(SCREEN_W/2+10, 220, 90, 20, font, "Submit", registerAccount)));
     240   vctComponents.push_back(wndRegister->addComponent(new Button(920, 10, 80, 20, font, "Quit", quit)));
     241   vctComponents.push_back(wndRegister->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging)));
    242242
    243243   txtUsernameRegister = (Textbox*)wndRegister->getComponent(0);
     
    252252   cout << "Created register screen" << endl;
    253253
     254   txtJoinGame = new Textbox(SCREEN_W*1/2+15+4, 40, 100, 20, font);
     255   txtCreateGame = new Textbox(SCREEN_W*3/4+4, 40, 100, 20, font);
     256
     257   vctComponents.push_back(txtJoinGame);
     258   vctComponents.push_back(txtCreateGame);
     259
    254260   wndLobby = new Window(0, 0, SCREEN_W, SCREEN_H);
    255    wndLobby->addComponent(new Button(920, 10, 80, 20, font, "Logout", logout));
    256    wndLobby->addComponent(new TextLabel(SCREEN_W*1/2+15-112, 40, 110, 20, font, "Game Name:", ALLEGRO_ALIGN_RIGHT));
    257    wndLobby->addComponent(new Textbox(SCREEN_W*1/2+15+4, 40, 100, 20, font));
    258    wndLobby->addComponent(new Button(SCREEN_W*1/2+15-100, 80, 200, 20, font, "Join Existing Game", joinGame));
    259    wndLobby->addComponent(new TextLabel(SCREEN_W*3/4-112, 40, 110, 20, font, "Game Name:", ALLEGRO_ALIGN_RIGHT));
    260    wndLobby->addComponent(new Textbox(SCREEN_W*3/4+4, 40, 100, 20, font));
    261    wndLobby->addComponent(new Button(SCREEN_W*3/4-100, 80, 200, 20, font, "Create New Game", createGame));
    262    wndLobby->addComponent(new Textbox(95, 40, 300, 20, font));
    263    wndLobby->addComponent(new Button(95, 70, 60, 20, font, "Send", sendChatMessage));
    264 
    265    txtJoinGame = (Textbox*)wndLobby->getComponent(2);
    266    txtCreateGame = (Textbox*)wndLobby->getComponent(5);
     261   vctComponents.push_back(wndLobby->addComponent(new Button(920, 10, 80, 20, font, "Logout", logout)));
     262   vctComponents.push_back(wndLobby->addComponent(new TextLabel(SCREEN_W*1/2+15-112, 40, 110, 20, font, "Game Name:", ALLEGRO_ALIGN_RIGHT)));
     263   wndLobby->addComponent(txtJoinGame);
     264   vctComponents.push_back(wndLobby->addComponent(new Button(SCREEN_W*1/2+15-100, 80, 200, 20, font, "Join Existing Game", joinGame)));
     265   vctComponents.push_back(wndLobby->addComponent(new TextLabel(SCREEN_W*3/4-112, 40, 110, 20, font, "Game Name:", ALLEGRO_ALIGN_RIGHT)));
     266   wndLobby->addComponent(txtCreateGame);
     267   vctComponents.push_back(wndLobby->addComponent(new Button(SCREEN_W*3/4-100, 80, 200, 20, font, "Create New Game", createGame)));
     268   vctComponents.push_back(wndLobby->addComponent(new Textbox(95, 40, 300, 20, font)));
     269   vctComponents.push_back(wndLobby->addComponent(new Button(95, 70, 60, 20, font, "Send", sendChatMessage)));
     270   vctComponents.push_back(wndLobby->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging)));
     271
     272
    267273   txtChat = (Textbox*)wndLobby->getComponent(7);
    268274
    269275   cout << "Created lobby screen" << endl;
    270276
    271    // this is the old game screen
    272    wndGame = new Window(0, 0, SCREEN_W, SCREEN_H);
    273    wndGame->addComponent(new Textbox(95, 40, 300, 20, font));
    274    wndGame->addComponent(new Button(95, 70, 60, 20, font, "Send", sendChatMessage));
    275    wndGame->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging));
    276    wndGame->addComponent(new Button(920, 10, 80, 20, font, "Logout", logout));
    277 
    278    wndGameDebug = new Window(0, 0, SCREEN_W, SCREEN_H);
    279    wndGameDebug->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging));
    280    wndGameDebug->addComponent(new Button(920, 10, 80, 20, font, "Logout", logout));
    281 
    282    cout << "Created game screen" << endl;
    283 
    284    // this is the new game screen, without a debug console
     277   wndLobbyDebug = new Window(0, 0, SCREEN_W, SCREEN_H);
     278   vctComponents.push_back(wndLobbyDebug->addComponent(new Button(920, 10, 80, 20, font, "Logout", logout)));
     279   vctComponents.push_back(wndLobbyDebug->addComponent(new TextLabel(SCREEN_W*1/2+15-112, 40, 110, 20, font, "Game Name:", ALLEGRO_ALIGN_RIGHT)));
     280   wndLobbyDebug->addComponent(txtJoinGame);
     281   vctComponents.push_back(wndLobbyDebug->addComponent(new Button(SCREEN_W*1/2+15-100, 80, 200, 20, font, "Join Existing Game", joinGame)));
     282   vctComponents.push_back(wndLobbyDebug->addComponent(new TextLabel(SCREEN_W*3/4-112, 40, 110, 20, font, "Game Name:", ALLEGRO_ALIGN_RIGHT)));
     283   wndLobbyDebug->addComponent(txtCreateGame);
     284   vctComponents.push_back(wndLobbyDebug->addComponent(new Button(SCREEN_W*3/4-100, 80, 200, 20, font, "Create New Game", createGame)));
     285   vctComponents.push_back(wndLobbyDebug->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging)));
     286
     287   cout << "Created debug lobby screen" << endl;
     288
    285289   wndNewGame = new Window(0, 0, SCREEN_W, SCREEN_H);
    286    wndNewGame->addComponent(new Button(880, 10, 120, 20, font, "Leave Game", leaveGame));
     290   vctComponents.push_back(wndNewGame->addComponent(new Button(880, 10, 120, 20, font, "Leave Game", leaveGame)));
    287291
    288292   cout << "Created new game screen" << endl;
    289293
    290294   wndGameSummary = new Window(0, 0, SCREEN_W, SCREEN_H);
    291    wndGameSummary->addComponent(new Button(840, 730, 160, 20, font, "Back to Lobby", closeGameSummary));
     295   vctComponents.push_back(wndGameSummary->addComponent(new Button(840, 730, 160, 20, font, "Back to Lobby", closeGameSummary)));
    292296
    293297   cout << "Created game summary screen" << endl;
     
    394398      }
    395399      else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
    396          if(wndCurrent == wndGame || wndCurrent == wndNewGame) {
     400         if(wndCurrent == wndNewGame) {
    397401            if (ev.mouse.button == 1) {   // left click
    398402               msgTo.type = MSG_TYPE_PLAYER_MOVE;
     
    455459         msgProcessor.resendUnackedMessages();
    456460
    457          if (debugging && wndCurrent == wndGame)
    458             wndGameDebug->draw(display);
     461         if (debugging && wndCurrent == wndLobby)
     462            wndLobbyDebug->draw(display);
    459463         else
    460464            wndCurrent->draw(display);
    461465
    462466         if (wndCurrent == wndLobby) {
    463             chatConsole.draw(font, al_map_rgb(255,255,255));
     467            if (!debugging)
     468               chatConsole.draw(font, al_map_rgb(255,255,255));
    464469
    465470            map<string, int>::iterator it;
     
    537542            }
    538543         }
    539          else if (wndCurrent == wndGame)
     544         else if (wndCurrent == wndGameSummary)
    540545         {
    541             if (!debugging)
    542                chatConsole.draw(font, al_map_rgb(255,255,255));
    543 
    544             al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Message:");
    545 
    546             ostringstream ossScoreBlue, ossScoreRed;
    547 
    548             ossScoreBlue << "Blue: " << game->getBlueScore() << endl;
    549             ossScoreRed << "Red: " << game->getRedScore() << endl;
    550 
    551             al_draw_text(font, al_map_rgb(0, 255, 0), 330, 80, ALLEGRO_ALIGN_LEFT, ossScoreBlue.str().c_str());
    552             al_draw_text(font, al_map_rgb(0, 255, 0), 515, 80, ALLEGRO_ALIGN_LEFT, ossScoreRed.str().c_str());
    553 
    554             // update players
    555             map<unsigned int, Player*>::iterator it;
    556             for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
    557             {
    558                it->second->updateTarget(mapPlayers);
    559             }
    560 
    561             for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
    562             {
    563                it->second->move(gameMap);    // ignore return value
    564             }
    565 
    566             // update projectile positions
    567             map<unsigned int, Projectile>::iterator it2;
    568             for (it2 = mapProjectiles.begin(); it2 != mapProjectiles.end(); it2++)
    569             {
    570                it2->second.move(mapPlayers);
    571             }
    572 
    573             GameRender::drawMap(gameMap);
    574             GameRender::drawPlayers(mapPlayers, font, curPlayerId);
    575 
    576             // draw projectiles
    577             for (it2 = mapProjectiles.begin(); it2 != mapProjectiles.end(); it2++)
    578             {
    579                Projectile proj = it2->second;
    580 
    581                FLOAT_POSITION target = mapPlayers[proj.target]->pos;
    582                float angle =  atan2(target.y-proj.pos.toFloat().y, target.x-proj.pos.toFloat().x);
    583 
    584                POSITION start, end;
    585                start.x = cos(angle)*15+proj.pos.x;
    586                start.y = sin(angle)*15+proj.pos.y;
    587                end.x = proj.pos.x;
    588                end.y = proj.pos.y;
    589 
    590                start = mapToScreen(start);
    591                end = mapToScreen(end);
    592 
    593                al_draw_line(start.x, start.y, end.x, end.y, al_map_rgb(0, 0, 0), 4);
    594             }
    595          }else if (wndCurrent == wndGameSummary) {
    596546            ostringstream ossBlueScore, ossRedScore;
    597547
     
    631581   shutdownWinSock();
    632582   
     583   // delete all components
     584   for (unsigned int x=0; x<vctComponents.size(); x++)
     585      delete vctComponents[x];
     586
    633587   delete wndLogin;
    634588   delete wndRegister;
    635589   delete wndLobby;
    636    delete wndGame;
    637    delete wndGameDebug;
     590   delete wndLobbyDebug;
     591   delete wndNewGame;
     592   delete wndGameSummary;
    638593
    639594   delete gameMap;
Note: See TracChangeset for help on using the changeset viewer.