Start ECSsing the window
This commit is contained in:
@@ -58,7 +58,7 @@ void Controller::update()
|
||||
{
|
||||
Flycam::keyboard_movement(registry());
|
||||
|
||||
if (registry().ctx().get<Window::MouseCatched>().catched) {
|
||||
if (registry().ctx().get<WindowDeprecated::MouseCatched>().catched) {
|
||||
Flycam::mouse_orientation(registry());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,15 +7,15 @@
|
||||
|
||||
int main()
|
||||
{
|
||||
glfwInit();
|
||||
|
||||
flecs::world world;
|
||||
flecs::entity window_entity = world.entity();
|
||||
window_entity.add<Input::State<Input::KeyCode>>();
|
||||
Window window(&world, window_entity);
|
||||
|
||||
world.import<WindowModule>();
|
||||
spawn_window(world);
|
||||
auto window = world.singleton<Window>();
|
||||
auto glfw_window = window.get<std::shared_ptr<GLFWwindow>>();
|
||||
|
||||
window_entity.observe<Input::KeyInput>([window_entity](Input::KeyInput& key_input) {
|
||||
auto* key_state = window_entity.get_mut<Input::State<Input::KeyCode>>();
|
||||
window.observe<Input::KeyInput>([window](Input::KeyInput& key_input) {
|
||||
auto* key_state = window.get_mut<Input::State<Input::KeyCode>>();
|
||||
|
||||
if (key_input.action == static_cast<Input::Action>(GLFW_PRESS)) {
|
||||
key_state->press(key_input.key_code);
|
||||
@@ -24,15 +24,15 @@ int main()
|
||||
}
|
||||
});
|
||||
|
||||
while (glfwWindowShouldClose(&window.handle()) == GLFW_FALSE) {
|
||||
while (glfwWindowShouldClose(glfw_window->get()) == GLFW_FALSE) {
|
||||
glfwPollEvents();
|
||||
|
||||
// X just pressed
|
||||
auto* keycode= window_entity.get<Input::State<Input::KeyCode>>();
|
||||
auto* keycode= window.get<Input::State<Input::KeyCode>>();
|
||||
if (keycode->just_pressed(Input::KeyCode{GLFW_KEY_X}))
|
||||
std::cout << "X was just pressed!\n";
|
||||
|
||||
Input::State<Input::KeyCode>::update_state(window_entity);
|
||||
glfwSwapBuffers(&window.handle());
|
||||
Input::State<Input::KeyCode>::update_state(window);
|
||||
glfwSwapBuffers(glfw_window->get());
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@
|
||||
#include <memory>
|
||||
|
||||
class Camera;
|
||||
class Window;
|
||||
class WindowDeprecated;
|
||||
|
||||
namespace FeverCore {
|
||||
|
||||
@@ -40,7 +40,7 @@ protected:
|
||||
virtual void register_context_variables();
|
||||
void recreate_framebuffer();
|
||||
|
||||
std::shared_ptr<Window> game_window;
|
||||
std::shared_ptr<WindowDeprecated> game_window;
|
||||
|
||||
Shader post_processing_shader{"post_processing", "data/shaders"};
|
||||
Framebuffer post_processing_framebuffer;
|
||||
|
||||
@@ -32,7 +32,7 @@ auto Camera::front_vector(GlobalTransform const& transform) -> glm::vec3
|
||||
|
||||
void Camera::aspect_ratio_update(entt::registry& registry)
|
||||
{
|
||||
float aspect_ratio = registry.ctx().get<Window::Descriptor>().aspect_ratio;
|
||||
float aspect_ratio = registry.ctx().get<WindowDeprecated::Descriptor>().aspect_ratio;
|
||||
|
||||
auto camera_view = registry.view<Camera>();
|
||||
|
||||
|
||||
@@ -11,8 +11,19 @@
|
||||
static constexpr unsigned INIT_WINDOW_WIDTH = 1280;
|
||||
static constexpr unsigned INIT_WINDOW_HEIGHT = 720;
|
||||
|
||||
Window::Window(flecs::world* world, flecs::entity window_entity) :
|
||||
world(world), window_entity(window_entity)
|
||||
static void glfw_error_callback(int error, char const* description);
|
||||
static void framebuffer_size_callback(GLFWwindow* glfw_window, int width, int height);
|
||||
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);
|
||||
|
||||
WindowModule::WindowModule(flecs::world& world)
|
||||
{
|
||||
glfwInit();
|
||||
world.component<Window>();
|
||||
world.component<std::shared_ptr<GLFWwindow>>();
|
||||
}
|
||||
|
||||
void spawn_window(flecs::world& world)
|
||||
{
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
@@ -26,7 +37,7 @@ Window::Window(flecs::world* world, flecs::entity window_entity) :
|
||||
glfwWindowHint(GLFW_MAXIMIZED, GLFW_TRUE);
|
||||
#endif
|
||||
|
||||
glfw_window = std::shared_ptr<GLFWwindow>(
|
||||
auto glfw_window = std::shared_ptr<GLFWwindow>(
|
||||
glfwCreateWindow(INIT_WINDOW_WIDTH, INIT_WINDOW_HEIGHT, "OpenGL", nullptr, nullptr),
|
||||
[](GLFWwindow* window) { glfwDestroyWindow(window); });
|
||||
|
||||
@@ -38,48 +49,50 @@ Window::Window(flecs::world* world, flecs::entity window_entity) :
|
||||
glfwMakeContextCurrent(glfw_window.get());
|
||||
|
||||
// Callbacks
|
||||
glfwSetWindowUserPointer(glfw_window.get(), this);
|
||||
glfwSetWindowUserPointer(glfw_window.get(), &world);
|
||||
glfwSetKeyCallback(glfw_window.get(), key_callback);
|
||||
glfwSetCursorPosCallback(glfw_window.get(), mouse_cursor_callback);
|
||||
glfwSetFramebufferSizeCallback(glfw_window.get(), framebuffer_size_callback);
|
||||
|
||||
init_glad();
|
||||
|
||||
{
|
||||
int width, height;
|
||||
glfwGetFramebufferSize(glfw_window.get(), &width, &height);
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
int width, height;
|
||||
glfwGetFramebufferSize(glfw_window.get(), &width, &height);
|
||||
glViewport(0, 0, width, height);
|
||||
|
||||
flecs::entity window = world.singleton<Window>();
|
||||
window.add<Input::State<Input::KeyCode>>();
|
||||
window.set<std::shared_ptr<GLFWwindow>>(glfw_window);
|
||||
}
|
||||
|
||||
void Window::glfw_error_callback(int error, char const* description)
|
||||
static void glfw_error_callback(int error, char const* description)
|
||||
{
|
||||
spdlog::warn("GLFW [{:d}]: {:s}\n", error, description);
|
||||
}
|
||||
|
||||
void Window::framebuffer_size_callback(GLFWwindow* glfw_window, int width, int height)
|
||||
void framebuffer_size_callback(GLFWwindow* glfw_window, int width, int height)
|
||||
{
|
||||
auto& window = *static_cast<Window*>(glfwGetWindowUserPointer(glfw_window));
|
||||
glViewport(0, 0, width, height);
|
||||
// auto& window = *static_cast<WindowDeprecated*>(glfwGetWindowUserPointer(glfw_window));
|
||||
// glViewport(0, 0, width, height);
|
||||
|
||||
// world->event<ResizeEvent>().emit();
|
||||
// window.event_dispatcher.enqueue<ResizeEvent>();
|
||||
}
|
||||
|
||||
void Window::key_callback(GLFWwindow* glfw_window,
|
||||
int key,
|
||||
[[maybe_unused]] int scancode,
|
||||
int action,
|
||||
[[maybe_unused]] int mods)
|
||||
void key_callback(GLFWwindow* glfw_window,
|
||||
int key,
|
||||
[[maybe_unused]] int scancode,
|
||||
int action,
|
||||
[[maybe_unused]] int mods)
|
||||
{
|
||||
auto& window = *static_cast<Window*>(glfwGetWindowUserPointer(glfw_window));
|
||||
auto* world = static_cast<flecs::world*>(glfwGetWindowUserPointer(glfw_window));
|
||||
flecs::entity window = world->component<Window>();
|
||||
|
||||
window.window_entity.emit<Input::KeyInput>(
|
||||
Input::KeyInput{.key_code = static_cast<Input::KeyCode>(key),
|
||||
.action = static_cast<Input::Action>(action)});
|
||||
window.emit<Input::KeyInput>(Input::KeyInput{.key_code = static_cast<Input::KeyCode>(key),
|
||||
.action = static_cast<Input::Action>(action)});
|
||||
}
|
||||
|
||||
void Window::mouse_cursor_callback(GLFWwindow* glfw_window, double xpos, double ypos)
|
||||
void mouse_cursor_callback(GLFWwindow* glfw_window, double xpos, double ypos)
|
||||
{
|
||||
// auto* world = static_cast<flecs::world*>(glfwGetWindowUserPointer(glfw_window));
|
||||
|
||||
@@ -99,7 +112,7 @@ void Window::mouse_cursor_callback(GLFWwindow* glfw_window, double xpos, double
|
||||
// window.event_dispatcher.enqueue<Input::MouseMotion>(Input::MouseMotion{.delta = delta});
|
||||
}
|
||||
|
||||
auto Window::logical_dimensions() const -> glm::u32vec2
|
||||
auto WindowDeprecated::logical_dimensions() const -> glm::u32vec2
|
||||
{
|
||||
int width{};
|
||||
int height{};
|
||||
@@ -107,7 +120,7 @@ auto Window::logical_dimensions() const -> glm::u32vec2
|
||||
return {width, height};
|
||||
}
|
||||
|
||||
auto Window::physical_dimensions() const -> glm::u32vec2
|
||||
auto WindowDeprecated::physical_dimensions() const -> glm::u32vec2
|
||||
{
|
||||
int width{};
|
||||
int height{};
|
||||
@@ -115,7 +128,7 @@ auto Window::physical_dimensions() const -> glm::u32vec2
|
||||
return {width, height};
|
||||
}
|
||||
|
||||
void Window::mouse_catching(entt::registry& registry) const
|
||||
void WindowDeprecated::mouse_catching(entt::registry& registry) const
|
||||
{
|
||||
auto& mouse_catched = registry.ctx().emplace<MouseCatched>(MouseCatched{.catched = false});
|
||||
auto const& key_state = registry.ctx().get<Input::State<Input::KeyCode>>();
|
||||
@@ -129,7 +142,7 @@ void Window::mouse_catching(entt::registry& registry) const
|
||||
}
|
||||
}
|
||||
|
||||
void Window::close_on_esc(entt::registry& registry) const
|
||||
void WindowDeprecated::close_on_esc(entt::registry& registry) const
|
||||
{
|
||||
auto const& key_state = registry.ctx().get<Input::State<Input::KeyCode>>();
|
||||
|
||||
@@ -138,7 +151,7 @@ void Window::close_on_esc(entt::registry& registry) const
|
||||
}
|
||||
}
|
||||
|
||||
void Window::update_descriptor(entt::registry& registry) const
|
||||
void WindowDeprecated::update_descriptor(entt::registry& registry) const
|
||||
{
|
||||
auto dimensions = logical_dimensions();
|
||||
|
||||
|
||||
@@ -3,11 +3,23 @@
|
||||
#include <entt/entt.hpp>
|
||||
#include <flecs.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <input/input.h>
|
||||
#include <memory>
|
||||
|
||||
class GLFWwindow;
|
||||
|
||||
class Window
|
||||
void glfw_init();
|
||||
|
||||
struct Window{};
|
||||
|
||||
struct WindowModule
|
||||
{
|
||||
WindowModule(flecs::world& world);
|
||||
};
|
||||
|
||||
void spawn_window(flecs::world& world);
|
||||
|
||||
class WindowDeprecated
|
||||
{
|
||||
public:
|
||||
struct MouseCatched
|
||||
@@ -24,8 +36,6 @@ public:
|
||||
struct ResizeEvent
|
||||
{};
|
||||
|
||||
Window(flecs::world* world, flecs::entity window_entity); // TODO: not like this...
|
||||
|
||||
[[nodiscard]] auto handle() -> GLFWwindow& { return *glfw_window; }
|
||||
[[nodiscard]] auto physical_dimensions() const -> glm::u32vec2;
|
||||
[[nodiscard]] auto logical_dimensions() const -> glm::u32vec2;
|
||||
@@ -35,11 +45,6 @@ public:
|
||||
void close_on_esc(entt::registry& registry) const;
|
||||
|
||||
private:
|
||||
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);
|
||||
static void framebuffer_size_callback(GLFWwindow* glfw_window, int width, int height);
|
||||
static void glfw_error_callback(int error, char const* description);
|
||||
|
||||
static constexpr float MOUSE_SENSITIVITY = 0.15F;
|
||||
|
||||
flecs::world* world;
|
||||
|
||||
Reference in New Issue
Block a user