1 Star 0 Fork 0

KiwanoEngine/KiwanoGo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
window.go 1.99 KB
一键复制 编辑 原始数据 按行查看 历史
Nomango 提交于 2019-07-18 15:01 . add external package
package kiwano
import (
"github.com/go-gl/glfw/v3.2/glfw"
"kiwanoengine.com/kiwano/external/gl"
)
type Option struct {
Width, Height int
Title string
ClearColor Color
NoTitleBar bool
Fullscreen bool
Resizable bool
Vsync bool
}
type Window struct {
Option
glfw.Window
}
func NewWindow(option *Option) (*Window, error) {
window := &Window{
Option: *option,
}
// Init GLFW
if err := glfw.Init(); err != nil {
return nil, err
}
glfw.WindowHint(glfw.ContextVersionMajor, 3)
glfw.WindowHint(glfw.ContextVersionMinor, 3)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.Visible, glfw.False)
if option.NoTitleBar {
glfw.WindowHint(glfw.Decorated, glfw.False)
} else {
glfw.WindowHint(glfw.Decorated, glfw.True)
}
if option.Resizable {
glfw.WindowHint(glfw.Resizable, glfw.True)
} else {
glfw.WindowHint(glfw.Resizable, glfw.False)
}
// Get monitor information
monitor := glfw.GetPrimaryMonitor()
var monitorMode *glfw.VidMode
if monitor != nil {
monitorMode = monitor.GetVideoMode()
}
// Fullscreen mode
if option.Fullscreen && monitorMode != nil {
option.Width, option.Height = monitorMode.Width, monitorMode.Height
} else {
monitor = nil
}
// Create window
w, err := glfw.CreateWindow(option.Width, option.Height, option.Title, monitor, nil)
if err != nil {
return nil, err
}
if !option.Fullscreen && monitorMode != nil {
w.SetPos((monitorMode.Width-option.Width)/2, (monitorMode.Height-option.Height)/2)
}
w.MakeContextCurrent()
w.SetFramebufferSizeCallback(window.onFramebufferSizeCallback)
if option.Vsync {
glfw.SwapInterval(1)
} else {
glfw.SwapInterval(0)
}
// Init OpenGL
if err := gl.Init(); err != nil {
return nil, err
}
gl.ClearColor(option.ClearColor.ToVec4())
window.Window = *w
return window, nil
}
func (w *Window) onFramebufferSizeCallback(win *glfw.Window, width int, height int) {
w.Width, w.Height = width, height
gl.Viewport(0, 0, int32(width), int32(height))
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/KiwanoEngine/KiwanoGo.git
git@gitee.com:KiwanoEngine/KiwanoGo.git
KiwanoEngine
KiwanoGo
KiwanoGo
master

搜索帮助