#include "../includes/tart.h" #include #include #include #include "term.h" struct tart_cell tart_test() { #ifdef TART_RGB_COLORS return (struct tart_cell){{0,0,0}, {0,0,0}, 0, '&',0}; #else return (struct tart_cell){196,105,0,'&'}; #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(); window.data = malloc((window.size.x * window.size.y * sizeof(char) * TART_CELL_DATA_SIZE)+ 100 ); for(int g = 0; g < (window.size.x * window.size.y * sizeof(char) * TART_CELL_DATA_SIZE)+ 100; g++) { window.data[g] = '\0'; } return window; } 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((size.x * size.y) * 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 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; } #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}; } #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; } return 0; } struct tart_buffer* tart_get_buffer(struct tart_window* window, tart_byte layer) { return &window->buffers[layer]; } struct tart_cell* tart_get_cell(struct tart_buffer* buffer, int idx) { return &buffer->cells[idx]; } 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; } tart_byte tart_draw_window(struct tart_window * window, char* rend_buffer) { int offset = 0; int i = 0; for (int b = 0;b < 0xFF; b++) { if(window->buffers[b].cell_count == 0) continue; for (int y = 0; y < window->buffers[b].size.y; y++) { for (int x = 0; x < window->buffers[b].size.x; x++) { // add data to window c buffer. struct tart_cell cell = window->buffers[b].cells[(y*window->buffers[b].size.x) + x]; char pre[20]; int size = sprintf(pre, "\033[%d;%d;%dm%c\033[0;0m", (int)cell.style, cell.foreground, cell.background, cell.display); for(int preIdx = 0; preIdx < size; preIdx++) { window->data[(y*window->buffers[b].size.x) + x + offset] = pre[preIdx]; offset++; } } window->data[((y)*window->buffers[b].size.x) + offset + window->buffers[b].size.x] = '\n'; offset++; // add cursor move command. i++; } } fwrite(window->data, sizeof(char), (window->size.x * window->size.y * sizeof(char))+ 100 , stdout); 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]; } return TART_OK; }