examples - rework shared into header only
This commit is contained in:
@@ -1,31 +1,16 @@
|
|||||||
#include <cstdint>
|
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include "GLFW/glfw3.h"
|
#include "GLFW/glfw3.h"
|
||||||
#include "shared/logger.h"
|
#include "shared/logger.h"
|
||||||
|
#include "shared/window.h"
|
||||||
|
|
||||||
#include "ctx.h"
|
#include "ctx.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
OatmealUtils::initLogging();
|
OatmealUtils::initLogging();
|
||||||
OatmealUtils::createLogger("window", nullptr);
|
|
||||||
OatmealUtils::createLogger("context", nullptr);
|
OatmealUtils::createLogger("context", nullptr);
|
||||||
|
|
||||||
OatmealUtils::get("window")->info("Initializing GLFW");
|
GLFWwindow *window = OatmealUtils::initWindow("Oatmeal - createContext", 800, 600);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Oatmeal::ctx ctx(window);
|
Oatmeal::ctx ctx(window);
|
||||||
|
|||||||
@@ -1,48 +0,0 @@
|
|||||||
#include "logger.h"
|
|
||||||
#include <memory>
|
|
||||||
#include <spdlog/async_logger.h>
|
|
||||||
#include <spdlog/common.h>
|
|
||||||
#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<spdlog::sink_ptr> sinks{};
|
|
||||||
|
|
||||||
const auto stdoutSink = std::make_shared<spdlog::sinks::stderr_color_sink_mt>();
|
|
||||||
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<spdlog::sinks::basic_file_sink_mt>(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<spdlog::async_logger>(
|
|
||||||
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<logger> get(const char *name) { return spdlog::get(name); }
|
|
||||||
std::shared_ptr<logger> get(std::string name) { return spdlog::get(name); }
|
|
||||||
} // namespace OatmealUtils
|
|
||||||
@@ -1,20 +1,57 @@
|
|||||||
#ifndef OATMEAL_LOGGER
|
#ifndef OATMEAL_UTILS_LOGGER
|
||||||
#define OATMEAL_LOGGER
|
#define OATMEAL_UTILS_LOGGER
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <spdlog/async_logger.h>
|
||||||
|
#include <spdlog/common.h>
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
|
#include "spdlog/async.h"
|
||||||
|
#include "spdlog/sinks/basic_file_sink.h"
|
||||||
|
#include "spdlog/sinks/stdout_color_sinks.h"
|
||||||
|
#include "spdlog/spdlog.h"
|
||||||
#include "string"
|
#include "string"
|
||||||
|
|
||||||
namespace OatmealUtils {
|
namespace OatmealUtils {
|
||||||
|
|
||||||
using logger = spdlog::logger;
|
using logger = spdlog::logger;
|
||||||
|
|
||||||
void initLogging();
|
inline void initLogging() {
|
||||||
void createLogger(const char *name, const char *filename);
|
spdlog::init_thread_pool(8192, 1);
|
||||||
void createLogger(std::string name, std::string filename);
|
spdlog::flush_every(std::chrono::seconds(1));
|
||||||
std::shared_ptr<logger> get(const char *name);
|
}
|
||||||
std::shared_ptr<logger> get(std::string name);
|
|
||||||
|
inline void createLogger(const char *name, const char *filename) {
|
||||||
|
std::vector<spdlog::sink_ptr> sinks{};
|
||||||
|
|
||||||
|
const auto stdoutSink = std::make_shared<spdlog::sinks::stderr_color_sink_mt>();
|
||||||
|
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<spdlog::sinks::basic_file_sink_mt>(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<spdlog::async_logger>(
|
||||||
|
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<logger> get(const char *name) { return spdlog::get(name); }
|
||||||
|
inline std::shared_ptr<logger> get(std::string name) { return spdlog::get(name); }
|
||||||
|
|
||||||
} // namespace OatmealUtils
|
} // namespace OatmealUtils
|
||||||
|
|
||||||
#endif // !OATMEAL_LOGGER
|
#endif // !OATMEAL_UTILS_LOGGER
|
||||||
|
|||||||
24
examples/shared/shared/window.h
Normal file
24
examples/shared/shared/window.h
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#include <cstdint>
|
||||||
|
#include <cstdlib>
|
||||||
|
#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
|
||||||
29
examples/shared/window.h
Normal file
29
examples/shared/window.h
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
#ifndef OATMEAL_UTILS_WINDOW
|
||||||
|
#define OATMEAL_UTILS_WINDOW
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#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
|
||||||
Reference in New Issue
Block a user