c++ 简单模拟实现 游戏定时开启任务
2014-07-19 16:48
866 查看
// game_task.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <Windows.h> #include <iostream> #include <sys/timeb.h> #include <time.h> #include <process.h> #include <map> #include <assert.h> using namespace std; // time_test.cpp : 定义控制台应用程序的入口点。 // /* author:郑金玮 time:2014/07/19 desc:impl task manager */ ////////////////////////////////////////////////////////////////////////// class CThread { public: CThread(); virtual ~CThread(); HANDLE getHandle() const; bool wait(); bool wait(DWORD timeoutMillsecs); void start(); DWORD terminate(DWORD exitcode=0); private: virtual int run() = 0; static unsigned int __stdcall threadFunction(void *threadParam); void resetHandle(); private: CThread(const CThread&); CThread& operator = (const CThread&); private: HANDLE m_handle; }; CThread::CThread():m_handle(NULL) { } CThread::~CThread() { resetHandle(); } HANDLE CThread::getHandle() const { return m_handle; } void CThread::start() { if (m_handle == NULL) { unsigned int _threadID = 0; m_handle = (HANDLE)::_beginthreadex(0, 0, threadFunction, (void*)this, 0, &_threadID ); assert(m_handle!=NULL); } } bool CThread::wait() { return wait(INFINITE); } bool CThread::wait(DWORD timeoutMillsecs) { bool _isOK = false; if (NULL == m_handle) { return true; } DWORD _result = ::WaitForSingleObject(m_handle,timeoutMillsecs); if (_result == WAIT_TIMEOUT) { _isOK = false; } if (_result == WAIT_OBJECT_0) { _isOK = true; } resetHandle(); return _isOK; } DWORD CThread::terminate(DWORD exitcode) { if (m_handle != NULL && ::TerminateThread(m_handle,exitcode)) { m_handle = NULL; return ERROR_SUCCESS; } else { return ::GetLastError(); } return 0; } unsigned int __stdcall CThread::threadFunction(void *threadParam) { CThread *pThread = (CThread*)threadParam; int _ret = pThread->run(); pThread->resetHandle(); return _ret; } void CThread::resetHandle() { if (m_handle) { ::CloseHandle(m_handle); m_handle =NULL; } } ////////////////////////////////////////////////////////////////////////// class CRealDateTime { public: CRealDateTime():m_dif(0),m_dif2(0) {} ~CRealDateTime(){} private: struct tm *m_t,m_t2; time_t m_tb; __int64 m_saveedTime; public: void calcTime() //计算当前时间到指定时间之间的时间差(秒级) { this->update(); m_saveedTime = m_dif2-m_dif; } __int64 getCalcTime() { return m_saveedTime; } void updateCalcTime(__int64 newTime) { m_saveedTime = newTime; } void setTime(int year,int month,int day,int hour,int minute,int sec) // set specify time { m_t2.tm_year = year - 1900; //tm结构记录年为实际-1900 m_t2.tm_mon = month - 1; m_t2.tm_mday = day; m_t2.tm_hour = hour; m_t2.tm_min = minute; m_t2.tm_sec = sec; m_dif2=mktime(&m_t2); } void setTime(int hour,int minute,int sec) { ::time(&m_tb); struct tm *_t; _t= ::localtime(&m_tb); m_t2.tm_year = _t->tm_year; m_t2.tm_mon = _t->tm_mon ; m_t2.tm_mday = _t->tm_mday; m_t2.tm_hour = hour; m_t2.tm_min = minute; m_t2.tm_sec = sec; m_dif2=mktime(&m_t2); } void update() //get current time { ::time(&m_tb); m_t= ::localtime(&m_tb); m_dif=mktime(m_t); } __int64 gettime() { return m_dif; } void updateBegain(__int64 newBegain) { m_dif = newBegain; } __int64 get_now2Begain(__int64 t) { return t-m_dif; } private: __int64 m_dif,m_dif2; }; class CTimerManager { public: __int64 getDiffTime(CRealDateTime *last,CRealDateTime *now) { __int64 __diff = now->gettime() - last->gettime(); return __diff; } public: static CTimerManager* instance() { static CTimerManager __inst; return &__inst; } }; #define TIMERMANAGER() CTimerManager::instance() //////////////////////////////////////////////// class CAction { public: CAction(){} virtual ~CAction(){} virtual void process() = 0; }; class CActionLogin : public CAction { public: CActionLogin(){} ~CActionLogin(){} public: void process(){ cout<<"login process...."<<endl; } }; class CActionGivePrize :public CAction { public: CActionGivePrize(){} ~CActionGivePrize(){} public: void process(){ cout<<"givePrize process...."<<endl; } }; ////////////////////////////////////////// class CTask { public: void init(unsigned int nTaskID,CAction& action,CRealDateTime& objTime) { m_pAction = &action; m_nTaskID = nTaskID; m_TrrigleTime = &objTime; m_bState = true; } bool getState(){ return m_bState ;} void setState(bool state) { m_bState = state; } unsigned int getID(){ return m_nTaskID;} bool checkTime(__int64 _nowEclps,__int64 _now) { __int64 _calctime = this->m_TrrigleTime->getCalcTime(); if (_calctime > 0) { _calctime -=_nowEclps; if(_calctime<=0) { _calctime=0; } this->m_TrrigleTime->updateBegain(_now); this->m_TrrigleTime->updateCalcTime(_calctime); return false; } setState(false);//修改任务状态 return true; } void processevent() { m_pAction->process(); } CRealDateTime* getTimer(){ return m_TrrigleTime; } private: CAction* m_pAction; unsigned int m_nTaskID; CRealDateTime *m_TrrigleTime; bool m_bState; }; ////////////////////////////////////////////////////////////////////////// class CTaskManager :public CThread { CTaskManager(){ start(); } ~CTaskManager(){ terminate(0); } public: static CTaskManager* instance() { static CTaskManager __instance; return &__instance; } public: bool add(CTask &task) { unsigned int _nTaskID = task.getID(); m_typeMapTask::iterator _itor = m_mapTask.find(_nTaskID); m_typeMapTask::iterator _end = m_mapTask.end(); if (_itor != _end) { return false; } m_mapTask.insert(make_pair<unsigned int,CTask*>(_nTaskID,&task)); return true; } bool close(unsigned int nTaskID) { m_typeMapTask::iterator _itor = m_mapTask.find(nTaskID); m_typeMapTask::iterator _end = m_mapTask.end(); if (_itor != _end) { m_mapTask.erase(_itor); return true; } return false; } bool close(CTask &task) { unsigned int _nTaskID = task.getID(); m_typeMapTask::iterator _itor = m_mapTask.find(_nTaskID); m_typeMapTask::iterator _end = m_mapTask.end(); if (_itor != _end) { m_mapTask.erase(_itor); return true; } return false; } bool triggle(CTask &task)//定时器触发 { //判断逻辑 CRealDateTime _now; _now.update(); //保存当前时间 __int64 _nowDif = _now.gettime(); //获取当前时间的时间戳 __int64 _Eclpse= task.getTimer()->get_now2Begain(_nowDif);//获取当前时间到任务开始加入时的时间差 if( (task.getState()) && (task.checkTime(_Eclpse,_nowDif))) { task.processevent(); return true; } return false; } bool triggle(unsigned int nTaskID) //主动触发 { m_typeMapTask::iterator _itor = m_mapTask.find(nTaskID); m_typeMapTask::iterator _end = m_mapTask.end(); if (_itor != _end) { return triggle(*(_itor->second)); } return false; } int run()//定时器在这里处理 { while(true) { m_typeMapTask::iterator _begain = m_mapTask.begin(); m_typeMapTask::iterator _end = m_mapTask.end(); m_typeMapTask::iterator _itor; for (_itor = _begain;_itor != _end ;_itor++) { triggle(*(_itor->second)); } } return 0; } private: typedef map<unsigned int,CTask*> m_typeMapTask; m_typeMapTask m_mapTask; }; #define TASKMANAGER() CTaskManager::instance() #define SAFE_RELEASE(point) \ { \ delete point; \ point = 0; \ } int _tmain(int argc, _TCHAR* argv[]) { CRealDateTime timer1; timer1.setTime(16,44,20); timer1.calcTime(); CAction *paction = new CActionLogin(); CTask task; task.init(1,*paction,timer1); TASKMANAGER()->add(task); ////////////////////////////////////////////////////////////////////////// CRealDateTime timer2; timer2.setTime(2014,7,19,16,44,30); timer2.calcTime(); CAction *paction2 = new CActionGivePrize(); CTask task2; task2.init(2,*paction2,timer2); TASKMANAGER()->add(task2); while (true); SAFE_RELEASE(paction); SAFE_RELEASE(paction2); return 0; }
相关文章推荐
- JAVA定时任务的简单实现
- 【Android】基于andengine游戏引擎的简单游戏实现(模拟控制器、tiledmap地图)
- Java实现简单定时任务
- 用简单的C语言实现多任务轮流切换(模拟操作系统线程机制)
- 我们的游戏世界(背包【仓库】,交易,任务,简单经济系统,装备)实现(基于仙剑demo聊聊游戏世界)第四篇谈谈背包
- 简单介绍Spring+Quartz实现定时任务的配置方法
- java实现job功能,定时开启任务
- 我们的游戏世界(背包【仓库】,交易,任务,简单经济系统,装备)实现(基于仙剑demo聊聊游戏世界)第一篇谈谈交易
- php简单定时执行任务的实现方法
- Quartz定时任务简单实现
- C++实现简单的分牌游戏
- Quartz + Spring 简单实现定时任务
- Java定时任务的简单实现
- 我们的游戏世界(背包【仓库】,交易,任务,简单经济系统,装备)实现(基于仙剑demo聊聊游戏世界)第二篇谈谈任务
- Quartz + Spring 简单实现定时任务
- 游戏中按概率播放某个音效简单c++实现
- 简单模拟实现c++智能指针-指针移交控制权
- 通过游标简单实现循环 定时任务
- SpringMVC整合Quartz实现定时任务---简单的例子
- 我们的游戏世界(背包【仓库】,交易,任务,简单经济系统,装备)实现(基于仙剑demo聊聊游戏世界)第三篇谈谈装备