[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
#include "sdl_engine_rect.hpp"
struct Window::impl
{
shared_ptr<SDL_Window> sWnd;
Renderer rnd;
};
struct Renderer::impl
{
private:
friend class Renderer;
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
{
private:
friend class Texture;
shared_ptr<SDL_Texture> 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<SDL_Surface> 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<TTF_Font> sTTF;
public:
void set(TTF_Font* font)
{
sTTF.reset(font,TTF_CloseFont);
}
};
/// Window

View File

@ -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;

View File

@ -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;
}

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()
{
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);
}
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);
}

View File

@ -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);
}

View File

@ -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);
}