diff options
Diffstat (limited to 'source/modules/asura-core/window')
-rw-r--r-- | source/modules/asura-core/window/binding/_window.cpp | 6 | ||||
-rw-r--r-- | source/modules/asura-core/window/window.cpp | 6 | ||||
-rw-r--r-- | source/modules/asura-core/window/window.h | 244 | ||||
-rw-r--r-- | source/modules/asura-core/window/window_impl_sdl.cpp | 240 | ||||
-rw-r--r-- | source/modules/asura-core/window/window_impl_sdl.h | 42 |
5 files changed, 264 insertions, 274 deletions
diff --git a/source/modules/asura-core/window/binding/_window.cpp b/source/modules/asura-core/window/binding/_window.cpp index cff1dd3..7119cd2 100644 --- a/source/modules/asura-core/window/binding/_window.cpp +++ b/source/modules/asura-core/window/binding/_window.cpp @@ -6,10 +6,8 @@ using namespace std; using namespace AEGraphics; using namespace AEImage; -namespace AsuraEngine -{ - namespace Window - { +namespace_begin(AsuraEngine) +namespace_begin(Window) LUAX_REGISTRY(Window) { diff --git a/source/modules/asura-core/window/window.cpp b/source/modules/asura-core/window/window.cpp index 939e974..bbcb949 100644 --- a/source/modules/asura-core/window/window.cpp +++ b/source/modules/asura-core/window/window.cpp @@ -6,10 +6,8 @@ #include "window_impl_glew.h" #include "window_impl_glut.h" -namespace AsuraEngine -{ - namespace Window - { +namespace_begin(AsuraEngine) +namespace_begin(Window) Window::Window() : m_Impl(nullptr) diff --git a/source/modules/asura-core/window/window.h b/source/modules/asura-core/window/window.h index deffc10..872c40f 100644 --- a/source/modules/asura-core/window/window.h +++ b/source/modules/asura-core/window/window.h @@ -9,131 +9,129 @@ #include "../graphics/render_state.h" #include "../graphics/render_target.h" -namespace AsuraEngine +namespace_begin(AsuraEngine) +namespace_begin(Window) + +class WindowImpl; + +/// +/// SDLģһЩõġ +/// +enum WindowFlag +{ + WINDOW_FULLSCREEN = 1 << 1, ///< fullscreen window + WINDOW_OPENGL = 1 << 2, ///< window usable with OpenGL context + WINDOW_SHOWN = 1 << 3, ///< window is visible + WINDOW_HIDDEN = 1 << 4, ///< window is not visible + WINDOW_BORDERLESS = 1 << 5, ///< no window decoration + WINDOW_RESIZABLE = 1 << 6, ///< window can be resized + WINDOW_MINIMIZED = 1 << 7, ///< window is minimized + WINDOW_MAXIMIZED = 1 << 8, ///< window is maximized + WINDOW_INPUT_GRABBED = 1 << 9, ///< window has grabbed input focus + WINDOW_INPUT_FOCUS = 1 << 10, ///< window has input focus + WINDOW_MOUSE_FOCUS = 1 << 11, ///< window has mouse focus + WINDOW_ALLOW_HIGHDPI = 1 << 12, ///< window should be created in high-DPI mode if supported. + ///< On macOS NSHighResolutionCapable must be set true in the + ///< application's Info.plist for this to have any effect. + WINDOW_MOUSE_CAPTURE = 1 << 13, ///< window has mouse captured (unrelated to INPUT_GRABBED) + WINDOW_ALWAYS_ON_TOP = 1 << 14, ///< window should always be above others +}; + +/// Windowʼ +struct WindowConfig +{ + uint width, height; ///< ߴ + int x, y; ///< ڳʼ + std::string title; ///< + bool vsync; ///< ֱͬ + AEImage::ImageData* icon; ///< ͼ + bool show; ///< Ƿʾ + int flag; ///< ڱ +}; + +/// +/// ϷĵڣrunnerֻҪһڡͬĿͻʵִ˽ӿڲֶעᵽlua༭ +/// ᵼ࣬ӵ༭ⴰϡ +/// +class Window ASURA_FINAL + : public AEScripting::Portable<Window, AEGraphics::RenderTarget> + , public Singleton<Window> { - namespace Window - { - - class WindowImpl; - - /// - /// SDLģһЩõġ - /// - enum WindowFlag - { - WINDOW_FULLSCREEN = 1 << 1, ///< fullscreen window - WINDOW_OPENGL = 1 << 2, ///< window usable with OpenGL context - WINDOW_SHOWN = 1 << 3, ///< window is visible - WINDOW_HIDDEN = 1 << 4, ///< window is not visible - WINDOW_BORDERLESS = 1 << 5, ///< no window decoration - WINDOW_RESIZABLE = 1 << 6, ///< window can be resized - WINDOW_MINIMIZED = 1 << 7, ///< window is minimized - WINDOW_MAXIMIZED = 1 << 8, ///< window is maximized - WINDOW_INPUT_GRABBED = 1 << 9, ///< window has grabbed input focus - WINDOW_INPUT_FOCUS = 1 << 10, ///< window has input focus - WINDOW_MOUSE_FOCUS = 1 << 11, ///< window has mouse focus - WINDOW_ALLOW_HIGHDPI = 1 << 12, ///< window should be created in high-DPI mode if supported. - ///< On macOS NSHighResolutionCapable must be set true in the - ///< application's Info.plist for this to have any effect. - WINDOW_MOUSE_CAPTURE = 1 << 13, ///< window has mouse captured (unrelated to INPUT_GRABBED) - WINDOW_ALWAYS_ON_TOP = 1 << 14, ///< window should always be above others - }; - - /// Windowʼ - struct WindowConfig - { - uint width, height; ///< ߴ - int x, y; ///< ڳʼ - std::string title; ///< - bool vsync; ///< ֱͬ - AEImage::ImageData* icon; ///< ͼ - bool show; ///< Ƿʾ - int flag; ///< ڱ - }; - - /// - /// ϷĵڣrunnerֻҪһڡͬĿͻʵִ˽ӿڲֶעᵽlua༭ - /// ᵼ࣬ӵ༭ⴰϡ - /// - class Window ASURA_FINAL - : public AEScripting::Portable<Window, AEGraphics::RenderTarget> - , public Singleton<Window> - { - public: - - /// ϷʱĴΨһģ༭õࡣ - LUAX_DECL_SINGLETON(Window); - - Window(); - ~Window(); - - /// ڡ - bool Init(const WindowConfig& config); - void Exit(); - - void SetSize(uint width, uint height); - void SetPosition(int x, int y); - void SetTitle(const std::string& title); - void SetIcon(AEImage::ImageData* imgData); - - void Show(); - void Hide(); - - /// ǿ˫ĴڣҪչʾǰ̨ - void SwapRenderBuffer(); - - void Clear(const AEGraphics::Color& col = AEGraphics::Color::Black) override; - void Clear(const Math::Recti& quad, const AEGraphics::Color& col = AEGraphics::Color::Black) override; - - void Draw(const AEGraphics::Drawable* texture, const AEGraphics::RenderState& state) override; - void Draw(const AEGraphics::Drawable* texture, const Math::Recti& quad, const AEGraphics::RenderState& state) override; - - private: - - WindowImpl* m_Impl; - - luaxport: - - LUAX_DECL_ENUM(WindowFlag, 0); - - LUAX_DECL_METHOD(_Init); - LUAX_DECL_METHOD(_Exit); - LUAX_DECL_METHOD(_Show); - LUAX_DECL_METHOD(_Hide); - LUAX_DECL_METHOD(_SetSize); - LUAX_DECL_METHOD(_SetPosition); - LUAX_DECL_METHOD(_SetTitle); - LUAX_DECL_METHOD(_SetIcon); - LUAX_DECL_METHOD(_SwapRenderBuffer); - LUAX_DECL_METHOD(_Clear); - LUAX_DECL_METHOD(_Draw); - - }; - - using RenderWindow = Window; - - ASURA_ABSTRACT class WindowImpl - { - public: - - WindowImpl() {}; - virtual ~WindowImpl() {}; - - virtual bool Init(const WindowConfig& config); - - virtual void SetSize(uint width, uint height) = 0; - virtual void SetPosition(int x, int y) = 0; - virtual void SetTitils(const std::string& title) = 0; - - virtual void Show() = 0; - virtual void Hide() = 0; - - virtual void SwapRenderBuffer() = 0; - - }; +public: + + /// ϷʱĴΨһģ༭õࡣ + LUAX_DECL_SINGLETON(Window); + + Window(); + ~Window(); + + /// ڡ + bool Init(const WindowConfig& config); + void Exit(); + + void SetSize(uint width, uint height); + void SetPosition(int x, int y); + void SetTitle(const std::string& title); + void SetIcon(AEImage::ImageData* imgData); + + void Show(); + void Hide(); + + /// ǿ˫ĴڣҪչʾǰ̨ + void SwapRenderBuffer(); + + void Clear(const AEGraphics::Color& col = AEGraphics::Color::Black) override; + void Clear(const Math::Recti& quad, const AEGraphics::Color& col = AEGraphics::Color::Black) override; + + void Draw(const AEGraphics::Drawable* texture, const AEGraphics::RenderState& state) override; + void Draw(const AEGraphics::Drawable* texture, const Math::Recti& quad, const AEGraphics::RenderState& state) override; + +private: + + WindowImpl* m_Impl; + +luaxport: + + LUAX_DECL_ENUM(WindowFlag, 0); + + LUAX_DECL_METHOD(_Init); + LUAX_DECL_METHOD(_Exit); + LUAX_DECL_METHOD(_Show); + LUAX_DECL_METHOD(_Hide); + LUAX_DECL_METHOD(_SetSize); + LUAX_DECL_METHOD(_SetPosition); + LUAX_DECL_METHOD(_SetTitle); + LUAX_DECL_METHOD(_SetIcon); + LUAX_DECL_METHOD(_SwapRenderBuffer); + LUAX_DECL_METHOD(_Clear); + LUAX_DECL_METHOD(_Draw); + +}; + +using RenderWindow = Window; + +ASURA_ABSTRACT class WindowImpl +{ +public: + + WindowImpl() {}; + virtual ~WindowImpl() {}; + + virtual bool Init(const WindowConfig& config); + + virtual void SetSize(uint width, uint height) = 0; + virtual void SetPosition(int x, int y) = 0; + virtual void SetTitils(const std::string& title) = 0; + + virtual void Show() = 0; + virtual void Hide() = 0; + + virtual void SwapRenderBuffer() = 0; + +}; - } -} +namespace_end +namespace_end namespace AEWindow = AsuraEngine::Window; diff --git a/source/modules/asura-core/window/window_impl_sdl.cpp b/source/modules/asura-core/window/window_impl_sdl.cpp index 29a7540..59562a4 100644 --- a/source/modules/asura-core/window/window_impl_sdl.cpp +++ b/source/modules/asura-core/window/window_impl_sdl.cpp @@ -11,145 +11,143 @@ using namespace AEGraphics; using namespace AEImage; -namespace AsuraEngine -{ - namespace Window - { +namespace_begin(AsuraEngine) +namespace_begin(Window) #define asura_flag_to_sdl_flag(flag, _flag, _sdl_flag) \ - if ((flag & _flag) != 0) \ - flag |= _sdl_flag +if ((flag & _flag) != 0) \ + flag |= _sdl_flag - WindowImplSDL::WindowImplSDL() - : m_Wnd(nullptr) - , m_GLContext(0) - { - } +WindowImplSDL::WindowImplSDL() + : m_Wnd(nullptr) + , m_GLContext(0) +{ +} - WindowImplSDL::~WindowImplSDL() - { - SDL_GL_DeleteContext(m_GLContext); - SDL_DestroyWindow(m_Wnd); - SDL_FlushEvent(SDL_WINDOWEVENT); - } +WindowImplSDL::~WindowImplSDL() +{ + SDL_GL_DeleteContext(m_GLContext); + SDL_DestroyWindow(m_Wnd); + SDL_FlushEvent(SDL_WINDOWEVENT); +} - bool WindowImplSDL::Init(const WindowConfig& config) +bool WindowImplSDL::Init(const WindowConfig& config) +{ + if (SDL_Init(SDL_INIT_VIDEO) < 0) + return false; + + int flag = 0; + asura_flag_to_sdl_flag(flag, WINDOW_FULLSCREEN, SDL_WINDOW_FULLSCREEN); + asura_flag_to_sdl_flag(flag, WINDOW_OPENGL, SDL_WINDOW_OPENGL); + asura_flag_to_sdl_flag(flag, WINDOW_SHOWN, SDL_WINDOW_SHOWN); + asura_flag_to_sdl_flag(flag, WINDOW_HIDDEN, SDL_WINDOW_HIDDEN); + asura_flag_to_sdl_flag(flag, WINDOW_BORDERLESS, SDL_WINDOW_BORDERLESS); + asura_flag_to_sdl_flag(flag, WINDOW_RESIZABLE, SDL_WINDOW_RESIZABLE); + asura_flag_to_sdl_flag(flag, WINDOW_MINIMIZED, SDL_WINDOW_MINIMIZED); + asura_flag_to_sdl_flag(flag, WINDOW_MAXIMIZED, SDL_WINDOW_MAXIMIZED); + asura_flag_to_sdl_flag(flag, WINDOW_INPUT_GRABBED, SDL_WINDOW_INPUT_GRABBED); + asura_flag_to_sdl_flag(flag, WINDOW_INPUT_FOCUS, SDL_WINDOW_INPUT_FOCUS); + asura_flag_to_sdl_flag(flag, WINDOW_MOUSE_FOCUS, SDL_WINDOW_MOUSE_FOCUS); + asura_flag_to_sdl_flag(flag, WINDOW_ALLOW_HIGHDPI, SDL_WINDOW_ALLOW_HIGHDPI); + asura_flag_to_sdl_flag(flag, WINDOW_MOUSE_CAPTURE, SDL_WINDOW_MOUSE_CAPTURE); + asura_flag_to_sdl_flag(flag, WINDOW_ALWAYS_ON_TOP, SDL_WINDOW_ALWAYS_ON_TOP); + + // Set GL window / framebuffer attributes. + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); + SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING, 0); + + m_Wnd = SDL_CreateWindow(config.title.c_str(), config.x, config.y, config.width, config.height, flag); + + if (!m_Wnd) + return false; + + // ͼ + try + { + if (config.icon) { - if (SDL_Init(SDL_INIT_VIDEO) < 0) - return false; - - int flag = 0; - asura_flag_to_sdl_flag(flag, WINDOW_FULLSCREEN, SDL_WINDOW_FULLSCREEN); - asura_flag_to_sdl_flag(flag, WINDOW_OPENGL, SDL_WINDOW_OPENGL); - asura_flag_to_sdl_flag(flag, WINDOW_SHOWN, SDL_WINDOW_SHOWN); - asura_flag_to_sdl_flag(flag, WINDOW_HIDDEN, SDL_WINDOW_HIDDEN); - asura_flag_to_sdl_flag(flag, WINDOW_BORDERLESS, SDL_WINDOW_BORDERLESS); - asura_flag_to_sdl_flag(flag, WINDOW_RESIZABLE, SDL_WINDOW_RESIZABLE); - asura_flag_to_sdl_flag(flag, WINDOW_MINIMIZED, SDL_WINDOW_MINIMIZED); - asura_flag_to_sdl_flag(flag, WINDOW_MAXIMIZED, SDL_WINDOW_MAXIMIZED); - asura_flag_to_sdl_flag(flag, WINDOW_INPUT_GRABBED, SDL_WINDOW_INPUT_GRABBED); - asura_flag_to_sdl_flag(flag, WINDOW_INPUT_FOCUS, SDL_WINDOW_INPUT_FOCUS); - asura_flag_to_sdl_flag(flag, WINDOW_MOUSE_FOCUS, SDL_WINDOW_MOUSE_FOCUS); - asura_flag_to_sdl_flag(flag, WINDOW_ALLOW_HIGHDPI, SDL_WINDOW_ALLOW_HIGHDPI); - asura_flag_to_sdl_flag(flag, WINDOW_MOUSE_CAPTURE, SDL_WINDOW_MOUSE_CAPTURE); - asura_flag_to_sdl_flag(flag, WINDOW_ALWAYS_ON_TOP, SDL_WINDOW_ALWAYS_ON_TOP); - - // Set GL window / framebuffer attributes. - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); - SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); - SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); - SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); - SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); - SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); - SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING, 0); - - m_Wnd = SDL_CreateWindow(config.title.c_str(), config.x, config.y, config.width, config.height, flag); - - if (!m_Wnd) - return false; - - // ͼ - try + ImageData* img = config.icon; + if (img->format == COLOR_FORMAT_RGBA8) { - if (config.icon) - { - ImageData* img = config.icon; - if (img->format == COLOR_FORMAT_RGBA8) - { - SDL_Surface *surface; - - img->Lock(); - - int w = img->width, h = img->height; - surface = SDL_CreateRGBSurfaceFrom( - img->pixels, - w, h, - 32, - w * 4, - Color32::RMASK, - Color32::GMASK, - Color32::BMASK, - Color32::AMASK - ); - - img->Unlock(); - - SDL_SetWindowIcon(m_Wnd, surface); - SDL_FreeSurface(surface); - } - } + SDL_Surface *surface; + + img->Lock(); + + int w = img->width, h = img->height; + surface = SDL_CreateRGBSurfaceFrom( + img->pixels, + w, h, + 32, + w * 4, + Color32::RMASK, + Color32::GMASK, + Color32::BMASK, + Color32::AMASK + ); + + img->Unlock(); + + SDL_SetWindowIcon(m_Wnd, surface); + SDL_FreeSurface(surface); } - catch (...) - { - } - - m_GLContext = SDL_GL_CreateContext(m_Wnd); + } + } + catch (...) + { + } - if (!m_GLContext) - { - SDL_DestroyWindow(m_Wnd); - return false; - } + m_GLContext = SDL_GL_CreateContext(m_Wnd); - SDL_GL_MakeCurrent(m_Wnd, m_GLContext); - SDL_GL_SetSwapInterval(config.vsync ? 1 : 0); + if (!m_GLContext) + { + SDL_DestroyWindow(m_Wnd); + return false; + } - return true; - } + SDL_GL_MakeCurrent(m_Wnd, m_GLContext); + SDL_GL_SetSwapInterval(config.vsync ? 1 : 0); - void WindowImplSDL::SetSize(uint width, uint height) - { - SDL_SetWindowSize(m_Wnd, width, height); - } + return true; +} - void WindowImplSDL::SetPosition(int x, int y) - { - SDL_SetWindowPosition(m_Wnd, x, y); - } +void WindowImplSDL::SetSize(uint width, uint height) +{ + SDL_SetWindowSize(m_Wnd, width, height); +} - void WindowImplSDL::SetTitils(const std::string& title) - { - SDL_SetWindowTitle(m_Wnd, title.c_str()); - } +void WindowImplSDL::SetPosition(int x, int y) +{ + SDL_SetWindowPosition(m_Wnd, x, y); +} - void WindowImplSDL::Show() - { - SDL_ShowWindow(m_Wnd); - } +void WindowImplSDL::SetTitils(const std::string& title) +{ + SDL_SetWindowTitle(m_Wnd, title.c_str()); +} - void WindowImplSDL::Hide() - { - SDL_HideWindow(m_Wnd); - } +void WindowImplSDL::Show() +{ + SDL_ShowWindow(m_Wnd); +} - void WindowImplSDL::SwapRenderBuffer() - { - SDL_GL_SwapWindow(m_Wnd); - } +void WindowImplSDL::Hide() +{ + SDL_HideWindow(m_Wnd); +} - } +void WindowImplSDL::SwapRenderBuffer() +{ + SDL_GL_SwapWindow(m_Wnd); } +namespace_end +namespace_end + #endif // ASURA_WINDOW_SDL
\ No newline at end of file diff --git a/source/modules/asura-core/window/window_impl_sdl.h b/source/modules/asura-core/window/window_impl_sdl.h index 1f4ea0e..20f5c95 100644 --- a/source/modules/asura-core/window/window_impl_sdl.h +++ b/source/modules/asura-core/window/window_impl_sdl.h @@ -9,38 +9,36 @@ #include "window.h" -namespace AsuraEngine -{ - namespace Window - { +namespace_begin(AsuraEngine) +namespace_begin(Window) - class WindowImplSDL ASURA_FINAL : public WindowImpl - { - public: +class WindowImplSDL ASURA_FINAL : public WindowImpl +{ +public: - WindowImplSDL(); - ~WindowImplSDL(); + WindowImplSDL(); + ~WindowImplSDL(); - bool Init(const WindowConfig& config); + bool Init(const WindowConfig& config); - void SetSize(uint width, uint height) override; - void SetPosition(int x, int y) override; - void SetTitils(const std::string& title) override; + void SetSize(uint width, uint height) override; + void SetPosition(int x, int y) override; + void SetTitils(const std::string& title) override; - void Show() override; - void Hide() override; + void Show() override; + void Hide() override; - void SwapRenderBuffer() override; + void SwapRenderBuffer() override; - private: +private: - SDL_Window* m_Wnd; - SDL_GLContext m_GLContext; + SDL_Window* m_Wnd; + SDL_GLContext m_GLContext; - }; +}; - } -} +namespace_end +namespace_end #endif // ASURA_WINDOW_SDL |