Add coroutines on C++ side, Implement the test in C++
This commit is contained in:
2030
src/external/minicoro.h
vendored
Normal file
2030
src/external/minicoro.h
vendored
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,5 @@
|
|||||||
|
|
||||||
void GlobalCommand(Event event) {
|
void OnCommand(Event event) {
|
||||||
ProfileFunction();
|
ProfileFunction();
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|||||||
48
src/text_editor/coroutines.cpp
Normal file
48
src/text_editor/coroutines.cpp
Normal 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;
|
||||||
|
}
|
||||||
@@ -21,10 +21,10 @@ enum {
|
|||||||
#define EVENT_FIELDS \
|
#define EVENT_FIELDS \
|
||||||
X(EventKind, Int, kind) \
|
X(EventKind, Int, kind) \
|
||||||
X(SDL_Keycode, Int, key) \
|
X(SDL_Keycode, Int, key) \
|
||||||
X(int16_t, Int, xmouse) \
|
|
||||||
X(int16_t, Int, ymouse) \
|
|
||||||
X(int16_t, Int, xwindow) \
|
X(int16_t, Int, xwindow) \
|
||||||
X(int16_t, Int, ywindow) \
|
X(int16_t, Int, ywindow) \
|
||||||
|
X(int16_t, Int, xmouse) \
|
||||||
|
X(int16_t, Int, ymouse) \
|
||||||
X(uint8_t, Int, clicks) \
|
X(uint8_t, Int, clicks) \
|
||||||
X(uint8_t, Int, shift) \
|
X(uint8_t, Int, shift) \
|
||||||
X(uint8_t, Int, ctrl) \
|
X(uint8_t, Int, ctrl) \
|
||||||
|
|||||||
20
src/text_editor/test.cpp
Normal file
20
src/text_editor/test.cpp
Normal 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});
|
||||||
|
}
|
||||||
@@ -12,6 +12,8 @@
|
|||||||
#include "external/glad/glad.h"
|
#include "external/glad/glad.h"
|
||||||
#include "external/stb_truetype.h"
|
#include "external/stb_truetype.h"
|
||||||
#include "external/stb_truetype.c"
|
#include "external/stb_truetype.c"
|
||||||
|
#define MINICORO_IMPL
|
||||||
|
#include "external/minicoro.h"
|
||||||
#include "backup_font.cpp"
|
#include "backup_font.cpp"
|
||||||
|
|
||||||
SDL_Window *SDLWindow;
|
SDL_Window *SDLWindow;
|
||||||
@@ -49,6 +51,9 @@ int FullScreenPositionX, FullScreenPositionY;
|
|||||||
|
|
||||||
#include "window_draw.cpp"
|
#include "window_draw.cpp"
|
||||||
|
|
||||||
|
#include "coroutines.cpp"
|
||||||
|
#include "test.cpp"
|
||||||
|
|
||||||
void FillEventWithBasicData(Event *event) {
|
void FillEventWithBasicData(Event *event) {
|
||||||
SDL_Keymod mod = SDL_GetModState();
|
SDL_Keymod mod = SDL_GetModState();
|
||||||
event->shift = (mod & SDL_KMOD_SHIFT) != 0;
|
event->shift = (mod & SDL_KMOD_SHIFT) != 0;
|
||||||
@@ -163,9 +168,10 @@ void Update(Event event) {
|
|||||||
view->update_scroll = true;
|
view->update_scroll = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
GlobalCommand(event);
|
OnCommand(event);
|
||||||
For(Windows) if (it.o->is_title_bar) ReplaceTitleBarData(it.o);
|
For(Windows) if (it.o->is_title_bar) ReplaceTitleBarData(it.o);
|
||||||
UpdateProcesses();
|
UpdateProcesses();
|
||||||
|
UpdateCo(&event);
|
||||||
ReloadLuaConfigs();
|
ReloadLuaConfigs();
|
||||||
CallLuaOnUpdate(&event);
|
CallLuaOnUpdate(&event);
|
||||||
UpdateDebugBuffer();
|
UpdateDebugBuffer();
|
||||||
@@ -311,6 +317,9 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
InitLuaConfig();
|
InitLuaConfig();
|
||||||
|
|
||||||
|
StyleWaitForEvents = false;
|
||||||
|
AddCo(Test);
|
||||||
|
|
||||||
Serializer ser = {EventBuffer};
|
Serializer ser = {EventBuffer};
|
||||||
while (AppIsRunning) {
|
while (AppIsRunning) {
|
||||||
FrameID += 1;
|
FrameID += 1;
|
||||||
|
|||||||
Reference in New Issue
Block a user