Files
tinyblocks/include/tinyblocks/tb_renderable_block.h
T

126 lines
5.9 KiB
C

/*
* TinyBlocks - tb_renderable_block.h
*
* TinyBlocks_RenderableBlock: lista de comandos de desenho produzida por on_draw.
* O backend (ex: SDL2.App) interpreta os comandos para renderizar na tela.
*
* Uso típico:
* bool my_on_draw(TinyBlocks_Block* self, TinyBlocks* rt,
* TinyBlocks_RenderableBlock** dst) {
* TinyBlocks_RenderableBlock* rb = tb_renderable_acquire();
* if (!rb) return false;
* tb_draw_clear(rb, 0.1f, 0.1f, 0.18f, 1.f);
* tb_draw_fill_rect(rb, 10, 10, 200, 100, 0.5f, 0.3f, 0.9f, 1.f);
* tb_draw_text(rb, 20, 50, "Olá", 13.f, 1,1,1,1);
* *dst = rb;
* return true;
* }
* // Após renderizar: tb_renderable_release_all();
*/
#ifndef TINYBLOCKS_RENDERABLE_BLOCK_H
#define TINYBLOCKS_RENDERABLE_BLOCK_H
#include "tb_defs.h"
/* -- Tipos de comando --- */
typedef enum {
TB_DRAW_CLEAR, /* limpa toda a área com uma cor */
TB_DRAW_FILL_RECT, /* retângulo preenchido */
TB_DRAW_FILL_RRECT, /* retângulo com cantos arredondados (param=radius) */
TB_DRAW_FILL_CIRCLE, /* círculo preenchido (x=cx, y=cy, param=radius) */
TB_DRAW_STROKE_LINE, /* linha (x,y -> w,h como x2,y2; param=espessura) */
TB_DRAW_STROKE_RECT, /* contorno de retângulo (param=espessura) */
TB_DRAW_STROKE_CIRCLE, /* contorno de círculo (x=cx,y=cy,param=r,w=espessura)*/
TB_DRAW_SET_CLIP, /* define rect de clip (coordenadas tela) */
TB_DRAW_CLEAR_CLIP, /* remove clip */
TB_DRAW_TEXT, /* texto (x,y=baseline-left; font_size em px) */
TB_DRAW_IMAGE, /* imagem escalada em (x,y,w,h); ver field image */
} TinyBlocks_DrawCmdKind;
typedef struct {
TinyBlocks_DrawCmdKind kind;
float x, y, w, h; /* geometria (significado varia por kind) */
float r, g, b, a; /* cor RGBA [0..1] */
float param; /* radius (rrect/circle), espessura (linha/stroke) */
float font_size; /* tamanho da fonte em pixels (TB_DRAW_TEXT) */
char text[128]; /* conteúdo do texto (TB_DRAW_TEXT) */
const TinyBlocks_Image* image; /* pixels (TB_DRAW_IMAGE, não-owning) */
} TinyBlocks_DrawCmd;
/* -- RenderableBlock --- */
#define TB_DRAW_CMD_CAP 8192
struct TinyBlocks_RenderableBlock {
TinyBlocks_DrawCmd cmds[TB_DRAW_CMD_CAP];
uint32_t count;
};
/* -- Pool de renderables (reset por frame, sem malloc durante o loop) --- */
/* Adquire um renderable limpo do pool. Retorna NULL se esgotado. */
TB_API TinyBlocks_RenderableBlock* tb_renderable_acquire(void);
/* Libera um renderable individual de volta ao pool (uso após merge). */
TB_API void tb_renderable_release(TinyBlocks_RenderableBlock* rb);
/* Libera todos os renderables de volta ao pool (chamar uma vez por frame,
depois que o backend terminar de renderizar). */
TB_API void tb_renderable_release_all(void);
/* -- Helpers para acrescentar comandos --- */
TB_API void tb_draw_clear (TinyBlocks_RenderableBlock* rb,
float r, float g, float b, float a);
TB_API void tb_draw_fill_rect (TinyBlocks_RenderableBlock* rb,
float x, float y, float w, float h,
float r, float g, float b, float a);
TB_API 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);
TB_API void tb_draw_fill_circle(TinyBlocks_RenderableBlock* rb,
float cx, float cy, float radius,
float r, float g, float b, float a);
TB_API void tb_draw_stroke_line(TinyBlocks_RenderableBlock* rb,
float x1, float y1, float x2, float y2, float w,
float r, float g, float b, float a);
TB_API 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);
TB_API 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);
TB_API void tb_draw_set_clip (TinyBlocks_RenderableBlock* rb,
float x, float y, float w, float h);
TB_API void tb_draw_clear_clip (TinyBlocks_RenderableBlock* rb);
TB_API 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);
/* Desenha a imagem escalada para o retângulo (x,y,w,h). A imagem é
* ref-counted; o comando apenas guarda um ponteiro não-owning, portanto a
* imagem precisa continuar viva até o backend renderizar o frame. */
TB_API void tb_draw_image (TinyBlocks_RenderableBlock* rb,
float x, float y, float w, float h,
const TinyBlocks_Image* image);
/* Mescla src em dst deslocando todas as coordenadas por (dx, dy).
Usado pelo editor para incorporar sub-renderables de blocos com on_draw. */
TB_API void tb_draw_merge_offset(TinyBlocks_RenderableBlock* dst,
const TinyBlocks_RenderableBlock* src,
float dx, float dy);
/* Estimativa de largura de texto (independente de fonte real).
Precisa ser substituída por medição real pelo backend quando disponível. */
TB_API float tb_measure_text(const char* text, float font_size);
#endif /* TINYBLOCKS_RENDERABLE_BLOCK_H */