Update texture atlas straight from memory

This commit is contained in:
bloeys
2022-07-03 09:22:51 +04:00
parent 323cd1099f
commit a4b7dfd395
2 changed files with 7 additions and 15 deletions

View File

@ -67,8 +67,8 @@ func NewFontAtlasFromFont(f *truetype.Font, face font.Face, pointSize uint) (*Fo
assert.T(len(glyphs) > 0, "no glyphs") assert.T(len(glyphs) > 0, "no glyphs")
//Choose atlas size //Choose atlas size
atlasSizeX := 512 atlasSizeX := 64
atlasSizeY := 512 atlasSizeY := 64
const charPaddingX = 2 const charPaddingX = 2
const charPaddingY = 2 const charPaddingY = 2

View File

@ -3,7 +3,6 @@ package glyphs
import ( import (
"errors" "errors"
"math" "math"
"os"
"github.com/bloeys/gglm/gglm" "github.com/bloeys/gglm/gglm"
"github.com/bloeys/nmage/assets" "github.com/bloeys/nmage/assets"
@ -140,7 +139,7 @@ func (gr *GlyphRend) SetFace(fontOptions *truetype.Options) error {
} }
gr.Atlas = newAtlas gr.Atlas = newAtlas
gr.updateFontAtlasTexture("temp-atlas") gr.updateFontAtlasTexture()
return nil return nil
} }
@ -152,7 +151,7 @@ func (gr *GlyphRend) SetFontFromFile(fontFile string, fontOptions *truetype.Opti
} }
gr.Atlas = atlas gr.Atlas = atlas
gr.updateFontAtlasTexture(fontFile) gr.updateFontAtlasTexture()
return nil return nil
} }
@ -160,20 +159,13 @@ func (gr *GlyphRend) SetFontFromFile(fontFile string, fontOptions *truetype.Opti
//and updates the GlyphRend.AtlasTex field. //and updates the GlyphRend.AtlasTex field.
// //
//Any old textures are deleted //Any old textures are deleted
func (gr *GlyphRend) updateFontAtlasTexture(fontFile string) error { func (gr *GlyphRend) updateFontAtlasTexture() error {
if gr.AtlasTex != nil { if gr.AtlasTex != nil {
gl.DeleteTextures(1, &gr.AtlasTex.TexID) gl.DeleteTextures(1, &gr.AtlasTex.TexID)
} }
pngFileName := fontFile + "-atlas.png" atlasTex, err := assets.LoadTextureInMemImg(gr.Atlas.Img, nil)
err := SaveImgToPNG(gr.Atlas.Img, pngFileName)
if err != nil {
return err
}
defer os.Remove(pngFileName)
atlasTex, err := assets.LoadTexturePNG(pngFileName, nil)
if err != nil { if err != nil {
return err return err
} }
@ -216,7 +208,7 @@ func NewGlyphRend(fontFile string, fontOptions *truetype.Options, screenWidth, s
} }
gr.Atlas = atlas gr.Atlas = atlas
err = gr.updateFontAtlasTexture(fontFile) err = gr.updateFontAtlasTexture()
if err != nil { if err != nil {
return nil, err return nil, err
} }