new repo for codebase
This commit is contained in:
112
src/scratch/scratch_main.c
Normal file
112
src/scratch/scratch_main.c
Normal file
@@ -0,0 +1,112 @@
|
||||
#include "core/core.h"
|
||||
#include "core/core.c"
|
||||
|
||||
#define STB_TRUETYPE_IMPLEMENTATION
|
||||
#define STBTT_ifloor(x) ((int)f64_floor(x))
|
||||
#define STBTT_iceil(x) ((int)f64_ceil(x))
|
||||
#define STBTT_sqrt(x) (f64_sqrt(x))
|
||||
#define STBTT_fmod(x,y) (f64_mod(x,y))
|
||||
#define STBTT_pow(x,y) (f64_pow(x,y))
|
||||
#define STBTT_cos(x) (f64_cos(x))
|
||||
#define STBTT_acos(x) (f64_acos(x))
|
||||
#define STBTT_fabs(x) (f64_abs(x))
|
||||
#define STBTT_malloc(x,u) ((void)u,ma_push_size(&tcx->temp,x))
|
||||
#define STBTT_free(x,u) ((void)x,(void)u)
|
||||
#define STBTT_assert(x) assert(x)
|
||||
#define STBTT_strlen(x) str_len(x)
|
||||
#define STBTT_memcpy memory_copy
|
||||
#define STBTT_memset memory_set
|
||||
|
||||
#include "vendor/stb/stb_truetype.h"
|
||||
#include "fira_code.c"
|
||||
|
||||
typedef struct glyph_t glyph_t;
|
||||
struct glyph_t {
|
||||
u8 *data;
|
||||
i32 width, height;
|
||||
i32 xoff, yoff;
|
||||
i32 left_side_bearing, xadvance;
|
||||
};
|
||||
|
||||
typedef struct font_t font_t;
|
||||
struct font_t {
|
||||
glyph_t glyphs[96];
|
||||
i32 ascent, descent, line_gap, size;
|
||||
f32 scale;
|
||||
};
|
||||
|
||||
typedef struct bitmap_t bitmap_t;
|
||||
struct bitmap_t {
|
||||
u32 *data;
|
||||
i32 x, y;
|
||||
};
|
||||
|
||||
fn_wasm_export void init(void) {
|
||||
os_core_init();
|
||||
}
|
||||
|
||||
fn v2i32_t draw_string(bitmap_t *canvas, font_t *font, int ix, int 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 - ' ');
|
||||
|
||||
i32 curr_xiter = xiter;
|
||||
xiter += g->xadvance * font->scale;
|
||||
|
||||
i32 x0 = curr_xiter + g->xoff + ix;
|
||||
i32 y0 = g->yoff + font->ascent*font->scale + iy;
|
||||
i32 x1 = g->width + x0;
|
||||
i32 y1 = g->height + y0;
|
||||
|
||||
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);
|
||||
|
||||
R.x = xiter;
|
||||
R.y = MAX(R.y, g->height);
|
||||
|
||||
if (!draw || (cx0 >= cx1 || cy0 >= cy1)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
i32 sx_off = cx0 - x0;
|
||||
i32 sy_off = cy0 - y0;
|
||||
|
||||
i32 sy = sy_off;
|
||||
for (i32 y = cy0; y < cy1; y += 1) {
|
||||
i32 sx = sx_off;
|
||||
for (i32 x = cx0; x < cx1; x += 1) {
|
||||
canvas->data[x + y * canvas->x] = g->data[sx + sy * g->width] << 24;
|
||||
sx += 1;
|
||||
}
|
||||
sy += 1;
|
||||
}
|
||||
}
|
||||
return R;
|
||||
}
|
||||
|
||||
fn_wasm_export u32 *update(i32 width, i32 height, f32 dpr) {
|
||||
gb font_t font = {0};
|
||||
font.size = (i32)(130.f * dpr);
|
||||
stbtt_fontinfo stb_font;
|
||||
int rc = stbtt_InitFont(&stb_font, main_font_data, stbtt_GetFontOffsetForIndex(main_font_data,0));
|
||||
assert_expr(rc != 0);
|
||||
|
||||
font.scale = stbtt_ScaleForPixelHeight(&stb_font, (f32)font.size);
|
||||
stbtt_GetFontVMetrics(&stb_font, &font.ascent, &font.descent, &font.line_gap);
|
||||
for (int c = ' '; c < '~'; c += 1) {
|
||||
glyph_t *glyph = font.glyphs + (c - ' ');
|
||||
u8 *temp_data = (u8 *)stbtt_GetCodepointBitmap(&stb_font, 0, font.scale, c, &glyph->width, &glyph->height, &glyph->xoff, &glyph->yoff);
|
||||
glyph->data = ma_push_array_copy(&tcx->perm, temp_data, glyph->width*glyph->height);
|
||||
stbtt_GetCodepointHMetrics(&stb_font, c, &glyph->xadvance, &glyph->left_side_bearing);
|
||||
}
|
||||
|
||||
ma_set0(&tcx->temp);
|
||||
u32 *pixels = ma_push_array(&tcx->temp, u32, width * height);
|
||||
bitmap_t canvas = {pixels, width, height};
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user