diff --git a/build_file.cpp b/build_file.cpp index cce466c..4d66ee8 100644 --- a/build_file.cpp +++ b/build_file.cpp @@ -324,10 +324,24 @@ function ApplyRules(s) return nil end +Coroutines = {} +function AddCo(f) + local i = #Coroutines + 1 + Coroutines[i] = coroutine.create(f) + return Coroutines[i] +end + + function OnUpdate() - -- @todo: implement coroutine dispatch here - -- probably also want constatnly rerunning jobs with a set wake up time - -- @warning: make sure to not rewrite the thread list when rerunning this file + local new_co_list = {} + for key, co in pairs(Coroutines) do + local status = coroutine.status(co) + if status ~= "dead" then + coroutine.resume(co) + new_co_list[#new_co_list + 1] = co + end + end + Coroutines = new_co_list end )=="; diff --git a/src/text_editor/event.cpp b/src/text_editor/event.cpp index 6e9f9c0..a091227 100644 --- a/src/text_editor/event.cpp +++ b/src/text_editor/event.cpp @@ -40,6 +40,7 @@ struct Event { }; Array EventPlayback; +// :Event void Serialize(Serializer *s, Event *e) { SerializeBegin(s); Serialize(s, "kind", &e->kind); @@ -80,12 +81,3 @@ bool WaitForEvents = true; #define Mouse(x) (event.kind == EVENT_MOUSE_##x) #define MousePress() (Mouse(LEFT) || Mouse(RIGHT) || Mouse(MIDDLE)) #define MouseUp() (Mouse(LEFT_UP) || Mouse(RIGHT_UP) || Mouse(MIDDLE_UP)) - -void ParseEvents(Array *events, Buffer *buffer) { - Serializer s = {NULL, GetString(buffer), false}; - for (; s.string.len;) { - Event event = {}; - Serialize(&s, &event); - Add(events, event); - } -} \ No newline at end of file diff --git a/src/text_editor/generated.cpp b/src/text_editor/generated.cpp index a33b423..fdf8b24 100644 --- a/src/text_editor/generated.cpp +++ b/src/text_editor/generated.cpp @@ -213,10 +213,24 @@ function ApplyRules(s) return nil end +Coroutines = {} +function AddCo(f) + local i = #Coroutines + 1 + Coroutines[i] = coroutine.create(f) + return i +end + + function OnUpdate() - -- @todo: implement coroutine dispatch here - -- probably also want constatnly rerunning jobs with a set wake up time - -- @warning: make sure to not rewrite the thread list when rerunning this file + local new_co_list = {} + for key, co in pairs(Coroutines) do + local status = coroutine.status(co) + if status ~= "dead" then + coroutine.resume(co) + new_co_list[#new_co_list + 1] = co + end + end + Coroutines = new_co_list end diff --git a/src/text_editor/lua_api.cpp b/src/text_editor/lua_api.cpp index 69230b7..3dcc160 100644 --- a/src/text_editor/lua_api.cpp +++ b/src/text_editor/lua_api.cpp @@ -329,7 +329,7 @@ Vec2 GetVec2(const char *name) { return result; } -// #Play { {kind = 10, key = 1073741902, xmouse = 612, ymouse = 357, xwindow = 1280, ywindow = 720 } } +// :Event int Lua_Play(lua_State *L) { if (!lua_istable(L, -1)) luaL_error(LuaState, "expected a table of events"); diff --git a/src/text_editor/serializer.cpp b/src/text_editor/serializer.cpp index 52844fb..806c4c9 100644 --- a/src/text_editor/serializer.cpp +++ b/src/text_editor/serializer.cpp @@ -1,17 +1,9 @@ struct Serializer { - Buffer *buffer; // for writing - String16 string; // for reading - bool is_writing; + Buffer *buffer; // for writing }; void Serialize(Serializer *s, String name, Int *datum) { - if (s->is_writing) { - IKnowWhatImDoing_Appendf(s->buffer, "%.*s = %lld, ", FmtString(name), (long long)*datum); - } else { - s->string = Skip(s->string, name.len + 3); - *datum = SkipNumber(&s->string); - s->string = Skip(s->string, 2); - } + IKnowWhatImDoing_Appendf(s->buffer, "%.*s = %lld, ", FmtString(name), (long long)*datum); } void Serialize(Serializer *s, String name, uint32_t *datum) { @@ -33,48 +25,18 @@ void Serialize(Serializer *s, String name, int16_t *datum) { } void Serialize(Serializer *s, String name, Vec2 *datum) { - if (s->is_writing) { - IKnowWhatImDoing_Appendf(s->buffer, "%.*s = {%lld, %lld}, ", FmtString(name), (long long)datum->x, (long long)datum->y); - } else { - s->string = Skip(s->string, name.len + 3 + 1); - datum->x = (float)SkipNumber(&s->string); - s->string = Skip(s->string, 2); - datum->y = (float)SkipNumber(&s->string); - s->string = Skip(s->string, 1 + 2); - } + IKnowWhatImDoing_Appendf(s->buffer, "%.*s = {%lld, %lld}, ", FmtString(name), (long long)datum->x, (long long)datum->y); } void Serialize(Serializer *s, String name, char **text) { - if (s->is_writing) { - String str = *text; - IKnowWhatImDoing_Appendf(s->buffer, "%.*s = \"(%.*s)\", ", FmtString(name), FmtString(str)); - } else { - s->string = Skip(s->string, name.len + 3 + 2); - String16 str = SkipUntil(&s->string, L")\""); - if (str.len) { - Scratch scratch; - String s8 = ToString(scratch, str); - *text = Intern(&GlobalInternTable, s8).data; - } - s->string = Skip(s->string, 2 + 2); - } + String str = *text; + IKnowWhatImDoing_Appendf(s->buffer, "%.*s = \"(%.*s)\", ", FmtString(name), FmtString(str)); } void SerializeBegin(Serializer *s) { - if (s->is_writing) { - IKnowWhatImDoing_Appendf(s->buffer, "{"); - } else { - SkipWhitespace(&s->string); - Assert(s->string[0] == '{'); - s->string = Skip(s->string, 1); - } + IKnowWhatImDoing_Appendf(s->buffer, "{"); } void SerializeEnd(Serializer *s) { - if (s->is_writing) { - IKnowWhatImDoing_Appendf(s->buffer, "},\n"); - } else { - Assert(s->string[0] == '}'); - s->string = Skip(s->string, 3); - } + IKnowWhatImDoing_Appendf(s->buffer, "},\n"); } diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index 2bc7691..94c55da 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -301,7 +301,7 @@ int main(int argc, char **argv) WindowOpenBufferView(window, it); } - Serializer ser = {EventBuffer, {}, true}; + Serializer ser = {EventBuffer}; while (AppIsRunning) { FrameID += 1; diff --git a/src/text_editor/todo.txt b/src/text_editor/todo.txt index ba8b88b..7c87987 100644 --- a/src/text_editor/todo.txt +++ b/src/text_editor/todo.txt @@ -10,6 +10,7 @@ - Fuzzy search buffer which uses titlebar as query! - Check if file exists in ApplyRules otherwise return null - Gotos, jumping between views should preserve cursors +- ReportWarning should be signaled visibly but you should be able to do things! no focus switching - OnWindowCommand allow config user to overwrite the WindowCommand keybinding, introduce his own