func LoadDictionary(r io.Reader) (*Dictionary, error) { init_once.Do(func() { render.Queue(func() { err := render.RegisterShader("glop.font", []byte(font_vertex_shader), []byte(font_fragment_shader)) if err != nil { panic(err) } }) render.Purge() }) var d Dictionary err := gob.NewDecoder(r).Decode(&d.data) if err != nil { return nil, err } d.setupGlStuff() return &d, nil }
// LoadDictionary reads a gobbed Dictionary object from r, registers its atlas texture with opengl, // and returns a Dictionary that is ready to render text. func LoadDictionary(r io.Reader) (*Dictionary, error) { errChan := make(chan error) initOnce.Do(func() { render.Queue(func() { // errChan <- render.RegisterShader("glop.font", []byte(font_vertex_shader), []byte(font_fragment_shader)) errChan <- render.RegisterShader("glop.font", []byte(font_vshader), []byte(font_fshader)) }) }) err := <-errChan if err != nil { return nil, err } var dict Dictionary dec := gob.NewDecoder(r) err = dec.Decode(&dict) if err != nil { return nil, err } render.Queue(func() { // Create the gl texture for the atlas gl.GenTextures(1, &dict.atlas.texture) glerr := gl.GetError() if glerr != 0 { errChan <- fmt.Errorf("Gl Error on gl.GenTextures: %v", glerr) return } // Send the atlas to opengl gl.BindTexture(gl.TEXTURE_2D, dict.atlas.texture) gl.PixelStorei(gl.UNPACK_ALIGNMENT, 1) gl.TexImage2D( gl.TEXTURE_2D, 0, gl.RED, dict.Dx, dict.Dy, 0, gl.RED, gl.UNSIGNED_BYTE, gl.Ptr(&dict.Pix[0])) glerr = gl.GetError() if glerr != 0 { errChan <- fmt.Errorf("Gl Error on creating texture: %v", glerr) return } // Create the atlas sampler and set the parameters we want for it gl.GenSamplers(1, &dict.atlas.sampler) gl.SamplerParameteri(dict.atlas.sampler, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.SamplerParameteri(dict.atlas.sampler, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.SamplerParameteri(dict.atlas.sampler, gl.TEXTURE_WRAP_S, gl.REPEAT) gl.SamplerParameteri(dict.atlas.sampler, gl.TEXTURE_WRAP_T, gl.REPEAT) glerr = gl.GetError() if glerr != 0 { errChan <- fmt.Errorf("Gl Error on creating sampler: %v", glerr) return } errChan <- nil }) err = <-errChan if err != nil { return nil, err } return &dict, nil }