首页 技术 正文
技术 2022年11月16日
0 收藏 959 点赞 2,179 浏览 3976 个字

不多说,贴代码:

 #include "stdafx.h"
#include <list>
#include <thread>
#include <chrono> struct ICoroutine
{
virtual void reset(){}
virtual bool move_next(int & r, float & fv) { return false; }
virtual ~ICoroutine() {}
public:
float mWaitSeconds;
}; template<typename T>
struct _IGenerator : public ICoroutine
{
T* _stack;
int _line;
_IGenerator() :_stack(), _line(-) {}
virtual void reset()
{
_line = -;
}
void _push() { T* n = new T; *n = *static_cast<T*>(this); _stack = n; }
bool _pop() { if (!_stack) return false; T* t = _stack; *static_cast<T*>(this) = *_stack; t->_stack = ; delete t; return true; }
~_IGenerator() { while (_pop()); }
}; #define $coroutine(NAME) struct NAME : public _IGenerator<NAME> #define $begin virtual bool move_next(int& _rv, float& _rv2) { \
if(_line < ) _line=; \
$START: switch(_line) { case :; #define $stop } _line = 0; if(_pop()) goto $START; return false; } #define $restart(WITH) { _push(); _stack->_line = __LINE__; _line=0; WITH; goto $START; case __LINE__:; } #define $yield(V) \
do {\
_line=__LINE__;\
_rv = (V); return true; case __LINE__:;\
} while () #define $yield_f(V, V2) \
do {\
_line=__LINE__;\
_rv = (V); _rv2 = V2; return true; case __LINE__:;\
} while () enum CoroutineState
{
CO_None,
CO_WaitForNextUpdate,
CO_WaitForSeconds,
CO_Exit
}; class GScheduler
{
protected:
std::list<ICoroutine *> mActivityGList;
std::list<ICoroutine *> mWaitingGList;
std::list<ICoroutine *> mDeadingGList; std::list<ICoroutine *> mGList; void DestroyAllCoroutine()
{
std::list<ICoroutine *>::iterator iter;
for (iter = mGList.begin(); iter != mGList.end(); iter++)
{
ICoroutine * co = *iter;
delete co;
}
mGList.clear();
mDeadingGList.clear();
mWaitingGList.clear();
mActivityGList.clear();
}
public:
~GScheduler()
{
DestroyAllCoroutine();
} template<typename T, typename T2>
ICoroutine* StartCoroutine(T2 * tObj)
{
ICoroutine * gen = new T(tObj);
mGList.push_back(gen);
mActivityGList.push_back(gen);
return gen;
} void StopCoroutine(ICoroutine *)
{
} void RestartAllCoroutine()
{
std::list<ICoroutine *>::iterator iter;
for (iter = mGList.begin(); iter != mGList.end(); iter++)
{
ICoroutine * co = *iter;
co->reset();
mActivityGList.push_back(co);
}
} void StopAllCoroutine()
{
mDeadingGList.clear();
mWaitingGList.clear();
mActivityGList.clear();
}
void UpdateAllCoroutine(float dt)
{
std::list<ICoroutine *>::iterator iter, next;
for (iter = mWaitingGList.begin(); iter != mWaitingGList.end();iter = next)
{
next = iter; next++; ICoroutine * co = *iter;
co->mWaitSeconds -= dt;
if (co->mWaitSeconds <= )
{
next = mWaitingGList.erase(iter);
mActivityGList.push_back(co);
}
} for (iter = mActivityGList.begin(); iter != mActivityGList.end(); iter = next)
{
next = iter; next++; ICoroutine * co = *iter; bool isDeading = false; int retValue = ;
float retFValue = ;
if (!co->move_next(retValue, retFValue))
{
isDeading = true;
}
CoroutineState state = (CoroutineState)retValue;
if (state == CO_Exit)
{
isDeading = true;
}
else if (state == CO_WaitForNextUpdate)
{ }
else if (state == CO_WaitForSeconds)
{
float seconds = retFValue;
co->mWaitSeconds = seconds;
next = mActivityGList.erase(iter);
mWaitingGList.push_back(co);
} if (isDeading)
{
next = mActivityGList.erase(iter);
mDeadingGList.push_back(co);
}
}
}
};
//**********************************************************************************************************
//以下是测试程序:
class TestCoroutine1;
class TestCoroutine2;
class UIMain : public GScheduler
{
public:
UIMain()
{
}
void Enable()
{
RestartAllCoroutine();
}
void Disable()
{
StopAllCoroutine();
} void Start()
{
ICoroutine *testCo = StartCoroutine<TestCoroutine1, UIMain>(this);
StartCoroutine<TestCoroutine2, UIMain>(this);
} void Update(float dt)
{
UpdateAllCoroutine(dt);
} void Test1(int v)
{
printf("Test1, v = %d\n", v);
}
void Test2(int v)
{
printf("Test2, v = %d\n", v);
}
}; $coroutine(TestCoroutine1)
{
UIMain* n;
TestCoroutine1(UIMain* root = ) : n(root) {}
int i = ;
$begin
for (i = ; i < ; i++)
{
n->Test1(i);
$yield(CO_WaitForNextUpdate);
}
$yield(CO_Exit);
n->Test1();
$stop
}; $coroutine(TestCoroutine2)
{
UIMain* n;
TestCoroutine2(UIMain* root = ) : n(root) {}
int i = ;
$begin
for (i = ; i < ; i++)
{
n->Test2(i);
$yield_f(CO_WaitForSeconds, 0.5f);
}
$yield(CO_Exit);
n->Test1();
$stop
}; int _tmain(int argc, _TCHAR* argv[])
{
UIMain uiMain; uiMain.Enable();
uiMain.Start(); float dt = 0.05f;
float time = ;
while (true)
{
uiMain.Update(dt);
std::this_thread::sleep_for(std::chrono::milliseconds((int)(dt*)));
time += dt;
if (time > ) //10秒后重开协程
{
uiMain.Disable();
uiMain.Enable(); //重新开始协程
time = ;
}
}
return ;
}
下一篇: Editplus 注册码
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,489
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,290