SelectSrv.cpp
上传用户:cydong117
上传日期:2009-11-10
资源大小:638k
文件大小:9k
源码类别:

模拟服务器

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #define POS_SERVERLIST_BORDER_X 50
  3. #define POS_SERVERLIST_BORDER_Y 0
  4. #define SERVERLIST_BORDER_WIDTH 151
  5. #define POS_TOP_SERVER_BTN_X 0
  6. #define POS_TOP_SERVER_BTN_Y 10
  7. #define SERVER_BTN_WIDTH 100
  8. #define SERVER_BTN_HEIGHT 20
  9. #define SERVER_BTN_GAP 10
  10. #define COUNT_BUTTON_PER_COLUME 8
  11. #define SERVER_BTN_TEXTCOLOR_NORMAL RGB(10, 10, 10) // 绢滴款 祸
  12. #define SERVER_BTN_TEXTCOLOR_FOCUS RGB(0, 255, 255) // YELLOW
  13. #define SERVER_BTN_TEXTCOLOR_CLICK RGB(0,0,0) // COLORKEY
  14. #define SERVER_BTN_BKCOLOR_NORMAL RGB(125,125,125) // 
  15. #define SERVER_BTN_BKCOLOR_FOCUS RGB(95,95,95) //
  16. #define SERVER_BTN_BKCOLOR_CLICK RGB(125,125,0) //  YELLOW
  17. #define _LEFT_BORDER 20
  18. #define _TOP_BORDER 60
  19. #define _RIGHT_BORDER 120
  20. #define _BOTTOM_BORDER 310
  21. CSelectSrv::CSelectSrv():CBMWnd()
  22. {
  23. m_ppxSrvBtn = NULL;
  24. D3DVECTOR vNorm(0, 0, -1);
  25.     m_avBillBoard[0]  = D3DVERTEX(D3DVECTOR(-0.5f, 0.5f, 0), vNorm, 0, 0);
  26.     m_avBillBoard[1]  = D3DVERTEX(D3DVECTOR(-0.5f,-0.5f, 0), vNorm, 0, 1);
  27.     m_avBillBoard[2]  = D3DVERTEX(D3DVECTOR( 0.5f, 0.5f, 0), vNorm, 1, 0);
  28.     m_avBillBoard[3]  = D3DVERTEX(D3DVECTOR( 0.5f,-0.5f, 0), vNorm, 1, 1);
  29. }
  30. CSelectSrv::~CSelectSrv()
  31. {
  32. INT i;
  33. for(i = 0 ; i <m_nSrvCount; i++)
  34. delete m_ppxSrvBtn[i];
  35. delete[] m_ppxSrvBtn;
  36. }
  37. VOID CSelectSrv::Create(CWHWilImageData* pxImage,CPDLList<ServerList>* pxSList)
  38. {
  39. INT i,j;
  40. INT nTop,nLeft;
  41. RECT rcTemp;
  42. // Buttons
  43. m_nSrvCount = pxSList->GetCounter() + 1;
  44. m_ppxSrvBtn = new _PSERVERBUTTON[m_nSrvCount];
  45. m_pxImage = pxImage;
  46. pxSList->MoveCurrentToTop();
  47. for(i = 0 ; i <= ((m_nSrvCount - 1) / COUNT_BUTTON_PER_COLUME) ; i++)
  48. { for(j = i * COUNT_BUTTON_PER_COLUME; (j < m_nSrvCount && j < (COUNT_BUTTON_PER_COLUME * (i + 1))) ; j ++)
  49. { m_ppxSrvBtn[j] = new _SERVERBUTTON;
  50. strcpy(m_ppxSrvBtn[j]->szName, pxSList->GetCurrentData()->Name);
  51. strcpy(m_ppxSrvBtn[j]->szCaption, pxSList->GetCurrentData()->Caption);
  52. nLeft = POS_TOP_SERVER_BTN_X + i * 100;
  53. nTop  = POS_TOP_SERVER_BTN_Y + (j - i * COUNT_BUTTON_PER_COLUME) * (SERVER_BTN_HEIGHT+SERVER_BTN_GAP);
  54. SetRect(&rcTemp, nLeft, nTop, nLeft + SERVER_BTN_WIDTH, nTop + SERVER_BTN_HEIGHT);
  55. m_ppxSrvBtn[j]->rcBtn = rcTemp;
  56. m_ppxSrvBtn[j]->dwTxtColor = SERVER_BTN_TEXTCOLOR_NORMAL;
  57. m_ppxSrvBtn[j]->dwBkColor = SERVER_BTN_BKCOLOR_NORMAL;
  58. m_ppxSrvBtn[j]->nAlpha = 125;
  59. pxSList->MoveNextNode();
  60. }
  61. }
  62. SetRect(&m_rcWnd,_LEFT_BORDER,_TOP_BORDER,_LEFT_BORDER+SERVER_BTN_WIDTH*(m_nSrvCount/COUNT_BUTTON_PER_COLUME+1),(m_nSrvCount/COUNT_BUTTON_PER_COLUME) > 0 ? _BOTTOM_BORDER : POS_TOP_SERVER_BTN_Y + (m_nSrvCount * COUNT_BUTTON_PER_COLUME) * (SERVER_BTN_HEIGHT+SERVER_BTN_GAP));
  63. }
  64. HRESULT CSelectSrv::OnKeyDown(WPARAM wParam, LPARAM lParam)
  65. {
  66. return 0;
  67. }
  68. HRESULT CSelectSrv::OnButtonDown(WPARAM wParam, LPARAM lParam)
  69. {
  70. INT i;
  71. RECT tRect;
  72. m_fIsButtonDown = TRUE;
  73. for( i = 0 ;  i < m_nSrvCount  ; i++)
  74. { SetRect(&tRect,m_rcWnd.left + m_ppxSrvBtn[i]->rcBtn.left, m_rcWnd.top + m_ppxSrvBtn[i]->rcBtn.top, m_rcWnd.left +m_ppxSrvBtn[i]->rcBtn.right ,m_rcWnd.top +m_ppxSrvBtn[i]->rcBtn.bottom);
  75. if(IsInRect(tRect,LOWORD(lParam), HIWORD(lParam)))
  76. { m_ppxSrvBtn[i]->dwTxtColor = SERVER_BTN_TEXTCOLOR_CLICK; // COLOR(BLACK)
  77. m_ppxSrvBtn[i]->dwBkColor = SERVER_BTN_BKCOLOR_CLICK;
  78. m_ppxSrvBtn[i]->nAlpha = 125;
  79. }
  80. }
  81. return 0;
  82. }
  83. HRESULT CSelectSrv::OnButtonDown(POINT ptMouse)
  84. {
  85. m_fIsButtonDown = TRUE;
  86. return 0;
  87. }
  88. HRESULT CSelectSrv::OnButtonUp(WPARAM wParam, LPARAM lParam)
  89. {
  90. INT i;
  91. RECT tRect;
  92. m_fIsButtonDown = FALSE;
  93. for( i = 0 ;  i < m_nSrvCount  ; i++)
  94. { SetRect(&tRect,m_rcWnd.left + m_ppxSrvBtn[i]->rcBtn.left, m_rcWnd.top + m_ppxSrvBtn[i]->rcBtn.top, m_rcWnd.left +m_ppxSrvBtn[i]->rcBtn.right ,m_rcWnd.top +m_ppxSrvBtn[i]->rcBtn.bottom);
  95. if(IsInRect(tRect,LOWORD(lParam), HIWORD(lParam)))
  96. {
  97. g_xClientSocket.OnSelectServer(m_ppxSrvBtn[i]->szName);
  98. }
  99. m_ppxSrvBtn[i]->dwTxtColor = SERVER_BTN_TEXTCOLOR_NORMAL; // COLOR(YELLOW)
  100. m_ppxSrvBtn[i]->dwBkColor = SERVER_BTN_BKCOLOR_NORMAL;
  101. m_ppxSrvBtn[i]->nAlpha = 125;
  102. }
  103. return 0;
  104. }
  105. HRESULT CSelectSrv::OnButtonUp(POINT ptMouse)
  106. {
  107. return 0;
  108. }
  109. LRESULT CSelectSrv::OnMouseMove(WPARAM wParam, LPARAM lParam)
  110. {
  111. INT i;
  112. RECT tRect;
  113. if(!m_fIsButtonDown)
  114. for( i = 0 ;  i < m_nSrvCount  ; i++)
  115. { SetRect(&tRect,m_rcWnd.left + m_ppxSrvBtn[i]->rcBtn.left, m_rcWnd.top + m_ppxSrvBtn[i]->rcBtn.top, m_rcWnd.left +m_ppxSrvBtn[i]->rcBtn.right ,m_rcWnd.top +m_ppxSrvBtn[i]->rcBtn.bottom);
  116. if(IsInRect(tRect,LOWORD(lParam), HIWORD(lParam)))
  117. { m_ppxSrvBtn[i]->dwTxtColor = SERVER_BTN_TEXTCOLOR_FOCUS; // COLOR(YELLOW)
  118. m_ppxSrvBtn[i]->dwBkColor = SERVER_BTN_BKCOLOR_FOCUS;
  119. m_ppxSrvBtn[i]->nAlpha = 125;
  120. }
  121. else
  122. { m_ppxSrvBtn[i]->dwTxtColor = SERVER_BTN_TEXTCOLOR_NORMAL; // COLOR(WHITE)
  123. m_ppxSrvBtn[i]->dwBkColor = SERVER_BTN_BKCOLOR_NORMAL;
  124. m_ppxSrvBtn[i]->nAlpha = 125;
  125. }
  126. }
  127. return 0;
  128. }
  129. VOID CSelectSrv::Render(INT nLoopTime)
  130. {
  131. INT i,j;
  132. RECT tRect;
  133. COLORREF nBkColor;
  134. if(m_fIsActive)
  135. {
  136. // Write Texts;
  137. Draw3DBorder(m_rcWnd,RGB(125,125,125),125);
  138. for(i = 0 ; i <= ((m_nSrvCount - 1) / COUNT_BUTTON_PER_COLUME) ; i++)
  139. {
  140. for(j = i * COUNT_BUTTON_PER_COLUME; (j < m_nSrvCount && j < (COUNT_BUTTON_PER_COLUME * (i + 1))) ; j ++)
  141. { nBkColor = m_ppxSrvBtn[j]->dwBkColor;
  142. SetRect(&tRect,m_rcWnd.left + m_ppxSrvBtn[j]->rcBtn.left, m_rcWnd.top + m_ppxSrvBtn[j]->rcBtn.top, m_rcWnd.left +m_ppxSrvBtn[j]->rcBtn.right ,m_rcWnd.top +m_ppxSrvBtn[j]->rcBtn.bottom);
  143. if(nBkColor!=RGB(125,125,125))Draw3DBorder(tRect, nBkColor,m_ppxSrvBtn[j]->nAlpha);
  144. g_xMainWnd.PutsHan(NULL, tRect,
  145. m_ppxSrvBtn[j]->dwTxtColor, RGB(0,0,0), m_ppxSrvBtn[j]->szCaption,g_xMainWnd.CreateGameFont("疙炼",12 ));
  146. }
  147. }
  148. }
  149. }
  150. VOID CSelectSrv::Draw3DBorder(RECT rcWnd,COLORREF dwColor,INT nAlpha)
  151. {
  152. if ( g_xMainWnd.Get3DDevice() )
  153. {
  154. D3DVECTOR vTrans;
  155. D3DMATRIX matTrans;
  156. D3DMATRIX matScale;
  157. D3DMATRIX matWorld;
  158. D3DMATRIX matView;
  159. D3DMATRIX matProj;
  160. D3DMATRIX matWorldOriginal;
  161. D3DMATERIAL7 mtrl;
  162. if( SUCCEEDED(g_xMainWnd.Get3DDevice()->BeginScene()))
  163. {
  164. g_xMainWnd.Get3DDevice()->GetTransform(D3DTRANSFORMSTATE_WORLD, &matWorldOriginal);
  165. ZeroMemory(&matView, sizeof(D3DMATRIX));
  166.   D3DVECTOR vEyePt    = D3DVECTOR(0, 0, -(float)((g_xMainWnd.m_rcWindow.bottom - g_xMainWnd.m_rcWindow.top)/2));
  167. D3DVECTOR vLookatPt = D3DVECTOR(0, 0, 0);
  168. D3DVECTOR vUpVec    = D3DVECTOR(0, 1, 0);
  169. D3DUtil_SetViewMatrix(matView, vEyePt, vLookatPt, vUpVec);
  170. D3DUtil_SetProjectionMatrix(matProj, g_PI/2, float(240.0f/320.0f), -1.0f, 1.0f);
  171. g_xMainWnd.Get3DDevice()->SetTransform(D3DTRANSFORMSTATE_VIEW,       &matView);
  172. g_xMainWnd.Get3DDevice()->SetTransform(D3DTRANSFORMSTATE_PROJECTION, &matProj);
  173. vTrans.x = (FLOAT)rcWnd.left - 320.0f + (rcWnd.right  - rcWnd.left)/2;
  174. vTrans.y = (FLOAT)-rcWnd.top + 240.0f - (rcWnd.bottom - rcWnd.top )/2;
  175. vTrans.z = 0;
  176. D3DUtil_SetTranslateMatrix(matTrans, vTrans);
  177. D3DUtil_SetScaleMatrix(matScale, (FLOAT)(rcWnd.right - rcWnd.left), (FLOAT)(rcWnd.bottom - rcWnd.top), 0.0f);
  178. D3DMath_MatrixMultiply(matWorld, matScale, matTrans);
  179. g_xMainWnd.Get3DDevice()->SetTransform(D3DTRANSFORMSTATE_WORLD, &matWorld);
  180. // 烙矫
  181. D3DUtil_InitMaterial(mtrl, 
  182.  (FLOAT)GetRValue(dwColor)/255.0f,
  183.  (FLOAT)GetGValue(dwColor)/255.0f, 
  184.  (FLOAT)GetBValue(dwColor)/255.0f);
  185. mtrl.diffuse.a = nAlpha/255.0f; // alpha value
  186. g_xMainWnd.Get3DDevice()->SetMaterial(&mtrl);
  187. g_xMainWnd.Get3DDevice()->SetRenderState( D3DRENDERSTATE_ALPHABLENDENABLE, TRUE );
  188. g_xMainWnd.Get3DDevice()->SetRenderState( D3DRENDERSTATE_COLORKEYENABLE, TRUE);
  189. g_xMainWnd.Get3DDevice()->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
  190. g_xMainWnd.Get3DDevice()->SetTextureStageState(0, D3DTSS_ALPHAOP,   D3DTOP_MODULATE);
  191. g_xMainWnd.Get3DDevice()->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
  192. g_xMainWnd.Get3DDevice()->SetRenderState(D3DRENDERSTATE_SRCBLEND, D3DBLEND_ONE);
  193. g_xMainWnd.Get3DDevice()->SetRenderState(D3DRENDERSTATE_DESTBLEND, D3DBLEND_INVSRCALPHA);
  194. g_xMainWnd.Get3DDevice()->SetTexture(0, NULL);
  195. g_xMainWnd.Get3DDevice()->DrawPrimitive(D3DPT_TRIANGLESTRIP, D3DFVF_VERTEX, m_avBillBoard, 4, NULL);
  196. g_xMainWnd.Get3DDevice()->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, FALSE);
  197. g_xMainWnd.Get3DDevice()->SetRenderState(D3DRENDERSTATE_COLORKEYENABLE, FALSE);
  198. g_xMainWnd.Get3DDevice()->SetRenderState(D3DRENDERSTATE_SRCBLEND , D3DBLEND_ONE);
  199. g_xMainWnd.Get3DDevice()->SetRenderState(D3DRENDERSTATE_DESTBLEND, D3DBLEND_ZERO);
  200. g_xMainWnd.Get3DDevice()->SetTransform(D3DTRANSFORMSTATE_WORLD, &matWorldOriginal);
  201. g_xMainWnd.Get3DDevice()->EndScene();
  202. }
  203. }
  204. }