diff --git a/CMakeLists.txt b/CMakeLists.txt index a5e8dcd..785bc60 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,4 +24,6 @@ enable_testing() add_subdirectory(externals) # PROJECT add_subdirectory(source) +# +add_subdirectory(test) diff --git a/includes/tart.h b/includes/tart.h index efc151f..0f1559c 100644 --- a/includes/tart.h +++ b/includes/tart.h @@ -1,46 +1,105 @@ #ifndef TART_H #define TART_H +#include +#define TART_UTF8 #ifdef __cplusplus extern "C" { #endif -#include "../source/term.h" -#include -// #========================================================================# -// | PreacherDHM:TART -// | -// | Tarts stands for Terminal Art. Tart is a terminal renderer that uses a | -// | render buffer like system that takes csprites or character sprites and | -// | displays them in the terminal. The render buffer consists of cells and | -// | each cell consists of -// | - Color -// | - Rendered Character -// | - Reset -// | In toaltol around 19 to 24 bytes. This alows for complete controle | -// | over each cell. -// #========================================================================# - -#ifdef TART_RGB_COLORS -#define NULL_CELL (struct tart_cell){{0,0,0},{0,0,0},0,0,0} -#define TART_CELL_DATA_SIZE 32 // todo add number -typedef char tart_display[2]; -#else -#define NULL_CELL (struct tart_cell){0,0,0,' '} -#define TART_CELL_DATA_SIZE 20 typedef char tart_display; + + + +typedef unsigned int TART_FLAGS; +typedef unsigned char tart_byte; +typedef unsigned short tart_id; + + +#ifdef TART_UTF8 +#include +#undef t_char +typedef wchar_t t_char; +#else +typedef char t_char; #endif + + +typedef struct { + tart_byte r; + tart_byte g; + tart_byte b; +} tart_color; + +#ifndef TART_PALET_SIZE +#define TART_PALET_SIZE 32 +#endif + +typedef struct { + tart_color forground; + tart_color background; +} tart_pallet; + +typedef struct { + int x; + int y; +} tart_vec2; + +#ifdef TART_UTF8 +#define TART_CELL_SIZE 36 * 2 +#else +#define TART_CELL_SIZE 26 + 19 +#endif +typedef unsigned int tart_uint; +typedef unsigned int tart_style; +typedef char tart_cell[TART_CELL_SIZE]; + + +typedef struct { + tart_cell** buffer; + tart_vec2 curser; + tart_vec2 jump; + tart_uint bufferSize; + int fd; +}tart_buffer; + +/* Tart Window + * + * The tart window will have the window size and all of the buffers. + **/ +typedef struct { + tart_pallet pallet[TART_PALET_SIZE]; + tart_vec2 homePosition; + tart_vec2 windowSize; + tart_vec2 terminalSize; + tart_buffer buffer; + const char* name; +}tart_window ; + + #define TART_OK 0 +#define TART_NG -1 +#define TART_SET_FLAG(f,x) f |= (x) -#define TART_STYLE_BOLD 1 -#define TART_STYLE_DIM 2 -#define TART_STYLE_ITALIC 3 -#define TART_STYLE_UNDERLINE 4 -#define TART_STYLE_BLINKING 5 -#define TART_STYLE_INVERSE 7 -#define TART_STYLE_HIDDEN 8 -#define TART_STYLE_STRIKETHROUGH 9 +#define TART_STYLE_NONE 0 +#define TART_STYLE_BOLD 1 << 0 +#define TART_STYLE_DIM 1 << 1 +#define TART_STYLE_ITALIC 1 << 2 +#define TART_STYLE_UNDERLINE 1 << 3 +#define TART_STYLE_BLINKING 1 << 4 +#define TART_STYLE_INVERSE 1 << 5 +#define TART_STYLE_HIDDEN 1 << 6 +#define TART_STYLE_STRIKETHROUGH 1 << 7 + +#define TART_STYLE_DRAW_BOLD 1 +#define TART_STYLE_DRAW_DIM 2 +#define TART_STYLE_DRAW_ITALIC 3 +#define TART_STYLE_DRAW_UNDERLINE 4 +#define TART_STYLE_DRAW_BLINKING 5 +#define TART_STYLE_DRAW_INVERSE 7 +#define TART_STYLE_DRAW_HIDDEN 8 +#define TART_STYLE_DRAW_STRIKETHROUGH 9 #define TART_COLOR_BLACK_FOREGROUND 30 #define TART_COLOR_RED_FOREGROUND 31 @@ -80,152 +139,58 @@ typedef char tart_display; #define TART_COLOR_BRIGHT_CYAN_BACKGROUND 106 #define TART_COLOR_BRIGHT_WHITE_BACKGROUND 107 -typedef unsigned char tart_byte; -typedef unsigned short tart_id; +#define T "" +#define TC(x) x -struct tart_vec2 { - unsigned short x,y; -}; - -struct tart_rgb { - tart_byte r,g,b; -}; - -/* Tart Cell -* -* This holds a rgb for the foreground and the background. -* Includeing the display character. -* -*/ -#ifdef TART_RGB_COLORS -struct tart_cell { - struct tart_rgb foreground; - struct tart_rgb background; - tart_byte style; - char display; -}; -#else +#define t_strlen strlen +#define t_strcmp strcmp +#define t_strcpy strcpy +#define t_strncpy strncpy +#define t_snprintf snprintf +#define t_sprintf sprintf +#define t_vsprintf vsprintf +#define t_vsnprintf vsnprintf #ifdef TART_UTF8 -struct tart_cell { - tart_byte foreground; - tart_byte background; - tart_byte style; - char[2] display; -}; -#endif -struct tart_cell { - tart_byte foreground; - tart_byte background; - tart_byte style; - char display; - struct tart_vec2 screen_pos; -}; +#undef T +#define T L"" +#undef TC +#define TC(x) L##x +#undef t_strlen +#define t_strlen wcslen +#undef t_strcmp +#define t_strcmp wcscmp +#undef t_snprintf +#define t_snprintf swprintf +#undef t_sprintf +#define t_sprintf wprintf +#undef t_vsprintf +#define t_vsprintf vswprintf +#undef t_vsnprintf +#define t_vsnprintf vswprintf +#undef t_strcpy +#define t_strcpy wcscpy +#undef t_strncpy +#define t_strncpy wcncpy #endif -struct tart_cstring { - struct tart_cell* data; - long size; -}; +int tart_create_window(tart_window* w, tart_vec2 ws, const char* title); +int tart_init(tart_window* w); +int tart_close(tart_window* w); +void tart_disable_cusor(); +void tart_enable_cusor(); +int tart_draw(tart_window* w); -struct tart_csprite { - struct tart_cell* data; - struct tart_vec2* position; - struct tart_vec2 bounds; - long size; -}; - -/* Tart Buffer - * - * The Buffer is a contner that holds all of the cells for that buffer. - * Allso containes the size of the buffer. - * - * ........................width............... - * ..........<-------------------------------->. - * ........^ @################################@. - * ........| #................................#. - * ........| #................................#. - * height--| #.............Buffer.............#. - * ........| #................................#. - * ........| #................................#. - * ........V @################################@. - */ -struct tart_buffer { - unsigned int cell_count; - tart_byte layer; - tart_id id; - struct tart_vec2 size; - struct tart_vec2 position; - struct tart_cell* cells; - unsigned int current_idx; -}; - -/* Tart Window - * - * The tart window will have the window size and all of the buffers. - **/ -struct tart_window { - struct tart_buffer buffers[0xFF+1]; - tart_byte buffer_count; - struct tart_vec2 size; - char* data; - int data_count; -}; - -struct tart_window tart_create_window(); -void tart_destroy_window(struct tart_window* window); -struct tart_buffer tart_create_buffer(tart_id id, struct tart_vec2 size, struct tart_vec2 position); -tart_byte tart_restore_window(struct tart_window*); - -#ifdef TART_RGB_COLORS -struct tart_cell tart_create_cell(char display, tart_byte style, struct tart_rgb foreground, struct tart_rgb background); -#else -struct tart_cell tart_create_cell(char display, tart_byte style, tart_byte foreground, tart_byte background); -#endif - -/* tart_add_buffer - * - * This adds a buffer to the window. - * - * tart_byte tart_add_buffer(struct tart_window*, struct struct tart_buffer*) - */ -tart_byte tart_add_buffer(struct tart_window*, struct tart_buffer); -/* tart_remove_buffer - * - * This will remove the buffer using the *tart_id*. - */ -tart_byte tart_remove_buffer(struct tart_window*, tart_id); -/* tart_set_buffer - * - * This will replace the the buffer at the index *(tart_byte)*. - */ -tart_byte tart_set_buffer(struct tart_window*, struct tart_buffer, tart_byte); +int tart_jump(tart_window* w, tart_vec2 pos); +int tart_move_cursor(tart_window* w, tart_vec2 pos); +int tart_insert_cell(tart_window* w, tart_byte p, t_char c); +int tart_printf(tart_window* w,tart_byte p, const t_char* c, ...); +int tart_box(tart_window* w, tart_byte bp, tart_byte fp, const t_char* c, + tart_vec2 fpos, + tart_vec2 spos); +int tart_line(tart_window* w, tart_byte p, const t_char* c, tart_vec2 fpos, + tart_vec2 spos); -struct tart_buffer* tart_get_buffer(struct tart_window*, tart_byte); - -struct tart_cell* tart_get_cell(struct tart_buffer*, int); -struct tart_cell tart_set_cell(struct tart_buffer*, struct tart_cell,int); - -tart_byte tart_draw_window(struct tart_window*, char*); - -tart_byte tart_add_cells_to_buffer(struct tart_buffer*, struct tart_cell*); - -// rendering -// Resering positionial cells. -struct tart_cstring tart_cstring(char* string, long, struct tart_cell type); -tart_byte tart_cstring_free(struct tart_cstring*); -struct tart_cstring tart_cstring_append(struct tart_cstring*, struct tart_cstring*); - -struct tart_csprite tart_csprite(struct tart_cell*, struct tart_vec2*, long); -tart_byte tart_csprite_free(struct tart_csprite*); - -tart_byte tart_draw_cell_position(struct tart_buffer*, struct tart_cell, struct tart_vec2); -tart_byte tart_draw_cstring_position(struct tart_buffer*, struct tart_cstring, struct tart_vec2); -tart_byte tart_draw_csprite_position(struct tart_buffer*, struct tart_csprite, struct tart_vec2); -/* - * tart_restore_buffer sets the buffer to NULL_CELL. - * */ -tart_byte tart_restore_buffer(struct tart_buffer*); #ifdef __cplusplus } diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 8b1763c..1f9dcd5 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -5,8 +5,6 @@ set( CMAKE_C_STANDARD_REQUIRED ON) set(LIB_SOURCES - term.c - term.h tart.c ) diff --git a/source/tart.c b/source/tart.c index 25f1aa8..c2102a7 100644 --- a/source/tart.c +++ b/source/tart.c @@ -1,255 +1,255 @@ #include "../includes/tart.h" +#include #include #include -#include -#include "term.h" +#include +#include +#include +#include +#include -struct tart_cell tart_test() { -#ifdef TART_RGB_COLORS - return (struct tart_cell){{0,0,0}, {0,0,0}, 0, '&',0}; + +#define BUFFER_AT_POS(W) (W->windowSize.x * W->buffer.curser.y) + W->buffer.curser.x +#define BUFFER_LINE_SIZE (w->windowSize.x) * TART_CELL_SIZE + sizeof(tart_cell) +#define BUFFER_SIZE (w->windowSize.x * w->windowSize.y) +#define BUFFER_BUFFER_SIZE BUFFER_LINE_SIZE * w->windowSize.y + +tart_vec2 GetTerminalSize(tart_window* w) { + struct winsize win; + ioctl(STDOUT_FILENO, TIOCGWINSZ, &win); + return (tart_vec2){win.ws_col,win.ws_row}; +}; + +int tart_create_buffer(tart_window* w) { + w->buffer.buffer = (tart_cell**)malloc(w->windowSize.y * sizeof(tart_cell*)); + if(w->buffer.buffer == NULL) { + return TART_NG; + } + for(int i = 0; i < w->windowSize.y; i++) { + + int x = BUFFER_LINE_SIZE; + w->buffer.buffer[i] = (tart_cell*)malloc(x); + bzero(w->buffer.buffer[i], (BUFFER_LINE_SIZE)); + } + tart_cell ce; + bzero(ce, TART_CELL_SIZE); + snprintf(ce,TART_CELL_SIZE, +#ifdef TART_UTF8 + "\e[38;2;%03hhu;%03hhu;%03hhum%lc\e[0m", #else - return (struct tart_cell){196,105,0,'&'}; + "\e[38;2;%03hhu;%03hhu;%03hhum%c\e[0m", #endif -} -struct tart_window tart_create_window() { - struct tart_window window; - window.buffer_count = 0; - for(int i = 0; i < 0xFF; i++) { - window.buffers[i] = tart_create_buffer(0, (struct tart_vec2){0,0}, - (struct tart_vec2){0,0}); - } - window.size = term_current_size(); + w->pallet[0].forground.r, + w->pallet[0].forground.g, + w->pallet[0].forground.b, +#ifdef TART_UTF8 + L' ' +#else + ' ' +#endif + ); + for(int i = 0; i < w->windowSize.y; i++ ) { + for(int j = 0; j < w->windowSize.x + 1; j++) { + memcpy(w->buffer.buffer[i][j], ce, sizeof(tart_cell)); + } + //t_snprintf(w->buffer.buffer[i][w->windowSize.x], + // TART_CELL_SIZE, + // TC("\e[1B\e[%dC"), + // w->windowSize.x); + } - int dataSize = (window.size.x * window.size.y * sizeof(char) * - TART_CELL_DATA_SIZE) + 100; + + w->buffer.curser = (tart_vec2){0,0}; - window.data = malloc(dataSize); - for(int g = 0; g < dataSize; g++) { - window.data[g] = '\0'; - } - window.data_count = dataSize; - printf("\033[?1049h"); - return window; + //bzero(w->buffer.buffer[w->windowSize.y], (BUFFER_LINE_SIZE)); + + //t_snprintf(w->buffer.buffer[w->windowSize.y][w->windowSize.x], + // TART_CELL_SIZE, + // TC("\e[n") + // ); + return TART_OK; } -void tart_destroy_window(struct tart_window* winodw) { - printf("\033[?1049l"); +void tart_free_buffer(tart_window* w) { + free(w->buffer.buffer); } -tart_byte tart_restore_window(struct tart_window* window) { - for (int i = 0; i < window->data_count; i++) { - window->data[i] = '\0'; - } +int tart_create_window(tart_window* w, tart_vec2 ws, const char* title) { + w->windowSize.x = GetTerminalSize(w).x - 1; + w->windowSize.y = GetTerminalSize(w).y; + w->terminalSize = GetTerminalSize(w); + w->homePosition = (tart_vec2){ + (w->terminalSize.x / 2) - (w->windowSize.x / 2), + (w->terminalSize.x / 2) - (w->windowSize.x / 2) + }; + + tart_create_buffer(w); + return TART_OK; } -struct tart_buffer tart_create_buffer(tart_id id, struct tart_vec2 size, - struct tart_vec2 position) { - unsigned int cell_count = size.x * size.y; - - struct tart_cell* cells = (struct tart_cell*)malloc(cell_count * sizeof(struct tart_cell)); - - for (int i = 0;i < cell_count;i++) { - cells[i] = NULL_CELL; - } - - unsigned int data_count = (size.x*size.y) * TART_CELL_DATA_SIZE; - struct tart_buffer buf = {cell_count,0,id,size,position,cells}; // -NOTE- dose not set the layer - buf.cell_count = cell_count; - return buf; -} -#ifdef TART_RGB_COLORS -struct tart_cell tart_create_cell(char display, tart_byte style, struct tart_rgb foreground, struct tart_rgb background) { - struct tart_cell b = {foreground, background, style, display,0}; - return b; -} +int flush_buffer(tart_window* w) { + tart_cell ce; + bzero(ce, TART_CELL_SIZE); + snprintf(ce,TART_CELL_SIZE, +#ifdef TART_UTF8 + "\e[48;2;%03hhu;%03hhu;%03hhum\e[38;2;%03hhu;%03hhu;%03hhum%lc\e[0m", #else - -struct tart_cell tart_create_cell(char display, tart_byte style, - tart_byte foreground, tart_byte background) { - return (struct tart_cell){foreground,background,style,display}; -} + "\e[38;2;%03hhu;%03hhu;%03hhum%c\e[0m", #endif -tart_byte tart_add_buffer(struct tart_window* window, - struct tart_buffer buffer) { - if(window->buffer_count <= 0xFF) { - window->buffers[window->buffer_count] = buffer; - window->buffer_count++; - return window->buffer_count; - } - return 0; -} -tart_byte tart_set_buffer(struct tart_window* window, struct tart_buffer buffer, tart_byte layer) { - if(layer <= 0xFF) { - window->buffers[layer] = buffer; - return layer; + w->pallet[0].background.r, + w->pallet[0].background.g, + w->pallet[0].background.b, + w->pallet[0].forground.r, + w->pallet[0].forground.g, + w->pallet[0].forground.b, +#ifdef TART_UTF8 + L' ' +#else + ' ' +#endif + ); + for(int i = 0; i < w->windowSize.y; i++) { + for(int j = 0; j < w->windowSize.x ; j++) { + bzero(w->buffer.buffer[i][j], sizeof(tart_cell)); + memcpy(w->buffer.buffer[i][j], ce, sizeof(tart_cell)); + } + } return 0; } -struct tart_buffer* tart_get_buffer(struct tart_window* window, tart_byte layer) { - return &window->buffers[layer]; +void flush_palets(tart_window* w) { + for(int i = 0; i < TART_PALET_SIZE; i++) { + w->pallet[i].background.r = 0; + w->pallet[i].background.g = 0; + w->pallet[i].background.b = 0; + w->pallet[i].forground.r = 254; + w->pallet[i].forground.g = 254; + w->pallet[i].forground.b = 254; + } } -struct tart_cell* tart_get_cell(struct tart_buffer* buffer, int idx) { - return &buffer->cells[idx]; +int tart_init(tart_window* w) { + int y = 0; + int x = 0; + w->buffer.jump = (tart_vec2){0,0}; + flush_buffer(w); + flush_palets(w); + + //t_snprintf(ce,TART_CELL_SIZE, TC("\e[%d;%dh"), + // (w->terminalSize.x/2)-(w->windowSize.x/2), + // (w->terminalSize.y/2)-(w->windowSize.y/2)); + + //write(STDOUT_FILENO, ce, TART_CELL_SIZE); + + write(STDOUT_FILENO, "\e[?1049h", 8); + write(STDOUT_FILENO, "\e[H", 3); + write(STDOUT_FILENO, "\e[s", 3); + + return TART_OK; } -struct tart_cell tart_set_cell(struct tart_buffer* buffer, struct tart_cell cell,int idx) { - struct tart_cell c = buffer->cells[idx]; - buffer->cells[idx] = cell; - return c; +void tart_disable_cusor() { + write(STDOUT_FILENO, "\e[?25l", 8); +} +void tart_enable_cusor() { + write(STDOUT_FILENO, "\e[?25h", 8); } -tart_byte tart_draw_window(struct tart_window * window, char* rend_buffer) { - int offset = 0; - int i = 0; - int bufferWidth = 0; - for (int b = 0;b < 0xFF; b++) { - if(window->buffers[b].cell_count == 0) - continue; - //char bufferSetPre[16]; - //int bufferSetSize = sprintf(bufferSetPre, "\033[%d;%dH", window->buffers[b].position.y, window->buffers[b].position.x); - //for(int preIdx = 0; preIdx < bufferSetSize; preIdx++) { - // //window->data[(y*window->buffers[b].size.x) + offset] = movePre[preIdx]; - // window->data[offset] = bufferSetPre[preIdx]; - // offset++; - //} - // offset++; - //bufferWidth = window->buffers[b].size.x; - for (int y = 0; y < window->buffers[b].current_idx; y++) { - // add data to window c buffer. - struct tart_cell cell = window->buffers[b].cells[y]; - // puts cell drawing data in pre - - - char pre[TART_CELL_DATA_SIZE+20]; - int size = sprintf(pre, "\033[%d;%dH\033[0m\033[%d;%d;%dm%c\033[0;0m", - cell.screen_pos.y+1,cell.screen_pos.x+1, - (int)cell.style, cell.foreground, - cell.background, cell.display) -1; - for(int preIdx = 0; preIdx < size; preIdx++) { - window->data[offset] = pre[preIdx]; - offset++; - } - #ifdef TART_RGB_COLORS - char pre[TART_CELL_DATA_SIZE+4]; - int size = sprintf(pre, "\033[0m\033[38;2;%d;%d;%dm\033[0m\033[48;2;%d;%d;%dm%\033[%dmc\033[0;0m", - // TODO add rgb - (int)cell.style, cell.foreground, - cell.background, cell.display) -1; - #endif - - - - - offset++; - - - // add cursor move command. - i++; +int tart_close(tart_window* w) { + write(STDOUT_FILENO, "\e[?1049l", 8); + //for(int i = 0; i < w->windowSize.y; i++) { + // free(w->buffer.buffer[i]); + //} + //free(w->buffer.buffer); + return TART_OK; +} +int tart_draw(tart_window* w) { + for(int i = 0; i < w->windowSize.y; i++) { + for(int j = 0; j < w->windowSize.x + 1; j++) { + write(STDOUT_FILENO, + w->buffer.buffer[i][j], + TART_CELL_SIZE + ); } } - fwrite(window->data, sizeof(char), window->data_count, stdout); + flush_buffer(w); + write(STDOUT_FILENO, "\e[u", strlen("\e[u")); return TART_OK; -}; +} -tart_byte tart_add_cells_to_buffer(struct tart_buffer* buffer, struct tart_cell* cells) { - for(int i = 0; i < buffer->cell_count; i++) { - buffer->cells[i] = cells[i]; - } +int tart_jump(tart_window* w, tart_vec2 pos) { + w->buffer.jump = pos; + w->buffer.curser = pos; return TART_OK; } +int tart_move_cursor(tart_window* w, tart_vec2 pos) { -struct tart_cstring tart_cstring(char *string, long size, struct tart_cell type){ - struct tart_cstring str = {0,size}; - str.data = malloc(size * sizeof(struct tart_cell)); - for(int i = 0; i < size; i++) { - str.data[i] = type; - str.data[i].display = string[i]; - } - return str; -} - -tart_byte tart_cstring_free(struct tart_cstring* string) { - free(string->data); - string->size = 0; - return TART_OK; -} - -struct tart_cstring tart_cstring_append(struct tart_cstring* lhs, struct tart_cstring* rhs) { - struct tart_cstring ret = {0,0}; - ret.size = lhs->size + rhs->size; - ret.data = malloc(ret.size * sizeof(struct tart_cell)); - for(int i = 0; i < lhs->size; i++) { - ret.data[i] = lhs->data[i]; - } - for(int i = 0; i < rhs->size; i++) { - ret.data[lhs->size + i] = rhs->data[i]; - } - return ret; -} - -struct tart_csprite tart_csprite(struct tart_cell* cells, struct tart_vec2* positions, long size) { - - struct tart_csprite sprite = {0,0,0,size}; - sprite.data = cells; - sprite.position = positions; - for(int i = 0; i < size; i++) { - - if(positions[i].x > sprite.bounds.x) { - sprite.bounds.x = positions[i].x; - } - - if(positions[i].y > sprite.bounds.y) { - sprite.bounds.y = positions[i].y; + if(pos.x >= w->windowSize.x) { + for(int i = pos.x; i >= w->windowSize.x; i /= w->windowSize.x) { + w->buffer.curser.x = i; + w->buffer.curser.y++; } + } else { + w->buffer.curser.x = pos.x; } - sprite.size = size; - return sprite; -}; - -tart_byte tart_csprite_free(struct tart_csprite* sprite) { - sprite->bounds.x = 0; - sprite->bounds.y = 0; - sprite->size = 0; - free(sprite->data); - free(sprite->position); + if(pos.y >= w->windowSize.y - 1) { + for(int i = pos.y; i >= w->windowSize.y - 1; i /= w->windowSize.y) { + w->buffer.curser.y = i; + } + }else { + w->buffer.curser.y = pos.y; + } + return TART_OK; } - -tart_byte tart_draw_cell_position(struct tart_buffer * buffer, struct tart_cell cell, struct tart_vec2 position) { - if(buffer->size.x > position.x && buffer->size.y > position.y) { - cell.screen_pos = position; - buffer->cells[buffer->current_idx] = cell; - buffer->current_idx++; +int tart_insert_cell(tart_window* w, tart_byte p, t_char c) { + if(c == TC('\n')) { + tart_move_cursor(w, (tart_vec2){w->buffer.jump.x, w->buffer.curser.y + 1}); + return TART_OK; } + bzero(w->buffer.buffer[w->buffer.curser.y][w->buffer.curser.x], TART_CELL_SIZE); + tart_cell ce; + bzero(ce, TART_CELL_SIZE); + snprintf(ce,TART_CELL_SIZE, +#ifdef TART_UTF8 + "\e[48;2;%03hhu;%03hhu;%03hhum\e[38;2;%03hhu;%03hhu;%03hhum%lc\e[0m", +#else + "\e[48;2;%03hhu;%03hhu;%03hhum\e[38;2;%03hhu;%03hhu;%03hhum%c\e[0m", +#endif + w->pallet[p].background.r, + w->pallet[p].background.g, + w->pallet[p].background.b, + w->pallet[p].forground.r, + w->pallet[p].forground.g, + w->pallet[p].forground.b, + c + ); + + memcpy(w->buffer.buffer[w->buffer.curser.y][w->buffer.curser.x], ce, TART_CELL_SIZE); + tart_move_cursor(w, (tart_vec2){w->buffer.curser.x + 1, w->buffer.curser.y}); return TART_OK; } - -tart_byte tart_draw_cstring_position(struct tart_buffer *buffer, struct tart_cstring string, struct tart_vec2 position) { - struct tart_vec2 pos = position; - for(int i = 0; i < string.size; i++) { - tart_draw_cell_position(buffer, string.data[i], pos); - pos.x++; +int tart_printf(tart_window* w,tart_byte p, const t_char* c, ...) { + va_list args; + va_start(args, c); + t_char tmp[1024]; + t_vsnprintf(tmp,1024, c, args); + for(int i = 0; i < t_strlen(tmp); i++) { + tart_insert_cell(w, p, tmp[i]); } + va_end(args); return TART_OK; } - -tart_byte tart_draw_csprite_position(struct tart_buffer * buffer, struct tart_csprite sprite, struct tart_vec2 basePosition) { - for(int i = 0; i < sprite.size; i++) { - struct tart_vec2 position = {sprite.position[i].x + basePosition.x, sprite.position[i].y + basePosition.y}; - tart_draw_cell_position(buffer, sprite.data[i], position); - } +int tart_box(tart_window* w, tart_byte bp, tart_byte fp, const t_char* c, + tart_vec2 fpos, + tart_vec2 spos) { return TART_OK; } - -tart_byte tart_restore_buffer(struct tart_buffer *buffer) { - for (int i = 0; i < buffer->cell_count; i++) { - buffer->cells[i] = NULL_CELL; - buffer->current_idx = 0; - } +int tart_line(tart_window* w, tart_byte p, const t_char* c, tart_vec2 fpos, + tart_vec2 spos) { return TART_OK; } diff --git a/source/term.c b/source/term.c deleted file mode 100644 index f7cead9..0000000 --- a/source/term.c +++ /dev/null @@ -1,208 +0,0 @@ -#include "term.h" -#include - -unsigned char haveread = 0; -unsigned char __state__ = 0; -char __term_input_buffer__[8] = {' ',' ',' ',' ',' ',' ',' ',' '}; -void set_char_push_back(char t) { - char tmp1; - char hold1; - char hold2; - __state__ = __state__ + 1; - hold1 = __term_input_buffer__[0]; - for(int i = 0; i < 8 - 1; i++) { - hold2 = __term_input_buffer__[i+1]; - __term_input_buffer__[i+1] = hold1; - hold1 = hold2; - - } - __term_input_buffer__[0] = t; -} - -// if windows is defined. - -#if defined(_WIN64) || defined(_WIN32) -#include -#include - - - - -struct tart_vec2 term_current_size() { - struct tart_vec2 ret; - CONSOLE_SCREEN_BUFFER_INFO csbi; - GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); - unsigned int rows = (csbi.srWindow.Right - csbi.srWindow.Left + 1); - unsigned int cols = (csbi.srWindow.Bottom - csbi.srWindow.Top + 1); - - unsigned short max_short = 0XFFFF; - if(rows < max_short && cols < max_short) { - - ret = (struct tart_vec2){(unsigned short) rows ,(unsigned short) cols}; - } - - return ret; -} - -HANDLE __term_input_thread; -int __term_input_thread_stop; - -DWORD WINAPI input_threaded_func(void* data) { - while(__term_input_thread_stop == 1) { - if(_kbhit()) - set_char_push_back(_getch()); - } - return 0; -} - - -int term_threaded_input_init() { - __term_input_thread_stop = 1; - __term_input_thread = CreateThread(NULL,0,input_threaded_func,NULL, 0, NULL); - return 0; -} -int term_threaded_input_stop() { - __term_input_thread_stop = 0; - WaitForSingleObject(__term_input_thread, INFINITE); - return 0; -} - -#else -#include -#include -#include -#include -#include -#include -struct tart_vec2 term_current_size() { - struct tart_vec2 ret; - - struct winsize w; - - - ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); - - unsigned int rows = w.ws_col; - unsigned int cols = w.ws_row; - - unsigned short max_short = 0XFFFF; - if(rows < max_short && cols < max_short) { - - ret = (struct tart_vec2){(unsigned short) rows ,(unsigned short) cols}; - } - - return ret; -} - -struct termios old, current; - -void init_termios(int args) { - tcgetattr(0, &old); - - current = old; - - current.c_lflag &= ~ICANON; - - if(args == 0x01) { - //current.c_lflag ^ ECHO; - } else { - //current.c_lflag &= ~ECHO; - current.c_lflag ^= ECHO; - //current.c_lflag |= ECHO; - } - - tcsetattr(0,0, ¤t); -} - -void reset_termios(void) { - - tcsetattr(0, TCSANOW, &old); -} - -char term_getch() { - char tmp; - init_termios(0x00); - tmp = getchar(); - reset_termios(); - return tmp; -} - -char term_getche() { - char tmp; - init_termios(0x01); - tmp = getchar(); - reset_termios(); - return tmp; -} - - -pthread_t input_thread; - - -void* input_threaded_func(void* vargp) { - int myid = getpid(); - while(1) { - set_char_push_back(term_getch()); - usleep(1000); - } - return NULL; -} - -int term_threaded_input_stop() { - pthread_cancel(input_thread); - reset_termios(); - return 1; -} - -int term_threaded_input_init() { - - pthread_create(&input_thread, NULL, input_threaded_func, NULL); - return 1; -} - -unsigned char term_key_pressed() { - if( __state__ > 0 ) { - return 1; - } - return 0; -} - -void term_handled_key() { - if( __state__ > 0 ) { - __state__ = 0; - } -} - -void term_end_of_key() { - if(__state__ > 0 && haveread > 200) { - //__state__ = 0; - } -} - -void term_start_read() { - haveread += 1; -} - -#endif - -char term_tinputi(int idx){ - if(idx < 8) { - return __term_input_buffer__[idx]; - } - return 0x0; -} - -char* term_tinputb() { - return __term_input_buffer__; -} - -char term_tinput() { - return __term_input_buffer__[0]; -} -void term_disable_cursor() { - printf("\033[?25l", stdout); -} - -void term_enable_cursor() { - printf("\033[?25h", stdout); -} diff --git a/source/term.h b/source/term.h deleted file mode 100644 index b0345ad..0000000 --- a/source/term.h +++ /dev/null @@ -1,37 +0,0 @@ -// #========================================================================# -// | PREACHERDHM:TERM | -// | | -// | term renders all of the data from TART. Term takes a tart_window and | -// | renders all of the buffers and cells inside the that window object. | -// | There is a draw function and a draw buffer function. | -// | | -// | Term is just a way to output the data from tart. | -// #========================================================================# -#ifndef TERM_H -#define TERM_H - -#define NOREAD 0 - -#include "tart.h" - -struct tart_vec2 term_current_size(); - -char term_getch(); -char term_getche(); - - -// tinput is threaded input -int term_threaded_input_init(); -int term_threaded_input_stop(); -char term_tinputi(int idx); -char* term_tinputb(); -char term_tinput(); -unsigned char term_key_pressed(); -void term_handled_key(); -void term_end_of_key(); -void term_start_read(); - -void term_disable_cursor(); -void term_enable_cursor(); - -#endif diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..8443856 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,30 @@ +cmake_minimum_required(VERSION 3.20.0) +project(testing) +# CPP +set( CMAKE_CXX_STANDARD 11) +set( CMAKE_CXX_STANDARD_REQUIRED ON) +# C +set( CMAKE_C_STANDARD 11) +set( CMAKE_C_STANDARD_REQUIRED ON) + + +#set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin/libs) +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) + +set( CMAKE_COLOR_MAKEFILE ON) +set( CMAKE_COLOR_DIAGNOSTICS ON) + +set( CMAKE_EXPORT_COMPILE_COMMANDS ON) +set( SOURCES + main.c +) +add_executable( + Testing + ${SOURCES} +) + +target_link_libraries( + Testing + TartLib +) diff --git a/test/main.c b/test/main.c new file mode 100644 index 0000000..6d77cc7 --- /dev/null +++ b/test/main.c @@ -0,0 +1,121 @@ +#define TART_UTF8 +#include "../includes/tart.h" +#include +#include +#include +#include + +#define jump tart_move_cursor + +int e = 1; + +void aOnExit() { + //printf("shutting Down"); + e = 0; +} + + +void draw_button(tart_window *w,const t_char* label, tart_vec2 position) { + + jump(w, (tart_vec2){position.x,position.y + 0}); + tart_printf(w, 1, T"┏━━━"); + int size = t_strlen(label); + for(int i = 0; i < size; i++ ) { + tart_printf(w, 1, T"━"); + } + tart_printf(w, 1, T"━━━┓"); + jump(w, (tart_vec2){position.x,position.y + 1}); + tart_printf(w, 1, T"┃ %ls ┃", label); + jump(w, (tart_vec2){position.x,position.y + 2}); + tart_printf(w, 1, T"┗━━━"); + for(int i = 0; i < size; i++ ) { + tart_printf(w, 1, T"━"); + } + tart_printf(w, 1, T"━━━┛"); +} + +int main(int argc, char *argv[]) +{ + signal(SIGINT, aOnExit); + setlocale(LC_CTYPE, "en_US.UTF-8"); + + tart_window w; + int color0 = 0; + int color1 = 0; + int color2 = 0; + tart_create_window(&w, (tart_vec2){80,40}, "testing"); + tart_init(&w); + tart_disable_cusor(); + + w.pallet[0].background.r = 0; + w.pallet[0].background.g = 0; + w.pallet[0].background.b = 0; + w.pallet[0].forground.r = 0; + w.pallet[0].forground.g = 0; + w.pallet[0].forground.b = 0; + + w.pallet[1].forground.r = 0xff; + w.pallet[1].forground.g = 0x00; + w.pallet[1].forground.b = 0xcc; + + w.pallet[2].forground.r = 0x3b; + w.pallet[2].forground.g = 0x3b; + w.pallet[2].forground.b = 0x3b; + jump(&w, (tart_vec2){1,1}); + jump(&w, (tart_vec2){1,2}); + int x = 0; + while(e != 0) { + + tart_draw(&w); + + draw_button(&w, T"This is really cool!", (tart_vec2){5,5}); + tart_jump(&w, (tart_vec2){3,10}); + tart_printf(&w,1, T"this is\na new line test\nyay"); + tart_jump(&w, (tart_vec2){3+ 20,10}); + tart_printf(&w,1, T"this is\na new line test\nyay"); + tart_jump(&w, (tart_vec2){3,14}); + tart_printf(&w,2, T"this is\na new line test\nyay"); + if(w.pallet[1].forground.r == 254) { + color0 = 0; + } + if(w.pallet[1].forground.r == 0) { + color0 = 1; + } + if(w.pallet[1].forground.g == 254) { + color1 = 0; + } + if(w.pallet[1].forground.g == 0) { + color1 = 1; + } + if(w.pallet[1].forground.b == 254) { + color2 = 0; + } + if(w.pallet[1].forground.b == 0) { + color2 = 1; + } + + if(color0 == 1) { + w.pallet[1].forground.r++; + }else { + w.pallet[1].forground.r--; + } + if(color1 == 1) { + w.pallet[1].forground.g++; + }else { + w.pallet[1].forground.g--; + } + if(color2 == 1) { + w.pallet[1].forground.b++; + }else { + w.pallet[1].forground.b--; + } + + } + + + tart_close(&w); + tart_enable_cusor(); + //printf("\e[?1049l"); + printf("done\n"); + return 0; +}