KMessage.cpp
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:3k
源码类别:

模拟服务器

开发平台:

C/C++

  1. //---------------------------------------------------------------------------
  2. // Sword3 Engine (c) 1999-2000 by Kingsoft
  3. //
  4. // File: KMessage.cpp
  5. // Date: 2000.08.08
  6. // Code: WangWei(Daphnis)
  7. // Desc: Message Functions
  8. //---------------------------------------------------------------------------
  9. #include "KWin32.h"
  10. #include "KDebug.h"
  11. #include "KList.h"
  12. #include "KNode.h"
  13. #include "KMemManager.h"
  14. #include "KMessage.h"
  15. //---------------------------------------------------------------------------
  16. static KList m_MsgList;
  17. static int m_nMsgNum = 0;
  18. //---------------------------------------------------------------------------
  19. #define MAX_MSG 100
  20. //---------------------------------------------------------------------------
  21. class KMsgNode : public KNode
  22. {
  23. public:
  24. int m_nType; // 消息类型
  25. int m_nParam[3]; // 消息参数
  26. };
  27. //---------------------------------------------------------------------------
  28. // 函数: Send Message
  29. // 功能: 发送消息
  30. // 参数: Type 消息类型
  31. // Param1 参数1
  32. // Param2 参数2
  33. // Param3 参数3
  34. // 返回: void
  35. //---------------------------------------------------------------------------
  36. ENGINE_API void g_SendMessage(int nType, int nP0, int nP1, int nP2)
  37. {
  38. if (m_nMsgNum >= MAX_MSG)
  39. return;
  40. m_nMsgNum++;
  41. KMsgNode* pNode = (KMsgNode *) g_MemManager.Calloc(sizeof(KMsgNode));
  42. pNode->m_pNext = NULL;
  43. pNode->m_pPrev = NULL;
  44. pNode->m_nType = nType;
  45. pNode->m_nParam[0] = nP0;
  46. pNode->m_nParam[1] = nP1;
  47. pNode->m_nParam[2] = nP2;
  48. m_MsgList.AddHead(pNode);
  49. }
  50. //---------------------------------------------------------------------------
  51. // 函数: Peek Message
  52. // 功能: 窥探下一条消息
  53. // 参数: pMsg 消息指针
  54. // 返回: TRUE-成功 FALSE-失败
  55. //---------------------------------------------------------------------------
  56. ENGINE_API BOOL g_PeekMessage(KMessage* pMsg)
  57. {
  58. KMsgNode* pNode = (KMsgNode *)m_MsgList.GetHead();
  59. if (pNode)
  60. {
  61. pMsg->nType = pNode->m_nType;
  62. pMsg->nParam[0] = pNode->m_nParam[0];
  63. pMsg->nParam[1] = pNode->m_nParam[1];
  64. pMsg->nParam[2] = pNode->m_nParam[2];
  65. return TRUE;
  66. }
  67. pMsg->nType = TM_NONE;
  68. return FALSE;
  69. }
  70. //---------------------------------------------------------------------------
  71. // 函数: Get Message
  72. // 功能: 取得下一条消息
  73. // 参数: pMsg 消息指针
  74. // 返回: TRUE-成功 FALSE-失败
  75. //---------------------------------------------------------------------------
  76. ENGINE_API BOOL g_GetMessage(KMessage* pMsg)
  77. {
  78. KMsgNode* pNode = (KMsgNode *)m_MsgList.GetHead();
  79. if (pNode)
  80. {
  81. pMsg->nType = pNode->m_nType;
  82. pMsg->nParam[0] = pNode->m_nParam[0];
  83. pMsg->nParam[1] = pNode->m_nParam[1];
  84. pMsg->nParam[2] = pNode->m_nParam[2];
  85. pNode->Remove();
  86. g_MemManager.Free(pNode);
  87. m_nMsgNum--;
  88. return TRUE;
  89. }
  90. pMsg->nType = TM_NONE;
  91. return FALSE;
  92. }
  93. //---------------------------------------------------------------------------
  94. // 函数: Clear
  95. // 功能: 清空消息队列
  96. // 参数: void
  97. // 返回: void
  98. //---------------------------------------------------------------------------
  99. ENGINE_API void g_ClearMessage()
  100. {
  101. KMessage Msg;
  102. while (g_GetMessage(&Msg)){};
  103. }
  104. //---------------------------------------------------------------------------