summaryrefslogtreecommitdiff
path: root/board.c
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-05-25 00:57:32 -0700
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-05-25 00:58:15 -0700
commit9edcd68e7ddcdc4deca8c07b5b1c3c59cef13255 (patch)
tree1a7a0069e8d51709238d991dc43f0323ec32bb9c /board.c
parent9a02c0367351c311734f42ab2afe2b377ca12908 (diff)
downloaddartboat-9edcd68e7ddcdc4deca8c07b5b1c3c59cef13255.tar.gz
dartboat-9edcd68e7ddcdc4deca8c07b5b1c3c59cef13255.tar.xz
pregenerate segment names to avoid constant mallocs
Diffstat (limited to 'board.c')
-rw-r--r--board.c33
1 files changed, 17 insertions, 16 deletions
diff --git a/board.c b/board.c
index 1a543ee..f61bd42 100644
--- a/board.c
+++ b/board.c
@@ -14,10 +14,16 @@ int SECTORS[] = { 20, 1, 18, 4, 13, 6, 10, 15, 2, 17, 3, 19, 7,
int SECTOR_INDS[NUM_SECTORS];
double SECTOR_ANGLES[NUM_SECTORS];
-char *RING_NAMES[] = {
- FOREACH_RING(GEN_RING_STRING)
+static const char *SECTOR_NAMES[] = {
+ FOREACH_SECTOR(GEN_SECTOR_STR)
+ FOREACH_SECTOR(GEN_SECTOR_STR_D)
+ FOREACH_SECTOR(GEN_SECTOR_STR_T)
+};
+static const char *RING_NAMES[] = {
+ FOREACH_RING(GEN_RING_STR)
"OUT"
};
+
double OUTER_DISTS[] = {
FOREACH_RING(GEN_RING_OUTER_DIST)
};
@@ -70,22 +76,17 @@ struct segment get_segment(struct pcoords c)
.ring = get_ring(c.r) };
}
-char *segment_name(struct segment seg)
+const char *segment_name(struct segment seg)
{
- char *str = malloc(SEGMENT_MAX_LEN);
-
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wstringop-truncation"
if (seg.ring == R_BULL || seg.ring == R_25 || seg.ring == R_OUT)
- strncpy(str, RING_NAMES[seg.ring], SEGMENT_MAX_LEN);
- else
- snprintf(str, SEGMENT_MAX_LEN,
- (seg.ring == R_TREBLE ? "T%d" :
- (seg.ring == R_DOUBLE ? "D%d" : "%d")),
- seg.sector);
-#pragma GCC diagnostic pop
-
- return str;
+ return RING_NAMES[seg.ring];
+
+ int ind = seg.sector - 1;
+ if (seg.ring == R_DOUBLE)
+ ind += 20;
+ else if (seg.ring == R_TREBLE)
+ ind += 40;
+ return SECTOR_NAMES[ind];
}
int segment_points(struct segment seg)