Add coroutines on C++ side, Implement the test in C++

This commit is contained in:
Krzosa Karol
2024-08-15 11:28:27 +02:00
parent 8f09e25710
commit 41a05fb08c
6 changed files with 2111 additions and 4 deletions

2030
src/external/minicoro.h vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +1,5 @@
void GlobalCommand(Event event) {
void OnCommand(Event event) {
ProfileFunction();
//

View File

@@ -0,0 +1,48 @@
typedef void CoroutineProc(mco_coro *co);
Array<mco_coro *> ActiveCoroutines;
mco_coro *AddCo(CoroutineProc *proc) {
mco_desc desc = mco_desc_init(proc, 0);
mco_coro *coro = NULL;
mco_result ok = mco_create(&coro, &desc);
if (ok != MCO_SUCCESS) {
ReportWarningf("failed to create coroutine %d", ok);
return NULL;
}
mco_resume(coro);
Add(&ActiveCoroutines, coro);
return coro;
}
void UpdateCo(Event *event) {
IterRemove(ActiveCoroutines) {
IterRemovePrepare(ActiveCoroutines);
mco_state status = mco_status(it);
if (status == MCO_DEAD) {
mco_destroy(it);
remove_item = true;
} else {
mco_push(it, &event, sizeof(Event *));
mco_result ok = mco_resume(it);
if (ok != MCO_SUCCESS) {
ReportWarningf("failed to resume coroutine %d", ok);
mco_destroy(it);
remove_item = true;
}
}
}
}
Event *Yield(mco_coro *co) {
mco_result ok = mco_yield(co);
Assert(ok == MCO_SUCCESS);
Event *event = NULL;
ok = mco_pop(co, &event, sizeof(Event *));
Assert(ok == MCO_SUCCESS);
return event;
}

View File

@@ -21,10 +21,10 @@ enum {
#define EVENT_FIELDS \
X(EventKind, Int, kind) \
X(SDL_Keycode, Int, key) \
X(int16_t, Int, xmouse) \
X(int16_t, Int, ymouse) \
X(int16_t, Int, xwindow) \
X(int16_t, Int, ywindow) \
X(int16_t, Int, xmouse) \
X(int16_t, Int, ymouse) \
X(uint8_t, Int, clicks) \
X(uint8_t, Int, shift) \
X(uint8_t, Int, ctrl) \

20
src/text_editor/test.cpp Normal file
View File

@@ -0,0 +1,20 @@
void Test(mco_coro *co) {
Add(&EventPlayback, {EVENT_KEY_PRESS, SDLK_DOWN, 1280, 720});
Add(&EventPlayback, {EVENT_KEY_PRESS, SDLK_DOWN, 1280, 720});
Add(&EventPlayback, {EVENT_KEY_PRESS, SDLK_DOWN, 1280, 720});
Add(&EventPlayback, {EVENT_KEY_PRESS, SDLK_DOWN, 1280, 720});
Add(&EventPlayback, {EVENT_KEY_PRESS, SDLK_DOWN, 1280, 720});
Add(&EventPlayback, {EVENT_KEY_PRESS, SDLK_DOWN, 1280, 720});
Add(&EventPlayback, {EVENT_KEY_PRESS, SDLK_DOWN, 1280, 720});
Add(&EventPlayback, {EVENT_KEY_PRESS, SDLK_Q, 1280, 720});
GetLast(EventPlayback)->ctrl = 1;
Add(&EventPlayback, {EVENT_KEY_PRESS, SDLK_PAGEDOWN, 1280, 720});
Add(&EventPlayback, {111});
for (Event *event = Yield(co); event->kind != 111; event = Yield(co)) {
}
BSet main = GetActiveMainSet();
Assert(main.buffer->name == "C:/Work/text_editor/src/text_editor/buffer_history.cpp");
Add(&EventPlayback, {EVENT_QUIT});
}

View File

@@ -12,6 +12,8 @@
#include "external/glad/glad.h"
#include "external/stb_truetype.h"
#include "external/stb_truetype.c"
#define MINICORO_IMPL
#include "external/minicoro.h"
#include "backup_font.cpp"
SDL_Window *SDLWindow;
@@ -49,6 +51,9 @@ int FullScreenPositionX, FullScreenPositionY;
#include "window_draw.cpp"
#include "coroutines.cpp"
#include "test.cpp"
void FillEventWithBasicData(Event *event) {
SDL_Keymod mod = SDL_GetModState();
event->shift = (mod & SDL_KMOD_SHIFT) != 0;
@@ -163,9 +168,10 @@ void Update(Event event) {
view->update_scroll = true;
}
GlobalCommand(event);
OnCommand(event);
For(Windows) if (it.o->is_title_bar) ReplaceTitleBarData(it.o);
UpdateProcesses();
UpdateCo(&event);
ReloadLuaConfigs();
CallLuaOnUpdate(&event);
UpdateDebugBuffer();
@@ -311,6 +317,9 @@ int main(int argc, char **argv)
InitLuaConfig();
StyleWaitForEvents = false;
AddCo(Test);
Serializer ser = {EventBuffer};
while (AppIsRunning) {
FrameID += 1;