[update] SDL Engine

This commit is contained in:
Kirigaya Kazuto 2017-01-05 13:25:09 +08:00
parent 57f0e87542
commit 9e8f43d7ae
10 changed files with 225 additions and 187 deletions

View File

@ -9,10 +9,5 @@ namespace App
{
Window wnd(1024,768);
Renderer rnd=wnd.getRenderer();
Texture text=rnd.loadImage("sample.jpg");
rnd.clear();
rnd.copyFullFill(text);
rnd.update();
SDL_Delay(3000);
}
}

View File

@ -3,16 +3,6 @@
#include <memory>
using namespace std;
SDL_Texture* RenderText(SDL_Renderer* rnd,TTF_Font* font,const char* Text,SDL_Color color,int* pw,int* ph)
{
SDL_Surface* surf=TTF_RenderText_Blended(font,Text,color);
if(surf==NULL) return NULL;
SDL_Texture* texture=SDL_CreateTextureFromSurface(rnd,surf);
SDL_FreeSurface(surf);
if(pw&&ph) SDL_QueryTexture(texture,NULL,NULL,pw,ph);
return texture;
}
SDL_Texture* RenderUTF8(SDL_Renderer* rnd,TTF_Font* font,const char* Text,SDL_Color color,int* pw,int* ph)
{
SDL_Surface* surf=TTF_RenderUTF8_Blended(font,Text,color);
@ -23,19 +13,6 @@ SDL_Texture* RenderUTF8(SDL_Renderer* rnd,TTF_Font* font,const char* Text,SDL_Co
return texture;
}
void TextureDraw(SDL_Renderer* rnd,SDL_Texture* texture,int dstx,int dsty)
{
if(!rnd||!texture) return;
int w,h;
SDL_QueryTexture(texture,NULL,NULL,&w,&h);
SDL_Rect rect;
rect.x=dstx;
rect.y=dsty;
rect.w=w;
rect.h=h;
SDL_RenderCopy(rnd,texture,NULL,&rect);
}
bool isInRect(int x,int y,SDL_Rect rect)
{
return ((x>=rect.x&&x<=rect.x+rect.w)&&(y>=rect.y&&y<=rect.y+rect.h));
@ -67,30 +44,8 @@ int MyChangeDir(const char* DirName)
namespace Engine
{
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;
}
Rect::Rect(int incx,int incy,int incw,int inch)
{
x=incx;
y=incy;
w=incw;
h=inch;
}
Rect::~Rect()
{
}
/// Rect
#include "sdl_engine_rect.hpp"
struct Window::impl
{
@ -109,144 +64,29 @@ struct Texture::impl
int w,h;
};
Window::Window(int winw,int winh)
struct Surface::impl
{
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);
}
Window::~Window()
{
delete pimpl;
}
Renderer Window::getRenderer()
{
return pimpl->rnd;
}
Renderer::Renderer()
{
pimpl=new impl;
}
Renderer::~Renderer()
{
delete pimpl;
}
int Renderer::clear()
{
return SDL_RenderClear(pimpl->sRnd.get());
}
Texture Renderer::loadImage(const char* FileName)
{
Texture t;
t.pimpl->sText.reset(IMG_LoadTexture(pimpl->sRnd.get(),FileName),SDL_DestroyTexture);
t.updateSize();
return t;
}
Texture Renderer::render(Surface surface)
{
Texture t;
t.pimpl->sText.reset(SDL_CreateTextureFromSurface(pimpl->sRnd.get(),surface.pimpl->sSurf.get()));
t.updateSize();
return t;
}
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);
}
int Renderer::copyTo(Texture t,Rect dst)
{
SDL_Rect d=dst.toSDLRect();
return SDL_RenderCopy(pimpl->sRnd.get(),t.pimpl->sText.get(),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);
}
int Renderer::copyFullFill(Texture t)
{
return SDL_RenderCopy(pimpl->sRnd.get(),t.pimpl->sText.get(),NULL,NULL);
}
Texture::Texture()
{
pimpl=new impl;
}
int Texture::getw()
{
return pimpl->w;
}
int Texture::geth()
{
return pimpl->h;
}
Texture::~Texture()
{
delete pimpl;
}
int Texture::updateSize()
{
return SDL_QueryTexture(pimpl->sText.get(),NULL,NULL,&pimpl->w,&pimpl->h);
}
RGBA::RGBA()
{
r=g=b=a=0;
}
RGBA::RGBA(int incr,int incg,int incb,int inca)
{
r=incr;
g=incg;
b=incb;
a=inca;
}
SDL_Color RGBA::toSDLColor()
{
SDL_Color color;
color.r=r;
color.g=g;
color.b=b;
color.a=a;
return color;
}
shared_ptr<SDL_Surface> sSurf;
};
struct Font::impl
{
shared_ptr<TTF_Font> sTTF;
};
Font::Font()
{
pimpl=new impl;
}
/// Window
#include "sdl_engine_window.hpp"
/// Renderer
#include "sdl_engine_renderer.hpp"
/// Surface
#include "sdl_engine_surface.hpp"
/// Texture
#include "sdl_engine_texture.hpp"
/// RGBA
#include "sdl_engine_rgba.hpp"
/// Font
#include "sdl_engine_font.hpp"
Font::Font(const char* FontFileName,int sz) : Font()
{
use(FontFileName,sz);
}
int Font::use(const char* FontFileName,int sz)
{
TTF_Font* font=TTF_OpenFont(FontFileName,sz);
if(font==NULL) return -1;
pimpl->sTTF.reset(font,TTF_CloseFont);
return 0;
}
Font::~Font()
{
delete pimpl;
}
Texture
}/// End of namespace Engine

View File

@ -10,9 +10,6 @@ void ClearMessageQueue();
int MyChangeDir(const char* DirName);
extern SDL_Color color_white;
extern SDL_Color color_black;
namespace Engine
{
class Rect
@ -28,12 +25,27 @@ public:
class Renderer;
class Texture;
class Font;
#define _SDL_ENGINE_IMPL_COPY_DECL(ClassName) \
ClassName(const ClassName&); \
ClassName(ClassName&&); \
ClassName& operator = (const ClassName&);
#define _SDL_ENGINE_IMPL \
struct impl; \
impl* pimpl;
class Window
{
public:
Window(int winw,int winh);
~Window();
Window(Window&&);
Window(const Window&);
Window& operator = (const Window&);
Renderer getRenderer();
Rect getSize();
void setSize(Rect r);
@ -46,18 +58,22 @@ class Surface
{
public:
~Surface();
_SDL_ENGINE_IMPL_COPY_DECL(Surface);
protected:
Surface();
private:
struct impl;
impl* pimpl;
friend class Renderer;
friend class Font;
};
class Renderer
{
public:
~Renderer();
_SDL_ENGINE_IMPL_COPY_DECL(Renderer);
int clear();
void update();
int copy(Texture t,Rect src,Rect dst);
@ -66,6 +82,7 @@ public:
int copyFullFill(Texture t);
Texture loadImage(const char* FileName);
Texture render(Surface surface);
protected:
Renderer();
private:
@ -80,6 +97,7 @@ public:
~Texture();
int getw();
int geth();
_SDL_ENGINE_IMPL_COPY_DECL(Texture);
protected:
Texture();
int updateSize();
@ -106,9 +124,10 @@ class Font
public:
Font();
Font(const char* FontFileName,int sz);
int use(const char* FontFileName,int sz);
~Font();
int use(const char* FontFileName,int sz);
_SDL_ENGINE_IMPL_COPY_DECL(Font);
Texture renderText(Renderer rnd,const char* Word,RGBA fg);
Texture renderTextShaded(Renderer rnd,const char* Word,RGBA fg,RGBA bg);
@ -117,7 +136,6 @@ public:
Texture renderUTF8(Renderer rnd,const char* Word,RGBA fg);
Texture renderUTF8Shaded(Renderer rnd,const char* Word,RGBA fg,RGBA bg);
Texture renderUTF8Solid(Renderer rnd,const char* Word,RGBA fg);
private:
struct impl;
impl* pimpl;

30
sdl_engine_font.hpp Normal file
View File

@ -0,0 +1,30 @@
Font::Font()
{
pimpl=new impl;
}
Font::Font(const char* FontFileName,int sz) : Font()
{
use(FontFileName,sz);
}
int Font::use(const char* FontFileName,int sz)
{
TTF_Font* font=TTF_OpenFont(FontFileName,sz);
if(font==NULL) return -1;
pimpl->sTTF.reset(font,TTF_CloseFont);
return 0;
}
Font::~Font()
{
delete pimpl;
}
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);
Texture t=rnd.render(surf);
return t;
}

24
sdl_engine_rect.hpp Normal file
View File

@ -0,0 +1,24 @@
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;
}
Rect::Rect(int incx,int incy,int incw,int inch)
{
x=incx;
y=incy;
w=incw;
h=inch;
}
Rect::~Rect()
{
}

55
sdl_engine_renderer.hpp Normal file
View File

@ -0,0 +1,55 @@
Renderer::Renderer()
{
pimpl=new impl;
}
Renderer::Renderer(const Renderer& inc) : Renderer()
{
*pimpl=*(inc.pimpl);
}
Renderer::~Renderer()
{
delete pimpl;
}
int Renderer::clear()
{
return SDL_RenderClear(pimpl->sRnd.get());
}
Texture Renderer::loadImage(const char* FileName)
{
Texture t;
t.pimpl->sText.reset(IMG_LoadTexture(pimpl->sRnd.get(),FileName),SDL_DestroyTexture);
t.updateSize();
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();
return t;
}
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);
}
int Renderer::copyTo(Texture t,Rect dst)
{
SDL_Rect d=dst.toSDLRect();
return SDL_RenderCopy(pimpl->sRnd.get(),t.pimpl->sText.get(),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);
}
int Renderer::copyFullFill(Texture t)
{
return SDL_RenderCopy(pimpl->sRnd.get(),t.pimpl->sText.get(),NULL,NULL);
}

24
sdl_engine_rgba.hpp Normal file
View File

@ -0,0 +1,24 @@
RGBA::RGBA()
{
r=g=b=a=0;
}
RGBA::RGBA(int incr,int incg,int incb,int inca)
{
r=incr;
g=incg;
b=incb;
a=inca;
}
SDL_Color RGBA::toSDLColor()
{
SDL_Color color;
color.r=r;
color.g=g;
color.b=b;
color.a=a;
return color;
}
RGBA::~RGBA()
{
}

12
sdl_engine_surface.hpp Normal file
View File

@ -0,0 +1,12 @@
Surface::Surface()
{
pimpl=new impl;
}
Surface::~Surface()
{
delete pimpl;
}
Surface::Surface(const Surface& inc) : Surface()
{
*pimpl=*(inc.pimpl);
}

24
sdl_engine_texture.hpp Normal file
View File

@ -0,0 +1,24 @@
Texture::Texture()
{
pimpl=new impl;
}
Texture::Texture(const Texture& inc) : Texture()
{
*pimpl=*(inc.pimpl);
}
int Texture::getw()
{
return pimpl->w;
}
int Texture::geth()
{
return pimpl->h;
}
Texture::~Texture()
{
delete pimpl;
}
int Texture::updateSize()
{
return SDL_QueryTexture(pimpl->sText.get(),NULL,NULL,&pimpl->w,&pimpl->h);
}

16
sdl_engine_window.hpp Normal file
View File

@ -0,0 +1,16 @@
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);
}
Window::~Window()
{
delete pimpl;
}
Renderer Window::getRenderer()
{
return pimpl->rnd;
}