diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 2a9ffe8..0393d36 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -45,6 +45,7 @@ jobs: odin check glfw/window $FLAGS odin check json/load_json $FLAGS + odin check json/load_json_unmarshal $FLAGS odin check json/odin_info $FLAGS odin check learn_metal/00-window $FLAGS diff --git a/json/load_json_unmarshal/game_settings.json b/json/load_json_unmarshal/game_settings.json new file mode 100644 index 0000000..adc8abe --- /dev/null +++ b/json/load_json_unmarshal/game_settings.json @@ -0,0 +1,13 @@ +{ + + "window_width": 1920, + "window_height": 1080, + "window_title": "My Game!", + + "rendering_api": "opengl", + "renderer_settings": { + "msaa": false, + "depth_testing": true + } + +} \ No newline at end of file diff --git a/json/load_json_unmarshal/load_json_unmarshal.odin b/json/load_json_unmarshal/load_json_unmarshal.odin new file mode 100644 index 0000000..817aa2d --- /dev/null +++ b/json/load_json_unmarshal/load_json_unmarshal.odin @@ -0,0 +1,39 @@ +package load_json_unmarshal + +import "core:os" +import "core:fmt" +import "core:encoding/json" + +Game_Settings :: struct { + window_width: i32, + window_height: i32, + window_title: string, + rendering_api: string, + renderer_settings: struct{ + msaa: bool, + depth_testing: bool, + }, +} + +main :: proc(){ + // Load in your json file! + data, ok := os.read_entire_file_from_filename("game_settings.json") + if !ok { + fmt.eprintln("Failed to load the file!") + return + } + defer delete(data) // Free the memory at the end + + // Load data from the json bytes directly to the struct + settings: Game_Settings + unmarshal_err := json.unmarshal(data, &settings) + if unmarshal_err != nil { + fmt.eprintln("Failed to unmarshal the file!") + return + } + fmt.eprintf("Result %v\n", settings) + + // Clear allocated strings + delete(settings.window_title) + delete(settings.rendering_api) +} \ No newline at end of file