Files
kryon/include/kry_sw.h
T
waozi 3319143452 kry_sw: track the dirty rectangle per call
KrySwDirty previously reported the full frame. Drawing ops now union
their clip-intersected bounds into an accumulator (fill_rect covers
rects, circles, rings, blits and the bitmap font; text marks its
string bbox so the atlas path is included), and KrySwDirty became
one-shot: it reports everything drawn since the previous read and
resets, so hosts can present partial frames.
2026-08-18 16:17:29 -03:00

92 lines
2.9 KiB
C

#ifndef KRY_SW_H
#define KRY_SW_H
/*
* kry_sw — portable software rasterizer behind the KryBackend vtable.
* Renders KRB cartridges into a caller-visible pixel buffer with no GPU,
* no libc beyond memcpy/malloc. Targets RGBA8 (host byte order R,G,B,A)
* with an RGB565 conversion for embedded panels.
*
* Text uses a built-in public-domain 8x8 bitmap font scaled to the
* requested size; the pre-baked glyph-atlas pipeline (plan 11, phase 6)
* replaces this later without touching the interface.
*/
#include "kry_backend.h"
typedef struct KrySwAtlasSize {
int px;
int glyphs;
int w;
int h;
unsigned table_off;
unsigned pixels_off;
} KrySwAtlasSize;
typedef struct KrySw {
unsigned char *pixels; /* RGBA8, stride * h bytes */
int w;
int h;
int stride; /* bytes per row, >= w * 4 */
int owns; /* pixels were malloc'd here */
int clip_n;
int clip_x[16];
int clip_y[16];
int clip_w[16];
int clip_h[16];
/* accumulated dirty rect since the last KrySwDirty read */
int dirty_x0;
int dirty_y0;
int dirty_x1;
int dirty_y1;
int dirty_any;
int mouse_x;
int mouse_y;
int buttons_down;
int buttons_pressed;
int wheel; /* pending wheel delta, consumed by the backend poll */
unsigned keys[16]; /* typed codepoints (0x08 = backspace) */
int keys_n;
float now;
int scale; /* UI scale per mille, 1000 = 1:1 */
unsigned theme[KRY_THEME_COUNT];
/* KFA1 atlas */
const unsigned char *atlas;
unsigned atlas_len;
int atlas_sizes;
KrySwAtlasSize size_tab[8];
KryBackend backend;
} KrySw;
/* pixels == NULL allocates an internal w*h*4 buffer (free with KrySwFree). */
int KrySwInit(KrySw *sw, unsigned char *pixels, int w, int h);
void KrySwFree(KrySw *sw);
const KryBackend *KrySwBackend(KrySw *sw);
/* Input injection for hosts/tests: move the pointer, latch a press. */
void KrySwMouse(KrySw *sw, int x, int y);
void KrySwButtonDown(KrySw *sw, int button);
void KrySwButtonUp(KrySw *sw, int button);
void KrySwWheel(KrySw *sw, int dy);
void KrySwText(KrySw *sw, unsigned codepoint);
void KrySwAdvance(KrySw *sw, float dt); /* clears pressed edges, ticks time */
void KrySwSetTheme(KrySw *sw, int slot, unsigned rgba);
/* Dirty rectangle: union of everything drawn since the previous call,
* clipped to the active scissor stack and the target. One-shot — reading
* resets the accumulator; hosts present the rect once per frame. Nothing
* drawn reports x = y = w = h = 0. */
void KrySwDirty(KrySw *sw, int *x, int *y, int *w, int *h);
/* Install a KFA1 glyph atlas (cartridge asset kind 1). Text draws from
* atlas glyphs with proper advances; without one the built-in 8x8 font
* is used. */
int KrySwSetAtlas(KrySw *sw, const unsigned char *data, unsigned len);
/* Convert the RGBA8 target to dst (w*h uint16, RGB565). */
void KrySwToRGB565(const KrySw *sw, unsigned short *dst);
#endif