diff options
Diffstat (limited to 'web/svg_dartboard.c')
-rw-r--r-- | web/svg_dartboard.c | 79 |
1 files changed, 33 insertions, 46 deletions
diff --git a/web/svg_dartboard.c b/web/svg_dartboard.c index 74c4f1c..6209e65 100644 --- a/web/svg_dartboard.c +++ b/web/svg_dartboard.c @@ -106,6 +106,7 @@ struct elem *gen_arc(double a1, double a2, double r1, double r2, char *col) buflen += sprintf(buf + buflen, "A%f %f 0 1 1 %f %f", r2, r2, cc3.x, cc3.y); buf[buflen++] = 'Z'; + buf[buflen++] = 0; elem_add_attr(e, "d", buf); return e; @@ -162,50 +163,34 @@ void draw_elem(struct elem *e) e->name, e->n_attrs, e->attr_names, e->attr_vals, e->content); } -void draw_spider() +int draw_spider(int elemc, struct elem **elemv) { - struct elem *e; - for (int i = 5; i > 1; --i) { - e = gen_ring(OUTER_DISTS[i], WIRE_WIDTH, C_WIRE); - draw_elem(e); - elem_free(e); - e = gen_ring(OUTER_DISTS[i], WIRE_WIDTH/2, C_WIRE_INNER); - draw_elem(e); - elem_free(e); + elemv[elemc++] = gen_ring(OUTER_DISTS[i], WIRE_WIDTH, C_WIRE); + elemv[elemc++] = gen_ring(OUTER_DISTS[i], WIRE_WIDTH/2, C_WIRE_INNER); } for (int i = 0; i < NUM_SECTORS; ++i) { double a = 90 - i*SECTOR_WIDTH - SECTOR_WIDTH/2; if (a < 0) a += 360; - e = gen_line(a, OUTER_DISTS[1], OUTER_DISTS[5] + 10, + elemv[elemc++] = gen_line(a, OUTER_DISTS[1], OUTER_DISTS[5] + 10, WIRE_WIDTH, C_WIRE); - draw_elem(e); - elem_free(e); - e = gen_line(a, OUTER_DISTS[1], OUTER_DISTS[5] + 10, + elemv[elemc++] = gen_line(a, OUTER_DISTS[1], OUTER_DISTS[5] + 10, WIRE_WIDTH/2, C_WIRE_INNER); - draw_elem(e); - elem_free(e); } for (int i = 1; i >= 0; --i) { - e = gen_ring(OUTER_DISTS[i], WIRE_WIDTH, C_WIRE); - draw_elem(e); - elem_free(e); - e = gen_ring(OUTER_DISTS[i], WIRE_WIDTH/2, C_WIRE_INNER); - draw_elem(e); - elem_free(e); + elemv[elemc++] = gen_ring(OUTER_DISTS[i], WIRE_WIDTH, C_WIRE); + elemv[elemc++] = gen_ring(OUTER_DISTS[i], WIRE_WIDTH/2, C_WIRE_INNER); } + + return elemc; } -EMSCRIPTEN_KEEPALIVE void draw_numbers() +int draw_numbers(int elemc, struct elem **elemv) { - struct elem *e; - - e = gen_ring(DIAMETER/2 - WIRE_WIDTH*4, WIRE_WIDTH, "#ddd"); - draw_elem(e); - elem_free(e); + elemv[elemc++] = gen_ring(DIAMETER/2 - WIRE_WIDTH*4, WIRE_WIDTH, "#ddd"); int r = DIAMETER/2 - 33/2; for (int i = 0; i < 20; ++i) { @@ -213,7 +198,7 @@ EMSCRIPTEN_KEEPALIVE void draw_numbers() if (a < 0) a += 360; struct ccoords cc = pol_to_cart((struct pcoords){ .a = a, .r = r }); - e = elem_init("text", 5); + struct elem *e = elemv[elemc++] = elem_init("text", 5); elem_add_attr(e, "font-size", "33"); elem_add_attr(e, "fill", "#fff"); elem_add_attr(e, "text-anchor", "middle"); @@ -226,41 +211,43 @@ EMSCRIPTEN_KEEPALIVE void draw_numbers() e->content = malloc(3); sprintf(e->content, "%d", SECTORS[i]); - - draw_elem(e); - elem_free(e); } + + return elemc; } void draw_board() { - struct elem *e; + struct elem **elemv = malloc(500 * sizeof(*elemv)); + int elemc = 0; - e = gen_circle(DIAMETER/2, C_BLACK); - draw_elem(e); - elem_free(e); + elemv[elemc++] = gen_circle(DIAMETER/2, C_BLACK); for (int i = 5; i > 1; --i) { for (int j = 0; j < NUM_SECTORS; ++j) { double a = 90 - j*SECTOR_WIDTH; if (a < 0) a += 360; - e = gen_segment(a, OUTER_DISTS[i-1], OUTER_DISTS[i], + elemv[elemc++] = gen_segment(a, OUTER_DISTS[i-1], OUTER_DISTS[i], i%2 ? (j%2 ? C_GREEN : C_RED) : (j%2 ? C_WHITE : C_BLACK)); - draw_elem(e); - elem_free(e); } } - e = gen_circle(OUTER_DISTS[1], C_GREEN); - draw_elem(e); - elem_free(e); - e = gen_circle(OUTER_DISTS[0], C_RED); - draw_elem(e); - elem_free(e); + elemv[elemc++] = gen_circle(OUTER_DISTS[1], C_GREEN); + elemv[elemc++] = gen_circle(OUTER_DISTS[0], C_RED); + + elemc = draw_spider(elemc, elemv); + elemc = draw_numbers(elemc, elemv); + + EM_ASM({drawElemv($0, $1, $2, $3, $4, $5, $6)}, + elemc, elemv, + offsetof(struct elem, name), offsetof(struct elem, n_attrs), + offsetof(struct elem, attr_names), offsetof(struct elem, attr_vals), + offsetof(struct elem, content)); - draw_spider(); - draw_numbers(); + for (int i = 0; i < elemc; ++i) + free(elemv[i]); + free(elemv); } void draw_point(double x, double y) { |