]> git.lizzy.rs Git - nothing.git/commitdiff
(#1030) Remove Context size limit
authorrexim <reximkut@gmail.com>
Sat, 31 Aug 2019 17:58:32 +0000 (00:58 +0700)
committerrexim <reximkut@gmail.com>
Sat, 31 Aug 2019 17:58:32 +0000 (00:58 +0700)
src/game/level/level_editor/background_layer.c
src/game/level/level_editor/player_layer.c
src/game/level/level_editor/point_layer.c
src/game/level/level_editor/rect_layer.c
src/game/level/level_editor/undo_history.c
src/game/level/level_editor/undo_history.h

index 7df40b14586cb95ff66a1e9e22b70db658ed6ce1..6c62141877f7186284e9c959934b2193e4c36ffc 100644 (file)
@@ -49,13 +49,14 @@ int background_layer_render(BackgroundLayer *layer,
 }
 
 static
-void background_undo_color(void *layer, Context context)
+void background_undo_color(void *layer, void *context, size_t context_size)
 {
     trace_assert(layer);
-    BackgroundLayer *background_layer = layer;
+    trace_assert(context);
+    trace_assert(sizeof(Color) == context_size);
 
-    trace_assert(sizeof(Color) < CONTEXT_SIZE);
-    Color *color = (Color *)context.data;
+    BackgroundLayer *background_layer = layer;
+    Color *color = context;
 
     background_layer->color_picker = create_color_picker_from_rgba(*color);
 }
index fb9c9a214122cf8d1b80c976bcbc8fc48037046a..50c3cfda40019ad03f18deeda69a15e6563e4c5a 100644 (file)
@@ -26,12 +26,14 @@ UndoContext player_layer_create_undo_context(PlayerLayer *player_layer)
 }
 
 static
-void player_layer_undo(void *layer, Context context)
+void player_layer_undo(void *layer, void *context, size_t context_size)
 {
     trace_assert(layer);
-    PlayerLayer *player_layer = layer;
+    trace_assert(context);
+    trace_assert(sizeof(UndoContext) == context_size);
 
-    UndoContext *undo_context = (UndoContext *)context.data;
+    PlayerLayer *player_layer = layer;
+    UndoContext *undo_context = context;
 
     player_layer->position = undo_context->position;
     player_layer->color_picker = create_color_picker_from_rgba(undo_context->color);
index 5261f849d4a915cb28b0105a64c42c6f97bb1f72..0d04ef040857e612a687e455091ddcc1021d31cb 100644 (file)
@@ -65,12 +65,14 @@ UndoContext point_layer_create_undo_context(PointLayer *point_layer,
 }
 
 static
-void point_layer_undo(void *layer, Context context)
+void point_layer_undo(void *layer, void *context, size_t context_size)
 {
     trace_assert(layer);
-    PointLayer *point_layer = layer;
+    trace_assert(context);
+    trace_assert(sizeof(UndoContext) == context_size);
 
-    UndoContext *undo_context = (UndoContext *)context.data;
+    PointLayer *point_layer = layer;
+    UndoContext *undo_context = context;
 
     switch (undo_context->type) {
     case UNDO_ADD: {
index 475d0e17c40198e39b92d73bd12c6b10b69c366a..66495398d34071160c83d093c0b7493aac02d342 100644 (file)
@@ -72,13 +72,14 @@ UndoContext rect_layer_create_undo_context(RectLayer *rect_layer, size_t index,
 }
 
 static
-void rect_layer_undo(void *layer, Context context)
+void rect_layer_undo(void *layer, void *context, size_t context_size)
 {
     trace_assert(layer);
-    RectLayer *rect_layer = layer;
+    trace_assert(context);
+    trace_assert(sizeof(UndoContext) == context_size);
 
-    trace_assert(sizeof(UndoContext) < CONTEXT_SIZE);
-    UndoContext *undo_context = (UndoContext *)context.data;
+    RectLayer *rect_layer = layer;
+    UndoContext *undo_context = context;
 
     switch (undo_context->type) {
     case UNDO_ADD: {
index 32f9c6bd1ecf820d99e2893a6295f65928ddba08..7f3f17ae90863f05655d938369599202ff3a2e77 100644 (file)
@@ -9,41 +9,16 @@
 #include "undo_history.h"
 #include "stack.h"
 
-struct UndoHistory
-{
+struct UndoHistory {
     Lt *lt;
     Stack actions;
 };
 
-
-
 typedef struct {
-    void *layer;
-    Context context;
     RevertAction revert;
+    void *layer;
 } Action;
 
-static inline
-Action create_action(void *layer, RevertAction revert,
-                     void *context_data,
-                     size_t context_data_size)
-{
-    trace_assert(layer);
-    trace_assert(revert);
-    trace_assert(context_data_size < CONTEXT_SIZE);
-
-    Action action = {
-        .layer = layer,
-        .revert = revert
-    };
-
-    if (context_data) {
-        memcpy(action.context.data, context_data, context_data_size);
-    }
-
-    return action;
-}
-
 UndoHistory *create_undo_history(void)
 {
     Lt *lt = create_lt();
@@ -72,16 +47,13 @@ void undo_history_push(UndoHistory *undo_history,
 {
     trace_assert(undo_history);
 
-    Action action = create_action(
-        layer,
-        revert,
-        context_data,
-        context_data_size);
+    Action action = {
+        .revert = revert,
+        .layer = layer
+    };
 
-    stack_push(
-        &undo_history->actions,
-        &action,
-        sizeof(action));
+    stack_push(&undo_history->actions, context_data, context_data_size);
+    stack_push(&undo_history->actions, &action, sizeof(action));
 }
 
 void undo_history_pop(UndoHistory *undo_history)
@@ -89,8 +61,13 @@ void undo_history_pop(UndoHistory *undo_history)
     trace_assert(undo_history);
 
     if (stack_empty(&undo_history->actions) > 0) {
-        Action *action = stack_top_element(&undo_history->actions);
-        action->revert(action->layer, action->context);
+        Action action = *(Action *)stack_top_element(&undo_history->actions);
+        stack_pop(&undo_history->actions);
+
+        size_t context_size = stack_top_size(&undo_history->actions);
+        void *context = stack_top_element(&undo_history->actions);
+
+        action.revert(action.layer, context, context_size);
         stack_pop(&undo_history->actions);
     }
 }
index 801059e10838df49689f33b3941c2c3999226250..92211c075878e03e86bf825e4aae4174c33a33cd 100644 (file)
@@ -3,22 +3,13 @@
 
 #include "layer.h"
 
-#define CONTEXT_SIZE 256
-
-#define ASSERT_CONTEXT_SIZE(context)               \
-    trace_assert(sizeof(context) <= CONTEXT_SIZE)
-
 typedef enum {
     UNDO_ADD,
     UNDO_DELETE,
     UNDO_UPDATE
 } UndoType;
 
-typedef struct {
-    char data[CONTEXT_SIZE];
-} Context;
-
-typedef void (*RevertAction)(void *layer, Context context);
+typedef void (*RevertAction)(void *layer, void *context, size_t context_size);
 
 typedef struct UndoHistory UndoHistory;