From abc559995546eda81a51ad7d03422b5c7e09fa87 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Fri, 29 Apr 2022 02:06:33 -0700 Subject: web: javascript needs more pointers --- web/static/dartboat.js | 24 +++++++++++++++ web/svg_dartboard.c | 79 +++++++++++++++++++++----------------------------- 2 files changed, 57 insertions(+), 46 deletions(-) (limited to 'web') diff --git a/web/static/dartboat.js b/web/static/dartboat.js index 53a113a..c17e29d 100644 --- a/web/static/dartboat.js +++ b/web/static/dartboat.js @@ -136,6 +136,30 @@ function drawVisit(visit_no, p1_pts, p1_rem, p2_pts, p2_rem, p2_darts) { e.scrollTop = e.scrollHeight; } +function drawElemv(elemc, elemv, off_name, off_n_attrs, off_attr_names, + off_attr_vals, off_content) { + for (let ptr = elemv; ptr < elemv + 4*elemc; ptr += 4) { + const struct = HEAP32[ptr>>2]; + + const e = document.createElementNS('http://www.w3.org/2000/svg', + UTF8ToString(HEAP32[(struct + off_name)>>2])); + + const n_attrs = HEAP32[(struct + off_n_attrs)>>2]; + if (n_attrs) { + const attr_names = HEAP32[(struct + off_attr_names)>>2]; + const attr_vals = HEAP32[(struct + off_attr_vals)>>2]; + for (let i = 0; i < n_attrs; ++i) + e.setAttribute(UTF8ToString(HEAP32[(attr_names + i*4)>>2]), + UTF8ToString(HEAP32[(attr_vals + i*4)>>2])); + } + + const content = HEAP32[(struct + off_content)>>2]; + if (content) e.textContent = UTF8ToString(content); + + $('#dartboard').appendChild(e); + } +} + function drawElem(target, name, n_attrs, attr_names, attr_vals, content) { const e = document.createElementNS('http://www.w3.org/2000/svg', UTF8ToString(name)); 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) { -- cgit v1.2.3-70-g09d2