diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-05-03 15:40:09 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-05-03 15:40:09 -0700 |
commit | e091cdb3187b60a9901bd14d475eebc481c9a299 (patch) | |
tree | 0c559ff47821c6868ec29dc1078993d4ff9c008c | |
parent | 12e0680e13e3b00fe085ff419abcef88dd512b23 (diff) | |
download | dartboat-e091cdb3187b60a9901bd14d475eebc481c9a299.tar.gz dartboat-e091cdb3187b60a9901bd14d475eebc481c9a299.tar.xz |
web: move elem construction to shared dom file
-rw-r--r-- | web/web_dom.c | 67 | ||||
-rw-r--r-- | web/web_dom.h | 16 | ||||
-rw-r--r-- | web/web_scoreboard.c | 18 | ||||
-rw-r--r-- | web/web_svg.c | 69 |
4 files changed, 90 insertions, 80 deletions
diff --git a/web/web_dom.c b/web/web_dom.c new file mode 100644 index 0000000..ddb59ad --- /dev/null +++ b/web/web_dom.c @@ -0,0 +1,67 @@ +#include "web_dom.h" + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <emscripten/emscripten.h> + +struct elem *elem_init(char *ns, char *name, int size_attrs) +{ + struct elem *e = malloc(sizeof(*e)); + + e->ns = ns ? strdup(ns) : NULL; + e->name = strdup(name); + + e->n_attrs = 0; + e->size_attrs = size_attrs; + e->attr_names = malloc(size_attrs * sizeof(*(e->attr_names))); + e->attr_vals = malloc(size_attrs * sizeof(*(e->attr_vals))); + e->content = NULL; + + return e; +} + +void elem_free(struct elem *e) +{ + for(int i = 0; i < e->n_attrs; ++i) { + free(e->attr_names[i]); + free(e->attr_vals[i]); + } + + free(e->content); + free(e->attr_names); + free(e->attr_vals); + free(e->name); + free(e); +} + +void elem_add_attr(struct elem *e, char *name, char *val) +{ + if (e->n_attrs == e->size_attrs) { + e->size_attrs *= 2; + e->attr_names = realloc(e->attr_names, + e->size_attrs * sizeof(*(e->attr_names))); + e->attr_vals = realloc(e->attr_vals, + e->size_attrs * sizeof(*(e->attr_vals))); + } + + e->attr_names[e->n_attrs] = strdup(name); + e->attr_vals[e->n_attrs++] = strdup(val); +} + +void elem_add_attr_double(struct elem *e, char *name, double val) +{ + char str[512]; + sprintf(str, "%f", val); + elem_add_attr(e, name, str); +} + +void append_elemv(char *sel, int elemc, struct elem **elemv) +{ + EM_ASM({elemAppendElemv($0, $1, $2, $3, $4, $5, $6, $7, $8)}, + sel, elemc, elemv, + offsetof(struct elem, ns), offsetof(struct elem, name), + offsetof(struct elem, content), offsetof(struct elem, n_attrs), + offsetof(struct elem, attr_names), offsetof(struct elem, attr_vals)); +} diff --git a/web/web_dom.h b/web/web_dom.h new file mode 100644 index 0000000..a86d432 --- /dev/null +++ b/web/web_dom.h @@ -0,0 +1,16 @@ +#ifndef WEB_DOM_H +#define WEB_DOM_H + +struct elem { + char *ns, *name, *content; + int n_attrs, size_attrs; + char **attr_names, **attr_vals; +}; + +struct elem *elem_init(char *ns, char *name, int size_attrs); +void elem_free(struct elem *e); +void elem_add_attr(struct elem *e, char *name, char *val); +void elem_add_attr_double(struct elem *e, char *name, double val); +void append_elemv(char *sel, int elemc, struct elem **elemv); + +#endif diff --git a/web/web_scoreboard.c b/web/web_scoreboard.c index 18835eb..bc1a9d0 100644 --- a/web/web_scoreboard.c +++ b/web/web_scoreboard.c @@ -1,3 +1,4 @@ +#include "web_dom.h" #include "web_match.h" #include "web_scoreboard.h" @@ -176,19 +177,6 @@ void clear_player_info(int pn) scoreboard_set_player_avg(pn, 0); } - -// FIXME move html/svg elem stuff to common file -struct elem { - char *ns, *name, *content; - int n_attrs, size_attrs; - char **attr_names, **attr_vals; -}; -struct elem *elem_init(char *ns, char *name, int size_attrs); -void elem_free(struct elem *e); -void elem_add_attr(struct elem *e, char *name, char *val); -void elem_add_attr_double(struct elem *e, char *name, double val); -void append_elemv(char *sel, int elemc, struct elem **elemv); - struct elem *gen_div(char *str, char *class) { struct elem *e = elem_init(NULL, "div", 1); @@ -262,5 +250,9 @@ void draw_visits() } append_elemv("#visits", elemc, elemv); + for (int i = 0; i < elemc; ++i) + elem_free(elemv[i]); + free(elemv); + EM_ASM({elemScrollToBottom($0)}, "#visits"); } diff --git a/web/web_svg.c b/web/web_svg.c index 2f7dc07..3d3288c 100644 --- a/web/web_svg.c +++ b/web/web_svg.c @@ -1,4 +1,5 @@ #include "web_svg.h" +#include "web_dom.h" #include "board.h" #include "comp.h" @@ -19,68 +20,11 @@ #define C_WIRE "#909ca0" #define C_WIRE_INNER "#d8e6ec" -struct elem { - char *ns, *name, *content; - int n_attrs, size_attrs; - char **attr_names, **attr_vals; -}; - -struct elem *elem_init(char *ns, char *name, int size_attrs) -{ - struct elem *e = malloc(sizeof(*e)); - - e->ns = ns ? strdup(ns) : NULL; - e->name = strdup(name); - - e->n_attrs = 0; - e->size_attrs = size_attrs; - e->attr_names = malloc(size_attrs * sizeof(*(e->attr_names))); - e->attr_vals = malloc(size_attrs * sizeof(*(e->attr_vals))); - e->content = NULL; - - return e; -} - static inline struct elem *svg_elem_init(char *name, int size_attrs) { return elem_init("http://www.w3.org/2000/svg", name, size_attrs); } -void elem_free(struct elem *e) -{ - for(int i = 0; i < e->n_attrs; ++i) { - free(e->attr_names[i]); - free(e->attr_vals[i]); - } - - free(e->content); - free(e->attr_names); - free(e->attr_vals); - free(e->name); - free(e); -} - -void elem_add_attr(struct elem *e, char *name, char *val) -{ - if (e->n_attrs == e->size_attrs) { - e->size_attrs *= 2; - e->attr_names = realloc(e->attr_names, - e->size_attrs * sizeof(*(e->attr_names))); - e->attr_vals = realloc(e->attr_vals, - e->size_attrs * sizeof(*(e->attr_vals))); - } - - e->attr_names[e->n_attrs] = strdup(name); - e->attr_vals[e->n_attrs++] = strdup(val); -} - -void elem_add_attr_double(struct elem *e, char *name, double val) -{ - char str[512]; - sprintf(str, "%f", val); - elem_add_attr(e, name, str); -} - // draws a ring if angles are equal struct elem *gen_arc(double a1, double a2, double r1, double r2, char *col) { @@ -204,15 +148,6 @@ int draw_numbers(int elemc, struct elem **elemv) return elemc; } -void append_elemv(char *sel, int elemc, struct elem **elemv) -{ - EM_ASM({elemAppendElemv($0, $1, $2, $3, $4, $5, $6, $7, $8)}, - sel, elemc, elemv, - offsetof(struct elem, ns), offsetof(struct elem, name), - offsetof(struct elem, content), offsetof(struct elem, n_attrs), - offsetof(struct elem, attr_names), offsetof(struct elem, attr_vals)); -} - void svg_draw_board() { struct elem **elemv = malloc(200 * sizeof(*elemv)); @@ -238,7 +173,7 @@ void svg_draw_board() append_elemv("#dartboard .base", elemc, elemv); for (int i = 0; i < elemc; ++i) - free(elemv[i]); + elem_free(elemv[i]); free(elemv); } |