Console works again | UI crash fixes

This commit is contained in:
2024-12-22 00:49:35 +01:00
parent 664626c838
commit f8b540f583
7 changed files with 119 additions and 9 deletions

View File

@@ -1,10 +1,8 @@
#import "File_Utilities";
console : *Console; console : *Console;
Command_Proc :: struct { Command_Proc :: struct {
name: string; name: string;
proc: (arguments: [] string, buffer: *[..] Buffer_Entry); proc: (arguments: [] string);
} }
Buffer_Entry :: struct { Buffer_Entry :: struct {
@@ -30,7 +28,15 @@ Console :: struct {
verts: [..] Colored_Vert; 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 : Command_Proc;
command.name = copy_string(cmd); command.name = copy_string(cmd);
command.proc = proc; command.proc = proc;
@@ -40,7 +46,7 @@ add_command :: (console: *Console, cmd: string, proc: (arguments: [] string, buf
init_console :: () { init_console :: () {
console = New(Console); console = New(Console);
console.verts.allocator = temp; //console.verts.allocator = temp;
buffer_size := size_of(Colored_Vert) * 12; 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.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); 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, "copy", copy_scene);
//add_command(console, "vsync", set_vsync); //add_command(console, "vsync", set_vsync);
add_engine_console_commands();
//engine.console = console; //engine.console = console;
} }
@@ -112,12 +120,16 @@ update_console :: () {
// So each subsequent space will be an argument itself. // So each subsequent space will be an argument itself.
// This is a bit of a hacky fix for now // This is a bit of a hacky fix for now
arguments := split(console.current_string, " "); arguments := split(console.current_string, " ");
index := 0; index := 0;
command := arguments[0];
while index < arguments.count - 1 { while index < arguments.count - 1 {
if arguments[index].count == 0 { if arguments[index].count == 0 {
arguments[index] = arguments[arguments.count - 1]; arguments[index] = arguments[arguments.count - 1];
arguments.count -= 1; arguments.count -= 1;
} else { } else {
if index > 0 {
}
index += 1; index += 1;
} }
} }
@@ -126,12 +138,21 @@ update_console :: () {
arguments.count -= 1; 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]); cmd, success := find_command(console, arguments[0]);
push_entry(*console.buffer, console.current_string, .{1,1,1,1}); push_entry(*console.buffer, console.current_string, .{1,1,1,1});
if success { if success {
cmd.proc(arguments, *console.buffer); cmd.proc(for_cmd);
} else { } else {
push_entry(*console.buffer, console.current_string, .{1,0,0,1}); 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; entry.color = color;
array_add(buffer, entry); array_add(buffer, entry);
} }
#import "File_Utilities";
#load "console_commands.jai";

58
core/console_commands.jai Normal file
View File

@@ -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]);
}

View File

@@ -71,9 +71,25 @@ load_scene :: (name: string) -> *Scene {
engine.procs.on_scene_loaded(scene, engine.mode); engine.procs.on_scene_loaded(scene, engine.mode);
} }
if engine.mode == .EDITING {
save_last_opened_scene_file(name);
}
return scene; 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) { save_scene :: (scene: *Scene) {
path := "../assets/scenes/"; path := "../assets/scenes/";
save_scene(scene, path); save_scene(scene, path);

View File

@@ -104,9 +104,9 @@ editor_ui :: () {
clicked := false; clicked := false;
selected := array_find(engine.editor.selected_entities, it); selected := array_find(engine.editor.selected_entities, it);
if it.name.count == 0 { 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 { } else {
clicked = ui_clickable_label(it.name, selected, it_index); clicked = ui_clickable_label(it.name, selected, it.id);
} }
if clicked { if clicked {

View File

@@ -601,6 +601,8 @@ deserialize_entity :: (scene: *Scene, id: Entity_Id, path: string) -> *Entity {
%1 %1
} }
free(content);
return e; return e;
} }

View File

@@ -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)) { 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(); time = xx seconds_since_init();
while !quit { while !quit {

View File

@@ -1,4 +1,5 @@
MAX_VERT_BUFFERS :: 64; MAX_VERT_BUFFERS :: 64;
MAX_BOXES :: 4096;
UI_Box_Flags :: enum_flags u32 { UI_Box_Flags :: enum_flags u32 {
NONE :: 0; NONE :: 0;
@@ -264,7 +265,7 @@ ui_init :: () {
ui_state.colored_verts.allocator = ui_state.allocator; ui_state.colored_verts.allocator = ui_state.allocator;
ui_state.boxes.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.sampler = create_sampler(engine.renderer);
ui_state.fonts.regular = create_font(engine.renderer, "../assets/fonts/roboto/Roboto-Regular.ttf", 14); 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); ptr := table_find_pointer(*ui_state.boxes, hash);
if ptr == null { if ptr == null {
assert(ui_state.boxes.count < MAX_BOXES);
table_add(*ui_state.boxes, hash, .{}); table_add(*ui_state.boxes, hash, .{});
ptr = table_find_pointer(*ui_state.boxes, hash); ptr = table_find_pointer(*ui_state.boxes, hash);
} }