draw_rect

This commit is contained in:
Krzosa Karol
2025-12-14 11:28:59 +01:00
parent 2e087824a3
commit e69684c877
4 changed files with 59 additions and 10 deletions

View File

@@ -256,6 +256,9 @@ int IsDebuggerPresent(void);
#include <signal.h>
static void os_unix_sigtrap(int signum) { signal(SIGTRAP, SIG_DFL); }
#define debug__break() (signal(SIGTRAP, os_unix_sigtrap), raise(SIGTRAP))
#elif PLATFORM_WASM
void wasm_trap(void);
#define debug__break() wasm_trap()
#else
#define debug__break() __builtin_trap()
#endif

View File

@@ -4,12 +4,13 @@
#if PLATFORM_WASM && PLATFORM_EMSCRIPTEN == 0
#define gb_wasm_export __attribute__((visibility("default")))
#define fn_wasm_export __attribute__((visibility("default")))
#define fn_export __attribute__((visibility("default")))
#define fn_wasm_import
fn_wasm_import void wasm_alert(isize ptr, i32 len);
fn_wasm_import f64 wasm_parse_float(isize str, i32 len);
fn_wasm_import void wasm_write_to_console(isize str, i32 len);
fn_wasm_import void wasm_trap(void);
extern char __heap_base;

View File

@@ -32,7 +32,8 @@ class wglRender {
uniform sampler2D u_texture;
out vec4 outColor;
void main() {
outColor = texture(u_texture, v_texCoord);
vec4 c = texture(u_texture, v_texCoord);
outColor = c;
}`;
CANVAS = document.getElementById('glcanvas');
@@ -182,7 +183,7 @@ async function WASMInit(name) {
memory: mem.mem,
wasm_parse_float: (str, len) => { return parseFloat(mem.decodeString(str, len)); },
wasm_alert: (str, len) => { alert(mem.decodeString(str,len)); },
wasm_trap: () => { throw new Error(); },
wasm_trap: () => { debugger; },
wasm_write_to_console: (str, len) => { console.log(mem.decodeString(str, len)); },
};
const program = await WebAssembly['instantiate'](binary, { "env": import_table });

View File

@@ -31,6 +31,7 @@ struct glyph_t {
bitmap_t bitmap;
i32 xoff, yoff;
i32 left_side_bearing, xadvance;
r2f32_t tex;
};
typedef struct font_t font_t;
@@ -53,7 +54,7 @@ fn font_t create_font(i32 size) {
stbtt_GetFontVMetrics(&stb_font, &font.ascent, &font.descent, &font.line_gap);
font.height = f32_round((font.ascent - font.descent) + font.line_gap) * font.scale;
for (i32 c = 0; c < 256; c += 1) {
glyph_t *glyph = font.glyphs + (c - ' ');
glyph_t *glyph = font.glyphs + c;
u8 *temp_data = (u8 *)stbtt_GetCodepointBitmap(&stb_font, 0, font.scale, c, &glyph->bitmap.x, &glyph->bitmap.y, &glyph->xoff, &glyph->yoff);
glyph->bitmap.data = ma_push_array(&tcx->temp, u32, glyph->bitmap.x * glyph->bitmap.y);
for (i32 y = 0; y < glyph->bitmap.y; y += 1) {
@@ -91,6 +92,11 @@ fn font_t create_font_atlas(i32 size) {
yiter += max_yiter_for_row + 2;
}
glyph->tex.min.x = (f32)xiter / (f32)font.atlas.x;
glyph->tex.min.y = (f32)yiter / (f32)font.atlas.y;
glyph->tex.max.x = (f32)(xiter + glyph->bitmap.x)/ (f32)font.atlas.x;
glyph->tex.max.y = (f32)(yiter + glyph->bitmap.y)/ (f32)font.atlas.y;
max_yiter_for_row = MAX(max_yiter_for_row, glyph->bitmap.y);
for (i32 y = 0; y < glyph->bitmap.y; y += 1) {
for (i32 x = 0; x < glyph->bitmap.x; x += 1) {
@@ -102,11 +108,11 @@ fn font_t create_font_atlas(i32 size) {
return font;
}
fn_wasm_export void init(void) {
fn_export void init(void) {
os_core_init();
}
fn void draw_rect(bitmap_t *canvas, bitmap_t *bitmap, i32 x, i32 y) {
fn void draw_bitmap(bitmap_t *canvas, bitmap_t *bitmap, i32 x, i32 y) {
i32 x0 = x;
i32 y0 = y;
i32 x1 = x + bitmap->x;
@@ -130,17 +136,45 @@ fn void draw_rect(bitmap_t *canvas, bitmap_t *bitmap, i32 x, i32 y) {
i32 sx = sx_off;
for (i32 x = cx0; x < cx1; x += 1) {
canvas->data[x + y * canvas->x] = bitmap->data[sx + sy * bitmap->x];
//canvas->data[x + y * canvas->x] = blend_pixel_u32(canvas->data[x + y * canvas->x], bitmap->data[sx + sy * bitmap->x]);
sx += 1;
}
sy += 1;
}
}
fn void draw_rect(bitmap_t *canvas, i32 x, i32 y, i32 w, i32 h, u32 color) {
i32 x0 = x;
i32 y0 = y;
i32 x1 = x + w;
i32 y1 = y + h;
i32 cx0 = CLAMP(x0, 0, canvas->x);
i32 cy0 = CLAMP(y0, 0, canvas->y);
i32 cx1 = CLAMP(x1, 0, canvas->x);
i32 cy1 = CLAMP(y1, 0, canvas->y);
if (cx0 >= cx1 || cy0 >= cy1) {
return;
}
i32 sx_off = cx0 - x0;
i32 sy_off = cy0 - y0;
for (i32 y = cy0; y < cy1; y += 1) {
for (i32 x = cx0; x < cx1; x += 1) {
canvas->data[x + y * canvas->x] = color;
//canvas->data[x + y * canvas->x] = blend_pixel_u32(canvas->data[x + y * canvas->x], color);
}
}
}
fn v2i32_t draw_string(bitmap_t *canvas, font_t *font, i32 ix, i32 iy, b32 draw, char *string) {
i32 xiter = 0;
v2i32_t R = {0};
for (char *it = string; *it; it += 1) {
glyph_t *g = font->glyphs + (*it - ' ');
glyph_t *g = font->glyphs + (*it);
i32 curr_xiter = xiter;
xiter += g->xadvance * font->scale;
@@ -151,17 +185,27 @@ fn v2i32_t draw_string(bitmap_t *canvas, font_t *font, i32 ix, i32 iy, b32 draw,
continue;
}
draw_rect(canvas, &g->bitmap, curr_xiter + g->xoff + ix, g->yoff + font->ascent*font->scale + iy);
draw_bitmap(canvas, &g->bitmap, curr_xiter + g->xoff + ix, g->yoff + font->ascent*font->scale + iy);
}
return R;
}
fn_wasm_export u32 *update(i32 width, i32 height, f32 dpr) {
fn_export u32 *update(i32 width, i32 height, f32 dpr) {
ma_set0(&tcx->temp);
u32 *pixels = ma_push_array(&tcx->temp, u32, width * height);
bitmap_t canvas = {pixels, width, height};
font_t font = create_font_atlas(60);
draw_rect(&canvas, &font.atlas, 0, 0);
draw_bitmap(&canvas, &font.atlas, 0, 0);
for (i32 i = 0; i < lengthof(font.glyphs); i += 1) {
glyph_t *g = &font.glyphs[i];
i32 x = g->tex.min.x * font.atlas.x;
i32 y = g->tex.min.y * font.atlas.y;
i32 w = (g->tex.max.x - g->tex.min.x) * font.atlas.x;
i32 h = (g->tex.max.y - g->tex.min.y) * font.atlas.y;
//draw_rect(&canvas, x, y, w, h, 0xFF0000FF);
}
//bitmap_t canvas = {pixels, width, height};
//font_t font = create_font(60);
//v2i32_t size = draw_string(&canvas, &font, 200, 500, false, "Hello world (stb)");
//draw_string(&canvas, &font, (width-size.x)/2, height - size.y*2, true, "Hello world (stb)");
return pixels;