gl01/cmd/main.go

106 lines
2.1 KiB
Go
Raw Normal View History

2022-01-20 21:58:50 +08:00
package main
import (
"fmt"
"runtime"
"time"
"edgaru089.ink/go/gl01/internal/game"
gio "edgaru089.ink/go/gl01/internal/io"
_ "edgaru089.ink/go/gl01/internal/render/gpu_preference"
"edgaru089.ink/go/gl01/internal/util/itype"
2022-01-20 21:58:50 +08:00
"github.com/go-gl/gl/all-core/gl"
"github.com/go-gl/glfw/v3.3/glfw"
)
2022-02-05 19:43:28 +08:00
var (
frameTick *time.Ticker
2022-01-20 21:58:50 +08:00
)
2022-02-05 19:43:28 +08:00
2022-01-20 21:58:50 +08:00
func init() {
runtime.LockOSThread()
}
2022-02-05 19:43:28 +08:00
2022-01-20 21:58:50 +08:00
func main() {
2022-02-05 19:43:28 +08:00
err := gio.LoadConfig()
if err != nil {
panic(err)
}
2022-01-20 21:58:50 +08:00
if err := glfw.Init(); err != nil {
panic(err)
}
defer glfw.Terminate()
2022-02-05 19:43:28 +08:00
// framerate limit ticker
if gio.MainConfig.FramerateLimit > 0 {
frameTick = time.NewTicker(time.Second / time.Duration(gio.MainConfig.FramerateLimit))
}
2022-01-20 21:58:50 +08:00
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)
2022-02-05 19:43:28 +08:00
win, err := glfw.CreateWindow(gio.MainConfig.WindowWidth, gio.MainConfig.WindowHeight, "Gl01", nil, nil)
2022-01-20 21:58:50 +08:00
if err != nil {
panic(err)
}
win.MakeContextCurrent()
2022-02-05 19:43:28 +08:00
// vsync
if gio.MainConfig.FramerateLimit == 0 {
glfw.SwapInterval(1)
} else {
glfw.SwapInterval(0)
}
2022-01-20 21:58:50 +08:00
err = gl.Init()
if err != nil {
panic(err)
}
fmt.Println("OpenGL Version: ", gl.GoStr(gl.GetString(gl.VERSION)))
game := game.NewGame()
game.Init(win)
gio.ClearColor = itype.Vec4f{0.6, 0.8, 1.0, 1.0}
gio.FogColor = itype.Vec4f{0.6, 0.8, 1.0, 1.0}
2022-01-20 21:58:50 +08:00
gl.ClearColor(0.6, 0.8, 1.0, 1)
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
win.SwapBuffers()
winClock := time.Now()
fpsClock := time.Now()
fpsCounter := 0
for !win.ShouldClose() {
deltaTime := time.Since(winClock)
winClock = time.Now()
game.Update(win, deltaTime)
2022-01-22 23:06:41 +08:00
gl.ClearColor(0.6, 0.8, 1.0, 1)
2022-01-20 21:58:50 +08:00
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
game.Render(win)
win.SwapBuffers()
glfw.PollEvents()
if time.Since(fpsClock) >= time.Second {
win.SetTitle(fmt.Sprintf("Gl01 %dFPS", fpsCounter))
fpsCounter = 0
fpsClock = time.Now()
} else {
fpsCounter++
}
2022-02-05 19:43:28 +08:00
if gio.MainConfig.FramerateLimit > 0 {
<-frameTick.C
}
2022-01-20 21:58:50 +08:00
}
}