diff --git a/JetBrainsMono-Regular.ttf b/JetBrainsMono-Regular.ttf new file mode 100644 index 0000000..8da8aa4 Binary files /dev/null and b/JetBrainsMono-Regular.ttf differ diff --git a/module.jai b/module.jai index ed5bc37..28d82c4 100644 --- a/module.jai +++ b/module.jai @@ -5,12 +5,17 @@ PlotSettings :: struct { bottom_right : Vector2; } -PLOT_STYLE :: enum { +PLOT_STYLE :: enum s32 { TICK_MARKS; SCALE_MARKER; } -draw_plot :: (key: string, xdata: []float64, yarrays: [][]float64, style: PLOT_STYLE, using settings: PlotSettings) { +PLOT_DATA_STYLE :: enum { + ONCE; + EVERY_FRAME; +} + +draw_plot :: (key: string, xdata: []float64, yarrays: [][]float64, using settings: PlotSettings, data_style := PLOT_DATA_STYLE.ONCE) { plot_size := ImGui.GetContentRegionAvail(); plot, success := table_find(*plots, key); @@ -19,6 +24,70 @@ draw_plot :: (key: string, xdata: []float64, yarrays: [][]float64, style: PLOT_S init_plot(plot, key, xx plot_size.x, xx plot_size.y); table_set(*plots, key, plot); + if data_style == .ONCE { + plot.xfloat = NewArray(xdata.count, float); + + for xdata { + plot.xfloat[it_index] = cast(float) it; + } + + plot.xmin = 0.0; + plot.xmax = 0.0; + + x_acc : float; + + for xdata { + if it > plot.xmax plot.xmax = xx it; + if it < plot.xmin plot.xmin = xx it; + + is_nan, is_inf := is_nan_is_inf(cast(float) it); + if !is_nan && !is_inf { + x_acc += cast(float) it; + } + } + + plot.x_avg = x_acc / xdata.count; + + plot.ymin = 0.0; + plot.ymax = 0.0; + + y_acc : float; + + for ydata: yarrays { + + yfloat := NewArray(ydata.count, float); + array_add(*plot.yfloats, yfloat); + + for ydata { + yfloat[it_index] = cast(float) it; + + if it > plot.ymax plot.ymax = xx it; + if it < plot.ymin plot.ymin = xx it; + + is_nan, is_inf := is_nan_is_inf(cast(float) it); + if !is_nan && !is_inf { + y_acc += cast(float) it; + } + } + } + + plot.y_avg = y_acc / (yarrays[0].count * yarrays.count); + + if fixed_bounds { + plot.xmin = top_left.x; + plot.xmax = bottom_right.x; + plot.ymin = bottom_right.y; + plot.ymax = top_left.y; + + plot.x_avg = plot.xmax - plot.xmin; + plot.y_avg = plot.ymax - plot.ymin; + } + } + + plot.zoom = 1.0; + } + + if data_style == .EVERY_FRAME { plot.xfloat = NewArray(xdata.count, float); for xdata { @@ -76,13 +145,12 @@ draw_plot :: (key: string, xdata: []float64, yarrays: [][]float64, style: PLOT_S plot.x_avg = plot.xmax - plot.xmin; plot.y_avg = plot.ymax - plot.ymin; } - - //plot.pos.x = plot.x_avg; - //plot.pos.y = plot.y_avg; - plot.zoom = 1.0; } - if style == { + ImGui.RadioButton("Tick marks", xx *plot.plot_style, 0); ImGui.SameLine(); + ImGui.RadioButton("Scale marker", xx *plot.plot_style, 1); + + if plot.plot_style == { case .TICK_MARKS; plot.left_bearing = 40; plot.bottom_bearing = 20; @@ -132,9 +200,9 @@ draw_plot :: (key: string, xdata: []float64, yarrays: [][]float64, style: PLOT_S glClearColor(colors.background.x, colors.background.y, colors.background.z, 0.0); glClear(GL_COLOR_BUFFER_BIT); - draw_axis(plot, style, colors); + draw_axis(plot, plot.plot_style, colors); - if style == .SCALE_MARKER + if plot.plot_style == .SCALE_MARKER draw_scale(plot, colors); glViewport(xx plot.left_bearing, xx plot.bottom_bearing, @@ -192,6 +260,14 @@ draw_plot :: (key: string, xdata: []float64, yarrays: [][]float64, style: PLOT_S plot.last_frame = frame; } + if data_style == .EVERY_FRAME { + array_free(plot.xfloat); + + for plot.yfloats { + array_free(it); + remove it; + } + } } free_old_plots :: () { @@ -219,7 +295,7 @@ init :: (_win_width: s32, _win_height: s32) { window_projection_matrix = orthographic_projection_matrix(0.0, xx win_width, xx win_height, 0.0, -1.0, 1.0); - init_font(*JetBrainsMonoRegular, "fonts/JetBrainsMono-Regular.ttf", 12); + init_font(*JetBrainsMonoRegular, "JetBrainsMono-Regular.ttf", 12); } window_resize :: (_win_width: s32, _win_height: s32) { @@ -262,6 +338,9 @@ PlotColors :: struct { Plot :: struct { key : string; + + plot_style : PLOT_STYLE; + msfbo : GLuint; mstexture : GLuint; fbo : GLuint; @@ -581,6 +660,7 @@ is_in_rect :: (pos: Vector2, size: Vector2, test: Vector2) -> bool { #import "freetype-2.12.1"; #import "harfbuzz"; #import "stb_rect_pack"; +#import "File"; ftlib : FT_Library; @@ -588,6 +668,8 @@ ATLAS_SIZE :: 2048; JetBrainsMonoRegular: Font; +JetBrainsMonoRegular_text :: #run read_entire_file(tprint("%/JetBrainsMono-Regular.ttf", #filepath)); + Glyph :: struct { utf32 : u32; index : u32; @@ -622,7 +704,10 @@ init_font :: (using font: *Font, filename: string, size: s32) { pixel_size := size * 96.0 / 72.0; //pixel_size := size; - FT_New_Face(ftlib, filename.data, 0, *face); + //FT_New_Face(ftlib, filename.data, 0, *face); + + FT_New_Memory_Face(ftlib, JetBrainsMonoRegular_text.data, JetBrainsMonoRegular_text.count, 0, *face); + //error := FT_Set_Pixel_Sizes(face, 0, xx size); error := FT_Set_Char_Size(face, 0, size * 64, 0, 96); if error @@ -690,7 +775,8 @@ init_font :: (using font: *Font, filename: string, size: s32) { graphics_init_font(*font.graphics_font, atlas); - blob : *hb_blob_t = hb_blob_create_from_file(filename.data); + blob : *hb_blob_t = hb_blob_create(JetBrainsMonoRegular_text.data, JetBrainsMonoRegular_text.count, .HB_MEMORY_MODE_READONLY, null, null); + hb_face : *hb_face_t = hb_face_create(blob, 0); hb = hb_font_create(hb_face); hb_font_set_ppem(hb, xx pixel_size, xx pixel_size);