specify texture id in the sprite command

c3
Alessandro Mauri 4 months ago
parent 7b7aac8df4
commit 3a7655a3f0
  1. 11
      src/main.c3
  2. 23
      src/ugui_cmd.c3
  3. 6
      src/ugui_font.c3

@ -64,7 +64,12 @@ fn int main(String[] args)
time::Clock clock;
Times ui_times;
Times draw_times;
// font stuff
rl::Shader font_shader = rl::load_shader_from_memory(null, FONT_FS);
rl::Image font_atlas;
rl::Texture2D font_texture;
ugui::Id font_id = ui.get_font_id("font1");
// Main loop
while (!rl::window_should_close()) {
@ -152,8 +157,6 @@ fn int main(String[] args)
rl::Color c;
rl::Rectangle r;
static rl::Image font_atlas;
static rl::Texture2D font_texture;
for (Cmd* cmd; (cmd = ui.cmd_queue.dequeue() ?? null) != null;) {
switch (cmd.type) {
case ugui::CmdType.CMD_RECT:
@ -174,7 +177,8 @@ fn int main(String[] args)
float roundness = r.width > r.height ? (2.1*rad)/r.height : (2.1*rad)/r.width;
rl::draw_rectangle_rounded(r, roundness, 0, c);
case ugui::CmdType.CMD_UPDATE_ATLAS:
rl::unload_image(font_atlas);
if (cmd.update_atlas.id != font_id) { break; }
//rl::unload_image(font_atlas);
font_atlas.data = cmd.update_atlas.raw_buffer;
font_atlas.width = cmd.update_atlas.width;
font_atlas.height = cmd.update_atlas.height;
@ -186,6 +190,7 @@ fn int main(String[] args)
}
font_texture = rl::load_texture_from_image(font_atlas);
case ugui::CmdType.CMD_SPRITE:
if (cmd.sprite.texture_id != font_id) { break; }
rl::Rectangle source = {
.x = cmd.sprite.texture_rect.x,
.y = cmd.sprite.texture_rect.y,

@ -29,6 +29,7 @@ struct CmdUpdateAtlas {
struct CmdSprite {
Rect rect;
Rect texture_rect;
Id texture_id;
}
// if rect is zero Rect{0} then reset the scissor
@ -86,12 +87,13 @@ fn void! Ctx.push_rect(&ctx, Rect rect, Color color, bool do_border = false, boo
}
// TODO: add texture id
fn void! Ctx.push_sprite(&ctx, Rect bounds, Rect texture)
fn void! Ctx.push_sprite(&ctx, Rect bounds, Rect texture, Id texture_id)
{
Cmd cmd = {
.type = CMD_SPRITE,
.sprite.rect = bounds,
.sprite.texture_rect = texture,
.sprite.texture_id = texture_id,
};
ctx.cmd_queue.enqueue(&cmd)!;
}
@ -107,6 +109,7 @@ fn void! Ctx.push_string(&ctx, Rect bounds, String text)
short baseline = (short)ctx.font.ascender;
short line_height = (short)ctx.font.ascender - (short)ctx.font.descender;
short line_gap = (short)ctx.font.linegap;
Id texture_id = ctx.font.id; // or ctx.font.atlas.id
Point orig = {
.x = bounds.x,
.y = bounds.y,
@ -121,19 +124,19 @@ fn void! Ctx.push_string(&ctx, Rect bounds, String text)
if (!ascii::is_cntrl((char)cp)) {
gp = ctx.font.get_glyph(cp)!;
Rect gb = {
.x = orig.x + line_len + gp.ox,
.y = orig.y + gp.oy + baseline,
.w = gp.w,
.h = gp.h,
.x = orig.x + line_len + gp.ox,
.y = orig.y + gp.oy + baseline,
.w = gp.w,
.h = gp.h,
};
Rect gt = {
.x = gp.u,
.y = gp.v,
.w = gp.w,
.h = gp.h,
.x = gp.u,
.y = gp.v,
.w = gp.w,
.h = gp.h,
};
if (rect_collision(gb, bounds)) {
ctx.push_sprite(gb, gt)!;
ctx.push_sprite(gb, gt, texture_id)!;
}
line_len += gp.adv;
} else if (cp == '\n'){

@ -210,4 +210,10 @@ fn Rect! Ctx.get_text_bounds(&ctx, String text)
}
return text_bounds;
}
// TODO: check if the font is present in the context
fn Id Ctx.get_font_id(&ctx, String label)
{
return (Id)label.hash();
}
Loading…
Cancel
Save