Merge branch 'mingw-dev' into pre-merge

This commit is contained in:
Kirigaya Kazuto 2017-04-10 20:49:07 +08:00
commit 7f238b9935
2 changed files with 152 additions and 8 deletions

View File

@ -129,6 +129,32 @@ namespace MiniEngine
return str.c_str(); return str.c_str();
} }
RWOP::RWOP(FILE* fp,bool autoclose)
{
SDL_bool b=autoclose?SDL_TRUE:SDL_FALSE;
op.reset(SDL_RWFromFP(fp,b),[](SDL_RWops* p){SDL_RWclose(p);});
}
RWOP::RWOP(const std::string& filename,const std::string& openmode)
{
op.reset(SDL_RWFromFile(filename.c_str(),openmode.c_str()),[](SDL_RWops* p){SDL_RWclose(p);});
}
RWOP::RWOP(const void* mem,int size)
{
op.reset(SDL_RWFromConstMem(mem,size),[](SDL_RWops* p){SDL_RWclose(p);});
}
RWOP::RWOP(void* mem,int size)
{
op.reset(SDL_RWFromMem(mem,size),[](SDL_RWops* p){SDL_RWclose(p);});
}
int Surface::savePNG(const std::string& filename)
{
return IMG_SavePNG(surf.get(),filename.c_str());
}
Texture::Texture() Texture::Texture()
{ {
updateInfo(); updateInfo();
@ -435,10 +461,10 @@ namespace MiniEngine
void Window::setRenderer(std::initializer_list<RendererType> RendererFlags) void Window::setRenderer(std::initializer_list<RendererType> RendererFlags)
{ {
int flag = 0; Uint32 flag = 0;
for (auto v : RendererFlags) for (auto v : RendererFlags)
{ {
flag |= static_cast<int>(v); flag |= _render_caster(v);
} }
_setRenderer_Real(flag); _setRenderer_Real(flag);
} }
@ -533,6 +559,24 @@ namespace MiniEngine
return s; return s;
} }
Uint32 Window::_render_caster(RendererType Type)
{
switch(Type)
{
case RendererType::Accelerated:
return SDL_RENDERER_ACCELERATED;
case RendererType::PresentSync:
return SDL_RENDERER_PRESENTVSYNC;
case RendererType::Software:
return SDL_RENDERER_SOFTWARE;
case RendererType::TargetTexture:
return SDL_RENDERER_TARGETTEXTURE;
}
/// If an error occurs, return 0 by default.
return 0;
}
void Window::_setRenderer_Real(Uint32 flags) void Window::_setRenderer_Real(Uint32 flags)
{ {
winrnd.rnd.reset(SDL_CreateRenderer(wnd.get(), -1, flags), SDL_DestroyRenderer); winrnd.rnd.reset(SDL_CreateRenderer(wnd.get(), -1, flags), SDL_DestroyRenderer);
@ -579,6 +623,31 @@ namespace MiniEngine
return (font.get() != nullptr); return (font.get() != nullptr);
} }
void Font::_real_setFontStyle(int Style)
{
TTF_SetFontStyle(font.get(),Style);
}
int Font::_style_caster(Style style)
{
switch(style)
{
case Style::Bold:
return TTF_STYLE_BOLD;
case Style::Italic:
return TTF_STYLE_ITALIC;
case Style::Normal:
return TTF_STYLE_NORMAL;
case Style::StrikeThrough:
return TTF_STYLE_STRIKETHROUGH;
case Style::UnderLine:
return TTF_STYLE_UNDERLINE;
}
/// If an error occurs, return 0 instead of -1.
return 0;
}
Texture Font::renderText(Renderer rnd, std::string Text, RGBA fg) Texture Font::renderText(Renderer rnd, std::string Text, RGBA fg)
{ {
Surface surf; Surface surf;

View File

@ -72,6 +72,19 @@ namespace MiniEngine
std::string str; std::string str;
}; };
class RWOP
{
public:
RWOP(FILE* fp,bool autoclose);
RWOP(const std::string& filename,const std::string& openmode);
RWOP(const void* mem,int size);
RWOP(void* mem,int size);
RWOP()=default;
~RWOP()=default;
private:
std::shared_ptr<SDL_RWops> op;
};
enum class BlendMode enum class BlendMode
{ {
None = SDL_BLENDMODE_NONE, None = SDL_BLENDMODE_NONE,
@ -84,6 +97,7 @@ namespace MiniEngine
{ {
public: public:
~Surface() = default; ~Surface() = default;
int savePNG(const std::string& filename);
protected: protected:
Surface() = default; Surface() = default;
private: private:
@ -124,10 +138,10 @@ namespace MiniEngine
enum class RendererType enum class RendererType
{ {
Software = SDL_RENDERER_SOFTWARE, Software,
Accelerated = SDL_RENDERER_ACCELERATED, Accelerated,
PresentSync = SDL_RENDERER_PRESENTVSYNC, PresentSync,
TargetTexture = SDL_RENDERER_TARGETTEXTURE TargetTexture
}; };
enum class FlipMode { None, Horizontal, Vertical }; enum class FlipMode { None, Horizontal, Vertical };
@ -135,6 +149,7 @@ namespace MiniEngine
class Renderer class Renderer
{ {
public: public:
Renderer() = default;
int setColor(RGBA pack); int setColor(RGBA pack);
RGBA getColor(); RGBA getColor();
int setBlendMode(BlendMode mode); int setBlendMode(BlendMode mode);
@ -163,7 +178,6 @@ namespace MiniEngine
Texture loadTexture(std::string FileName) throw(ErrorViewer); Texture loadTexture(std::string FileName) throw(ErrorViewer);
Texture createTexture(int Width, int Height) throw(ErrorViewer); Texture createTexture(int Width, int Height) throw(ErrorViewer);
Renderer() = default;
bool isReady(); bool isReady();
private: private:
std::shared_ptr<SDL_Renderer> rnd; std::shared_ptr<SDL_Renderer> rnd;
@ -178,7 +192,20 @@ namespace MiniEngine
Window(std::string Title, int Width, int Height, std::initializer_list<RendererType> RendererFlags = { RendererType::Accelerated,RendererType::TargetTexture }) throw(ErrorViewer); Window(std::string Title, int Width, int Height, std::initializer_list<RendererType> RendererFlags = { RendererType::Accelerated,RendererType::TargetTexture }) throw(ErrorViewer);
Renderer getRenderer() const; Renderer getRenderer() const;
void setRenderer(std::initializer_list<RendererType> RendererFlags); void setRenderer(RendererType Type)
{
_internal_rndflagcalc=0;
_setRenderer(Type);
}
template<typename... Args>
void setRenderer(RendererType Type,Args&&... args)
{
_internal_rndflagcalc=0;
_setRenderer(Type,std::forward<RendererType>(args...));
}
void setRenderer(std::initializer_list<RendererType>);
Rect getSize(); Rect getSize();
void setSize(Rect sizeRect); void setSize(Rect sizeRect);
@ -207,8 +234,23 @@ namespace MiniEngine
_DECL_DEPRECATED Surface getSurface(); _DECL_DEPRECATED Surface getSurface();
protected:
template<typename... Args>
void _setRenderer(RendererType Type,Args&&... args)
{
_internal_rndflagcalc|=_render_caster(Type);
_setRenderer(args...);
}
void _setRenderer(RendererType Type)
{
_internal_rndflagcalc|=_render_caster(Type);
_setRenderer_Real(_internal_rndflagcalc);
}
private: private:
void _setRenderer_Real(Uint32 flags); void _setRenderer_Real(Uint32 flags);
Uint32 _internal_rndflagcalc;
Uint32 _render_caster(RendererType);
std::shared_ptr<SDL_Window> wnd; std::shared_ptr<SDL_Window> wnd;
Renderer winrnd; Renderer winrnd;
}; };
@ -216,10 +258,27 @@ namespace MiniEngine
class Font class Font
{ {
public: public:
enum class Style { Normal, Bold, Italic, UnderLine, StrikeThrough };
Font() = default; Font() = default;
Font(std::string FontFileName, int size) throw(ErrorViewer); Font(std::string FontFileName, int size) throw(ErrorViewer);
int use(std::string FontFileName, int size); int use(std::string FontFileName, int size);
bool isReady(); bool isReady();
template<typename... Args>
void setFontStyle(Style style,Args&&... args)
{
_internal_fontcalc=0;
_setFontStyle(style,std::forward(args...));
}
void setFontStyle(Style style)
{
_real_setFontStyle(_style_caster(style));
}
std::tuple<Style> getFontStyles();
Texture renderText(Renderer rnd, std::string Text, RGBA fg); Texture renderText(Renderer rnd, std::string Text, RGBA fg);
Texture renderTextWrapped(Renderer rnd, std::string Text, RGBA fg, int WrapLength); Texture renderTextWrapped(Renderer rnd, std::string Text, RGBA fg, int WrapLength);
Texture renderTextShaded(Renderer rnd, std::string Text, RGBA fg, RGBA bg); Texture renderTextShaded(Renderer rnd, std::string Text, RGBA fg, RGBA bg);
@ -229,7 +288,23 @@ namespace MiniEngine
Texture renderUTF8Wrapped(Renderer rnd, std::string Text, RGBA fg, int WrapLength); Texture renderUTF8Wrapped(Renderer rnd, std::string Text, RGBA fg, int WrapLength);
Texture renderUTF8Shaded(Renderer rnd, std::string Text, RGBA fg, RGBA bg); Texture renderUTF8Shaded(Renderer rnd, std::string Text, RGBA fg, RGBA bg);
Texture renderUTF8Solid(Renderer rnd, std::string Text, RGBA fg); Texture renderUTF8Solid(Renderer rnd, std::string Text, RGBA fg);
protected:
template<typename... Args>
void _setFontStyle(Style style,Args&&... args)
{
_internal_fontcalc|=_style_caster(style);
_setFontStyle(args...);
}
void _setFontStyle(Style style)
{
_internal_fontcalc|=_style_caster(style);
_real_setFontStyle(_internal_fontcalc);
}
private: private:
void _real_setFontStyle(int);
int _style_caster(Style);
int _internal_fontcalc;
std::shared_ptr<TTF_Font> font; std::shared_ptr<TTF_Font> font;
}; };