-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsdl3_ttf.go
More file actions
56 lines (50 loc) · 1.5 KB
/
Copy pathsdl3_ttf.go
File metadata and controls
56 lines (50 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
/*
#cgo pkg-config: sdl3-ttf
#include <SDL3_ttf/SDL_ttf.h>
#include <stdlib.h>
*/
import "C"
import (
"fmt"
"unsafe"
)
func TTF3Init() error {
if !C.TTF_Init() {
return fmt.Errorf("TTF_Init: %s", C.GoString(C.SDL_GetError()))
}
return nil
}
func TTF3OpenFont(path string, ptsize float32) (*Font, error) {
cp := C.CString(path)
defer C.free(unsafe.Pointer(cp))
f := C.TTF_OpenFont(cp, C.float(ptsize))
if f == nil {
return nil, fmt.Errorf("TTF_OpenFont(%s): %s", path, C.GoString(C.SDL_GetError()))
}
return &Font{ptr: unsafe.Pointer(f)}, nil
}
func TTF3CloseFont(f *Font) {
if f != nil && f.ptr != nil {
C.TTF_CloseFont((*C.TTF_Font)(f.ptr))
}
}
// TTF3RenderTextBlended renders text to a new surface with blended (anti-aliased) quality.
func TTF3RenderTextBlended(f *Font, text string, color Color) (*Surface, error) {
ct := C.CString(text)
defer C.free(unsafe.Pointer(ct))
cc := C.SDL_Color{r: C.Uint8(color.R), g: C.Uint8(color.G), b: C.Uint8(color.B), a: C.Uint8(color.A)}
surf := C.TTF_RenderText_Blended((*C.TTF_Font)(f.ptr), ct, 0, cc)
if surf == nil {
return nil, fmt.Errorf("TTF_RenderText_Blended: %s", C.GoString(C.SDL_GetError()))
}
return &Surface{ptr: unsafe.Pointer(surf)}, nil
}
// TTF3GetStringSize returns the width and height of the rendered text.
func TTF3GetStringSize(f *Font, text string) (int32, int32) {
ct := C.CString(text)
defer C.free(unsafe.Pointer(ct))
var w, h C.int
C.TTF_GetStringSize((*C.TTF_Font)(f.ptr), ct, 0, &w, &h)
return int32(w), int32(h)
}