Files
tinyblocks/src/tb_stage.c
T
mguinhosandClaude Opus 5.5 bfa17eecea Linha de comando: pasta é ignorada com aviso, projeto é aberto
Todo argumento ia para o dlopen como library: abrir o editor com uma pasta
(./TinyBlocks.AppDir) dava "dlopen: ... Is a directory". Agora .so é
library, pasta é ignorada com um aviso claro e qualquer outro arquivo é
aberto como projeto (.json, ou .txt em scratchblocks).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 15:55:37 -03:00

204 lines
6.5 KiB
C

/*
* TinyBlocks - tb_stage.c
* Palco logico (canvas retido) do runtime: uma tela de 480x360 (origem no
* centro, y para cima, como no Scratch) onde blocos de desenho escrevem.
*
* Os desenhos ficam numa lista retida do runtime, entao qualquer library
* (como a Caneta) pode desenhar sem depender do editor. A UI chama
* tb_stage_set_view + tb_stage_render para exibir.
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tinyblocks/tb_stage.h"
#include "tinyblocks/tb_image.h"
#define STAGE_W 480.f
#define STAGE_H 360.f
#define STAGE_MAX_CMDS 4000
#define STAGE_TEXT_FS 14.f
typedef enum { SC_RECT, SC_CIRCLE, SC_LINE, SC_TEXT, SC_IMAGE } StageCmdKind;
typedef struct {
StageCmdKind kind;
float a, b, c, d; /* geometria em coordenadas do palco */
float size; /* espessura da caneta */
float col[3];
char text[96];
const TinyBlocks_Image* img; /* SC_IMAGE (vem do armazem de imagens) */
} StageCmd;
typedef struct {
StageCmd* cmds;
int n, cap;
float bg[3];
float pen[3];
float pen_size;
float view_w, view_h;
} Stage;
static Stage s_stage = {
NULL, 0, 0,
{1.f, 1.f, 1.f},
{76/255.f, 151/255.f, 1.f},
2.f, STAGE_W, STAGE_H
};
static float chan(float v) { return v < 0 ? 0.f : v > 255 ? 1.f : v / 255.f; }
static StageCmd* push(StageCmdKind kind) {
Stage* s = &s_stage;
if (s->n >= STAGE_MAX_CMDS) return NULL;
if (s->n >= s->cap) {
int nc = s->cap ? s->cap * 2 : 64;
StageCmd* p = (StageCmd*)realloc(s->cmds, (size_t)nc * sizeof(StageCmd));
if (!p) return NULL;
s->cmds = p; s->cap = nc;
}
StageCmd* c = &s->cmds[s->n++];
memset(c, 0, sizeof(*c));
c->kind = kind;
memcpy(c->col, s->pen, sizeof(c->col));
c->size = s->pen_size;
return c;
}
void tb_stage_clear(void) {
s_stage.n = 0;
}
void tb_stage_set_background(float r, float g, float b) {
s_stage.bg[0] = chan(r); s_stage.bg[1] = chan(g); s_stage.bg[2] = chan(b);
}
void tb_stage_set_pen(float r, float g, float b) {
s_stage.pen[0] = chan(r); s_stage.pen[1] = chan(g); s_stage.pen[2] = chan(b);
}
void tb_stage_set_pen_size(float size) {
s_stage.pen_size = size < 1.f ? 1.f : size;
}
void tb_stage_line(float x1, float y1, float x2, float y2) {
StageCmd* c = push(SC_LINE);
if (c) { c->a = x1; c->b = y1; c->c = x2; c->d = y2; }
}
void tb_stage_rect(float x, float y, float w, float h) {
StageCmd* c = push(SC_RECT);
if (c) { c->a = x; c->b = y; c->c = w; c->d = h; }
}
void tb_stage_circle(float x, float y, float radius) {
StageCmd* c = push(SC_CIRCLE);
if (c) { c->a = x; c->b = y; c->c = radius; }
}
void tb_stage_text(const char* text, float x, float y) {
StageCmd* c = push(SC_TEXT);
if (c) {
snprintf(c->text, sizeof(c->text), "%s", text ? text : "");
c->a = x; c->b = y;
}
}
bool tb_stage_image_zoom(const TinyBlocks_Image* img, float x, float y, float zoom) {
if (!img) return false;
StageCmd* c = push(SC_IMAGE);
if (!c) return false;
c->a = x; c->b = y;
c->d = zoom > 0.f ? zoom : 1.f;
c->img = img;
return true;
}
bool tb_stage_image(const TinyBlocks_Image* img, float x, float y) {
return tb_stage_image_zoom(img, x, y, 1.f);
}
bool tb_stage_image_named(const char* name, float x, float y) {
return tb_stage_image(tb_image_store_get(name), x, y);
}
bool tb_stage_image_number(int number, float x, float y) {
return tb_stage_image(tb_image_store_at(number), x, y);
}
void tb_stage_set_view(float w, float h) {
if (w > 0 && h > 0) { s_stage.view_w = w; s_stage.view_h = h; }
}
bool tb_stage_view_to_stage(float vx, float vy, float* sx, float* sy) {
const Stage* s = &s_stage;
float sc = s->view_w / STAGE_W < s->view_h / STAGE_H ? s->view_w / STAGE_W : s->view_h / STAGE_H;
if (sc <= 0.f) sc = 1.f;
float ox = (s->view_w - STAGE_W*sc) * 0.5f, oy = (s->view_h - STAGE_H*sc) * 0.5f;
float x = (vx - ox) / sc - STAGE_W * 0.5f;
float y = STAGE_H * 0.5f - (vy - oy) / sc;
if (sx) *sx = x;
if (sy) *sy = y;
return x >= -STAGE_W * 0.5f && x <= STAGE_W * 0.5f && y >= -STAGE_H * 0.5f && y <= STAGE_H * 0.5f;
}
void tb_stage_reset(void) {
free(s_stage.cmds);
s_stage.cmds = NULL; s_stage.n = 0; s_stage.cap = 0;
s_stage.bg[0] = s_stage.bg[1] = s_stage.bg[2] = 1.f;
s_stage.pen[0] = 76/255.f; s_stage.pen[1] = 151/255.f; s_stage.pen[2] = 1.f;
s_stage.pen_size = 2.f;
s_stage.view_w = STAGE_W; s_stage.view_h = STAGE_H;
}
bool tb_stage_render(TinyBlocks_RenderableBlock* rb) {
if (!rb) return false;
Stage* s = &s_stage;
float sc = s->view_w / STAGE_W < s->view_h / STAGE_H ? s->view_w / STAGE_W : s->view_h / STAGE_H;
float ox = (s->view_w - STAGE_W*sc) * 0.5f, oy = (s->view_h - STAGE_H*sc) * 0.5f;
#define SX(x) (ox + ((x) + STAGE_W*0.5f) * sc)
#define SY(y) (oy + (STAGE_H*0.5f - (y)) * sc)
if (ox > 0.5f || oy > 0.5f)
tb_draw_fill_rect(rb, 0, 0, s->view_w, s->view_h, 0.12f, 0.12f, 0.14f, 1.f);
tb_draw_fill_rect(rb, ox, oy, STAGE_W*sc, STAGE_H*sc, s->bg[0], s->bg[1], s->bg[2], 1.f);
tb_draw_set_clip(rb, ox, oy, STAGE_W*sc, STAGE_H*sc);
for (int i = 0; i < s->n && rb->count < TB_DRAW_CMD_CAP - 2; i++) {
const StageCmd* c = &s->cmds[i];
float r = c->col[0], g = c->col[1], b = c->col[2];
switch (c->kind) {
case SC_RECT:
tb_draw_fill_rect(rb, SX(c->a - c->c*0.5f), SY(c->b + c->d*0.5f),
c->c*sc, c->d*sc, r, g, b, 1.f);
break;
case SC_CIRCLE:
tb_draw_fill_circle(rb, SX(c->a), SY(c->b), c->c*sc, r, g, b, 1.f);
break;
case SC_LINE:
tb_draw_stroke_line(rb, SX(c->a), SY(c->b), SX(c->c), SY(c->d),
c->size*sc, r, g, b, 1.f);
break;
case SC_IMAGE: {
const TinyBlocks_Image* im = c->img;
int iw = tb_image_width(im), ih = tb_image_height(im);
if (iw > 0 && ih > 0) {
float z = c->d > 0.f ? c->d : 1.f;
float dw = iw * sc * z, dh = ih * sc * z;
tb_draw_image(rb, SX(c->a) - dw * 0.5f, SY(c->b) - dh * 0.5f, dw, dh, im);
}
break;
}
case SC_TEXT: {
float fs = STAGE_TEXT_FS * sc;
tb_draw_text(rb, SX(c->a), SY(c->b) + fs*0.36f, c->text, fs, r, g, b, 1.f);
break;
}
}
}
tb_draw_clear_clip(rb);
#undef SX
#undef SY
return true;
}