Change LoadFontEx to accept []rune, fixes #136

This commit is contained in:
Milan Nikolic 2022-08-27 17:37:54 +02:00
parent f59ebefc8a
commit 6e090424c5
No known key found for this signature in database
GPG key ID: 9229D0EAA3AA4E75
2 changed files with 17 additions and 11 deletions

View file

@ -3,7 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"github.com/gen2brain/raylib-go/raylib" rl "github.com/gen2brain/raylib-go/raylib"
) )
func main() { func main() {
@ -17,7 +17,7 @@ func main() {
// NOTE: Textures/Fonts MUST be loaded after Window initialization (OpenGL context is required) // NOTE: Textures/Fonts MUST be loaded after Window initialization (OpenGL context is required)
// TTF Font loading with custom generation parameters // TTF Font loading with custom generation parameters
font := rl.LoadFontEx("fonts/KAISG.ttf", 96, nil, 0) font := rl.LoadFontEx("fonts/KAISG.ttf", 96, nil)
// Generate mipmap levels to use trilinear filtering // Generate mipmap levels to use trilinear filtering
// NOTE: On 2D drawing it won't be noticeable, it looks like FILTER_BILINEAR // NOTE: On 2D drawing it won't be noticeable, it looks like FILTER_BILINEAR
@ -30,7 +30,7 @@ func main() {
rl.SetTextureFilter(font.Texture, rl.FilterPoint) rl.SetTextureFilter(font.Texture, rl.FilterPoint)
currentFontFilter := 0 // FilterPoint currentFontFilter := 0 // FilterPoint
count := int32(0) count := 0
droppedFiles := make([]string, 0) droppedFiles := make([]string, 0)
rl.SetTargetFPS(60) rl.SetTargetFPS(60)
@ -38,7 +38,7 @@ func main() {
for !rl.WindowShouldClose() { for !rl.WindowShouldClose() {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
fontSize += rl.GetMouseWheelMove() * 4.0 fontSize += int32(rl.GetMouseWheelMove() * 4.0)
// Choose font texture filter method // Choose font texture filter method
if rl.IsKeyPressed(rl.KeyOne) { if rl.IsKeyPressed(rl.KeyOne) {
@ -63,12 +63,13 @@ func main() {
// Load a dropped TTF file dynamically (at current fontSize) // Load a dropped TTF file dynamically (at current fontSize)
if rl.IsFileDropped() { if rl.IsFileDropped() {
droppedFiles = rl.GetDroppedFiles(&count) droppedFiles = rl.LoadDroppedFiles()
count = len(droppedFiles)
if count == 1 { // Only support one ttf file dropped if count == 1 { // Only support one ttf file dropped
rl.UnloadFont(font) rl.UnloadFont(font)
font = rl.LoadFontEx(droppedFiles[0], fontSize, nil, 0) font = rl.LoadFontEx(droppedFiles[0], fontSize, nil)
rl.ClearDroppedFiles() rl.UnloadDroppedFiles()
} }
} }
@ -104,7 +105,7 @@ func main() {
rl.UnloadFont(font) // Font unloading rl.UnloadFont(font) // Font unloading
rl.ClearDroppedFiles() // Clear internal buffers rl.UnloadDroppedFiles() // Clear internal buffers
rl.CloseWindow() rl.CloseWindow()
} }

View file

@ -37,12 +37,17 @@ func LoadFont(fileName string) Font {
} }
// LoadFontEx - Load Font from file with extended parameters // LoadFontEx - Load Font from file with extended parameters
func LoadFontEx(fileName string, fontSize int32, fontChars *int32, charsCount int32) Font { func LoadFontEx(fileName string, fontSize int32, fontChars []rune) Font {
var cfontChars *C.int
var ccharsCount C.int
cfileName := C.CString(fileName) cfileName := C.CString(fileName)
defer C.free(unsafe.Pointer(cfileName)) defer C.free(unsafe.Pointer(cfileName))
cfontSize := (C.int)(fontSize) cfontSize := (C.int)(fontSize)
cfontChars := (*C.int)(unsafe.Pointer(fontChars)) if fontChars != nil {
ccharsCount := (C.int)(charsCount) cfontChars = (*C.int)(unsafe.Pointer(&fontChars[0]))
ccharsCount = (C.int)(len(fontChars))
}
ret := C.LoadFontEx(cfileName, cfontSize, cfontChars, ccharsCount) ret := C.LoadFontEx(cfileName, cfontSize, cfontChars, ccharsCount)
v := newFontFromPointer(unsafe.Pointer(&ret)) v := newFontFromPointer(unsafe.Pointer(&ret))
return v return v