summaryrefslogtreecommitdiff
path: root/csrc/mkf/graph_struct.c
diff options
context:
space:
mode:
Diffstat (limited to 'csrc/mkf/graph_struct.c')
-rw-r--r--csrc/mkf/graph_struct.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/csrc/mkf/graph_struct.c b/csrc/mkf/graph_struct.c
new file mode 100644
index 0000000..52aa207
--- /dev/null
+++ b/csrc/mkf/graph_struct.c
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2018 Iru Cai <mytbk920423@gmail.com>
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include "graph_struct.h"
+
+static inline void mem_copy_words(void *dst, void *src, size_t n)
+{
+ memcpy(dst, src, n*2);
+}
+
+struct graph_st * allocate_graph_st(int w, int h, int x, int y)
+{
+ struct graph_st * newst = (struct graph_st *)malloc(w * h * 2 + sizeof(graph_st));
+ newst->width = w;
+ newst->height = h;
+ newst->x = x;
+ newst->y = y;
+ newst->gdata = newst->data;
+ return newst;
+}
+
+/* crop graph a1 starting at (x, y) as laft-top coordinate,
+ * and get graph a2 with size w*h.
+ *
+ * this function looks buggy and can crash the game */
+
+struct graph_st * crop_graph(struct graph_st *a1, struct graph_st *a2, int x, int y, int w, int h)
+{
+ if (a2 == NULL) {
+ a2 = allocate_graph_st(w, h, 0, 0);
+ }
+
+ int16_t *src = &a1->gdata[a1->width * y + x];
+ int16_t *dst = a2->gdata;
+
+ for (int i = 0; i < h; i++) {
+ mem_copy_words(esi, ebx, w);
+ dst = dst + w;
+ src = src + a1->width;
+ }
+ return a2;
+}