Run input and rendering in systems

This commit is contained in:
2025-04-19 20:19:35 +02:00
parent 077191dc10
commit d643288e56
4 changed files with 55 additions and 17 deletions

View File

@@ -15,13 +15,15 @@ int main()
world.import <Window::WindowModule>();
world.import <Input::InputModule>();
auto window = Window::spawn_window(world);
auto glfw_window = window.get<std::shared_ptr<GLFWwindow>>();
#ifndef NDEBUG
world.import <flecs::stats>();
world.set<flecs::Rest>({});
#endif
world.system<Window::Window, Input::ButtonInput<Input::KeyCode> const>().each(
[](flecs::entity e,
Window::Window,
Input::ButtonInput<Input::KeyCode> const& button_input) {
world.system<Window::Window, Input::ButtonInput<Input::KeyCode> const>("CatchMouse")
.each([](flecs::entity e,
Window::Window,
Input::ButtonInput<Input::KeyCode> const& button_input) {
if (button_input.just_pressed(Input::KeyCode(GLFW_KEY_LEFT_CONTROL))) {
auto glfw_window = e.get<std::shared_ptr<GLFWwindow>>();
auto input_mode = glfwGetInputMode(glfw_window->get(), GLFW_CURSOR);
@@ -34,21 +36,21 @@ int main()
}
});
world.system<Window::Window, Input::ButtonInput<Input::KeyCode> const>().each(
[](flecs::entity e,
Window::Window,
Input::ButtonInput<Input::KeyCode> const& button_input) {
world.system<Window::Window, Input::ButtonInput<Input::KeyCode> const>("CloseOnEsc")
.each([](flecs::entity e,
Window::Window,
Input::ButtonInput<Input::KeyCode> const& button_input) {
if (button_input.just_pressed(Input::KeyCode(GLFW_KEY_ESCAPE))) {
auto glfw_window = e.get<std::shared_ptr<GLFWwindow>>();
glfwSetWindowShouldClose(glfw_window->get(), GLFW_TRUE);
e.world().quit();
}
});
while (glfwWindowShouldClose(glfw_window->get()) == GLFW_FALSE) {
glfwPollEvents();
world.system("PollEvents").kind(flecs::PreUpdate).run([](flecs::iter&) { glfwPollEvents(); });
world.progress();
world.system<std::shared_ptr<GLFWwindow>>("Render")
.kind(flecs::PostUpdate)
.each([](std::shared_ptr<GLFWwindow>& glfw_window) { glfwSwapBuffers(glfw_window.get()); });
glfwSwapBuffers(glfw_window->get());
while (world.progress()) {
}
}

17
src/log/log.cpp Normal file
View File

@@ -0,0 +1,17 @@
#include "log.h"
#include <spdlog/cfg/env.h>
void Log::initialize()
{
spdlog::set_pattern("[%H:%M:%S.%e] [%^%l%$] %v");
#ifndef NDEBUG
spdlog::set_level(spdlog::level::debug);
#else
spdlog::set_level(spdlog::level::warn);
#endif
// Override level when running with environment variable.
spdlog::cfg::load_env_levels();
}

9
src/log/log.h Normal file
View File

@@ -0,0 +1,9 @@
#pragma once
#include <spdlog/spdlog.h>
namespace Log {
void initialize();
} // namespace Log

View File

@@ -16,6 +16,7 @@ static constexpr double MOUSE_SENSITIVITY = 0.15;
static void glfw_error_callback(int error, char const* description);
static void framebuffer_size_callback(GLFWwindow* glfw_window, int width, int height);
static void window_size_callback(GLFWwindow* glfw_window, int width, int height);
static void window_close_callback(GLFWwindow* glfw_window);
static void key_callback(GLFWwindow* glfw_window, int key, int scancode, int action, int mods);
static void mouse_cursor_callback(GLFWwindow* glfw_window, double xpos, double ypos);
@@ -28,6 +29,8 @@ WindowModule::WindowModule(flecs::world& world)
world.component<std::optional<CursorPosition>>();
world.component<LogicalSize>();
world.component<PhysicalSize>();
spawn_window(world);
}
flecs::entity spawn_window(flecs::world& world)
@@ -60,6 +63,7 @@ flecs::entity spawn_window(flecs::world& world)
glfwSetKeyCallback(glfw_window.get(), key_callback);
glfwSetCursorPosCallback(glfw_window.get(), mouse_cursor_callback);
glfwSetWindowSizeCallback(glfw_window.get(), window_size_callback);
glfwSetWindowCloseCallback(glfw_window.get(), window_close_callback);
glfwSetFramebufferSizeCallback(glfw_window.get(), framebuffer_size_callback);
init_glad();
@@ -71,7 +75,7 @@ flecs::entity spawn_window(flecs::world& world)
glfwGetFramebufferSize(glfw_window.get(), &framebuffer_width, &framebuffer_height);
glViewport(0, 0, framebuffer_width, framebuffer_height);
flecs::entity window = world.entity();
flecs::entity window = world.entity("PrimaryWindow");
window.add<Window>();
window.add<Input::ButtonInput<Input::KeyCode>>();
window.add<std::optional<CursorPosition>>();
@@ -97,6 +101,12 @@ void window_size_callback(GLFWwindow* glfw_window, int width, int height)
window.set<LogicalSize>({width, height});
}
void window_close_callback(GLFWwindow* glfw_window)
{
auto* world = static_cast<flecs::world*>(glfwGetWindowUserPointer(glfw_window));
world->quit();
}
void framebuffer_size_callback(GLFWwindow* glfw_window, int width, int height)
{
auto* world = static_cast<flecs::world*>(glfwGetWindowUserPointer(glfw_window));