forked from AllenDang/cimgui-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
133 lines (109 loc) · 3.36 KB
/
util.go
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package imgui
// #include "util.h"
import "C"
import (
"fmt"
"reflect"
"unsafe"
)
// VertexBufferLayout returns the byte sizes necessary to select fields in a vertex buffer of a DrawList.
func VertexBufferLayout() (entrySize int, posOffset int, uvOffset int, colOffset int) {
var entrySizeArg C.size_t
var posOffsetArg C.size_t
var uvOffsetArg C.size_t
var colOffsetArg C.size_t
C.wrap_GetVertexBufferLayout(&entrySizeArg, &posOffsetArg, &uvOffsetArg, &colOffsetArg)
entrySize = int(entrySizeArg)
posOffset = int(posOffsetArg)
uvOffset = int(uvOffsetArg)
colOffset = int(colOffsetArg)
return
}
// IndexBufferLayout returns the byte size necessary to select fields in an index buffer of DrawList.
func IndexBufferLayout() (entrySize int) {
var entrySizeArg C.size_t
C.wrap_GetIndexBufferLayout(&entrySizeArg)
entrySize = int(entrySizeArg)
return
}
type GlyphRange uintptr
func NewGlyphRange() GlyphRange {
return GlyphRange(unsafe.Pointer(C.wrap_NewGlyphRange()))
}
func (gr GlyphRange) handle() *C.ImVector_ImWchar {
return (*C.ImVector_ImWchar)(unsafe.Pointer(gr))
}
func (gr GlyphRange) Destroy() {
C.wrap_DestroyGlyphRange(gr.handle())
}
func (gr GlyphRange) Data() *Wchar {
return (*Wchar)(C.wrap_GlyphRange_GetData(gr.handle()))
}
func (fa FontAtlas) FontCount() int {
selfArg, selfFin := fa.handle()
defer selfFin()
return int(C.wrap_ImFontAtlas_GetFontCount(selfArg))
}
func (self FontAtlas) TextureDataAsAlpha8() (pixels unsafe.Pointer, width int32, height int32, outBytesPerPixel int32) {
var p *C.uchar
var w C.int
var h C.int
var bp C.int
selfArg, selfFin := self.handle()
defer selfFin()
C.ImFontAtlas_GetTexDataAsAlpha8(selfArg, &p, &w, &h, &bp)
pixels = unsafe.Pointer(p)
width = int32(w)
height = int32(h)
outBytesPerPixel = int32(bp)
return
}
func (self FontAtlas) GetTextureDataAsRGBA32() (pixels unsafe.Pointer, width int32, height int32, outBytesPerPixel int32) {
var p *C.uchar
var w C.int
var h C.int
var bp C.int
selfArg, selfFin := self.handle()
defer selfFin()
C.ImFontAtlas_GetTexDataAsRGBA32(selfArg, &p, &w, &h, &bp)
pixels = unsafe.Pointer(p)
width = int32(w)
height = int32(h)
outBytesPerPixel = int32(bp)
return
}
// Ptr takes a slice or pointer (to a singular scalar value or the first
// element of an array or slice) and returns its GL-compatible address.
//
// For example:
//
// var data []uint8
// ...
// gl.TexImage2D(gl.TEXTURE_2D, ..., gl.UNSIGNED_BYTE, gl.Ptr(&data[0]))
func Ptr(data interface{}) unsafe.Pointer {
if data == nil {
return unsafe.Pointer(nil)
}
var addr unsafe.Pointer
v := reflect.ValueOf(data)
switch v.Type().Kind() {
case reflect.Ptr:
e := v.Elem()
switch e.Kind() {
case
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64:
addr = unsafe.Pointer(e.UnsafeAddr())
default:
panic(fmt.Errorf("unsupported pointer to type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", e.Kind()))
}
case reflect.Uintptr:
addr = unsafe.Pointer(data.(uintptr))
case reflect.Slice:
addr = unsafe.Pointer(v.Index(0).UnsafeAddr())
default:
panic(fmt.Errorf("unsupported type %s; must be a slice or pointer to a singular scalar value or the first element of an array or slice", v.Type()))
}
return addr
}