ui: Bug fix: gl* method should be called after looping starts

This commit is contained in:
Hajime Hoshi 2016-02-21 21:53:48 +09:00
parent bae6d62067
commit bb39766873
2 changed files with 17 additions and 14 deletions

View File

@ -49,6 +49,12 @@ func GetProgramID(p Program) ProgramID {
type context struct{} type context struct{}
// TODO: These variables can be in the context struct.
var (
gl mgl.Context
worker mgl.Worker
)
func NewContext() *Context { func NewContext() *Context {
c := &Context{ c := &Context{
Nearest: mgl.NEAREST, Nearest: mgl.NEAREST,
@ -62,27 +68,21 @@ func NewContext() *Context {
Triangles: mgl.TRIANGLES, Triangles: mgl.TRIANGLES,
Lines: mgl.LINES, Lines: mgl.LINES,
} }
c.init() gl, worker = mgl.NewContext()
return c return c
} }
var (
gl mgl.Context
worker mgl.Worker
workerCreated = make(chan struct{})
)
func Loop() { func Loop() {
<-workerCreated
for { for {
<-worker.WorkAvailable() <-worker.WorkAvailable()
worker.DoWork() worker.DoWork()
} }
} }
func (c *Context) init() { func (c *Context) Init() {
gl, worker = mgl.NewContext() // This initialization must be done after Loop is called.
close(workerCreated) // This is why Init is separated from NewContext.
// Textures' pixel formats are alpha premultiplied. // Textures' pixel formats are alpha premultiplied.
gl.Enable(mgl.BLEND) gl.Enable(mgl.BLEND)
gl.BlendFunc(mgl.ONE, mgl.ONE_MINUS_SRC_ALPHA) gl.BlendFunc(mgl.ONE, mgl.ONE_MINUS_SRC_ALPHA)

View File

@ -32,7 +32,6 @@ func Now() int64 {
var currentUI *userInterface var currentUI *userInterface
func Init() *opengl.Context { func Init() *opengl.Context {
// TODO: Is this OK to lock OS thread only for UI?
runtime.LockOSThread() runtime.LockOSThread()
err := glfw.Init() err := glfw.Init()
@ -53,16 +52,20 @@ func Init() *opengl.Context {
u := &userInterface{ u := &userInterface{
window: window, window: window,
} }
ch := make(chan struct{})
go func() { go func() {
runtime.LockOSThread() runtime.LockOSThread()
u.window.MakeContextCurrent() u.window.MakeContextCurrent()
glfw.SwapInterval(1) glfw.SwapInterval(1)
close(ch)
opengl.Loop() opengl.Loop()
}() }()
currentUI = u currentUI = u
context := opengl.NewContext()
<-ch
context.Init()
return opengl.NewContext() return context
} }
func Start(width, height, scale int, title string) (actualScale int, err error) { func Start(width, height, scale int, title string) (actualScale int, err error) {