Source

watches / lunar / draw.c

Diff from to

File lunar/draw.c

  * Drawing functions.
  */
 #include "draw.h"
+#include "image.h"
 
 
 void
 	pulse_draw_point24(c);
 }
 
+static inline int32_t
+abs(
+	int32_t x
+)
+{
+	if (x < 0)
+		return -x;
+	return x;
+}
+
+void
+draw_line(
+	color24_t c,
+	int32_t x0,
+	int32_t y0,
+	int32_t x1,
+	int32_t y1
+)
+{
+	const int32_t dx = abs(x1 - x0);
+	const int32_t dy = abs(y1 - y0);
+
+	const int32_t sx = x0 < x1 ? 1 : -1;
+	const int32_t sy = y0 < y1 ? 1 : -1;
+	int32_t err = dx - dy;
+
+#if VSCREEN_SHIFT != 0
+	uint32_t last_px = -1;
+	uint32_t last_py = -1;
+#endif
+
+	while(1)
+	{
+#if VSCREEN_SHIFT != 0
+		uint32_t px = x0 >> VSCREEN_SHIFT;
+		uint32_t py = y0 >> VSCREEN_SHIFT;
+		if (px != last_px || py != last_py)
+		{
+			last_px = px;
+			last_py = py;
+			draw_pixel(c, x0, y0);
+		}
+#else
+		draw_pixel(c, x0, y0);
+#endif
+
+		if (x0 == x1 && y0 == y1)
+			break;
+		int32_t e2 = 2 * err;
+		if (e2 > -dy)
+		{
+			err -= dy;
+			x0 += sx;
+		}
+		if (e2 < +dx)
+		{
+			err += dx;
+			y0 += sy;
+		}
+	}
+}
+
+
+void
+draw_line_bg(
+	PulseResource image_id,
+	int32_t px, // position of image on screen
+	int32_t py,
+	int32_t x0, // position of line on screen
+	int32_t y0,
+	int32_t x1,
+	int32_t y1
+)
+{
+	image_resource_t resource;
+	image_resource(image_id, &resource);
+	
+	uint32_t offset = resource.offset;
+	const uint32_t width = 3 * resource.width;
+
+	const int32_t dx = abs(x1 - x0);
+	const int32_t dy = abs(y1 - y0);
+
+	const int32_t sx = x0 < x1 ? 1 : -1;
+	const int32_t sy = y0 < y1 ? 1 : -1;
+	int32_t err = dx - dy;
+
+	while(1)
+	{
+		uint8_t lum;
+		uint8_t img_x = x0 - px;
+		uint8_t img_y = y0 - py;
+		spiflash_read(
+			offset + width * img_y + img_x,
+			&lum,
+			sizeof(lum)
+		);
+
+		color24_t c = { lum, lum, lum };
+
+		draw_pixel(c, x0, y0);
+
+		if (x0 == x1 && y0 == y1)
+			break;
+		int32_t e2 = 2 * err;
+		if (e2 > -dy)
+		{
+			err -= dy;
+			x0 += sx;
+		}
+		if (e2 < +dx)
+		{
+			err += dx;
+			y0 += sy;
+		}
+	}
+}
 
 
 static inline int32_t
 
 
 void
-draw_line(
+draw_line_aa(
 	color24_t c,
 	int32_t x0,
 	int32_t y0,