From 88d5a9c96bb2225937bbade7c788341c9b5c59f0 Mon Sep 17 00:00:00 2001 From: Kiritow <1362050620@qq.com> Date: Sat, 7 Jan 2017 17:51:09 +0800 Subject: [PATCH] [Bug Fix & Update] SDL Engine Example code changed. --- App.cpp | 2 ++ sdl_engine.cpp | 4 ++++ sdl_engine.h | 4 +++- sdl_engine_font.hpp | 31 +++++++++++++++++++++++++++---- sdl_engine_renderer.hpp | 12 ++++++++++++ sdl_engine_surface.hpp | 23 +++++++++++++++++++---- sdl_engine_texture.hpp | 11 +++++++++++ sdl_engine_window.hpp | 38 ++++++++++++++++++++++++++++++++++++-- 8 files changed, 114 insertions(+), 11 deletions(-) diff --git a/App.cpp b/App.cpp index ad8b696..3ae2ac0 100644 --- a/App.cpp +++ b/App.cpp @@ -9,5 +9,7 @@ namespace App { Window wnd(1024,768); Renderer rnd=wnd.getRenderer(); + rnd.clear(); + rnd.update(); } } diff --git a/sdl_engine.cpp b/sdl_engine.cpp index c5a4808..f34370d 100644 --- a/sdl_engine.cpp +++ b/sdl_engine.cpp @@ -71,6 +71,10 @@ public: sWnd.reset(wnd,SDL_DestroyWindow); rnd.pimpl->set(SDL_CreateRenderer(wnd,-1,SDL_RENDERER_ACCELERATED|SDL_RENDERER_TARGETTEXTURE)); } + SDL_Window* getRawWindow() + { + return sWnd.get(); + } }; struct Texture::impl diff --git a/sdl_engine.h b/sdl_engine.h index 5314b53..cc5474d 100644 --- a/sdl_engine.h +++ b/sdl_engine.h @@ -30,7 +30,8 @@ class Font; #define _SDL_ENGINE_IMPL_COPY_DECL(ClassName) \ ClassName(const ClassName&); \ ClassName(ClassName&&); \ - ClassName& operator = (const ClassName&); + ClassName& operator = (const ClassName&); \ + ClassName& operator = (ClassName&&); #define _SDL_ENGINE_IMPL \ struct impl; \ @@ -51,6 +52,7 @@ public: void resetRenderer(); Rect getSize(); + void setSize(Rect r); private: diff --git a/sdl_engine_font.hpp b/sdl_engine_font.hpp index 9e41a7d..c1f6bd0 100644 --- a/sdl_engine_font.hpp +++ b/sdl_engine_font.hpp @@ -8,6 +8,32 @@ Font::Font(const char* FontFileName,int sz) : Font() use(FontFileName,sz); } +Font::Font(const Font& inc) : Font() +{ + *pimpl=*(inc.pimpl); +} +Font& Font::operator = (const Font& inc) +{ + *pimpl=*(inc.pimpl); + return *this; +} +Font::Font(Font&& inc) +{ + pimpl=inc.pimpl; + inc.pimpl=nullptr; +} +Font& Font::operator = (Font&& inc) +{ + *pimpl=*(inc.pimpl); + inc.pimpl=nullptr; + return *this; +} + +Font::~Font() +{ + delete pimpl; +} + int Font::use(const char* FontFileName,int sz) { TTF_Font* font=TTF_OpenFont(FontFileName,sz); @@ -16,10 +42,7 @@ int Font::use(const char* FontFileName,int sz) return 0; } -Font::~Font() -{ - delete pimpl; -} + Texture Font::renderText(Renderer rnd,const char* Word,RGBA fg) { diff --git a/sdl_engine_renderer.hpp b/sdl_engine_renderer.hpp index 439a82a..0794611 100644 --- a/sdl_engine_renderer.hpp +++ b/sdl_engine_renderer.hpp @@ -11,11 +11,23 @@ Renderer& Renderer::operator = (const Renderer& inc) *pimpl=*(inc.pimpl); return *this; } +Renderer::Renderer(Renderer&& inc) +{ + pimpl=inc.pimpl; + inc.pimpl=nullptr; +} +Renderer& Renderer::operator = (Renderer&& inc) +{ + *pimpl=*(inc.pimpl); + inc.pimpl=nullptr; + return *this; +} Renderer::~Renderer() { delete pimpl; } + int Renderer::clear() { return SDL_RenderClear(pimpl->sRnd.get()); diff --git a/sdl_engine_surface.hpp b/sdl_engine_surface.hpp index 54274cf..6e77f9e 100644 --- a/sdl_engine_surface.hpp +++ b/sdl_engine_surface.hpp @@ -2,11 +2,26 @@ Surface::Surface() { pimpl=new impl; } -Surface::~Surface() -{ - delete pimpl; -} Surface::Surface(const Surface& inc) : Surface() { *pimpl=*(inc.pimpl); } +Surface& Surface::operator = (const Surface& inc) +{ + *pimpl=*(inc.pimpl); + return *this; +} +Surface::Surface(Surface&& inc) +{ + pimpl=inc.pimpl; + inc.pimpl=nullptr; +} +Surface& Surface::operator = (Surface&& inc) +{ + *(pimpl)=*(inc.pimpl); + return *this; +} +Surface::~Surface() +{ + delete pimpl; +} diff --git a/sdl_engine_texture.hpp b/sdl_engine_texture.hpp index 393e107..72b7e09 100644 --- a/sdl_engine_texture.hpp +++ b/sdl_engine_texture.hpp @@ -11,6 +11,17 @@ Texture& Texture::operator = (const Texture& inc) *pimpl=*(inc.pimpl); return *this; } +Texture::Texture(Texture&& inc) +{ + pimpl=inc.pimpl; + inc.pimpl=nullptr; +} +Texture& Texture::operator = (Texture&& inc) +{ + *(pimpl)=*(inc.pimpl); + inc.pimpl=nullptr; + return *this; +} int Texture::getw() { diff --git a/sdl_engine_window.hpp b/sdl_engine_window.hpp index 2f254e5..6f20d34 100644 --- a/sdl_engine_window.hpp +++ b/sdl_engine_window.hpp @@ -5,6 +5,32 @@ Window::Window(int winw,int winh) pimpl->set(wnd); } +Window::Window(const Window& inc) +{ + pimpl=new impl; + *pimpl=*(inc.pimpl); +} + +Window& Window::operator = (const Window& inc) +{ + *pimpl=*(inc.pimpl); + return *this; +} + +Window::Window(Window&& inc) +{ + pimpl=inc.pimpl; + inc.pimpl=nullptr; +} + +Window& Window::operator = (Window&& inc) +{ + *pimpl=*(inc.pimpl); + inc.pimpl=nullptr; + return *this; +} + + Window::~Window() { delete pimpl; @@ -15,7 +41,15 @@ Renderer Window::getRenderer() return pimpl->rnd; } -Window::Window(const Window& inc) : Window(DEFAULT_WIDTH,DEFAULT_HEIGHT) +Rect Window::getSize() { - *pimpl=*(inc.pimpl); + int w,h; + SDL_GetWindowSize(pimpl->getRawWindow(),&w,&h); + Rect rect(0,0,w,h); + return rect; +} + +void Window::setSize(Rect rect) +{ + SDL_SetWindowSize(pimpl->getRawWindow(),rect.w,rect.h); }