Skip to content

Commit

Permalink
Merge pull request #17 from pkulev/master
Browse files Browse the repository at this point in the history
Add weakref set for opened fonts, close them on quit
  • Loading branch information
Failproofshark authored Feb 12, 2020
2 parents 8d3eddb + 7f385b0 commit ff2821a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/src/spec/*.spec linguist-generated=true
18 changes: 15 additions & 3 deletions src/general.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

(require 'sdl2)

(defvar *fonts* (list) "List of weak refs to fonts.")

(defun init ()
"Initialize the sdl trutype font API. Does not require a call to sdl-init prior to calling this function. Returns 0 if succesful -1 otherwise"
(check-rc (ttf-init)))
Expand All @@ -16,16 +18,26 @@
(ttf-was-init))

(defun quit ()
(dolist (pointer *fonts*)
(let ((ttf-font-struct (tg:weak-pointer-value pointer)))
(when ttf-font-struct (close-font ttf-font-struct))))
(ttf-quit))

(defun open-font (path-to-font point-size)
"Open a font specified by the path specifier path-to-font sized to integer point-size (based on 72DPI). Returns a ttf-font struct and null on errors"
(autocollect (ptr)
(check-null (ttf-open-font (namestring path-to-font) point-size))
(ttf-close-font ptr)))
(let ((font (autocollect (ptr)
(check-null (ttf-open-font (namestring path-to-font) point-size))
(ttf-close-font ptr))))
(push (tg:make-weak-pointer font) *fonts*)
font))

(defun close-font (ttf-font-struct)
"Frees the memory used by the ttf-font-struct"
(tg:cancel-finalization ttf-font-struct)
(setf *fonts*
(remove ttf-font-struct *fonts*
:key #'tg:weak-pointer-value
:test #'(lambda (l r)
(cffi:pointer-eq (autowrap:ptr l) (autowrap:ptr r)))))
(ttf-close-font ttf-font-struct)
(autowrap:invalidate ttf-font-struct))

0 comments on commit ff2821a

Please sign in to comment.