Compare commits

...

2 Commits

Author SHA1 Message Date
14b4e60d3a examples - comments
Some checks failed
Build / Build (push) Failing after 2m8s
2026-04-12 23:42:15 -04:00
fd35e93831 examples - rework shared into header only 2026-04-12 23:26:50 -04:00
7 changed files with 145 additions and 85 deletions

View File

@@ -1,33 +1,55 @@
// basicGLFWWindow
// While I've got a basic initWindow() function in shared/window.h, I mostly just wanted to give an example of how to
// create a window with glfw without it. You can always figure out from the window.h file as well, but it is nice and
// easy to just look here instead, as you're probably already here anyways.
// Oatmeal does ship with GLFW linked as public, so you should be able to just link with it in your CMakeLists.txt file
// without having to add it yourself.
// - Firewire
#include <cstdint>
#include <cstdlib>
#include "GLFW/glfw3.h"
#include "shared/logger.h"
int main() {
// Init and create the logger
OatmealUtils::initLogging();
OatmealUtils::createLogger("window", nullptr);
OatmealUtils::get("window")->info("Initializing GLFW");
// Initialize the GLFW backend
OatmealUtils::getLogger("window")->info("Initializing GLFW");
glfwInit();
OatmealUtils::get("window")->info("Setting window hints");
// GLFW works on hints for configuration.
// Since oatmeal is entirely vulkan based and GLFW creates an opengl context by default, we need to tell it not to.
// At the time of writing (very very early indev) there is no resizable window support, so thats turned off here as
// well.
OatmealUtils::getLogger("window")->info("Setting window hints");
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
OatmealUtils::get("window")->info("Create window");
// Create the window, running a small error check just in case it failed for whatever reason; and if it did, grab
// the error from glfw and print it out.
OatmealUtils::getLogger("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);
OatmealUtils::getLogger("window")->critical("Failed to create window: ({}) {}", code, desc);
exit(EXIT_FAILURE);
}
// This here is whats known as the main application loop. Everything with graphics/games/whatever runs in a loop,
// updating values as needed before it starts over again. The time it takes to run that loop is known at the
// frametime, and how many times you can run that loop per second is the FPS (sorta)
// Since we are just creating a window and have no way of updating it yet, we can just poll for any events and
// continue.
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
}
OatmealUtils::get("window")->info("Cleaning up");
// Cleanup the window and terminate the glfw backend
OatmealUtils::getLogger("window")->info("Cleaning up");
glfwDestroyWindow(window);
glfwTerminate();

View File

@@ -1,36 +1,26 @@
#include <cstdint>
// createContext
// Still in progress, although I'm 95% sure this wont get changed.
// Come back later!
// - Firewire
#include <cstdlib>
#include <exception>
#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);
} catch (const std::exception &e) {
OatmealUtils::get("context")->critical("{}", e.what());
OatmealUtils::getLogger("context")->critical("{}", e.what());
return EXIT_FAILURE;
}
@@ -39,7 +29,7 @@ int main() {
glfwPollEvents();
}
OatmealUtils::get("window")->info("Cleaning up");
OatmealUtils::getLogger("window")->info("Cleaning up");
glfwDestroyWindow(window);
glfwTerminate();

View File

@@ -1,3 +1,9 @@
// Logging
// This program is just to test out/show any new features I add to the shared/logging.h file.
// It is mostly a wrapper around spdlog, nothing fancy at all.
// The code should be self explanatory enough so I'm not going to bother commenting everything out.
// - Firewire
#include <cstdint>
#include "shared/logger.h"
@@ -5,13 +11,13 @@ int main() {
OatmealUtils::initLogging();
OatmealUtils::createLogger("logger", nullptr);
OatmealUtils::get("logger")->debug("This is a debug message!");
OatmealUtils::get("logger")->info("This is an info message!");
OatmealUtils::get("logger")->warn("This is a warning message!");
OatmealUtils::get("logger")->critical("This is a critical message!");
OatmealUtils::getLogger("logger")->debug("This is a debug message!");
OatmealUtils::getLogger("logger")->info("This is an info message!");
OatmealUtils::getLogger("logger")->warn("This is a warning message!");
OatmealUtils::getLogger("logger")->critical("This is a critical message!");
uint32_t a = 5;
std::string b = "Test string arg!";
OatmealUtils::get("logger")->info("String arg: {} uint32_t args: {}", b, a);
OatmealUtils::getLogger("logger")->info("String arg: {} uint32_t args: {}", b, a);
return 0;
}

View File

@@ -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

View File

@@ -1,20 +1,57 @@
#ifndef OATMEAL_LOGGER
#define OATMEAL_LOGGER
#ifndef OATMEAL_UTILS_LOGGER
#define OATMEAL_UTILS_LOGGER
#include <memory>
#include <spdlog/async_logger.h>
#include <spdlog/common.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"
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<logger> get(const char *name);
std::shared_ptr<logger> 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<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> getLogger(const char *name) { return spdlog::get(name); }
inline std::shared_ptr<logger> getLogger(std::string name) { return spdlog::get(name); }
} // namespace OatmealUtils
#endif // !OATMEAL_LOGGER
#endif // !OATMEAL_UTILS_LOGGER

View 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
View 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