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>
163 lines
8.7 KiB
C
163 lines
8.7 KiB
C
/*
|
|
* TinyBlocks — exemplo SDL2
|
|
*
|
|
* Demonstra o uso da library tinyblocks-sdl2 registrada estaticamente.
|
|
* Abre uma janela 640x480, desenha um retângulo pulsante e fecha ao pressionar
|
|
* Escape ou fechar a janela. Toda a lógica gráfica é expressa como blocos.
|
|
*/
|
|
#include <stdio.h>
|
|
#include <tinyblocks/tinyblocks.h>
|
|
#include <tb_sdl2.h>
|
|
|
|
int main(void)
|
|
{
|
|
/* ── Runtime ──────────────────────────────────────────────────────────── */
|
|
TinyBlocks* rt = NULL;
|
|
if (!tinyblocks_init(&rt, NULL)) { fprintf(stderr, "init falhou\n"); return 1; }
|
|
|
|
/* ── Registra a library SDL2 estaticamente ────────────────────────────── */
|
|
TinyBlocks_LibraryBlock* sdl2 = tb_library_register(rt, tb_sdl2_get_library());
|
|
if (!sdl2) {
|
|
fprintf(stderr, "Falha ao registrar SDL2: %s\n", tinyblocks_last_error(rt));
|
|
tinyblocks_deinit(&rt);
|
|
return 1;
|
|
}
|
|
printf("Library '%s' carregada com %u blocos\n",
|
|
sdl2->base.name, sdl2->block_count);
|
|
|
|
/* ── Busca os TypeBlocks que vamos usar ───────────────────────────────── */
|
|
TinyBlocks_TypeBlock* T_Init = (TinyBlocks_TypeBlock*)tb_library_find(sdl2, "SDL2.Init");
|
|
TinyBlocks_TypeBlock* T_Quit = (TinyBlocks_TypeBlock*)tb_library_find(sdl2, "SDL2.Quit");
|
|
TinyBlocks_TypeBlock* T_Window = (TinyBlocks_TypeBlock*)tb_library_find(sdl2, "SDL2.Window");
|
|
TinyBlocks_TypeBlock* T_Renderer = (TinyBlocks_TypeBlock*)tb_library_find(sdl2, "SDL2.Renderer");
|
|
TinyBlocks_TypeBlock* T_SetColor = (TinyBlocks_TypeBlock*)tb_library_find(sdl2, "SDL2.SetDrawColor");
|
|
TinyBlocks_TypeBlock* T_Clear = (TinyBlocks_TypeBlock*)tb_library_find(sdl2, "SDL2.Clear");
|
|
TinyBlocks_TypeBlock* T_FillRect = (TinyBlocks_TypeBlock*)tb_library_find(sdl2, "SDL2.FillRect");
|
|
TinyBlocks_TypeBlock* T_Present = (TinyBlocks_TypeBlock*)tb_library_find(sdl2, "SDL2.Present");
|
|
TinyBlocks_TypeBlock* T_PollEvent = (TinyBlocks_TypeBlock*)tb_library_find(sdl2, "SDL2.PollEvent");
|
|
TinyBlocks_TypeBlock* T_DestroyWin = (TinyBlocks_TypeBlock*)tb_library_find(sdl2, "SDL2.DestroyWindow");
|
|
TinyBlocks_TypeBlock* T_DestroyRend = (TinyBlocks_TypeBlock*)tb_library_find(sdl2, "SDL2.DestroyRenderer");
|
|
TinyBlocks_TypeBlock* T_Delay = (TinyBlocks_TypeBlock*)tb_library_find(sdl2, "SDL2.Delay");
|
|
|
|
/* ── SDL2.Init ────────────────────────────────────────────────────────── */
|
|
TinyBlocks_Block* b_init = tb_block_create(rt, T_Init);
|
|
tb_block_eval(b_init, rt);
|
|
|
|
/* ── SDL2.Window ─────────────────────────────────────────────────────── */
|
|
TinyBlocks_TextBlock* lit_title = tb_text_block_create(rt, "TinyBlocks SDL2");
|
|
TinyBlocks_IntegerBlock* lit_cx = tb_integer_block_create(rt, -1); /* centered */
|
|
TinyBlocks_IntegerBlock* lit_cy = tb_integer_block_create(rt, -1);
|
|
TinyBlocks_IntegerBlock* lit_w = tb_integer_block_create(rt, 640);
|
|
TinyBlocks_IntegerBlock* lit_h = tb_integer_block_create(rt, 480);
|
|
|
|
TinyBlocks_Block* b_window = tb_block_create(rt, T_Window);
|
|
tb_block_connect(b_window, 0, &lit_title->base, 0);
|
|
tb_block_connect(b_window, 1, &lit_cx->base, 0);
|
|
tb_block_connect(b_window, 2, &lit_cy->base, 0);
|
|
tb_block_connect(b_window, 3, &lit_w->base, 0);
|
|
tb_block_connect(b_window, 4, &lit_h->base, 0);
|
|
tb_block_eval(b_window, rt);
|
|
|
|
/* ── SDL2.Renderer ───────────────────────────────────────────────────── */
|
|
TinyBlocks_Block* b_renderer = tb_block_create(rt, T_Renderer);
|
|
tb_block_connect(b_renderer, 0, b_window, 0);
|
|
tb_block_eval(b_renderer, rt);
|
|
|
|
/* ── Blocos reutilizáveis por frame ──────────────────────────────────── */
|
|
TinyBlocks_Block* b_poll = tb_block_create(rt, T_PollEvent);
|
|
TinyBlocks_Block* b_bg_color = tb_block_create(rt, T_SetColor);
|
|
TinyBlocks_Block* b_clear = tb_block_create(rt, T_Clear);
|
|
TinyBlocks_Block* b_rect_color= tb_block_create(rt, T_SetColor);
|
|
TinyBlocks_Block* b_rect = tb_block_create(rt, T_FillRect);
|
|
TinyBlocks_Block* b_present = tb_block_create(rt, T_Present);
|
|
TinyBlocks_Block* b_delay = tb_block_create(rt, T_Delay);
|
|
|
|
/* Cor de fundo: preto */
|
|
TinyBlocks_IntegerBlock* lit_0 = tb_integer_block_create(rt, 0);
|
|
TinyBlocks_IntegerBlock* lit_255 = tb_integer_block_create(rt, 255);
|
|
tb_block_connect(b_bg_color, 0, b_renderer, 0);
|
|
tb_block_connect(b_bg_color, 1, &lit_0->base, 0);
|
|
tb_block_connect(b_bg_color, 2, &lit_0->base, 0);
|
|
tb_block_connect(b_bg_color, 3, &lit_0->base, 0);
|
|
tb_block_connect(b_bg_color, 4, &lit_255->base, 0);
|
|
|
|
tb_block_connect(b_clear, 0, b_renderer, 0);
|
|
|
|
/* Cor do retângulo: vermelho */
|
|
TinyBlocks_IntegerBlock* lit_r = tb_integer_block_create(rt, 220);
|
|
TinyBlocks_IntegerBlock* lit_g = tb_integer_block_create(rt, 50);
|
|
TinyBlocks_IntegerBlock* lit_b2= tb_integer_block_create(rt, 50);
|
|
tb_block_connect(b_rect_color, 0, b_renderer, 0);
|
|
tb_block_connect(b_rect_color, 1, &lit_r->base, 0);
|
|
tb_block_connect(b_rect_color, 2, &lit_g->base, 0);
|
|
tb_block_connect(b_rect_color, 3, &lit_b2->base, 0);
|
|
tb_block_connect(b_rect_color, 4, &lit_255->base, 0);
|
|
|
|
/* Retângulo dinâmico — usaremos IntegerBlocks mutáveis para posição */
|
|
TinyBlocks_IntegerBlock* lit_rx = tb_integer_block_create(rt, 100);
|
|
TinyBlocks_IntegerBlock* lit_ry = tb_integer_block_create(rt, 100);
|
|
TinyBlocks_IntegerBlock* lit_rw = tb_integer_block_create(rt, 200);
|
|
TinyBlocks_IntegerBlock* lit_rh = tb_integer_block_create(rt, 120);
|
|
tb_block_connect(b_rect, 0, b_renderer, 0);
|
|
tb_block_connect(b_rect, 1, &lit_rx->base, 0);
|
|
tb_block_connect(b_rect, 2, &lit_ry->base, 0);
|
|
tb_block_connect(b_rect, 3, &lit_rw->base, 0);
|
|
tb_block_connect(b_rect, 4, &lit_rh->base, 0);
|
|
|
|
tb_block_connect(b_present, 0, b_renderer, 0);
|
|
|
|
TinyBlocks_IntegerBlock* lit_delay = tb_integer_block_create(rt, 16); /* ~60fps */
|
|
tb_block_connect(b_delay, 0, &lit_delay->base, 0);
|
|
|
|
/* ── Loop principal ──────────────────────────────────────────────────── */
|
|
printf("Janela aberta. Pressione Escape ou feche a janela para sair.\n");
|
|
|
|
int running = 1;
|
|
int frame = 0;
|
|
|
|
while (running) {
|
|
/* Eventos */
|
|
b_poll->dirty = true;
|
|
tb_block_eval(b_poll, rt);
|
|
|
|
TinyBlocks_Value vquit = b_poll->outputs[1];
|
|
TinyBlocks_Value vtype = b_poll->outputs[0];
|
|
if (vquit.as.integer) running = 0;
|
|
if (vtype.as.integer == 768 /* SDL_KEYDOWN */) {
|
|
/* sacancode 41 = Escape */
|
|
if (b_poll->outputs[2].as.integer == 41) running = 0;
|
|
}
|
|
|
|
/* Animação simples: retângulo oscila horizontalmente */
|
|
frame++;
|
|
int offset = (int)(80.0 * (0.5 + 0.5 * ((frame % 120) < 60
|
|
? (frame % 60) / 60.0
|
|
: 1.0 - (frame % 60) / 60.0)));
|
|
tb_integer_block_set(lit_rx, 80 + offset);
|
|
|
|
/* Renderização — cada bloco é avaliado na ordem correta */
|
|
b_bg_color->dirty = true; tb_block_eval(b_bg_color, rt);
|
|
b_clear->dirty = true; tb_block_eval(b_clear, rt);
|
|
b_rect_color->dirty = true; tb_block_eval(b_rect_color, rt);
|
|
b_rect->dirty = true; tb_block_eval(b_rect, rt);
|
|
b_present->dirty = true; tb_block_eval(b_present, rt);
|
|
b_delay->dirty = true; tb_block_eval(b_delay, rt);
|
|
}
|
|
|
|
/* ── Limpeza SDL2 via blocos ──────────────────────────────────────────── */
|
|
TinyBlocks_Block* b_drend = tb_block_create(rt, T_DestroyRend);
|
|
TinyBlocks_Block* b_dwin = tb_block_create(rt, T_DestroyWin);
|
|
TinyBlocks_Block* b_quit = tb_block_create(rt, T_Quit);
|
|
|
|
tb_block_connect(b_drend, 0, b_renderer, 0);
|
|
tb_block_connect(b_dwin, 0, b_window, 0);
|
|
|
|
tb_block_eval(b_drend, rt);
|
|
tb_block_eval(b_dwin, rt);
|
|
tb_block_eval(b_quit, rt);
|
|
|
|
tinyblocks_deinit(&rt);
|
|
printf("Encerrado.\n");
|
|
return 0;
|
|
}
|