Extra2D/include/platform/window.h

169 lines
3.4 KiB
C
Raw Normal View History

#pragma once
#include <SDL.h>
#include <functional>
#include <module/imodule.h>
#include <string>
#include <types/base/types.h>
#include <types/const/priority.h>
#include <types/math/size.h>
#include <types/math/vec2.h>
namespace extra2d {
/**
* @brief
*/
struct WindowCfg {
std::string title = "Extra2D";
int32 width = 1280;
int32 height = 720;
bool fullscreen = false;
bool resizable = true;
bool vsync = true;
int32 glMajor = 3;
int32 glMinor = 3;
};
/**
* @brief
*/
using ResizeCb = std::function<void(int32 w, int32 h)>;
using CloseCb = std::function<void()>;
/**
* @brief
*
* SDL2 OpenGL
* Context
*/
class WindowModule : public IModule {
public:
WindowModule();
~WindowModule() override;
// 禁止拷贝
WindowModule(const WindowModule&) = delete;
WindowModule& operator=(const WindowModule&) = delete;
// 允许移动
WindowModule(WindowModule&&) noexcept;
WindowModule& operator=(WindowModule&&) noexcept;
// IModule 接口实现
const char* name() const override { return "Window"; }
ModuleType type() const override { return ModuleType::System; }
int priority() const override { return Pri::Window; }
bool init() override;
void shutdown() override;
/**
* @brief 使
*/
bool create(const WindowCfg& cfg);
/**
* @brief
* @return true false 退
*/
bool pollEvents();
/**
* @brief
*/
void swapBuffers();
/**
* @brief SDL
*/
SDL_Window* handle() const { return window_; }
/**
* @brief OpenGL
*/
SDL_GLContext glContext() const { return glCtx_; }
/**
* @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 setVsync(bool vsync);
/**
* @brief
*/
bool isVsync() 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_; }
private:
void handleWindowEvent(const SDL_WindowEvent& evt);
SDL_Window* window_ = nullptr;
SDL_GLContext glCtx_ = nullptr;
bool shouldClose_ = false;
bool vsync_ = true;
CloseCb onClose_;
ResizeCb onResize_;
};
} // namespace extra2d