Compare commits

..

4 Commits

5 changed files with 79 additions and 23 deletions

View File

@ -5,6 +5,7 @@ import ugui;
import rl;
import std::time;
import std::collections::ringbuffer;
import std::core::string;
def Times = ringbuffer::RingBuffer(<time::NanoDuration, 128>);
@ -23,6 +24,26 @@ fn void Times.print_stats(&times)
io::printfn("min=%s, max=%s, avg=%s", min, max, avg);
}
struct TimeStats {
time::NanoDuration min, max, avg;
}
fn TimeStats Times.get_stats(&times)
{
time::NanoDuration min, max, avg, x;
min = times.get(0);
for (usz i = 0; i < times.written; i++) {
x = times.get(i);
if (x < min) { min = x; }
if (x > max) { max = x; }
avg += x;
}
avg = (NanoDuration)((ulong)avg/128.0);
return TimeStats{.min = min, .max = max, .avg = avg};
}
const ZString FONT_FS = `
#version 330
@ -64,7 +85,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()) {
@ -141,6 +167,23 @@ fn int main(String[] args)
|};
ui.div_end()!!;
// Timings counter
TimeStats dts = draw_times.get_stats();
TimeStats uts = ui_times.get_stats();
ui.layout_set_floating()!!;
ui.div_begin("fps", ugui::Rect{0, ui.height-50, 200, 50})!!;
{|
ui.layout_set_row()!!;
ui.text_unbounded("ui avg", string::tformat("ui avg: %s", uts.avg))!!;
ui.layout_next_row()!!;
ui.text_unbounded("draw avg", string::tformat("draw avg: %s", dts.avg))!!;
//ui.force_update()!!;
|};
ui.div_end()!!;
ui.frame_end()!!;
/* End UI Handling */
ui_times.push(clock.mark());
@ -152,16 +195,14 @@ 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:
c = rl::Color{
.r = cmd.rect.color.r,
.g = cmd.rect.color.g,
.b = cmd.rect.color.b,
.a = cmd.rect.color.a,
.r = cmd.rect.color.r,
.g = cmd.rect.color.g,
.b = cmd.rect.color.b,
.a = cmd.rect.color.a,
};
r = rl::Rectangle{
.x = cmd.rect.rect.x,
@ -169,12 +210,13 @@ fn int main(String[] args)
.height = cmd.rect.rect.h,
.width = cmd.rect.rect.w,
};
float rad = cmd.rect.radius;
float rad = cmd.rect.radius;
// for some weird-ass reason the straight forward inverse formula does not work
float roundness = r.width > r.height ? (2.1*rad)/r.height : (2.1*rad)/r.width;
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 +228,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,

View File

@ -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'){

View File

@ -39,7 +39,8 @@ fn void! Ctx.div_begin(&ctx, String label, Rect size)
}
// Add the background to the draw stack
ctx.push_rect(c_elem.bounds, c_elem.div.color_bg)!;
bool do_border = parent.div.layout == LAYOUT_FLOATING;
ctx.push_rect(c_elem.bounds, c_elem.div.color_bg, do_border: do_border)!;
// TODO: check active
// TODO: check resizeable

View File

@ -92,9 +92,9 @@ fn void! Font.load(&font, String name, ZString path, uint height, float scale)
font.atlas.new(font.id, ATLAS_GRAYSCALE, size, size)!;
// preallocate the ASCII range
// for (char c = ' '; c < '~'; c++) {
// font.get_glyph((Codepoint)c)!;
// }
for (char c = ' '; c < '~'; c++) {
font.get_glyph((Codepoint)c)!;
}
}
fn Glyph*! Font.get_glyph(&font, Codepoint code)
@ -211,3 +211,9 @@ 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();
}

View File

@ -120,6 +120,9 @@ fn void! Ctx.force_update(&ctx)
fn void! Ctx.frame_end(&ctx)
{
Elem* root = ctx.get_elem_by_tree_idx(0)!;
root.div.layout = LAYOUT_ROW;
// 1. clear the tree
ctx.tree.prune(0)!;