mirror of
https://github.com/cimgui/cimgui.git
synced 2025-08-11 12:18:30 +01:00
add backend_test for sdl2 opengl3
This commit is contained in:
80
backend_test/CMakeLists.txt
Normal file
80
backend_test/CMakeLists.txt
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
Project(cimgui_sdl)
|
||||||
|
cmake_minimum_required(VERSION 2.8)
|
||||||
|
if(WIN32) #to mingw work as all the others
|
||||||
|
set(CMAKE_SHARED_LIBRARY_PREFIX "")
|
||||||
|
endif(WIN32)
|
||||||
|
#general settings
|
||||||
|
include_directories(../imgui)
|
||||||
|
add_definitions("-DIMGUI_DISABLE_OBSOLETE_FUNCTIONS=1")
|
||||||
|
|
||||||
|
include_directories(../)
|
||||||
|
set(IMGUI_SOURCES ../cimgui.cpp
|
||||||
|
../imgui/imgui.cpp
|
||||||
|
../imgui/imgui_draw.cpp
|
||||||
|
../imgui/imgui_demo.cpp
|
||||||
|
../imgui/imgui_widgets.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
set(IMGUI_SOURCES_sdl)
|
||||||
|
set(IMGUI_LIBRARIES )
|
||||||
|
|
||||||
|
if (WIN32)
|
||||||
|
add_definitions("-DIMGUI_IMPL_API=extern \"C\" __declspec\(dllexport\)")
|
||||||
|
else(WIN32)
|
||||||
|
add_definitions("-DIMGUI_IMPL_API=extern \"C\" ")
|
||||||
|
endif(WIN32)
|
||||||
|
|
||||||
|
add_compile_definitions("IMGUI_IMPL_OPENGL_LOADER_GL3W")
|
||||||
|
|
||||||
|
#optional adding freetype
|
||||||
|
option(IMGUI_FREETYPE "add Freetype2" OFF)
|
||||||
|
|
||||||
|
if(IMGUI_FREETYPE)
|
||||||
|
FIND_PACKAGE(freetype REQUIRED PATHS ${FREETYPE_PATH})
|
||||||
|
list(APPEND IMGUI_LIBRARIES freetype)
|
||||||
|
list(APPEND IMGUI_SOURCES ../imgui/misc/freetype/imgui_freetype.cpp)
|
||||||
|
add_definitions("-DCIMGUI_FREETYPE=1")
|
||||||
|
endif(IMGUI_FREETYPE)
|
||||||
|
|
||||||
|
#opengl3
|
||||||
|
list(APPEND IMGUI_SOURCES ../imgui/examples/imgui_impl_opengl3.cpp ../imgui/examples/libs/gl3w/GL/gl3w.c)
|
||||||
|
list(APPEND IMGUI_SOURCES ./cimgui_extras.cpp)
|
||||||
|
include_directories(../imgui/examples/libs/gl3w)
|
||||||
|
if(WIN32)
|
||||||
|
list(APPEND IMGUI_LIBRARIES opengl32)
|
||||||
|
else(WIN32)#Unix
|
||||||
|
list(APPEND IMGUI_LIBRARIES GL)
|
||||||
|
endif(WIN32)
|
||||||
|
|
||||||
|
|
||||||
|
#sdl2
|
||||||
|
list(APPEND IMGUI_SOURCES ../imgui/examples/imgui_impl_sdl.cpp)
|
||||||
|
if(DEFINED SDL_PATH)
|
||||||
|
message(STATUS "SDL_PATH defined as " ${SDL_PATH})
|
||||||
|
FIND_PACKAGE(SDL2 REQUIRED PATHS ${SDL_PATH})
|
||||||
|
get_target_property(SDL_INCLUDE SDL2::SDL2 INTERFACE_INCLUDE_DIRECTORIES)
|
||||||
|
message(STATUS "sdlinclude is " ${SDL_INCLUDE})
|
||||||
|
if ("${SDL_INCLUDE}" STREQUAL "") #if not found latest SDL2 cmake config use older
|
||||||
|
message(STATUS "sdlinclude2 is " ${SDL2_INCLUDE_DIRS})
|
||||||
|
include_directories(${SDL2_INCLUDE_DIRS})
|
||||||
|
set(IMGUI_SDL_LIBRARY ${SDL2_LIBRARIES})
|
||||||
|
message(STATUS IMGUI_SDL_LIBRARY ${SDL2_LIBRARIES})
|
||||||
|
else()#use new one SDL2 config
|
||||||
|
include_directories(${SDL_INCLUDE})
|
||||||
|
set(IMGUI_SDL_LIBRARY SDL2::SDL2)
|
||||||
|
set(SDL_MAIN SDL2::SDL2main)
|
||||||
|
message(STATUS ${SDL_MAIN} ${IMGUI_SDL_LIBRARY})
|
||||||
|
endif()
|
||||||
|
else(DEFINED SDL_PATH)
|
||||||
|
message(STATUS "SDL_PATH not defined")
|
||||||
|
set(IMGUI_SDL_LIBRARY SDL2)
|
||||||
|
endif(DEFINED SDL_PATH)
|
||||||
|
|
||||||
|
add_library(cimgui_sdl SHARED ${IMGUI_SOURCES})
|
||||||
|
target_link_libraries(cimgui_sdl ${IMGUI_LIBRARIES} ${IMGUI_SDL_LIBRARY})
|
||||||
|
|
||||||
|
#using library
|
||||||
|
include_directories(../generator/output/)
|
||||||
|
add_executable(test_sdl main.c ../imgui/examples/libs/gl3w/GL/gl3w.c)
|
||||||
|
target_link_libraries(test_sdl ${IMGUI_SDL_LIBRARY} cimgui_sdl)
|
||||||
|
|
11
backend_test/cimgui_extras.cpp
Normal file
11
backend_test/cimgui_extras.cpp
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#include "../imgui/imgui.h"
|
||||||
|
|
||||||
|
// GL3W/GLFW
|
||||||
|
#include <GL/gl3w.h> // This example is using gl3w to access OpenGL functions (because it is small). You may use glew/glad/glLoadGen/etc. whatever already works for you.
|
||||||
|
|
||||||
|
//making it accesible for luajit
|
||||||
|
#ifdef _WIN32
|
||||||
|
extern "C" __declspec( dllexport ) int Do_gl3wInit(void){ return gl3wInit();};
|
||||||
|
#else
|
||||||
|
extern "C" int Do_gl3wInit(void){ return gl3wInit();};
|
||||||
|
#endif
|
11
backend_test/cimgui_extras.h
Normal file
11
backend_test/cimgui_extras.h
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
|
||||||
|
|
||||||
|
// GL3W/GLFW
|
||||||
|
#include <GL/gl3w.h> // This example is using gl3w to access OpenGL functions (because it is small). You may use glew/glad/glLoadGen/etc. whatever already works for you.
|
||||||
|
|
||||||
|
//making it accesible for luajit
|
||||||
|
#ifdef _WIN32
|
||||||
|
__declspec( dllexport ) int Do_gl3wInit(void);
|
||||||
|
#else
|
||||||
|
int Do_gl3wInit(void);
|
||||||
|
#endif
|
197
backend_test/main.c
Normal file
197
backend_test/main.c
Normal file
@@ -0,0 +1,197 @@
|
|||||||
|
#define CIMGUI_DEFINE_ENUMS_AND_STRUCTS
|
||||||
|
#include "cimgui.h"
|
||||||
|
#include "cimgui_extras.h"
|
||||||
|
#include "cimgui_impl.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#define SDL_MAIN_HANDLED
|
||||||
|
#include <SDL.h>
|
||||||
|
|
||||||
|
|
||||||
|
SDL_Window *window = NULL;
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||||
|
SDL_Log("failed to init: %s", SDL_GetError());
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decide GL+GLSL versions
|
||||||
|
#if __APPLE__
|
||||||
|
// GL 3.2 Core + GLSL 150
|
||||||
|
const char* glsl_version = "#version 150";
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); // Always required on Mac
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
|
||||||
|
#else
|
||||||
|
// GL 3.0 + GLSL 130
|
||||||
|
const char* glsl_version = "#version 130";
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// and prepare OpenGL stuff
|
||||||
|
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||||
|
SDL_DisplayMode current;
|
||||||
|
SDL_GetCurrentDisplayMode(0, ¤t);
|
||||||
|
|
||||||
|
window = SDL_CreateWindow(
|
||||||
|
"Hello", 0, 0, 1024, 768,
|
||||||
|
SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
|
||||||
|
);
|
||||||
|
if (window == NULL) {
|
||||||
|
SDL_Log("Failed to create window: %s", SDL_GetError());
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_GLContext gl_context = SDL_GL_CreateContext(window);
|
||||||
|
SDL_GL_SetSwapInterval(1); // enable vsync
|
||||||
|
|
||||||
|
// Initialize OpenGL loader for cimgui_sdl
|
||||||
|
bool err = Do_gl3wInit() != 0;
|
||||||
|
if (err)
|
||||||
|
{
|
||||||
|
SDL_Log("Failed to initialize OpenGL loader for cimgui_sdl!");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize OpenGL loader for main
|
||||||
|
bool err2 = gl3wInit() != 0;
|
||||||
|
if (err2)
|
||||||
|
{
|
||||||
|
SDL_Log("Failed to initialize OpenGL loader for main!");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// check opengl version sdl uses
|
||||||
|
//SDL_Log("opengl version: %s", (char*)glGetString(GL_VERSION));
|
||||||
|
|
||||||
|
// setup imgui
|
||||||
|
igCreateContext(NULL);
|
||||||
|
|
||||||
|
//set docking
|
||||||
|
ImGuiIO* ioptr = igGetIO();
|
||||||
|
ioptr->ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
|
||||||
|
//ioptr->ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
|
||||||
|
ioptr->ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
|
||||||
|
ioptr->ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
|
||||||
|
|
||||||
|
|
||||||
|
ImGui_ImplSDL2_InitForOpenGL(window, gl_context);
|
||||||
|
ImGui_ImplOpenGL3_Init(glsl_version);
|
||||||
|
|
||||||
|
igStyleColorsDark(NULL);
|
||||||
|
//ImFontAtlas_AddFontDefault(io.Fonts, NULL);
|
||||||
|
|
||||||
|
|
||||||
|
bool showDemoWindow = true;
|
||||||
|
bool showAnotherWindow = false;
|
||||||
|
ImVec4 clearColor;
|
||||||
|
clearColor.x = 0.45f;
|
||||||
|
clearColor.y = 0.55f;
|
||||||
|
clearColor.z = 0.60f;
|
||||||
|
clearColor.w = 1.00f;
|
||||||
|
|
||||||
|
bool quit = false;
|
||||||
|
while (!quit)
|
||||||
|
{
|
||||||
|
SDL_Event e;
|
||||||
|
|
||||||
|
// we need to call SDL_PollEvent to let window rendered, otherwise
|
||||||
|
// no window will be shown
|
||||||
|
while (SDL_PollEvent(&e) != 0)
|
||||||
|
{
|
||||||
|
ImGui_ImplSDL2_ProcessEvent(&e);
|
||||||
|
if (e.type == SDL_QUIT)
|
||||||
|
quit = true;
|
||||||
|
if (e.type == SDL_WINDOWEVENT && e.window.event == SDL_WINDOWEVENT_CLOSE && e.window.windowID == SDL_GetWindowID(window))
|
||||||
|
quit = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// start imgui frame
|
||||||
|
ImGui_ImplOpenGL3_NewFrame();
|
||||||
|
ImGui_ImplSDL2_NewFrame(window);
|
||||||
|
igNewFrame();
|
||||||
|
|
||||||
|
if (showDemoWindow)
|
||||||
|
igShowDemoWindow(&showDemoWindow);
|
||||||
|
|
||||||
|
// show a simple window that we created ourselves.
|
||||||
|
{
|
||||||
|
static float f = 0.0f;
|
||||||
|
static int counter = 0;
|
||||||
|
|
||||||
|
igBegin("Hello, world!", NULL, 0);
|
||||||
|
igText("This is some useful text");
|
||||||
|
igCheckbox("Demo window", &showDemoWindow);
|
||||||
|
igCheckbox("Another window", &showAnotherWindow);
|
||||||
|
|
||||||
|
igSliderFloat("Float", &f, 0.0f, 1.0f, "%.3f", 1.0f);
|
||||||
|
igColorEdit3("clear color", (float*)&clearColor, 0);
|
||||||
|
|
||||||
|
ImVec2 buttonSize;
|
||||||
|
buttonSize.x = 0;
|
||||||
|
buttonSize.y = 0;
|
||||||
|
if (igButton("Button", buttonSize))
|
||||||
|
counter++;
|
||||||
|
igSameLine(0.0f, -1.0f);
|
||||||
|
igText("counter = %d", counter);
|
||||||
|
|
||||||
|
igText("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / igGetIO()->Framerate, igGetIO()->Framerate);
|
||||||
|
igEnd();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (showAnotherWindow)
|
||||||
|
{
|
||||||
|
igBegin("imgui Another Window", &showAnotherWindow, 0);
|
||||||
|
igText("Hello from imgui");
|
||||||
|
ImVec2 buttonSize;
|
||||||
|
buttonSize.x = 0; buttonSize.y = 0;
|
||||||
|
if (igButton("Close me", buttonSize))
|
||||||
|
{
|
||||||
|
showAnotherWindow = false;
|
||||||
|
}
|
||||||
|
igEnd();
|
||||||
|
}
|
||||||
|
|
||||||
|
// render
|
||||||
|
igRender();
|
||||||
|
SDL_GL_MakeCurrent(window, gl_context);
|
||||||
|
glViewport(0, 0, (int)ioptr->DisplaySize.x, (int)ioptr->DisplaySize.y);
|
||||||
|
glClearColor(clearColor.x, clearColor.y, clearColor.z, clearColor.w);
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
ImGui_ImplOpenGL3_RenderDrawData(igGetDrawData());
|
||||||
|
|
||||||
|
if (ioptr->ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
|
||||||
|
{
|
||||||
|
SDL_Window* backup_current_window = SDL_GL_GetCurrentWindow();
|
||||||
|
SDL_GLContext backup_current_context = SDL_GL_GetCurrentContext();
|
||||||
|
igUpdatePlatformWindows();
|
||||||
|
igRenderPlatformWindowsDefault(NULL,NULL);
|
||||||
|
SDL_GL_MakeCurrent(backup_current_window, backup_current_context);
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_GL_SwapWindow(window);
|
||||||
|
}
|
||||||
|
|
||||||
|
// clean up
|
||||||
|
ImGui_ImplOpenGL3_Shutdown();
|
||||||
|
ImGui_ImplSDL2_Shutdown();
|
||||||
|
igDestroyContext(NULL);
|
||||||
|
|
||||||
|
SDL_GL_DeleteContext(gl_context);
|
||||||
|
if (window != NULL)
|
||||||
|
{
|
||||||
|
SDL_DestroyWindow(window);
|
||||||
|
window = NULL;
|
||||||
|
}
|
||||||
|
SDL_Quit();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Reference in New Issue
Block a user