diff --git a/.gitignore b/.gitignore index 3c07540..a4a8420 100644 --- a/.gitignore +++ b/.gitignore @@ -1,15 +1,10 @@ -### Project ### - -TODO.md - -### ImGui ### -**.ini - ### Local build ### bin/ -build/ +build*/ lib/ -imnodes.sln + +### ImGui ### +**.ini # Created by https://www.toptal.com/developers/gitignore/api/sublimetext # Edit at https://www.toptal.com/developers/gitignore?templates=sublimetext @@ -499,4 +494,38 @@ healthchecksdb # Backup folder for Package Reference Convert tool in Visual Studio 2017 MigrationBackup/ -# End of https://www.gitignore.io/api/visualstudio \ No newline at end of file +# End of https://www.gitignore.io/api/visualstudio + +# Created by https://www.toptal.com/developers/gitignore/api/vcpkg +# Edit at https://www.toptal.com/developers/gitignore?templates=vcpkg + +### vcpkg ### +# Vcpkg + +## Manifest Mode +vcpkg_installed/ + +# End of https://www.toptal.com/developers/gitignore/api/vcpkg + + +# Created by https://www.toptal.com/developers/gitignore/api/cmake +# Edit at https://www.toptal.com/developers/gitignore?templates=cmake + +### CMake ### +CMakeLists.txt.user +CMakeCache.txt +CMakeFiles +CMakeScripts +Testing +Makefile +cmake_install.cmake +install_manifest.txt +compile_commands.json +CTestTestfile.cmake +_deps + +### CMake Patch ### +# External projects +*-prefix/ + +# End of https://www.toptal.com/developers/gitignore/api/cmake diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..7436d56 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,59 @@ +cmake_minimum_required(VERSION 3.15) + +project(imnodes) + +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED True) + +find_package(imgui CONFIG REQUIRED) +find_package(SDL2 CONFIG REQUIRED) + + +# Imnodes + +add_library(imnodes) +target_sources(imnodes PRIVATE + imnodes.h + imnodes_internal.h + imnodes.cpp) +target_include_directories(imnodes PUBLIC ${CMAKE_SOURCE_DIR}) +target_link_libraries(imnodes PUBLIC imgui::imgui) + + +# Example projects + +add_executable(colornode + ${CMAKE_SOURCE_DIR}/imnodes.cpp + ${CMAKE_SOURCE_DIR}/example/main.cpp + ${CMAKE_SOURCE_DIR}/example/color_node_editor.cpp) +target_link_libraries(colornode imnodes SDL2::SDL2) +if (APPLE) + target_link_libraries(colornode "-framework OpenGL") +endif() + +add_executable(multieditor + ${CMAKE_SOURCE_DIR}/imnodes.cpp + ${CMAKE_SOURCE_DIR}/example/main.cpp + ${CMAKE_SOURCE_DIR}/example/multi_editor.cpp) +target_link_libraries(multieditor imnodes SDL2::SDL2) +if (APPLE) + target_link_libraries(multieditor "-framework OpenGL") +endif() + +add_executable(saveload + ${CMAKE_SOURCE_DIR}/imnodes.cpp + ${CMAKE_SOURCE_DIR}/example/main.cpp + ${CMAKE_SOURCE_DIR}/example/save_load.cpp) +target_link_libraries(saveload imnodes SDL2::SDL2) +if (APPLE) + target_link_libraries(saveload "-framework OpenGL") +endif() + +add_executable(hello + ${CMAKE_SOURCE_DIR}/imnodes.cpp + ${CMAKE_SOURCE_DIR}/example/main.cpp + ${CMAKE_SOURCE_DIR}/example/hello.cpp) +target_link_libraries(hello imnodes SDL2::SDL2) +if (APPLE) + target_link_libraries(hello "-framework OpenGL") +endif() diff --git a/example/color_node_editor.cpp b/example/color_node_editor.cpp index d0983a2..7b03254 100644 --- a/example/color_node_editor.cpp +++ b/example/color_node_editor.cpp @@ -4,8 +4,8 @@ #include #include -#include -#include +#include +#include #include #include #include @@ -123,8 +123,11 @@ ImU32 evaluate(const Graph& graph, const int root_node) class ColorNodeEditor { public: - ColorNodeEditor() : graph_(), nodes_(), root_node_id_(-1), - minimap_location_(ImNodesMiniMapLocation_BottomRight) {} + ColorNodeEditor() + : graph_(), nodes_(), root_node_id_(-1), + minimap_location_(ImNodesMiniMapLocation_BottomRight) + { + } void show() { diff --git a/example/main.cpp b/example/main.cpp index 5ea1b8e..06be1f7 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -1,26 +1,39 @@ #include "node_editor.h" + #include #include #include #include +#include +#if defined(IMGUI_IMPL_OPENGL_ES2) +#include +#else +#include +#endif + #include -#include -#include int main(int, char**) { - if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) { printf("Error: %s\n", SDL_GetError()); return -1; } -#if __APPLE__ + // Decide GL+GLSL versions +#if defined(IMGUI_IMPL_OPENGL_ES2) + // GL ES 2.0 + GLSL 100 + const char* glsl_version = "#version 100"; + SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); +#elif defined(__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_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); @@ -33,48 +46,48 @@ int main(int, char**) SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); #endif + // Create window with graphics context SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); - SDL_DisplayMode current; - SDL_GetCurrentDisplayMode(0, ¤t); + SDL_WindowFlags window_flags = + (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI); SDL_Window* window = SDL_CreateWindow( - "imgui-node-editor example", + "Dear ImGui SDL2+OpenGL3 example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, - SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI); + window_flags); SDL_GLContext gl_context = SDL_GL_CreateContext(window); SDL_GL_MakeCurrent(window, gl_context); SDL_GL_SetSwapInterval(1); // Enable vsync - if (gl3wInit()) - { - fprintf(stderr, "Failed to initialize OpenGL loader!\n"); - return 1; - } - + // Setup Dear ImGui context IMGUI_CHECKVERSION(); ImGui::CreateContext(); - - ImGui_ImplSDL2_InitForOpenGL(window, gl_context); - ImGui_ImplOpenGL3_Init(glsl_version); + ImGuiIO& io = ImGui::GetIO(); + (void)io; ImNodes::CreateContext(); + example::NodeEditorInitialize(); - // Setup style + // Setup Dear ImGui style ImGui::StyleColorsDark(); + // ImGui::StyleColorsClassic(); ImNodes::StyleColorsDark(); - bool done = false; - bool initialized = false; + // Setup Platform/Renderer backends + ImGui_ImplSDL2_InitForOpenGL(window, gl_context); + ImGui_ImplOpenGL3_Init(glsl_version); - { - const ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); - glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w); - } + // Our state + bool show_demo_window = true; + bool show_another_window = false; + ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); + // Main loop + bool done = false; while (!done) { SDL_Event event; @@ -93,31 +106,26 @@ int main(int, char**) ImGui_ImplSDL2_NewFrame(); ImGui::NewFrame(); - if (!initialized) - { - initialized = true; - example::NodeEditorInitialize(); - } - example::NodeEditorShow(); // Rendering ImGui::Render(); - - int fb_width, fb_height; - SDL_GL_GetDrawableSize(window, &fb_width, &fb_height); - glViewport(0, 0, fb_width, fb_height); + glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y); + glClearColor( + clear_color.x * clear_color.w, + clear_color.y * clear_color.w, + clear_color.z * clear_color.w, + clear_color.w); glClear(GL_COLOR_BUFFER_BIT); - ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); SDL_GL_SwapWindow(window); } - example::NodeEditorShutdown(); - ImNodes::DestroyContext(); - + // Cleanup ImGui_ImplOpenGL3_Shutdown(); ImGui_ImplSDL2_Shutdown(); + example::NodeEditorShutdown(); + ImNodes::DestroyContext(); ImGui::DestroyContext(); SDL_GL_DeleteContext(gl_context); diff --git a/vcpkg.json b/vcpkg.json new file mode 100644 index 0000000..f16de75 --- /dev/null +++ b/vcpkg.json @@ -0,0 +1,11 @@ +{ + "name": "imnodes", + "version-string": "0.1.0-dev", + "dependencies": [ + "sdl2", + { + "name": "imgui", + "features": [ "sdl2-binding", "opengl3-binding" ] + } + ] +}