diff --git a/apps/flecs-test/main.cpp b/apps/flecs-test/main.cpp index 0353e1c..59b3735 100644 --- a/apps/flecs-test/main.cpp +++ b/apps/flecs-test/main.cpp @@ -15,13 +15,15 @@ int main() world.import (); world.import (); - auto window = Window::spawn_window(world); - auto glfw_window = window.get>(); +#ifndef NDEBUG + world.import (); + world.set({}); +#endif - world.system const>().each( - [](flecs::entity e, - Window::Window, - Input::ButtonInput const& button_input) { + world.system const>("CatchMouse") + .each([](flecs::entity e, + Window::Window, + Input::ButtonInput const& button_input) { if (button_input.just_pressed(Input::KeyCode(GLFW_KEY_LEFT_CONTROL))) { auto glfw_window = e.get>(); auto input_mode = glfwGetInputMode(glfw_window->get(), GLFW_CURSOR); @@ -34,21 +36,21 @@ int main() } }); - world.system const>().each( - [](flecs::entity e, - Window::Window, - Input::ButtonInput const& button_input) { + world.system const>("CloseOnEsc") + .each([](flecs::entity e, + Window::Window, + Input::ButtonInput const& button_input) { if (button_input.just_pressed(Input::KeyCode(GLFW_KEY_ESCAPE))) { - auto glfw_window = e.get>(); - 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>("Render") + .kind(flecs::PostUpdate) + .each([](std::shared_ptr& glfw_window) { glfwSwapBuffers(glfw_window.get()); }); - glfwSwapBuffers(glfw_window->get()); + while (world.progress()) { } } diff --git a/src/log/log.cpp b/src/log/log.cpp new file mode 100644 index 0000000..739da3c --- /dev/null +++ b/src/log/log.cpp @@ -0,0 +1,17 @@ +#include "log.h" + +#include + +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(); +} diff --git a/src/log/log.h b/src/log/log.h new file mode 100644 index 0000000..525d256 --- /dev/null +++ b/src/log/log.h @@ -0,0 +1,9 @@ +#pragma once + +#include + +namespace Log { + +void initialize(); + +} // namespace Log diff --git a/src/window/window.cpp b/src/window/window.cpp index ddf0808..66ed7f1 100644 --- a/src/window/window.cpp +++ b/src/window/window.cpp @@ -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>(); world.component(); world.component(); + + 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.add>(); window.add>(); @@ -97,6 +101,12 @@ void window_size_callback(GLFWwindow* glfw_window, int width, int height) window.set({width, height}); } +void window_close_callback(GLFWwindow* glfw_window) +{ + auto* world = static_cast(glfwGetWindowUserPointer(glfw_window)); + world->quit(); +} + void framebuffer_size_callback(GLFWwindow* glfw_window, int width, int height) { auto* world = static_cast(glfwGetWindowUserPointer(glfw_window));