Adiciona tipo imagem (core) + tinyblocks-ppm e previa de imagem no balao
This commit is contained in:
1 parent
f794d8b8c7
commit
959a4027c3
16 files changed
+721
-10
No files matched your search
@@ -250,6 +250,14 @@ static const BlockSpec s_specs[] = {
|
||||
/* Base64 */
|
||||
{"Base64.Encode", "codifique em base64 %1", NULL},
|
||||
{"Base64.Decode", "decodifique base64 %1", NULL},
|
||||
/* Imagens PPM (tipo imagem do core) */
|
||||
{"PPM.Load", "carregue a imagem %1", "imagem.ppm"},
|
||||
{"PPM.Save", "grave a imagem %1 em %2", "|saida.ppm"},
|
||||
{"PPM.New", "nova imagem(%1 x %2) cor r %3 g %4 b %5", "32|32|255|0|0"},
|
||||
{"PPM.Width", "largura de %1", NULL},
|
||||
{"PPM.Height", "altura de %1", NULL},
|
||||
{"PPM.SetPixel", "pinte %1 em (%2,%3) de r %4 g %5 b %6", "|0|0|0|0|0"},
|
||||
{"PPM.GetPixel", "cor de %1 em (%2,%3)", "|0|0"},
|
||||
};
|
||||
|
||||
/* ===
|
||||
@@ -401,6 +409,7 @@ typedef struct {
|
||||
int bubble_bi;
|
||||
char bubble_text[128];
|
||||
TinyBlocks_JsonValue* bubble_json; /* lista de pontos (retida) ou NULL */
|
||||
TinyBlocks_Image* bubble_img; /* imagem (retida) ou NULL */
|
||||
int last_click_bi;
|
||||
unsigned last_click_ms;
|
||||
|
||||
@@ -576,6 +585,8 @@ static void value_to_str(const TinyBlocks_Value* v, char* buf, size_t cap) {
|
||||
case TB_VALUE_BLOCK: snprintf(buf, cap, "[bloco]"); break;
|
||||
case TB_VALUE_VECTOR: snprintf(buf, cap, "[vetor]"); break;
|
||||
case TB_VALUE_JSON: snprintf(buf, cap, "[json]"); break;
|
||||
case TB_VALUE_IMAGE: snprintf(buf, cap, "[imagem %dx%d]",
|
||||
tb_image_width(v->as.image), tb_image_height(v->as.image)); break;
|
||||
default: buf[0] = 0; break;
|
||||
}
|
||||
}
|
||||
@@ -1578,6 +1589,7 @@ static void run_flag(Ed* ed) {
|
||||
/* Libera o balão de resultado (texto + eventual lista de pontos) */
|
||||
static void bubble_clear(Ed* ed) {
|
||||
if (ed->bubble_json) { tb_json_release(ed->bubble_json); ed->bubble_json = NULL; }
|
||||
if (ed->bubble_img) { tb_image_release(ed->bubble_img); ed->bubble_img = NULL; }
|
||||
ed->bubble_bi = -1;
|
||||
ed->bubble_text[0] = 0;
|
||||
}
|
||||
@@ -1624,6 +1636,13 @@ static void bubble_set(Ed* ed, int root, TinyBlocks_Value* v) {
|
||||
(unsigned)tb_json_array_length(v->as.json));
|
||||
return;
|
||||
}
|
||||
if (v->kind == TB_VALUE_IMAGE && v->as.image) {
|
||||
tb_image_retain(v->as.image);
|
||||
ed->bubble_img = v->as.image;
|
||||
snprintf(ed->bubble_text, sizeof(ed->bubble_text), "%dx%d",
|
||||
tb_image_width(v->as.image), tb_image_height(v->as.image));
|
||||
return;
|
||||
}
|
||||
value_to_str(v, ed->bubble_text, sizeof(ed->bubble_text));
|
||||
if (!ed->bubble_text[0]) strcpy(ed->bubble_text, "(vazio)");
|
||||
}
|
||||
@@ -2795,7 +2814,23 @@ static void render_workspace(Ed* ed, TinyBlocks_RenderableBlock* rb) {
|
||||
|
||||
if (ed->bubble_bi >= 0 && CT.ws[ed->bubble_bi].used) {
|
||||
EdBlock* b = &CT.ws[ed->bubble_bi];
|
||||
if (ed->bubble_json) {
|
||||
if (ed->bubble_img) {
|
||||
/* Balão com prévia da imagem (escalada, mantendo proporção) */
|
||||
const float BW = 184, BH = 160, PAD = 10;
|
||||
float bx = b->ax + ox + 8, by = b->ay + oy - (BH + 6);
|
||||
rb_fill_rrect(rb, bx, by, BW, BH, 10, COL_BORDER);
|
||||
rb_fill_rrect(rb, bx+1, by+1, BW-2, BH-2, 9, COL_WHITE);
|
||||
rb_fill_rect(rb, bx + 16, by + BH - 2, 10, 5, COL_WHITE);
|
||||
int iw = tb_image_width(ed->bubble_img);
|
||||
int ih = tb_image_height(ed->bubble_img);
|
||||
if (iw > 0 && ih > 0) {
|
||||
float aw = BW - 2*PAD, ah = BH - 2*PAD - 12;
|
||||
float sc = aw/iw < ah/ih ? aw/iw : ah/ih;
|
||||
float dw = iw*sc, dh = ih*sc;
|
||||
tb_draw_image(rb, bx + (BW-dw)*0.5f, by + (BH-12-dh)*0.5f, dw, dh, ed->bubble_img);
|
||||
}
|
||||
rb_text_c(rb, bx + 10, by + BH - 4, ed->bubble_text, COL_TEXT_DIM, 11.f);
|
||||
} else if (ed->bubble_json) {
|
||||
/* Balão com prévia gráfica de uma lista de pontos (curva) */
|
||||
const float BW = 180, BH = 140, PAD = 16;
|
||||
float bx = b->ax + ox + 8, by = b->ay + oy - (BH + 6);
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
# Makefile da library ppm (categoria "PPM")
|
||||
CC := gcc
|
||||
BUILD := build
|
||||
|
||||
CFLAGS := -std=c11 -Wall -Wextra -pedantic -fvisibility=hidden \
|
||||
-D_POSIX_C_SOURCE=200809L -I../../include -Iinclude
|
||||
|
||||
OBJ := $(BUILD)/tb_ppm.o
|
||||
LIB_A := $(BUILD)/libtinyblocks-ppm.a
|
||||
LIB_SO := $(BUILD)/libtinyblocks-ppm.so
|
||||
|
||||
.PHONY: all static shared clean
|
||||
all: static shared
|
||||
static: $(LIB_A)
|
||||
shared: $(LIB_SO)
|
||||
|
||||
$(OBJ): src/tb_ppm.c include/tb_ppm.h
|
||||
@mkdir -p $(BUILD)
|
||||
$(CC) $(CFLAGS) -fPIC -c $< -o $@
|
||||
|
||||
$(LIB_A): $(OBJ)
|
||||
ar rcs $@ $^
|
||||
|
||||
$(LIB_SO): $(OBJ)
|
||||
$(CC) -shared -o $@ $^
|
||||
|
||||
clean:
|
||||
$(RM) -r $(BUILD)
|
||||
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* TinyBlocks - tb_ppm.h
|
||||
* Library tinyblocks-ppm: carrega/grava imagens Netpbm (P6 binario e P3 ASCII)
|
||||
* como TinyBlocks_Image (TB_VALUE_IMAGE). Independente do editor.
|
||||
*/
|
||||
#ifndef TINYBLOCKS_PPM_H
|
||||
#define TINYBLOCKS_PPM_H
|
||||
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
|
||||
TB_API const TinyBlocks_LibraryDescriptor* tb_ppm_get_library(void);
|
||||
|
||||
/* Carrega um .ppm (P6/P3). Retorna NULL em erro; use tb_ppm_last_error(). */
|
||||
TB_API TinyBlocks_Image* tb_ppm_load(const char* path);
|
||||
/* Grava a imagem como P6 binario (converte cinza/RGBA para RGB). */
|
||||
TB_API bool tb_ppm_save(const TinyBlocks_Image* img, const char* path);
|
||||
TB_API const char* tb_ppm_last_error(void);
|
||||
|
||||
#endif /* TINYBLOCKS_PPM_H */
|
||||
@@ -0,0 +1,15 @@
|
||||
# Metadados da library ppm. Lido pela UI/build sem recompilar o .so.
|
||||
[library]
|
||||
name = "tinyblocks-ppm"
|
||||
version = "1.0.0"
|
||||
category = "PPM"
|
||||
description = "Imagens Netpbm (PPM): carregar, gravar e editar pixels"
|
||||
color = [255, 167, 38, 255]
|
||||
dark = [245, 124, 0, 255]
|
||||
|
||||
[build]
|
||||
sources = ["src/tb_ppm.c"]
|
||||
includes = ["include"]
|
||||
shared = "build/libtinyblocks-ppm.so"
|
||||
static = "build/libtinyblocks-ppm.a"
|
||||
depends = []
|
||||
@@ -0,0 +1,247 @@
|
||||
/*
|
||||
* TinyBlocks - tb_ppm.c
|
||||
* Categoria "PPM": PPM.Load / PPM.Save / PPM.New / PPM.Width / PPM.Height /
|
||||
* PPM.SetPixel / PPM.GetPixel. Le e grava Netpbm (P6 binario e P3 ASCII).
|
||||
*/
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include "tb_ppm.h"
|
||||
|
||||
static char s_err[200] = "";
|
||||
const char* tb_ppm_last_error(void) { return s_err; }
|
||||
|
||||
/* Le um token (inteiro ou magic) pulando espacos e comentarios '#'. */
|
||||
static bool next_token(FILE* f, char* buf, size_t cap) {
|
||||
int c;
|
||||
/* pula espacos e comentarios */
|
||||
for (;;) {
|
||||
c = fgetc(f);
|
||||
if (c == EOF) return false;
|
||||
if (c == '#') { while ((c = fgetc(f)) != EOF && c != '\n') {} continue; }
|
||||
if (!isspace(c)) break;
|
||||
}
|
||||
size_t n = 0;
|
||||
while (c != EOF && !isspace(c)) {
|
||||
if (n + 1 < cap) buf[n++] = (char)c;
|
||||
c = fgetc(f);
|
||||
}
|
||||
buf[n] = 0;
|
||||
return n > 0;
|
||||
}
|
||||
|
||||
TinyBlocks_Image* tb_ppm_load(const char* path) {
|
||||
s_err[0] = 0;
|
||||
if (!path) { snprintf(s_err, sizeof(s_err), "caminho nulo"); return NULL; }
|
||||
FILE* f = fopen(path, "rb");
|
||||
if (!f) { snprintf(s_err, sizeof(s_err), "nao abriu '%s'", path); return NULL; }
|
||||
|
||||
char magic[8] = "", wbuf[24] = "", hbuf[24] = "", mbuf[24] = "";
|
||||
if (!next_token(f, magic, sizeof(magic)) ||
|
||||
(strcmp(magic, "P6") != 0 && strcmp(magic, "P3") != 0)) {
|
||||
snprintf(s_err, sizeof(s_err), "formato PPM invalido (esperado P3/P6)");
|
||||
fclose(f); return NULL;
|
||||
}
|
||||
if (!next_token(f, wbuf, sizeof(wbuf)) ||
|
||||
!next_token(f, hbuf, sizeof(hbuf)) ||
|
||||
!next_token(f, mbuf, sizeof(mbuf))) {
|
||||
snprintf(s_err, sizeof(s_err), "cabecalho PPM incompleto");
|
||||
fclose(f); return NULL;
|
||||
}
|
||||
int w = atoi(wbuf), h = atoi(hbuf), maxv = atoi(mbuf);
|
||||
if (w <= 0 || h <= 0 || maxv <= 0 || maxv > 65535) {
|
||||
snprintf(s_err, sizeof(s_err), "dimensoes/maxval invalidos");
|
||||
fclose(f); return NULL;
|
||||
}
|
||||
|
||||
TinyBlocks_Image* img = tb_image_new(w, h, 3);
|
||||
if (!img) { snprintf(s_err, sizeof(s_err), "sem memoria"); fclose(f); return NULL; }
|
||||
uint8_t* px = tb_image_data(img);
|
||||
|
||||
if (strcmp(magic, "P6") == 0) {
|
||||
int bytes = maxv > 255 ? 2 : 1;
|
||||
for (size_t i = 0; i < (size_t)w * h * 3; i++) {
|
||||
int v;
|
||||
if (bytes == 2) {
|
||||
int hi = fgetc(f), lo = fgetc(f);
|
||||
if (hi == EOF || lo == EOF) { snprintf(s_err, sizeof(s_err), "dados truncados"); tb_image_release(img); fclose(f); return NULL; }
|
||||
v = (hi << 8) | lo;
|
||||
} else {
|
||||
v = fgetc(f);
|
||||
if (v == EOF) { snprintf(s_err, sizeof(s_err), "dados truncados"); tb_image_release(img); fclose(f); return NULL; }
|
||||
}
|
||||
px[i] = (uint8_t)((v * 255 + maxv/2) / maxv);
|
||||
}
|
||||
} else {
|
||||
for (size_t i = 0; i < (size_t)w * h * 3; i++) {
|
||||
char tb[24];
|
||||
if (!next_token(f, tb, sizeof(tb))) { snprintf(s_err, sizeof(s_err), "dados truncados"); tb_image_release(img); fclose(f); return NULL; }
|
||||
int v = atoi(tb);
|
||||
px[i] = (uint8_t)((v * 255 + maxv/2) / maxv);
|
||||
}
|
||||
}
|
||||
fclose(f);
|
||||
return img;
|
||||
}
|
||||
|
||||
bool tb_ppm_save(const TinyBlocks_Image* img, const char* path) {
|
||||
s_err[0] = 0;
|
||||
if (!img || !path) { snprintf(s_err, sizeof(s_err), "imagem/caminho nulo"); return false; }
|
||||
int w = tb_image_width(img), h = tb_image_height(img);
|
||||
if (w <= 0 || h <= 0) { snprintf(s_err, sizeof(s_err), "imagem vazia"); return false; }
|
||||
FILE* f = fopen(path, "wb");
|
||||
if (!f) { snprintf(s_err, sizeof(s_err), "nao criou '%s'", path); return false; }
|
||||
fprintf(f, "P6\n%d %d\n255\n", w, h);
|
||||
for (int y = 0; y < h; y++) {
|
||||
for (int x = 0; x < w; x++) {
|
||||
uint8_t r, g, b, a;
|
||||
tb_image_get_pixel(img, x, y, &r, &g, &b, &a);
|
||||
fputc(r, f); fputc(g, f); fputc(b, f);
|
||||
}
|
||||
}
|
||||
bool ok = (ferror(f) == 0);
|
||||
fclose(f);
|
||||
if (!ok) snprintf(s_err, sizeof(s_err), "erro ao gravar");
|
||||
return ok;
|
||||
}
|
||||
|
||||
/* -- Helpers de entrada -------------------------------------------------- */
|
||||
|
||||
static const char* in_text(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
|
||||
return (v.kind == TB_VALUE_TEXT && v.as.text) ? v.as.text : "";
|
||||
}
|
||||
static double in_real(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
|
||||
if (v.kind == TB_VALUE_REAL) return v.as.real;
|
||||
if (v.kind == TB_VALUE_INTEGER) return (double)v.as.integer;
|
||||
if (v.kind == TB_VALUE_TEXT && v.as.text) return atof(v.as.text);
|
||||
return 0.0;
|
||||
}
|
||||
static int in_int(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
|
||||
return (int)in_real(self, rt, i);
|
||||
}
|
||||
static TinyBlocks_Image* in_img(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
|
||||
return v.kind == TB_VALUE_IMAGE ? v.as.image : NULL;
|
||||
}
|
||||
|
||||
static uint8_t clamp8(double v) {
|
||||
if (v < 0) return 0;
|
||||
if (v > 255) return 255;
|
||||
return (uint8_t)(v + 0.5);
|
||||
}
|
||||
|
||||
/* -- Blocos -------------------------------------------------------------- */
|
||||
|
||||
static void eval_load(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_Image* img = tb_ppm_load(in_text(self, rt, 0));
|
||||
if (!img) { tinyblocks_set_error(rt, "%s", tb_ppm_last_error()); self->dirty = false; return; }
|
||||
tb_block_set_output(self, 0, tb_value_image(img));
|
||||
tb_image_release(img);
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void eval_save(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_Image* img = in_img(self, rt, 0);
|
||||
if (!img || !tb_ppm_save(img, in_text(self, rt, 1)))
|
||||
tinyblocks_set_error(rt, "%s", tb_ppm_last_error());
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void eval_new(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
int w = in_int(self, rt, 0), h = in_int(self, rt, 1);
|
||||
TinyBlocks_Image* img = tb_image_new(w > 0 ? w : 1, h > 0 ? h : 1, 3);
|
||||
if (!img) { tinyblocks_set_error(rt, "sem memoria"); self->dirty = false; return; }
|
||||
uint8_t r = clamp8(in_real(self, rt, 2));
|
||||
uint8_t g = clamp8(in_real(self, rt, 3));
|
||||
uint8_t b = clamp8(in_real(self, rt, 4));
|
||||
for (int y = 0; y < h; y++)
|
||||
for (int x = 0; x < w; x++)
|
||||
tb_image_set_pixel(img, x, y, r, g, b, 255);
|
||||
tb_block_set_output(self, 0, tb_value_image(img));
|
||||
tb_image_release(img);
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void eval_width(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
tb_block_set_output(self, 0, tb_value_integer(tb_image_width(in_img(self, rt, 0))));
|
||||
self->dirty = false;
|
||||
}
|
||||
static void eval_height(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
tb_block_set_output(self, 0, tb_value_integer(tb_image_height(in_img(self, rt, 0))));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void eval_setpixel(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_Image* img = in_img(self, rt, 0);
|
||||
if (!img) { tinyblocks_set_error(rt, "imagem invalida"); self->dirty = false; return; }
|
||||
tb_image_set_pixel(img, in_int(self, rt, 1), in_int(self, rt, 2),
|
||||
clamp8(in_real(self, rt, 3)), clamp8(in_real(self, rt, 4)),
|
||||
clamp8(in_real(self, rt, 5)), 255);
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void eval_getpixel(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_Image* img = in_img(self, rt, 0);
|
||||
uint8_t r = 0, g = 0, b = 0, a = 0;
|
||||
tb_image_get_pixel(img, in_int(self, rt, 1), in_int(self, rt, 2), &r, &g, &b, &a);
|
||||
TinyBlocks_JsonValue* arr = tb_json_array();
|
||||
tb_json_array_push(arr, tb_json_number(r));
|
||||
tb_json_array_push(arr, tb_json_number(g));
|
||||
tb_json_array_push(arr, tb_json_number(b));
|
||||
tb_block_set_output(self, 0, tb_value_json(arr));
|
||||
tb_json_release(arr);
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
/* -- Registro ------------------------------------------------------------ */
|
||||
|
||||
static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
|
||||
const TinyBlocks_SlotDesc* in, uint32_t ni,
|
||||
const TinyBlocks_SlotDesc* out, uint32_t no,
|
||||
void (*fn)(TinyBlocks_Block*, TinyBlocks*)) {
|
||||
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, name, in, ni, out, no, fn);
|
||||
return t && tb_library_add(lib, &t->base);
|
||||
}
|
||||
|
||||
static bool ppm_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
static const TinyBlocks_Category cat = {
|
||||
"PPM",
|
||||
{ 255/255.f, 167/255.f, 38/255.f, 1.f},
|
||||
{ 245/255.f, 124/255.f, 0/255.f, 1.f},
|
||||
};
|
||||
tb_library_set_category(lib, &cat);
|
||||
|
||||
static const TinyBlocks_SlotDesc img_out[] = {{"imagem", TB_VALUE_IMAGE}};
|
||||
static const TinyBlocks_SlotDesc n_out[] = {{"n", TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc rgb_out[] = {{"cor", TB_VALUE_JSON}};
|
||||
|
||||
static const TinyBlocks_SlotDesc load_in[] = {{"caminho", TB_VALUE_TEXT}};
|
||||
static const TinyBlocks_SlotDesc save_in[] = {{"imagem", TB_VALUE_IMAGE}, {"caminho", TB_VALUE_TEXT}};
|
||||
static const TinyBlocks_SlotDesc new_in[] = {{"largura",TB_VALUE_REAL},{"altura",TB_VALUE_REAL},
|
||||
{"r",TB_VALUE_REAL},{"g",TB_VALUE_REAL},{"b",TB_VALUE_REAL}};
|
||||
static const TinyBlocks_SlotDesc wh_in[] = {{"imagem", TB_VALUE_IMAGE}};
|
||||
static const TinyBlocks_SlotDesc setp_in[] = {{"imagem",TB_VALUE_IMAGE},{"x",TB_VALUE_INTEGER},{"y",TB_VALUE_INTEGER},
|
||||
{"r",TB_VALUE_REAL},{"g",TB_VALUE_REAL},{"b",TB_VALUE_REAL}};
|
||||
static const TinyBlocks_SlotDesc getp_in[] = {{"imagem",TB_VALUE_IMAGE},{"x",TB_VALUE_INTEGER},{"y",TB_VALUE_INTEGER}};
|
||||
|
||||
return reg(rt, lib, "PPM.Load", load_in, 1, img_out, 1, eval_load)
|
||||
&& reg(rt, lib, "PPM.Save", save_in, 2, NULL, 0, eval_save)
|
||||
&& reg(rt, lib, "PPM.New", new_in, 5, img_out, 1, eval_new)
|
||||
&& reg(rt, lib, "PPM.Width", wh_in, 1, n_out, 1, eval_width)
|
||||
&& reg(rt, lib, "PPM.Height", wh_in, 1, n_out, 1, eval_height)
|
||||
&& reg(rt, lib, "PPM.SetPixel", setp_in, 6, NULL, 0, eval_setpixel)
|
||||
&& reg(rt, lib, "PPM.GetPixel", getp_in, 3, rgb_out, 1, eval_getpixel);
|
||||
}
|
||||
|
||||
TINYBLOCKS_DEFINE_LIBRARY(ppm, "tinyblocks-ppm", "1.0.0", ppm_init, NULL)
|
||||
|
||||
const TinyBlocks_LibraryDescriptor* tb_ppm_get_library(void) {
|
||||
return &_tb_lib_desc_ppm;
|
||||
}
|
||||
@@ -320,6 +320,47 @@ static void sdl2_execute(SDL_Renderer* ren,
|
||||
SDL_RenderSetClipRect(ren, NULL);
|
||||
break;
|
||||
|
||||
case TB_DRAW_IMAGE:
|
||||
if (cmd->image) {
|
||||
int iw = tb_image_width(cmd->image);
|
||||
int ih = tb_image_height(cmd->image);
|
||||
int ch = tb_image_channels(cmd->image);
|
||||
const uint8_t* px = tb_image_data_const(cmd->image);
|
||||
if (iw > 0 && ih > 0 && px) {
|
||||
SDL_Surface* surf = NULL;
|
||||
uint8_t* tmp = NULL;
|
||||
if (ch == 4) {
|
||||
surf = SDL_CreateRGBSurfaceWithFormatFrom((void*)px, iw, ih, 32, iw*4,
|
||||
SDL_PIXELFORMAT_RGBA32);
|
||||
} else if (ch == 3) {
|
||||
surf = SDL_CreateRGBSurfaceWithFormatFrom((void*)px, iw, ih, 24, iw*3,
|
||||
SDL_PIXELFORMAT_RGB24);
|
||||
} else if (ch == 1) {
|
||||
tmp = (uint8_t*)malloc((size_t)iw * (size_t)ih * 4);
|
||||
if (tmp) {
|
||||
size_t n = (size_t)iw * (size_t)ih;
|
||||
for (size_t k = 0; k < n; k++) {
|
||||
tmp[k*4+0] = tmp[k*4+1] = tmp[k*4+2] = px[k];
|
||||
tmp[k*4+3] = 255;
|
||||
}
|
||||
surf = SDL_CreateRGBSurfaceWithFormatFrom(tmp, iw, ih, 32, iw*4,
|
||||
SDL_PIXELFORMAT_RGBA32);
|
||||
}
|
||||
}
|
||||
if (surf) {
|
||||
SDL_Texture* tex = SDL_CreateTextureFromSurface(ren, surf);
|
||||
if (tex) {
|
||||
SDL_Rect dst = {(int)cmd->x, (int)cmd->y, (int)cmd->w, (int)cmd->h};
|
||||
SDL_RenderCopy(ren, tex, NULL, &dst);
|
||||
SDL_DestroyTexture(tex);
|
||||
}
|
||||
SDL_FreeSurface(surf);
|
||||
}
|
||||
free(tmp);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case TB_DRAW_TEXT:
|
||||
if (cmd->text[0]) {
|
||||
TTF_Font* font = get_font((int)cmd->font_size);
|
||||
|
||||
Reference in new issue
Block a user