remove draw / gfx, baseline
This commit is contained in:
@@ -150,20 +150,21 @@ const ctx2d = canvas.getContext('2d');
|
||||
wasm_draw_text: (str, len, x, y, font_str, font_len, font_size, r, g, b, a) => {
|
||||
ctx2d.font = `${font_size}px ${mem.read_cstr(font_str, font_len)}`;
|
||||
ctx2d.fillStyle = `rgba(${r}, ${g}, ${b}, ${a})`;
|
||||
ctx2d.textBaseline = "hanging";
|
||||
ctx2d.textBaseline = "top";
|
||||
ctx2d.fillText(mem.read_cstr(str, len), x, y);
|
||||
},
|
||||
wasm_measure_text: (str, len, font_str, font_len, font_size) => {
|
||||
ctx2d.font = `${font_size}px ${mem.read_cstr(font_str, font_len)}`;
|
||||
ctx2d.textBaseline = "hanging";
|
||||
ctx2d.textBaseline = "top";
|
||||
const metrics = ctx2d.measureText(mem.read_cstr(str, len));
|
||||
return metrics.width;
|
||||
},
|
||||
wasm_get_font_height: (font_str, font_len, font_size) => {
|
||||
ctx2d.font = `${font_size}px ${mem.read_cstr(font_str, font_len)}`;
|
||||
ctx2d.textBaseline = "hanging";
|
||||
return ctx2d.measureText('NothinBelowTheBaseline').actualBoundingBoxDescent;
|
||||
},
|
||||
// wasm_get_font_height: (font_str, font_len, font_size) => {
|
||||
// ctx2d.font = `${font_size}px ${mem.read_cstr(font_str, font_len)}`;
|
||||
// ctx2d.textBaseline = "top";
|
||||
// const v = ctx2d.measureText('NothinBelowTheBaseline');
|
||||
// return v.fontBoundingBoxDescent;
|
||||
// },
|
||||
wasm_draw_rect: (x, y, w, h, r, g, b, a) => {
|
||||
ctx2d.beginPath();
|
||||
ctx2d.rect(x, y, w, h);
|
||||
|
||||
@@ -150,20 +150,21 @@ const ctx2d = canvas.getContext('2d');
|
||||
wasm_draw_text: (str, len, x, y, font_str, font_len, font_size, r, g, b, a) => {
|
||||
ctx2d.font = `${font_size}px ${mem.read_cstr(font_str, font_len)}`;
|
||||
ctx2d.fillStyle = `rgba(${r}, ${g}, ${b}, ${a})`;
|
||||
ctx2d.textBaseline = "hanging";
|
||||
ctx2d.textBaseline = "top";
|
||||
ctx2d.fillText(mem.read_cstr(str, len), x, y);
|
||||
},
|
||||
wasm_measure_text: (str, len, font_str, font_len, font_size) => {
|
||||
ctx2d.font = `${font_size}px ${mem.read_cstr(font_str, font_len)}`;
|
||||
ctx2d.textBaseline = "hanging";
|
||||
ctx2d.textBaseline = "top";
|
||||
const metrics = ctx2d.measureText(mem.read_cstr(str, len));
|
||||
return metrics.width;
|
||||
},
|
||||
wasm_get_font_height: (font_str, font_len, font_size) => {
|
||||
ctx2d.font = `${font_size}px ${mem.read_cstr(font_str, font_len)}`;
|
||||
ctx2d.textBaseline = "hanging";
|
||||
return ctx2d.measureText('NothinBelowTheBaseline').actualBoundingBoxDescent;
|
||||
},
|
||||
// wasm_get_font_height: (font_str, font_len, font_size) => {
|
||||
// ctx2d.font = `${font_size}px ${mem.read_cstr(font_str, font_len)}`;
|
||||
// ctx2d.textBaseline = "top";
|
||||
// const v = ctx2d.measureText('NothinBelowTheBaseline');
|
||||
// return v.fontBoundingBoxDescent;
|
||||
// },
|
||||
wasm_draw_rect: (x, y, w, h, r, g, b, a) => {
|
||||
ctx2d.beginPath();
|
||||
ctx2d.rect(x, y, w, h);
|
||||
|
||||
146
src/draw/gfx2d.c
146
src/draw/gfx2d.c
@@ -1,146 +0,0 @@
|
||||
fn_wasm_import void wasm_clear(void);
|
||||
fn_wasm_import void wasm_draw_text(isize str, i32 len, f64 x, f64 y, isize font_str, i32 font_len, i32 font_size, f32 r, f32 g, f32 b, f32 a);
|
||||
fn_wasm_import void wasm_draw_rect(f64 x, f64 y, f64 w, f64 h, f32 r, f32 g, f32 b, f32 a);
|
||||
fn_wasm_import f64 wasm_measure_text(isize str, i32 len, isize font_str, i32 font_len, i32 font_size);
|
||||
fn_wasm_import f64 wasm_get_font_height(isize font_str, i32 font_len, i32 font_size);
|
||||
fn_wasm_import void wasm_set_clip(f64 x, f64 y, f64 w, f64 h);
|
||||
|
||||
|
||||
// gb_read_only s8_t font_face = s8_const_lit("open_sans_regular");
|
||||
gb_read_only s8_t font_face = s8_const_lit("consolas");
|
||||
|
||||
fn void set_clip(r2f64_t rect) {
|
||||
wasm_set_clip(wasm_dpr * rect.min.x, wasm_dpr * rect.min.y, wasm_dpr * (rect.max.x - rect.min.x), wasm_dpr * (rect.max.y - rect.min.y));
|
||||
}
|
||||
|
||||
fn f64 get_font_height(void) {
|
||||
return wasm_get_font_height((isize) font_face.str, font_face.len, 20*wasm_dpr) / wasm_dpr;
|
||||
}
|
||||
|
||||
fn f64 measure_text_ex(s8_t string) {
|
||||
return wasm_measure_text((isize)string.str, string.len, (isize) font_face.str, font_face.len, 20*wasm_dpr) / wasm_dpr;
|
||||
}
|
||||
|
||||
fn f64 measure_text(char *str) {
|
||||
return measure_text_ex(s8_from_char(str));
|
||||
}
|
||||
|
||||
fn void draw_text(v2f64_t pos, v4f32_t color, s8_t string) {
|
||||
wasm_draw_text((isize)string.str, string.len, wasm_dpr * pos.x, wasm_dpr * pos.y, (isize) font_face.str, font_face.len, 20*wasm_dpr, color.r * 255.f, color.g * 255.f, color.b * 255.f, color.a);
|
||||
}
|
||||
|
||||
fn void draw_textf(v2f64_t pos, char *str, ...) {
|
||||
char buff[1024];
|
||||
va_list args;
|
||||
va_start(args, str);
|
||||
i32 len = stbsp_vsnprintf(buff, sizeof(buff), str, args);
|
||||
va_end(args);
|
||||
|
||||
wasm_draw_text((isize)buff, len, wasm_dpr * pos.x, wasm_dpr * pos.y, (isize) font_face.str, font_face.len, 20*wasm_dpr, 0, 0, 0, 1);
|
||||
}
|
||||
|
||||
fn void draw_rect(r2f64_t rect, v4f32_t color) {
|
||||
wasm_draw_rect(wasm_dpr * rect.min.x, wasm_dpr * rect.min.y, wasm_dpr * (rect.max.x - rect.min.x), wasm_dpr * (rect.max.y - rect.min.y), color.r * 255.f, color.g * 255.f, color.b * 255.f, color.a);
|
||||
}
|
||||
|
||||
typedef enum {
|
||||
gfx_kind_null,
|
||||
gfx_kind_clear,
|
||||
gfx_kind_draw_rect,
|
||||
gfx_kind_draw_text,
|
||||
gfx_kind_set_clip,
|
||||
} gfx_kind_t;
|
||||
|
||||
typedef struct gfx_cmd_t gfx_cmd_t;
|
||||
struct gfx_cmd_t {
|
||||
gfx_cmd_t *next;
|
||||
gfx_kind_t kind;
|
||||
r2f64_t rect;
|
||||
v4f32_t color;
|
||||
s8_t text;
|
||||
};
|
||||
|
||||
typedef struct gfx_t gfx_t;
|
||||
struct gfx_t {
|
||||
gfx_cmd_t *first;
|
||||
gfx_cmd_t *last;
|
||||
app_event_t *ev;
|
||||
};
|
||||
|
||||
// @todo:
|
||||
typedef struct gfx_group_t gfx_group_t;
|
||||
struct gfx_group_t {
|
||||
gfx_cmd_t *first;
|
||||
gfx_cmd_t *last;
|
||||
};
|
||||
|
||||
void gfx_begin(gfx_t *gfx, app_event_t *ev) {
|
||||
gfx->ev = ev;
|
||||
}
|
||||
|
||||
void gfx_end(gfx_t *gfx) {
|
||||
app_event_t *ev = gfx->ev;
|
||||
r2f64_t window = (r2f64_t){0, 0, ev->window_size.x, ev->window_size.y};
|
||||
for (gfx_cmd_t *cmd = gfx->first; cmd; cmd = cmd->next) {
|
||||
if (cmd->kind == gfx_kind_clear) {
|
||||
wasm_clear();
|
||||
draw_rect(window, cmd->color);
|
||||
} else if (cmd->kind == gfx_kind_draw_rect) {
|
||||
draw_rect(cmd->rect, cmd->color);
|
||||
} else if (cmd->kind == gfx_kind_draw_text) {
|
||||
draw_text(cmd->rect.min, cmd->color, cmd->text);
|
||||
} else if (cmd->kind == gfx_kind_set_clip) {
|
||||
set_clip(cmd->rect);
|
||||
} else {
|
||||
invalid_codepath;
|
||||
}
|
||||
}
|
||||
|
||||
gfx->first = gfx->last = NULL;
|
||||
}
|
||||
|
||||
void gfx_add_cmd(gfx_t *gfx, gfx_cmd_t cmd) {
|
||||
gfx_cmd_t *c = ma_push_type(tcx.temp, gfx_cmd_t);
|
||||
*c = cmd;
|
||||
SLLQ_APPEND(gfx->first, gfx->last, c);
|
||||
}
|
||||
|
||||
void gfx_clear(gfx_t *gfx, v4f32_t color) {
|
||||
gfx_add_cmd(gfx, (gfx_cmd_t){
|
||||
.kind = gfx_kind_clear,
|
||||
.color = color,
|
||||
});
|
||||
}
|
||||
|
||||
void gfx_rect(gfx_t *gfx, r2f64_t rect, v4f32_t color) {
|
||||
gfx_add_cmd(gfx, (gfx_cmd_t){
|
||||
.kind = gfx_kind_draw_rect,
|
||||
.color = color,
|
||||
.rect = rect,
|
||||
});
|
||||
}
|
||||
|
||||
void gfx_set_clip(gfx_t *gfx, r2f64_t rect) {
|
||||
gfx_add_cmd(gfx, (gfx_cmd_t){
|
||||
.kind = gfx_kind_set_clip,
|
||||
.rect = rect,
|
||||
});
|
||||
}
|
||||
|
||||
void gfx_text(gfx_t *gfx, v2f64_t pos, v4f32_t color, s8_t string) {
|
||||
gfx_add_cmd(gfx, (gfx_cmd_t){
|
||||
.kind = gfx_kind_draw_text,
|
||||
.text = s8_copy(tcx.temp, string),
|
||||
.color = color,
|
||||
.rect.min = pos,
|
||||
});
|
||||
}
|
||||
void gfx_textf(gfx_t *gfx, v2f64_t pos, v4f32_t color, char *str, ...) {
|
||||
S8_FMT(tcx.temp, str, text);
|
||||
gfx_add_cmd(gfx, (gfx_cmd_t){
|
||||
.kind = gfx_kind_draw_text,
|
||||
.text = text,
|
||||
.color = color,
|
||||
.rect.min = pos,
|
||||
});
|
||||
}
|
||||
@@ -89,7 +89,8 @@ fn i64 rn_get_line_spacing(rn_font_t *font) {
|
||||
}
|
||||
|
||||
fn v2f32_t rn_base_draw_string(rn_font_t *font, s8_t string, v2f32_t pos, v4f32_t color, b32 draw) {
|
||||
pos.y += rn_get_line_spacing(font) + font->descent;
|
||||
// old formula :: pos.y += rn_get_line_spacing(font) + font->descent;
|
||||
pos.y += font->ascent;
|
||||
v2f32_t original_pos = pos;
|
||||
|
||||
for (utf8_iter_t iter = utf8_iterate_ex(string.str, (int)string.len); iter.item; utf8_advance(&iter)) {
|
||||
|
||||
@@ -4,6 +4,7 @@ void mt_render(ma_arena_t *arena) {
|
||||
|
||||
sb8_printf(sb, "gb_read_only ");
|
||||
s8_t content = OS_ReadFile(&Perm, s8_lit("../package/FiraCode-Regular.ttf"));
|
||||
// s8_t content = OS_ReadFile(&Perm, s8_lit("C:/Windows/Fonts/arial.ttf"));
|
||||
mt_serial_to_cbyte_array_ex(sb, content, s8_printf(arena, "main_font_data"));
|
||||
sb8_printf(sb, "gb_read_only s8_t main_font = s8_array_lit(main_font_data);\n");
|
||||
os_write_file(mt_cpath(arena), sb8_serial_end(arena, sb));
|
||||
|
||||
@@ -2,7 +2,7 @@ fn_wasm_import void wasm_clear(void);
|
||||
fn_wasm_import void wasm_draw_text(isize str, i32 len, f32 x, f32 y, isize font_str, i32 font_len, i32 font_size, f32 r, f32 g, f32 b, f32 a);
|
||||
fn_wasm_import void wasm_draw_rect(f32 x, f32 y, f32 w, f32 h, f32 r, f32 g, f32 b, f32 a);
|
||||
fn_wasm_import f32 wasm_measure_text(isize str, i32 len, isize font_str, i32 font_len, i32 font_size);
|
||||
fn_wasm_import f32 wasm_get_font_height(isize font_str, i32 font_len, i32 font_size);
|
||||
// fn_wasm_import f32 wasm_get_font_height(isize font_str, i32 font_len, i32 font_size);
|
||||
fn_wasm_import void wasm_set_clip(f32 x, f32 y, f32 w, f32 h);
|
||||
|
||||
gb_read_only s8_t font_face = s8_const_lit("main_font");
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
fn void app_init(f32 dpr) {
|
||||
ma_arena_t *perm = &tcx._perm;
|
||||
unused(perm);
|
||||
mt_tweak_f32(font_size, 50, 4, 200);
|
||||
mt_tweak_f32(_font_size, 50, 50, 50);
|
||||
|
||||
@@ -27,8 +26,5 @@ fn b32 app_update(app_frame_t *frame) {
|
||||
rn_reload_font(font_size, frame->dpr);
|
||||
}
|
||||
ui_demo_update(frame);
|
||||
// v2f32_t size = rn_measure_string(rn_state.main_font, s8_lit("Memes and other things"));
|
||||
// debugf("font size = %f %f", size.x, size.y);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
6
todo.txt
6
todo.txt
@@ -15,8 +15,10 @@
|
||||
[ ] linux
|
||||
|
||||
[ ] render
|
||||
[ ] fix opengl warnings on win32
|
||||
[ ] wasm (maybe try first without opengl and then opengl) <<<<<<<<<<<
|
||||
[x] fix opengl warnings on win32 -> no need
|
||||
[ ] wasm (maybe try first without opengl and then opengl)
|
||||
[x] canvas
|
||||
[ ] opengl
|
||||
[ ] maybe copy package stuff to build?
|
||||
|
||||
[ ] ui
|
||||
|
||||
Reference in New Issue
Block a user