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:
commit
4950ad6006
49 files changed
+10596
No files matched your search
@@ -0,0 +1,190 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "tinyblocks/tinyblocks.h"
|
||||
|
||||
/* ══════════════════════════════════════════════════════════════════════════
|
||||
* TinyBlocks_VectorData — buffer de comandos ref-counted
|
||||
* ══════════════════════════════════════════════════════════════════════════ */
|
||||
TinyBlocks_VectorData* tb_vector_data_create(void) {
|
||||
TinyBlocks_VectorData* v = malloc(sizeof(TinyBlocks_VectorData));
|
||||
if (!v) return NULL;
|
||||
v->capacity = 16;
|
||||
v->count = 0;
|
||||
v->ref = 1;
|
||||
v->cmds = malloc(v->capacity * sizeof(TinyBlocks_VCmd));
|
||||
if (!v->cmds) { free(v); return NULL; }
|
||||
return v;
|
||||
}
|
||||
|
||||
void tb_vector_data_retain(TinyBlocks_VectorData* v) {
|
||||
if (v) v->ref++;
|
||||
}
|
||||
|
||||
void tb_vector_data_release(TinyBlocks_VectorData* v) {
|
||||
if (!v) return;
|
||||
if (--v->ref <= 0) { free(v->cmds); free(v); }
|
||||
}
|
||||
|
||||
TinyBlocks_VectorData* tb_vector_data_clone(const TinyBlocks_VectorData* src) {
|
||||
if (!src) return NULL;
|
||||
TinyBlocks_VectorData* v = tb_vector_data_create();
|
||||
if (!v) return NULL;
|
||||
for (uint32_t i = 0; i < src->count; i++)
|
||||
tb_vector_data_push(v, src->cmds[i]);
|
||||
return v;
|
||||
}
|
||||
|
||||
bool tb_vector_data_push(TinyBlocks_VectorData* v, TinyBlocks_VCmd cmd) {
|
||||
if (!v) return false;
|
||||
if (v->count >= v->capacity) {
|
||||
uint32_t nc = v->capacity * 2;
|
||||
TinyBlocks_VCmd* a = realloc(v->cmds, nc * sizeof(TinyBlocks_VCmd));
|
||||
if (!a) return false;
|
||||
v->cmds = a; v->capacity = nc;
|
||||
}
|
||||
v->cmds[v->count++] = cmd;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* ── Construtores de conveniência ────────────────────────────────────────── */
|
||||
#define PUSH0(v,k) tb_vector_data_push(v, (TinyBlocks_VCmd){(k), {0}})
|
||||
#define PUSH2(v,k,a,b) tb_vector_data_push(v, (TinyBlocks_VCmd){(k), {(a),(b)}})
|
||||
#define PUSH4(v,k,a,b,c,d) tb_vector_data_push(v,(TinyBlocks_VCmd){(k),{(a),(b),(c),(d)}})
|
||||
#define PUSH5(v,k,a,b,c,d,e) tb_vector_data_push(v,(TinyBlocks_VCmd){(k),{(a),(b),(c),(d),(e)}})
|
||||
|
||||
bool tb_vector_begin_path(TinyBlocks_VectorData* v) { return PUSH0(v, TB_VCMD_BEGIN_PATH); }
|
||||
bool tb_vector_move_to (TinyBlocks_VectorData* v, float x, float y) { return PUSH2(v, TB_VCMD_MOVE_TO, x,y); }
|
||||
bool tb_vector_line_to (TinyBlocks_VectorData* v, float x, float y) { return PUSH2(v, TB_VCMD_LINE_TO, x,y); }
|
||||
bool tb_vector_close_path(TinyBlocks_VectorData* v) { return PUSH0(v, TB_VCMD_CLOSE_PATH); }
|
||||
|
||||
bool tb_vector_stroke(TinyBlocks_VectorData* v, float r, float g, float b, float a, float w) {
|
||||
return PUSH5(v, TB_VCMD_STROKE, r, g, b, a, w);
|
||||
}
|
||||
bool tb_vector_fill(TinyBlocks_VectorData* v, float r, float g, float b, float a) {
|
||||
return PUSH4(v, TB_VCMD_FILL, r, g, b, a);
|
||||
}
|
||||
bool tb_vector_rect(TinyBlocks_VectorData* v, float x, float y, float w, float h) {
|
||||
return PUSH4(v, TB_VCMD_RECT, x, y, w, h);
|
||||
}
|
||||
bool tb_vector_circle(TinyBlocks_VectorData* v, float cx, float cy, float radius) {
|
||||
return PUSH5(v, TB_VCMD_CIRCLE, cx, cy, radius, 0, 0);
|
||||
}
|
||||
|
||||
#undef PUSH0
|
||||
#undef PUSH2
|
||||
#undef PUSH4
|
||||
#undef PUSH5
|
||||
|
||||
/* ── Value helpers ───────────────────────────────────────────────────────── */
|
||||
TinyBlocks_Value tb_value_vector(TinyBlocks_VectorData* v) {
|
||||
TinyBlocks_Value val;
|
||||
val.kind = TB_VALUE_VECTOR;
|
||||
val.as.vector = v;
|
||||
if (v) v->ref++; /* tb_value retém a referência */
|
||||
return val;
|
||||
}
|
||||
|
||||
TinyBlocks_VectorData* tb_value_as_vector(TinyBlocks_Value v) {
|
||||
return v.kind == TB_VALUE_VECTOR ? v.as.vector : NULL;
|
||||
}
|
||||
|
||||
/* ── RenderableBlock play ────────────────────────────────────────────────── */
|
||||
void tb_draw_ctx_play(TinyBlocks_RenderableBlock* rb, TinyBlocks_VectorData* v) {
|
||||
if (!rb || !v) return;
|
||||
float px = 0.f, py = 0.f; /* ponto atual do path */
|
||||
float sx = 0.f, sy = 0.f; /* início do path (close_path) */
|
||||
float fr = 1.f, fg = 1.f, fb = 1.f, fa = 1.f; /* cor de fill/stroke atual */
|
||||
for (uint32_t i = 0; i < v->count; i++) {
|
||||
TinyBlocks_VCmd* c = &v->cmds[i];
|
||||
switch (c->kind) {
|
||||
case TB_VCMD_BEGIN_PATH:
|
||||
break;
|
||||
case TB_VCMD_MOVE_TO:
|
||||
px = sx = c->args[0]; py = sy = c->args[1]; break;
|
||||
case TB_VCMD_LINE_TO:
|
||||
tb_draw_stroke_line(rb, px, py, c->args[0], c->args[1], 1.f, fr, fg, fb, fa);
|
||||
px = c->args[0]; py = c->args[1];
|
||||
break;
|
||||
case TB_VCMD_CLOSE_PATH:
|
||||
tb_draw_stroke_line(rb, px, py, sx, sy, 1.f, fr, fg, fb, fa);
|
||||
px = sx; py = sy;
|
||||
break;
|
||||
case TB_VCMD_STROKE:
|
||||
fr = c->args[0]; fg = c->args[1]; fb = c->args[2]; fa = c->args[3];
|
||||
break;
|
||||
case TB_VCMD_FILL:
|
||||
fr = c->args[0]; fg = c->args[1]; fb = c->args[2]; fa = c->args[3];
|
||||
break;
|
||||
case TB_VCMD_RECT:
|
||||
tb_draw_fill_rect(rb, c->args[0], c->args[1], c->args[2], c->args[3],
|
||||
fr, fg, fb, fa);
|
||||
break;
|
||||
case TB_VCMD_CIRCLE:
|
||||
tb_draw_fill_circle(rb, c->args[0], c->args[1], c->args[2], fr, fg, fb, fa);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ══════════════════════════════════════════════════════════════════════════
|
||||
* TinyBlocks_VectorBlock — block kind TB_KIND_VECTOR
|
||||
* ══════════════════════════════════════════════════════════════════════════ */
|
||||
static void vec_eval(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
(void)rt;
|
||||
TinyBlocks_VectorBlock* vb = (TinyBlocks_VectorBlock*)self;
|
||||
/* Coloca o VectorData no output[0] como valor (com retain) */
|
||||
if (self->output_count > 0)
|
||||
tb_block_set_output(self, 0, tb_value_vector(vb->data));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void vec_destroy(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
(void)rt;
|
||||
TinyBlocks_VectorBlock* vb = (TinyBlocks_VectorBlock*)self;
|
||||
tb_vector_data_release(vb->data);
|
||||
vb->data = NULL;
|
||||
}
|
||||
|
||||
static size_t vec_to_string(TinyBlocks_Block* self, TinyBlocks* rt,
|
||||
char* buf, size_t sz) {
|
||||
(void)rt;
|
||||
TinyBlocks_VectorBlock* vb = (TinyBlocks_VectorBlock*)self;
|
||||
return (size_t)snprintf(buf, sz, "[Vector %u cmds]",
|
||||
vb->data ? vb->data->count : 0u);
|
||||
}
|
||||
|
||||
static TinyBlocks_Block* vec_clone(const TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_VectorBlock* src = (TinyBlocks_VectorBlock*)self;
|
||||
TinyBlocks_VectorBlock* dst = tb_vector_block_create(rt);
|
||||
if (!dst) return NULL;
|
||||
if (src->data) {
|
||||
dst->data = tb_vector_data_clone(src->data);
|
||||
}
|
||||
return &dst->base;
|
||||
}
|
||||
|
||||
static const TinyBlocks_BlockVTable s_vec_vtable = {
|
||||
.eval = vec_eval,
|
||||
.destroy = vec_destroy,
|
||||
.to_string = vec_to_string,
|
||||
.clone = vec_clone,
|
||||
};
|
||||
|
||||
TinyBlocks_VectorBlock* tb_vector_block_create(TinyBlocks* rt) {
|
||||
TinyBlocks_VectorBlock* b = (TinyBlocks_VectorBlock*)tb_block_alloc_base(
|
||||
rt, TB_KIND_VECTOR, sizeof(TinyBlocks_VectorBlock),
|
||||
0, 1, &s_vec_vtable);
|
||||
if (!b) return NULL;
|
||||
b->data = tb_vector_data_create();
|
||||
return b;
|
||||
}
|
||||
|
||||
void tb_vector_block_set_data(TinyBlocks_VectorBlock* b, TinyBlocks_VectorData* v) {
|
||||
if (!b) return;
|
||||
tb_vector_data_release(b->data);
|
||||
b->data = v;
|
||||
if (v) v->ref++;
|
||||
b->base.dirty = true;
|
||||
}
|
||||
Reference in new issue
Block a user