summaryrefslogtreecommitdiff
path: root/Source/modules/asura-core/Window/WindowImplSDL.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Source/modules/asura-core/Window/WindowImplSDL.cpp')
-rw-r--r--Source/modules/asura-core/Window/WindowImplSDL.cpp153
1 files changed, 153 insertions, 0 deletions
diff --git a/Source/modules/asura-core/Window/WindowImplSDL.cpp b/Source/modules/asura-core/Window/WindowImplSDL.cpp
new file mode 100644
index 0000000..23b2aed
--- /dev/null
+++ b/Source/modules/asura-core/Window/WindowImplSDL.cpp
@@ -0,0 +1,153 @@
+#include "../CoreConfig.h"
+
+#if ASURA_WINDOW_SDL
+
+#include <SDL2/SDL.h>
+
+#include <asura-base/Exception.h>
+
+#include "WindowImplSDL.h"
+
+using namespace AEGraphics;
+using namespace AEImage;
+
+namespace_begin(AsuraEngine)
+namespace_begin(Window)
+
+#define asura_flag_to_sdl_flag(flag, _flag, _sdl_flag) \
+if ((flag & _flag) != 0) \
+ flag |= _sdl_flag
+
+WindowImplSDL::WindowImplSDL()
+ : m_Wnd(nullptr)
+ , m_GLContext(0)
+{
+}
+
+WindowImplSDL::~WindowImplSDL()
+{
+ SDL_GL_DeleteContext(m_GLContext);
+ SDL_DestroyWindow(m_Wnd);
+ SDL_FlushEvent(SDL_WINDOWEVENT);
+}
+
+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)
+ {
+ 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);
+ }
+ }
+ }
+ catch (...)
+ {
+ }
+
+ m_GLContext = SDL_GL_CreateContext(m_Wnd);
+
+ if (!m_GLContext)
+ {
+ SDL_DestroyWindow(m_Wnd);
+ return false;
+ }
+
+ SDL_GL_MakeCurrent(m_Wnd, m_GLContext);
+ SDL_GL_SetSwapInterval(config.vsync ? 1 : 0);
+
+ return true;
+}
+
+void WindowImplSDL::SetSize(uint width, uint height)
+{
+ SDL_SetWindowSize(m_Wnd, width, height);
+}
+
+void WindowImplSDL::SetPosition(int x, int y)
+{
+ SDL_SetWindowPosition(m_Wnd, x, y);
+}
+
+void WindowImplSDL::SetTitils(const std::string& title)
+{
+ SDL_SetWindowTitle(m_Wnd, title.c_str());
+}
+
+void WindowImplSDL::Show()
+{
+ SDL_ShowWindow(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