Skip to content

pygame_gui #1

Open
Open
@gabriel-v

Description

@gabriel-v

When trying to install and run pygame_gui, more specifically with this fork i found here, I run into this error:

Traceback (most recent call last):
  File "/data/data/interfatza-pygame-gui/assets/pygame_gui/core/utility.py", line 405, in load
    self.loaded_font = Font(self.location, self.size, resolution=72)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: The FreeType 2 library hasn't been initialized

I can reproduce this in the #debug terminal in the browser:

>>> import pygame.freetype
>>> pygame.freetype.init()
>>> pygame.freetype.get_init()
True
>>> pygame.freetype.Font('pygame_gui_data/FiraCode-Regular.ttf')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
RuntimeError: The FreeType 2 library hasn't been initialized
>>> 

I also see this problem was reached in the "penguins can't fly" demo here, very poetically explained:
https://github.com/pmp-p/pygame-PenguinsCantFly-wasm/blob/4072114d6d625d453e8d9786fcca0f7483d54c35/main.py#L154-L170

After a quick check, the problem seems to be a reality check in this C header that seems to be caused by threading issues - so it seems that async io is interfering with the C library bindings?

I tried moving the import / initialize around, in the top level, in the async main - same outcome.

I also tried wrapping the new pygame.freetype.Font class using the old pygame.font.Font class, but I'm not smart enough to do it correctly, so the text is not showing up.

Questions
  • did you manage to get the fork running?
  • do you think the existing freetype bindings could be used with asyncio in pygbag?
  • do you have any tips for trying to implement the new pygame.freetype.Font using the old pygame.font.Font and trick pygame_gui that way?
  • what if instead of of keeping the freetype2 state in the python module state memory, we could return it to Python, where we would feed it in every time we want to construct a new Font? Or even better, hack it to re-initialize itself instead of crashing....

Thanks!


edit pmp-p/ added more content from further comment :

I also found some theoretical discussions about freetype 2 thread safety and this email thread from 2003 - freetype 2 is not reentrant, and they suggest using one instance per thread.

I guess the next step here is to edit ft_wrap.c and ft_wrap.h to initialize the library and store it in the Font python object instead of the static python module memory

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions