From 112c1081aaf02fabbb58c763e98b10e2e3cbefec Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Thu, 15 Jun 2017 13:53:04 +0800 Subject: [PATCH] Move Renderer settings out of class Window --- MiniEngine.cpp | 49 ++++----- MiniEngine.h | 271 ++++++++++++++++++++++++------------------------- 2 files changed, 156 insertions(+), 164 deletions(-) diff --git a/MiniEngine.cpp b/MiniEngine.cpp index a1b50ac..a422b5f 100644 --- a/MiniEngine.cpp +++ b/MiniEngine.cpp @@ -1467,7 +1467,6 @@ namespace MiniEngine } Window::Window(std::string Title, int Width, int Height, - std::initializer_list RendererFlags, std::initializer_list WindowFlags , int WindowPositionX, int WindowPositionY) throw(ErrorViewer) { /// Calculate Window Flags @@ -1484,23 +1483,26 @@ namespace MiniEngine e.fetch(); throw e; } + _set(temp); - setRenderer(RendererFlags); } - Renderer Window::getRenderer() const + Renderer::Renderer(Window& wnd,std::initializer_list RendererFlags) throw (ErrorViewer) { - return _winrnd; + if(createRenderer(wnd,RendererFlags)!=0) + { + throw ErrorViewer(); + } } - void Window::setRenderer(std::initializer_list RendererFlags) + int Renderer::createRenderer(Window& wnd,std::initializer_list RendererFlags) { Uint32 flag = 0; for (auto v : RendererFlags) { - flag |= _render_caster(v); + flag |= _rendertype_caster(v); } - _setRenderer_Real(flag); + return _createRenderer_Real(wnd,flag); } Rect Window::getSize() const @@ -1621,7 +1623,7 @@ namespace MiniEngine } // private - Uint32 Window::_render_caster(RendererType Type) + Uint32 Renderer::_rendertype_caster(RendererType Type) { switch(Type) { @@ -1640,9 +1642,18 @@ namespace MiniEngine } // private - void Window::_setRenderer_Real(Uint32 flags) + int Renderer::_createRenderer_Real(Window& wnd,Uint32 flags) { - _winrnd._rnd.reset(SDL_CreateRenderer(_get(), -1, flags), SDL_DestroyRenderer); + SDL_Renderer* pSDLRnd=SDL_CreateRenderer(wnd._get(), -1, flags); + if(pSDLRnd!=nullptr) + { + _set(pSDLRnd); + return 0; + } + else + { + return -1; + } } int Window::showSimpleMessageBox(MessageBoxType type,const std::string& Title,const std::string& Message) const @@ -1736,24 +1747,6 @@ namespace MiniEngine return SDL_IsScreenKeyboardShown(_get())==SDL_TRUE; } - /// Experimental - void Window::resetRenderer() - { - /// Check if there is a renderer exists. - if(SDL_GetRenderer(_get())!=nullptr) - { - /// Clear internal Renderer class. - _winrnd._clear(); - /// Check again. - if(SDL_GetRenderer(_get())!=nullptr) - { - /// If it still exists, (May be some other Renderer is holding) - /// then destroy it. - SDL_DestroyRenderer(SDL_GetRenderer(_get())); - } - } - } - void Font::_set(TTF_Font* p) { _font.reset(p,TTF_CloseFont); diff --git a/MiniEngine.h b/MiniEngine.h index e104388..c7a257a 100644 --- a/MiniEngine.h +++ b/MiniEngine.h @@ -207,103 +207,6 @@ namespace MiniEngine friend class Renderer; }; - enum class RendererType { Software, Accelerated, PresentSync, TargetTexture }; - - enum class FlipMode { None, Horizontal, Vertical }; - - class Renderer - { - public: - Renderer() = default; - int setColor(const RGBA& pack); - RGBA getColor() const; - int setBlendMode(BlendMode mode); - BlendMode getBlendMode() const; - - int setTarget(Texture& t); - int setTarget(); - Texture getTarget(); - - int fillRect(const Rect& rect); - int drawRect(const Rect& rect); - int drawPoint(const Point& p); - int drawLine(const Point& A,const Point& B); - - /// Experimental - int fillRects(const SDL_Rect* pRectArray,int n); - int drawRects(const SDL_Rect* pRectArray,int n); - int drawPoints(const SDL_Point* pPointArray,int n); - int drawLines(const SDL_Point* pPointArray,int n); - /// Experimental - int fillRects(const std::vector& rectvec); - int drawRects(const std::vector& rectvec); - int drawPoints(const std::vector& pointvec); - int drawLines(const std::vector& pointvec); - - int setScale(float scaleX,float scaleY); - std::tuple getScale() const; - - int setViewport(const Rect& viewport); - Rect getViewport() const; - - int setLogicalSize(int w,int h); - Rect getLogicalSize() const; - - int setClipRect(const Rect& cliprect); - Rect getClipRect() const; - bool isClipEnabled() const; - - Rect getOutputSize() const; - - int clear(); - void update(); - - int copy(const Texture& t, const Rect& src, const Rect& dst); - int copyTo(const Texture& t, const Rect& dst); - int copyTo(const Texture& t, const Point& lupoint); - int copyFill(const Texture& t, const Rect& src); - int copyFullFill(const Texture& t); - - /// Super copy without center point. - int copy(const Texture& t, const Rect& src, const Rect& dst,double angle,FlipMode mode); - int copyTo(const Texture& t, const Rect& dst,double angle,FlipMode mode); - int copyTo(const Texture& t, const Point& lupoint,double angle,FlipMode mode); - int copyFill(const Texture& t, const Rect& src,double angle,FlipMode mode); - int copyFullFill(const Texture& t,double angle,FlipMode mode); - /// Super copy with center point - int copy(const Texture& t, const Rect& src, const Rect& dst,const Point& centerPoint,double angle,FlipMode mode); - int copyTo(const Texture& t, const Rect& dst,const Point& centerPoint,double angle,FlipMode mode); - int copyTo(const Texture& t, const Point& lupoint,const Point& centerPoint,double angle,FlipMode mode); - int copyFill(const Texture& t, const Rect& src,const Point& centerPoint,double angle,FlipMode mode); - int copyFullFill(const Texture& t,const Point& centerPoint,double angle,FlipMode mode); - - /// Reserved for compatibility - int supercopy(const Texture& t, - bool srcfull,const Rect& src,bool dstfull,const Rect& dst, - double angle, - bool haspoint,const Point& center,FlipMode mode); - - Texture render(const Surface& surf) const throw (ErrorViewer); - Texture loadTexture(const std::string& FileName) const throw(ErrorViewer); - Texture loadTextureRW(const RWOP& rwop) const throw(ErrorViewer); - Texture createTexture(int Width, int Height) const throw(ErrorViewer); - - bool isRenderTargetSupported() const; - bool isReady() const; - - void release(); - - /// Experimental - static int GetDriversNum(); - private: - std::shared_ptr _rnd; - void _set(SDL_Renderer*); - void _clear(); - SDL_Renderer* _get() const; - - friend class Window; - }; - enum class SystemCursorType { Arrow, Ibeam, CrossHair, @@ -391,25 +294,8 @@ namespace MiniEngine public: Window()=default; Window(std::string Title, int Width, int Height, - std::initializer_list RendererFlags = { RendererType::Accelerated,RendererType::TargetTexture }, std::initializer_list WindowFlags = {WindowType::Shown} , int WindowPositionX=SDL_WINDOWPOS_CENTERED, int WindowPositionY=SDL_WINDOWPOS_CENTERED) throw(ErrorViewer); - Renderer getRenderer() const; - - void setRenderer(RendererType Type) - { - int flagcalc=0; - _setRenderer(flagcalc,Type); - } - - template - void setRenderer(RendererType Type,Args&&... args) - { - int flagcalc=0; - _setRenderer(flagcalc,Type,std::forward(args...)); - } - - void setRenderer(std::initializer_list); Rect getSize() const; void setSize(const Rect& sizeRect); @@ -451,34 +337,147 @@ namespace MiniEngine bool isScreenKeyboardShown(); void release(); - - /// Experimental : Free current renderer. - /// This will cause all existing Renderer class throw an error on delete. - /// This function destroy current renderer (if exist) and all Renderer class will not be notified. - void resetRenderer(); - protected: - template - void _setRenderer(int& refcalc,RendererType Type,Args&&... args) - { - refcalc|=_render_caster(Type); - _setRenderer(refcalc,args...); - } - - void _setRenderer(int& refcalc,RendererType Type) - { - refcalc|=_render_caster(Type); - _setRenderer_Real(refcalc); - } private: - void _setRenderer_Real(Uint32 flags); - Uint32 _render_caster(RendererType); - std::shared_ptr _wnd; + void _set(SDL_Window*); void _clear(); SDL_Window* _get() const; - Renderer _winrnd; + friend class Renderer; + }; + + enum class RendererType { Software, Accelerated, PresentSync, TargetTexture }; + + enum class FlipMode { None, Horizontal, Vertical }; + + class Renderer + { + public: + Renderer() = default; + Renderer(Window& wnd,std::initializer_list RendererFlags = { RendererType::Accelerated,RendererType::TargetTexture }) throw (ErrorViewer); + ~Renderer() = default; + + /// If Renderer is current not ready, setRenderer will create Renderer. + /// Otherwise, setRenderer will fail. + int createRenderer(Window& wnd,RendererType Type) + { + int flagcalc=0; + return _createRenderer(wnd,flagcalc,Type); + } + + template + int createRenderer(Window& wnd,RendererType Type,Args&&... args) + { + int flagcalc=0; + return _createRenderer(wnd,flagcalc,Type,std::forward(args...)); + } + + int createRenderer(Window& wnd,std::initializer_list); + + int setColor(const RGBA& pack); + RGBA getColor() const; + int setBlendMode(BlendMode mode); + BlendMode getBlendMode() const; + + int setTarget(Texture& t); + int setTarget(); + Texture getTarget(); + + int fillRect(const Rect& rect); + int drawRect(const Rect& rect); + int drawPoint(const Point& p); + int drawLine(const Point& A,const Point& B); + + /// Experimental + int fillRects(const SDL_Rect* pRectArray,int n); + int drawRects(const SDL_Rect* pRectArray,int n); + int drawPoints(const SDL_Point* pPointArray,int n); + int drawLines(const SDL_Point* pPointArray,int n); + /// Experimental + int fillRects(const std::vector& rectvec); + int drawRects(const std::vector& rectvec); + int drawPoints(const std::vector& pointvec); + int drawLines(const std::vector& pointvec); + + int setScale(float scaleX,float scaleY); + std::tuple getScale() const; + + int setViewport(const Rect& viewport); + Rect getViewport() const; + + int setLogicalSize(int w,int h); + Rect getLogicalSize() const; + + int setClipRect(const Rect& cliprect); + Rect getClipRect() const; + bool isClipEnabled() const; + + Rect getOutputSize() const; + + int clear(); + void update(); + + int copy(const Texture& t, const Rect& src, const Rect& dst); + int copyTo(const Texture& t, const Rect& dst); + int copyTo(const Texture& t, const Point& lupoint); + int copyFill(const Texture& t, const Rect& src); + int copyFullFill(const Texture& t); + + /// Super copy without center point. + int copy(const Texture& t, const Rect& src, const Rect& dst,double angle,FlipMode mode); + int copyTo(const Texture& t, const Rect& dst,double angle,FlipMode mode); + int copyTo(const Texture& t, const Point& lupoint,double angle,FlipMode mode); + int copyFill(const Texture& t, const Rect& src,double angle,FlipMode mode); + int copyFullFill(const Texture& t,double angle,FlipMode mode); + /// Super copy with center point + int copy(const Texture& t, const Rect& src, const Rect& dst,const Point& centerPoint,double angle,FlipMode mode); + int copyTo(const Texture& t, const Rect& dst,const Point& centerPoint,double angle,FlipMode mode); + int copyTo(const Texture& t, const Point& lupoint,const Point& centerPoint,double angle,FlipMode mode); + int copyFill(const Texture& t, const Rect& src,const Point& centerPoint,double angle,FlipMode mode); + int copyFullFill(const Texture& t,const Point& centerPoint,double angle,FlipMode mode); + + /// Reserved for compatibility + int supercopy(const Texture& t, + bool srcfull,const Rect& src,bool dstfull,const Rect& dst, + double angle, + bool haspoint,const Point& center,FlipMode mode); + + Texture render(const Surface& surf) const throw (ErrorViewer); + Texture loadTexture(const std::string& FileName) const throw(ErrorViewer); + Texture loadTextureRW(const RWOP& rwop) const throw(ErrorViewer); + Texture createTexture(int Width, int Height) const throw(ErrorViewer); + + bool isRenderTargetSupported() const; + bool isReady() const; + + void release(); + + /// Experimental + static int GetDriversNum(); + protected: + template + int _createRenderer(Window& wnd,int& refcalc,RendererType Type,Args&&... args) + { + refcalc|=_rendertype_caster(Type); + return _createRenderer(wnd,refcalc,args...); + } + + // template<> + int _createRenderer(Window& wnd,int& refcalc,RendererType Type) + { + refcalc|=_rendertype_caster(Type); + return _createRenderer_Real(wnd,refcalc); + } + private: + std::shared_ptr _rnd; + + int _createRenderer_Real(Window& wnd,Uint32 flags); + Uint32 _rendertype_caster(RendererType); + + void _set(SDL_Renderer*); + void _clear(); + SDL_Renderer* _get() const; }; enum class FontStyle { Normal, Bold, Italic, UnderLine, StrikeThrough };