aboutsummaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorrodri <rgl@antares-labs.eu>2023-08-25 21:53:17 +0000
committerrodri <rgl@antares-labs.eu>2023-08-25 21:53:17 +0000
commitca4af600fdad77b3c4603a62c81eee51187fafcf (patch)
treef0bfeb0cbefa5ee39726db70b6c7c4b6095d62e5 /util.c
parent25e1b9489b9090b5d3372dd0dac09c1e7b3f10b3 (diff)
downloadbattleship-ca4af600fdad77b3c4603a62c81eee51187fafcf.tar.gz
battleship-ca4af600fdad77b3c4603a62c81eee51187fafcf.tar.bz2
battleship-ca4af600fdad77b3c4603a62c81eee51187fafcf.zip
implemented most of the layout code.
added a new util.c to host the cell/coordinate conversions. made the showproc a painter thread instead, to avoid problems with menuhit(2).
Diffstat (limited to 'util.c')
-rw-r--r--util.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/util.c b/util.c
new file mode 100644
index 0000000..4594a9a
--- /dev/null
+++ b/util.c
@@ -0,0 +1,42 @@
+#include <u.h>
+#include <libc.h>
+#include <thread.h>
+#include <draw.h>
+#include <mouse.h>
+#include <keyboard.h>
+#include <geometry.h>
+#include "dat.h"
+#include "fns.h"
+
+static char rowtab[] = "abcdefghijklmnopq";
+
+
+char *
+cell2coords(Point2 cell)
+{
+ static char s[3+1];
+
+ assert(cell.x < 17 && cell.x >= 0
+ && cell.y < 17 && cell.y >= 0);
+
+ snprint(s, sizeof s, "%c%d", rowtab[(int)cell.y], (int)cell.x);
+ return s;
+}
+
+Point2
+coords2cell(char *s)
+{
+ Point2 cell;
+ char *p;
+
+ assert(s[0] >= 'a' && s[0] <= 'q');
+
+ cell = Pt2(0,0,1);
+ p = strchr(rowtab, s[0]);
+ cell.y = p-rowtab;
+ cell.x = strtol(s+1, nil, 10);
+
+ assert(cell.x < 17 && cell.x >= 0);
+
+ return cell;
+}