Adiciona libraries toml/fs, metadados .toml e build por library

- tinyblocks-toml: lexer + parser TOML (include/lexeme.h, include/ast.h,
  src/lexer.c, src/parser.c) e bloco Toml.Get.
- tinyblocks-fs: categoria "Arquivos" com FS.Open/Read/Write/Append/Tell/Close.
- library.toml em cada library com metadados (categoria, cor, descricao) e
  receita de build, sem precisar embutir isso no .so.
- Makefiles proprios por library com build/ local; Makefile raiz delega.
- Palco (tb_stage) movido para o core; pen deixa de depender do editor, o
  que torna os .so arrastaveis.
- Editor linka com -rdynamic para resolver simbolos do core em .so via dlopen.
- Testes: test-toml (parser) e test-plugin (dlopen de .so standalone).
This commit is contained in:
mguinhos committed 2026-10-20 19:38:58 -03:00
1 parent 90d47bf8a0
commit dc23b92d15
48 files changed
+2405 -744

No files matched your search

+159
View File
@@ -0,0 +1,159 @@
/*
* TinyBlocks - tb_stage.c
* Palco logico (canvas retido) do runtime: uma tela de 480x360 (origem no
* centro, y para cima, como no Scratch) onde blocos de desenho escrevem.
*
* Os desenhos ficam numa lista retida do runtime, entao qualquer library
* (como a Caneta) pode desenhar sem depender do editor. A UI chama
* tb_stage_set_view + tb_stage_render para exibir.
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tinyblocks/tb_stage.h"
#define STAGE_W 480.f
#define STAGE_H 360.f
#define STAGE_MAX_CMDS 4000
#define STAGE_TEXT_FS 14.f
typedef enum { SC_RECT, SC_CIRCLE, SC_LINE, SC_TEXT } StageCmdKind;
typedef struct {
StageCmdKind kind;
float a, b, c, d; /* geometria em coordenadas do palco */
float size; /* espessura da caneta */
float col[3];
char text[96];
} StageCmd;
typedef struct {
StageCmd* cmds;
int n, cap;
float bg[3];
float pen[3];
float pen_size;
float view_w, view_h;
} Stage;
static Stage s_stage = {
NULL, 0, 0,
{1.f, 1.f, 1.f},
{76/255.f, 151/255.f, 1.f},
2.f, STAGE_W, STAGE_H
};
static float chan(float v) { return v < 0 ? 0.f : v > 255 ? 1.f : v / 255.f; }
static StageCmd* push(StageCmdKind kind) {
Stage* s = &s_stage;
if (s->n >= STAGE_MAX_CMDS) return NULL;
if (s->n >= s->cap) {
int nc = s->cap ? s->cap * 2 : 64;
StageCmd* p = (StageCmd*)realloc(s->cmds, (size_t)nc * sizeof(StageCmd));
if (!p) return NULL;
s->cmds = p; s->cap = nc;
}
StageCmd* c = &s->cmds[s->n++];
memset(c, 0, sizeof(*c));
c->kind = kind;
memcpy(c->col, s->pen, sizeof(c->col));
c->size = s->pen_size;
return c;
}
void tb_stage_clear(void) {
s_stage.n = 0;
}
void tb_stage_set_background(float r, float g, float b) {
s_stage.bg[0] = chan(r); s_stage.bg[1] = chan(g); s_stage.bg[2] = chan(b);
}
void tb_stage_set_pen(float r, float g, float b) {
s_stage.pen[0] = chan(r); s_stage.pen[1] = chan(g); s_stage.pen[2] = chan(b);
}
void tb_stage_set_pen_size(float size) {
s_stage.pen_size = size < 1.f ? 1.f : size;
}
void tb_stage_line(float x1, float y1, float x2, float y2) {
StageCmd* c = push(SC_LINE);
if (c) { c->a = x1; c->b = y1; c->c = x2; c->d = y2; }
}
void tb_stage_rect(float x, float y, float w, float h) {
StageCmd* c = push(SC_RECT);
if (c) { c->a = x; c->b = y; c->c = w; c->d = h; }
}
void tb_stage_circle(float x, float y, float radius) {
StageCmd* c = push(SC_CIRCLE);
if (c) { c->a = x; c->b = y; c->c = radius; }
}
void tb_stage_text(const char* text, float x, float y) {
StageCmd* c = push(SC_TEXT);
if (c) {
snprintf(c->text, sizeof(c->text), "%s", text ? text : "");
c->a = x; c->b = y;
}
}
void tb_stage_set_view(float w, float h) {
if (w > 0 && h > 0) { s_stage.view_w = w; s_stage.view_h = h; }
}
void tb_stage_reset(void) {
free(s_stage.cmds);
s_stage.cmds = NULL; s_stage.n = 0; s_stage.cap = 0;
s_stage.bg[0] = s_stage.bg[1] = s_stage.bg[2] = 1.f;
s_stage.pen[0] = 76/255.f; s_stage.pen[1] = 151/255.f; s_stage.pen[2] = 1.f;
s_stage.pen_size = 2.f;
s_stage.view_w = STAGE_W; s_stage.view_h = STAGE_H;
}
bool tb_stage_render(TinyBlocks_RenderableBlock* rb) {
if (!rb) return false;
Stage* s = &s_stage;
float sc = s->view_w / STAGE_W < s->view_h / STAGE_H ? s->view_w / STAGE_W : s->view_h / STAGE_H;
float ox = (s->view_w - STAGE_W*sc) * 0.5f, oy = (s->view_h - STAGE_H*sc) * 0.5f;
#define SX(x) (ox + ((x) + STAGE_W*0.5f) * sc)
#define SY(y) (oy + (STAGE_H*0.5f - (y)) * sc)
if (ox > 0.5f || oy > 0.5f)
tb_draw_fill_rect(rb, 0, 0, s->view_w, s->view_h, 0.12f, 0.12f, 0.14f, 1.f);
tb_draw_fill_rect(rb, ox, oy, STAGE_W*sc, STAGE_H*sc, s->bg[0], s->bg[1], s->bg[2], 1.f);
tb_draw_set_clip(rb, ox, oy, STAGE_W*sc, STAGE_H*sc);
for (int i = 0; i < s->n && rb->count < TB_DRAW_CMD_CAP - 2; i++) {
const StageCmd* c = &s->cmds[i];
float r = c->col[0], g = c->col[1], b = c->col[2];
switch (c->kind) {
case SC_RECT:
tb_draw_fill_rect(rb, SX(c->a - c->c*0.5f), SY(c->b + c->d*0.5f),
c->c*sc, c->d*sc, r, g, b, 1.f);
break;
case SC_CIRCLE:
tb_draw_fill_circle(rb, SX(c->a), SY(c->b), c->c*sc, r, g, b, 1.f);
break;
case SC_LINE:
tb_draw_stroke_line(rb, SX(c->a), SY(c->b), SX(c->c), SY(c->d),
c->size*sc, r, g, b, 1.f);
break;
case SC_TEXT: {
float fs = STAGE_TEXT_FS * sc;
tb_draw_text(rb, SX(c->a), SY(c->b) + fs*0.36f, c->text, fs, r, g, b, 1.f);
break;
}
}
}
tb_draw_clear_clip(rb);
#undef SX
#undef SY
return true;
}