From 734d37bb046e51404cb094cda66d8274694fe086 Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 16 Jul 2021 19:28:25 +0200 Subject: Put together a stub of the viewer --- .gitignore | 5 +++ Makefile | 15 +++++++ markdown.cpp | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 146 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 markdown.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9542e0a --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +*.o +*.a +*.so* +markdown +imgui.ini diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9f647f5 --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +CXXFLAGS+=-Wall -Wpedantic -Wextra +CXXFLAGS+=$(shell pkg-config --cflags gl glfw3 glew) +CXXFLAGS+=-I$(HOME)/repos/imgui +CXXFLAGS+=-I$(HOME)/repos/imgui/backends +CXXFLAGS+=-I$(HOME)/repos/imgui_md +LDLIBS+=-L$(HOME)/repos/imgui_md -l:libimgui_md.a +LDLIBS+=$(shell pkg-config --libs gl glfw3 glew md4c) +LDLIBS+=-lstdc++ -lm + +markdown: markdown.o imgui.o imgui_draw.o imgui_widgets.o imgui_demo.o imgui_impl_opengl3.o imgui_impl_glfw.o imgui_tables.o + +clean: + rm -f *.o + +.PHONY: clean diff --git a/markdown.cpp b/markdown.cpp new file mode 100644 index 0000000..a7c1312 --- /dev/null +++ b/markdown.cpp @@ -0,0 +1,126 @@ +#include + +#include +#include + +#include +#include +#include +#include + +ImFont * g_font_regular; +ImFont * g_font_bold; +ImFont * g_font_bold_large; + +struct Markdown : public imgui_md +{ + ImFont * get_font() const override + { + if (m_is_table_header) + { + return g_font_bold; + } + switch (m_hlevel) + { + case 0: + return m_is_strong ? g_font_bold : g_font_regular; + case 1: + return g_font_bold_large; + default: + return g_font_bold; + } + } +}; + +static void glfw_error_callback(int error, const char * description) +{ + fprintf(stderr, "glfw (%d): %s\n", error, description); +} + +static void markdown(const char * str, const char * str_end) +{ + static Markdown s_printer; + s_printer.print(str, str_end); +} + +ImVec2 g_window_size; + +static void resized(GLFWwindow *, int width, int height) +{ + g_window_size.x = static_cast(width); + g_window_size.y = static_cast(height); +} + +int main(int, char **) +{ + glfwSetErrorCallback(glfw_error_callback); + if (!glfwInit()) + return 1; + + const char * glsl_version = "#version 130"; + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); + + GLFWwindow * window = glfwCreateWindow(1280, 720, "Markdown", nullptr, nullptr); + if (nullptr == window) + return 1; + glfwMakeContextCurrent(window); + glfwSwapInterval(1); + glfwSetWindowSizeCallback(window, resized); + + if (GLEW_OK != glewInit()) + return 1; + + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO & io = ImGui::GetIO(); + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; + io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; + g_font_regular = io.Fonts->AddFontFromFileTTF("OpenSans-Regular.ttf", 18); + g_font_bold = io.Fonts->AddFontFromFileTTF("OpenSans-Bold.ttf", 18); + g_font_bold_large = io.Fonts->AddFontFromFileTTF("OpenSans-Bold.ttf", 26); + + ImGui::StyleColorsDark(); + ImGui_ImplGlfw_InitForOpenGL(window, true); + ImGui_ImplOpenGL3_Init(glsl_version); + + while (!glfwWindowShouldClose(window)) + { + glfwPollEvents(); + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); + + ImGui::SetNextWindowPos({0, 0}); + ImGui::SetNextWindowSize(g_window_size); + ImGui::Begin( + "Markdown", + nullptr, + ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoSavedSettings + ); + static const char * text = + "# Hello world!\n" // 15 + "Lorem ipsum dolor\n" // 18 33 + " * sit\n" // 7 40 + " * amet\n"; // 8 48 + markdown(text, text + 48); + ImGui::End(); + + ImGui::Render(); + int display_w, display_h; + glfwGetFramebufferSize(window, &display_w, &display_h); + glViewport(0, 0, display_w, display_h); + glClearColor(0, 0, 0, 0); + glClear(GL_COLOR_BUFFER_BIT); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + + glfwSwapBuffers(window); + } + + ImGui_ImplOpenGL3_Shutdown(); + ImGui_ImplGlfw_Shutdown(); + ImGui::DestroyContext(); + + glfwDestroyWindow(window); + glfwTerminate(); +} -- cgit v1.1