Example #1
0
func init() {
	renderVSync.Callback(func() {
		if glfw.GetCurrentContext() == nil {
			return
		}
		if renderVSync.Value() {
			glfw.SwapInterval(1)
		} else {
			glfw.SwapInterval(0)
		}
	})
}
Example #2
0
// GetTexture returns the related TextureInfo for the requested texture.
// If the texture isn't found a placeholder is returned instead.
// The plugin prefix of 'minecraft:' is default
func GetTexture(name string) TextureInfo {
	textureLock.RLock()
	defer textureLock.RUnlock()
	t, ok := textureMap[name]
	if !ok {
		textureLock.RUnlock()
		ret := make(chan struct{}, 1)
		f := func() {
			textureLock.Lock()
			defer textureLock.Unlock()
			// Check to see if it was already loaded between
			// requesting it
			if _, ok = textureMap[name]; ok {
				t = textureMap[name]
				ret <- struct{}{}
				return
			}
			ns := name
			plugin := "minecraft"
			if pos := strings.IndexRune(name, ':'); pos != -1 {
				plugin = name[:pos]
				ns = name[pos+1:]
			}
			r, err := resource.Open(plugin, "textures/"+ns+".png")
			if err == nil {
				defer r.Close()
				img, err := png.Decode(r)
				if err != nil {
					panic(fmt.Sprintf("(%s): %s", name, err))
				}
				s := &loadedTexture{
					Plugin: plugin,
					File:   "textures/" + ns + ".png",
					Image:  img,
				}
				loadedTextures = append(loadedTextures, s)
				loadTexFile(s)
			}
			t, ok = textureMap[name]
			if !ok {
				ti := *textureMap["missing_texture"]
				t = &ti
				textureMap[name] = t
				s := &loadedTexture{
					Plugin: plugin,
					File:   "textures/" + ns + ".png",
					Image:  nil,
				}
				loadedTextures = append(loadedTextures, s)
			}
			ret <- struct{}{}
		}
		if w := glfw.GetCurrentContext(); w == nil {
			syncChan <- f
		} else {
			f()
		}
		<-ret
		textureLock.RLock()
	}
	return t
}