k3/src/immdraw.h
Mid a571d2d999 Move to runtime generated SDF fonts
GlyphCache is introduced as a layer that handles the bin packing with a
shelf heuristic. SDF rendering is handled in the fixed-function pipeline
using a simple alpha test. The shader version also applies a little
smoothing.

k3Batch had to be updated to add a minimum alpha parameter.
2025-12-01 23:58:55 +02:00

65 lines
2.1 KiB
C

#pragma once
#include"k3batch.h"
#include"k3font.h"
#include"k3menu.h"
extern uint16_t GameWndH;
static inline void immdraw_fill_rect(int16_t x, int16_t y, int16_t w, int16_t h, float r, float g, float b, float a, float borderRadius) {
k3BatchAdd(NULL, (struct k3RectF) {0, 0, 1, 1}, (struct k3RectF) {
x, GameWndH - y - h,
w, h
}, 0, (vec4) {r, g, b, a}, borderRadius, 0);
}
static inline void immdraw_font_draw(struct k3Font *font, int16_t x, int16_t y, int16_t w, float sz, size_t len, const char *txt, int alignment, float r, float g, float b, float a) {
k3FontDraw(font, x, GameWndH - y - sz, sz, txt, w, alignment, (vec4) {r, g, b, a});
}
static inline void immdraw_font_size(struct k3Font *font, float sz, const char *txt, int16_t wall, int16_t aabb[2]) {
struct k3RectF txtsz;
k3FontSz(font, sz, txt, wall, &txtsz);
aabb[0] = ceilf(txtsz.w);
aabb[1] = ceilf(txtsz.h);
}
static inline void immdraw_image_draw(k3MImageData *data, int16_t x, int16_t y, int16_t w, int16_t h, float r, float g, float b, float a) {
struct k3Tex *tex = *data;
k3BatchAdd(tex, (struct k3RectF) {0, 0, 1, 1}, (struct k3RectF) {x, GameWndH - y - h, w, h}, 0, (vec4) {r, g, b, a}, 0, 0);
}
static int16_t crop_aabb[4] = {-1, -1, -1, -1};
static inline void immdraw_crop_get(int16_t aabb[4]) {
memcpy(aabb, crop_aabb, sizeof(crop_aabb));
}
static inline void immdraw_crop_set(int16_t aabb[4]) {
memcpy(crop_aabb, aabb, sizeof(crop_aabb));
if(aabb[0] == -1 && aabb[1] == -1 && aabb[2] == -1 && aabb[3] == -1) {
glDisable(GL_SCISSOR_TEST);
} else {
glEnable(GL_SCISSOR_TEST);
glScissor(aabb[0], GameWndH - aabb[3], aabb[2] - aabb[0], aabb[3] - aabb[1]);
}
}
static inline void immdraw_crop_add(int16_t aabb[4]) {
int16_t newaabb[4];
memcpy(newaabb, crop_aabb, sizeof(newaabb));
if(aabb[0] > newaabb[0] || newaabb[0] == -1) {
newaabb[0] = aabb[0];
}
if(aabb[1] > newaabb[1] || newaabb[1] == -1) {
newaabb[1] = aabb[1];
}
if(aabb[2] < newaabb[2] || newaabb[2] == -1) {
newaabb[2] = aabb[2];
}
if(aabb[3] < newaabb[3] || newaabb[3] == -1) {
newaabb[3] = aabb[3];
}
immdraw_crop_set(newaabb);
}