Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
gucio321 committed Nov 23, 2023
2 parents 9293151 + e9c342c commit ff2e3e2
Show file tree
Hide file tree
Showing 2,108 changed files with 631,198 additions and 1,012 deletions.
66 changes: 63 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jobs:
sudo apt-get -y update
sudo apt install xorg-dev
# for GLFW
- name: configure glfw
run: cmake -S . -Bbuild
working-directory: ./thirdparty/glfw
Expand All @@ -32,6 +33,20 @@ jobs:
run: |
cp -f ./thirdparty/glfw/build/src/libglfw3.a ./lib/linux/x64
# for SDL2
- name: configure sdl
run: cmake -S . -Bbuild
working-directory: ./thirdparty/SDL

- name: make sdl
run: make -j 4
working-directory: ./thirdparty/SDL/build

- name: copy sdl to lib
run: |
cp -f ./thirdparty/SDL/build//libSDL2.a ./lib/linux/x64
# for cimgui
- name: configure cimgui
run: cmake -Bbuild
working-directory: ./lib
Expand All @@ -46,7 +61,7 @@ jobs:
cp -f ./lib/build/cimgui.a ./lib/linux/x64/
- run: git pull --rebase --autostash
- uses: stefanzweifel/git-auto-commit-action@v4
- uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: Update linux lib from ci
build-macos:
Expand All @@ -61,6 +76,7 @@ jobs:
steps:
- uses: actions/checkout@main

# glfw
- name: configure glfw for x64
run: cmake -S . -Bbuild -DCMAKE_OSX_ARCHITECTURES=x86_64
working-directory: ./thirdparty/glfw
Expand Down Expand Up @@ -89,6 +105,36 @@ jobs:
run: |
cp -f ./thirdparty/glfw/build/src/libglfw3.a ./lib/macos/arm64
# sdl
- name: configure sdl for x64
run: cmake -S . -Bbuild -DCMAKE_OSX_ARCHITECTURES=x86_64
working-directory: ./thirdparty/SDL

- name: make sdl x84
run: make -j 4
working-directory: ./thirdparty/SDL/build

- name: copy sdl to lib
run: |
cp -f ./thirdparty/SDL/build/libSDL2.a ./lib/macos/x64
- name: clean sdl
run: cmake --build ./build --target clean
working-directory: ./thirdparty/SDL

- name: configure sdl for arm64
run: cmake -S . -Bbuild -DCMAKE_OSX_ARCHITECTURES=arm64
working-directory: ./thirdparty/SDL

- name: make sdl arm64
run: make -j 4
working-directory: ./thirdparty/SDL/build

- name: copy sdl to lib
run: |
cp -f ./thirdparty/SDL/build/libSDL2.a ./lib/macos/arm64
# cimgui
- name: configure cimgui for x64
run: cmake -Bbuild -DCMAKE_OSX_ARCHITECTURES=x86_64
working-directory: ./lib
Expand Down Expand Up @@ -120,7 +166,7 @@ jobs:
cp -f ./lib/build/cimgui.a ./lib/macos/arm64/
- run: git pull --rebase --autostash
- uses: stefanzweifel/git-auto-commit-action@v4
- uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: Update macos lib from ci
build-windows-x64:
Expand Down Expand Up @@ -151,6 +197,7 @@ jobs:
cmake:p
ninja:p
# glfw
- name: configure glfw
run: cmake -G Ninja -S . -B build -DCMAKE_BUILD_TYPE=Release
working-directory: ./thirdparty/glfw
Expand All @@ -162,6 +209,19 @@ jobs:
- name: copy glfw to lib
run: cp -f ./thirdparty/glfw/build/src/libglfw3.a ./lib/windows/x64

# sdl
- name: configure sdl
run: cmake -G Ninja -S . -B build -DCMAKE_BUILD_TYPE=Release
working-directory: ./thirdparty/SDL

- name: make sdl
run: cmake --build build -j 4
working-directory: ./thirdparty/SDL

- name: copy sdl to lib
run: cp -f ./thirdparty/SDL/build/libSDL2.a ./lib/windows/x64

# cimgui
- name: configure cimgui
run: cmake -G Ninja -B build
working-directory: .\lib
Expand All @@ -174,6 +234,6 @@ jobs:
run: cp -f ./lib/build/cimgui.a ./lib/windows/x64/

- run: git pull --rebase --autostash
- uses: stefanzweifel/git-auto-commit-action@v4
- uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: Update windows lib from ci
47 changes: 25 additions & 22 deletions backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"unsafe"
)

type VoidCallbackFunc func()

var currentBackend backendCExpose

// TODO: Maybe we should get rid of it?
Expand All @@ -23,7 +25,7 @@ var textureManager TextureManager
//export loopCallback
func loopCallback() {
if currentBackend != nil {
if f := currentBackend.loopFunc(); f != nil {
if f := currentBackend.LoopFunc(); f != nil {
f()
}
}
Expand All @@ -32,7 +34,7 @@ func loopCallback() {
//export beforeRender
func beforeRender() {
if currentBackend != nil {
if f := currentBackend.beforeRenderHook(); f != nil {
if f := currentBackend.BeforeRenderHook(); f != nil {
f()
}
}
Expand All @@ -41,7 +43,7 @@ func beforeRender() {
//export afterRender
func afterRender() {
if currentBackend != nil {
if f := currentBackend.afterRenderHook(); f != nil {
if f := currentBackend.AfterRenderHook(); f != nil {
f()
}
}
Expand All @@ -50,7 +52,7 @@ func afterRender() {
//export afterCreateContext
func afterCreateContext() {
if currentBackend != nil {
if f := currentBackend.afterCreateHook(); f != nil {
if f := currentBackend.AfterCreateHook(); f != nil {
f()
}
}
Expand All @@ -59,7 +61,7 @@ func afterCreateContext() {
//export beforeDestoryContext
func beforeDestoryContext() {
if currentBackend != nil {
if f := currentBackend.beforeDestroyHook(); f != nil {
if f := currentBackend.BeforeDestroyHook(); f != nil {
f()
}
}
Expand All @@ -68,7 +70,7 @@ func beforeDestoryContext() {
//export keyCallback
func keyCallback(_ unsafe.Pointer, key, scanCode, action, mods C.int) {
if currentBackend != nil {
if f := currentBackend.keyCallback(); f != nil {
if f := currentBackend.KeyCallback(); f != nil {
f(int(key), int(scanCode), int(action), int(mods))
}
}
Expand All @@ -77,21 +79,23 @@ func keyCallback(_ unsafe.Pointer, key, scanCode, action, mods C.int) {
//export sizeCallback
func sizeCallback(_ unsafe.Pointer, w, h C.int) {
if currentBackend != nil {
if f := currentBackend.sizeCallback(); f != nil {
if f := currentBackend.SizeCallback(); f != nil {
f(int(w), int(h))
}
}
}

type DropCallback func([]string)
type KeyCallback func(key, scanCode, action, mods int)
type SizeChangeCallback func(w, h int)
type (
DropCallback func([]string)
KeyCallback func(key, scanCode, action, mods int)
SizeChangeCallback func(w, h int)
)

type WindowCloseCallback[BackendFlagsT ~int] func(b Backend[BackendFlagsT])

//export closeCallback
func closeCallback(wnd unsafe.Pointer) {
currentBackend.closeCallback()(wnd)
currentBackend.CloseCallback()(wnd)
}

//export dropCallback
Expand All @@ -103,7 +107,7 @@ func dropCallback(wnd unsafe.Pointer, count C.int, names **C.char) {
namesSlice[i] = C.GoString(*p)
}

currentBackend.dropCallback()(namesSlice)
currentBackend.DropCallback()(namesSlice)
}

// Backend is a special interface that implements all methods required
Expand Down Expand Up @@ -155,7 +159,6 @@ type TextureManager interface {
}

type backendCExpose interface {

// for C callbacks
// What happens here is a bit tricky:
// - user sets these callbacks via Set* methods of the backend
Expand All @@ -166,15 +169,15 @@ type backendCExpose interface {
// - backend implementation uses C references to Go callbacks to share them (again ;-) )
// into backend code.
// As you can see this is all to convert Go callback int C callback
afterCreateHook() func()
beforeRenderHook() func()
loopFunc() func()
afterRenderHook() func()
beforeDestroyHook() func()
dropCallback() DropCallback
closeCallback() func(window unsafe.Pointer)
keyCallback() KeyCallback
sizeCallback() SizeChangeCallback
AfterCreateHook() func()
BeforeRenderHook() func()
LoopFunc() func()
AfterRenderHook() func()
BeforeDestroyHook() func()
DropCallback() DropCallback
CloseCallback() func(window unsafe.Pointer)
KeyCallback() KeyCallback
SizeCallback() SizeChangeCallback
}

func CreateBackend[BackendFlagsT ~int](backend Backend[BackendFlagsT]) Backend[BackendFlagsT] {
Expand Down
10 changes: 10 additions & 0 deletions backend/cimgui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package backend

// #cgo CPPFLAGS: -DCIMGUI_DEFINE_ENUMS_AND_STRUCTS
// #cgo CXXFLAGS: --std=c++11
// #cgo amd64,linux LDFLAGS: ${SRCDIR}/../lib/linux/x64/cimgui.a
// #cgo linux CXXFLAGS: -Wno-changes-meaning -fpermissive
// #cgo amd64,windows LDFLAGS: -L${SRCDIR}/../lib/windows/x64 -l:cimgui.a
// #cgo amd64,darwin LDFLAGS: ${SRCDIR}/../lib/macos/x64/cimgui.a
// #cgo arm64,darwin LDFLAGS: ${SRCDIR}/../lib/macos/arm64/cimgui.a
import "C"
6 changes: 3 additions & 3 deletions glfw_backend.cpp → backend/glfw_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#define CIMGUI_USE_OPENGL3

#include "glfw_backend.h"
#include "cimgui/cimgui.h"
#include "cimgui/cimgui_impl.h"
#include "../cimgui/cimgui.h"
#include "../cimgui/cimgui_impl.h"
#include "thirdparty/glfw/include/GLFW/glfw3.h" // Will drag system OpenGL headers
#include <cstdlib>

Expand Down Expand Up @@ -161,7 +161,7 @@ void glfw_render(GLFWwindow *window, VoidCallback renderLoop) {
glfwSwapBuffers(window);
}

void igRunLoop(GLFWwindow *window, VoidCallback loop, VoidCallback beforeRender, VoidCallback afterRender,
void igGLFWRunLoop(GLFWwindow *window, VoidCallback loop, VoidCallback beforeRender, VoidCallback afterRender,
VoidCallback beforeDestroyContext) {
glfwMakeContextCurrent(window);
ImGuiIO *io = igGetIO();
Expand Down
Loading

0 comments on commit ff2e3e2

Please sign in to comment.