简体   繁体   English

Libgdx-加载字体会减慢游戏速度

[英]Libgdx - Loading fonts slows down game

I'm using FreeTypeFontGenerator to load my fonts. 我正在使用FreeTypeFontGenerator加载字体。 Currently I have 6 different BitmapFonts and I load them in the constructor of my Screen class and draw them in the render function. 目前,我有6种不同的BitmapFonts,并将它们加载到Screen类的构造函数中,并在render函数中绘制它们。 Here's a code snippet, myFont is a class I've written which internally uses FreeTypeFontGenerator and the loadFont function returns a BitmapFont object. 这是一个代码段,myFont是我编写的一个类,在内部使用FreeTypeFontGenerator,而loadFont函数返回一个BitmapFont对象。

public Screen1 () {
    .....
    .....

    myFont = new MyFont();

    circleFonts = new BitmapFont[4];
    for (int i=0; i<4; i++) {
        circleFonts[i] = myFont.loadFont("fonts/roboto/Roboto-Light.ttf", 20);
    }
    textOrColorFont = myFont.loadFont("fonts/roboto/Roboto-Light.ttf",15);
    currentColorFont = myFont.loadFont("fonts/roboto/Roboto-Light.ttf", 15);
}

The problem is since I have to load these fonts every time I move to Screen1, the game gets stuck for a good 2-3 seconds because it has to load the fonts from the file. 问题是由于每次移动到Screen1时都必须加载这些字体,因此游戏会停留2-3秒,因为它必须从文件中加载字体。 Is there a better way to do this? 有一个更好的方法吗?

Thanks. 谢谢。

maybe you want to look at this: 也许你想看看这个:

Libgdx FreeTypeFontGenerator with AssetManager 使用AssetManager的Libgdx FreeTypeFontGenerator

not only for font, you can also use it for all their asset either bitmap font, music, pictures, or anything else that supports AssetManager. 您不仅可以将其用于字体,还可以将其用于其所有资产,包括位图字体,音乐,图片或任何其他支持AssetManager的资源。

AssetManager wiki -> https://github.com/libgdx/libgdx/wiki/Managing-your-assets AssetManager Wiki-> https://github.com/libgdx/libgdx/wiki/Managing-your-assets

I hope you help. 希望对您有所帮助。

Well I'm not familiar with this library in particular but I had a slimier problem a couple of years ago. 好吧,我对这个库不是特别熟悉,但是几年前我遇到了一个更苗条的问题。

As the fonts are a Resource they should be loaded at the game/level start-up. 由于字体是资源,因此应在游戏/关卡启动时加载它们。

And not while the game is already running. 而且不是在游戏已经运行的时候。

Reading from the disc is an IO operation and it is quite costly, so loading all of your resources before usage (fonts sprites and so on) and then using their reference is a smarter way of handling this. 从光盘读取是一项IO操作,而且成本很高,因此在使用之前加载所有资源(字体精灵等),然后使用它们的引用是一种更明智的处理方式。

It should also be noted that doing any kind logic in the constructor is bad practice and should be avoided at any cost, the initialization phase of class should be quick. 还应该注意的是,在构造函数中执行任何种类的逻辑都是不好的做法,应不惜一切代价避免,类的初始化阶段应该很快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM