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;
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";

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