main: backport gl02 goodies

This commit is contained in:
2022-02-05 19:43:28 +08:00
parent 92b0ace7a2
commit d376a0fd39
4 changed files with 92 additions and 7 deletions

20
internal/io/config.go Normal file
View File

@@ -0,0 +1,20 @@
package io
import "edgaru089.ml/go/gl01/internal/util"
var (
MainConfig struct {
WindowWidth, WindowHeight int
FramerateLimit int // 0: vsync, -1: unlimited
}
)
func LoadConfig() (err error) {
err = util.LoadJSON("main.cfg.json", &MainConfig)
if err != nil {
return
}
return
}

40
internal/util/json.go Normal file
View File

@@ -0,0 +1,40 @@
package util
import (
"encoding/json"
"os"
)
func LoadJSON(filename string, object interface{}) (err error) {
file, err := os.Open(filename)
if err != nil {
return
}
dec := json.NewDecoder(file)
err = dec.Decode(object)
if err != nil {
return
}
file.Close()
return
}
func SaveJSON(filename string, object interface{}) (err error) {
file, err := os.Create(filename)
if err != nil {
return
}
enc := json.NewEncoder(file)
err = enc.Encode(object)
if err != nil {
return
}
file.Close()
return
}