From 1e8b2472ab9f68893682004f280c595e28c0d864 Mon Sep 17 00:00:00 2001 From: Kiritow <1362050620@qq.com> Date: Sat, 7 Jan 2017 09:52:56 +0800 Subject: [PATCH] [update] SDL Engine --- sdl_engine.cpp | 57 ++++++++++++++++++++++++++++++++++++----- sdl_engine.h | 7 ++++- sdl_engine_font.hpp | 10 +++++++- sdl_engine_rect.hpp | 19 +++++++------- sdl_engine_renderer.hpp | 21 +++++++++------ sdl_engine_texture.hpp | 10 +++++--- sdl_engine_window.hpp | 9 +++++-- 7 files changed, 102 insertions(+), 31 deletions(-) diff --git a/sdl_engine.cpp b/sdl_engine.cpp index 9651eb3..c5a4808 100644 --- a/sdl_engine.cpp +++ b/sdl_engine.cpp @@ -47,31 +47,76 @@ namespace Engine /// Rect #include "sdl_engine_rect.hpp" -struct Window::impl -{ - shared_ptr sWnd; - Renderer rnd; -}; - struct Renderer::impl { +private: + friend class Renderer; shared_ptr sRnd; +public: + void set(SDL_Renderer* rnd) + { + sRnd.reset(rnd,SDL_DestroyRenderer); + } +}; + +struct Window::impl +{ +private: + friend class Window; + shared_ptr sWnd; + Renderer rnd; +public: + void set(SDL_Window* wnd) + { + sWnd.reset(wnd,SDL_DestroyWindow); + rnd.pimpl->set(SDL_CreateRenderer(wnd,-1,SDL_RENDERER_ACCELERATED|SDL_RENDERER_TARGETTEXTURE)); + } }; struct Texture::impl { +private: + friend class Texture; shared_ptr sText; int w,h; +public: + void set(SDL_Texture* text) + { + sText.reset(text,SDL_DestroyTexture); + SDL_QueryTexture(text,NULL,NULL,&w,&h); + } + SDL_Texture* getRawTexture() + { + return sText.get(); + } }; struct Surface::impl { +private: + friend class Surface; shared_ptr sSurf; +public: + void set(SDL_Surface* surf) + { + sSurf.reset(surf,SDL_FreeSurface); + } + SDL_Surface* getRawSurface() + { + return sSurf.get(); + } }; struct Font::impl { +private: + friend class Font; shared_ptr sTTF; +public: + void set(TTF_Font* font) + { + sTTF.reset(font,TTF_CloseFont); + } }; /// Window diff --git a/sdl_engine.h b/sdl_engine.h index 20439af..f85cb44 100644 --- a/sdl_engine.h +++ b/sdl_engine.h @@ -36,6 +36,9 @@ class Font; struct impl; \ impl* pimpl; +#define DEFAULT_WIDTH 1024 +#define DEFAULT_HEIGHT 768 + class Window { public: @@ -47,8 +50,11 @@ public: Window& operator = (const Window&); Renderer getRenderer(); + void resetRenderer(); + Rect getSize(); void setSize(Rect r); + private: struct impl; impl* pimpl; @@ -100,7 +106,6 @@ public: _SDL_ENGINE_IMPL_COPY_DECL(Texture); protected: Texture(); - int updateSize(); private: struct impl; impl* pimpl; diff --git a/sdl_engine_font.hpp b/sdl_engine_font.hpp index 2c0c442..9e41a7d 100644 --- a/sdl_engine_font.hpp +++ b/sdl_engine_font.hpp @@ -24,7 +24,15 @@ Font::~Font() Texture Font::renderText(Renderer rnd,const char* Word,RGBA fg) { Surface surf; - surf.pimpl->sSurf.reset(TTF_RenderText_Blended(pimpl->sTTF.get(),Word,fg.toSDLColor()),SDL_FreeSurface); + surf.pimpl->set(TTF_RenderText_Blended(pimpl->sTTF.get(),Word,fg.toSDLColor())); + Texture t=rnd.render(surf); + return t; +} + +Texture Font::renderUTF8(Renderer rnd,const char* Word,RGBA fg) +{ + Surface surf; + surf.pimpl->set(TTF_RenderUTF8_Blended(pimpl->sTTF.get(),Word,fg.toSDLColor())); Texture t=rnd.render(surf); return t; } diff --git a/sdl_engine_rect.hpp b/sdl_engine_rect.hpp index 0653f08..4c4bec7 100644 --- a/sdl_engine_rect.hpp +++ b/sdl_engine_rect.hpp @@ -1,12 +1,3 @@ -SDL_Rect Rect::toSDLRect() -{ - SDL_Rect rect; - rect.x=x; - rect.y=y; - rect.w=w; - rect.h=h; - return rect; -} Rect::Rect() { x=y=w=h=0; @@ -22,3 +13,13 @@ Rect::~Rect() { } + +SDL_Rect Rect::toSDLRect() +{ + SDL_Rect rect; + rect.x=x; + rect.y=y; + rect.w=w; + rect.h=h; + return rect; +} diff --git a/sdl_engine_renderer.hpp b/sdl_engine_renderer.hpp index 8fa8d91..439a82a 100644 --- a/sdl_engine_renderer.hpp +++ b/sdl_engine_renderer.hpp @@ -6,6 +6,12 @@ Renderer::Renderer(const Renderer& inc) : Renderer() { *pimpl=*(inc.pimpl); } +Renderer& Renderer::operator = (const Renderer& inc) +{ + *pimpl=*(inc.pimpl); + return *this; +} + Renderer::~Renderer() { delete pimpl; @@ -17,15 +23,13 @@ int Renderer::clear() Texture Renderer::loadImage(const char* FileName) { Texture t; - t.pimpl->sText.reset(IMG_LoadTexture(pimpl->sRnd.get(),FileName),SDL_DestroyTexture); - t.updateSize(); + t.pimpl->set(IMG_LoadTexture(pimpl->sRnd.get(),FileName)); return t; } Texture Renderer::render(Surface surface) { Texture t; - t.pimpl->sText.reset(SDL_CreateTextureFromSurface(pimpl->sRnd.get(),surface.pimpl->sSurf.get()),SDL_DestroyTexture); - t.updateSize(); + t.pimpl->set(SDL_CreateTextureFromSurface(pimpl->sRnd.get(),surface.pimpl->getRawSurface())); return t; } @@ -33,23 +37,24 @@ void Renderer::update() { SDL_RenderPresent(pimpl->sRnd.get()); } + int Renderer::copy(Texture t,Rect src,Rect dst) { SDL_Rect s=src.toSDLRect(); SDL_Rect d=dst.toSDLRect(); - return SDL_RenderCopy(pimpl->sRnd.get(),t.pimpl->sText.get(),&s,&d); + return SDL_RenderCopy(pimpl->sRnd.get(),t.pimpl->getRawTexture(),&s,&d); } int Renderer::copyTo(Texture t,Rect dst) { SDL_Rect d=dst.toSDLRect(); - return SDL_RenderCopy(pimpl->sRnd.get(),t.pimpl->sText.get(),NULL,&d); + return SDL_RenderCopy(pimpl->sRnd.get(),t.pimpl->getRawTexture(),NULL,&d); } int Renderer::copyFill(Texture t,Rect src) { SDL_Rect s=src.toSDLRect(); - return SDL_RenderCopy(pimpl->sRnd.get(),t.pimpl->sText.get(),&s,NULL); + return SDL_RenderCopy(pimpl->sRnd.get(),t.pimpl->getRawTexture(),&s,NULL); } int Renderer::copyFullFill(Texture t) { - return SDL_RenderCopy(pimpl->sRnd.get(),t.pimpl->sText.get(),NULL,NULL); + return SDL_RenderCopy(pimpl->sRnd.get(),t.pimpl->getRawTexture(),NULL,NULL); } diff --git a/sdl_engine_texture.hpp b/sdl_engine_texture.hpp index ce38057..393e107 100644 --- a/sdl_engine_texture.hpp +++ b/sdl_engine_texture.hpp @@ -6,6 +6,12 @@ Texture::Texture(const Texture& inc) : Texture() { *pimpl=*(inc.pimpl); } +Texture& Texture::operator = (const Texture& inc) +{ + *pimpl=*(inc.pimpl); + return *this; +} + int Texture::getw() { return pimpl->w; @@ -18,7 +24,3 @@ Texture::~Texture() { delete pimpl; } -int Texture::updateSize() -{ - return SDL_QueryTexture(pimpl->sText.get(),NULL,NULL,&pimpl->w,&pimpl->h); -} diff --git a/sdl_engine_window.hpp b/sdl_engine_window.hpp index 0ca85f2..2f254e5 100644 --- a/sdl_engine_window.hpp +++ b/sdl_engine_window.hpp @@ -1,8 +1,8 @@ Window::Window(int winw,int winh) { pimpl=new impl; - pimpl->sWnd.reset(SDL_CreateWindow("Engine",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,winw,winh,SDL_WINDOW_SHOWN),SDL_DestroyWindow); - pimpl->rnd.pimpl->sRnd.reset(SDL_CreateRenderer(pimpl->sWnd.get(),-1,SDL_RENDERER_ACCELERATED|SDL_RENDERER_TARGETTEXTURE),SDL_DestroyRenderer); + SDL_Window* wnd=SDL_CreateWindow("Engine",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,winw,winh,SDL_WINDOW_SHOWN); + pimpl->set(wnd); } Window::~Window() @@ -14,3 +14,8 @@ Renderer Window::getRenderer() { return pimpl->rnd; } + +Window::Window(const Window& inc) : Window(DEFAULT_WIDTH,DEFAULT_HEIGHT) +{ + *pimpl=*(inc.pimpl); +}