diff --git a/cmd/main.cfg.json b/cmd/main.cfg.json new file mode 100644 index 0000000..287a602 --- /dev/null +++ b/cmd/main.cfg.json @@ -0,0 +1,7 @@ +{ + "WindowWidth": 1600, + "WindowHeight": 900, + + "FramerateLimit": 60 +} + diff --git a/cmd/main.go b/cmd/main.go index ca4181e..7a7f0ad 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -13,34 +13,48 @@ import ( "github.com/go-gl/glfw/v3.3/glfw" ) -const ( - //windowWidth = 852 - //windowHeight = 480 - windowWidth = 1600 - windowHeight = 900 +var ( + frameTick *time.Ticker ) + func init() { runtime.LockOSThread() } + func main() { + err := gio.LoadConfig() + if err != nil { + panic(err) + } + if err := glfw.Init(); err != nil { panic(err) } defer glfw.Terminate() + // framerate limit ticker + if gio.MainConfig.FramerateLimit > 0 { + frameTick = time.NewTicker(time.Second / time.Duration(gio.MainConfig.FramerateLimit)) + } + glfw.WindowHint(glfw.Resizable, 1) glfw.WindowHint(glfw.ContextVersionMajor, 3) glfw.WindowHint(glfw.ContextVersionMinor, 3) glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile) glfw.WindowHint(glfw.OpenGLForwardCompatible, 1) - win, err := glfw.CreateWindow(windowWidth, windowHeight, "Gl01", nil, nil) + win, err := glfw.CreateWindow(gio.MainConfig.WindowWidth, gio.MainConfig.WindowHeight, "Gl01", nil, nil) if err != nil { panic(err) } win.MakeContextCurrent() - glfw.SwapInterval(1) + // vsync + if gio.MainConfig.FramerateLimit == 0 { + glfw.SwapInterval(1) + } else { + glfw.SwapInterval(0) + } err = gl.Init() if err != nil { @@ -83,5 +97,9 @@ func main() { } else { fpsCounter++ } + + if gio.MainConfig.FramerateLimit > 0 { + <-frameTick.C + } } } diff --git a/internal/io/config.go b/internal/io/config.go new file mode 100644 index 0000000..df84c26 --- /dev/null +++ b/internal/io/config.go @@ -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 +} diff --git a/internal/util/json.go b/internal/util/json.go new file mode 100644 index 0000000..bece338 --- /dev/null +++ b/internal/util/json.go @@ -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 +}