-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathplug.cpp
More file actions
178 lines (149 loc) · 3.4 KB
/
plug.cpp
File metadata and controls
178 lines (149 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <coroutine>
#include <vector>
#include <memory>
#include <raylib.h>
#include <raymath.h>
#include "env.h"
#include "interpolators.h"
#include "plug.h"
#define SQUARE_SIZE 20
#define AWAIT(task) do { \
auto __t = (task); \
while (!__t.done()) { __t(); co_yield 0; } \
} while (0)
struct promise;
struct Task : std::coroutine_handle<promise>
{
using promise_type = ::promise;
};
struct promise
{
Task get_return_object() { return {Task::from_promise(*this)}; }
std::suspend_always initial_suspend() noexcept { return {}; }
std::suspend_always final_suspend() noexcept { return {}; }
void return_void() {}
void unhandled_exception() {}
std::suspend_always yield_value(int /* dummy */)
{
return {};
}
};
#define FONT_SIZE 68
typedef struct {
size_t size;
Task task;
Vector2 position1;
Vector2 position2;
Vector2 position3;
} Plug;
static Plug *p;
static void load_assets(void)
{
}
static void unload_assets(void)
{
}
static Task move(Vector2 &position, Vector2 direction, size_t n)
{
for (size_t i = 0; i < n; ++i)
{
position = Vector2Add(position, direction);
co_yield 0;
}
}
static Task move2(Vector2 &position, Vector2 direction1, size_t n1, Vector2 direction2, size_t n2)
{
AWAIT(move(position, direction1, n1));
printf("move 1 done\n");
AWAIT(move(position, direction2, n2));
}
static Task combine(std::vector<Task> tasks)
{
while (true)
{
bool ran = false;
for (auto &task : tasks)
{
if (!task.done())
{
ran = true;
task();
}
}
if (!ran)
break;
co_yield 0;
}
}
extern "C" {
#define PLUG(name, ret, ...) ret name(__VA_ARGS__);
LIST_OF_PLUGS
#undef PLUG
void plug_reset(void)
{
p->position1 = {0, 0};
p->position2 = {300, 0};
p->position3 = {100, 300};
p->task = combine({ move(p->position1, {0.1, 0}, 300), move(p->position2, {0, 0.1}, 300), move2(p->position3, {0, 0.5}, 100, {0.5, 0}, 200) });
}
void plug_init(void)
{
p = (Plug*)malloc(sizeof(*p));
assert(p != NULL);
memset(p, 0, sizeof(*p));
p->size = sizeof(*p);
load_assets();
plug_reset();
}
void *plug_pre_reload(void)
{
unload_assets();
return p;
}
void plug_post_reload(void *state)
{
p = (Plug*)state;
if (p->size < sizeof(*p)) {
TraceLog(LOG_INFO, "Migrating plug state schema %zu bytes -> %zu bytes", p->size, sizeof(*p));
p = (Plug*)realloc(p, sizeof(*p));
p->size = sizeof(*p);
}
load_assets();
}
void plug_update(Env env)
{
if (!p->task.done())
p->task();
Color background_color = ColorFromHSV(0, 0, 0.05);
Color foreground_color = ColorFromHSV(0, 0, 0.95);
ClearBackground(background_color);
Rectangle boundary = {
.x = p->position1.x,
.y = p->position1.y,
.width = SQUARE_SIZE,
.height = SQUARE_SIZE,
};
DrawRectangleRec(boundary, foreground_color);
boundary = {
.x = p->position2.x,
.y = p->position2.y,
.width = SQUARE_SIZE,
.height = SQUARE_SIZE,
};
DrawRectangleRec(boundary, foreground_color);
boundary = {
.x = p->position3.x,
.y = p->position3.y,
.width = SQUARE_SIZE,
.height = SQUARE_SIZE,
};
DrawRectangleRec(boundary, foreground_color);
}
bool plug_finished(void)
{
return p->task.done();
}
}