From 9dfe0faa5662a3bc0c1c05d1291a037c88bea19a Mon Sep 17 00:00:00 2001 From: Pcornat Date: Sat, 8 Jan 2022 21:57:17 +0100 Subject: [PATCH 1/2] Beginning writing gui and window wrapper for GLFW --- CMakeLists.txt | 57 +++++++++++++++++++++++++++++++++++++++++--- include/gui.hpp | 18 ++++++++++++++ include/gui_data.hpp | 16 +++++++++++++ include/window.hpp | 25 +++++++++++++++++++ src/gui.cpp | 5 ++++ src/gui_data.cpp | 5 ++++ src/main.cpp | 6 ++++- src/window.cpp | 13 ++++++++++ 8 files changed, 141 insertions(+), 4 deletions(-) create mode 100644 include/gui.hpp create mode 100644 include/gui_data.hpp create mode 100644 include/window.hpp create mode 100644 src/gui.cpp create mode 100644 src/gui_data.cpp create mode 100644 src/window.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index cde0bea..47f8f1e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,11 +1,62 @@ cmake_minimum_required(VERSION 3.19 FATAL_ERROR) -project(BillySheet) +project(BillySheet LANGUAGES CXX C) -add_executable(BillySheet src/main.cpp) +set(SOURCE_HEADERS + include/imgui/imconfig.h + include/imgui/imgui.h + include/imgui/imgui_impl_glfw.h + include/imgui/imgui_impl_opengl3.h + include/imgui/imgui_impl_opengl3_loader.h + include/imgui/imgui_internal.h + include/gui.hpp + include/gui_data.hpp + include/window.hpp) -set_target_properties(BillySheet PROPERTIES +set(SOURCE_FILES + src/imgui/imgui.cpp + src/imgui/imgui_draw.cpp + src/imgui/imgui_impl_glfw.cpp + src/imgui/imgui_impl_opengl3.cpp + src/imgui/imgui_tables.cpp + src/imgui/imgui_widgets.cpp + src/main.cpp + src/gui.cpp + src/gui_data.cpp + src/window.cpp) + +set(SOURCES + ${SOURCE_HEADERS} + ${SOURCE_FILES}) + +option(BUILD_SHARED_LIBS "Build shared libraries" OFF) +option(GLFW_BUILD_EXAMPLES "Build the GLFW example programs" OFF) +option(GLFW_BUILD_TESTS "Build the GLFW test programs" OFF) +option(GLFW_BUILD_DOCS "Build the GLFW documentation" OFF) +option(GLFW_INSTALL "Generate installation target" OFF) +option(GLFW_VULKAN_STATIC "Assume the Vulkan loader is linked with the application" OFF) +add_subdirectory(external/glfw) + +option(SPDLOG_ENABLE_PCH "Build static or shared library using precompiled header to speed up compilation time" ON) +option(SPDLOG_BUILD_WARNINGS "Enable compiler warnings" ON) +option(SPDLOG_PREVENT_CHILD_FD "Prevent from child processes to inherit log file descriptors" ON) +option(SPDLOG_NO_THREAD_ID "prevent spdlog from querying the thread id on each log call if thread id is not needed" ON) +option(SPDLOG_NO_TLS "prevent spdlog from using thread local storage" ON) +option(SPDLOG_NO_ATOMIC_LEVELS "prevent spdlog from using of std::atomic log levels (use only if your code never modifies log levels concurrently" ON) +add_subdirectory(external/spdlog) + +add_executable(BillySheet ${SOURCES}) + +target_include_directories(BillySheet PRIVATE include) + +set_target_properties(BillySheet spdlog glfw PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF INTERPROCEDURAL_OPTIMIZATION ON UNITY_BUILD ON) +target_precompile_headers(BillySheet PRIVATE ${SOURCE_HEADERS}) +target_compile_definitions(BillySheet PRIVATE + $<$:_GLIBCXX_DEBUG> + $<$:SPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_DEBUG> + $<$:SPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_ERROR>) +target_link_libraries(BillySheet glfw spdlog) diff --git a/include/gui.hpp b/include/gui.hpp new file mode 100644 index 0000000..7894976 --- /dev/null +++ b/include/gui.hpp @@ -0,0 +1,18 @@ +#ifndef BILLYSHEET_GUI_HPP +#define BILLYSHEET_GUI_HPP + + +namespace gui { + class Gui final { + private: + + public: + Gui() = delete; + + + + ~Gui() noexcept; + }; +} + +#endif //BILLYSHEET_GUI_HPP diff --git a/include/gui_data.hpp b/include/gui_data.hpp new file mode 100644 index 0000000..edf1f25 --- /dev/null +++ b/include/gui_data.hpp @@ -0,0 +1,16 @@ +#ifndef BILLYSHEET_GUI_DATA_HPP +#define BILLYSHEET_GUI_DATA_HPP + +namespace gui { + class GuiData final { + private: +// GLFWwindow *window{ nullptr }; + + public: + GuiData() = delete; + ~GuiData() noexcept = default; + }; +} + + +#endif //BILLYSHEET_GUI_DATA_HPP diff --git a/include/window.hpp b/include/window.hpp new file mode 100644 index 0000000..3a143e0 --- /dev/null +++ b/include/window.hpp @@ -0,0 +1,25 @@ +#ifndef BILLYSHEET_WINDOW_HPP +#define BILLYSHEET_WINDOW_HPP + +#include +#include + +namespace gui { + class Window final { + private: + static void delete_glfw_window(GLFWwindow *glfWwindow) { + glfwDestroyWindow(glfWwindow); + glfwTerminate(); + } + + std::unique_ptr wwindow{ nullptr, delete_glfw_window }; + + public: + Window(); + + ~Window() noexcept = default; + }; +} + + +#endif //BILLYSHEET_WINDOW_HPP diff --git a/src/gui.cpp b/src/gui.cpp new file mode 100644 index 0000000..24f1faf --- /dev/null +++ b/src/gui.cpp @@ -0,0 +1,5 @@ +#include "gui.hpp" + +gui::Gui::~Gui() noexcept { + +} diff --git a/src/gui_data.cpp b/src/gui_data.cpp new file mode 100644 index 0000000..adbb944 --- /dev/null +++ b/src/gui_data.cpp @@ -0,0 +1,5 @@ +// +// Created by postaron on 08/01/2022. +// + +#include "gui_data.hpp" diff --git a/src/main.cpp b/src/main.cpp index ec3351e..fdb0829 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,10 @@ #include +#include +#include int main() { - std::cout << "Hello, World!" << std::endl; + std::ios::sync_with_stdio(false); + spdlog::set_default_logger(spdlog::stdout_logger_st("console")); + SPDLOG_WARN("Hello world !"); return EXIT_SUCCESS; } diff --git a/src/window.cpp b/src/window.cpp new file mode 100644 index 0000000..ccfe9ea --- /dev/null +++ b/src/window.cpp @@ -0,0 +1,13 @@ +#include +#include "window.hpp" + +gui::Window::Window() { + glfwSetErrorCallback(nullptr); + if (glfwInit() == GLFW_FALSE) { + throw std::runtime_error("GLFW init failed."); + } + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); + wwindow = std::unique_ptr(glfwCreateWindow(600, 800, "Billy Sheet tracker", nullptr, nullptr), + delete_glfw_window); +} \ No newline at end of file From 01697f9df4613ae004b59d40185452122c2f9fb4 Mon Sep 17 00:00:00 2001 From: Pcornat Date: Sat, 8 Jan 2022 23:40:31 +0100 Subject: [PATCH 2/2] Compiles, not run yet. --- CMakeLists.txt | 79 +++++++++++++++++++++++++++++++++++++++++--- README.md | 8 ++++- include/gui.hpp | 2 -- include/gui_data.hpp | 7 +++- src/gui_data.cpp | 4 --- src/window.cpp | 9 +++-- 6 files changed, 94 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 47f8f1e..7bd6082 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,14 @@ cmake_minimum_required(VERSION 3.19 FATAL_ERROR) project(BillySheet LANGUAGES CXX C) +find_package(OpenGL REQUIRED) + +set(PRECOMPILE_HEADERS + include/gui.hpp + include/gui_data.hpp + include/window.hpp + ) + set(SOURCE_HEADERS include/imgui/imconfig.h include/imgui/imgui.h @@ -8,6 +16,9 @@ set(SOURCE_HEADERS include/imgui/imgui_impl_opengl3.h include/imgui/imgui_impl_opengl3_loader.h include/imgui/imgui_internal.h + include/imgui/imstb_rectpack.h + include/imgui/imstb_textedit.h + include/imgui/imstb_truetype.h include/gui.hpp include/gui_data.hpp include/window.hpp) @@ -28,6 +39,17 @@ set(SOURCES ${SOURCE_HEADERS} ${SOURCE_FILES}) +find_program(CCACHE_FOUND ccache) +if (CCACHE_FOUND) + message(STATUS "ccache found !") + set_property(GLOBAL PROPERTY C_COMPILER_LAUNCHER ccache) + set_property(GLOBAL PROPERTY C_LINKER_LAUNCHER ccache) + set_property(GLOBAL PROPERTY CXX_COMPILER_LAUNCHER ccache) + set_property(GLOBAL PROPERTY CXX_LINKER_LAUNCHER ccache) +else () + message(STATUS "ccache not found") +endif () + option(BUILD_SHARED_LIBS "Build shared libraries" OFF) option(GLFW_BUILD_EXAMPLES "Build the GLFW example programs" OFF) option(GLFW_BUILD_TESTS "Build the GLFW test programs" OFF) @@ -44,19 +66,66 @@ option(SPDLOG_NO_TLS "prevent spdlog from using thread local storage" ON) option(SPDLOG_NO_ATOMIC_LEVELS "prevent spdlog from using of std::atomic log levels (use only if your code never modifies log levels concurrently" ON) add_subdirectory(external/spdlog) +set(COMPILE_FLAGS + -pipe + -march=skylake # change to native or your architecture. + -mtune=skylake # same as above + -mrdseed # be careful about this, this is linked to the x86 architecture. + -mrdrnd # same as above + -Wall + -Wextra + -Wpedantic + -pedantic + -ffunction-sections + -fdata-sections + -fuse-ld=gold + -funroll-loops + -fdevirtualize-at-ltrans + -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free + ) +set(LINKER_OPTIONS + -Wl,--sort-common,--as-needed,--gc-sections,--strip-all + -fuse-ld=gold + -fdevirtualize-at-ltrans + ) + +set(LINKER_FLAGS + jemalloc + ) + add_executable(BillySheet ${SOURCES}) -target_include_directories(BillySheet PRIVATE include) +target_include_directories(BillySheet PRIVATE include include/imgui) -set_target_properties(BillySheet spdlog glfw PROPERTIES +set_target_properties(BillySheet spdlog PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF INTERPROCEDURAL_OPTIMIZATION ON - UNITY_BUILD ON) -target_precompile_headers(BillySheet PRIVATE ${SOURCE_HEADERS}) + # UNITY_BUILD ON + ) +set_target_properties(glfw PROPERTIES + C_STANDARD 11 + C_STANDARD_REQUIRED ON + C_EXTENSIONS OFF + INTERPROCEDURAL_OPTIMIZATION ON + # UNITY_BUILD ON + ) + +#target_precompile_headers(BillySheet PRIVATE ${PRECOMPILE_HEADERS}) target_compile_definitions(BillySheet PRIVATE $<$:_GLIBCXX_DEBUG> $<$:SPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_DEBUG> $<$:SPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_ERROR>) -target_link_libraries(BillySheet glfw spdlog) + +target_compile_options(spdlog PRIVATE ${COMPILE_FLAGS}) +target_compile_options(glfw PRIVATE ${COMPILE_FLAGS}) +target_compile_options(BillySheet PRIVATE ${COMPILE_FLAGS}) + +target_link_options(spdlog PRIVATE ${LINKER_OPTIONS}) +target_link_options(glfw PRIVATE ${LINKER_OPTIONS}) +target_link_options(BillySheet PRIVATE ${LINKER_OPTIONS}) + +target_link_libraries(spdlog PRIVATE ${LINKER_FLAGS}) +target_link_libraries(glfw PRIVATE ${LINKER_FLAGS}) +target_link_libraries(BillySheet glfw spdlog OpenGL::OpenGL ${LINKER_FLAGS}) diff --git a/README.md b/README.md index 48b338e..4835699 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,12 @@ This project is about to digitally control your character sheet from the book "La Forteresse du Chaudron Noir" from Bob Lennon. ## External dependencies -All dependencies are inside the "external" directory. The followings are inside: +Some dependencies are inside the "external" directory. The followings are inside: - GLFW: windowing - Spdlog: logging the app. + +The others are: +- jemalloc +- ccache +- the gold linker +- OpenGL diff --git a/include/gui.hpp b/include/gui.hpp index 7894976..21778dc 100644 --- a/include/gui.hpp +++ b/include/gui.hpp @@ -9,8 +9,6 @@ namespace gui { public: Gui() = delete; - - ~Gui() noexcept; }; } diff --git a/include/gui_data.hpp b/include/gui_data.hpp index edf1f25..dfe1471 100644 --- a/include/gui_data.hpp +++ b/include/gui_data.hpp @@ -1,13 +1,18 @@ #ifndef BILLYSHEET_GUI_DATA_HPP #define BILLYSHEET_GUI_DATA_HPP +#include "window.hpp" + namespace gui { class GuiData final { private: -// GLFWwindow *window{ nullptr }; + Window &window; public: GuiData() = delete; + + explicit GuiData(Window &wwindow) : window(wwindow) {} + ~GuiData() noexcept = default; }; } diff --git a/src/gui_data.cpp b/src/gui_data.cpp index adbb944..e3411e5 100644 --- a/src/gui_data.cpp +++ b/src/gui_data.cpp @@ -1,5 +1 @@ -// -// Created by postaron on 08/01/2022. -// - #include "gui_data.hpp" diff --git a/src/window.cpp b/src/window.cpp index ccfe9ea..163122e 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -1,8 +1,13 @@ -#include #include "window.hpp" +#include +#include + +static void glfwErrorCallback(int error, const char *message) { + spdlog::error("Error code {}: {}", error, message); +} gui::Window::Window() { - glfwSetErrorCallback(nullptr); + glfwSetErrorCallback(glfwErrorCallback); if (glfwInit() == GLFW_FALSE) { throw std::runtime_error("GLFW init failed."); }