Estado atual do TinyBlocks: editor estilo Scratch + graphics como interface

Snapshot do projeto antes de implementar o diálogo "Criar um Bloco":
- runtime C block-based (src/, include/)
- libraries: graphics (interface), sdl2 (backend), editor (estilo Scratch),
  python, mcp
- exemplos (hello, sdl2, editor, vector, json, customappearance, python, mcp)
- Makefile com alvo help e build do editor

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
mguinhosandClaude committed 2026-10-20 19:10:43 -03:00
commit 4950ad6006
49 files changed
+10596

No files matched your search

+43
View File
@@ -0,0 +1,43 @@
CC := gcc
BUILD := ../../build
SDL2_CFLAGS := $(shell sdl2-config --cflags)
SDL2_LIBS := $(shell sdl2-config --libs)
SDL2TTF_CFLAGS:= $(shell pkg-config --cflags SDL2_ttf 2>/dev/null)
SDL2TTF_LIBS := $(shell pkg-config --libs SDL2_ttf 2>/dev/null)
ifeq ($(SDL2TTF_LIBS),)
$(error SDL2_ttf não encontrado. Instale: sudo apt install libsdl2-ttf-dev)
endif
CFLAGS := -std=c11 -Wall -Wextra -pedantic -fvisibility=hidden \
-D_POSIX_C_SOURCE=200809L \
-I../../include \
-Iinclude \
$(SDL2_CFLAGS) $(SDL2TTF_CFLAGS)
LDFLAGS := $(SDL2_LIBS) $(SDL2TTF_LIBS) -lm
OBJ := $(BUILD)/tb_editor.o
LIB_A := $(BUILD)/libtinyblocks-editor.a
LIB_SO := $(BUILD)/libtinyblocks-editor.so
.PHONY: all static shared clean
all: static shared
static: $(LIB_A)
shared: $(LIB_SO)
$(OBJ): src/tb_editor.c include/tb_editor.h
@mkdir -p $(BUILD)
$(CC) $(CFLAGS) -fPIC -c $< -o $@
$(LIB_A): $(OBJ)
ar rcs $@ $^
$(LIB_SO): $(OBJ)
$(CC) -shared -o $@ $^ $(LDFLAGS)
clean:
$(RM) $(OBJ) $(LIB_A) $(LIB_SO)
@@ -0,0 +1,37 @@
/*
* TinyBlocks — tb_editor.h
* Editor visual block-based (estilo Scratch) implementado como LibraryBlock.
*
* O editor é o próprio bloco "Editor.Scene": não abre janela nem processa
* eventos SDL2 diretamente. Em vez disso:
* - implementa on_draw, produzindo um TinyBlocks_RenderableBlock
* - lê o estado de input/mouse/teclado a partir de um bloco SDL2.Events
* conectado ao seu input "events"
* - deve ser conectado ao input "scene" de um bloco SDL2.App, que é quem
* de fato cria a janela, roda o loop e interpreta o RenderableBlock.
*
* Uso:
* tb_library_register(rt, tb_editor_get_library());
* tb_library_register(rt, tb_sdl2_get_library());
*
* TinyBlocks_Block* events = tb_block_create(rt, tb_type_block_find(rt,"SDL2.Events"));
* TinyBlocks_Block* scene = tb_block_create(rt, tb_type_block_find(rt,"Editor.Scene"));
* tb_block_set_default(scene, 0, tb_value_block(events));
*
* TinyBlocks_Block* app = tb_block_create(rt, tb_type_block_find(rt,"SDL2.App"));
* tb_block_set_default(app, 3, tb_value_block(scene));
* tb_block_set_default(app, 4, tb_value_block(events));
* tb_block_eval(app, rt); // roda o loop principal — bloqueia até fechar
*/
#ifndef TINYBLOCKS_EDITOR_H
#define TINYBLOCKS_EDITOR_H
#include <tinyblocks/tinyblocks.h>
TB_API const TinyBlocks_LibraryDescriptor* tb_editor_get_library(void);
TB_API const TinyBlocks_LibraryDescriptor* tinyblocks_get_library(void);
/* Enfileira um .so para ser carregado automaticamente ao abrir o editor */
TB_API void tb_editor_preload(const char* path);
#endif /* TINYBLOCKS_EDITOR_H */
File diff suppressed because it is too large. Load diff
+259
View File
@@ -0,0 +1,259 @@
/*
* TinyBlocks — tb_stage.c
* O Palco do editor: uma tela lógica de 480x360 (origem no centro, y para
* cima, como no Scratch) onde os blocos Stage.* desenham. É um componente de
* UI: o editor o embute no painel direito (chamando seu on_draw) ou o entrega
* como cena de um Graphics.Window próprio ("destacar palco").
*
* Os desenhos ficam numa lista retida e são re-emitidos a cada on_draw,
* escalados para o tamanho da área (letterbox mantendo 4:3).
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tb_graphics.h>
#include "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;
bool full_warned;
} Stage;
static TinyBlocks_Block* s_current = NULL;
static Stage* stage_of(TinyBlocks_Block* b) { return b ? *(Stage**)b->userdata : NULL; }
TinyBlocks_Block* tb_stage_current(void) { return s_current; }
void tb_stage_set_view(TinyBlocks_Block* stage, float w, float h) {
Stage* s = stage_of(stage);
if (s && w > 0 && h > 0) { s->view_w = w; s->view_h = h; }
}
/* ══════════════════════════════════════════════════════════════════════════
* Editor.Stage
* ══════════════════════════════════════════════════════════════════════════ */
static bool stage_instance_init(TinyBlocks_Block* self, TinyBlocks* rt) {
(void)rt;
Stage* s = (Stage*)calloc(1, sizeof(Stage));
if (!s) return false;
s->bg[0] = s->bg[1] = s->bg[2] = 1.f;
s->pen[0] = 76/255.f; s->pen[1] = 151/255.f; s->pen[2] = 1.f;
s->pen_size = 2.f;
s->view_w = STAGE_W; s->view_h = STAGE_H;
*(Stage**)self->userdata = s;
s_current = self;
return true;
}
static void stage_instance_deinit(TinyBlocks_Block* self, TinyBlocks* rt) {
(void)rt;
Stage* s = stage_of(self);
if (s) { free(s->cmds); free(s); }
*(Stage**)self->userdata = NULL;
if (s_current == self) s_current = NULL;
}
/* input 0 (opcional): Graphics.Input da janela que mostra o palco — dá o tamanho da vista */
static void eval_stage(TinyBlocks_Block* self, TinyBlocks* rt) {
self->dirty = false;
TinyBlocks_Value v = tb_block_get_input(self, rt, 0);
TinyBlocks_Block* in = v.kind == TB_VALUE_BLOCK ? v.as.block : NULL;
if (!in || in->output_count < GFX_IN_COUNT) return;
TinyBlocks_Value w = in->outputs[GFX_IN_WIN_W], h = in->outputs[GFX_IN_WIN_H];
if (w.kind == TB_VALUE_INTEGER && h.kind == TB_VALUE_INTEGER)
tb_stage_set_view(self, (float)w.as.integer, (float)h.as.integer);
}
static bool stage_on_draw(TinyBlocks_Block* self, TinyBlocks* rt, TinyBlocks_RenderableBlock** dst) {
(void)rt;
Stage* s = stage_of(self);
if (!s) return false;
TinyBlocks_RenderableBlock* rb = tb_renderable_acquire();
if (!rb) return false;
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
*dst = rb;
return true;
}
/* ══════════════════════════════════════════════════════════════════════════
* Blocos Stage.* — desenham no palco atual
* ══════════════════════════════════════════════════════════════════════════ */
static float num(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
if (v.kind == TB_VALUE_REAL) return (float)v.as.real;
if (v.kind == TB_VALUE_INTEGER) return (float)v.as.integer;
if (v.kind == TB_VALUE_TEXT && v.as.text) return (float)strtod(v.as.text, NULL);
return 0.f;
}
static float chan(float v) { return v < 0 ? 0.f : v > 255 ? 1.f : v / 255.f; }
static StageCmd* push(TinyBlocks* rt, StageCmdKind kind) {
Stage* s = stage_of(s_current);
if (!s) { tinyblocks_set_error(rt, "nenhum palco (Editor.Stage) criado"); return NULL; }
if (s->n >= STAGE_MAX_CMDS) {
if (!s->full_warned) tinyblocks_set_error(rt, "palco cheio (%d desenhos) — use \"apague tudo\"", STAGE_MAX_CMDS);
s->full_warned = true;
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;
}
static void eval_clear(TinyBlocks_Block* self, TinyBlocks* rt) {
(void)rt;
Stage* s = stage_of(s_current);
if (s) { s->n = 0; s->full_warned = false; }
self->dirty = false;
}
static void eval_background(TinyBlocks_Block* self, TinyBlocks* rt) {
Stage* s = stage_of(s_current);
if (s) { s->bg[0] = chan(num(self, rt, 0)); s->bg[1] = chan(num(self, rt, 1)); s->bg[2] = chan(num(self, rt, 2)); }
self->dirty = false;
}
static void eval_pen_color(TinyBlocks_Block* self, TinyBlocks* rt) {
Stage* s = stage_of(s_current);
if (s) { s->pen[0] = chan(num(self, rt, 0)); s->pen[1] = chan(num(self, rt, 1)); s->pen[2] = chan(num(self, rt, 2)); }
self->dirty = false;
}
static void eval_pen_size(TinyBlocks_Block* self, TinyBlocks* rt) {
Stage* s = stage_of(s_current);
float v = num(self, rt, 0);
if (s) s->pen_size = v < 1.f ? 1.f : v;
self->dirty = false;
}
static void eval_line(TinyBlocks_Block* self, TinyBlocks* rt) {
StageCmd* c = push(rt, SC_LINE);
if (c) { c->a = num(self, rt, 0); c->b = num(self, rt, 1); c->c = num(self, rt, 2); c->d = num(self, rt, 3); }
self->dirty = false;
}
static void eval_rect(TinyBlocks_Block* self, TinyBlocks* rt) {
StageCmd* c = push(rt, SC_RECT);
if (c) { c->a = num(self, rt, 0); c->b = num(self, rt, 1); c->c = num(self, rt, 2); c->d = num(self, rt, 3); }
self->dirty = false;
}
static void eval_circle(TinyBlocks_Block* self, TinyBlocks* rt) {
StageCmd* c = push(rt, SC_CIRCLE);
if (c) { c->a = num(self, rt, 0); c->b = num(self, rt, 1); c->c = num(self, rt, 2); }
self->dirty = false;
}
static void eval_text(TinyBlocks_Block* self, TinyBlocks* rt) {
TinyBlocks_Value v = tb_block_get_input(self, rt, 0);
char buf[96] = "";
if (v.kind == TB_VALUE_TEXT && v.as.text) snprintf(buf, sizeof(buf), "%s", v.as.text);
else if (v.kind == TB_VALUE_INTEGER) snprintf(buf, sizeof(buf), "%lld", (long long)v.as.integer);
else if (v.kind == TB_VALUE_REAL) snprintf(buf, sizeof(buf), "%g", v.as.real);
StageCmd* c = push(rt, SC_TEXT);
if (c) { memcpy(c->text, buf, sizeof(c->text)); c->a = num(self, rt, 1); c->b = num(self, rt, 2); }
self->dirty = false;
}
/* ══════════════════════════════════════════════════════════════════════════
* Registro
* ══════════════════════════════════════════════════════════════════════════ */
static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
const TinyBlocks_SlotDesc* in, uint32_t ni,
void (*fn)(TinyBlocks_Block*, TinyBlocks*)) {
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, name, in, ni, NULL, 0, fn);
if (!t) return false;
tb_library_add(lib, &t->base);
return true;
}
bool tb_stage_register(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
static const TinyBlocks_SlotDesc stage_in[] = {{"entrada", TB_VALUE_BLOCK}};
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, "Editor.Stage", stage_in, 1, NULL, 0, eval_stage);
if (!t) return false;
tb_type_block_set_instance_hooks(t, sizeof(Stage*), stage_instance_init, stage_instance_deinit);
t->on_draw = stage_on_draw;
tb_library_add(lib, &t->base);
static const TinyBlocks_SlotDesc rgb[] = {{"r", TB_VALUE_REAL}, {"g", TB_VALUE_REAL}, {"b", TB_VALUE_REAL}};
static const TinyBlocks_SlotDesc size[] = {{"tamanho", TB_VALUE_REAL}};
static const TinyBlocks_SlotDesc line[] = {{"x1", TB_VALUE_REAL}, {"y1", TB_VALUE_REAL},
{"x2", TB_VALUE_REAL}, {"y2", TB_VALUE_REAL}};
static const TinyBlocks_SlotDesc rect[] = {{"x", TB_VALUE_REAL}, {"y", TB_VALUE_REAL},
{"largura", TB_VALUE_REAL}, {"altura", TB_VALUE_REAL}};
static const TinyBlocks_SlotDesc circ[] = {{"x", TB_VALUE_REAL}, {"y", TB_VALUE_REAL}, {"raio", TB_VALUE_REAL}};
static const TinyBlocks_SlotDesc text[] = {{"texto", TB_VALUE_TEXT}, {"x", TB_VALUE_REAL}, {"y", TB_VALUE_REAL}};
return reg(rt, lib, "Stage.Clear", NULL, 0, eval_clear)
&& reg(rt, lib, "Stage.Background", rgb, 3, eval_background)
&& reg(rt, lib, "Stage.PenColor", rgb, 3, eval_pen_color)
&& reg(rt, lib, "Stage.PenSize", size, 1, eval_pen_size)
&& reg(rt, lib, "Stage.Line", line, 4, eval_line)
&& reg(rt, lib, "Stage.Rect", rect, 4, eval_rect)
&& reg(rt, lib, "Stage.Circle", circ, 3, eval_circle)
&& reg(rt, lib, "Stage.Text", text, 3, eval_text);
}
@@ -0,0 +1,19 @@
/*
* TinyBlocks — tb_stage.h
* Palco do editor (Editor.Stage) e blocos de desenho Stage.*.
*/
#ifndef TINYBLOCKS_EDITOR_STAGE_H
#define TINYBLOCKS_EDITOR_STAGE_H
#include <tinyblocks/tinyblocks.h>
/* Registra Editor.Stage e os blocos Stage.* na library do editor */
bool tb_stage_register(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib);
/* Tamanho da área onde o palco será desenhado (on_draw usa 0..w × 0..h) */
void tb_stage_set_view(TinyBlocks_Block* stage, float w, float h);
/* Palco onde os blocos Stage.* desenham (o último criado) */
TinyBlocks_Block* tb_stage_current(void);
#endif /* TINYBLOCKS_EDITOR_STAGE_H */