From 396a506fb48b2d5bfcd3bef753c7960bd9d0aaf9 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Fri, 13 Aug 2021 10:48:54 -0500 Subject: [PATCH] starting to use macros because they are powerful --- core/container/array.onyx | 21 +++++++++++++-------- modules/bmfont/bmfont_loader.onyx | 10 +++++----- modules/ui/ui.onyx | 2 +- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/core/container/array.onyx b/core/container/array.onyx index 3893f016..5e66d6e2 100644 --- a/core/container/array.onyx +++ b/core/container/array.onyx @@ -74,6 +74,9 @@ push :: (arr: ^[..] $T, x: T) -> bool { return true; } +// Semi-useful shortcut for adding something to an array. +#operator << macro (arr: [..] $T, v: T) do array.push(^arr, v); + insert :: (arr: ^[..] $T, idx: u32, x: T) -> bool { if !ensure_capacity(arr, arr.count + 1) do return false; @@ -305,14 +308,16 @@ fold :: #match { } map :: #match { - (arr: ^[..] $T, f: (^T) -> void) do for ^it: *arr do f(it);, - (arr: ^[..] $T, f: (T) -> T) do for ^it: *arr do *it = f(*it);, - (arr: ^[..] $T, data: $R, f: (^T, R) -> void) do for ^it: *arr do f(it, data);, - (arr: ^[..] $T, data: $R, f: (T, R) -> T) do for ^it: *arr do *it = f(*it, data);, - (arr: [] $T, f: (^T) -> void) do for ^it: arr do f(it);, - (arr: [] $T, f: (T) -> T) do for ^it: arr do *it = f(*it);, - (arr: [] $T, data: $R, f: (^T, R) -> void) do for ^it: arr do f(it, data);, - (arr: [] $T, data: $R, f: (T, R) -> T) do for ^it: arr do *it = f(*it, data);, + macro (arr: [..] $T, f: (^T) -> void) do for ^it: arr do f(it);, + macro (arr: [..] $T, f: (T) -> T) do for ^it: arr do *it = f(*it);, + macro (arr: [..] $T, body: Code) do for ^it: arr do #insert body;, + macro (arr: [..] $T, data: $R, f: (^T, R) -> void) do for ^it: arr do f(it, data);, + macro (arr: [..] $T, data: $R, f: (T, R) -> T) do for ^it: arr do *it = f(*it, data);, + macro (arr: [] $T, f: (^T) -> void) do for ^it: arr do f(it);, + macro (arr: [] $T, f: (T) -> T) do for ^it: arr do *it = f(*it);, + macro (arr: [] $T, body: Code) do for ^it: arr do #insert body;, + macro (arr: [] $T, data: $R, f: (^T, R) -> void) do for ^it: arr do f(it, data);, + macro (arr: [] $T, data: $R, f: (T, R) -> T) do for ^it: arr do *it = f(*it, data);, } every :: (arr: ^[..] $T, predicate: (T) -> bool) -> bool { diff --git a/modules/bmfont/bmfont_loader.onyx b/modules/bmfont/bmfont_loader.onyx index 8e98e384..5d7656dc 100644 --- a/modules/bmfont/bmfont_loader.onyx +++ b/modules/bmfont/bmfont_loader.onyx @@ -13,11 +13,11 @@ load_bmfont :: (fnt_data: [] u8) -> BMFont { parse_bmfont(fnt_data, ^bmf); @Cleanup // this was a stupid way of doing this. Just use a f-ing for loop. - array.map(^bmf.glyphs.entries, ^bmf, (glyph: ^map.Map.Entry(i32, BMFont_Glyph), font: ^BMFont) { - glyph.value.tex_x = ~~ glyph.value.x / cast(f32) font.common.scale_width; - glyph.value.tex_y = ~~ glyph.value.y / cast(f32) font.common.scale_height; - glyph.value.tex_w = ~~ glyph.value.w / cast(f32) font.common.scale_width; - glyph.value.tex_h = ~~ glyph.value.h / cast(f32) font.common.scale_height; + array.map(bmf.glyphs.entries, #code { + it.value.tex_x = ~~ it.value.x / cast(f32) bmf.common.scale_width; + it.value.tex_y = ~~ it.value.y / cast(f32) bmf.common.scale_height; + it.value.tex_w = ~~ it.value.w / cast(f32) bmf.common.scale_width; + it.value.tex_h = ~~ it.value.h / cast(f32) bmf.common.scale_height; }); return bmf; diff --git a/modules/ui/ui.onyx b/modules/ui/ui.onyx index 972b3a14..66db315a 100644 --- a/modules/ui/ui.onyx +++ b/modules/ui/ui.onyx @@ -115,7 +115,7 @@ draw_rect :: #match { } } -draw_text :: (use r: Rectangle, text: str, theme := ^default_text_theme, site := #callsite) -> bool { +draw_text :: (use r: Rectangle, text: str, theme := ^default_text_theme, site := #callsite) { draw_text_raw(text, x0, y0 + current_font->get_baseline(theme.font_size), theme.font, theme.font_size, theme.text_color); } -- 2.25.1