Extra2D/include/platform/window_module.h

160 lines
3.2 KiB
C
Raw Normal View History

#pragma once
#include <SDL.h>
#include <config/app_config.h>
#include <config/window_config.h>
#include <event/events.h>
#include <functional>
#include <memory>
#include <module/module.h>
#include <module/module_registry.h>
#include <types/math/size.h>
#include <types/math/vec2.h>
namespace extra2d {
/**
* @brief
*/
using ResizeCb = std::function<void(int32 w, int32 h)>;
using CloseCb = std::function<void()>;
/**
* @brief - Vulkan
*
* SDL2 Vulkan
* 使 Module
*/
class WindowModule : public Module {
// 自动注册到模块系统,优先级为 0最先初始化
E2D_REGISTER_MODULE(WindowModule, "Window", 0)
public:
WindowModule();
~WindowModule() override;
// 禁止拷贝
WindowModule(const WindowModule &) = delete;
WindowModule &operator=(const WindowModule &) = delete;
// 允许移动
WindowModule(WindowModule &&) noexcept;
WindowModule &operator=(WindowModule &&) noexcept;
// Module 接口实现
bool init() override;
void shutdown() override;
/**
* @brief 使
*/
bool create(const WindowCfg &cfg);
/**
* @brief
* @return true false 退
*/
bool pollEvents();
/**
* @brief SDL
*/
SDL_Window *handle() const { return window_; }
/**
* @brief
*/
Size getSize() const;
/**
* @brief
*/
Vec2 getPosition() const;
/**
* @brief
*/
void setSize(int32 w, int32 h);
/**
* @brief
*/
void setTitle(const std::string &title);
/**
* @brief
*/
void setFullscreen(bool fullscreen);
/**
* @brief
*/
bool isFullscreen() const;
/**
* @brief /
*/
void setVisible(bool visible);
/**
* @brief
*/
bool isVisible() const;
/**
* @brief
*/
void setOnClose(CloseCb cb) { onClose_ = std::move(cb); }
/**
* @brief
*/
void setOnResize(ResizeCb cb) { onResize_ = std::move(cb); }
/**
* @brief
*/
void requestClose() { shouldClose_ = true; }
/**
* @brief
*/
bool shouldClose() const { return shouldClose_; }
/**
* @brief Vulkan
* @return
*/
std::vector<const char *> getVulkanExtensions() const;
/**
* @brief Vulkan
* @param instance Vulkan
* @param surface
* @return
*/
bool createVulkanSurface(void *instance, void **surface) const;
private:
void handleWindowEvent(const SDL_WindowEvent &evt);
/**
* @brief
* @param config
*
* AppConfig
*/
void onModuleConfig(const AppConfig &config);
SDL_Window *window_ = nullptr;
bool shouldClose_ = false;
CloseCb onClose_;
ResizeCb onResize_;
// 模块配置事件监听器
std::unique_ptr<events::OnModuleConfig<AppConfig>::Listener> configListener_;
};
} // namespace extra2d