MonoGame.Extended icon indicating copy to clipboard operation
MonoGame.Extended copied to clipboard

[BitmapFont] Loading BitmapFont at runtime

Open codecat opened this issue 8 years ago • 19 comments

Is this possible? Right now you need to process any BitmapFont you want to use through the pipeline, but we'd like to be able to load them at runtime, much like you can load Texture2D using Texture2D.FromStream().

codecat avatar Mar 12 '16 13:03 codecat

Definitely a much needed feature :+1:

faejr avatar Mar 12 '16 13:03 faejr

The main downside would be having the core library taking a dependency on XmlSerializer. I've tried really hard not to have any extra dependencies in the core library.

That said, it may not actually be an issue in this case. We'll just have to check if XML de-serialization is supported by PCL's without too much pain.

craftworkgames avatar Mar 13 '16 04:03 craftworkgames

You mean a dependency on System.Xml? That doesn't sound too bad to me.

codecat avatar Mar 15 '16 00:03 codecat

Yeah. It looks like it should be fine. I just wasn't sure before.

craftworkgames avatar Mar 15 '16 00:03 craftworkgames

Yes please, i want to stay away from that Content Pipeline annoying thing

ghost avatar Apr 13 '16 07:04 ghost

i want to stay away from that Content Pipeline annoying thing

I used to think the Content Pipeline was pretty annoying also but there are some benefits to using it. The most obvious reason is that the content is optimized before loading into your game.

However, one other often overlooked benefit is that your game doesn't have to take any dependencies on libraries that may or may not work on other platforms. This means you can process your content on Windows and be sure that it will work on Android and iOS for example.

So I guess what I'm saying is that we may add support for loading some file formats at runtime, but we'll be very selective. The last thing I want is to implement something in the core library that only works on Windows.

Luckily, portable class libraries enforce this by default.

craftworkgames avatar Apr 13 '16 10:04 craftworkgames

My experience of library dependencies has been the other way around - missing dependencies or errors during compile-time of content. This used to not be an issue with XNA, but sadly, Monogame has not implemented the support properly the way that XNA has done it - a custom project that builds the content as-is, without needing the content pipeline tools, or a content pipeline list of contents.

You'd just add the files to the project and it worked. It was pretty good. Right now it's a PITA unless Monogame decides to fix it.

codecat avatar Apr 13 '16 10:04 codecat

It can be good when your game is ready to be published, but it's a nightmare when you are prototyping and testing/editing lot of assets..

ghost avatar Apr 14 '16 21:04 ghost

That's a good point @Scellow. Prototyping can be a pain in MonoGame in general. It would be nice to come up with a way that gives us the best of both worlds.

Arguably, the way XNA did it with a separate project in Visual Studio was better in some ways. Although, I never really loved that either.

craftworkgames avatar Apr 15 '16 05:04 craftworkgames

Neither did I. I always went back to runtime loading of textures regardless, for several reasons - easy editing, and it makes modding possible.

codecat avatar Apr 15 '16 09:04 codecat

Note: in my experience, referencing System.Xml will add about 7Mb(!) to your compiled binary (on iOS fat builds at least) even with full stripping enabled. This may be an issue for some users.

Maybe consider supporting the simpler text format for runtime loading of bitmap fonts, or seeing if a lighter Xml reader can be used instead of System.Xml such as TinyXmlReader or Mono.Xml.

johngirvin avatar Mar 08 '17 14:03 johngirvin

Yeah, I agree. I don't think we want to be referencing System.Xml from the core library at all. Using a lighter XML reader might be a way to go.

craftworkgames avatar Mar 09 '17 03:03 craftworkgames

I use this library: https://github.com/cyotek/Cyotek.Drawing.BitmapFont It works like a charm (though I didnt test it on many different FNTs).

It uses System.Xml. However if it is an issue that code could be commented out. And there still will be an option of loading FNT in ASCII format.

rds1983 avatar Apr 05 '17 17:04 rds1983

Actually there seems to be another solution. That library doesnt use XmlSerializer, but parses Xml manually. So that code could be converted to use System.Xml.Linq(which doesnt require any additional dependencies).

rds1983 avatar Apr 05 '17 17:04 rds1983

Another option that nobody has mentioned yet is that BMFont actually has 2 other export formats.

It would be relatively straightforward to support either of these formats without taking on any other dependencies and since BMFont is stable (last updated in 2016) they are not likely to change anytime soon.

(oh wait, @johngirvin did actually mention that already)

That said, I actually think we could support XML loading at runtime as well, but this just hasn't been on the top of my priorities to figure out. Maybe someone else want's to give it a go?

@RUSshy That XML parser actually looks pretty sweet. LibGDX is a very nice library to reference 👍

craftworkgames avatar Apr 09 '17 13:04 craftworkgames

Interesting. The libGDX parser is generated (partially) from a Ragel FSM definition here XmlReader.rl.

Ragel compiled OOTB on macos and produced the linked C# source: ./ragel -L -G2 -A -o XmlReader.cs XmlReader.rl (gist) ...still a lot of Java to pick out of that, but it's a start.

johngirvin avatar Apr 10 '17 10:04 johngirvin

Alternatively: https://github.com/KirillOsenkov/XmlParser

johngirvin avatar Apr 10 '17 11:04 johngirvin

Moving forward I would recommend people to use https://github.com/rds1983/SpriteFontPlus by @rds1983.

lithiumtoast avatar Nov 02 '20 21:11 lithiumtoast