added debug_log
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 12 Jul 2021 23:31:55 +0000 (18:31 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 12 Jul 2021 23:31:55 +0000 (18:31 -0500)
res/fonts/mononoki.fnt [new file with mode: 0644]
res/fonts/mononoki.png [new file with mode: 0644]
src/build.onyx
src/config.onyx
src/debug_log.onyx [new file with mode: 0644]
src/main.onyx

diff --git a/res/fonts/mononoki.fnt b/res/fonts/mononoki.fnt
new file mode 100644 (file)
index 0000000..56ddbdd
--- /dev/null
@@ -0,0 +1,102 @@
+info face="mononoki-Regular Nerd Font Complete" size=32 bold=0 italic=0 charset="" unicode=0 stretchH=100 smooth=1 aa=1 padding=1,1,1,1 spacing=-2,-2
+common lineHeight=37 base=29 scaleW=256 scaleH=256 pages=1 packed=0
+page id=0 file="mononoki.png"
+chars count=97
+char id=0       x=52   y=108  width=16   height=24   xoffset=1    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=10      x=0    y=0    width=0    height=0    xoffset=-1   yoffset=0    xadvance=0    page=0    chnl=0 
+char id=32      x=0    y=0    width=0    height=0    xoffset=-1   yoffset=0    xadvance=18   page=0    chnl=0 
+char id=33      x=244  y=84   width=6    height=24   xoffset=6    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=34      x=146  y=132  width=14   height=13   xoffset=2    yoffset=4    xadvance=18   page=0    chnl=0 
+char id=35      x=16   y=108  width=18   height=24   xoffset=0    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=36      x=0    y=0    width=18   height=34   xoffset=0    yoffset=2    xadvance=18   page=0    chnl=0 
+char id=37      x=116  y=108  width=18   height=21   xoffset=0    yoffset=9    xadvance=18   page=0    chnl=0 
+char id=38      x=34   y=108  width=18   height=24   xoffset=0    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=39      x=160  y=132  width=6    height=13   xoffset=6    yoffset=4    xadvance=18   page=0    chnl=0 
+char id=40      x=18   y=0    width=15   height=32   xoffset=1    yoffset=5    xadvance=18   page=0    chnl=0 
+char id=41      x=67   y=0    width=15   height=31   xoffset=2    yoffset=5    xadvance=18   page=0    chnl=0 
+char id=42      x=116  y=132  width=18   height=17   xoffset=0    yoffset=12   xadvance=18   page=0    chnl=0 
+char id=43      x=98   y=132  width=18   height=18   xoffset=0    yoffset=12   xadvance=18   page=0    chnl=0 
+char id=44      x=134  y=132  width=12   height=14   xoffset=1    yoffset=24   xadvance=18   page=0    chnl=0 
+char id=45      x=0    y=150  width=18   height=5    xoffset=0    yoffset=18   xadvance=18   page=0    chnl=0 
+char id=46      x=234  y=132  width=8    height=6    xoffset=5    yoffset=24   xadvance=18   page=0    chnl=0 
+char id=47      x=152  y=0    width=18   height=30   xoffset=0    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=48      x=226  y=84   width=18   height=24   xoffset=0    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=49      x=78   y=84   width=16   height=24   xoffset=1    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=50      x=94   y=84   width=17   height=24   xoffset=1    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=51      x=111  y=84   width=15   height=24   xoffset=2    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=52      x=126  y=84   width=16   height=24   xoffset=1    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=53      x=142  y=84   width=15   height=24   xoffset=2    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=54      x=157  y=84   width=17   height=24   xoffset=0    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=55      x=174  y=84   width=17   height=24   xoffset=1    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=56      x=191  y=84   width=18   height=24   xoffset=0    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=57      x=209  y=84   width=17   height=24   xoffset=0    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=58      x=247  y=108  width=8    height=18   xoffset=5    yoffset=12   xadvance=18   page=0    chnl=0 
+char id=59      x=133  y=34   width=12   height=26   xoffset=1    yoffset=12   xadvance=18   page=0    chnl=0 
+char id=60      x=84   y=108  width=16   height=22   xoffset=1    yoffset=10   xadvance=18   page=0    chnl=0 
+char id=61      x=186  y=132  width=18   height=12   xoffset=0    yoffset=15   xadvance=18   page=0    chnl=0 
+char id=62      x=100  y=108  width=16   height=22   xoffset=1    yoffset=10   xadvance=18   page=0    chnl=0 
+char id=63      x=0    y=108  width=16   height=24   xoffset=1    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=64      x=170  y=0    width=18   height=30   xoffset=0    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=65      x=145  y=34   width=20   height=24   xoffset=-1   yoffset=6    xadvance=18   page=0    chnl=0 
+char id=66      x=165  y=34   width=18   height=24   xoffset=0    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=67      x=183  y=34   width=16   height=24   xoffset=1    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=68      x=199  y=34   width=18   height=24   xoffset=0    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=69      x=217  y=34   width=17   height=24   xoffset=1    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=70      x=234  y=34   width=17   height=24   xoffset=1    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=71      x=0    y=60   width=16   height=24   xoffset=1    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=72      x=16   y=60   width=16   height=24   xoffset=1    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=73      x=32   y=60   width=16   height=24   xoffset=1    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=74      x=48   y=60   width=16   height=24   xoffset=1    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=75      x=64   y=60   width=17   height=24   xoffset=1    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=76      x=81   y=60   width=18   height=24   xoffset=1    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=77      x=99   y=60   width=20   height=24   xoffset=-1   yoffset=6    xadvance=18   page=0    chnl=0 
+char id=78      x=119  y=60   width=16   height=24   xoffset=1    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=79      x=135  y=60   width=16   height=24   xoffset=1    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=80      x=151  y=60   width=17   height=24   xoffset=1    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=81      x=33   y=0    width=18   height=31   xoffset=1    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=82      x=168  y=60   width=18   height=24   xoffset=1    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=83      x=186  y=60   width=17   height=24   xoffset=1    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=84      x=203  y=60   width=20   height=24   xoffset=-1   yoffset=6    xadvance=18   page=0    chnl=0 
+char id=85      x=223  y=60   width=16   height=24   xoffset=1    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=86      x=0    y=84   width=20   height=24   xoffset=-1   yoffset=6    xadvance=18   page=0    chnl=0 
+char id=87      x=20   y=84   width=20   height=24   xoffset=-1   yoffset=6    xadvance=18   page=0    chnl=0 
+char id=88      x=40   y=84   width=18   height=24   xoffset=0    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=89      x=58   y=84   width=20   height=24   xoffset=-1   yoffset=6    xadvance=18   page=0    chnl=0 
+char id=90      x=239  y=60   width=16   height=24   xoffset=1    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=91      x=82   y=0    width=14   height=31   xoffset=3    yoffset=4    xadvance=18   page=0    chnl=0 
+char id=92      x=188  y=0    width=18   height=30   xoffset=0    yoffset=6    xadvance=18   page=0    chnl=0 
+char id=93      x=96   y=0    width=14   height=31   xoffset=1    yoffset=4    xadvance=18   page=0    chnl=0 
+char id=94      x=166  y=132  width=20   height=12   xoffset=-1   yoffset=5    xadvance=18   page=0    chnl=0 
+char id=95      x=18   y=150  width=18   height=5    xoffset=0    yoffset=27   xadvance=18   page=0    chnl=0 
+char id=96      x=222  y=132  width=12   height=8    xoffset=2    yoffset=5    xadvance=18   page=0    chnl=0 
+char id=97      x=134  y=108  width=15   height=18   xoffset=1    yoffset=12   xadvance=18   page=0    chnl=0 
+char id=98      x=206  y=0    width=16   height=26   xoffset=1    yoffset=4    xadvance=18   page=0    chnl=0 
+char id=99      x=149  y=108  width=16   height=18   xoffset=1    yoffset=12   xadvance=18   page=0    chnl=0 
+char id=100     x=222  y=0    width=16   height=26   xoffset=1    yoffset=4    xadvance=18   page=0    chnl=0 
+char id=101     x=165  y=108  width=16   height=18   xoffset=1    yoffset=12   xadvance=18   page=0    chnl=0 
+char id=102     x=238  y=0    width=16   height=26   xoffset=1    yoffset=4    xadvance=18   page=0    chnl=0 
+char id=103     x=0    y=34   width=19   height=26   xoffset=0    yoffset=12   xadvance=18   page=0    chnl=0 
+char id=104     x=19   y=34   width=16   height=26   xoffset=1    yoffset=4    xadvance=18   page=0    chnl=0 
+char id=105     x=35   y=34   width=16   height=26   xoffset=1    yoffset=4    xadvance=18   page=0    chnl=0 
+char id=106     x=51   y=0    width=16   height=31   xoffset=1    yoffset=4    xadvance=18   page=0    chnl=0 
+char id=107     x=51   y=34   width=16   height=26   xoffset=1    yoffset=4    xadvance=18   page=0    chnl=0 
+char id=108     x=67   y=34   width=16   height=26   xoffset=1    yoffset=4    xadvance=18   page=0    chnl=0 
+char id=109     x=181  y=108  width=18   height=18   xoffset=0    yoffset=12   xadvance=18   page=0    chnl=0 
+char id=110     x=199  y=108  width=16   height=18   xoffset=1    yoffset=12   xadvance=18   page=0    chnl=0 
+char id=111     x=215  y=108  width=16   height=18   xoffset=1    yoffset=12   xadvance=18   page=0    chnl=0 
+char id=112     x=83   y=34   width=16   height=26   xoffset=1    yoffset=12   xadvance=18   page=0    chnl=0 
+char id=113     x=99   y=34   width=16   height=26   xoffset=1    yoffset=12   xadvance=18   page=0    chnl=0 
+char id=114     x=231  y=108  width=16   height=18   xoffset=1    yoffset=12   xadvance=18   page=0    chnl=0 
+char id=115     x=0    y=132  width=14   height=18   xoffset=2    yoffset=12   xadvance=18   page=0    chnl=0 
+char id=116     x=68   y=108  width=16   height=23   xoffset=1    yoffset=7    xadvance=18   page=0    chnl=0 
+char id=117     x=14   y=132  width=16   height=18   xoffset=1    yoffset=12   xadvance=18   page=0    chnl=0 
+char id=118     x=30   y=132  width=16   height=18   xoffset=1    yoffset=12   xadvance=18   page=0    chnl=0 
+char id=119     x=46   y=132  width=20   height=18   xoffset=-1   yoffset=12   xadvance=18   page=0    chnl=0 
+char id=120     x=66   y=132  width=16   height=18   xoffset=1    yoffset=12   xadvance=18   page=0    chnl=0 
+char id=121     x=115  y=34   width=18   height=26   xoffset=-1   yoffset=12   xadvance=18   page=0    chnl=0 
+char id=122     x=82   y=132  width=16   height=18   xoffset=1    yoffset=12   xadvance=18   page=0    chnl=0 
+char id=123     x=110  y=0    width=18   height=31   xoffset=0    yoffset=4    xadvance=18   page=0    chnl=0 
+char id=124     x=146  y=0    width=6    height=31   xoffset=6    yoffset=4    xadvance=18   page=0    chnl=0 
+char id=125     x=128  y=0    width=18   height=31   xoffset=0    yoffset=4    xadvance=18   page=0    chnl=0 
+char id=126     x=204  y=132  width=18   height=9    xoffset=0    yoffset=17   xadvance=18   page=0    chnl=0 
+kernings count=0
diff --git a/res/fonts/mononoki.png b/res/fonts/mononoki.png
new file mode 100644 (file)
index 0000000..799ada9
Binary files /dev/null and b/res/fonts/mononoki.png differ
index 5d96599c280aab72ba186b1507d150d4fa1c841c..a651694d60891548463b780d6d4957520ab7f075 100644 (file)
@@ -14,6 +14,7 @@
 
     #load "src/main"
     #load "src/ui/window"
+    #load "src/debug_log"
 }
 
 #if (package runtime).Runtime == (package runtime).Runtime_Wasi {
index fc36faca32ab9a8df1ab4a9b19f3fef5233c487b..260d83defc0b1490d3b026ccda32c38996436e2b 100644 (file)
@@ -4,6 +4,7 @@ package config
 
 use package immediate_mode { Color4 }
 
+color_scheme_file :: "/res/colors_dark.json"
 
 Colors : struct {
     dark_background : Color4;
diff --git a/src/debug_log.onyx b/src/debug_log.onyx
new file mode 100644 (file)
index 0000000..ca8b309
--- /dev/null
@@ -0,0 +1,82 @@
+package debug
+
+use package core
+#private_file ui  :: package ui
+#private_file gfx :: package immediate_mode
+
+init :: () {
+    log_buffer.line_arena = alloc.arena.make(context.allocator, 4096);
+    log_buffer.lines = array.make(str);
+
+    debug_log(.Debug, "Debug system initialized.", null);
+}
+
+Severity :: enum {
+    Debug;
+    Info;
+    Warning;
+    Error;
+}
+
+minimum_severity := Severity.Debug;
+
+debug_log :: (severity: Severity, format: str, args: ..any) {
+    if severity < minimum_severity do return;
+
+    buffer1: [4096] u8;
+    s := conv.str_format_va(format, ~~buffer1, ~~args);
+    lines := string.split(s, #char "\n", context.temp_allocator);
+
+    line_alloc := alloc.arena.make_allocator(^log_buffer.line_arena);
+    for l: lines {
+        if l.count == 0 do continue;
+
+        buffer2: [4096] u8;
+        s = conv.str_format("[{}] {}\n", ~~buffer2, severity, l);
+        line := string.alloc_copy(s, line_alloc);
+
+        array.push(^log_buffer.lines, line);
+    }
+}
+
+draw_debug_log :: (r: ui.Rectangle, y_scroll: ^f32 = null, site := #callsite) {
+    hash := ui.get_site_hash(site);
+
+    mx, my := ui.get_mouse_position();
+    if ui.Rectangle.contains(r, mx, my) {
+        ui.set_hot_item(hash);
+    }
+
+    line_spacing := 28.0f;
+
+    if ui.is_hot_item(hash) && y_scroll != null {
+        if ui.mouse_state.dwheel > 0 do *y_scroll -= 20.0f;
+        if ui.mouse_state.dwheel < 0 do *y_scroll += 20.0f;
+
+        if ui.is_key_down(38) do *y_scroll -= 20.0f;
+        if ui.is_key_down(40) do *y_scroll += 20.0f;
+    }
+
+    x, y := ui.Rectangle.bottom_left(r);
+    w, h := ui.Rectangle.dimensions(r);
+
+    gfx.push_scissor(x, y - h, w, h);
+    defer gfx.pop_scissor();
+
+    ui.draw_rect(r, color=.{0,0,0,0.8});
+
+    y_offset := line_spacing + *y_scroll;
+
+    while i := log_buffer.lines.count - 1; cast(i32) i >= 0 {
+        defer i -= 1;
+
+        ui.draw_text_raw(log_buffer.lines[i], x, y - y_offset, color=.{ 0.2, 1.0, 0.2 });
+        y_offset += line_spacing;
+    }
+}
+
+#private_file log_buffer : struct {
+    line_arena : alloc.arena.ArenaState;
+    lines      : [..] str;
+}
+
index 31945e4f006b4f1c5bf9f8959a9db1dbf61a00a0..d7f82bbc8be6d369347149888de68d2b81157846 100644 (file)
@@ -8,12 +8,18 @@ use package core
 #private_file config :: package config
 #private_file wasm   :: package wasm_utils
 
+use package debug { init as debug_init, debug_log, draw_debug_log }
+
 search_buffer: string.String_Buffer;
 background_tile_texture : gfx.Texture;
 on_file_load_callbacks : map.Map(u32, (file_event: ^events.Event) -> void);
 
 wasm_data : [] u8;
 
+debug_log_y_offset := 0.0f;
+debug_log_y_scroll := 0.0f;
+debug_log_y_offset_target := 0.0f;
+
 main :: (args: [] cstr) {
     init();
 
@@ -22,6 +28,8 @@ main :: (args: [] cstr) {
 }
 
 init :: () {
+    debug_init();
+
     gl.init("main_canvas");
     events.init();
     gfx.immediate_renderer_init();
@@ -30,7 +38,7 @@ init :: () {
 
     map.init(^on_file_load_callbacks);
 
-    color_file := events.request_file("/res/colors_dark.json");
+    color_file := events.request_file(config.color_scheme_file);
     map.put(^on_file_load_callbacks, color_file, load_colors);
 
     search_buffer = string.buffer_make(memory.make_slice(u8, 256));
@@ -50,7 +58,7 @@ init :: () {
         gl.bindTexture(gl.TEXTURE_2D, -1);
     }
 
-    test_window.size     = .{ 800, 800 };
+    test_window.size = .{ 800, 800 };
     test_window.border_width = 8.0f;
 }
 
@@ -121,6 +129,10 @@ poll_events :: () -> bool {
                         refresh();
                     }
 
+                    if event.keyboard->get_name() == "F7" {
+                        debug_log_y_offset_target = 1 - debug_log_y_offset_target;
+                    }
+
                 } else {
                     ui.key_up(event.keyboard.keycode, modifiers);    @KeycodeIsWrong // see above
                 }
@@ -144,7 +156,7 @@ poll_events :: () -> bool {
             }
 
             case .FileDropped {
-                printf("File with size {} and status {} was dropped.\n", event.file.size, event.file.status);
+                debug_log(.Info, "File with size {} and status {} was dropped.\n", event.file.size, event.file.status);
 
                 if wasm_data.count > 0 do memory.free_slice(^wasm_data);
 
@@ -155,12 +167,12 @@ poll_events :: () -> bool {
                 defer wasm.free(^wasm_module);
 
                 for ^entry: wasm_module.sections.entries {
-                    printf("Section: {}\nOffset: {}\n", entry.key, entry.value);
+                    debug_log(.Info, "Section: {}\nOffset: {}\n", entry.key, entry.value);
                 }
 
                 wasm_sections := wasm.parse_sections(^wasm_module);
                 for instr: wasm.instruction_iterator(^wasm_module, ^wasm_sections.code_section[1]) {
-                    printf("{}\n", instr);
+                    debug_log(.Info, "{}\n", instr);
                 }
             }
         }
@@ -173,8 +185,10 @@ update :: (dt: f32) {
     t += dt;
 
     if ui.is_key_just_down(~~#char "L") {
-        color_file := events.request_file("/res/colors_dark.json");
+        color_file := events.request_file(config.color_scheme_file);
         map.put(^on_file_load_callbacks, color_file, load_colors);
+
+        debug_log(.Info, "Requested updated colorscheme.", null);
     }
 }
 
@@ -214,6 +228,18 @@ draw :: () {
         draw_menu_bar(^menu_bar);
     }
 
+    {
+        ui.move_towards(^debug_log_y_offset, debug_log_y_offset_target, 0.07f);
+
+        if debug_log_y_offset > 0.0f {
+            top_half, _ := ui.Flow.split_horizontal(window_rectangle, top_percent=.5);
+            height := ui.Rectangle.height(top_half);
+            top_half.y0 -= height * (1 - debug_log_y_offset);
+            top_half.y1 -= height * (1 - debug_log_y_offset);
+            draw_debug_log(top_half, ^debug_log_y_scroll);
+        }
+    }
+
     gfx.flush();
     ui.end_frame();
 }
@@ -273,8 +299,6 @@ load_colors :: (event: ^events.Event) {
     assert(event.kind == .FileRequest, "Bad event type");
     assert(event.file.status == .Success, "Failed to load color file");
 
-    println("Reloading colorscheme...");
-
     color_data := memory.make_slice(u8, event.file.size);
     defer if color_data.count > 0 do cfree(color_data.data);
     events.get_requested_file_data(event.file.file_id, color_data);
@@ -314,4 +338,6 @@ load_colors :: (event: ^events.Event) {
     test_window.border_color = config.Colors.primary;
     test_window.background_color = config.Colors.dark_background;
     test_window.active_color = config.Colors.background;
+
+    debug_log(.Info, "Successfully loaded colorscheme.", null);
 }