Files
Tart/source/tart.c

256 lines
7.8 KiB
C

#include "../includes/tart.h"
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#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();
int dataSize = (window.size.x * window.size.y * sizeof(char) *
TART_CELL_DATA_SIZE) + 100;
window.data = malloc(dataSize);
for(int g = 0; g < dataSize; g++) {
window.data[g] = '\0';
}
window.data_count = dataSize;
printf("\033[?1049h");
return window;
}
void tart_destroy_window(struct tart_window* winodw) {
printf("\033[?1049l");
}
tart_byte tart_restore_window(struct tart_window* window) {
for (int i = 0; i < window->data_count; i++) {
window->data[i] = '\0';
}
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;
}
#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;
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++;
}
}
fwrite(window->data, sizeof(char), window->data_count, 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;
}
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;
}
}
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);
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++;
}
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++;
}
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);
}
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;
}
return TART_OK;
}