GameLib is a collection of libraries for creating applications in Cakelisp.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
6.1 KiB

;; Included only for FT_BEGIN_HEADER etc.
;; Normally we are "supposed" to include the headers via the defines, but that would break the build
;; system's ability to find dependent headers to scan.
(c-import "<stdio.h>")
(defmacro freetype-check-result-or-return (result symbol while-doing string)
(tokenize-push output
(unless (= (token-splice result) FT_Err_Ok)
(fprintf stderr "error: encountered error %d while %s\n" (token-splice result)
(token-splice while-doing))
(return 1)))
(return true))
(defun test--freetype (&return int)
(var freetype-library FT_Library)
(var result int
(FT_Init_FreeType (addr freetype-library)))
(freetype-check-result-or-return result "initializing FreeType")
(var typeface FT_Face)
(var face-index int 0)
(var load-font (* (const char)) "data/Fonts/Ubuntu-R.ttf")
(set result (FT_New_Face freetype-library load-font face-index (addr typeface)))
(unless (= result FT_Err_Ok)
(fprintf stderr "error: encountered error %d while loading font %s\n" result load-font)
(return 1))
(set result (FT_Set_Char_Size
typeface ;; handle to face object
0 ;; char_width in 1/64th of points
(* 16 64) ;; char_height in 1/64th of points
300 ;; horizontal device resolution
300)) ;; vertical device resolution
(freetype-check-result-or-return result "setting character size")
(var test-glyph-index int (FT_Get_Char_Index typeface 'g'))
(unless test-glyph-index
(fprintf stderr "error: could not find glyph 'g' in font\n")
(return 1))
(set result (FT_Load_Glyph typeface test-glyph-index FT_LOAD_DEFAULT))
(freetype-check-result-or-return result "loading glyph")
;; One slot for the whole face
(set result (FT_Render_Glyph (path typeface > glyph) FT_RENDER_MODE_NORMAL))
(freetype-check-result-or-return result "rendering glyph")
(var num-rows int (path typeface > glyph > bitmap . rows))
(var num-columns int (path typeface > glyph > bitmap . width))
(each-in-range num-rows row
(each-in-range num-columns column
(var current-pixel (* (unsigned char))
(at (+ column (* row num-columns))
(path typeface > glyph > bitmap . buffer))))
(fprintf stderr "%c" (? (> (deref current-pixel) 128) '#' '.')))
(fprintf stderr "\n"))
(FT_Done_Face typeface)
(FT_Done_FreeType freetype-library)
(return 0))))
;; Building
(defun-comptime build-freetype (manager (& ModuleManager) module (* Module) &return bool)
(comptime-error "Need to add Windows support to FreeType"))
;; TODO: These paths are duplicated elsewhere in this file
(var freetype-build-dir (* (const char)) "cakelisp_cache/FreeTypeBuildDir")
(var freetype-install-dir ([] 1024 char) (array 0))
(SafeSnprintf freetype-install-dir (array-size freetype-install-dir)
"%s/install" freetype-build-dir)
(var freetype-expect-file ([] 1024 char) (array 0))
(SafeSnprintf freetype-expect-file (array-size freetype-expect-file) "%s/lib/libfreetype.a"
;; Add dependency
(call-on push_back (field manager environment additionalStaticLinkObjects) freetype-expect-file)
;; Already built?
;; We could enhance this by checking for modifications, but that's pretty rare
(when (fileExists freetype-expect-file)
(return true))
(Log "FreeType: Building via Configure and Make\n")
(makeDirectory freetype-build-dir)
(makeDirectory freetype-install-dir)
("sh" "" :in-directory "Dependencies/FreeType")
"failed at FreeType autogen step. This requires a sh/bash-style shell to execute.\n")
(return false))
(var configure-output-prefix ([] MAX_PATH_LENGTH char) (array 0))
(scope ;; Output must be absolute directory
(var absolute-output-path (* (const char))
(makeAbsolutePath_Allocated null freetype-install-dir))
(unless absolute-output-path
(Logf "error: failed to make FreeType install directory '%s'\n" freetype-install-dir)
(return false))
(PrintfBuffer configure-output-prefix "--prefix=%s" absolute-output-path)
(free (type-cast absolute-output-path (* void))))
("sh" "../../Dependencies/FreeType/configure"
;; Somewhat arbitrary. If I end up needing these, I'll add them back in
:in-directory freetype-build-dir)
"failed at FreeType configure step. This requires a sh/bash-style shell to execute.\n")
(return false))
("make" "--jobs=8" :in-directory freetype-build-dir)
(Log "failed at FreeType make. This tool requires Makefile support.")
(return false))
("make" "install" :in-directory freetype-build-dir)
(Log "failed at FreeType make install. This tool requires Makefile support.")
(return false))
;; One final to check to ensure everything's good to go
(unless (fileExists freetype-expect-file)
"error: FreeType build sequence completed, but files are not where expected. Is there an issue
with the configuration?\n")
(return false)))
(comptime-error "need to define platform, e.g. (comptime-define-symbol 'Unix), or your platform
has not been implemented yet.")))
(Log "FreeType: Successfully built\n")
(return true))
(add-compile-time-hook-module pre-build build-freetype)