diff --git a/examples/createContext/main.cpp b/examples/createContext/main.cpp index a8853a8..bdbfcad 100644 --- a/examples/createContext/main.cpp +++ b/examples/createContext/main.cpp @@ -1,31 +1,16 @@ -#include #include #include #include "GLFW/glfw3.h" #include "shared/logger.h" +#include "shared/window.h" #include "ctx.h" int main() { OatmealUtils::initLogging(); - OatmealUtils::createLogger("window", nullptr); OatmealUtils::createLogger("context", nullptr); - OatmealUtils::get("window")->info("Initializing GLFW"); - glfwInit(); - - OatmealUtils::get("window")->info("Setting window hints"); - glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); - glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); - - OatmealUtils::get("window")->info("Create window"); - GLFWwindow *window = glfwCreateWindow(800, 600, "Oatmeal - Basic GLFW window", nullptr, nullptr); - if (window == nullptr) { - const char *desc; - uint32_t code = glfwGetError(&desc); - OatmealUtils::get("window")->critical("Failed to create window: ({}) {}", code, desc); - exit(EXIT_FAILURE); - } + GLFWwindow *window = OatmealUtils::initWindow("Oatmeal - createContext", 800, 600); try { Oatmeal::ctx ctx(window); diff --git a/examples/shared/shared/logger.cpp b/examples/shared/shared/logger.cpp deleted file mode 100644 index d27ab99..0000000 --- a/examples/shared/shared/logger.cpp +++ /dev/null @@ -1,48 +0,0 @@ -#include "logger.h" -#include -#include -#include -#include "spdlog/async.h" -#include "spdlog/sinks/basic_file_sink.h" -#include "spdlog/sinks/stdout_color_sinks.h" -#include "spdlog/spdlog.h" - -namespace OatmealUtils { - - void initLogging() { - spdlog::init_thread_pool(8192, 1); - spdlog::flush_every(std::chrono::seconds(1)); - } - - void createLogger(const char *name, const char *filename) { - std::vector sinks{}; - - const auto stdoutSink = std::make_shared(); - stdoutSink->set_level(spdlog::level::debug); - stdoutSink->set_pattern("%^[%D %r %z] [%n] [%l] [thread %t] %v%$"); - sinks.emplace_back(stdoutSink); - - if (filename != nullptr) { - const auto fileSink = std::make_shared(filename); - fileSink->set_level(spdlog::level::debug); - fileSink->set_pattern("%^[%D %r %z] [%n] [%l] [thread %t] %v%$"); - sinks.emplace_back(fileSink); - } - - const auto logger = std::make_shared( - name, sinks.begin(), sinks.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block); - - logger->flush_on(spdlog::level::warn); -#ifndef NDEBUG - logger->set_level(spdlog::level::debug); -#else - logger->set_level(spdlog::level::info); -#endif // !NDEBUG - - spdlog::register_logger(logger); - } - - void createLogger(std::string name, std::string filename) { createLogger(name.c_str(), filename.c_str()); } - std::shared_ptr get(const char *name) { return spdlog::get(name); } - std::shared_ptr get(std::string name) { return spdlog::get(name); } -} // namespace OatmealUtils diff --git a/examples/shared/shared/logger.h b/examples/shared/shared/logger.h index 32881a5..ddf5aef 100644 --- a/examples/shared/shared/logger.h +++ b/examples/shared/shared/logger.h @@ -1,20 +1,57 @@ -#ifndef OATMEAL_LOGGER -#define OATMEAL_LOGGER +#ifndef OATMEAL_UTILS_LOGGER +#define OATMEAL_UTILS_LOGGER #include +#include +#include #include +#include "spdlog/async.h" +#include "spdlog/sinks/basic_file_sink.h" +#include "spdlog/sinks/stdout_color_sinks.h" +#include "spdlog/spdlog.h" #include "string" namespace OatmealUtils { using logger = spdlog::logger; - void initLogging(); - void createLogger(const char *name, const char *filename); - void createLogger(std::string name, std::string filename); - std::shared_ptr get(const char *name); - std::shared_ptr get(std::string name); + inline void initLogging() { + spdlog::init_thread_pool(8192, 1); + spdlog::flush_every(std::chrono::seconds(1)); + } + + inline void createLogger(const char *name, const char *filename) { + std::vector sinks{}; + + const auto stdoutSink = std::make_shared(); + stdoutSink->set_level(spdlog::level::debug); + stdoutSink->set_pattern("%^[%D %r %z] [%n] [%l] [thread %t] %v%$"); + sinks.emplace_back(stdoutSink); + + if (filename != nullptr) { + const auto fileSink = std::make_shared(filename); + fileSink->set_level(spdlog::level::debug); + fileSink->set_pattern("%^[%D %r %z] [%n] [%l] [thread %t] %v%$"); + sinks.emplace_back(fileSink); + } + + const auto logger = std::make_shared( + name, sinks.begin(), sinks.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block); + + logger->flush_on(spdlog::level::warn); +#ifndef NDEBUG + logger->set_level(spdlog::level::debug); +#else + logger->set_level(spdlog::level::info); +#endif // !NDEBUG + + spdlog::register_logger(logger); + } + + inline void createLogger(std::string name, std::string filename) { createLogger(name.c_str(), filename.c_str()); } + inline std::shared_ptr get(const char *name) { return spdlog::get(name); } + inline std::shared_ptr get(std::string name) { return spdlog::get(name); } } // namespace OatmealUtils -#endif // !OATMEAL_LOGGER +#endif // !OATMEAL_UTILS_LOGGER diff --git a/examples/shared/shared/window.h b/examples/shared/shared/window.h new file mode 100644 index 0000000..d736510 --- /dev/null +++ b/examples/shared/shared/window.h @@ -0,0 +1,24 @@ +#include +#include +#include "GLFW/glfw3.h" +#include "shared/logger.h" + +namespace OatmealUtils { + inline GLFWwindow *initWindow(const char *title, uint32_t width, uint32_t height) { + OatmealUtils::createLogger("window", nullptr); + OatmealUtils::get("window")->debug("Initializing window backend"); + glfwInit(); + glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); + glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); + OatmealUtils::get("window")->debug("Creating window"); + GLFWwindow *window = glfwCreateWindow(width, height, title, nullptr, nullptr); + if (window == nullptr) { + const char *desc; + uint32_t code = glfwGetError(&desc); + OatmealUtils::get("window")->critical("Failed to create window: ({}) {}", code, desc); + exit(EXIT_FAILURE); + } + + return window; + } +} // namespace OatmealUtils diff --git a/examples/shared/window.h b/examples/shared/window.h new file mode 100644 index 0000000..525ea63 --- /dev/null +++ b/examples/shared/window.h @@ -0,0 +1,29 @@ +#ifndef OATMEAL_UTILS_WINDOW +#define OATMEAL_UTILS_WINDOW + +#include +#include "GLFW/glfw3.h" +#include "shared/logger.h" + +namespace OatmealUtils { + inline GLFWwindow *initWindow(const char *title, uint32_t width, uint32_t height) { + OatmealUtils::initLogging(); + OatmealUtils::createLogger("window", nullptr); + OatmealUtils::get("window")->debug("Initializing window backend"); + glfwInit(); + glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); + glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); + OatmealUtils::get("window")->debug("Creating window"); + GLFWwindow *window = glfwCreateWindow(width, height, title, nullptr, nullptr); + if (window == nullptr) { + const char *desc; + uint32_t code = glfwGetError(&desc); + OatmealUtils::get("window")->critical("Failed to create window: ({}) {}", code, desc); + exit(EXIT_FAILURE); + } + + return window; + } +} // namespace OatmealUtils + +#endif // !OATMEAL_UTILS_WINDOW