#ifndef FRAMEBUFFER_H_ #define FRAMEBUFFER_H_ #include /* uint8_t */ #include /* wchar_t */ #include /* ssize_t */ /* Globals for the terminal size, set by term_update_size() */ extern size_t term_width, term_height; extern struct termios initial_terminal_state; /* Represents a 24 bit colour, 8 bit per channel */ typedef struct Colour_s { uint8_t r, g, b; } Colour; /* Basic colours */ #define C_BLACK (Colour){0, 0, 0} #define C_WHITE (Colour){255, 255, 255} #define C_RED (Colour){255, 0, 0} #define C_GREEN (Colour){0, 255, 0} #define C_BLUE (Colour){0, 0, 255} #define C_YELLOW (Colour){255, 255, 0} #define C_CYAN (Colour){0, 255, 255} #define C_MAGENTA (Colour){255, 0, 255} /* One 'pixel' of the image, AKA one character in the terminal. * For a solid colour pixel, set ch to a space and change the bg colour */ typedef struct Pixel_s { Colour fg, bg; wchar_t ch; /* the char of the pixel */ } Pixel; /* Solid colour pixels */ #define P_BLACK (Pixel){C_BLACK, C_BLACK, ' '} #define P_WHITE (Pixel){C_WHITE, C_WHITE, ' '} #define P_RED (Pixel){C_RED, C_RED, '4'} #define P_GREEN (Pixel){C_GREEN, C_GREEN, ' '} #define P_BLUE (Pixel){C_BLUE, C_BLUE, ' '} #define P_YELLOW (Pixel){C_YELLOW, C_YELLOW, ' '} #define P_CYAN (Pixel){C_CYAN, C_CYAN, ' '} #define P_MAGENTA (Pixel){C_MAGENTA, C_MAGENTA, ' '} typedef struct Framebuffer_s { Pixel **fb; /* Adressed as fb[row][col] or fb[x][y] */ size_t width, height; char *print_buf; /* is of size (42 * width * height) * 42 is ANSI 24-bit fg and bg + one wchar_t */ } Framebuffer; /* Check if fb == NULL in case malloc() fails */ Framebuffer fb_new(size_t width, size_t height); void fb_free(Framebuffer *fb); /* Fills fb with pixel */ void fb_fill(Framebuffer fb, Pixel pixel); /* Fills fb with black */ #define fb_clear(fb) fb_fill(fb, P_BLACK) /* Puts a pixel onto fb in a specified position */ void fb_put(Framebuffer fb, size_t row, size_t col, Pixel pixel); /* Initializes the terminal and sets TERM_WIDTH and TERM_HEIGHT */ void term_init(void); /* Undoes everything fb_init() did */ void term_cleanup(void); /* Updates term_width and term_height */ void term_update_size(void); /* Prints fb to the screen */ void fb_print(Framebuffer fb); static inline void set_fg(Colour col); static inline void set_bg(Colour col); #define get_input(c) read(STDIN_FILENO, &c, 1) #endif /* FRAMEBUFFER_H_ */