[ui] Basic integration of imgui using opengl3 & sdl on macos.

This commit is contained in:
Alexander Arlt 2022-08-05 23:09:50 +02:00
parent 31bd4f6b80
commit 8ba06a8675
13 changed files with 1419 additions and 2 deletions

View File

@ -22,8 +22,8 @@ eth_policy()
# project name and version should be set after cmake_policy CMP0048
set(PROJECT_VERSION "0.8.17")
# OSX target needed in order to support std::visit
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.14")
# OSX target needed in order to support std::visit & std::filesystem
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.15")
project(solidity VERSION ${PROJECT_VERSION} LANGUAGES C CXX)
include(TestBigEndian)

View File

@ -11,6 +11,7 @@ macro(configure_project)
# components
eth_default_option(TESTS ON)
eth_default_option(TOOLS ON)
eth_default_option(SOLIDITY_UI OFF)
# Define a matching property name of each of the "features".
foreach(FEATURE ${ARGN})
@ -39,6 +40,7 @@ endif()
if (SUPPORT_TOOLS)
message("-- TOOLS Build tools ${TOOLS}")
endif()
message("-- SOLIDITY_UI Build solidity-ui ${SOLIDITY_UI}")
message("------------------------------------------------------------------ flags")
message("-- OSSFUZZ ${OSSFUZZ}")
message("------------------------------------------------------------------------")

View File

@ -0,0 +1,13 @@
// The generation of this file is defined in tools/solidity-ui/CMakeLists.txt.
// This file was generated by using the content of tools/solidity-ui/assets/@SOLIDITY_UI_ASSET_FILE_NAME@.yul.
#pragma once
namespace solidity::ui::assets
{
static uint8_t const @SOLIDITY_UI_ASSET_FILE_NAME@[] = {
@SOLIDITY_UI_ASSET_FILE_CONTENT@, 0
};
} // namespace solidity::ui::assets

View File

@ -0,0 +1,28 @@
FetchContent_Declare(
imgui
GIT_REPOSITORY https://github.com/ocornut/imgui
GIT_TAG 58eb40db76783f5da09e592ca3eb421f4f2197e3
)
FetchContent_MakeAvailable(imgui)
add_library(imgui
${imgui_SOURCE_DIR}/imconfig.h
${imgui_SOURCE_DIR}/imgui.h
${imgui_SOURCE_DIR}/imgui.cpp
${imgui_SOURCE_DIR}/imgui_demo.cpp
${imgui_SOURCE_DIR}/imgui_draw.cpp
${imgui_SOURCE_DIR}/imgui_internal.h
${imgui_SOURCE_DIR}/imgui_tables.cpp
${imgui_SOURCE_DIR}/imgui_widgets.cpp
${imgui_SOURCE_DIR}/imstb_textedit.h
${imgui_SOURCE_DIR}/imstb_truetype.h
${imgui_SOURCE_DIR}/backends/imgui_impl_sdl.h
# ${imgui_SOURCE_DIR}/backends/imgui_impl_sdl.cpp
${CMAKE_SOURCE_DIR}/tools/solidity-ui/platform/macos/imgui_impl_sdl.cpp
${imgui_SOURCE_DIR}/backends/imgui_impl_opengl3.h
${imgui_SOURCE_DIR}/backends/imgui_impl_opengl3.cpp
)
find_package(SDL2 CONFIG REQUIRED)
find_package(OpenGL REQUIRED COMPONENTS OpenGL)
target_link_libraries(imgui PRIVATE SDL2::SDL2 OpenGL::GL)
#target_compile_definitions(imgui PUBLIC IMGUI_DISABLE_OBSOLETE_FUNCTIONS=1)
target_include_directories(imgui PUBLIC ${imgui_SOURCE_DIR} ${imgui_SOURCE_DIR}/backends ${SDL2_INCLUDE_DIRS} ${OPENGL_INCLUDE_DIRS})

View File

@ -0,0 +1,12 @@
FetchContent_Declare(
imgui_memory_editor
GIT_REPOSITORY https://github.com/ocornut/imgui_club.git
GIT_TAG d4cd9896e15a03e92702a578586c3f91bbde01e8
)
FetchContent_GetProperties(imgui_memory_editor)
if(NOT imgui_memory_editor_POPULATED)
FetchContent_Populate(imgui_memory_editor)
endif()
add_library(imgui_memory_editor INTERFACE IMPORTED)
target_include_directories(imgui_memory_editor INTERFACE ${imgui_memory_editor_SOURCE_DIR}/imgui_memory_editor)

View File

@ -0,0 +1,23 @@
FetchContent_Declare(
implot
GIT_REPOSITORY https://github.com/epezent/implot.git
GIT_TAG fc0fd112467c2be84bc56daa57612b0c579ff1af
)
FetchContent_GetProperties(implot)
if(NOT implot_POPULATED)
FetchContent_Populate(implot)
endif()
add_library(implot)
target_sources(implot PRIVATE
${implot_SOURCE_DIR}/implot.h
${implot_SOURCE_DIR}/implot.cpp
${implot_SOURCE_DIR}/implot_internal.h
${implot_SOURCE_DIR}/implot_items.cpp)
target_compile_options(implot PRIVATE "-Wno-sign-conversion")
target_compile_options(implot PRIVATE "-Wno-extra-semi")
target_compile_options(implot PRIVATE "-Wno-implicit-fallthrough")
target_compile_options(implot PRIVATE "-Wno-null-arithmetic")
target_link_libraries(implot PRIVATE imgui)
target_include_directories(implot INTERFACE ${implot_SOURCE_DIR})
add_dependencies(implot imgui)

View File

@ -0,0 +1,15 @@
set(BUILD_EXTENSIONS OFF)
set(BUILD_DEMOS OFF)
add_compile_definitions(ZEP_SINGLE_HEADER_BUILD=1 ZEP_FEATURE_CPP_FILE_SYSTEM=1)
FetchContent_Declare(
zep
GIT_REPOSITORY https://github.com/Rezonality/zep.git
GIT_TAG 46a783d6bc14f2dad2660c98ffcfddec5500b085
)
FetchContent_GetProperties(zep)
if(NOT zep_POPULATED)
FetchContent_Populate(zep)
endif()
add_library(zep INTERFACE IMPORTED)
target_include_directories(zep INTERFACE ${zep_SOURCE_DIR}/include)

View File

@ -49,3 +49,7 @@ add_executable(yul-phaser yulPhaser/main.cpp)
target_link_libraries(yul-phaser PRIVATE phaser)
install(TARGETS yul-phaser DESTINATION "${CMAKE_INSTALL_BINDIR}")
if (SOLIDITY_UI)
add_subdirectory(solidity-ui)
endif()

View File

@ -0,0 +1,51 @@
include_directories(${CMAKE_SOURCE_DIR}/tools/solidity-ui)
include(solidity-ui/imgui)
include(solidity-ui/zep)
include(solidity-ui/imgui_memory_editor)
include(solidity-ui/implot)
set(SOLIDITY_UI_ASSETS JetBrainsMono-Regular.ttf)
set(GENERATED_SOLIDITY_UI_ASSETS_HEADERS)
foreach(asset IN LISTS SOLIDITY_UI_ASSETS)
set(SOLIDITY_UI_ASSET_FILE ${CMAKE_SOURCE_DIR}/tools/solidity-ui/assets/${asset})
file(READ ${SOLIDITY_UI_ASSET_FILE} SOLIDITY_UI_ASSET_FILE_CONTENT HEX)
string(REGEX MATCHALL ".." SOLIDITY_UI_ASSET_FILE_CONTENT "${SOLIDITY_UI_ASSET_FILE_CONTENT}")
string(REGEX REPLACE ";" ",\n\t0x" SOLIDITY_UI_ASSET_FILE_CONTENT "${SOLIDITY_UI_ASSET_FILE_CONTENT}")
set(SOLIDITY_UI_ASSET_FILE_CONTENT "0x${SOLIDITY_UI_ASSET_FILE_CONTENT}")
set(SOLIDITY_UI_ASSET_FILE_NAME ${asset})
string(REGEX REPLACE "\\." "_" SOLIDITY_UI_ASSET_FILE_NAME "${SOLIDITY_UI_ASSET_FILE_NAME}")
string(REGEX REPLACE "-" "_" SOLIDITY_UI_ASSET_FILE_NAME "${SOLIDITY_UI_ASSET_FILE_NAME}")
configure_file("${CMAKE_SOURCE_DIR}/cmake/solidity-ui/asset.in" ${CMAKE_BINARY_DIR}/tools/solidity-ui/assets/${asset}.h @ONLY)
list(APPEND SOLIDITY_UI_ASSETS_HEADERS ${CMAKE_BINARY_DIR}/tools/solidity-ui/assets/${asset}.h)
endforeach()
set(sources
${SOLIDITY_UI_ASSETS_HEADERS}
main.cpp
)
add_executable(solidity-ui ${sources})
target_link_libraries(solidity-ui PRIVATE solcli SYSTEM imgui zep imgui_memory_editor implot)
target_compile_definitions(solidity-ui PUBLIC CMAKE_SOURCE_DIR="${CMAKE_SOURCE_DIR}")
target_include_directories(solidity-ui PUBLIC ${CMAKE_BINARY_DIR}/tools/solidity-ui)
include(GNUInstallDirs)
install(TARGETS solidity-ui DESTINATION "${CMAKE_INSTALL_BINDIR}")
if (SOLC_LINK_STATIC AND UNIX AND NOT APPLE)
# Produce solc as statically linked binary (includes C/C++ standard libraries)
# This is not supported on macOS, see
# https://developer.apple.com/library/content/qa/qa1118/_index.html.
set_target_properties(
solidity-ui PROPERTIES
LINK_FLAGS -static
LINK_SEARCH_START_STATIC ON
LINK_SEARCH_END_STATIC ON
)
elseif (SOLC_STATIC_STDLIBS AND UNIX AND NOT APPLE)
set_target_properties(
solidity-ui PROPERTIES
LINK_FLAGS "-static-libgcc -static-libstdc++"
)
endif ()

Binary file not shown.

View File

@ -0,0 +1 @@
#pragma once

132
tools/solidity-ui/main.cpp Normal file
View File

@ -0,0 +1,132 @@
#include <SDL.h>
#include <backends/imgui_impl_opengl3.h>
#include <backends/imgui_impl_sdl.h>
#include <imgui.h>
#if defined(IMGUI_IMPL_OPENGL_ES2)
#include <SDL_opengles2.h>
#else
#include <SDL_opengl.h>
#endif
#include "assets/JetBrainsMono-Regular.ttf.h"
int main(int argc, char* argv[])
{
(void)argc;
(void)argv;
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0)
{
printf("Error: %s\n", SDL_GetError());
return -1;
}
// 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_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
// 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);
auto window_flags = (SDL_WindowFlags) (SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
// auto window_flags = (SDL_WindowFlags) (SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
SDL_Window* window
= SDL_CreateWindow("solidity-ui", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags);
SDL_GLContext gl_context = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, gl_context);
SDL_GL_SetSwapInterval(1); // Enable vsync
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
// Setup Dear ImGui style
ImGui::StyleColorsDark();
// ImGui::StyleColorsClassic();
// Setup Platform/Renderer backends
ImGui_ImplSDL2_InitForOpenGL(window, gl_context);
ImGui_ImplOpenGL3_Init(glsl_version);
// Load Fonts
auto font_data = new uint8_t[sizeof(solidity::ui::assets::JetBrainsMono_Regular_ttf)];
memcpy(font_data, solidity::ui::assets::JetBrainsMono_Regular_ttf, sizeof(solidity::ui::assets::JetBrainsMono_Regular_ttf));
// ownership of font_data will be transferred imgui
io.Fonts->AddFontFromMemoryTTF(font_data, sizeof(font_data), 16 * ImGui::GetPlatformIO().Monitors.begin()->DpiScale);
// #2b2b2b
ImVec4 clear_color = ImVec4(0.169f, 0.169f, 0.169f, 1.00f);
// Main loop
bool done = false;
while (!done)
{
// Poll and handle events (inputs, window resize, etc.)
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your
// inputs.
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or
// clear/overwrite your copy of the mouse data.
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or
// clear/overwrite your copy of the keyboard data. Generally you may always pass all inputs to dear imgui, and
// hide them from your application based on those two flags.
SDL_Event event;
while (SDL_PollEvent(&event))
{
ImGui_ImplSDL2_ProcessEvent(&event);
if (event.type == SDL_QUIT)
done = true;
if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE
&& event.window.windowID == SDL_GetWindowID(window))
done = true;
}
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
// Rendering
ImGui::Render();
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);
}
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext();
SDL_GL_DeleteContext(gl_context);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}

File diff suppressed because it is too large Load Diff