starting to use macros because they are powerful
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 13 Aug 2021 15:48:54 +0000 (10:48 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 13 Aug 2021 15:48:54 +0000 (10:48 -0500)
core/container/array.onyx
modules/bmfont/bmfont_loader.onyx
modules/ui/ui.onyx

index 3893f016a725d191dfa842a3dbe032bb421d94f8..5e66d6e2ba3f0dd7c42dd329666cc8fa0713d40f 100644 (file)
@@ -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 {
index 8e98e384f185206450f458022d9bff6e8da835c3..5d7656dca74981edfc7561599d028a15561b31b1 100644 (file)
@@ -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;
index 972b3a14349bbff7fcf327e963fa1d8bf3ecf97f..66db315abb9c02d76eab1ece8a3b82a4bd901ae0 100644 (file)
@@ -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);
 }