Files
tinyblocks/src/tb_renderable_block.c
T
mguinhosandClaude 4950ad6006 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>
2026-10-20 19:10:43 -03:00

166 lines
5.9 KiB
C

#define _POSIX_C_SOURCE 200809L
#include <string.h>
#include <stddef.h>
#include "tinyblocks/tb_renderable_block.h"
/* ── Pool estático — 4 frames em paralelo é suficiente para qualquer backend ─ */
#define POOL_CAP 4
static TinyBlocks_RenderableBlock s_pool[POOL_CAP];
static bool s_used[POOL_CAP];
TinyBlocks_RenderableBlock* tb_renderable_acquire(void) {
for (int i = 0; i < POOL_CAP; i++) {
if (!s_used[i]) {
s_used[i] = true;
s_pool[i].count = 0;
return &s_pool[i];
}
}
return NULL;
}
void tb_renderable_release(TinyBlocks_RenderableBlock* rb) {
for (int i = 0; i < POOL_CAP; i++) {
if (&s_pool[i] == rb) { s_used[i] = false; return; }
}
}
void tb_renderable_release_all(void) {
for (int i = 0; i < POOL_CAP; i++)
s_used[i] = false;
}
/* ── Append interno ─────────────────────────────────────────────────────────── */
static TinyBlocks_DrawCmd* append(TinyBlocks_RenderableBlock* rb) {
if (!rb || rb->count >= TB_DRAW_CMD_CAP) return NULL;
TinyBlocks_DrawCmd* c = &rb->cmds[rb->count++];
memset(c, 0, sizeof(*c));
return c;
}
/* ── Helpers públicos ────────────────────────────────────────────────────────── */
void tb_draw_clear(TinyBlocks_RenderableBlock* rb,
float r, float g, float b, float a) {
TinyBlocks_DrawCmd* c = append(rb); if (!c) return;
c->kind = TB_DRAW_CLEAR;
c->r=r; c->g=g; c->b=b; c->a=a;
}
void tb_draw_fill_rect(TinyBlocks_RenderableBlock* rb,
float x, float y, float w, float h,
float r, float g, float b, float a) {
TinyBlocks_DrawCmd* c = append(rb); if (!c) return;
c->kind = TB_DRAW_FILL_RECT;
c->x=x; c->y=y; c->w=w; c->h=h;
c->r=r; c->g=g; c->b=b; c->a=a;
}
void tb_draw_fill_rrect(TinyBlocks_RenderableBlock* rb,
float x, float y, float w, float h, float radius,
float r, float g, float b, float a) {
TinyBlocks_DrawCmd* c = append(rb); if (!c) return;
c->kind = TB_DRAW_FILL_RRECT;
c->x=x; c->y=y; c->w=w; c->h=h; c->param=radius;
c->r=r; c->g=g; c->b=b; c->a=a;
}
void tb_draw_fill_circle(TinyBlocks_RenderableBlock* rb,
float cx, float cy, float radius,
float r, float g, float b, float a) {
TinyBlocks_DrawCmd* c = append(rb); if (!c) return;
c->kind = TB_DRAW_FILL_CIRCLE;
c->x=cx; c->y=cy; c->param=radius;
c->r=r; c->g=g; c->b=b; c->a=a;
}
void tb_draw_stroke_line(TinyBlocks_RenderableBlock* rb,
float x1, float y1, float x2, float y2, float stroke_w,
float r, float g, float b, float a) {
TinyBlocks_DrawCmd* c = append(rb); if (!c) return;
c->kind = TB_DRAW_STROKE_LINE;
c->x=x1; c->y=y1; c->w=x2; c->h=y2; c->param=stroke_w;
c->r=r; c->g=g; c->b=b; c->a=a;
}
void tb_draw_stroke_circle(TinyBlocks_RenderableBlock* rb,
float cx, float cy, float radius, float stroke_w,
float r, float g, float b, float a) {
TinyBlocks_DrawCmd* c = append(rb); if (!c) return;
c->kind = TB_DRAW_STROKE_CIRCLE;
c->x=cx; c->y=cy; c->param=radius; c->w=stroke_w;
c->r=r; c->g=g; c->b=b; c->a=a;
}
void tb_draw_stroke_rect(TinyBlocks_RenderableBlock* rb,
float x, float y, float w, float h, float stroke_w,
float r, float g, float b, float a) {
TinyBlocks_DrawCmd* c = append(rb); if (!c) return;
c->kind = TB_DRAW_STROKE_RECT;
c->x=x; c->y=y; c->w=w; c->h=h; c->param=stroke_w;
c->r=r; c->g=g; c->b=b; c->a=a;
}
void tb_draw_set_clip(TinyBlocks_RenderableBlock* rb,
float x, float y, float w, float h) {
TinyBlocks_DrawCmd* c = append(rb); if (!c) return;
c->kind = TB_DRAW_SET_CLIP;
c->x=x; c->y=y; c->w=w; c->h=h;
}
void tb_draw_clear_clip(TinyBlocks_RenderableBlock* rb) {
TinyBlocks_DrawCmd* c = append(rb); if (!c) return;
c->kind = TB_DRAW_CLEAR_CLIP;
}
void tb_draw_text(TinyBlocks_RenderableBlock* rb,
float x, float y, const char* text, float font_size,
float r, float g, float b, float a) {
if (!text || !text[0]) return;
TinyBlocks_DrawCmd* c = append(rb); if (!c) return;
c->kind = TB_DRAW_TEXT;
c->x=x; c->y=y;
c->font_size = font_size;
c->r=r; c->g=g; c->b=b; c->a=a;
strncpy(c->text, text, sizeof(c->text) - 1);
}
void tb_draw_merge_offset(TinyBlocks_RenderableBlock* dst,
const TinyBlocks_RenderableBlock* src,
float dx, float dy) {
if (!dst || !src) return;
for (uint32_t i = 0; i < src->count && dst->count < TB_DRAW_CMD_CAP; i++) {
TinyBlocks_DrawCmd cmd = src->cmds[i];
switch (cmd.kind) {
case TB_DRAW_FILL_RECT:
case TB_DRAW_FILL_RRECT:
case TB_DRAW_STROKE_RECT:
case TB_DRAW_SET_CLIP:
case TB_DRAW_TEXT:
cmd.x += dx; cmd.y += dy;
break;
case TB_DRAW_FILL_CIRCLE:
case TB_DRAW_STROKE_CIRCLE:
cmd.x += dx; cmd.y += dy;
break;
case TB_DRAW_STROKE_LINE:
cmd.x += dx; cmd.y += dy;
cmd.w += dx; cmd.h += dy;
break;
case TB_DRAW_CLEAR:
case TB_DRAW_CLEAR_CLIP:
break;
}
dst->cmds[dst->count++] = cmd;
}
}
float tb_measure_text(const char* text, float font_size) {
if (!text) return 0.f;
size_t len = 0;
for (const char* p = text; *p; p++) {
if ((*p & 0xC0) != 0x80) len++; /* conta codepoints UTF-8 */
}
return (float)len * font_size * 0.55f;
}