Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve ktx-assets integration with gdx-freetype #113

Closed
keturn opened this issue Nov 12, 2017 · 7 comments
Closed

improve ktx-assets integration with gdx-freetype #113

keturn opened this issue Nov 12, 2017 · 7 comments
Assignees
Labels
assets Issues of the ktx-assets module freetype Issues of the ktx-freetype and ktx-freetype-async modules
Milestone

Comments

@keturn
Copy link
Contributor

keturn commented Nov 12, 2017

As someone new to GDX and Kotlin, this one took me a while.

As the libgdx wiki describes, you need to set up FreetypeFontLoader on your AssetManager, since it doesn't register itself as a loader by default.

My transcription of that for ktx-assets looks like:

fun fontAssetManager(): AssetManager {
    val assetManager = AssetManager()
    val resolver = InternalFileHandleResolver()

    assetManager.setLoader<FreeTypeFontGenerator, FreeTypeFontGeneratorLoader.FreeTypeFontGeneratorParameters>(FreeTypeFontGeneratorLoader(resolver))

    val freeTypeLoader = FreetypeFontLoader(resolver)

    assetManager.setLoader<BitmapFont, FreetypeFontLoader.FreeTypeFontLoaderParameter>(freeTypeLoader, suffix = ".ttf")
    assetManager.setLoader<BitmapFont, FreetypeFontLoader.FreeTypeFontLoaderParameter>(freeTypeLoader, suffix = ".otf")

    return assetManager
}

and then that asset manager may be used like

val fontName = "myfont.otf"

val fontParameter = FreetypeFontLoader.FreeTypeFontLoaderParameter()
fontParameter.fontFileName = fontName
fontParameter.fontParameters.size = titleFontSize

val fontDescriptor = assetManager.load<BitmapFont>(fontName, fontParameter)

fontDescriptor.finishLoading()
font = titleFontDescriptor.asset

So I guess the areas of improvement include

  • have ktx-assets (or ktx-freetype?) register the loaders
  • a more Kotlinesque interface to FreeTypeFontLoaderParameter? The mutable parameter object you have to initialize as empty and then set the properties on seems not very safe.
  • some documentation reminding users that yes, it's load<BitmapFont>, not load<FreetypeFont>. Because you're constructing a bitmap font out of this, even though the input is a TTF file.
@czyzby czyzby added the assets Issues of the ktx-assets module label Nov 12, 2017
@czyzby
Copy link
Member

czyzby commented Nov 28, 2017

I agree that the loader registration syntax seems overcomplicated, this is exactly the kind of thing that KTX could improve. Not sure if another dependency in ktx-assets is a good idea - ktx-freetype module seems better. I'll look into it.

The mutable parameter object you have to initialize as empty and then set the properties on seems not very safe.

Welcome to LibGDX.

@czyzby czyzby added the freetype Issues of the ktx-freetype and ktx-freetype-async modules label Nov 28, 2017
@czyzby czyzby added this to the 1.9.7 milestone Nov 28, 2017
@czyzby czyzby self-assigned this Nov 28, 2017
czyzby added a commit that referenced this issue Nov 28, 2017
@czyzby
Copy link
Member

czyzby commented Nov 28, 2017

io.github.libktx:ktx-freetype:1.9.7-SNAPSHOT should be available shortly. Can you check out the new module and see if that's what you meant?

@keturn
Copy link
Contributor Author

keturn commented Dec 3, 2017

Okay, with that my class looks like this:

const val TITLE_FONT = "neat-font.otf"

class MyScreen : KtxScreen {
    val batch = SpriteBatch()
    val assetManager = AssetManager()
    val titleFont: BitmapFont
    var titleFontSize = 48

    init {
        assetManager.registerFreeTypeFontLoaders()
        assetManager.loadFreeTypeFont(TITLE_FONT) {
            size = titleFontSize;
            color = Color.DARK_GRAY
        }
        assetManager.finishLoadingAsset(TITLE_FONT)
        titleFont = assetManager.get(TITLE_FONT)
    }
}

Which is certainly an improvement!

Some questions I'm left with:

  1. assetManager.load<BitmapFont> returned a descriptor I could call finishLoading() on, but loadFreeTypeFont doesn't seem to return any such thing?
  2. When I was reading Getting Started materials those few weeks ago, I got the strong impression that I'd want to define as many things as possible where they're first declared in the class definition, rather than having separate initialization code in the class's init method. Is that desirable & practical here?

@czyzby
Copy link
Member

czyzby commented Dec 3, 2017

assetManager.load<BitmapFont> returned a descriptor I could call finishLoading()

Good point, I'll unify the API.

As for the initialization code: I guess it depends on your code style and project structure. I try to initialize vals as soon as possible and tend to avoid init blocks when it comes to most classes, but there are things like the GUI with Scene2D DSL which might as well be moved to the init. Some LibGDX APIs are not very Kotlin-friendly, like the create method in ApplicationListener which kind of prevents you from using val and forces to use the nullable var or lateinit var instead.

And be careful with finishLoadingAsset. What it does is block the thread and keeps on checking if that particular asset was loaded, without prioritizing it or anything. If you've scheduled multiple assets for loading, it might freeze your application. Take a look at AssetManager article and try to use field delegates instead (see ktx-assets readme).

@czyzby czyzby modified the milestones: 1.9.7, 1.9.8 Dec 17, 2017
@czyzby czyzby mentioned this issue Dec 17, 2017
4 tasks
@czyzby
Copy link
Member

czyzby commented Dec 17, 2017

@keturn Have you tried using the latest snapshot with the API improvements?

I think the module is OK as it is, but I realized that AssetStorage (coroutines-based AssetManager alternative) could also use the same API improvements, so expect more updates.

czyzby added a commit that referenced this issue Jan 26, 2018
@czyzby
Copy link
Member

czyzby commented Jan 26, 2018

I think I'm done with both ktx-freetype (for LibGDX AssetManager) and ktx-freetype-async (for our AssetStorage from ktx-async. Both modules should be available through snapshots as 1.9.8-SNAPSHOT - since there have been quite a few changes and we still have not released 1.9.8 yet, I'd like to publish a release pretty soon. @keturn, @Jkly, would you mind checking out the FreeType utilities before the release?

czyzby added a commit that referenced this issue Jan 27, 2018
@czyzby
Copy link
Member

czyzby commented Jan 27, 2018

I've created a few mock applications to test this out and it seems to work without issues on desktop, with both OTF/TTF files and AssetManager/AssetStorage. The only thing that could have been improved was font generator API, so I added a small Kotlin DSL for it. I'm closing this for now, feel free to reopen if you think the modules could still use some improvements or more utilities.

@czyzby czyzby closed this as completed Jan 27, 2018
dwursteisen pushed a commit to dwursteisen/ktx that referenced this issue Mar 27, 2018
dwursteisen pushed a commit to dwursteisen/ktx that referenced this issue Mar 27, 2018
dwursteisen pushed a commit to dwursteisen/ktx that referenced this issue Mar 27, 2018
DavidPartouche pushed a commit to DavidPartouche/ktx that referenced this issue Aug 20, 2018
DavidPartouche pushed a commit to DavidPartouche/ktx that referenced this issue Aug 20, 2018
DavidPartouche pushed a commit to DavidPartouche/ktx that referenced this issue Aug 20, 2018
DavidPartouche pushed a commit to DavidPartouche/ktx that referenced this issue Aug 20, 2018
DavidPartouche pushed a commit to DavidPartouche/ktx that referenced this issue Aug 20, 2018
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
assets Issues of the ktx-assets module freetype Issues of the ktx-freetype and ktx-freetype-async modules
Projects
None yet
Development

No branches or pull requests

2 participants