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

(c-import
;; 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.
"freetype/config/ftheader.h"
"freetype/freetype.h")
(comptime-cond
('auto-test
(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 glyph-index-y int (FT_Get_Char_Index typeface 'g'))
(unless glyph-index-y
(fprintf stderr "error: could not find glyph 'g' in font\n")
(return 1))
(set result (FT_Load_Glyph typeface glyph-index-y 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))
(addr
(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
;;
(add-dependency-git-submodule
clone-freetype
"https://gitlab.freedesktop.org/freetype/freetype.git"
"Dependencies/FreeType")
(defun-comptime build-freetype (manager (& ModuleManager) module (* Module) &return bool)
(comptime-cond
('Windows
(comptime-error "Need to add Windows support to FreeType"))
('Unix
;; 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"
freetype-install-dir)
;; 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)
(run-process-sequential-or
("sh" "autogen.sh" :in-directory "Dependencies/FreeType")
(Log
"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))))
(run-process-sequential-or
("sh" "../../Dependencies/FreeType/configure"
;; Somewhat arbitrary. If I end up needing these, I'll add them back in
"--with-zlib=no"
"--with-bzip2=no"
"--with-png=no"
"--with-harfbuzz=no"
"--with-brotli=no"
configure-output-prefix
:in-directory freetype-build-dir)
(Log
"failed at FreeType configure step. This requires a sh/bash-style shell to execute.\n")
(return false))
(run-process-sequential-or
("make" "--jobs=8" :in-directory freetype-build-dir)
(Log "failed at FreeType make. This tool requires Makefile support.")
(return false))
(run-process-sequential-or
("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)
(Log
"error: FreeType build sequence completed, but files are not where expected. Is there an issue
with the configuration?\n")
(return false)))
(true
(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)
(export-and-evaluate
(add-c-search-directory-module
"cakelisp_cache/FreeTypeBuildDir/install/include/freetype2"))