]> git.lizzy.rs Git - nothing.git/blobdiff - src/game/level/level_editor/undo_history.c
(#1108) add development ding
[nothing.git] / src / game / level / level_editor / undo_history.c
index b0c48aa6432a529271485a26f537b2670070a1c4..5aba786d37b5593df6a3f43c11f4636255c7fb99 100644 (file)
@@ -1,49 +1,44 @@
 #include <stdlib.h>
+#include <stdio.h>
 
 #include <SDL.h>
 
 #include "system/nth_alloc.h"
 #include "system/lt.h"
-#include "dynarray.h"
 #include "system/stacktrace.h"
 #include "undo_history.h"
 
-struct UndoHistory
-{
-    Lt *lt;
-    Dynarray *actions;
-};
+typedef struct {
+    RevertAction revert;
+} Action;
 
-UndoHistory *create_undo_history(void)
+void undo_history_push(UndoHistory *undo_history,
+                       RevertAction revert,
+                       void *context_data,
+                       size_t context_data_size)
 {
-    Lt *lt = create_lt();
-
-    UndoHistory *undo_history = PUSH_LT(
-        lt,
-        nth_calloc(1, sizeof(UndoHistory)),
-        free);
+    trace_assert(undo_history);
 
-    undo_history->actions = PUSH_LT(
-        lt,
-        create_dynarray(sizeof(Action)),
-        destroy_dynarray);
+    Action action = {
+        .revert = revert,
+    };
 
-    return undo_history;
+    stack_push(&undo_history->actions, context_data, context_data_size);
+    stack_push(&undo_history->actions, &action, sizeof(action));
 }
 
-void destroy_undo_history(UndoHistory *undo_history)
+void undo_history_pop(UndoHistory *undo_history)
 {
     trace_assert(undo_history);
-    RETURN_LT0(undo_history->lt);
-}
 
-void undo_history_push(UndoHistory *undo_history, Action action)
-{
-    trace_assert(undo_history);
-    (void) action;
-}
+    if (stack_empty(&undo_history->actions) > 0) {
+        Action action = *(Action *)stack_top_element(&undo_history->actions);
+        stack_pop(&undo_history->actions);
 
-void undo_history_pop(UndoHistory *undo_history)
-{
-    trace_assert(undo_history);
+        size_t context_size = stack_top_size(&undo_history->actions);
+        void *context = stack_top_element(&undo_history->actions);
+
+        action.revert(context, context_size);
+        stack_pop(&undo_history->actions);
+    }
 }