Move Window out

This commit is contained in:
Kirigaya Kazuto 2017-06-18 17:40:17 +08:00
parent dfa628cd14
commit 681ef2b0c6
5 changed files with 237 additions and 224 deletions

View File

@ -458,179 +458,11 @@ namespace MiniEngine
return _vec.at(index);
}
//private
void Window::_set(SDL_Window* p)
{
_wnd.reset(p,SDL_DestroyWindow);
}
//private
void Window::_clear()
{
_wnd.reset();
}
//private
SDL_Window* Window::_get() const
{
return _wnd.get();
}
Window::Window(std::string Title, int Width, int Height,
std::initializer_list<WindowType> WindowFlags , int WindowPositionX, int WindowPositionY) throw(ErrorViewer)
{
/// Calculate Window Flags
Uint32 windowFlag=0;
for(auto v:WindowFlags)
{
windowFlag|=_internal::getSDLWindowFlagsFromWindowType(v);
}
SDL_Window* temp = SDL_CreateWindow(Title.c_str(), WindowPositionX, WindowPositionY, Width, Height, windowFlag);
if (temp == NULL)
{
ErrorViewer e;
e.fetch();
throw e;
}
_set(temp);
}
Renderer::Renderer(Window& wnd,std::initializer_list<RendererType> RendererFlags) throw (ErrorViewer)
{
if(createRenderer(wnd,RendererFlags)!=0)
{
throw ErrorViewer();
}
}
int Renderer::createRenderer(Window& wnd,std::initializer_list<RendererType> RendererFlags)
{
Uint32 flag = 0;
for (auto v : RendererFlags)
{
flag |= _rendertype_caster(v);
}
return _createRenderer_Real(wnd,flag);
}
Rect Window::getSize() const
{
int w, h;
SDL_GetWindowSize(_get(), &w, &h);
return Rect(0, 0, w, h);
}
void Window::setSize(const Rect& sizeRect)
{
setSize(sizeRect.w, sizeRect.h);
}
void Window::setSize(int w, int h)
{
SDL_SetWindowSize(_get(), w, h);
}
Point Window::getPosition() const
{
int x, y;
SDL_GetWindowPosition(_get(), &x, &y);
return Point(x, y);
}
void Window::setPosition(int x, int y)
{
SDL_SetWindowPosition(_get(), x, y);
}
void Window::setPosition(const Point& point)
{
SDL_SetWindowPosition(_get(), point.x, point.y);
}
void Window::setTitle(const std::string& Title)
{
SDL_SetWindowTitle(_get(), Title.c_str());
}
std::string Window::getTitle() const
{
return std::string(SDL_GetWindowTitle(_get()));
}
void Window::setGrab(bool isGrab)
{
SDL_SetWindowGrab(_get(),isGrab?SDL_TRUE:SDL_FALSE);
}
bool Window::getGrab() const
{
return (SDL_GetWindowGrab(_get())==SDL_TRUE)?true:false;
}
#if _MINIENGINE_SDL_VERSION_ATLEAST(2,0,5)
int Window::setOpacity(float opacity)
{
return SDL_SetWindowOpacity(_get(),opacity);
}
float Window::getOpacity() const
{
float op=-1;
SDL_GetWindowOpacity(_get(),&op);
return op;
}
#endif /// End of SDL2 2.0.5 Require.
/// FIXME: Not Implemented.
void Window::setResizable(bool resizable)
{
//SDL_SetWindowResizable(_get(), resizable?SDL_TRUE:SDL_FALSE);
}
void Window::show()
{
SDL_ShowWindow(_get());
}
void Window::hide()
{
SDL_HideWindow(_get());
}
void Window::raise()
{
SDL_RaiseWindow(_get());
}
void Window::minimize()
{
SDL_MinimizeWindow(_get());
}
void Window::maximize()
{
SDL_MaximizeWindow(_get());
}
void Window::restore()
{
SDL_RestoreWindow(_get());
}
_DECL_DEPRECATED Surface Window::getSurface()
{
SDL_Surface* temp = SDL_GetWindowSurface(_get());
Surface s;
/// Don't Free This Surface
s._set_no_delete(temp);
return s;
}
void Window::release()
{
_clear();
}
// private
Uint32 Renderer::_rendertype_caster(RendererType Type)

View File

@ -136,63 +136,7 @@ namespace MiniEngine
std::vector<WindowMessageBoxButton> _vec;
};
class Window
{
public:
Window()=default;
Window(std::string Title, int Width, int Height,
std::initializer_list<WindowType> WindowFlags = {WindowType::Shown} ,
int WindowPositionX=SDL_WINDOWPOS_CENTERED, int WindowPositionY=SDL_WINDOWPOS_CENTERED) throw(ErrorViewer);
Rect getSize() const;
void setSize(const Rect& sizeRect);
void setSize(int w, int h);
Point getPosition() const;
void setPosition(int x, int y);
void setPosition(const Point& point);
void setTitle(const std::string& Title);
std::string getTitle() const;
void setGrab(bool isGrab);
bool getGrab() const;
#if _MINIENGINE_SDL_VERSION_ATLEAST(2,0,5)
/// SDL2.0.5 Required.
int setOpacity(float opacity);
float getOpacity() const;
#endif
/// FIXME: Not Implemented.
void setResizable(bool resizable);
/// Use UTF8 in Title and Message please.
int showSimpleMessageBox(MessageBoxType type,const std::string& Title,const std::string& Message) const;
int showMessageBox(const WindowMessageBox& box) const;
void show();
void hide();
void raise();
void minimize();
void maximize();
void restore();
_DECL_DEPRECATED Surface getSurface();
bool isScreenKeyboardShown();
void release();
private:
std::shared_ptr<SDL_Window> _wnd;
void _set(SDL_Window*);
void _clear();
SDL_Window* _get() const;
friend class Renderer;
};
enum class RendererType { Software, Accelerated, PresentSync, TargetTexture };

View File

@ -19,6 +19,24 @@ SDL_Renderer* Renderer::_get() const
return _rnd.get();
}
Renderer::Renderer(Window& wnd,std::initializer_list<RendererType> RendererFlags) throw (ErrorViewer)
{
if(createRenderer(wnd,RendererFlags)!=0)
{
throw ErrorViewer();
}
}
int Renderer::createRenderer(Window& wnd,std::initializer_list<RendererType> RendererFlags)
{
Uint32 flag = 0;
for (auto v : RendererFlags)
{
flag |= _rendertype_caster(v);
}
return _createRenderer_Real(wnd,flag);
}
int Renderer::setColor(const RGBA& pack)
{
return SDL_SetRenderDrawColor(_get(), pack.r, pack.g, pack.b, pack.a);

158
SDLWrapper/Window.cpp Normal file
View File

@ -0,0 +1,158 @@
#include "Window.h"
#include "begin_code.h"
//private
void Window::_set(SDL_Window* p)
{
_wnd.reset(p,SDL_DestroyWindow);
}
//private
void Window::_clear()
{
_wnd.reset();
}
//private
SDL_Window* Window::_get() const
{
return _wnd.get();
}
Window::Window(std::string Title, int Width, int Height,
std::initializer_list<WindowType> WindowFlags, int WindowPositionX, int WindowPositionY) throw(ErrorViewer)
{
/// Calculate Window Flags
Uint32 windowFlag=0;
for(auto v:WindowFlags)
{
windowFlag|=_internal::getSDLWindowFlagsFromWindowType(v);
}
SDL_Window* temp = SDL_CreateWindow(Title.c_str(), WindowPositionX, WindowPositionY, Width, Height, windowFlag);
if (temp == NULL)
{
ErrorViewer e;
e.fetch();
throw e;
}
_set(temp);
}
Rect Window::getSize() const
{
int w, h;
SDL_GetWindowSize(_get(), &w, &h);
return Rect(0, 0, w, h);
}
void Window::setSize(const Rect& sizeRect)
{
setSize(sizeRect.w, sizeRect.h);
}
void Window::setSize(int w, int h)
{
SDL_SetWindowSize(_get(), w, h);
}
Point Window::getPosition() const
{
int x, y;
SDL_GetWindowPosition(_get(), &x, &y);
return Point(x, y);
}
void Window::setPosition(int x, int y)
{
SDL_SetWindowPosition(_get(), x, y);
}
void Window::setPosition(const Point& point)
{
SDL_SetWindowPosition(_get(), point.x, point.y);
}
void Window::setTitle(const std::string& Title)
{
SDL_SetWindowTitle(_get(), Title.c_str());
}
std::string Window::getTitle() const
{
return std::string(SDL_GetWindowTitle(_get()));
}
void Window::setGrab(bool isGrab)
{
SDL_SetWindowGrab(_get(),isGrab?SDL_TRUE:SDL_FALSE);
}
bool Window::getGrab() const
{
return (SDL_GetWindowGrab(_get())==SDL_TRUE)?true:false;
}
#if _MINIENGINE_SDL_VERSION_ATLEAST(2,0,5)
int Window::setOpacity(float opacity)
{
return SDL_SetWindowOpacity(_get(),opacity);
}
float Window::getOpacity() const
{
float op=-1;
SDL_GetWindowOpacity(_get(),&op);
return op;
}
#endif /// End of SDL2 2.0.5 Require.
/// FIXME: Not Implemented.
void Window::setResizable(bool resizable)
{
//SDL_SetWindowResizable(_get(), resizable?SDL_TRUE:SDL_FALSE);
}
void Window::show()
{
SDL_ShowWindow(_get());
}
void Window::hide()
{
SDL_HideWindow(_get());
}
void Window::raise()
{
SDL_RaiseWindow(_get());
}
void Window::minimize()
{
SDL_MinimizeWindow(_get());
}
void Window::maximize()
{
SDL_MaximizeWindow(_get());
}
void Window::restore()
{
SDL_RestoreWindow(_get());
}
_DECL_DEPRECATED Surface Window::getSurface()
{
SDL_Surface* temp = SDL_GetWindowSurface(_get());
Surface s;
/// Don't Free This Surface
s._set_no_delete(temp);
return s;
}
void Window::release()
{
_clear();
}
#include "end_code.h"

61
SDLWrapper/Window.h Normal file
View File

@ -0,0 +1,61 @@
#pragma once
#include "includes.h"
#include "begin_code.h"
class Window
{
public:
Window()=default;
Window(std::string Title, int Width, int Height,
std::initializer_list<WindowType> WindowFlags = {WindowType::Shown},
int WindowPositionX=SDL_WINDOWPOS_CENTERED, int WindowPositionY=SDL_WINDOWPOS_CENTERED) throw(ErrorViewer);
Rect getSize() const;
void setSize(const Rect& sizeRect);
void setSize(int w, int h);
Point getPosition() const;
void setPosition(int x, int y);
void setPosition(const Point& point);
void setTitle(const std::string& Title);
std::string getTitle() const;
void setGrab(bool isGrab);
bool getGrab() const;
#if _MINIENGINE_SDL_VERSION_ATLEAST(2,0,5)
/// SDL2.0.5 Required.
int setOpacity(float opacity);
float getOpacity() const;
#endif
/// FIXME: Not Implemented.
void setResizable(bool resizable);
/// Use UTF8 in Title and Message please.
int showSimpleMessageBox(MessageBoxType type,const std::string& Title,const std::string& Message) const;
int showMessageBox(const WindowMessageBox& box) const;
void show();
void hide();
void raise();
void minimize();
void maximize();
void restore();
_DECL_DEPRECATED Surface getSurface();
bool isScreenKeyboardShown();
void release();
private:
std::shared_ptr<SDL_Window> _wnd;
void _set(SDL_Window*);
void _clear();
SDL_Window* _get() const;
friend class Renderer;
};
#include "end_code.h"