forked from AllenDang/cimgui-go
/
backend.go
161 lines (128 loc) · 4.18 KB
/
backend.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//go:build !exclude_cimgui_glfw
// +build !exclude_cimgui_glfw
package imgui
// #cgo amd64,linux LDFLAGS: ${SRCDIR}/lib/linux/x64/libglfw3.a -ldl -lGL -lX11
// #cgo amd64,windows LDFLAGS: -L${SRCDIR}/lib/windows/x64 -l:libglfw3.a -lgdi32 -lopengl32 -limm32
// #cgo darwin LDFLAGS: -framework Cocoa -framework IOKit -framework CoreVideo
// #cgo amd64,darwin LDFLAGS: ${SRCDIR}/lib/macos/x64/libglfw3.a
// #cgo arm64,darwin LDFLAGS: ${SRCDIR}/lib/macos/arm64/libglfw3.a
// #cgo !gles2,darwin LDFLAGS: -framework OpenGL
// #cgo gles2,darwin LDFLAGS: -lGLESv2
// #cgo CPPFLAGS: -DCIMGUI_GO_USE_GLFW
// extern void glfwWindowLoopCallback();
// extern void glfwBeforeRender();
// extern void glfwAfterRender();
// extern void glfwAfterCreateContext();
// extern void glfwBeforeDestoryContext();
// #include <stdint.h>
// #include "backend.h"
import "C"
import (
"image"
"unsafe"
)
type GLFWWindowFlags int
const (
GLFWWindowFlagsNone GLFWWindowFlags = GLFWWindowFlags(C.GLFWWindowFlagsNone)
GLFWWindowFlagsNotResizable GLFWWindowFlags = GLFWWindowFlags(C.GLFWWindowFlagsNotResizable)
GLFWWindowFlagsMaximized GLFWWindowFlags = GLFWWindowFlags(C.GLFWWindowFlagsMaximized)
GLFWWindowFlagsFloating GLFWWindowFlags = GLFWWindowFlags(C.GLFWWindowFlagsFloating)
GLFWWindowFlagsFrameless GLFWWindowFlags = GLFWWindowFlags(C.GLFWWindowFlagsFrameless)
GLFWWindowFlagsTransparent GLFWWindowFlags = GLFWWindowFlags(C.GLFWWindowFlagsTransparent)
)
type voidCallbackFunc func()
var (
afterCreateContext voidCallbackFunc
loopFunc voidCallbackFunc
beforeRender voidCallbackFunc
afterRender voidCallbackFunc
beforeDestoryContext voidCallbackFunc
)
type GLFWwindow uintptr
func (w GLFWwindow) handle() *C.GLFWwindow {
return (*C.GLFWwindow)(unsafe.Pointer(w))
}
func SetAfterCreateContextHook(hook func()) {
afterCreateContext = hook
}
func SetBeforeDestroyContextHook(hook func()) {
beforeDestoryContext = hook
}
func SetBeforeRenderHook(hook func()) {
beforeRender = hook
}
func SetAfterRenderHook(hook func()) {
afterRender = hook
}
func SetBgColor(color Vec4) {
C.igSetBgColor(color.toC())
}
func (w GLFWwindow) Run(loop func()) {
loopFunc = loop
C.igRunLoop(w.handle(), C.VoidCallback(C.glfwWindowLoopCallback), C.VoidCallback(C.glfwBeforeRender), C.VoidCallback(C.glfwAfterRender), C.VoidCallback(C.glfwBeforeDestoryContext))
}
func (w GLFWwindow) DisplaySize() (width int32, height int32) {
widthArg, widthFin := wrapNumberPtr[C.int, int32](&width)
defer widthFin()
heightArg, heightFin := wrapNumberPtr[C.int, int32](&height)
defer heightFin()
C.igGLFWWindow_GetDisplaySize(w.handle(), widthArg, heightArg)
return
}
func (w GLFWwindow) SetShouldClose(value bool) {
C.igGLFWWindow_SetShouldClose(w.handle(), C.int(castBool(value)))
}
//export glfwWindowLoopCallback
func glfwWindowLoopCallback() {
if loopFunc != nil {
loopFunc()
}
}
//export glfwBeforeRender
func glfwBeforeRender() {
if beforeRender != nil {
beforeRender()
}
}
//export glfwAfterRender
func glfwAfterRender() {
if afterRender != nil {
afterRender()
}
}
//export glfwAfterCreateContext
func glfwAfterCreateContext() {
if afterCreateContext != nil {
afterCreateContext()
}
}
//export glfwBeforeDestoryContext
func glfwBeforeDestoryContext() {
if beforeDestoryContext != nil {
beforeDestoryContext()
}
}
func CreateGlfwWindow(title string, width, height int, flags GLFWWindowFlags) GLFWwindow {
titleArg, titleFin := wrapString(title)
defer titleFin()
window := GLFWwindow(unsafe.Pointer(C.igCreateGLFWWindow(titleArg, C.int(width), C.int(height), C.GLFWWindowFlags(flags), C.VoidCallback(C.glfwAfterCreateContext))))
if window == 0 {
panic("Failed to create GLFW window")
}
return window
}
func SetTargetFPS(fps uint) {
C.igSetTargetFPS(C.uint(fps))
}
func Refresh() {
C.igRefresh()
}
func CreateTexture(pixels unsafe.Pointer, width, height int) TextureID {
return TextureID(C.igCreateTexture((*C.uchar)(pixels), C.int(width), C.int(height)))
}
func CreateTextureRgba(img *image.RGBA, width, height int) TextureID {
return TextureID(C.igCreateTexture((*C.uchar)(&(img.Pix[0])), C.int(width), C.int(height)))
}
func DeleteTexture(id TextureID) {
C.igDeleteTexture(C.ImTextureID(id))
}