Extra2D/include/platform/window.h

162 lines
2.8 KiB
C
Raw Normal View History

#pragma once
#include <SDL.h>
#include <core/service.h>
#include <string>
#include <types/base/types.h>
#include <types/math/size.h>
#include <types/math/vec2.h>
#include <types/ptr/ref_counted.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 = Fn<void(int32 w, int32 h)>;
using CloseCb = Fn<void()>;
/**
* @brief
*
* SDL2 OpenGL
*/
class WindowSvc : public IService {
public:
/**
* @brief
*/
static WindowSvc &inst();
const char *name() const override { return "WindowSvc"; }
int pri() const override { return Pri::System; }
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:
WindowSvc() = default;
SDL_Window *window_ = nullptr;
SDL_GLContext glCtx_ = nullptr;
bool shouldClose_ = false;
bool vsync_ = true;
CloseCb onClose_;
ResizeCb onResize_;
};
#define WINDOW extra2d::WindowSvc::inst()
} // namespace extra2d