Compare commits
4 Commits
7b7aac8df4
...
c1a7b4fcdb
Author | SHA1 | Date | |
---|---|---|---|
c1a7b4fcdb | |||
7d9a8a1363 | |||
2e0c6333d3 | |||
3a7655a3f0 |
61
src/main.c3
61
src/main.c3
@ -5,6 +5,7 @@ import ugui;
|
|||||||
import rl;
|
import rl;
|
||||||
import std::time;
|
import std::time;
|
||||||
import std::collections::ringbuffer;
|
import std::collections::ringbuffer;
|
||||||
|
import std::core::string;
|
||||||
|
|
||||||
def Times = ringbuffer::RingBuffer(<time::NanoDuration, 128>);
|
def Times = ringbuffer::RingBuffer(<time::NanoDuration, 128>);
|
||||||
|
|
||||||
@ -23,6 +24,26 @@ fn void Times.print_stats(×)
|
|||||||
io::printfn("min=%s, max=%s, avg=%s", min, max, avg);
|
io::printfn("min=%s, max=%s, avg=%s", min, max, avg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct TimeStats {
|
||||||
|
time::NanoDuration min, max, avg;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn TimeStats Times.get_stats(×)
|
||||||
|
{
|
||||||
|
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 = `
|
const ZString FONT_FS = `
|
||||||
#version 330
|
#version 330
|
||||||
|
|
||||||
@ -64,7 +85,12 @@ fn int main(String[] args)
|
|||||||
time::Clock clock;
|
time::Clock clock;
|
||||||
Times ui_times;
|
Times ui_times;
|
||||||
Times draw_times;
|
Times draw_times;
|
||||||
|
|
||||||
|
// font stuff
|
||||||
rl::Shader font_shader = rl::load_shader_from_memory(null, FONT_FS);
|
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
|
// Main loop
|
||||||
while (!rl::window_should_close()) {
|
while (!rl::window_should_close()) {
|
||||||
@ -141,6 +167,23 @@ fn int main(String[] args)
|
|||||||
|};
|
|};
|
||||||
ui.div_end()!!;
|
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()!!;
|
ui.frame_end()!!;
|
||||||
/* End UI Handling */
|
/* End UI Handling */
|
||||||
ui_times.push(clock.mark());
|
ui_times.push(clock.mark());
|
||||||
@ -152,16 +195,14 @@ fn int main(String[] args)
|
|||||||
|
|
||||||
rl::Color c;
|
rl::Color c;
|
||||||
rl::Rectangle r;
|
rl::Rectangle r;
|
||||||
static rl::Image font_atlas;
|
|
||||||
static rl::Texture2D font_texture;
|
|
||||||
for (Cmd* cmd; (cmd = ui.cmd_queue.dequeue() ?? null) != null;) {
|
for (Cmd* cmd; (cmd = ui.cmd_queue.dequeue() ?? null) != null;) {
|
||||||
switch (cmd.type) {
|
switch (cmd.type) {
|
||||||
case ugui::CmdType.CMD_RECT:
|
case ugui::CmdType.CMD_RECT:
|
||||||
c = rl::Color{
|
c = rl::Color{
|
||||||
.r = cmd.rect.color.r,
|
.r = cmd.rect.color.r,
|
||||||
.g = cmd.rect.color.g,
|
.g = cmd.rect.color.g,
|
||||||
.b = cmd.rect.color.b,
|
.b = cmd.rect.color.b,
|
||||||
.a = cmd.rect.color.a,
|
.a = cmd.rect.color.a,
|
||||||
};
|
};
|
||||||
r = rl::Rectangle{
|
r = rl::Rectangle{
|
||||||
.x = cmd.rect.rect.x,
|
.x = cmd.rect.rect.x,
|
||||||
@ -169,12 +210,13 @@ fn int main(String[] args)
|
|||||||
.height = cmd.rect.rect.h,
|
.height = cmd.rect.rect.h,
|
||||||
.width = cmd.rect.rect.w,
|
.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
|
// 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);
|
rl::draw_rectangle_rounded(r, roundness, 0, c);
|
||||||
case ugui::CmdType.CMD_UPDATE_ATLAS:
|
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.data = cmd.update_atlas.raw_buffer;
|
||||||
font_atlas.width = cmd.update_atlas.width;
|
font_atlas.width = cmd.update_atlas.width;
|
||||||
font_atlas.height = cmd.update_atlas.height;
|
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);
|
font_texture = rl::load_texture_from_image(font_atlas);
|
||||||
case ugui::CmdType.CMD_SPRITE:
|
case ugui::CmdType.CMD_SPRITE:
|
||||||
|
if (cmd.sprite.texture_id != font_id) { break; }
|
||||||
rl::Rectangle source = {
|
rl::Rectangle source = {
|
||||||
.x = cmd.sprite.texture_rect.x,
|
.x = cmd.sprite.texture_rect.x,
|
||||||
.y = cmd.sprite.texture_rect.y,
|
.y = cmd.sprite.texture_rect.y,
|
||||||
|
@ -29,6 +29,7 @@ struct CmdUpdateAtlas {
|
|||||||
struct CmdSprite {
|
struct CmdSprite {
|
||||||
Rect rect;
|
Rect rect;
|
||||||
Rect texture_rect;
|
Rect texture_rect;
|
||||||
|
Id texture_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if rect is zero Rect{0} then reset the scissor
|
// 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
|
// 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 = {
|
Cmd cmd = {
|
||||||
.type = CMD_SPRITE,
|
.type = CMD_SPRITE,
|
||||||
.sprite.rect = bounds,
|
.sprite.rect = bounds,
|
||||||
.sprite.texture_rect = texture,
|
.sprite.texture_rect = texture,
|
||||||
|
.sprite.texture_id = texture_id,
|
||||||
};
|
};
|
||||||
ctx.cmd_queue.enqueue(&cmd)!;
|
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 baseline = (short)ctx.font.ascender;
|
||||||
short line_height = (short)ctx.font.ascender - (short)ctx.font.descender;
|
short line_height = (short)ctx.font.ascender - (short)ctx.font.descender;
|
||||||
short line_gap = (short)ctx.font.linegap;
|
short line_gap = (short)ctx.font.linegap;
|
||||||
|
Id texture_id = ctx.font.id; // or ctx.font.atlas.id
|
||||||
Point orig = {
|
Point orig = {
|
||||||
.x = bounds.x,
|
.x = bounds.x,
|
||||||
.y = bounds.y,
|
.y = bounds.y,
|
||||||
@ -121,19 +124,19 @@ fn void! Ctx.push_string(&ctx, Rect bounds, String text)
|
|||||||
if (!ascii::is_cntrl((char)cp)) {
|
if (!ascii::is_cntrl((char)cp)) {
|
||||||
gp = ctx.font.get_glyph(cp)!;
|
gp = ctx.font.get_glyph(cp)!;
|
||||||
Rect gb = {
|
Rect gb = {
|
||||||
.x = orig.x + line_len + gp.ox,
|
.x = orig.x + line_len + gp.ox,
|
||||||
.y = orig.y + gp.oy + baseline,
|
.y = orig.y + gp.oy + baseline,
|
||||||
.w = gp.w,
|
.w = gp.w,
|
||||||
.h = gp.h,
|
.h = gp.h,
|
||||||
};
|
};
|
||||||
Rect gt = {
|
Rect gt = {
|
||||||
.x = gp.u,
|
.x = gp.u,
|
||||||
.y = gp.v,
|
.y = gp.v,
|
||||||
.w = gp.w,
|
.w = gp.w,
|
||||||
.h = gp.h,
|
.h = gp.h,
|
||||||
};
|
};
|
||||||
if (rect_collision(gb, bounds)) {
|
if (rect_collision(gb, bounds)) {
|
||||||
ctx.push_sprite(gb, gt)!;
|
ctx.push_sprite(gb, gt, texture_id)!;
|
||||||
}
|
}
|
||||||
line_len += gp.adv;
|
line_len += gp.adv;
|
||||||
} else if (cp == '\n'){
|
} else if (cp == '\n'){
|
||||||
|
@ -39,7 +39,8 @@ fn void! Ctx.div_begin(&ctx, String label, Rect size)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add the background to the draw stack
|
// 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 active
|
||||||
// TODO: check resizeable
|
// TODO: check resizeable
|
||||||
|
@ -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)!;
|
font.atlas.new(font.id, ATLAS_GRAYSCALE, size, size)!;
|
||||||
|
|
||||||
// preallocate the ASCII range
|
// preallocate the ASCII range
|
||||||
// for (char c = ' '; c < '~'; c++) {
|
for (char c = ' '; c < '~'; c++) {
|
||||||
// font.get_glyph((Codepoint)c)!;
|
font.get_glyph((Codepoint)c)!;
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn Glyph*! Font.get_glyph(&font, Codepoint code)
|
fn Glyph*! Font.get_glyph(&font, Codepoint code)
|
||||||
@ -211,3 +211,9 @@ fn Rect! Ctx.get_text_bounds(&ctx, String text)
|
|||||||
|
|
||||||
return text_bounds;
|
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();
|
||||||
|
}
|
@ -120,6 +120,9 @@ fn void! Ctx.force_update(&ctx)
|
|||||||
|
|
||||||
fn void! Ctx.frame_end(&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
|
// 1. clear the tree
|
||||||
ctx.tree.prune(0)!;
|
ctx.tree.prune(0)!;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user