From f8b540f58397ea8ff474a3ad57284c80f4741103 Mon Sep 17 00:00:00 2001 From: Daniel Bross Date: Sun, 22 Dec 2024 00:49:35 +0100 Subject: [PATCH] Console works again | UI crash fixes --- core/console.jai | 36 ++++++++++++++++++++---- core/console_commands.jai | 58 +++++++++++++++++++++++++++++++++++++++ core/scene.jai | 16 +++++++++++ editor/editor_ui.jai | 4 +-- metaprogram.jai | 2 ++ module.jai | 8 ++++++ ui/ui.jai | 4 ++- 7 files changed, 119 insertions(+), 9 deletions(-) create mode 100644 core/console_commands.jai diff --git a/core/console.jai b/core/console.jai index b9a6e8c..72b1e55 100644 --- a/core/console.jai +++ b/core/console.jai @@ -1,10 +1,8 @@ -#import "File_Utilities"; - console : *Console; Command_Proc :: struct { name: string; - proc: (arguments: [] string, buffer: *[..] Buffer_Entry); + proc: (arguments: [] string); } Buffer_Entry :: struct { @@ -30,7 +28,15 @@ Console :: struct { verts: [..] Colored_Vert; } -add_command :: (console: *Console, cmd: string, proc: (arguments: [] string, buffer: *[..] Buffer_Entry)) { +console_error :: (str: string, args: ..Any) { + array_add(*console.buffer, .{sprint(str, ..args), .{1,0,0,1}}); +} + +console_success :: (str: string, args: ..Any) { + array_add(*console.buffer, .{sprint(str, ..args), .{0,1,0,1}}); +} + +add_command :: (console: *Console, cmd: string, proc: (arguments: [] string)) { command : Command_Proc; command.name = copy_string(cmd); command.proc = proc; @@ -40,7 +46,7 @@ add_command :: (console: *Console, cmd: string, proc: (arguments: [] string, buf init_console :: () { console = New(Console); - console.verts.allocator = temp; + //console.verts.allocator = temp; buffer_size := size_of(Colored_Vert) * 12; console.vb = create_vertex_buffer(engine.renderer, null, xx buffer_size, stride=size_of(Colored_Vert), mappable=true); console.cursor_vb = create_vertex_buffer(engine.renderer, null, xx buffer_size, stride=size_of(Colored_Vert), mappable=true); @@ -80,6 +86,8 @@ init_console :: () { //add_command(console, "copy", copy_scene); //add_command(console, "vsync", set_vsync); + add_engine_console_commands(); + //engine.console = console; } @@ -112,12 +120,16 @@ update_console :: () { // So each subsequent space will be an argument itself. // This is a bit of a hacky fix for now arguments := split(console.current_string, " "); + index := 0; + command := arguments[0]; while index < arguments.count - 1 { if arguments[index].count == 0 { arguments[index] = arguments[arguments.count - 1]; arguments.count -= 1; } else { + if index > 0 { + } index += 1; } } @@ -126,12 +138,21 @@ update_console :: () { arguments.count -= 1; } + for_cmd : [..] string; + for_cmd.allocator = temp; + array_reserve(*for_cmd, arguments.count-1); + + for arguments { + if it_index == 0 continue; + array_add(*for_cmd, it); + } + cmd, success := find_command(console, arguments[0]); push_entry(*console.buffer, console.current_string, .{1,1,1,1}); if success { - cmd.proc(arguments, *console.buffer); + cmd.proc(for_cmd); } else { push_entry(*console.buffer, console.current_string, .{1,0,0,1}); } @@ -269,3 +290,6 @@ push_entry :: (buffer: *[..] Buffer_Entry, text: string, color: Color) { entry.color = color; array_add(buffer, entry); } + +#import "File_Utilities"; +#load "console_commands.jai"; \ No newline at end of file diff --git a/core/console_commands.jai b/core/console_commands.jai new file mode 100644 index 0000000..318563a --- /dev/null +++ b/core/console_commands.jai @@ -0,0 +1,58 @@ +add_engine_console_commands :: () { + add_command(console, "new", cmd_new_scene); + add_command(console, "load", cmd_load_scene); +} + +cmd_new_scene :: (arguments: [] string) { + if arguments.count != 1 { + console_error("Expected one argument, got %\n", arguments.count); + return; + } + + if engine.mode == .PLAYING { + console_error("Can't create new scene while in play-mode\n"); + return; + } + + scene_path := tprint("../assets/scenes/%", arguments[0]); + + if file_exists(scene_path) { + console_error("Scene '%' already exist\n", arguments[0]); + return; + } + + #if EDITOR { + engine.editor.selected_entities.count = 0; + } + + if engine.current_scene != null { + unload_scene(engine.current_scene); + } + + engine.current_scene = create_scene(arguments[0]); + + + switch_to_scene(arguments[0]); + console_success("Scene '%' loaded\n", arguments[0]); +} + +cmd_load_scene :: (arguments: [] string) { + if arguments.count != 1 { + console_error("Scene '%' doesn't exist\n", arguments[0]); + return; + } + + scene_path := tprint("../assets/scenes/%", arguments[0]); + + if !file_exists(scene_path) { + console_error("Scene '%' doesn't exist\n", arguments[0]); + return; + } + + #if EDITOR { + engine.editor.selected_entities.count = 0; + } + + switch_to_scene(arguments[0]); + console_success("Scene '%' loaded\n", arguments[0]); +} \ No newline at end of file diff --git a/core/scene.jai b/core/scene.jai index d5e185c..5f5e3e7 100644 --- a/core/scene.jai +++ b/core/scene.jai @@ -71,9 +71,25 @@ load_scene :: (name: string) -> *Scene { engine.procs.on_scene_loaded(scene, engine.mode); } + if engine.mode == .EDITING { + save_last_opened_scene_file(name); + } + return scene; } +get_last_opened_scene_file :: () -> string { + if file_exists("../.config") { + return read_entire_file("../.config"); + } + + return .{}; +} + +save_last_opened_scene_file :: (name: string) { + write_entire_file("../.config", name); +} + save_scene :: (scene: *Scene) { path := "../assets/scenes/"; save_scene(scene, path); diff --git a/editor/editor_ui.jai b/editor/editor_ui.jai index a7d9ed3..cf61a7c 100644 --- a/editor/editor_ui.jai +++ b/editor/editor_ui.jai @@ -104,9 +104,9 @@ editor_ui :: () { clicked := false; selected := array_find(engine.editor.selected_entities, it); if it.name.count == 0 { - clicked = ui_clickable_label(tprint("%", it.type), selected, it_index); + clicked = ui_clickable_label(tprint("%", it.type), selected, it.id); } else { - clicked = ui_clickable_label(it.name, selected, it_index); + clicked = ui_clickable_label(it.name, selected, it.id); } if clicked { diff --git a/metaprogram.jai b/metaprogram.jai index 1c52352..501f9fc 100644 --- a/metaprogram.jai +++ b/metaprogram.jai @@ -601,6 +601,8 @@ deserialize_entity :: (scene: *Scene, id: Entity_Id, path: string) -> *Entity { %1 } + free(content); + return e; } diff --git a/module.jai b/module.jai index 62d8f56..ba9f720 100644 --- a/module.jai +++ b/module.jai @@ -82,6 +82,14 @@ coven_init :: (window_title: string, window_width: u32, window_height: u32, full } coven_run :: (game_update_proc: (float), game_editor_update_proc: (float), game_update_post_physics_proc: (float)) { + if engine.current_scene == null { + last_opened_scene := get_last_opened_scene_file(); + if last_opened_scene.count > 0 { + engine.current_scene = load_scene(last_opened_scene); + free(last_opened_scene); + } + } + time = xx seconds_since_init(); while !quit { diff --git a/ui/ui.jai b/ui/ui.jai index a2d441f..8fab4fb 100644 --- a/ui/ui.jai +++ b/ui/ui.jai @@ -1,4 +1,5 @@ MAX_VERT_BUFFERS :: 64; +MAX_BOXES :: 4096; UI_Box_Flags :: enum_flags u32 { NONE :: 0; @@ -264,7 +265,7 @@ ui_init :: () { ui_state.colored_verts.allocator = ui_state.allocator; ui_state.boxes.allocator = ui_state.allocator; - init(*ui_state.boxes, 1024); + init(*ui_state.boxes, MAX_BOXES); ui_state.sampler = create_sampler(engine.renderer); ui_state.fonts.regular = create_font(engine.renderer, "../assets/fonts/roboto/Roboto-Regular.ttf", 14); @@ -720,6 +721,7 @@ get_ui_box_or_create_new :: (hash: u32) -> *UI_Box, bool { ptr := table_find_pointer(*ui_state.boxes, hash); if ptr == null { + assert(ui_state.boxes.count < MAX_BOXES); table_add(*ui_state.boxes, hash, .{}); ptr = table_find_pointer(*ui_state.boxes, hash); }