summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorP. J. McDermott <pj@pehjota.net>2021-08-05 02:13:13 (EDT)
committer P. J. McDermott <pj@pehjota.net>2021-08-05 02:18:38 (EDT)
commit9976dd05a2c5342d62313da1dc3950c521aa2d22 (patch)
tree670913136e0c067bf9d8abf638fbb30f1a8244f4 /src
parent910c8c55521abc3b8ed415b9d5678e85abb3a408 (diff)
downloadmazefight-9976dd05a2c5342d62313da1dc3950c521aa2d22.zip
mazefight-9976dd05a2c5342d62313da1dc3950c521aa2d22.tar.gz
mazefight-9976dd05a2c5342d62313da1dc3950c521aa2d22.tar.bz2
tk: Add internal grid constructor
Diffstat (limited to 'src')
-rw-r--r--src/tk/grid.c47
-rw-r--r--src/tk/widget.h5
2 files changed, 36 insertions, 16 deletions
diff --git a/src/tk/grid.c b/src/tk/grid.c
index ca6b180..8b6b99a 100644
--- a/src/tk/grid.c
+++ b/src/tk/grid.c
@@ -156,24 +156,14 @@ _mftk_grid_destroy(struct mftk_widget *w)
}
struct mftk_widget *
-mftk_grid_new(int rows, int cols, int row_spacing, int col_spacing, ...)
+mftk_grid_new_a(int rows, int cols, int row_spacing, int col_spacing,
+ struct mftk_widget **children)
{
struct mftk_widget *w;
struct mftk_grid *g;
- va_list ap;
- int r;
- int c;
mftk_widget_init(w, g, grid);
- g->children = calloc(rows * cols, sizeof(*g->children));
- if (g->children == NULL) {
- SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
- "Couldn't create widget: %s",
- strerror(errno));
- free(g);
- return NULL;
- }
g->children_x = calloc(cols, sizeof(*g->children_x));
if (g->children_x == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
@@ -198,15 +188,40 @@ mftk_grid_new(int rows, int cols, int row_spacing, int col_spacing, ...)
g->cols = cols;
g->row_spacing = row_spacing;
g->col_spacing = col_spacing;
+ g->children = children;
+
+ return w;
+}
+
+struct mftk_widget *
+mftk_grid_new(int rows, int cols, int row_spacing, int col_spacing, ...)
+{
+ struct mftk_widget **children;
+ struct mftk_widget *w;
+ va_list ap;
+ int r;
+ int c;
+
+ children = calloc(rows * cols, sizeof(*children));
+ if (children == NULL) {
+ SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
+ "Couldn't create widget: %s",
+ strerror(errno));
+ return NULL;
+ }
va_start(ap, col_spacing);
- for (r = 0; r < g->rows; ++r) {
- for (c = 0; c < g->cols; ++c) {
- g->children[r * g->cols + c] = va_arg(ap,
- struct mftk_widget *);
+ for (r = 0; r < rows; ++r) {
+ for (c = 0; c < cols; ++c) {
+ children[r*cols + c] = va_arg(ap, struct mftk_widget *);
}
}
va_end(ap);
+ w = mftk_grid_new_a(rows, cols, row_spacing, col_spacing, children);
+ if (w == NULL) {
+ free(children);
+ }
+
return w;
}
diff --git a/src/tk/widget.h b/src/tk/widget.h
index 047dd6d..1ac5f46 100644
--- a/src/tk/widget.h
+++ b/src/tk/widget.h
@@ -21,6 +21,7 @@
#define MFTK_WIDGET_H_
#include <SDL.h>
+#include <stdarg.h>
struct mftk_widget {
int w;
@@ -49,6 +50,10 @@ mftk_widget_new(size_t size);
t_w = (struct mftk_##name *) w; \
} while (0)
+struct mftk_widget *
+mftk_grid_new_a(int rows, int cols, int row_spacing, int col_spacing,
+ struct mftk_widget **children);
+
enum mftk_check_shape {
MFTK_CHECK_SHAPE_BOX,
MFTK_CHECK_SHAPE_CIR