Skip to content

Commit

Permalink
Build library and examples using CMake on macos
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelarius committed Mar 13, 2022
1 parent f2c6379 commit 5d0b9b1
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 54 deletions.
49 changes: 39 additions & 10 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
# 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
59 changes: 59 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
11 changes: 7 additions & 4 deletions example/color_node_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <imnodes.h>
#include <imgui.h>

#include <SDL_keycode.h>
#include <SDL_timer.h>
#include <SDL2/SDL_keycode.h>
#include <SDL2/SDL_timer.h>
#include <algorithm>
#include <cassert>
#include <chrono>
Expand Down Expand Up @@ -123,8 +123,11 @@ ImU32 evaluate(const Graph<Node>& 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()
{
Expand Down
88 changes: 48 additions & 40 deletions example/main.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
#include "node_editor.h"

#include <imgui.h>
#include <imgui_impl_sdl.h>
#include <imgui_impl_opengl3.h>
#include <imnodes.h>
#include <SDL2/SDL.h>
#if defined(IMGUI_IMPL_OPENGL_ES2)
#include <SDL2/SDL_opengles2.h>
#else
#include <SDL2/SDL_opengl.h>
#endif

#include <stdio.h>
#include <SDL.h>
#include <GL/gl3w.h>

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);
Expand All @@ -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, &current);
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;
Expand All @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions vcpkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "imnodes",
"version-string": "0.1.0-dev",
"dependencies": [
"sdl2",
{
"name": "imgui",
"features": [ "sdl2-binding", "opengl3-binding" ]
}
]
}

0 comments on commit 5d0b9b1

Please sign in to comment.