]> git.lizzy.rs Git - nothing.git/blob - src/game/level/level_editor/undo_history.c
7a51aaba9e62a87d5643bd631e5ca252b1e9b03d
[nothing.git] / src / game / level / level_editor / undo_history.c
1 #include <stdlib.h>
2 #include <stdio.h>
3
4 #include <SDL.h>
5
6 #include "system/nth_alloc.h"
7 #include "system/lt.h"
8 #include "system/stacktrace.h"
9 #include "undo_history.h"
10
11 typedef struct {
12     RevertAction revert;
13 } HistoryAction;
14
15 void undo_history_push(UndoHistory *undo_history,
16                        RevertAction revert,
17                        void *context_data,
18                        size_t context_data_size)
19 {
20     trace_assert(undo_history);
21
22     HistoryAction action = {
23         .revert = revert,
24     };
25
26     stack_push(&undo_history->actions, context_data, context_data_size);
27     stack_push(&undo_history->actions, &action, sizeof(action));
28 }
29
30 void undo_history_pop(UndoHistory *undo_history)
31 {
32     trace_assert(undo_history);
33
34     if (stack_empty(&undo_history->actions) > 0) {
35         HistoryAction action = *(HistoryAction *)stack_top_element(&undo_history->actions);
36         stack_pop(&undo_history->actions);
37
38         size_t context_size = stack_top_size(&undo_history->actions);
39         void *context = stack_top_element(&undo_history->actions);
40
41         action.revert(context, context_size);
42         stack_pop(&undo_history->actions);
43     }
44 }