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

This commit is contained in:
Alexander Arlt
2022-08-15 12:22:07 +02:00
parent 31bd4f6b80
commit 8ba06a8675
13 changed files with 1419 additions and 2 deletions
+51
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.
+1
View File
@@ -0,0 +1 @@
#pragma once
+132
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