不多说,贴代码:

  1. #include "stdafx.h"
  2. #include <list>
  3. #include <thread>
  4. #include <chrono>
  5.  
  6. struct ICoroutine
  7. {
  8. virtual void reset(){}
  9. virtual bool move_next(int & r, float & fv) { return false; }
  10. virtual ~ICoroutine() {}
  11. public:
  12. float mWaitSeconds;
  13. };
  14.  
  15. template<typename T>
  16. struct _IGenerator : public ICoroutine
  17. {
  18. T* _stack;
  19. int _line;
  20. _IGenerator() :_stack(), _line(-) {}
  21. virtual void reset()
  22. {
  23. _line = -;
  24. }
  25. void _push() { T* n = new T; *n = *static_cast<T*>(this); _stack = n; }
  26. bool _pop() { if (!_stack) return false; T* t = _stack; *static_cast<T*>(this) = *_stack; t->_stack = ; delete t; return true; }
  27. ~_IGenerator() { while (_pop()); }
  28. };
  29.  
  30. #define $coroutine(NAME) struct NAME : public _IGenerator<NAME>
  31.  
  32. #define $begin virtual bool move_next(int& _rv, float& _rv2) { \
  33. if(_line < ) _line=; \
  34. $START: switch(_line) { case :;
  35.  
  36. #define $stop } _line = 0; if(_pop()) goto $START; return false; }
  37.  
  38. #define $restart(WITH) { _push(); _stack->_line = __LINE__; _line=0; WITH; goto $START; case __LINE__:; }
  39.  
  40. #define $yield(V) \
  41. do {\
  42. _line=__LINE__;\
  43. _rv = (V); return true; case __LINE__:;\
  44. } while ()
  45.  
  46. #define $yield_f(V, V2) \
  47. do {\
  48. _line=__LINE__;\
  49. _rv = (V); _rv2 = V2; return true; case __LINE__:;\
  50. } while ()
  51.  
  52. enum CoroutineState
  53. {
  54. CO_None,
  55. CO_WaitForNextUpdate,
  56. CO_WaitForSeconds,
  57. CO_Exit
  58. };
  59.  
  60. class GScheduler
  61. {
  62. protected:
  63. std::list<ICoroutine *> mActivityGList;
  64. std::list<ICoroutine *> mWaitingGList;
  65. std::list<ICoroutine *> mDeadingGList;
  66.  
  67. std::list<ICoroutine *> mGList;
  68.  
  69. void DestroyAllCoroutine()
  70. {
  71. std::list<ICoroutine *>::iterator iter;
  72. for (iter = mGList.begin(); iter != mGList.end(); iter++)
  73. {
  74. ICoroutine * co = *iter;
  75. delete co;
  76. }
  77. mGList.clear();
  78. mDeadingGList.clear();
  79. mWaitingGList.clear();
  80. mActivityGList.clear();
  81. }
  82. public:
  83. ~GScheduler()
  84. {
  85. DestroyAllCoroutine();
  86. }
  87.  
  88. template<typename T, typename T2>
  89. ICoroutine* StartCoroutine(T2 * tObj)
  90. {
  91. ICoroutine * gen = new T(tObj);
  92. mGList.push_back(gen);
  93. mActivityGList.push_back(gen);
  94. return gen;
  95. }
  96.  
  97. void StopCoroutine(ICoroutine *)
  98. {
  99. }
  100.  
  101. void RestartAllCoroutine()
  102. {
  103. std::list<ICoroutine *>::iterator iter;
  104. for (iter = mGList.begin(); iter != mGList.end(); iter++)
  105. {
  106. ICoroutine * co = *iter;
  107. co->reset();
  108. mActivityGList.push_back(co);
  109. }
  110. }
  111.  
  112. void StopAllCoroutine()
  113. {
  114. mDeadingGList.clear();
  115. mWaitingGList.clear();
  116. mActivityGList.clear();
  117. }
  118. void UpdateAllCoroutine(float dt)
  119. {
  120. std::list<ICoroutine *>::iterator iter, next;
  121. for (iter = mWaitingGList.begin(); iter != mWaitingGList.end();iter = next)
  122. {
  123. next = iter; next++;
  124.  
  125. ICoroutine * co = *iter;
  126. co->mWaitSeconds -= dt;
  127. if (co->mWaitSeconds <= )
  128. {
  129. next = mWaitingGList.erase(iter);
  130. mActivityGList.push_back(co);
  131. }
  132. }
  133.  
  134. for (iter = mActivityGList.begin(); iter != mActivityGList.end(); iter = next)
  135. {
  136. next = iter; next++;
  137.  
  138. ICoroutine * co = *iter;
  139.  
  140. bool isDeading = false;
  141.  
  142. int retValue = ;
  143. float retFValue = ;
  144. if (!co->move_next(retValue, retFValue))
  145. {
  146. isDeading = true;
  147. }
  148. CoroutineState state = (CoroutineState)retValue;
  149. if (state == CO_Exit)
  150. {
  151. isDeading = true;
  152. }
  153. else if (state == CO_WaitForNextUpdate)
  154. {
  155.  
  156. }
  157. else if (state == CO_WaitForSeconds)
  158. {
  159. float seconds = retFValue;
  160. co->mWaitSeconds = seconds;
  161. next = mActivityGList.erase(iter);
  162. mWaitingGList.push_back(co);
  163. }
  164.  
  165. if (isDeading)
  166. {
  167. next = mActivityGList.erase(iter);
  168. mDeadingGList.push_back(co);
  169. }
  170. }
  171. }
  172. };
  173. //**********************************************************************************************************
    //以下是测试程序:
  174. class TestCoroutine1;
  175. class TestCoroutine2;
  176. class UIMain : public GScheduler
  177. {
  178. public:
  179. UIMain()
  180. {
  181. }
  182. void Enable()
  183. {
  184. RestartAllCoroutine();
  185. }
  186. void Disable()
  187. {
  188. StopAllCoroutine();
  189. }
  190.  
  191. void Start()
  192. {
  193. ICoroutine *testCo = StartCoroutine<TestCoroutine1, UIMain>(this);
  194. StartCoroutine<TestCoroutine2, UIMain>(this);
  195. }
  196.  
  197. void Update(float dt)
  198. {
  199. UpdateAllCoroutine(dt);
  200. }
  201.  
  202. void Test1(int v)
  203. {
  204. printf("Test1, v = %d\n", v);
  205. }
  206. void Test2(int v)
  207. {
  208. printf("Test2, v = %d\n", v);
  209. }
  210. };
  211.  
  212. $coroutine(TestCoroutine1)
  213. {
  214. UIMain* n;
  215. TestCoroutine1(UIMain* root = ) : n(root) {}
  216. int i = ;
  217. $begin
  218. for (i = ; i < ; i++)
  219. {
  220. n->Test1(i);
  221. $yield(CO_WaitForNextUpdate);
  222. }
  223. $yield(CO_Exit);
  224. n->Test1();
  225. $stop
  226. };
  227.  
  228. $coroutine(TestCoroutine2)
  229. {
  230. UIMain* n;
  231. TestCoroutine2(UIMain* root = ) : n(root) {}
  232. int i = ;
  233. $begin
  234. for (i = ; i < ; i++)
  235. {
  236. n->Test2(i);
  237. $yield_f(CO_WaitForSeconds, 0.5f);
  238. }
  239. $yield(CO_Exit);
  240. n->Test1();
  241. $stop
  242. };
  243.  
  244. int _tmain(int argc, _TCHAR* argv[])
  245. {
  246. UIMain uiMain;
  247.  
  248. uiMain.Enable();
  249. uiMain.Start();
  250.  
  251. float dt = 0.05f;
  252. float time = ;
  253. while (true)
  254. {
  255. uiMain.Update(dt);
  256. std::this_thread::sleep_for(std::chrono::milliseconds((int)(dt*)));
  257. time += dt;
  258. if (time > ) //10秒后重开协程
  259. {
  260. uiMain.Disable();
  261. uiMain.Enable(); //重新开始协程
  262. time = ;
  263. }
  264. }
  265. return ;
  266. }

跨平台c++ Coroutine,仿unity3d实现的更多相关文章

  1. Coroutine协同程序介绍(Unity3D开发之三)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢! 原文地址: http://www.cocos2dev.com/?p=496 Coroutine在Uni ...

  2. 在unity3d中使用opencv

    1.首先下载opencv2.4.10,解压缩后放在合适的地方,然后根据自己的电脑(32位或64位)选择X86或X64,我的是32位,将“opencv存放路径\build\x86\vc12\bin”加入 ...

  3. Electron-Vite2-MacUI桌面管理框架|electron13+vue3.x仿mac桌面UI

    基于vue3.0.11+electron13仿制macOS桌面UI管理系统ElectronVue3MacUI. 前段时间有分享一个vue3结合electron12开发后台管理系统项目.今天要分享的是最 ...

  4. 漫谈C#编程语言在游戏领域的应用

    0x00 前言 随着微软越来越开放,C#也变得越来越吸引人们的眼球.而在游戏行业中,C#也开始慢慢地获得了关注.这不, 网易绝代双娇手游团队已经全面使用.Net Core支持前后端统一C#开发,跨平台 ...

  5. Index

    我主要在研究.NET/C# 实现 PC IMERP 和 Android IMERP ,目的在解决企业通信中遇到的各类自动化问题   分布式缓存框架: Microsoft Velocity:微软自家分布 ...

  6. GitHub上整理的一些工具

    技术站点 Hacker News:非常棒的针对编程的链接聚合网站 Programming reddit:同上 MSDN:微软相关的官方技术集中地,主要是文档类 infoq:企业级应用,关注软件开发领域 ...

  7. 推荐书籍 -《移动App测试的22条军规》

    在今天的博文中,博主希望给大家分享一本博主同事黄勇的最新利作:<移动App测试的22条军规>.黄勇是ThoughtWorks资深敏捷QA和咨询师.对于我来说,和黄勇在一起的工作的这个项目, ...

  8. GitHub上整理的一些工具[转载]

    Source:http://segmentfault.com/q/1010000002404545 技术站点 Hacker News:非常棒的针对编程的链接聚合网站 Programming reddi ...

  9. GitHub 开源工具整理

    技术站点 Hacker News:非常棒的针对编程的链接聚合网站 Programming reddit:同上 MSDN:微软相关的官方技术集中地,主要是文档类 infoq:企业级应用,关注软件开发领域 ...

随机推荐

  1. (转)用Eclipse进行C++开发时Bianry not found的问题解决

    本文转载自:http://blog.csdn.net/baimafujinji/article/details/49722399 由于Visual Studio体积过于庞大,很多人选择在Eclipse ...

  2. bzoj 3920: Yuuna的礼物

    Description 转眼就要到Karin的生日了!Yuuna她们想为她准备生日礼物!现在有许多礼物被排列成了一个一维序列,每个礼物都有一个价值.Yuuna对这个序列十分感兴趣.因此,你需要多次回答 ...

  3. Dell vsotro 14 3000系列从win10重装win7

    1. F2启动进入新的BIOS界面,首先Disable Secure Boot,然后把UEFI改为Legeacy模式,当然是改不回来的,不知道为什么: 2. 插入U盘(老毛桃+UEFI启动镜像): 3 ...

  4. TCP/IP四层模型和OSI七层模型的概念

    转:http://blog.csdn.net/superjunjin/article/details/7841099/ TCP/IP四层模型 TCP/IP是一组协议的代名词,它还包括许多协议,组成了T ...

  5. [系统开发] FileMaker进销存系统

    一.简介 这是我用 FileMaker 编写的进销存系统: FileMaker 是一种在欧美流行的桌面型数据库:它使用非常方便,功能也很强大,用户可以在它上面开发自己的系统: 开发时间:2008年 二 ...

  6. eclipse下编译hadoop源代码(转)

    hadoop是一个分布式存储和分布式计算的框架.在日常使用hadoop时,我们会发现hadoop不能完全满足我们的需要,我们可能需要修改hadoop的源代码并重新编译.打包. 下面将详细描述如何从sv ...

  7. DBA_Oracle冷备份案例脚本本法(案例)

    2014-08-10 Created By BaoXinjian

  8. c#复习整理

    一.基本语法 1.数据类型 整数类型:int.long 浮点类型:float.double.decimal 布尔类型:bool 字符串类型:string 2.类型转换 int a; double b ...

  9. ZOJ 3606 Lazy Salesgirl 浙江省第九届省赛

    Lazy Salesgirl Time Limit: 5 Seconds      Memory Limit: 65536 KB Kochiya Sanae is a lazy girl who ma ...

  10. image和字节流之间的相互转换

    //将图片转化为长二进制 public Byte[] SetImgToByte(string imgPath) { FileStream file = new FileStream(imgPath, ...