From 6053d87cd02cd5aec9da5bc46039ec4cca181dec Mon Sep 17 00:00:00 2001
From: sanine <sanine.not@pm.me>
Date: Sun, 27 Nov 2022 01:19:14 -0600
Subject: add scale parsing

---
 src/transform.c      | 24 ++++++++++++++++++++++
 src/transform.h      |  3 +++
 src/transform.test.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 84 insertions(+)

(limited to 'src')

diff --git a/src/transform.c b/src/transform.c
index 6e4db56..465841d 100644
--- a/src/transform.c
+++ b/src/transform.c
@@ -83,6 +83,30 @@ int kai_parse_rotate(ka_matrix_t *m, ezxml_t tag)
 }
 
 
+int kai_parse_scale(ka_matrix_t *m, ezxml_t tag)
+{
+	if (strcmp("scale", ezxml_name(tag)) != 0)
+		return -1;
+	
+	char *str = ezxml_txt(tag);
+	char *end;
+	int i;
+	double scale[3];
+	for (i=0; i<3; i++) {
+		scale[i] = strtod(str, &end);
+		str = end;
+	}
+
+	memset(*m, 0, 16*sizeof(double));
+	(*m)[0] = scale[0];
+	(*m)[5] = scale[1];
+	(*m)[10] = scale[2];
+	(*m)[15] = 1.0f;
+	
+	return 0;
+}
+
+
 int kai_parse_translate(ka_matrix_t *m, ezxml_t tag)
 {
 	if (strcmp("translate", ezxml_name(tag)) != 0)
diff --git a/src/transform.h b/src/transform.h
index bbc79d1..f8dc2ad 100644
--- a/src/transform.h
+++ b/src/transform.h
@@ -11,6 +11,9 @@ int kai_parse_matrix(ka_matrix_t *m, ezxml_t tag);
 /* parse a <rotate> tag into a ka_matrix_t */
 int kai_parse_rotate(ka_matrix_t *m, ezxml_t tag);
 
+/* parse a <scale> tag into a ka_matrix_t */
+int kai_parse_scale(ka_matrix_t *m, ezxml_t tag);
+
 /* parse a <translate> tag into a ka_matrix_t */
 int kai_parse_translate(ka_matrix_t *m, ezxml_t tag);
 
diff --git a/src/transform.test.c b/src/transform.test.c
index f0cd06f..ba663bb 100644
--- a/src/transform.test.c
+++ b/src/transform.test.c
@@ -10,6 +10,9 @@ void parse_identity();
 void parse_rotate_fail();
 void parse_rotate();
 
+void parse_scale_fail();
+void parse_scale();
+
 void parse_translate_fail();
 void parse_translate();
 
@@ -20,6 +23,8 @@ void suite_transform()
 	lily_run_test(parse_identity);
 	lily_run_test(parse_rotate_fail);
 	lily_run_test(parse_rotate);
+	lily_run_test(parse_scale_fail);
+	lily_run_test(parse_scale);
 	lily_run_test(parse_translate_fail);
 	lily_run_test(parse_translate);
 }
@@ -134,6 +139,58 @@ void parse_rotate()
 }
 
 
+/* ======== scale ======== */
+void parse_scale_fail()
+{
+	char str[128];
+	strncpy(str, "<non></non>", 128);
+	ezxml_t tag = ezxml_parse_str(str, strlen(str));
+
+	ka_matrix_t mat;
+	int rc = kai_parse_scale(&mat, tag);
+	lily_assert_true(rc != 0);
+
+	ezxml_free(tag);
+}
+
+
+void parse_scale()
+{
+	char str[512];
+	strncpy(
+		str, 
+		"<scale>\n"
+		"	10 5 -1\n"
+		"</scale>",
+		512
+	);
+	ezxml_t tag = ezxml_parse_str(str, strlen(str));
+
+	ka_matrix_t mat;
+	mat[0] = 100;
+	int rc = kai_parse_scale(&mat, tag);
+	lily_assert_true(rc == 0);
+	ezxml_free(tag);
+
+	lily_assert_float_equal(mat[0], 10.0f, 1e-3);
+	lily_assert_float_equal(mat[1],  0.0f, 1e-3);
+	lily_assert_float_equal(mat[2],  0.0f, 1e-3);
+	lily_assert_float_equal(mat[3],  0.0f, 1e-3);
+	lily_assert_float_equal(mat[4],  0.0f, 1e-3);
+	lily_assert_float_equal(mat[5],  5.0f, 1e-3);
+	lily_assert_float_equal(mat[6],  0.0f, 1e-3);
+	lily_assert_float_equal(mat[7],  0.0f, 1e-3);
+	lily_assert_float_equal(mat[8],  0.0f, 1e-3);
+	lily_assert_float_equal(mat[9],  0.0f, 1e-3);
+	lily_assert_float_equal(mat[10],-1.0f, 1e-3);
+	lily_assert_float_equal(mat[11], 0.0f, 1e-3);
+	lily_assert_float_equal(mat[12], 0.0f, 1e-3);
+	lily_assert_float_equal(mat[13], 0.0f, 1e-3);
+	lily_assert_float_equal(mat[14], 0.0f, 1e-3);
+	lily_assert_float_equal(mat[15], 1.0f, 1e-3);
+}
+
+
 /* ======== translate ======== */
 void parse_translate_fail()
 {
-- 
cgit v1.2.1