faiface / pixel

A hand-crafted 2D game library in Go
MIT License
4.46k stars 245 forks source link

func text.NewAtlas() cost too much memory #274

Closed gakkilove closed 3 years ago

gakkilove commented 3 years ago

I use SourceHanSansK-RegularSub.ttf in my game, so i can print Chinese charactors here is my code fontFaceSourceHan, err := utils.LoadTTF("./resources/font/SourceHanSansK-RegularSub.ttf", 16) utils.PanicIfErr(err) utils.PrintMemoryUsage() SourceHanSansKAtlas = text.NewAtlas(fontFaceSourceHan, text.RangeTable(unicode.Han), text.ASCII) utils.PrintMemoryUsage() I find that it costs too much memory and it will cause other problems like frame drop.

image

by the way, if i use basicfont.face7x13,it works very well text.NewAtlas(basicfont.Face7x13, text.ASCII)

How can i fix this? Thank you very much

bcvery1 commented 3 years ago

Just wondering, maybe it'll help others debug this, are you creating a new atlas every frame, or just once then reusing it?

gakkilove commented 3 years ago

Just wondering, maybe it'll help others debug this, are you creating a new atlas every frame, or just once then reusing it?

I create this atlas in function init,keep it as a global variable like this image I only use text.Text.Clear() and text.WriteString() in every frame and i have decreased the size of my ttf file, now there are about 3500 chinese charactors in my ttf file,but it still not work well

Asday commented 3 years ago

ASCII has way less characters in it that the 3500 you're mentioning - it's possible this is simply expected from loading such a large atlas.

I wonder, back in the day, developers were limited on space not just in memory but also on storage. Maybe it's worth looking up how developers managed storage of fonts for Chinese game boy games or similar.

For this, I might suggest loading exactly only the characters you're going to use in the near future, maybe deleting and recreating the atlas with different characters at different points in the game. I won't pretend to know much about Chinese specifically, but generally languages use the most common word/character way more than the second most common, and so on. It's unlikely you'll need all 3500 characters for every scene throughout your whole game.

gakkilove commented 3 years ago

ASCII has way less characters in it that the 3500 you're mentioning - it's possible this is simply expected from loading such a large atlas.

I wonder, back in the day, developers were limited on space not just in memory but also on storage. Maybe it's worth looking up how developers managed storage of fonts for Chinese game boy games or similar.

For this, I might suggest loading exactly only the characters you're going to use in the near future, maybe deleting and recreating the atlas with different characters at different points in the game. I won't pretend to know much about Chinese specifically, but generally languages use the most common word/character way more than the second most common, and so on. It's unlikely you'll need all 3500 characters for every scene throughout your whole game.

Thank you very much for rely this issue. :) Actually, i only need about 50 or less characters per scene. i realized it is really stupid to load all 3500 characters into memory, that's terrible. -_-|| And now, i have changed my code, i will load those characters which i will use before i get into every scene. Thank you!