Skip to content

Commit

Permalink
Merge #489
Browse files Browse the repository at this point in the history
489: Fixes Text Rendering using 1 Font object r=astronouth7303 a=pathunstrom

Needs to use the mutex around FT_Libary.

Closes #488 

Co-authored-by: Jamie Bliss <jamie@ivyleav.es>

Co-authored-by: Piper Thunstrom <pathunstrom@gmail.com>
  • Loading branch information
bors[bot] and pathunstrom authored Jun 6, 2020
2 parents 7554c2e + 362a71c commit 4351517
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
14 changes: 9 additions & 5 deletions ppb/systems/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
# used around FT_New_Face and FT_Done_Face.
#
# I assume this translates to TTF_OpenFont* and TTF_CloseFont
# SDL_ttf manages a single global FT_Library, so we need to use the lock
# for threaded calls into it, like in Asset._background.

_freetype_lock = threading.RLock()


Expand Down Expand Up @@ -122,11 +125,12 @@ def __repr__(self):
return f"<{type(self).__name__} txt={self.txt!r} font={self.font!r} color={self.color!r}{' loaded' if self.is_loaded() else ''} at 0x{id(self):x}>"

def _background(self):
return ttf_call(
TTF_RenderUTF8_Blended, self.font.load(), self.txt.encode('utf-8'),
SDL_Color(*self.color),
_check_error=lambda rv: not rv
)
with _freetype_lock:
return ttf_call(
TTF_RenderUTF8_Blended, self.font.load(), self.txt.encode('utf-8'),
SDL_Color(*self.color),
_check_error=lambda rv: not rv
)

def free(self, object, _SDL_FreeSurface=SDL_FreeSurface):
# ^^^ is a way to keep required functions during interpreter cleanup
Expand Down
13 changes: 13 additions & 0 deletions viztests/text_shared_font.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import ppb

font = ppb.Font("resources/ubuntu_font/Ubuntu-R.ttf", size=72)
my_first_text = ppb.Text("My first text", font=font, color=(255, 255, 255))
my_second_text = ppb.Text("My second text", font=font, color=(255, 255, 255))


def setup(scene):
scene.add(ppb.Sprite(image=my_first_text))
scene.add(ppb.Sprite(image=my_second_text, position=ppb.Vector(0, -2)))


ppb.run(setup)

0 comments on commit 4351517

Please sign in to comment.