~aritra1911/ansi_anim

23a7675d4d7d5b6d92e8947f65a8d2f1c84d408d — Aritra Sarkar 2 years ago f428295
Implement  and draw
M Makefile => Makefile +8 -5
@@ 7,18 7,21 @@ CFLAGS = $(INC) -Wall -Wextra -pedantic -g -std=c99
CPPFLAGS =
LDFLAGS =

SRCS = src/main.c \
       src/io.c \
SRCS = src/io.c \
       src/main.c \
       ansilib/src/ansi/cursor.c \
       ansilib/src/ansi/screen.c \
       ansilib/src/graphics/common.c \
       ansilib/src/graphics/box.c \
       ansilib/src/graphics/shapes.c

OBJS = obj/main.o \
       obj/io.o \
       obj/ansi/screen.o \

OBJS = obj/io.o \
       obj/main.o \
       obj/ansi/cursor.o \
       obj/ansi/screen.o \
       obj/graphics/common.o \
       obj/graphics/box.o \
       obj/graphics/shapes.o

.PHONY: all clean

A ansilib/inc/graphics/box.h => ansilib/inc/graphics/box.h +18 -0
@@ 0,0 1,18 @@
#ifndef __GRAPHICS_BOX_H_
# define __GRAPHICS_BOX_H_

# include <graphics/common.h>

typedef enum {
    RECTANGLE,
} shape_t;

typedef struct {
    shape_t type;
    point_t origin;
    uint32_t width, height;
} box_t;

void draw(const box_t *box);

#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(point_t pos, uint32_t width, uint32_t height);
void draw_rectangle(uint32_t width, uint32_t height);

#endif  /* __GRAPHICS_SHAPES_H_ */

A ansilib/src/graphics/box.c => ansilib/src/graphics/box.c +13 -0
@@ 0,0 1,13 @@
#include <ansi/cursor.h>
#include <graphics/box.h>
#include <graphics/shapes.h>

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

M ansilib/src/graphics/shapes.c => ansilib/src/graphics/shapes.c +8 -3
@@ 3,17 3,22 @@

#include <io.h>

void draw_rectangle(point_t pos, uint32_t width, uint32_t height)
void draw_rectangle(uint32_t width, uint32_t height)
{
    move_cursor(pos);

    /* 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("#");
    }

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

        /* TODO: Add fill character argument.
         *       If fill character is not '\0', we should fill the
         *       rectangle with that character. */

        /* Get to the next line */
        nudge_cursor(LEFT, width);
        nudge_cursor(DOWN, 1);

M src/main.c => src/main.c +13 -11
@@ 8,7 8,7 @@
#include <ansi/cursor.h>
#include <ansi/screen.h>
#include <graphics/common.h>
#include <graphics/shapes.h>
#include <graphics/box.h>

struct termios orig_termios;    /* Keeps the original attributes */



@@ 63,21 63,23 @@ int main(void)

    enable_raw_mode();

    /* TODO:
     * box_t objects[32];
     *
     * for (int i = 0; i < 32; i++) {
     *     draw(&object[i]);
     * }
     */

    /* print out screen size */
    point_t screen_size = get_screen_size();
    printf("LINES = %i  COLUMNS = %i\r\n", screen_size.y, screen_size.x);

    /* Draw a rectangle */
    /* Draw beautiful geometric rectangles of various sizes at various
     * places all over the screen. */
    box_t rects[] = {
        { RECTANGLE, { 20, 12 }, 12, 13 },
        { RECTANGLE, { 52,  7 },  7,  8 },
        { RECTANGLE, { 42, 30 }, 20, 16 },
        { RECTANGLE, { 72, 12 }, 32, 16 },
    };

    point_t orig_cursor_pos = get_cursor_pos();
    draw_rectangle((point_t) { 72, 12 }, 32, 16);
    for (int i = 0; i < 4; i++) {
        draw(&rects[i]);
    }
    move_cursor(orig_cursor_pos);

    while ( 1 ) {