home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 15 / Example 15.2 / lobby.h < prev    next >
Encoding:
C/C++ Source or Header  |  2006-07-17  |  2.3 KB  |  119 lines

  1. #ifndef _RTS_LOBBY_
  2. #define _RTS_LOBBY_
  3.  
  4. #include <vector>
  5. #include "network.h"
  6. #include "mouse.h"
  7.  
  8. extern NETWORK network;
  9.  
  10. //Textbox is used for text input
  11. struct TEXTBOX
  12. {
  13.     void Init(RECT r, int _maxLength)
  14.     {
  15.         active = false;
  16.         rect = r;
  17.         maxLength = _maxLength;
  18.         lastKey = -1;
  19.         lastKeydown = GetTickCount();
  20.         time = 0.0f;
  21.     }
  22.  
  23.     void Update(float deltaTime, MOUSE &mouse)
  24.     {
  25.         time += deltaTime;
  26.         if(time > 0.5f){time = 0.0f; blink = !blink;}
  27.  
  28.         //Active true/false
  29.         if(mouse.ClickLeft())
  30.             active = mouse.Over(rect);
  31.  
  32.         //Keyboard input
  33.         if(active && strlen(text.c_str()) < maxLength)
  34.             for(int i=32;i<126;i++)
  35.                 if(KEYDOWN(i))
  36.                 {
  37.                     if(GetTickCount() - lastKeydown > 200 || i != lastKey)
  38.                     {            
  39.                         lastKeydown = GetTickCount();
  40.                         lastKey = i;
  41.                         text += (char)i;
  42.                     }
  43.  
  44.                     break;
  45.                 }
  46.  
  47.         if(GetTickCount() - lastKeydown > 200)
  48.         {
  49.             if(KEYDOWN(VK_BACK) && strlen(text.c_str()) > 0)
  50.             {
  51.                 lastKeydown = GetTickCount();
  52.                 text.erase(text.end() - 1);
  53.             }
  54.             else if(KEYDOWN(VK_OEM_PERIOD))
  55.             {
  56.                 lastKeydown = GetTickCount();
  57.                 text += ".";
  58.             }
  59.             else if(KEYDOWN(VK_OEM_COMMA))
  60.             {
  61.                 lastKeydown = GetTickCount();
  62.                 text += ",";
  63.             }
  64.         }
  65.  
  66.         //Implement any other keyboard input you would like here...
  67.     }
  68.  
  69.     std::string GetText()
  70.     {
  71.         std::string str = text;
  72.         if(blink && active)str += "_";
  73.         return str;
  74.     }
  75.  
  76.     RECT rect;
  77.     bool active, blink;
  78.     int maxLength;
  79.     DWORD lastKeydown;
  80.     int lastKey;
  81.     float time;
  82.     std::string text;
  83. };
  84.  
  85. class LOBBY
  86. {
  87.     friend class APPLICATION;
  88.     public:
  89.         LOBBY();
  90.         ~LOBBY();
  91.  
  92.         void Init(IDirect3DDevice9* Dev);
  93.         void Update(float deltaTime, MOUSE &mouse);
  94.         void Draw(MOUSE &mouse);
  95.  
  96.         void DrawTextbox(TEXTBOX &tb);
  97.         bool TextButton(char text[], RECT r, bool border, MOUSE &mouse);
  98.         void Square(RECT r, DWORD col);        
  99.         bool Done(){return room == 10;}
  100.  
  101.         //public variables
  102.         int room;
  103.  
  104.     private:        
  105.         IDirect3DTexture9 *m_pBackGround;
  106.         IDirect3DTexture9 *m_pUI;
  107.         ID3DXFont *m_pMiniText;
  108.         ID3DXFont *m_pSmallText;
  109.         ID3DXFont *m_pCaption;
  110.         IDirect3DDevice9* m_pDevice;
  111.         ID3DXSprite *m_pSprite;
  112.         ID3DXLine *m_pLine;
  113.  
  114.         //Lobby Content
  115.         TEXTBOX m_nameTB, m_sessionTB, m_sendTB;
  116.         std::string m_error;
  117. };
  118.  
  119. #endif