Skip to content

Commit

Permalink
Merge pull request #66 from drmargarido/master
Browse files Browse the repository at this point in the history
Loading a json file with the unmarshal function
  • Loading branch information
karl-zylinski authored Jan 12, 2025
2 parents 41b774a + 2977351 commit 745fa3d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions json/load_json_unmarshal/game_settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{

"window_width": 1920,
"window_height": 1080,
"window_title": "My Game!",

"rendering_api": "opengl",
"renderer_settings": {
"msaa": false,
"depth_testing": true
}

}
39 changes: 39 additions & 0 deletions json/load_json_unmarshal/load_json_unmarshal.odin
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 745fa3d

Please sign in to comment.