pythonjsonpygletformatted-text

Render formatted text in python (currently with pyglet)


I'm using pyglet to make a game. At some point, this game will have an in-game help/reference to some "core" RPG rules, plus descriptions and such (the game uses Pathfinder SRD). So there are long formatted texts (with different sizes, colors, etc.). An example of the style I'm aiming to provide (further formatting will be made): In-game SRD

Currently I'm using pyglet's formatted document model (plain text with a few python generated fields for style/formatting):

"""
{font_name 'Fontin'}{.align "center"}{bold True}{font_size 28}{italic True}{indent 0}{color (128,144,160, 223)}
The Title
{.align "left"}{bold False}{font_size 12}
Huge amount of text with further reformatting goes here...
And ends with a copyright notice (section 15 of OGL).
"""

Solution

  • Storage

    If I were you, I'd keep the text in raw format, read it in once into your appropriate dictionary structure and then dump it in a pickle binary. It's insanely fast both to read in and to work with and it takes up less space.

    Decoding

    This is something that needs to be done, one way or another.
    Initially simply having a loading... screen that does the initial setups is fine, but later on you could optimize it and load only the few initial things and load the rest in the background while the user is fiddling in the menus or something.

    Usually you can't serialize GL library stuff (some parts you can, but it is tricky at times).
    So your best best would be to separate the stuff you need to load for the startup, and then package the rest ins sequential decodes.

    Pyglet

    Now, everyone has their religion. Mine is Python and Pyglet.
    With that said, are you sure you really need Pyglet? I mean if you're only going to work with Text, perhaps Pygame or even a UI kit such as TkInter or Kiwy are better options?

    Pyglet is really diverse and can handle complex stuff with ease, but it's also a very heavy and manual tedious work for doing things. Usually this is beneficial if you're going to do crazy stuff that hasn't been done before. For instance, your own gfx engine or custom buttons that can't be done otherwise.

    2d

    You mentioned this is a requirement, I'm not sure why since you're the one with the vision.
    But have a look at another question I answered yesterday about how to improve performance.

    It goes into some detail on how to render stuff efficient and also how to set up a sprite class that works well with what you need to do.

    I've done a 2d/side-scroller/rpg game before and used Pyglet, but only because I feel more at ease with manual tedious work than using others predefined libraries, again..
    If you know why you won't regret the hard work ahead, go for it! But this is all the help we can give you since you haven't actually asked for any code help per se.

    Best of luck to you!