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.1 / lobby.h < prev    next >
Encoding:
C/C++ Source or Header  |  2006-07-17  |  2.1 KB  |  110 lines

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