Skip to content

Commit

Permalink
Merge pull request #307 from gucio321/move-all-backend-stuff
Browse files Browse the repository at this point in the history
move backend.go and XXX backend to backend/ package
  • Loading branch information
gucio321 authored Sep 16, 2024
2 parents 7f124dc + 654d11e commit 17cffc5
Show file tree
Hide file tree
Showing 35 changed files with 178 additions and 128 deletions.
24 changes: 10 additions & 14 deletions backend.go → backend/backend.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package imgui
package backend

// extern void loopCallback();
// extern void beforeRender();
Expand All @@ -8,16 +8,18 @@ package imgui
// extern void dropCallback(void*, int, char**);
// extern void keyCallback(void*, int, int, int, int);
// extern void sizeCallback(void*, int, int);
// #include "extra_types.h"
// #include "cimgui_wrapper.h"
// #include "cimgui_typedefs.h"
// #include "../extra_types.h"
// #include "../cimgui_wrapper.h"
// #include "../cimgui_typedefs.h"
import "C"

import (
"errors"
"fmt"
"image"
"unsafe"

imgui "github.com/AllenDang/cimgui-go"
)

type voidCallbackFunc func()
Expand Down Expand Up @@ -143,7 +145,7 @@ type Backend[BackendFlagsT ~int] interface {
SetBeforeRenderHook(func())
SetAfterRenderHook(func())

SetBgColor(color Vec4)
SetBgColor(color imgui.Vec4)
Run(func())
Refresh()

Expand Down Expand Up @@ -180,9 +182,9 @@ type Backend[BackendFlagsT ~int] interface {
// Why I separate it? Current impl of local texture.go needs to store this somewhere, and I don't want
// to make Texture relate on BackendFlagsT.
type TextureManager interface {
CreateTexture(pixels unsafe.Pointer, width, Height int) TextureID
CreateTextureRgba(img *image.RGBA, width, height int) TextureID
DeleteTexture(id TextureID)
CreateTexture(pixels unsafe.Pointer, width, Height int) imgui.TextureID
CreateTextureRgba(img *image.RGBA, width, height int) imgui.TextureID
DeleteTexture(id imgui.TextureID)
}

type backendCExpose interface {
Expand Down Expand Up @@ -228,9 +230,3 @@ func CreateBackend[BackendFlagsT ~int](backend Backend[BackendFlagsT]) (sameBack
textureManager = backend
return backend, err
}

// Export some methods that are necessary for externally packaged backends

func (i Vec4) ToC() C.ImVec4 {
return i.toC()
}
2 changes: 1 addition & 1 deletion sdlbackend/cflags.go → backend/cflags.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sdlbackend
package backend

// #cgo CPPFLAGS: -DCIMGUI_DEFINE_ENUMS_AND_STRUCTS
// #cgo CXXFLAGS: --std=c++11
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"unsafe"

imgui "github.com/AllenDang/cimgui-go"
"github.com/AllenDang/cimgui-go/backend"
"github.com/hajimehoshi/ebiten/v2"
)

Expand Down Expand Up @@ -88,16 +89,16 @@ func (e *EbitenBackend) SetTargetFPS(fps uint) {
}

// TODO: Not implemented
func (b *EbitenBackend) SetDropCallback(imgui.DropCallback) {
func (b *EbitenBackend) SetDropCallback(backend.DropCallback) {
panic("SetDropCallback is not implemented for Ebiten backend yet.")
}

func (b *EbitenBackend) SetCloseCallback(cb imgui.WindowCloseCallback[EbitenBackendFlags]) {
func (b *EbitenBackend) SetCloseCallback(cb backend.WindowCloseCallback[EbitenBackendFlags]) {
b.closeCb = cb
}

func (b *EbitenBackend) SetKeyCallback(imgui.KeyCallback) {} // TODO
func (b *EbitenBackend) SetSizeChangeCallback(imgui.SizeChangeCallback) {} // TODO
func (b *EbitenBackend) SetKeyCallback(backend.KeyCallback) {} // TODO
func (b *EbitenBackend) SetSizeChangeCallback(backend.SizeChangeCallback) {} // TODO

func (b *EbitenBackend) SetWindowFlags(flag EbitenBackendFlags, value int) {
switch flag {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"runtime"

imgui "github.com/AllenDang/cimgui-go"
"github.com/AllenDang/cimgui-go/backend"
"github.com/hajimehoshi/ebiten/v2"
)

type getCursorFn func() (x, y float32)

var (
_ imgui.Backend[EbitenBackendFlags] = &EbitenBackend{}
_ ebiten.Game = &EbitenBackend{}
_ backend.Backend[EbitenBackendFlags] = &EbitenBackend{}
_ ebiten.Game = &EbitenBackend{}
)

// EbitenBackend implements imgui.Backend and ebiten.Game.
Expand All @@ -26,7 +27,7 @@ type EbitenBackend struct {
afterRender,
beforeDestroy,
loop func()
closeCb imgui.WindowCloseCallback[EbitenBackendFlags]
closeCb backend.WindowCloseCallback[EbitenBackendFlags]

// ebiten stuff
filter ebiten.Filter
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"unsafe"

imgui "github.com/AllenDang/cimgui-go"
"github.com/AllenDang/cimgui-go/ebiten-backend/internal/native"
"github.com/AllenDang/cimgui-go/backend/ebiten-backend/internal/native"
"github.com/hajimehoshi/ebiten/v2"
)

Expand Down
File renamed without changes.
10 changes: 10 additions & 0 deletions backend/glfwbackend/cflags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package glfwbackend

// #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 -Wno-invalid-conversion -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"
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#define CIMGUI_USE_OPENGL3

#include "glfw_backend.h"
#include "../cimgui/cimgui.h"
#include "../cimgui/cimgui_impl.h"
#include "../thirdparty/glfw/include/GLFW/glfw3.h" // Will drag system OpenGL headers
#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
35 changes: 18 additions & 17 deletions glfwbackend/glfw_backend.go → backend/glfwbackend/glfw_backend.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package glfwbackend

// #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 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 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
Expand All @@ -19,6 +19,7 @@ import (
"unsafe"

imgui "github.com/AllenDang/cimgui-go"
"github.com/AllenDang/cimgui-go/backend"
)

type voidCallbackFunc func()
Expand Down Expand Up @@ -192,18 +193,18 @@ const (
GLFWModNumLock = GLFWModifierKey(C.GLFWModNumLock)
)

var _ imgui.Backend[GLFWWindowFlags] = &GLFWBackend{}
var _ backend.Backend[GLFWWindowFlags] = &GLFWBackend{}

type GLFWBackend struct {
afterCreateContext voidCallbackFunc
loop voidCallbackFunc
beforeRender voidCallbackFunc
afterRender voidCallbackFunc
beforeDestoryContext voidCallbackFunc
dropCB imgui.DropCallback
dropCB backend.DropCallback
closeCB func(pointer unsafe.Pointer)
keyCb imgui.KeyCallback
sizeCb imgui.SizeChangeCallback
keyCb backend.KeyCallback
sizeCb backend.SizeChangeCallback
window uintptr
}

Expand Down Expand Up @@ -259,14 +260,14 @@ func (b *GLFWBackend) SetBgColor(color imgui.Vec4) {

func (b *GLFWBackend) Run(loop func()) {
b.loop = loop
C.igGLFWRunLoop(b.handle(), C.VoidCallback(imgui.LoopCallback()), C.VoidCallback(imgui.BeforeRender()), C.VoidCallback(imgui.AfterRender()), C.VoidCallback(imgui.BeforeDestroyContext()))
C.igGLFWRunLoop(b.handle(), C.VoidCallback(backend.LoopCallback()), C.VoidCallback(backend.BeforeRender()), C.VoidCallback(backend.AfterRender()), C.VoidCallback(backend.BeforeDestroyContext()))
}

func (b *GLFWBackend) LoopFunc() func() {
return b.loop
}

func (b *GLFWBackend) DropCallback() imgui.DropCallback {
func (b *GLFWBackend) DropCallback() backend.DropCallback {
return b.dropCB
}

Expand Down Expand Up @@ -344,7 +345,7 @@ func (b *GLFWBackend) CreateWindow(title string, width, height int) {
(*C.char)(titleArg),
C.int(width),
C.int(height),
C.VoidCallback(imgui.AfterCreateContext()),
C.VoidCallback(backend.AfterCreateContext()),
)))
if b.window == 0 {
panic("Failed to create GLFW window")
Expand Down Expand Up @@ -375,12 +376,12 @@ func (b *GLFWBackend) DeleteTexture(id imgui.TextureID) {

// SetDropCallback sets the drop callback which is called when an object
// is dropped over the window.
func (b *GLFWBackend) SetDropCallback(cbfun imgui.DropCallback) {
func (b *GLFWBackend) SetDropCallback(cbfun backend.DropCallback) {
b.dropCB = cbfun
C.igGLFWWindow_SetDropCallbackCB(b.handle())
}

func (b *GLFWBackend) SetCloseCallback(cbfun imgui.WindowCloseCallback[GLFWWindowFlags]) {
func (b *GLFWBackend) SetCloseCallback(cbfun backend.WindowCloseCallback[GLFWWindowFlags]) {
b.closeCB = func(_ unsafe.Pointer) {
cbfun(b)
}
Expand Down Expand Up @@ -442,24 +443,24 @@ func (b *GLFWBackend) SetIcons(images ...image.Image) {
}
}

func (b *GLFWBackend) SetKeyCallback(cbfun imgui.KeyCallback) {
func (b *GLFWBackend) SetKeyCallback(cbfun backend.KeyCallback) {
b.keyCb = func(k, s, a, m int) {
C.iggImplGlfw_KeyCallback(b.handle(), C.int(k), C.int(s), C.int(a), C.int(m))
cbfun(k, s, a, m)
}
C.igGLFWWindow_SetKeyCallback(b.handle())
}

func (b *GLFWBackend) KeyCallback() imgui.KeyCallback {
func (b *GLFWBackend) KeyCallback() backend.KeyCallback {
return b.keyCb
}

func (b *GLFWBackend) SetSizeChangeCallback(cbfun imgui.SizeChangeCallback) {
func (b *GLFWBackend) SetSizeChangeCallback(cbfun backend.SizeChangeCallback) {
b.sizeCb = cbfun
C.igGLFWWindow_SetSizeCallback(b.handle())
}

func (b *GLFWBackend) SizeCallback() imgui.SizeChangeCallback {
func (b *GLFWBackend) SizeCallback() backend.SizeChangeCallback {
return b.sizeCb
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include "../cimgui_wrapper.h"
#include "../thirdparty/glfw/include/GLFW/glfw3.h" // Will drag system OpenGL headers
#include "../extra_types.h"
#include "../../cimgui_wrapper.h"
#include "../../thirdparty/glfw/include/GLFW/glfw3.h" // Will drag system OpenGL headers
#include "../../extra_types.h"

#ifdef __cplusplus
extern "C" {
Expand Down
10 changes: 10 additions & 0 deletions backend/sdlbackend/cflags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package sdlbackend

// #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 -Wno-invalid-conversion -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"
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@
// - Introduction, links and more at the top of imgui.cpp

#include "sdl_backend.h"
#include "../cimgui/cimgui.h"
#include "../cimgui/cimgui_impl.h"
#include "../../cimgui/cimgui.h"
#include "../../cimgui/cimgui_impl.h"

#include <cstdlib>
#include <stdio.h>
#include "../thirdparty/SDL/include/SDL.h"
#include "../../thirdparty/SDL/include/SDL.h"
#if defined(IMGUI_IMPL_OPENGL_ES2)
#include "../thirdparty/SDL/include/SDL_opengles2.h"
#include "../../thirdparty/SDL/include/SDL_opengles2.h"
#else
#include "../thirdparty/SDL/include/SDL_opengl.h"
#include "../../thirdparty/SDL/include/SDL_opengl.h"
#endif

// This example can also compile and run with Emscripten! See 'Makefile.emscripten' for details.
#ifdef __EMSCRIPTEN__
#include "../libs/emscripten/emscripten_mainloop_stub.h"
#include "../../libs/emscripten/emscripten_mainloop_stub.h"
#endif

ImVec4 sdl_clear_color = *ImVec4_ImVec4_Float(0.45, 0.55, 0.6, 1.0);
Expand Down
Loading

0 comments on commit 17cffc5

Please sign in to comment.