~aritra1911/ansi_anim

15b2358ba963e1d793d63bee6a40f4a759fc7ae9 — Aritra Sarkar 2 years ago 23a7675
Move one of the rects with hjkl
M ansilib/inc/graphics/box.h => ansilib/inc/graphics/box.h +2 -0
@@ 14,5 14,7 @@ typedef struct {
} box_t;

void draw(const box_t *box);
void erase(const box_t *box);
void nudge(box_t *box, dir_t dir, int32_t steps);

#endif  /* __GRAPHICS_BOX_H_ */

M ansilib/inc/graphics/shapes.h => ansilib/inc/graphics/shapes.h +1 -1
@@ 5,6 5,6 @@

# include <graphics/common.h>

void draw_rectangle(uint32_t width, uint32_t height);
void draw_rectangle(uint32_t width, uint32_t height, char ch);

#endif  /* __GRAPHICS_SHAPES_H_ */

M ansilib/src/graphics/box.c => ansilib/src/graphics/box.c +34 -1
@@ 1,3 1,5 @@
#include <stdint.h>

#include <ansi/cursor.h>
#include <graphics/box.h>
#include <graphics/shapes.h>


@@ 7,7 9,38 @@ void draw(const box_t *box)
    switch ( box->type ) {
    case RECTANGLE:
        move_cursor(box->origin);
        draw_rectangle(box->width, box->height);
        draw_rectangle(box->width, box->height, '#');
        break;
    }
}

void erase(const box_t *box)
{
    switch ( box->type ) {
    case RECTANGLE:
        move_cursor(box->origin);
        draw_rectangle(box->width, box->height, ' ');
        break;
    }
}

void nudge(box_t *box, dir_t dir, int32_t steps)
{
    switch ( dir ) {
    case UP:
        box->origin.y -= steps;
        break;

    case DOWN:
        box->origin.y += steps;
        break;

    case LEFT:
        box->origin.x -= steps;
        break;

    case RIGHT:
        box->origin.x += steps;
        break;
    }
}

M ansilib/src/graphics/shapes.c => ansilib/src/graphics/shapes.c +5 -5
@@ 3,14 3,14 @@

#include <io.h>

void draw_rectangle(uint32_t width, uint32_t height)
void draw_rectangle(uint32_t width, uint32_t height, char ch)
{
    /* Top edge */
    for (uint32_t i = 0; i < width; i++) {
        /* TODO: This character should be an argument such as
         *       `border_char`. Also, let's not forget about
         *       `border_width` */
        printmsg("#");
        printmsg("%c", ch);
    }

    for (uint32_t i = 0; i < height - 2; i++) {


@@ 24,11 24,11 @@ void draw_rectangle(uint32_t width, uint32_t height)
        nudge_cursor(DOWN, 1);

        /* Left edges */
        printmsg("#");
        printmsg("%c", ch);

        /* Right edges */
        nudge_cursor(RIGHT, width - 2);
        printmsg("#");
        printmsg("%c", ch);
    }

    /* Get to the next line */


@@ 37,6 37,6 @@ void draw_rectangle(uint32_t width, uint32_t height)

    /* Bottom edge */
    for (uint32_t i = 0; i < width; i++) {
        printmsg("#");
        printmsg("%c", ch);
    }
}

M src/main.c => src/main.c +21 -11
@@ 83,21 83,31 @@ int main(void)
    move_cursor(orig_cursor_pos);

    while ( 1 ) {

        /* Read a byte from the standard input */
        while ( read(STDIN_FILENO, &c, 1) < 1 );

        if ( iscntrl(c) ) {
            /* Control characters are non-printable,
             * hence don't even try to print them */
            printf("%d\r\n", c);

        } else {
            /* print character ascii values and
             * show the characters as well */
            printf("%d ('%c')\r\n", c, c);

        if ( !iscntrl(c) ) {
            if ( c == 'q' ) break;
            else if ( c == 'h' || c == 'j' || c == 'k' || c == 'l' ) {
                orig_cursor_pos = get_cursor_pos();
                erase(&rects[0]);
                switch ( c ) {
                case 'h':
                    nudge(&rects[0], LEFT, 1);
                    break;
                case 'j':
                    nudge(&rects[0], DOWN, 1);
                    break;
                case 'k':
                    nudge(&rects[0], UP, 1);
                    break;
                case 'l':
                    nudge(&rects[0], RIGHT, 1);
                    break;
                }
                draw(&rects[0]);
                move_cursor(orig_cursor_pos);
            }
        }
    }