Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SDL backend #230

Merged
merged 14 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
60 changes: 60 additions & 0 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 Down Expand Up @@ -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 @@ -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 Down
2 changes: 2 additions & 0 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 Down
2 changes: 2 additions & 0 deletions extra_types.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

typedef void (*VoidCallback)();

#ifdef __WIN32
typedef unsigned long long xulong;
typedef long long xlong;
Expand Down
2 changes: 1 addition & 1 deletion glfw_backend.cpp
Original file line number Diff line number Diff line change
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
4 changes: 1 addition & 3 deletions glfw_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,6 @@ const (
GLFWModNumLock = GLFWModifierKey(C.GLFWModNumLock)
)

type voidCallbackFunc func()

var _ Backend[GLFWWindowFlags] = &GLFWBackend{}

type GLFWBackend struct {
Expand Down Expand Up @@ -248,7 +246,7 @@ func (b *GLFWBackend) SetBgColor(color Vec4) {

func (b *GLFWBackend) Run(loop func()) {
b.loop = loop
C.igRunLoop(b.handle(), C.VoidCallback(C.loopCallback), C.VoidCallback(C.beforeRender), C.VoidCallback(C.afterRender), C.VoidCallback(C.beforeDestoryContext))
C.igGLFWRunLoop(b.handle(), C.VoidCallback(C.loopCallback), C.VoidCallback(C.beforeRender), C.VoidCallback(C.afterRender), C.VoidCallback(C.beforeDestoryContext))
}

func (b *GLFWBackend) loopFunc() func() {
Expand Down
5 changes: 2 additions & 3 deletions glfw_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#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 Expand Up @@ -170,14 +171,12 @@ struct GLFWwindow;
struct GLFWmonitor;
struct GLFWimage;

typedef void (*VoidCallback)();

extern void igSetBgColor(ImVec4 color);
extern void igSetTargetFPS(unsigned int fps);
extern int igInitGLFW();
extern GLFWwindow *igCreateGLFWWindow(const char *title, int width, int height,
VoidCallback afterCreateContext);
extern void igRunLoop(GLFWwindow *window, VoidCallback loop, VoidCallback beforeRender, VoidCallback afterRender,
extern void igGLFWRunLoop(GLFWwindow *window, VoidCallback loop, VoidCallback beforeRender, VoidCallback afterRender,
VoidCallback beforeDestroyContext);
extern void igGLFWWindow_GetDisplaySize(GLFWwindow *window, int *width, int *height);
extern void igGLFWWindow_GetContentScale(GLFWwindow *window, float *width, float *height);
Expand Down
7 changes: 7 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ set(GLFW3_INCLUDE_DIR ${GLFW3_PREFIX}/include)
list(APPEND IMGUI_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/../cimgui/imgui/backends/imgui_impl_glfw.cpp)
list(APPEND IMGUI_LIBRARIES glfw)

#SDL
set(SDL2_PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/../thirdparty/SDL)
set(SDL2_INCLUDE_DIR ${SDL2_PREFIX}/include)
list(APPEND IMGUI_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/../cimgui/imgui/backends/imgui_impl_sdl2.cpp)
list(APPEND IMGUI_LIBRARIES sdl2)

#opengl3
list(APPEND IMGUI_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/../cimgui/imgui/backends/imgui_impl_opengl3.cpp)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../cimgui/imgui/examples/libs/gl3w)
Expand Down Expand Up @@ -77,6 +83,7 @@ target_compile_definitions(cimgui PUBLIC IMGUI_IMPL_API=extern\t\"C\"\t)
target_include_directories(cimgui PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../cimgui/)
target_include_directories(cimgui PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../cimgui/imgui)
target_include_directories(cimgui PUBLIC ${GLFW3_INCLUDE_DIR})
target_include_directories(cimgui PUBLIC ${SDL2_INCLUDE_DIR})
set_target_properties(cimgui PROPERTIES PREFIX "")
target_link_libraries(cimgui ${IMGUI_LIBRARIES})

Expand Down
Binary file modified lib/linux/x64/cimgui.a
Binary file not shown.
Binary file added lib/linux/x64/libSDL2.a
Binary file not shown.
Binary file modified lib/macos/arm64/cimgui.a
Binary file not shown.
Binary file added lib/macos/arm64/libSDL2.a
Binary file not shown.
Binary file modified lib/macos/arm64/libglfw3.a
Binary file not shown.
Binary file modified lib/macos/x64/cimgui.a
Binary file not shown.
Binary file added lib/macos/x64/libSDL2.a
Binary file not shown.
Binary file modified lib/macos/x64/libglfw3.a
Binary file not shown.
Binary file modified lib/windows/x64/cimgui.a
Binary file not shown.
Binary file added lib/windows/x64/libSDL2.a
Binary file not shown.
Loading
Loading