[update] SDL Engine

This commit is contained in:
Kirigaya Kazuto 2017-01-07 09:52:56 +08:00
parent 35763cf92f
commit 1e8b2472ab
7 changed files with 102 additions and 31 deletions

View File

@ -47,31 +47,76 @@ namespace Engine
/// Rect /// Rect
#include "sdl_engine_rect.hpp" #include "sdl_engine_rect.hpp"
struct Window::impl
{
shared_ptr<SDL_Window> sWnd;
Renderer rnd;
};
struct Renderer::impl struct Renderer::impl
{ {
private:
friend class Renderer;
shared_ptr<SDL_Renderer> sRnd; shared_ptr<SDL_Renderer> sRnd;
public:
void set(SDL_Renderer* rnd)
{
sRnd.reset(rnd,SDL_DestroyRenderer);
}
};
struct Window::impl
{
private:
friend class Window;
shared_ptr<SDL_Window> 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 struct Texture::impl
{ {
private:
friend class Texture;
shared_ptr<SDL_Texture> sText; shared_ptr<SDL_Texture> sText;
int w,h; 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 struct Surface::impl
{ {
private:
friend class Surface;
shared_ptr<SDL_Surface> sSurf; shared_ptr<SDL_Surface> sSurf;
public:
void set(SDL_Surface* surf)
{
sSurf.reset(surf,SDL_FreeSurface);
}
SDL_Surface* getRawSurface()
{
return sSurf.get();
}
}; };
struct Font::impl struct Font::impl
{ {
private:
friend class Font;
shared_ptr<TTF_Font> sTTF; shared_ptr<TTF_Font> sTTF;
public:
void set(TTF_Font* font)
{
sTTF.reset(font,TTF_CloseFont);
}
}; };
/// Window /// Window

View File

@ -36,6 +36,9 @@ class Font;
struct impl; \ struct impl; \
impl* pimpl; impl* pimpl;
#define DEFAULT_WIDTH 1024
#define DEFAULT_HEIGHT 768
class Window class Window
{ {
public: public:
@ -47,8 +50,11 @@ public:
Window& operator = (const Window&); Window& operator = (const Window&);
Renderer getRenderer(); Renderer getRenderer();
void resetRenderer();
Rect getSize(); Rect getSize();
void setSize(Rect r); void setSize(Rect r);
private: private:
struct impl; struct impl;
impl* pimpl; impl* pimpl;
@ -100,7 +106,6 @@ public:
_SDL_ENGINE_IMPL_COPY_DECL(Texture); _SDL_ENGINE_IMPL_COPY_DECL(Texture);
protected: protected:
Texture(); Texture();
int updateSize();
private: private:
struct impl; struct impl;
impl* pimpl; impl* pimpl;

View File

@ -24,7 +24,15 @@ Font::~Font()
Texture Font::renderText(Renderer rnd,const char* Word,RGBA fg) Texture Font::renderText(Renderer rnd,const char* Word,RGBA fg)
{ {
Surface surf; 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); Texture t=rnd.render(surf);
return t; return t;
} }

View File

@ -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() Rect::Rect()
{ {
x=y=w=h=0; 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;
}

View File

@ -6,6 +6,12 @@ Renderer::Renderer(const Renderer& inc) : Renderer()
{ {
*pimpl=*(inc.pimpl); *pimpl=*(inc.pimpl);
} }
Renderer& Renderer::operator = (const Renderer& inc)
{
*pimpl=*(inc.pimpl);
return *this;
}
Renderer::~Renderer() Renderer::~Renderer()
{ {
delete pimpl; delete pimpl;
@ -17,15 +23,13 @@ int Renderer::clear()
Texture Renderer::loadImage(const char* FileName) Texture Renderer::loadImage(const char* FileName)
{ {
Texture t; Texture t;
t.pimpl->sText.reset(IMG_LoadTexture(pimpl->sRnd.get(),FileName),SDL_DestroyTexture); t.pimpl->set(IMG_LoadTexture(pimpl->sRnd.get(),FileName));
t.updateSize();
return t; return t;
} }
Texture Renderer::render(Surface surface) Texture Renderer::render(Surface surface)
{ {
Texture t; Texture t;
t.pimpl->sText.reset(SDL_CreateTextureFromSurface(pimpl->sRnd.get(),surface.pimpl->sSurf.get()),SDL_DestroyTexture); t.pimpl->set(SDL_CreateTextureFromSurface(pimpl->sRnd.get(),surface.pimpl->getRawSurface()));
t.updateSize();
return t; return t;
} }
@ -33,23 +37,24 @@ void Renderer::update()
{ {
SDL_RenderPresent(pimpl->sRnd.get()); SDL_RenderPresent(pimpl->sRnd.get());
} }
int Renderer::copy(Texture t,Rect src,Rect dst) int Renderer::copy(Texture t,Rect src,Rect dst)
{ {
SDL_Rect s=src.toSDLRect(); SDL_Rect s=src.toSDLRect();
SDL_Rect d=dst.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) int Renderer::copyTo(Texture t,Rect dst)
{ {
SDL_Rect d=dst.toSDLRect(); 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) int Renderer::copyFill(Texture t,Rect src)
{ {
SDL_Rect s=src.toSDLRect(); 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) 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);
} }

View File

@ -6,6 +6,12 @@ Texture::Texture(const Texture& inc) : Texture()
{ {
*pimpl=*(inc.pimpl); *pimpl=*(inc.pimpl);
} }
Texture& Texture::operator = (const Texture& inc)
{
*pimpl=*(inc.pimpl);
return *this;
}
int Texture::getw() int Texture::getw()
{ {
return pimpl->w; return pimpl->w;
@ -18,7 +24,3 @@ Texture::~Texture()
{ {
delete pimpl; delete pimpl;
} }
int Texture::updateSize()
{
return SDL_QueryTexture(pimpl->sText.get(),NULL,NULL,&pimpl->w,&pimpl->h);
}

View File

@ -1,8 +1,8 @@
Window::Window(int winw,int winh) Window::Window(int winw,int winh)
{ {
pimpl=new impl; pimpl=new impl;
pimpl->sWnd.reset(SDL_CreateWindow("Engine",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,winw,winh,SDL_WINDOW_SHOWN),SDL_DestroyWindow); SDL_Window* wnd=SDL_CreateWindow("Engine",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,winw,winh,SDL_WINDOW_SHOWN);
pimpl->rnd.pimpl->sRnd.reset(SDL_CreateRenderer(pimpl->sWnd.get(),-1,SDL_RENDERER_ACCELERATED|SDL_RENDERER_TARGETTEXTURE),SDL_DestroyRenderer); pimpl->set(wnd);
} }
Window::~Window() Window::~Window()
@ -14,3 +14,8 @@ Renderer Window::getRenderer()
{ {
return pimpl->rnd; return pimpl->rnd;
} }
Window::Window(const Window& inc) : Window(DEFAULT_WIDTH,DEFAULT_HEIGHT)
{
*pimpl=*(inc.pimpl);
}