From 1b491f11a998a1be08e7f6af6177150516c31d4f Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sat, 11 Nov 2006 07:56:30 +0000 Subject: [PATCH] * Implemented cucul_draw_cp437_box(). --- cucul/box.c | 68 +++++++++++++++++++++++++++++++++++++++++++++------ cucul/cucul.h | 1 + 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/cucul/box.c b/cucul/box.c index c2703dc..95ef35d 100644 --- a/cucul/box.c +++ b/cucul/box.c @@ -99,17 +99,69 @@ int cucul_draw_thin_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2) cucul_putchar(cv, x2, y, '|'); /* Draw corners */ - if(x1 >= 0 && y1 >= 0) - cucul_putchar(cv, x1, y1, ','); + cucul_putchar(cv, x1, y1, ','); + cucul_putchar(cv, x1, y2, '`'); + cucul_putchar(cv, x2, y1, '.'); + cucul_putchar(cv, x2, y2, '\''); - if(x1 >= 0 && y2 <= ymax) - cucul_putchar(cv, x1, y2, '`'); + return 0; +} + +/** \brief Draw a box on the canvas using CP437 characters. + * + * This function never fails. + * + * \param cv The handle to the libcucul canvas. + * \param x1 X coordinate of the upper-left corner of the box. + * \param y1 Y coordinate of the upper-left corner of the box. + * \param x2 X coordinate of the lower-right corner of the box. + * \param y2 Y coordinate of the lower-right corner of the box. + * \return This function always returns 0. + */ +int cucul_draw_cp437_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2) +{ + int x, y, xmax, ymax; + + if(x1 > x2) + { + int tmp = x1; + x1 = x2; x2 = tmp; + } + + if(y1 > y2) + { + int tmp = y1; + y1 = y2; y2 = tmp; + } + + xmax = cv->width - 1; + ymax = cv->height - 1; - if(x2 <= xmax && y1 >= 0) - cucul_putchar(cv, x2, y1, '.'); + if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax) + return 0; - if(x2 <= xmax && y2 <= ymax) - cucul_putchar(cv, x2, y2, '\''); + /* Draw edges */ + if(y1 >= 0) + for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++) + cucul_putchar(cv, x, y1, 0x2500); /* ─ */ + + if(y2 <= ymax) + for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++) + cucul_putchar(cv, x, y2, 0x2500); /* ─ */ + + if(x1 >= 0) + for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++) + cucul_putchar(cv, x1, y, 0x2502); /* │ */ + + if(x2 <= xmax) + for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++) + cucul_putchar(cv, x2, y, 0x2502); /* │ */ + + /* Draw corners */ + cucul_putchar(cv, x1, y1, 0x250c); /* ┌ */ + cucul_putchar(cv, x1, y2, 0x2514); /* └ */ + cucul_putchar(cv, x2, y1, 0x2510); /* ┐ */ + cucul_putchar(cv, x2, y2, 0x2518); /* ┘ */ return 0; } diff --git a/cucul/cucul.h b/cucul/cucul.h index 1168055..622b6e4 100644 --- a/cucul/cucul.h +++ b/cucul/cucul.h @@ -146,6 +146,7 @@ int cucul_fill_ellipse(cucul_canvas_t *, int, int, int, int, char const *); int cucul_draw_box(cucul_canvas_t *, int, int, int, int, char const *); int cucul_draw_thin_box(cucul_canvas_t *, int, int, int, int); +int cucul_draw_cp437_box(cucul_canvas_t *, int, int, int, int); int cucul_fill_box(cucul_canvas_t *, int, int, int, int, char const *); int cucul_draw_triangle(cucul_canvas_t *, int, int, int, int, int, int, char const *);