Separate files between include and src

This commit is contained in:
Pcornat 2025-05-29 13:29:46 +02:00
commit ac620fb3b7
Signed by: Pcornat
GPG key ID: E0326CC678A00BDD
4 changed files with 3 additions and 3 deletions

View file

@ -54,8 +54,8 @@ elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
set(LINKER_OPTIMIZED_OPTIONS ${LINKER_OPTIMIZED_OPTIONS} -fdevirtualize-at-ltrans) set(LINKER_OPTIMIZED_OPTIONS ${LINKER_OPTIMIZED_OPTIONS} -fdevirtualize-at-ltrans)
endif () endif ()
add_library(${PROJECT_NAME} SHARED window.cpp window.hpp basic_data.hpp) add_library(${PROJECT_NAME} SHARED src/window.cpp include/window.hpp include/basic_data.hpp)
target_precompile_headers(${PROJECT_NAME} PUBLIC window.hpp basic_data.hpp) target_precompile_headers(${PROJECT_NAME} PUBLIC include/window.hpp include/basic_data.hpp)
target_include_directories(${PROJECT_NAME} target_include_directories(${PROJECT_NAME}
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
INTERFACE $<INSTALL_INTERFACE:include/windowGlfwGlLib> INTERFACE $<INSTALL_INTERFACE:include/windowGlfwGlLib>
@ -92,7 +92,7 @@ configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/windowGlfwGlLibTargets INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/windowGlfwGlLibTargets
) )
install(FILES window.hpp basic_data.hpp DESTINATION include/windowGlfwGlLib) install(FILES include/window.hpp include/basic_data.hpp DESTINATION include/windowGlfwGlLib)
install(FILES install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/windowGlfwGlLibTargetsConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/windowGlfwGlLibTargetsConfig.cmake"

33
include/basic_data.hpp Normal file
View file

@ -0,0 +1,33 @@
#ifndef POC2DMODULAR_BASIC_DATA_HPP
#define POC2DMODULAR_BASIC_DATA_HPP
#include <string>
#include <utility>
namespace data {
class BasicData {
public:
const std::string name{ "Basic" };
BasicData() = delete;
explicit BasicData(std::string name) : name(std::move(name)) {}
virtual ~BasicData() noexcept = default;
virtual void key_callback(int key, int scancode, int action, int mods) = 0;
virtual void cursor_position_callback(double xpos, double ypos) = 0;
virtual void scroll_callback(double xoffset, double yoffset) = 0;
virtual void window_pos(int xpos, int ypos) = 0;
virtual void window_size(int width, int height) = 0;
};
}
#endif //POC2DMODULAR_BASIC_DATA_HPP

89
include/window.hpp Normal file
View file

@ -0,0 +1,89 @@
#ifndef POC2DMODULAR_WINDOW_HPP
#define POC2DMODULAR_WINDOW_HPP
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <GLFW/glfw3.h>
namespace data {
class BasicData;
}
namespace gui {
class Window final {
public:
using modulesType = std::unordered_map<std::string, data::BasicData *>;
using windowPtr = GLFWwindow *;
private:
static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods);
static void cursor_position_callback(GLFWwindow *window, double xpos, double ypos);
static void scroll_callback(GLFWwindow *window, double xoffset, double yoffset);
static void window_size_callback(GLFWwindow *window, int width, int height);
static void window_pos_callback(GLFWwindow *window, int xpos, int ypos);
static void delete_glfw_window(GLFWwindow *glfWwindow) {
glfwDestroyWindow(glfWwindow);
glfwTerminate();
}
static std::uint_fast8_t count_instance;
std::unique_ptr<GLFWwindow, decltype(&delete_glfw_window)> wwindow{ nullptr, delete_glfw_window };
modulesType modules;
explicit Window(const bool debugOpengl,
const GLFWframebuffersizefun framebufferCallback,
GLFWwindow *shared,
std::initializer_list<modulesType::value_type> initializer);
public:
[[nodiscard]] static bool init_glfw(GLFWerrorfun errorCallback) noexcept;
Window() noexcept = delete;
Window(Window &&window) noexcept = default;
[[nodiscard]] friend std::optional<Window> create_window(
const GLFWerrorfun errorCallback,
GLFWframebuffersizefun framebufferCallback,
const bool debugOpengl,
GLFWwindow *shared,
std::initializer_list<Window::modulesType::value_type> initializer) noexcept;
~Window() noexcept;
[[nodiscard]] const std::unique_ptr<GLFWwindow, decltype(&delete_glfw_window)> &get_window() const noexcept;
[[nodiscard]] bool should_close() const noexcept;
void swap_buffers() const noexcept;
void add_module(data::BasicData &module);
void delete_module(const std::string &module);
/**
* \brief Make window's OpenGL context current
*/
void get_context() const noexcept;
Window &operator=(Window &&other) noexcept = default;
};
void color_10_bits() noexcept;
bool check_color_depth(int color_depth) noexcept;
void opengl_debug() noexcept;
}
#endif //POC2DMODULAR_WINDOW_HPP

157
src/window.cpp Normal file
View file

@ -0,0 +1,157 @@
#include "window.hpp"
#include <algorithm>
#include <GL/glew.h>
static void internalFramebufferCallback([[maybe_unused]] GLFWwindow *glfWwindow, const int width, const int height) {
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
}
namespace gui {
static bool init = false;
std::uint_fast8_t Window::count_instance = 0;
Window::Window(const bool debugOpengl,
const GLFWframebuffersizefun framebufferCallback,
GLFWwindow *shared,
const std::initializer_list<modulesType::value_type> initializer) : modules(initializer) {
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, debugOpengl ? GLFW_TRUE : GLFW_FALSE);
wwindow = std::unique_ptr<GLFWwindow, decltype(&delete_glfw_window)>(
glfwCreateWindow(720, 1280, "Billy Sheet tracker", nullptr, shared),
delete_glfw_window
);
if (!wwindow) {
glfwTerminate();
init = false;
count_instance = 0;
return;
}
glfwMakeContextCurrent(wwindow.get());
glewExperimental = true;
if (glewInit() != GLEW_OK) {
wwindow.reset(nullptr);
glfwTerminate();
init = false;
count_instance = 0;
return;
}
++count_instance;
glfwSetFramebufferSizeCallback(wwindow.get(),
framebufferCallback != nullptr ? framebufferCallback : internalFramebufferCallback);
glfwSwapInterval(1); // VSync on
glfwSetKeyCallback(wwindow.get(), key_callback);
glfwSetScrollCallback(wwindow.get(), scroll_callback);
glfwSetCursorPosCallback(wwindow.get(), cursor_position_callback);
glfwSetWindowSizeCallback(wwindow.get(), window_size_callback);
glfwSetWindowPosCallback(wwindow.get(), window_pos_callback);
glfwSetWindowUserPointer(wwindow.get(), &modules);
}
Window::~Window() noexcept {
if (count_instance > 0) {
--count_instance;
}
}
const std::unique_ptr<GLFWwindow, decltype(&Window::delete_glfw_window)> &Window::get_window() const noexcept {
return wwindow;
}
bool Window::should_close() const noexcept {
return glfwWindowShouldClose(wwindow.get());
}
void Window::swap_buffers() const noexcept {
glfwSwapBuffers(wwindow.get());
}
void Window::key_callback(GLFWwindow *window, const int key, const int scancode, const int action, const int mods) {
auto *modules = static_cast<modulesType *>(glfwGetWindowUserPointer(window));
std::ranges::for_each(*modules,
[=](modulesType::reference item) { item.second->key_callback(key, scancode, action, mods); });
}
void Window::cursor_position_callback(GLFWwindow *window, const double xpos, const double ypos) {
auto *modules = static_cast<modulesType *>(glfwGetWindowUserPointer(window));
std::ranges::for_each(*modules,
[=](modulesType::reference item) { item.second->cursor_position_callback(xpos, ypos); });
}
void Window::scroll_callback(GLFWwindow *window, const double xoffset, const double yoffset) {
auto *modules = static_cast<modulesType *>(glfwGetWindowUserPointer(window));
std::ranges::for_each(*modules,
[=](modulesType::reference item) { item.second->scroll_callback(xoffset, yoffset); });
}
void Window::window_size_callback(GLFWwindow *window, const int width, const int height) {
auto *modules = static_cast<modulesType *>(glfwGetWindowUserPointer(window));
std::ranges::for_each(*modules,
[=](modulesType::reference item) { item.second->window_size(width, height); });
}
void Window::window_pos_callback(GLFWwindow *window, const int xpos, const int ypos) {
auto *modules = static_cast<modulesType *>(glfwGetWindowUserPointer(window));
std::ranges::for_each(*modules,
[=](modulesType::reference item) { item.second->window_pos(xpos, ypos); });
}
void Window::add_module(data::BasicData &module) {
modules.emplace(module.name, &module);
}
void Window::delete_module(const std::string &module) {
modules.erase(module);
}
void Window::get_context() const noexcept {
glfwMakeContextCurrent(wwindow.get());
}
std::optional<Window> create_window(
const GLFWerrorfun errorCallback,
const GLFWframebuffersizefun framebufferCallback,
const bool debugOpengl,
GLFWwindow *shared,
const std::initializer_list<Window::modulesType::value_type> initializer
) noexcept {
if (!init) {
glfwSetErrorCallback(errorCallback);
if (glfwInit() == GLFW_FALSE) {
return std::nullopt;
}
init = true;
}
if (Window win{ debugOpengl, framebufferCallback, shared, initializer }; win.get_window()) {
return win;
}
return std::nullopt;
}
bool Window::init_glfw(const GLFWerrorfun errorCallback) noexcept {
glfwSetErrorCallback(errorCallback);
init = glfwInit() == GLFW_TRUE;
return init;
}
void color_10_bits() noexcept {
glfwWindowHint(GLFW_RED_BITS, 10);
glfwWindowHint(GLFW_GREEN_BITS, 10);
glfwWindowHint(GLFW_BLUE_BITS, 10);
}
bool check_color_depth(const int color_depth) noexcept {
GLint redBits = 0, greenBits = 0, blueBits = 0;
glGetIntegerv(GL_RED_BITS, &redBits);
glGetIntegerv(GL_GREEN_BITS, &greenBits);
glGetIntegerv(GL_BLUE_BITS, &blueBits);
return redBits == color_depth && greenBits == color_depth && blueBits == color_depth;
}
void opengl_debug() noexcept {
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE);
}
}