diff --git a/samples/breakout/src/main.c b/samples/breakout/src/main.c index 86d54b0..7cfa813 100644 --- a/samples/breakout/src/main.c +++ b/samples/breakout/src/main.c @@ -287,6 +287,7 @@ int check_collision(oc_rect block) ORCA_EXPORT void oc_on_frame_refresh(void) { + oc_arena_scope scratch = oc_scratch_begin(); f32 aspect = frameSize.x / frameSize.y; if(leftDown) @@ -422,7 +423,7 @@ ORCA_EXPORT void oc_on_frame_refresh(void) oc_rounded_rectangle_stroke(r.x, r.y, r.w, r.h, 4); int fontSize = 18; - oc_str8 text = oc_str8_pushf(oc_scratch(), "%d", blockHealth[i]); + oc_str8 text = oc_str8_pushf(scratch.arena, "%d", blockHealth[i]); oc_rect textRect = oc_font_text_metrics(font, fontSize, text).ink; oc_vec2 textPos = { @@ -457,7 +458,7 @@ ORCA_EXPORT void oc_on_frame_refresh(void) // draw score text { oc_move_to(20, 20); - oc_str8 text = oc_str8_pushf(oc_scratch(), "Destroy all %d blocks to win! Current score: %d", NUM_BLOCKS_TO_WIN, score); + oc_str8 text = oc_str8_pushf(scratch.arena, "Destroy all %d blocks to win! Current score: %d", NUM_BLOCKS_TO_WIN, score); oc_vec2 textPos = { 20, 20 }; oc_matrix_multiply_push(flip_y_at(textPos)); { @@ -474,5 +475,5 @@ ORCA_EXPORT void oc_on_frame_refresh(void) oc_render(canvas); oc_surface_present(surface); - oc_arena_clear(oc_scratch()); + oc_scratch_end(scratch); } diff --git a/samples/ui/src/main.c b/samples/ui/src/main.c index 32dc6ae..aaa8b96 100644 --- a/samples/ui/src/main.c +++ b/samples/ui/src/main.c @@ -25,13 +25,15 @@ ORCA_EXPORT void oc_on_init(void) //NOTE: load font { + oc_arena_scope scratch = oc_scratch_begin(); + oc_file file = oc_file_open(OC_STR8("/OpenSansLatinSubset.ttf"), OC_FILE_ACCESS_READ, 0); if(oc_file_last_error(file) != OC_IO_OK) { oc_log_error("Couldn't open file OpenSansLatinSubset.ttf\n"); } u64 size = oc_file_size(file); - char* buffer = (char*)oc_arena_push(oc_scratch(), size); + char* buffer = (char*)oc_arena_push(scratch.arena, size); oc_file_read(file, size, buffer); oc_file_close(file); oc_unicode_range ranges[5] = { OC_UNICODE_BASIC_LATIN, @@ -41,9 +43,10 @@ ORCA_EXPORT void oc_on_init(void) OC_UNICODE_SPECIALS }; font = oc_font_create_from_memory(oc_str8_from_buffer(size, buffer), 5, ranges); + + oc_scratch_end(scratch); } - oc_arena_clear(oc_scratch()); oc_arena_init(&textArena); } @@ -82,6 +85,8 @@ void widget_end_view(void) ORCA_EXPORT void oc_on_frame_refresh(void) { + oc_arena_scope scratch = oc_scratch_begin(); + oc_ui_style defaultStyle = { .font = font }; oc_ui_style_mask defaultMask = OC_UI_STYLE_FONT; @@ -285,7 +290,7 @@ ORCA_EXPORT void oc_on_frame_refresh(void) .size.height = { OC_UI_SIZE_TEXT } }, OC_UI_STYLE_SIZE); static oc_str8 text = { 0 }; - oc_ui_text_box_result res = oc_ui_text_box("textbox", oc_scratch(), text); + oc_ui_text_box_result res = oc_ui_text_box("textbox", scratch.arena, text); if(res.changed) { oc_arena_clear(&textArena); @@ -368,5 +373,5 @@ ORCA_EXPORT void oc_on_frame_refresh(void) oc_render(canvas); oc_surface_present(surface); - oc_arena_clear(oc_scratch()); + oc_scratch_end(scratch); } diff --git a/sketches/atlas/main.c b/sketches/atlas/main.c index fb73783..c51fb91 100644 --- a/sketches/atlas/main.c +++ b/sketches/atlas/main.c @@ -41,14 +41,17 @@ int main() } //NOTE: create atlas + + oc_arena_scope scratch = oc_scratch_begin(); + oc_arena permanentArena = { 0 }; oc_arena_init(&permanentArena); oc_rect_atlas* atlas = oc_rect_atlas_create(&permanentArena, 16000, 16000); oc_image atlasImage = oc_image_create(surface, 16000, 16000); - oc_str8 path1 = oc_path_executable_relative(oc_scratch(), OC_STR8("../../../sketches/resources/triceratops.png")); - oc_str8 path2 = oc_path_executable_relative(oc_scratch(), OC_STR8("../../../sketches/resources/Top512.png")); + oc_str8 path1 = oc_path_executable_relative(scratch.arena, OC_STR8("../../../sketches/resources/triceratops.png")); + oc_str8 path2 = oc_path_executable_relative(scratch.arena, OC_STR8("../../../sketches/resources/Top512.png")); oc_image_region image1 = oc_image_atlas_alloc_from_file(atlas, atlasImage, path1, false); oc_image_region image2 = oc_image_atlas_alloc_from_file(atlas, atlasImage, path2, false); @@ -57,11 +60,14 @@ int main() oc_window_bring_to_front(window); oc_window_focus(window); + oc_scratch_end(scratch); + while(!oc_should_quit()) { + scratch = oc_scratch_begin(); oc_pump_events(0); oc_event* event = 0; - while((event = oc_next_event(oc_scratch())) != 0) + while((event = oc_next_event(scratch.arena)) != 0) { switch(event->type) { @@ -89,7 +95,7 @@ int main() oc_render(canvas); oc_surface_present(surface); - oc_arena_clear(oc_scratch()); + oc_scratch_end(scratch); } oc_image_atlas_recycle(atlas, image1); diff --git a/sketches/canvas/main.c b/sketches/canvas/main.c index 3df0c45..bac0cb5 100644 --- a/sketches/canvas/main.c +++ b/sketches/canvas/main.c @@ -51,10 +51,11 @@ int main() while(!oc_should_quit()) { f64 startTime = oc_clock_time(OC_CLOCK_MONOTONIC); + oc_arena_scope* scratch = oc_scratch_begin(); oc_pump_events(0); oc_event* event = 0; - while((event = oc_next_event(oc_scratch())) != 0) + while((event = oc_next_event(scratch.arena)) != 0) { switch(event->type) { @@ -182,7 +183,8 @@ int main() oc_render(canvas); oc_surface_present(surface); - oc_arena_clear(oc_scratch()); + oc_scratch_end(scratch); + frameTime = oc_clock_time(OC_CLOCK_MONOTONIC) - startTime; } diff --git a/sketches/image/main.c b/sketches/image/main.c index 3b07f67..cfa72eb 100644 --- a/sketches/image/main.c +++ b/sketches/image/main.c @@ -42,23 +42,28 @@ int main() } //NOTE: create image - oc_str8 imagePath = oc_path_executable_relative(oc_scratch(), OC_STR8("../../resources/triceratops.png")); + oc_arena_scope* scratch = oc_scratch_begin(); + + oc_str8 imagePath = oc_path_executable_relative(scratch.arena, OC_STR8("../../resources/triceratops.png")); oc_image image = oc_image_create_from_file(surface, imagePath, false); oc_vec2 imageSize = oc_image_size(image); - oc_str8 imagePath2 = oc_path_executable_relative(oc_scratch(), OC_STR8("../../resources/Top512.png")); + oc_str8 imagePath2 = oc_path_executable_relative(scratch.arena, OC_STR8("../../resources/Top512.png")); oc_image image2 = oc_image_create_from_file(surface, imagePath2, false); oc_vec2 imageSize2 = oc_image_size(image2); + oc_scratch_end(scratch); // start app oc_window_bring_to_front(window); oc_window_focus(window); while(!oc_should_quit()) { + scratch = oc_scratch_begin(); + oc_pump_events(0); oc_event* event = 0; - while((event = oc_next_event(oc_scratch())) != 0) + while((event = oc_next_event(scratch.arena)) != 0) { switch(event->type) { @@ -104,7 +109,7 @@ int main() oc_render(canvas); oc_surface_present(surface); - oc_arena_clear(oc_scratch()); + oc_scratch_end(scratch); } oc_image_destroy(image); diff --git a/sketches/keyboard/main.c b/sketches/keyboard/main.c index c26857d..7f77a50 100644 --- a/sketches/keyboard/main.c +++ b/sketches/keyboard/main.c @@ -23,9 +23,10 @@ int main() while(!oc_should_quit()) { + oc_arena_scope* scratch = oc_scratch_begin(); oc_pump_events(0); oc_event* event = 0; - while((event = oc_next_event(oc_scratch())) != 0) + while((event = oc_next_event(scratch.arena)) != 0) { switch(event->type) { @@ -60,7 +61,7 @@ int main() break; } } - oc_arena_clear(oc_scratch()); + oc_scratch_end(scratch); } oc_terminate(); return (0); diff --git a/sketches/multi_surface/main.c b/sketches/multi_surface/main.c index 7ef9331..f724023 100644 --- a/sketches/multi_surface/main.c +++ b/sketches/multi_surface/main.c @@ -58,11 +58,12 @@ int main() while(!oc_should_quit()) { + oc_arena_scope* scratch = oc_scratch_begin(); f64 startTime = oc_clock_time(OC_CLOCK_MONOTONIC); oc_pump_events(0); oc_event* event = 0; - while((event = oc_next_event(oc_scratch())) != 0) + while((event = oc_next_event(scratch.arena)) != 0) { switch(event->type) { @@ -120,7 +121,7 @@ int main() oc_surface_present(surface1); oc_surface_present(surface2); - oc_arena_clear(oc_scratch()); + oc_scratch_end(scratch); } oc_canvas_destroy(canvas1); diff --git a/sketches/perf_text/main.c b/sketches/perf_text/main.c index 251302a..cc967ed 100644 --- a/sketches/perf_text/main.c +++ b/sketches/perf_text/main.c @@ -64,13 +64,15 @@ static const char* TEST_STRING = oc_font create_font(const char* path) { //NOTE(martin): create font - oc_str8 fontPath = oc_path_executable_relative(oc_scratch(), OC_STR8(path)); - char* fontPathCString = oc_str8_to_cstring(oc_scratch(), fontPath); + oc_arena_scope* scratch = oc_scratch_begin() + oc_str8 fontPath = oc_path_executable_relative(scratch.arena, OC_STR8(path)); + char* fontPathCString = oc_str8_to_cstring(scratch.arena, fontPath); FILE* fontFile = fopen(fontPathCString, "r"); if(!fontFile) { oc_log_error("Could not load font file '%s'\n", fontPathCString); + oc_scratch_end(scratch); return (oc_font_nil()); } unsigned char* fontData = 0; @@ -89,7 +91,7 @@ oc_font create_font(const char* path) oc_font font = oc_font_create_from_memory(oc_str8_from_buffer(fontDataSize, (char*)fontData), 5, ranges); free(fontData); - + oc_scratch_end(scratch); return (font); } @@ -167,10 +169,11 @@ int main() while(!oc_should_quit()) { f64 startFrameTime = oc_clock_time(OC_CLOCK_MONOTONIC); + oc_arena_scope* scratch = oc_scratch_begin(); oc_pump_events(0); oc_event* event = 0; - while((event = oc_next_event(oc_scratch())) != 0) + while((event = oc_next_event(scratch.arena)) != 0) { oc_input_process_event(&inputState, event); @@ -250,7 +253,7 @@ int main() */ oc_matrix_multiply_push((oc_mat2x3){ zoom, 0, 0, - 0, zoom, 0 }); + 0, zoom, 0 }); oc_set_color_rgba(1, 1, 1, 1); oc_clear(); @@ -294,7 +297,7 @@ int main() oc_set_font_size(14); oc_move_to(10, contentRect.h - 10 - lineHeights[fontIndex]); - oc_str8 text = oc_str8_pushf(oc_scratch(), + oc_str8 text = oc_str8_pushf(scratch.arena, "Test program: %i glyphs, frame time = %fs, fps = %f", glyphCount, frameTime, @@ -322,7 +325,7 @@ int main() (endFrameTime - startPresentTime) * 1000); oc_input_next_frame(&inputState); - oc_arena_clear(oc_scratch()); + oc_scratch_end(scratch); } for(int i = 0; i < FONT_COUNT; i++) diff --git a/sketches/render_thread/main.c b/sketches/render_thread/main.c index e83ed7a..6a8f087 100644 --- a/sketches/render_thread/main.c +++ b/sketches/render_thread/main.c @@ -18,8 +18,10 @@ i32 render_thread(void* user) { while(!oc_should_quit()) { + oc_arena_scope* scratch = oc_scratch_begin(); + oc_event* event = 0; - while((event = oc_next_event(oc_scratch())) != 0) + while((event = oc_next_event(scratch.arena)) != 0) { switch(event->type) { @@ -47,7 +49,7 @@ i32 render_thread(void* user) oc_surface_present(surface); - oc_arena_clear(oc_scratch()); + oc_scratch_end(scratch); } return (0); } diff --git a/sketches/simpleWindow/main.c b/sketches/simpleWindow/main.c index bdfad76..3839d0b 100644 --- a/sketches/simpleWindow/main.c +++ b/sketches/simpleWindow/main.c @@ -25,9 +25,10 @@ int main() while(!oc_should_quit()) { + oc_arena_scope* scratch = oc_scratch_begin(); oc_pump_events(0); oc_event* event = 0; - while((event = oc_next_event(oc_scratch())) != 0) + while((event = oc_next_event(scratch.arena)) != 0) { switch(event->type) { @@ -121,7 +122,7 @@ int main() break; } } - oc_arena_clear(oc_scratch()); + oc_scratch_end(scratch); } oc_terminate(); diff --git a/sketches/smiley/main.c b/sketches/smiley/main.c index 2878f53..2b5ce76 100644 --- a/sketches/smiley/main.c +++ b/sketches/smiley/main.c @@ -18,13 +18,15 @@ oc_font create_font() { //NOTE(martin): create font - oc_str8 fontPath = oc_path_executable_relative(oc_scratch(), OC_STR8("../../resources/OpenSansLatinSubset.ttf")); - char* fontPathCString = oc_str8_to_cstring(oc_scratch(), fontPath); + oc_arena_scope* scratch = oc_scratch_begin(); + oc_str8 fontPath = oc_path_executable_relative(scratch.arena, OC_STR8("../../resources/OpenSansLatinSubset.ttf")); + char* fontPathCString = oc_str8_to_cstring(scratch.arena, fontPath); FILE* fontFile = fopen(fontPathCString, "r"); if(!fontFile) { oc_log_error("Could not load font file '%s': %s\n", fontPathCString, strerror(errno)); + oc_scratch_end(scratch); return (oc_font_nil()); } unsigned char* fontData = 0; @@ -43,7 +45,7 @@ oc_font create_font() oc_font font = oc_font_create_from_memory(oc_str8_from_buffer(fontDataSize, (char*)fontData), 5, ranges); free(fontData); - + oc_scratch_end(scratch); return (font); } @@ -87,10 +89,11 @@ int main() while(!oc_should_quit()) { f64 startTime = oc_clock_time(OC_CLOCK_MONOTONIC); + oc_arena_scope* scratch = oc_scratch_begin(); oc_pump_events(0); oc_event* event = 0; - while((event = oc_next_event(oc_scratch())) != 0) + while((event = oc_next_event(scratch.arena)) != 0) { switch(event->type) { @@ -185,7 +188,7 @@ int main() oc_set_font_size(12); oc_move_to(50, 600 - 50); - oc_str8 text = oc_str8_pushf(oc_scratch(), + oc_str8 text = oc_str8_pushf(scratch.arena, "Orca vector graphics test program (frame time = %fs, fps = %f)...", frameTime, 1. / frameTime); @@ -200,7 +203,7 @@ int main() oc_render(canvas); oc_surface_present(surface); - oc_arena_clear(oc_scratch()); + oc_scratch_end(scratch); frameTime = oc_clock_time(OC_CLOCK_MONOTONIC) - startTime; } diff --git a/sketches/smooth_resize/main.c b/sketches/smooth_resize/main.c index 03b7a46..861b941 100644 --- a/sketches/smooth_resize/main.c +++ b/sketches/smooth_resize/main.c @@ -120,8 +120,6 @@ void update_and_render(app_data* app) glDrawArrays(GL_TRIANGLES, 0, 3); oc_surface_present(app->surface); - - oc_arena_clear(oc_scratch()); } i32 render(void* user) @@ -169,14 +167,16 @@ i32 render(void* user) while(!oc_should_quit()) { + oc_arena_scope* scratch = oc_scratch_begin(); + oc_event* event = 0; - while((event = oc_next_event(oc_scratch())) != 0) + while((event = oc_next_event(scratch.arena)) != 0) { process_event(app, *event); } update_and_render(app); - oc_arena_clear(oc_scratch()); + oc_scratch_end(scratch); } return (0); diff --git a/sketches/tiger/main.c b/sketches/tiger/main.c index a13d643..ae3fdbf 100644 --- a/sketches/tiger/main.c +++ b/sketches/tiger/main.c @@ -20,13 +20,15 @@ oc_font create_font() { //NOTE(martin): create font - oc_str8 fontPath = oc_path_executable_relative(oc_scratch(), OC_STR8("../../resources/OpenSansLatinSubset.ttf")); - char* fontPathCString = oc_str8_to_cstring(oc_scratch(), fontPath); + oc_arena_scope* scratch = oc_scratch_begin(); + oc_str8 fontPath = oc_path_executable_relative(scratch.arena, OC_STR8("../../resources/OpenSansLatinSubset.ttf")); + char* fontPathCString = oc_str8_to_cstring(scratch.arena, fontPath); FILE* fontFile = fopen(fontPathCString, "r"); if(!fontFile) { oc_log_error("Could not load font file '%s': %s\n", fontPathCString, strerror(errno)); + oc_scratch_end(scratch); return (oc_font_nil()); } unsigned char* fontData = 0; @@ -45,7 +47,7 @@ oc_font create_font() oc_font font = oc_font_create_from_memory(oc_str8_from_buffer(fontDataSize, (char*)fontData), 5, ranges); free(fontData); - + oc_scratch_end(scratch); return (font); } @@ -96,11 +98,12 @@ int main() while(!oc_should_quit()) { + oc_arena_scope* scratch = oc_scratch_begin(); f64 startTime = oc_clock_time(OC_CLOCK_MONOTONIC); oc_pump_events(0); oc_event* event = 0; - while((event = oc_next_event(oc_scratch())) != 0) + while((event = oc_next_event(scratch.arena)) != 0) { oc_input_process_event(&inputState, event); @@ -203,7 +206,7 @@ int main() oc_clear(); oc_matrix_multiply_push((oc_mat2x3){ zoom, 0, startX, - 0, zoom, startY }); + 0, zoom, startY }); draw_tiger(singlePath, singlePathIndex); @@ -221,7 +224,7 @@ int main() oc_set_font_size(12); oc_move_to(50, 600 - 50); - oc_str8 text = oc_str8_pushf(oc_scratch(), + oc_str8 text = oc_str8_pushf(scratch.arena, "Orca vector graphics test program (frame time = %fs, fps = %f)...", frameTime, 1. / frameTime); @@ -236,7 +239,9 @@ int main() oc_surface_present(surface); oc_input_next_frame(&inputState); - oc_arena_clear(oc_scratch()); + + oc_scratch_end(scratch); + frameTime = oc_clock_time(OC_CLOCK_MONOTONIC) - startTime; } diff --git a/sketches/triangleGL/main.c b/sketches/triangleGL/main.c index 9c2496f..eb65631 100644 --- a/sketches/triangleGL/main.c +++ b/sketches/triangleGL/main.c @@ -120,9 +120,10 @@ int main() while(!oc_should_quit()) { + oc_arena_scope* scratch = oc_scratch_begin(); oc_pump_events(0); oc_event* event = 0; - while((event = oc_next_event(oc_scratch())) != 0) + while((event = oc_next_event(scratch.arena)) != 0) { switch(event->type) { @@ -163,7 +164,7 @@ int main() oc_surface_present(surface); - oc_arena_clear(oc_scratch()); + oc_scratch_end(scratch); } oc_terminate(); diff --git a/sketches/triangleGLES/main.c b/sketches/triangleGLES/main.c index ac10ce8..4d67298 100644 --- a/sketches/triangleGLES/main.c +++ b/sketches/triangleGLES/main.c @@ -109,9 +109,10 @@ int main() while(!oc_should_quit()) { + oc_arena_scope* scratch = oc_scratch_begin(); oc_pump_events(0); oc_event* event = 0; - while((event = oc_next_event(oc_scratch())) != 0) + while((event = oc_next_event(scratch.arena)) != 0) { switch(event->type) { @@ -150,7 +151,7 @@ int main() oc_surface_present(surface); - oc_arena_clear(oc_scratch()); + oc_scratch_end(scratch); } oc_surface_destroy(surface); diff --git a/sketches/triangleMetal/main.m b/sketches/triangleMetal/main.m index 5c126a1..edb1ba6 100644 --- a/sketches/triangleMetal/main.m +++ b/sketches/triangleMetal/main.m @@ -36,8 +36,10 @@ int main() //NOTE(martin): load the library id device = MTLCreateSystemDefaultDevice(); - oc_str8 shaderPath = oc_path_executable_relative(oc_scratch(), OC_STR8("triangle_shader.metallib")); - const char* shaderPathCString = oc_str8_to_cstring(oc_scratch(), shaderPath); + oc_arena_scope* scratch = oc_scratch_begin(); + + oc_str8 shaderPath = oc_path_executable_relative(scratch.arena, OC_STR8("triangle_shader.metallib")); + const char* shaderPathCString = oc_str8_to_cstring(scratch.arena, shaderPath); NSString* metalFileName = [[NSString alloc] initWithCString:shaderPathCString encoding:NSUTF8StringEncoding]; NSError* err = 0; id library = [device newLibraryWithFile:metalFileName error:&err]; @@ -67,6 +69,7 @@ int main() return (-1); } + oc_scratch_end(scrathc); // start app oc_window_bring_to_front(window); @@ -74,9 +77,10 @@ int main() while(!oc_should_quit()) { + scratch = oc_scratch_begin(); oc_pump_events(0); oc_event* event = 0; - while((event = oc_next_event(oc_scratch())) != 0) + while((event = oc_next_event(scratch.arena)) != 0) { switch(event->type) { @@ -89,8 +93,6 @@ int main() default: break; } - - oc_arena_clear(oc_scratch()); } vector_uint2 viewportSize; @@ -116,6 +118,8 @@ int main() [encoder endEncoding]; oc_surface_present(surface); + + oc_scratch_end(scratch); } oc_terminate(); diff --git a/sketches/ui/main.c b/sketches/ui/main.c index c8e8673..aaec438 100644 --- a/sketches/ui/main.c +++ b/sketches/ui/main.c @@ -152,13 +152,15 @@ void debug_print_styles(oc_ui_box* box, int indent) oc_font create_font() { //NOTE(martin): create font - oc_str8 fontPath = oc_path_executable_relative(oc_scratch(), OC_STR8("../../resources/OpenSansLatinSubset.ttf")); - char* fontPathCString = oc_str8_to_cstring(oc_scratch(), fontPath); + oc_arena_scope scratch = oc_scratch_begin(); + oc_str8 fontPath = oc_path_executable_relative(scratch.arena, OC_STR8("../../resources/OpenSansLatinSubset.ttf")); + char* fontPathCString = oc_str8_to_cstring(scratch.arena, fontPath); FILE* fontFile = fopen(fontPathCString, "r"); if(!fontFile) { oc_log_error("Could not load font file '%s': %s\n", fontPathCString, strerror(errno)); + oc_scratch_end(scratch); return (oc_font_nil()); } unsigned char* fontData = 0; @@ -178,6 +180,7 @@ oc_font create_font() oc_font font = oc_font_create_from_memory(oc_str8_from_buffer(fontDataSize, (char*)fontData), 5, ranges); free(fontData); + oc_scratch_end(scratch); return (font); } @@ -240,7 +243,7 @@ int main() while(!oc_should_quit()) { - oc_arena* scratch = oc_scratch(); + oc_arena_scope scratch = oc_scratch_begin(); bool printDebugStyle = false; @@ -545,7 +548,7 @@ int main() .size.height = { OC_UI_SIZE_TEXT } }, OC_UI_STYLE_SIZE); static oc_str8 text = { 0 }; - oc_ui_text_box_result res = oc_ui_text_box("textbox", oc_scratch(), text); + oc_ui_text_box_result res = oc_ui_text_box("textbox", scratch.arena, text); if(res.changed) { oc_arena_clear(&textArena); @@ -559,7 +562,7 @@ int main() widget_view("Test") { oc_ui_pattern pattern = { 0 }; - oc_ui_pattern_push(oc_scratch(), &pattern, (oc_ui_selector){ .kind = OC_UI_SEL_TEXT, .text = OC_STR8("panel") }); + oc_ui_pattern_push(scratch.arena, &pattern, (oc_ui_selector){ .kind = OC_UI_SEL_TEXT, .text = OC_STR8("panel") }); oc_ui_style_match_after(pattern, &(oc_ui_style){ .bgColor = { 0.3, 0.3, 1, 1 } }, OC_UI_STYLE_BG_COLOR); static int selected = 0; @@ -634,7 +637,7 @@ int main() oc_render(canvas); oc_surface_present(surface); - oc_arena_clear(scratch); + oc_scratch_end(scratch); } oc_surface_destroy(surface); diff --git a/src/graphics/mtl_renderer.m b/src/graphics/mtl_renderer.m index 10088bf..fd75ba8 100644 --- a/src/graphics/mtl_renderer.m +++ b/src/graphics/mtl_renderer.m @@ -1484,8 +1484,13 @@ oc_canvas_backend* oc_mtl_canvas_backend_create(oc_mtl_surface* surface) @autoreleasepool { //NOTE: load metal library - oc_str8 shaderPath = oc_path_executable_relative(oc_scratch(), OC_STR8("mtl_renderer.metallib")); + oc_arena_scope scratch = oc_scratch_begin(); + + oc_str8 shaderPath = oc_path_executable_relative(scratch.arena, OC_STR8("mtl_renderer.metallib")); NSString* metalFileName = [[NSString alloc] initWithBytes:shaderPath.ptr length:shaderPath.len encoding:NSUTF8StringEncoding]; + + oc_scratch_end(scratch); + NSError* err = 0; id library = [surface->device newLibraryWithFile:metalFileName error:&err]; if(err != nil) diff --git a/src/platform/native_debug.c b/src/platform/native_debug.c index b1a7c12..acdae4d 100644 --- a/src/platform/native_debug.c +++ b/src/platform/native_debug.c @@ -86,14 +86,14 @@ void platform_log_push(oc_log_output* output, _Noreturn void oc_abort_ext(const char* file, const char* function, int line, const char* fmt, ...) { - oc_arena* scratch = oc_scratch(); + oc_arena_scope scratch = oc_scratch_begin(); va_list ap; va_start(ap, fmt); - oc_str8 note = oc_str8_pushfv(scratch, fmt, ap); + oc_str8 note = oc_str8_pushfv(scratch.arena, fmt, ap); va_end(ap); - oc_str8 msg = oc_str8_pushf(scratch, + oc_str8 msg = oc_str8_pushf(scratch.arena, "Fatal error in function %s() in file \"%s\", line %i:\n%.*s\n", function, file, @@ -104,7 +104,7 @@ _Noreturn void oc_abort_ext(const char* file, const char* function, int line, co oc_log_error(msg.ptr); oc_str8_list options = { 0 }; - oc_str8_list_push(scratch, &options, OC_STR8("OK")); + oc_str8_list_push(scratch.arena, &options, OC_STR8("OK")); oc_alert_popup(OC_STR8("Fatal Error"), msg, options); @@ -114,14 +114,14 @@ _Noreturn void oc_abort_ext(const char* file, const char* function, int line, co _Noreturn void oc_assert_fail(const char* file, const char* function, int line, const char* src, const char* fmt, ...) { - oc_arena* scratch = oc_scratch(); + oc_arena_scope scratch = oc_scratch_begin(); va_list ap; va_start(ap, fmt); - oc_str8 note = oc_str8_pushfv(scratch, fmt, ap); + oc_str8 note = oc_str8_pushfv(scratch.arena, fmt, ap); va_end(ap); - oc_str8 msg = oc_str8_pushf(scratch, + oc_str8 msg = oc_str8_pushf(scratch.arena, "Assertion failed in function %s() in file \"%s\", line %i:\n%s\nNote: %.*s\n", function, file, @@ -132,7 +132,7 @@ _Noreturn void oc_assert_fail(const char* file, const char* function, int line, oc_log_error(msg.ptr); oc_str8_list options = { 0 }; - oc_str8_list_push(scratch, &options, OC_STR8("OK")); + oc_str8_list_push(scratch.arena, &options, OC_STR8("OK")); oc_alert_popup(OC_STR8("Assertion Failed"), msg, options); diff --git a/src/platform/orca_debug.c b/src/platform/orca_debug.c index ad96985..3d0daf3 100644 --- a/src/platform/orca_debug.c +++ b/src/platform/orca_debug.c @@ -50,13 +50,13 @@ static oc_log_output oc_logDefaultOutput = { .kind = ORCA_LOG_OUTPUT_CONSOLE }; oc_log_output* OC_LOG_DEFAULT_OUTPUT = &oc_logDefaultOutput; void ORCA_IMPORT(oc_bridge_log)(oc_log_level level, - int fileLen, - const char* file, - int functionLen, - const char* function, - int line, - int msgLen, - const char* msg); + int fileLen, + const char* file, + int functionLen, + const char* function, + int line, + int msgLen, + const char* msg); void platform_log_push(oc_log_output* output, oc_log_level level, @@ -66,20 +66,19 @@ void platform_log_push(oc_log_output* output, const char* fmt, va_list ap) { - oc_arena* scratch = oc_scratch(); - oc_arena_scope tmp = oc_arena_scope_begin(scratch); + oc_arena_scope scratch = oc_scratch_begin(); - oc_stbsp_context ctx = { .arena = scratch, + oc_stbsp_context ctx = { .arena = scratch.arena, .list = { 0 } }; char buf[STB_SPRINTF_MIN]; stbsp_vsprintfcb(oc_stbsp_callback, &ctx, buf, fmt, ap); - oc_str8 string = oc_str8_list_join(scratch, ctx.list); + oc_str8 string = oc_str8_list_join(scratch.arena, ctx.list); oc_bridge_log(level, strlen(file), file, strlen(function), function, line, oc_str8_ip(string)); - oc_arena_scope_end(tmp); + oc_scratch_end(scratch); } //---------------------------------------------------------------- diff --git a/src/runtime.c b/src/runtime.c index d9e6f4d..6d99c8c 100644 --- a/src/runtime.c +++ b/src/runtime.c @@ -21,32 +21,37 @@ oc_font orca_font_create(const char* resourcePath) { //NOTE(martin): create default fonts - oc_str8 fontPath = oc_path_executable_relative(oc_scratch(), OC_STR8(resourcePath)); + oc_arena_scope scratch = oc_scratch_begin(); + oc_str8 fontPath = oc_path_executable_relative(scratch.arena, OC_STR8(resourcePath)); + + oc_font font = oc_font_nil(); FILE* fontFile = fopen(fontPath.ptr, "r"); if(!fontFile) { oc_log_error("Could not load font file '%s': %s\n", fontPath.ptr, strerror(errno)); - return (oc_font_nil()); } - char* fontData = 0; - fseek(fontFile, 0, SEEK_END); - u32 fontDataSize = ftell(fontFile); - rewind(fontFile); - fontData = malloc(fontDataSize); - fread(fontData, 1, fontDataSize, fontFile); - fclose(fontFile); + else + { + char* fontData = 0; + fseek(fontFile, 0, SEEK_END); + u32 fontDataSize = ftell(fontFile); + rewind(fontFile); + fontData = malloc(fontDataSize); + fread(fontData, 1, fontDataSize, fontFile); + fclose(fontFile); - oc_unicode_range ranges[5] = { OC_UNICODE_BASIC_LATIN, - OC_UNICODE_C1_CONTROLS_AND_LATIN_1_SUPPLEMENT, - OC_UNICODE_LATIN_EXTENDED_A, - OC_UNICODE_LATIN_EXTENDED_B, - OC_UNICODE_SPECIALS }; + oc_unicode_range ranges[5] = { OC_UNICODE_BASIC_LATIN, + OC_UNICODE_C1_CONTROLS_AND_LATIN_1_SUPPLEMENT, + OC_UNICODE_LATIN_EXTENDED_A, + OC_UNICODE_LATIN_EXTENDED_B, + OC_UNICODE_SPECIALS }; - oc_font font = oc_font_create_from_memory(oc_str8_from_buffer(fontDataSize, fontData), 5, ranges); - - free(fontData); + font = oc_font_create_from_memory(oc_str8_from_buffer(fontDataSize, fontData), 5, ranges); + free(fontData); + } + oc_scratch_end(scratch); return (font); } @@ -301,6 +306,8 @@ void debug_overlay_toggle(oc_debug_overlay* overlay) void log_entry_ui(oc_debug_overlay* overlay, log_entry* entry) { + oc_arena_scope scratch = oc_scratch_begin(); + static const char* levelNames[] = { "Error: ", "Warning: ", "Info: " }; static const oc_color levelColors[] = { { 0.8, 0, 0, 1 }, { 1, 0.5, 0, 1 }, @@ -325,7 +332,7 @@ void log_entry_ui(oc_debug_overlay* overlay, log_entry* entry) | OC_UI_STYLE_LAYOUT_MARGINS | OC_UI_STYLE_BG_COLOR); - oc_str8 key = oc_str8_pushf(oc_scratch(), "%ull", entry->recordIndex); + oc_str8 key = oc_str8_pushf(scratch.arena, "%ull", entry->recordIndex); oc_ui_container_str8(key, OC_UI_FLAG_DRAW_BACKGROUND) { @@ -343,7 +350,7 @@ void log_entry_ui(oc_debug_overlay* overlay, log_entry* entry) | OC_UI_STYLE_FONT); oc_ui_label(levelNames[entry->level]); - oc_str8 loc = oc_str8_pushf(oc_scratch(), + oc_str8 loc = oc_str8_pushf(scratch.arena, "%.*s() in %.*s:%i:", oc_str8_ip(entry->file), oc_str8_ip(entry->function), @@ -352,6 +359,7 @@ void log_entry_ui(oc_debug_overlay* overlay, log_entry* entry) } oc_ui_label_str8(entry->msg); } + oc_scratch_end(scratch); } char m3_type_to_tag(M3ValueType type) @@ -399,8 +407,10 @@ i32 orca_runloop(void* user) oc_wasm_env_init(&app->env); //NOTE: loads wasm module + oc_arena_scope scratch = oc_scratch_begin(); + const char* bundleNameCString = "module"; - oc_str8 modulePath = oc_path_executable_relative(oc_scratch(), OC_STR8("../app/wasm/module.wasm")); + oc_str8 modulePath = oc_path_executable_relative(scratch.arena, OC_STR8("../app/wasm/module.wasm")); FILE* file = fopen(modulePath.ptr, "rb"); if(!file) @@ -437,7 +447,7 @@ i32 orca_runloop(void* user) } m3_SetModuleName(app->env.m3Module, bundleNameCString); - oc_arena_clear(oc_scratch()); + oc_scratch_end(scratch); //NOTE: bind orca APIs { @@ -522,7 +532,9 @@ i32 orca_runloop(void* user) //NOTE: preopen the app local root dir { - oc_str8 localRootPath = oc_path_executable_relative(oc_scratch(), OC_STR8("../app/data")); + scratch = oc_scratch_begin(); + + oc_str8 localRootPath = oc_path_executable_relative(scratch.arena, OC_STR8("../app/data")); oc_io_req req = { .op = OC_IO_OPEN_AT, .open.rights = OC_FILE_ACCESS_READ | OC_FILE_ACCESS_WRITE, @@ -530,6 +542,8 @@ i32 orca_runloop(void* user) .buffer = localRootPath.ptr }; oc_io_cmp cmp = oc_io_wait_single_req_for_table(&req, &app->fileTable); app->rootDir = cmp.handle; + + oc_scratch_end(scratch); } IM3Function* exports = app->env.exports; @@ -561,8 +575,10 @@ i32 orca_runloop(void* user) while(!app->quit) { + scratch = oc_scratch_begin(); oc_event* event = 0; - while((event = oc_next_event(oc_scratch())) != 0) + + while((event = oc_next_event(scratch.arena)) != 0) { if(app->debugOverlay.show) { @@ -571,7 +587,6 @@ i32 orca_runloop(void* user) if(exports[OC_EXPORT_RAW_EVENT]) { - oc_arena_scope scratch = oc_scratch_begin(); oc_event* clipboardEvent = oc_runtime_clipboard_process_event_begin(&__orcaApp.clipboard, scratch.arena, event); oc_event* events[2]; u64 eventsCount; @@ -605,7 +620,6 @@ i32 orca_runloop(void* user) } oc_runtime_clipboard_process_event_end(&__orcaApp.clipboard); - oc_scratch_end(scratch); } switch(event->type) @@ -818,8 +832,8 @@ i32 orca_runloop(void* user) // and we need to change that to size according to its parent (whereas the default is sizing according // to its children) oc_ui_pattern pattern = { 0 }; - oc_ui_pattern_push(oc_scratch(), &pattern, (oc_ui_selector){ .kind = OC_UI_SEL_OWNER }); - oc_ui_pattern_push(oc_scratch(), &pattern, (oc_ui_selector){ .kind = OC_UI_SEL_TEXT, .text = OC_STR8("contents") }); + oc_ui_pattern_push(scratch.arena, &pattern, (oc_ui_selector){ .kind = OC_UI_SEL_OWNER }); + oc_ui_pattern_push(scratch.arena, &pattern, (oc_ui_selector){ .kind = OC_UI_SEL_TEXT, .text = OC_STR8("contents") }); oc_ui_style_match_after(pattern, &(oc_ui_style){ .size.width = { OC_UI_SIZE_PARENT, 1 } }, OC_UI_STYLE_SIZE_WIDTH); oc_ui_box* panel = oc_ui_box_lookup("log view"); @@ -877,7 +891,7 @@ i32 orca_runloop(void* user) oc_render(app->debugOverlay.canvas); oc_surface_present(app->debugOverlay.surface); - oc_arena_clear(oc_scratch()); + oc_scratch_end(scratch); #if OC_PLATFORM_WINDOWS //NOTE(martin): on windows we set all surfaces to non-synced, and do a single "manual" wait here. diff --git a/src/util/memory.c b/src/util/memory.c index 7531b16..65e00fe 100644 --- a/src/util/memory.c +++ b/src/util/memory.c @@ -216,40 +216,34 @@ static oc_arena* oc_scratch_at_index(int index) return (scratch); } -oc_arena* oc_scratch() -{ - return (oc_scratch_at_index(0)); -} - -ORCA_API oc_arena* oc_scratch_next(oc_arena* used) -{ - oc_arena* res = 0; - if((used >= __scratchPool) - && (used - __scratchPool < OC_SCRATCH_POOL_SIZE)) - { - u64 index = used - __scratchPool; - if(index + 1 < OC_SCRATCH_POOL_SIZE) - { - res = oc_scratch_at_index(index + 1); - } - } - else - { - res = oc_scratch_at_index(0); - } - return (res); -} - ORCA_API oc_arena_scope oc_scratch_begin(void) { - oc_arena* scratch = oc_scratch(); + oc_arena* scratch = oc_scratch_at_index(0); oc_arena_scope scope = oc_arena_scope_begin(scratch); return (scope); } ORCA_API oc_arena_scope oc_scratch_begin_next(oc_arena* used) { - oc_arena* scratch = oc_scratch_next(used); - oc_arena_scope scope = oc_arena_scope_begin(scratch); + oc_arena_scope scope = { 0 }; + oc_arena* arena = 0; + if((used >= __scratchPool) + && (used - __scratchPool < OC_SCRATCH_POOL_SIZE)) + { + u64 index = used - __scratchPool; + if(index + 1 < OC_SCRATCH_POOL_SIZE) + { + arena = oc_scratch_at_index(index + 1); + } + } + else + { + arena = oc_scratch_at_index(0); + } + + OC_ASSERT(arena); + + scope = oc_arena_scope_begin(arena); + return (scope); } diff --git a/src/util/memory.h b/src/util/memory.h index 4b5d7fe..ae2115c 100644 --- a/src/util/memory.h +++ b/src/util/memory.h @@ -96,8 +96,6 @@ ORCA_API void oc_pool_clear(oc_pool* pool); //-------------------------------------------------------------------------------- //NOTE(martin): per-thread implicit scratch arena //-------------------------------------------------------------------------------- -ORCA_API oc_arena* oc_scratch(); -ORCA_API oc_arena* oc_scratch_next(oc_arena* used); ORCA_API oc_arena_scope oc_scratch_begin(void); ORCA_API oc_arena_scope oc_scratch_begin_next(oc_arena* used); diff --git a/tests/file_dialog/main.c b/tests/file_dialog/main.c index bf60eab..44a1d14 100644 --- a/tests/file_dialog/main.c +++ b/tests/file_dialog/main.c @@ -12,7 +12,8 @@ int main(int argc, char** argv) { oc_init(); - oc_str8 path = oc_path_executable_relative(oc_scratch(), OC_STR8("../")); + oc_arena_scope* scratch = oc_scratch_begin(); + oc_str8 path = oc_path_executable_relative(scratch.arena, OC_STR8("../")); oc_file dir = oc_file_open(path, OC_FILE_ACCESS_READ, 0); oc_file_dialog_desc desc = { @@ -24,9 +25,9 @@ int main(int argc, char** argv) .startPath = OC_STR8(".."), }; - oc_str8_list_push(oc_scratch(), &desc.filters, OC_STR8("txt")); + oc_str8_list_push(scratch.arena, &desc.filters, OC_STR8("txt")); - oc_file_dialog_result res = oc_file_dialog(oc_scratch(), &desc); + oc_file_dialog_result res = oc_file_dialog(scratch.arena, &desc); if(res.button == OC_FILE_DIALOG_CANCEL) { diff --git a/tests/file_open_dialog/main.c b/tests/file_open_dialog/main.c index bbd82ad..f01e9ce 100644 --- a/tests/file_open_dialog/main.c +++ b/tests/file_open_dialog/main.c @@ -19,7 +19,9 @@ int main(int argc, char** argv) .okLabel = OC_STR8("Select") }; - oc_str8_list_push(oc_scratch(), &desc.filters, OC_STR8("txt")); + oc_arena_scope* scratch = oc_scratch_begin(); + + oc_str8_list_push(scratch.arena, &desc.filters, OC_STR8("txt")); oc_file file = oc_file_open_with_dialog(OC_FILE_ACCESS_READ, 0, &desc); if(oc_file_is_nil(file))