From 08a2d53eb448fe80f68929111b8f6e74a44ffd50 Mon Sep 17 00:00:00 2001
From: Přemysl Janouch
Date: Sat, 29 Nov 2014 23:58:41 +0100
Subject: Draw continuous lines
So that the application is actually usable in shitty terminals.
---
autistdraw.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 60 insertions(+), 2 deletions(-)
diff --git a/autistdraw.c b/autistdraw.c
index 77094a0..0a7ad62 100644
--- a/autistdraw.c
+++ b/autistdraw.c
@@ -497,6 +497,14 @@ make_place_for_point (app_context_t *app, int x, int y)
static void
draw_point (app_context_t *app, int x, int y, uint8_t color)
{
+ // We don't actually draw anything immediately in client mode,
+ // instead we wait for confirmation from the server
+ if (app->mode == NETWORK_MODE_CLIENT)
+ {
+ send_draw_point_request (app, x, y, color);
+ return;
+ }
+
make_place_for_point (app, x, y);
BITMAP_PIXEL (app, x - app->bitmap_x, y - app->bitmap_y) = color;
@@ -516,6 +524,50 @@ draw_point (app_context_t *app, int x, int y, uint8_t color)
send_draw_point_response (iter, x, y, color);
}
+static void
+draw_line (app_context_t *app, int x0, int x1, int y0, int y1, uint8_t color)
+{
+ // Integer version of Bresenham's line drawing algorithm,
+ // loosely based on code from libcaca because screw math
+ int dx = abs (x1 - x0);
+ int dy = abs (y1 - y0);
+
+ bool steep = dx < dy;
+ if (steep)
+ {
+ // Flip the coordinate system on input
+ int tmp;
+ tmp = x0; x0 = y0; y0 = tmp;
+ tmp = x1; x1 = y1; y1 = tmp;
+ tmp = dx; dx = dy; dy = tmp;
+ }
+
+ int step_x = x0 > x1 ? -1 : 1;
+ int step_y = y0 > y1 ? -1 : 1;
+
+ int dpr = dy * 2;
+ int delta = dpr - dx;
+ int dpru = delta - dx;
+
+ while (dx-- >= 0)
+ {
+ // Unflip the coordinate system on output
+ if (steep)
+ draw_point (app, y0, x0, color);
+ else
+ draw_point (app, x0, y0, color);
+
+ x0 += step_x;
+ if (delta > 0)
+ {
+ y0 += step_y;
+ delta += dpru;
+ }
+ else
+ delta += dpr;
+ }
+}
+
// --- Exports -----------------------------------------------------------------
static bool
@@ -854,10 +906,16 @@ on_mouse (app_context_t *app, termo_key_t *key)
if (screen_y >= TOP_BAR_CUTOFF)
{
- if (app->mode == NETWORK_MODE_CLIENT)
- send_draw_point_request (app, canvas_x, canvas_y, *color);
+ if (event == TERMO_MOUSE_DRAG)
+ draw_line (app,
+ app->move_saved_x, canvas_x,
+ app->move_saved_y, canvas_y,
+ *color);
else
draw_point (app, canvas_x, canvas_y, *color);
+
+ app->move_saved_x = canvas_x;
+ app->move_saved_y = canvas_y;
}
else if (screen_y > 0 && event != TERMO_MOUSE_DRAG)
{
--
cgit v1.2.3-70-g09d2