~aritra1911/ansi_anim

35ce9f02859baa98c8086ef99b8c8395a0b51e5d — Aritra Sarkar 3 years ago c7a732d
Render rectangle
M .gitignore => .gitignore +2 -2
@@ 1,2 1,2 @@
*.o
anim
bin/
obj/

M Makefile => Makefile +17 -2
@@ 7,8 7,15 @@ CFLAGS = $(INC) -Wall -Wextra -pedantic -std=c99
CPPFLAGS =
LDFLAGS =

SRCS = src/main.c
OBJS = $(SRCS:src/%.c=obj/%.o)
SRCS = lib/src/graphics/common.c \
       lib/src/graphics/shapes.c \
       lib/src/ansi/cursor.c \
       src/main.c

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

.PHONY: all clean



@@ 18,6 25,14 @@ bin/anim: $(OBJS)
	@mkdir -p bin
	$(CC) $(LDFLAGS) -o $@ $^

obj/graphics/%.o: lib/src/graphics/%.c
	@mkdir -p obj/graphics
	$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<

obj/ansi/%.o: lib/src/ansi/%.c
	@mkdir -p obj/ansi
	$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<

obj/%.o: src/%.c
	@mkdir -p obj
	$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<

A lib/inc/ansi/common.h => lib/inc/ansi/common.h +6 -0
@@ 0,0 1,6 @@
#ifndef __ANSI_COMMON_H_
# define __ANSI_COMMON_H_

# define CSI "\x1b["

#endif  /* __ANSI_COMMON_H_ */

A lib/inc/ansi/cursor.h => lib/inc/ansi/cursor.h +11 -0
@@ 0,0 1,11 @@
#ifndef __ANSI_CURSOR_H_
# define __ANSI_CURSOR_H_

# include <graphics/common.h>

typedef enum { LEFT, DOWN, UP, RIGHT } dir_t;

void move_cursor(point_t);
void nudge_cursor(dir_t, uint32_t step);

#endif  /* __ANSI_CURSOR_H_ */

A lib/inc/graphics/common.h => lib/inc/graphics/common.h +12 -0
@@ 0,0 1,12 @@
#ifndef __GRAPHICS_COMMON_H_
# define __GRAPHICS_COMMON_H_

#include <stdint.h>

typedef struct {
    int32_t x, y;
} point_t;

point_t point(int32_t x, int32_t y);

#endif  /* __GRAPHICS_COMMON_H_ */

A lib/inc/graphics/shapes.h => lib/inc/graphics/shapes.h +6 -0
@@ 0,0 1,6 @@
#ifndef __GRAPHICS_SHAPES_H_
# define __GRAPHICS_SHAPES_H_

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

#endif  /* __GRAPHICS_SHAPES_H_ */

A lib/src/ansi/cursor.c => lib/src/ansi/cursor.c +42 -0
@@ 0,0 1,42 @@
#include <unistd.h>
#include <stdio.h>
#include <string.h>

#include <ansi/common.h>
#include <ansi/cursor.h>

void move_cursor(point_t pos)
{
    char buf[16];

    /*
     * ESC[{line};{column}H
     * or
     * ESC[{line};{column}f
     */

    sprintf(buf, CSI"%i;%iH", /* line */ pos.y, /* column */ pos.x);
    write(STDOUT_FILENO, (void *) buf, strlen(buf));
}

void nudge_cursor(dir_t direction, uint32_t step)
{
    char buf[16], end_ch;

    /*
     * ESC[#A   moves cursor up # lines
     * ESC[#B   moves cursor down # lines
     * ESC[#C   moves cursor right # columns
     * ESC[#D   moves cursor left # columns
     */

    switch ( direction ) {
    case UP:    end_ch = 'A'; break;
    case DOWN:  end_ch = 'B'; break;
    case RIGHT: end_ch = 'C'; break;
    case LEFT:  end_ch = 'D'; break;
    }

    sprintf(buf, CSI"%i%c", step, end_ch);
    write(STDOUT_FILENO, (void *) buf, strlen(buf));
}

A lib/src/graphics/common.c => lib/src/graphics/common.c +9 -0
@@ 0,0 1,9 @@
#include <stdint.h>

#include <graphics/common.h>

point_t point(int32_t x, int32_t y)
{
    point_t temp = { x, y };
    return temp;
}

A lib/src/graphics/shapes.c => lib/src/graphics/shapes.c +37 -0
@@ 0,0 1,37 @@
#include <unistd.h>

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

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

    /* Top Edge */
    for (uint32_t i = 0; i < width; i++) {
        write(STDOUT_FILENO, (void *) "#", 1);
    }

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

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

        /* print left and right edges */
        write(STDOUT_FILENO, (void *) "#", 1);
        for (uint32_t j = 0; j < width - 2; j++) {
            write(STDOUT_FILENO, (void *) " ", 1);
        }
        write(STDOUT_FILENO, (void *) "#", 1);
    }

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

    /* Bottom Edge */
    for (uint32_t i = 0; i < width; i++) {
        write(STDOUT_FILENO, (void *) "#", 1);
    }
}

M src/main.c => src/main.c +13 -0
@@ 4,6 4,9 @@
#include <termios.h>
#include <unistd.h>

#include <graphics/common.h>
#include <graphics/shapes.h>

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

static inline void enable_raw_mode(void);


@@ 57,6 60,16 @@ int main(void)

    enable_raw_mode();

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

    draw_rectangle((point_t) { 10, 2 }, 3, 4);

    while ( 1 ) {

        /* Read a byte from the standard input */