changeset 106:ed1a6bb02a78

inventory_dialog: use a small circle as test
author David Demelier <markand@malikania.fr>
date Wed, 01 Apr 2020 12:10:00 +0200
parents f6b5e2fbbc81
children 62aeb864093f
files Makefile src/core/button.c src/core/inventory_dialog.c src/core/math.c src/core/math.h src/core/maths.c src/core/maths.h src/core/painter.c src/core/painter.h src/core/theme.c
diffstat 10 files changed, 147 insertions(+), 122 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Tue Mar 31 21:29:54 2020 +0200
+++ b/Makefile	Wed Apr 01 12:10:00 2020 +0200
@@ -21,6 +21,7 @@
 CC=             cc
 AR=             ar
 CFLAGS=         -O0 -std=c11 -g
+LDFLAGS=        -lm
 # Use this instead to build a release.
 # CFLAGS=         -O3 -DNDEBUG -std=c11 -Wall -Wextra
 PROG=           molko
@@ -50,7 +51,7 @@
                 src/core/label.c                        \
                 src/core/map.c                          \
                 src/core/map_state.c                    \
-                src/core/math.c                         \
+                src/core/maths.c                        \
                 src/core/message.c                      \
                 src/core/painter.c                      \
                 src/core/panic.c                        \
--- a/src/core/button.c	Tue Mar 31 21:29:54 2020 +0200
+++ b/src/core/button.c	Wed Apr 01 12:10:00 2020 +0200
@@ -20,7 +20,7 @@
 
 #include "button.h"
 #include "event.h"
-#include "math.h"
+#include "maths.h"
 #include "theme.h"
 
 static bool
@@ -30,7 +30,7 @@
 	assert(click);
 	assert(click->type == EVENT_CLICKDOWN || click->type == EVENT_CLICKUP);
 
-	return math_is_boxed(button->x, button->y, button->w, button->h,
+	return maths_is_boxed(button->x, button->y, button->w, button->h,
 	    click->x, click->y);
 }
 
--- a/src/core/inventory_dialog.c	Tue Mar 31 21:29:54 2020 +0200
+++ b/src/core/inventory_dialog.c	Wed Apr 01 12:10:00 2020 +0200
@@ -26,7 +26,7 @@
 #include "inventory.h"
 #include "inventory_dialog.h"
 #include "item.h"
-#include "math.h"
+#include "maths.h"
 #include "painter.h"
 #include "texture.h"
 #include "window.h"
@@ -132,9 +132,9 @@
 	}
 
 	if (selected) {
-		x -= 16 + 8;
-		y += (ITEM_SIZE / 2) - 8;
-		painter_draw_rectangle(x, y, 16, 16);
+		x -= 16;
+		y += (ITEM_SIZE / 2) - 4;
+		painter_draw_circle(x, y, 8);
 	}
 }
 
@@ -255,7 +255,7 @@
 		for (int c = 0; c < INVENTORY_COLS_MAX; ++c) {
 			compute_box_position(dlg, r, c, &x, &y);
 
-			if (math_is_boxed(x, y, ITEM_SIZE, ITEM_SIZE, ev->x, ev->y)) {
+			if (maths_is_boxed(x, y, ITEM_SIZE, ITEM_SIZE, ev->x, ev->y)) {
 				dlg->selrow = r;
 				dlg->selcol = c;
 			}
--- a/src/core/math.c	Tue Mar 31 21:29:54 2020 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,44 +0,0 @@
-/*
- * math.c -- basic maths
- *
- * Copyright (c) 2020 David Demelier <markand@malikania.fr>
- *
- * Permission to use, copy, modify, and/or distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
-
-#include "math.h"
-
-bool
-math_is_boxed(int x, int y, unsigned int w, unsigned int h, int px, int py)
-{
-	return px >= x &&
-	       py >= y &&
-	       px <= x + w &&
-	       py <= y + h;
-}
-
-void
-math_centerize(int *x,
-               int *y,
-               unsigned int w,
-               unsigned int h,
-               int px,
-               int py,
-               unsigned int pw,
-               unsigned int ph)
-{
-	if (x)
-		*x = px + (pw / 2) - (w / 2);
-	if (y)
-		*y = py + (ph / 2) - (h / 2);
-}
--- a/src/core/math.h	Tue Mar 31 21:29:54 2020 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,68 +0,0 @@
-/*
- * math.h -- basic maths
- *
- * Copyright (c) 2020 David Demelier <markand@malikania.fr>
- *
- * Permission to use, copy, modify, and/or distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
-
-#ifndef MOLKO_MATH_H
-#define MOLKO_MATH_H
-
-#include <stdbool.h>
-
-/**
- * \file math.h
- * \brief Basic maths.
- */
-
-/**
- * Tells if a coordinate is within a rectangle region.
- *
- * \param x the region start
- * \param y the region start
- * \param w the region width
- * \param h the region height
- * \param px the point to test
- * \param py the point to test
- * \return True if within the region
- */
-bool
-math_is_boxed(int x, int y, unsigned int w, unsigned int h, int px, int py);
-
-/**
- * Update x, y to be centered into a parent region.
- *
- * You can select to ignore horizontal/vertical centering by passing NULL to x
- * or y respectively.
- *
- * \param x the pointer to x coordinate to modify
- * \param y the pointer yo y coordinate to modify
- * \param w the object width
- * \param h the object height
- * \param px the parent region start
- * \param py the parent region start
- * \param pw the parent region width
- * \param ph the parent region height
- */
-void
-math_centerize(int *x,
-               int *y,
-               unsigned int w,
-               unsigned int h,
-               int px,
-               int py,
-               unsigned int pw,
-               unsigned int ph);
-
-#endif /* !MOLKO_MATH_H */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/core/maths.c	Wed Apr 01 12:10:00 2020 +0200
@@ -0,0 +1,44 @@
+/*
+ * maths.c -- basic maths
+ *
+ * Copyright (c) 2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include "maths.h"
+
+bool
+maths_is_boxed(int x, int y, unsigned int w, unsigned int h, int px, int py)
+{
+	return px >= x &&
+	       py >= y &&
+	       px <= x + w &&
+	       py <= y + h;
+}
+
+void
+maths_centerize(int *x,
+               int *y,
+               unsigned int w,
+               unsigned int h,
+               int px,
+               int py,
+               unsigned int pw,
+               unsigned int ph)
+{
+	if (x)
+		*x = px + (pw / 2) - (w / 2);
+	if (y)
+		*y = py + (ph / 2) - (h / 2);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/core/maths.h	Wed Apr 01 12:10:00 2020 +0200
@@ -0,0 +1,68 @@
+/*
+ * maths.h -- basic maths
+ *
+ * Copyright (c) 2020 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef MOLKO_MATHS_H
+#define MOLKO_MATHS_H
+
+#include <stdbool.h>
+
+/**
+ * \file maths.h
+ * \brief Basic maths.
+ */
+
+/**
+ * Tells if a coordinate is within a rectangle region.
+ *
+ * \param x the region start
+ * \param y the region start
+ * \param w the region width
+ * \param h the region height
+ * \param px the point to test
+ * \param py the point to test
+ * \return True if within the region
+ */
+bool
+maths_is_boxed(int x, int y, unsigned int w, unsigned int h, int px, int py);
+
+/**
+ * Update x, y to be centered into a parent region.
+ *
+ * You can select to ignore horizontal/vertical centering by passing NULL to x
+ * or y respectively.
+ *
+ * \param x the pointer to x coordinate to modify
+ * \param y the pointer yo y coordinate to modify
+ * \param w the object width
+ * \param h the object height
+ * \param px the parent region start
+ * \param py the parent region start
+ * \param pw the parent region width
+ * \param ph the parent region height
+ */
+void
+maths_centerize(int *x,
+                int *y,
+                unsigned int w,
+                unsigned int h,
+                int px,
+                int py,
+                unsigned int pw,
+                unsigned int ph);
+
+#endif /* !MOLKO_MATHS_H */
--- a/src/core/painter.c	Tue Mar 31 21:29:54 2020 +0200
+++ b/src/core/painter.c	Wed Apr 01 12:10:00 2020 +0200
@@ -16,6 +16,8 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+#include <math.h>
+
 #include "color.h"
 #include "painter.h"
 #include "texture.h"
@@ -86,6 +88,25 @@
 }
 
 void
+painter_draw_circle(int x, int y, int radius)
+{
+	// Note that there is more to altering the bitrate of this 
+	// method than just changing this value.  See how pixels are
+	// altered at the following web page for tips:
+	//   http://www.libsdl.org/intro.en/usingvideo.html
+	static const int BPP = 4;
+
+	//double ra = (double)radius;
+
+	for (double dy = 1; dy <= radius; dy += 1.0) {
+		double dx = floor(sqrt((2.0 * radius * dy) - (dy * dy)));
+
+		SDL_RenderDrawLine(RENDERER(), x - dx, y + dy - radius, x + dx, y + dy - radius);
+		SDL_RenderDrawLine(RENDERER(), x - dx, y - dy + radius, x + dx, y - dy + radius);
+	}
+}
+
+void
 painter_clear(void)
 {
 	SDL_RenderClear(RENDERER());
--- a/src/core/painter.h	Tue Mar 31 21:29:54 2020 +0200
+++ b/src/core/painter.h	Wed Apr 01 12:10:00 2020 +0200
@@ -101,6 +101,9 @@
 void
 painter_draw_rectangle(int x, int y, unsigned int w, unsigned int h);
 
+void
+painter_draw_circle(int x, int y, int radius);
+
 /**
  * Clear the window.
  */
--- a/src/core/theme.c	Tue Mar 31 21:29:54 2020 +0200
+++ b/src/core/theme.c	Wed Apr 01 12:10:00 2020 +0200
@@ -15131,7 +15131,7 @@
 		if (!font_render(t->fonts[THEME_FONT_INTERFACE], &tex, label->text))
 			panic();
 
-		math_centerize(px, py, tex.w, tex.h,
+		maths_centerize(px, py, tex.w, tex.h,
 		    label->x, label->y, label->w, label->h);
 
 		texture_draw(&tex, x + 1, y + 1);
@@ -15146,7 +15146,7 @@
 	if (!font_render(t->fonts[THEME_FONT_INTERFACE], &tex, label->text))
 		panic();
 
-	math_centerize(px, py, tex.w, tex.h,
+	maths_centerize(px, py, tex.w, tex.h,
 	    label->x, label->y, label->w, label->h);
 
 	texture_draw(&tex, x, y);