游戏.消息管理器

来源:互联网 发布:adobe muse mac 网盘 编辑:程序博客网 时间:2024/05/17 09:36

/**************************************************
WinMain.cpp
堆栈式的函数管理器
http://blog.csdn.net/chinayaosir
**************************************************/

//0.头文件
#include <windows.h>
#include <stdio.h>

//1.管理器类定义
class cProcessManager
{
  // A structure that stores a function pointer and linked list
  typedef struct sProcess {
    void  (*Function)();
    sProcess *Next;
  } sProcess;

  protected:
    sProcess *m_ProcessParent; // The top state in the stack
                               // (the head of the stack)
  public:
    cProcessManager() { m_ProcessParent = NULL; }

    ~cProcessManager() {
      sProcess *ProcessPtr;
      // Remove all processes from the stack
      while((ProcessPtr = m_ProcessParent) != NULL) {
        m_ProcessParent = ProcessPtr->Next;
        delete ProcessPtr;
      }
    }

    // Add function on to the stack
    void Add(void (*Process)())
    {
      // Don't push a NULL value
      if(Process != NULL) {
        // Allocate a new process and push it on stack
        sProcess *ProcessPtr = new sProcess;
        ProcessPtr->Next = m_ProcessParent;
        m_ProcessParent = ProcessPtr;
        ProcessPtr->Function = Process;
      }
    }

    // Process all functions
    void Process()
    {
      sProcess *ProcessPtr = m_ProcessParent;

      while(ProcessPtr != NULL) {
        ProcessPtr->Function();
        ProcessPtr = ProcessPtr->Next;
      }
    }
};

//3.生成此类的一个对象g_ProcessManager
cProcessManager g_ProcessManager;

//4.WinMain主函数
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow);

// Macro to ease the use of MessageBox function
#define MB(s) MessageBox(NULL, s, s, MB_OK);

// Processfunction prototypes - must follow this prototype!
void Func1() { MB("function1"); }
void Func2() { MB("function2"); }
void Func3() { MB("function3"); }

int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow)
{
  g_ProcessManager.Add(Func1);//函数压入堆栈
  g_ProcessManager.Add(Func2);//函数压入堆栈
  g_ProcessManager.Add(Func3);//函数压入堆栈

  g_ProcessManager.Process();//依次遍历堆栈中的函数

  return 0;
}

原创粉丝点击