-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from drmargarido/master
Loading a json file with the unmarshal function
- Loading branch information
Showing
3 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |