#define _POSIX_C_SOURCE 200809L #include #include #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_text_font(TinyBlocks_RenderableBlock* rb, float x, float y, const char* text, float font_size, int font, float r, float g, float b, float a) { uint32_t n = rb ? rb->count : 0; tb_draw_text(rb, x, y, text, font_size, r, g, b, a); if (rb && rb->count > n) rb->cmds[n].param = (float)font; } void tb_draw_poly_set(TinyBlocks_DrawCmd* c, const float* xy, int n) { if (n > TB_POLY_MAX) n = TB_POLY_MAX; if (n < 0) n = 0; memcpy(c->text, xy, (size_t)n * 2 * sizeof(float)); /* 16 pontos = 128 bytes */ c->param = (float)n; float x0 = n ? xy[0] : 0, y0 = n ? xy[1] : 0, x1 = x0, y1 = y0; for (int i = 1; i < n; i++) { if (xy[2*i] < x0) x0 = xy[2*i]; if (xy[2*i] > x1) x1 = xy[2*i]; if (xy[2*i+1] < y0) y0 = xy[2*i+1]; if (xy[2*i+1] > y1) y1 = xy[2*i+1]; } c->x = x0; c->y = y0; c->w = x1 - x0; c->h = y1 - y0; } int tb_draw_poly_get(const TinyBlocks_DrawCmd* c, float* xy) { int n = (int)c->param; if (n > TB_POLY_MAX) n = TB_POLY_MAX; if (n < 0) n = 0; memcpy(xy, c->text, (size_t)n * 2 * sizeof(float)); return n; } void tb_draw_fill_poly(TinyBlocks_RenderableBlock* rb, const float* xy, int n, float r, float g, float b, float a) { if (!xy || n < 3) return; TinyBlocks_DrawCmd* c = append(rb); if (!c) return; c->kind = TB_DRAW_FILL_POLY; c->r=r; c->g=g; c->b=b; c->a=a; tb_draw_poly_set(c, xy, n); } void tb_draw_image(TinyBlocks_RenderableBlock* rb, float x, float y, float w, float h, const TinyBlocks_Image* image) { if (!image) return; TinyBlocks_DrawCmd* c = append(rb); if (!c) return; c->kind = TB_DRAW_IMAGE; c->x = x; c->y = y; c->w = w; c->h = h; c->image = image; } 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_IMAGE: 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_FILL_POLY: { float xy[2 * TB_POLY_MAX]; int n = tb_draw_poly_get(&cmd, xy); for (int i = 0; i < n; i++) { xy[2*i] += dx; xy[2*i+1] += dy; } tb_draw_poly_set(&cmd, xy, n); 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; }