Created
June 23, 2014 23:43
-
-
Save TiliSleepStealer/df49ffbc9792d1af6ec7 to your computer and use it in GitHub Desktop.
C++ Coroutines kinda like Unity but shitier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// c++ Coroutines by [email protected] | |
// using modified coroutines.hpp from boost | |
#include <stdio.h> | |
#include <string.h> | |
#include "SDL/SDL.h" | |
#include "CoroutineTest.h" | |
#include <boost/foreach.hpp> | |
namespace { // anonymous namespace | |
class TestCoro : coroutine { | |
// all state should be put here | |
int& a; | |
public: | |
TestCoro(int& outsideVar) : coroutine(), a(outsideVar) {} | |
void operator()() { | |
if (is_complete()) { | |
return; | |
} | |
reenter (this) // really want to get rid of this line, its so ugly... | |
{ | |
// normal yield skips until next frame | |
a = 0; | |
printf("some code before yield a=%i\n", a); | |
yield; | |
a += 1; | |
printf("some other code after yield a=%i\n", a); | |
yield; | |
a += 1; | |
printf("wait for time test %i\n", SDL_GetTicks()); | |
printf("a=%i\n", a); | |
WaitForSeconds(2.0f); | |
a += 1; | |
printf("waited 2 seconds %i\n", SDL_GetTicks()); | |
printf("a=%i\n", a); | |
WaitForSeconds(4.0f); | |
a += 1; | |
printf("waited 4 seconds %i\n", SDL_GetTicks()); | |
printf("a=%i\n", a); | |
// this yield until someting is done, handy for tweens or ai for example | |
while (a < 40) { | |
a++; | |
printf("a=%i\n", a); | |
yield; // normal yield skips until next frame | |
} | |
printf("coroutine done!\n"); | |
} | |
} | |
}; | |
} | |
void Update() { | |
} | |
void Render() { | |
} | |
int main(int argc, char *argv[]) { | |
int testVar = -1; | |
// we want the value testVar to get to 40 using a coroutine | |
StartCoroutine(TestCoro(testVar)); // start a new coroutine and stop at the first yield | |
// game loop | |
while (testVar != 40) { | |
Update(); | |
Render(); | |
// delete finished coroutines | |
coroutine::currentCoroutines.erase(remove_if(coroutine::currentCoroutines.begin(), coroutine::currentCoroutines.end(), DeleteCoroutinePredicate), coroutine::currentCoroutines.end()); | |
// loop trough all existing coroutines and give them a go. | |
BOOST_FOREACH(boost::shared_ptr<coroutine> coro, coroutine::currentCoroutines) { | |
(*coro)(); | |
} | |
} | |
printf("finished main loop!\n"); | |
return 0; | |
} | |
output: | |
some code before yield a=0 | |
some other code after yield a=1 | |
wait for time test 122 | |
a=2 | |
waited 2 seconds 2131 | |
a=3 | |
waited 4 seconds 6148 | |
a=4 | |
a=5 | |
a=6 | |
a=7 | |
a=8 | |
a=9 | |
a=10 | |
a=11 | |
a=12 | |
a=13 | |
a=14 | |
a=15 | |
a=16 | |
a=17 | |
a=18 | |
a=19 | |
a=20 | |
a=21 | |
a=22 | |
a=23 | |
a=24 | |
a=25 | |
a=26 | |
a=27 | |
a=28 | |
a=29 | |
a=30 | |
a=31 | |
a=32 | |
a=33 | |
a=34 | |
a=35 | |
a=36 | |
a=37 | |
a=38 | |
a=39 | |
a=40 | |
coroutine done! | |
finished main loop! | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Coroutine like Unity using C++ and boost coroutine2:
https://github.com/exawon/CoroBehaviour