Move Font,Music,Renderer,Texture,Timer out

This commit is contained in:
Kirigaya Kazuto 2017-06-18 17:36:13 +08:00
parent 62b86cb072
commit dfa628cd14
14 changed files with 1760 additions and 1702 deletions

File diff suppressed because it is too large Load Diff

View File

@ -9,15 +9,7 @@
namespace MiniEngine
{
class Point
{
public:
int x, y;
Point(int X, int Y);
Point();
SDL_Point toSDLPoint() const;
bool inRect(const Rect& rect) const;
};
class NonCopyable
{
@ -60,40 +52,7 @@ namespace MiniEngine
enum class BlendMode { None,Blend,Add,Mod };
class Texture
{
public:
Texture();
~Texture() = default;
Rect getSize();
int getw() const;
int geth() const;
bool isReady() const;
int setBlendMode(BlendMode mode);
BlendMode getBlendMode() const;
/// Alpha: 0: Transparent 255: opaque
int setAlphaMode(int alpha);
int getAlphaMode() const;
ColorMode getColorMode() const;
int setColorMode(ColorMode mode);
RGBA getRGBA() const;
void setRGBA(const RGBA& pack);
void release();
protected:
/// updateInfo() must be called after Texture is changed.
void updateInfo();
private:
std::shared_ptr<SDL_Texture> _text;
void _set(SDL_Texture*);
/// Just used for "SDL_GetRenderTarget"
void _set_no_delete(SDL_Texture*);
void _clear();
SDL_Texture* _get() const;
Rect rect;
friend class Renderer;
};
enum class SystemCursorType
{
@ -239,249 +198,11 @@ namespace MiniEngine
enum class FlipMode { None, Horizontal, Vertical };
class Renderer
{
public:
Renderer() = default;
Renderer(Window& wnd,std::initializer_list<RendererType> RendererFlags = { RendererType::Accelerated,RendererType::TargetTexture }) throw (ErrorViewer);
~Renderer() = default;
/// If Renderer is current not ready, setRenderer will create Renderer.
/// Otherwise, setRenderer will fail.
int createRenderer(Window& wnd,RendererType Type)
{
int flagcalc=0;
return _createRenderer(wnd,flagcalc,Type);
}
template<typename... Args>
int createRenderer(Window& wnd,RendererType Type,Args&&... args)
{
int flagcalc=0;
return _createRenderer(wnd,flagcalc,Type,std::forward<RendererType>(args...));
}
int createRenderer(Window& wnd,std::initializer_list<RendererType>);
int setColor(const RGBA& pack);
RGBA getColor() const;
int setBlendMode(BlendMode mode);
BlendMode getBlendMode() const;
int setTarget(Texture& t);
int setTarget();
Texture getTarget();
int fillRect(const Rect& rect);
int drawRect(const Rect& rect);
int drawPoint(const Point& p);
int drawLine(const Point& A,const Point& B);
/// Experimental
int fillRects(const SDL_Rect* pRectArray,int n);
int drawRects(const SDL_Rect* pRectArray,int n);
int drawPoints(const SDL_Point* pPointArray,int n);
int drawLines(const SDL_Point* pPointArray,int n);
/// Experimental
int fillRects(const std::vector<SDL_Rect>& rectvec);
int drawRects(const std::vector<SDL_Rect>& rectvec);
int drawPoints(const std::vector<SDL_Point>& pointvec);
int drawLines(const std::vector<SDL_Point>& pointvec);
int setScale(float scaleX,float scaleY);
std::tuple<float,float> getScale() const;
int setViewport(const Rect& viewport);
Rect getViewport() const;
int setLogicalSize(int w,int h);
Rect getLogicalSize() const;
int setClipRect(const Rect& cliprect);
Rect getClipRect() const;
bool isClipEnabled() const;
Rect getOutputSize() const;
int clear();
void update();
int copy(const Texture& t, const Rect& src, const Rect& dst);
int copyTo(const Texture& t, const Rect& dst);
int copyTo(const Texture& t, const Point& lupoint);
int copyFill(const Texture& t, const Rect& src);
int copyFullFill(const Texture& t);
/// Super copy without center point.
int copy(const Texture& t, const Rect& src, const Rect& dst,double angle,FlipMode mode);
int copyTo(const Texture& t, const Rect& dst,double angle,FlipMode mode);
int copyTo(const Texture& t, const Point& lupoint,double angle,FlipMode mode);
int copyFill(const Texture& t, const Rect& src,double angle,FlipMode mode);
int copyFullFill(const Texture& t,double angle,FlipMode mode);
/// Super copy with center point
int copy(const Texture& t, const Rect& src, const Rect& dst,const Point& centerPoint,double angle,FlipMode mode);
int copyTo(const Texture& t, const Rect& dst,const Point& centerPoint,double angle,FlipMode mode);
int copyTo(const Texture& t, const Point& lupoint,const Point& centerPoint,double angle,FlipMode mode);
int copyFill(const Texture& t, const Rect& src,const Point& centerPoint,double angle,FlipMode mode);
int copyFullFill(const Texture& t,const Point& centerPoint,double angle,FlipMode mode);
/// Reserved for compatibility
int supercopy(const Texture& t,
bool srcfull,const Rect& src,bool dstfull,const Rect& dst,
double angle,
bool haspoint,const Point& center,FlipMode mode);
Texture render(const Surface& surf) const throw (ErrorViewer);
Texture loadTexture(const std::string& FileName) const throw(ErrorViewer);
Texture loadTextureRW(const RWOP& rwop) const throw(ErrorViewer);
Texture createTexture(int Width, int Height) const throw(ErrorViewer);
bool isRenderTargetSupported() const;
bool isReady() const;
void release();
/// Experimental
static int GetDriversNum();
protected:
template<typename... Args>
int _createRenderer(Window& wnd,int& refcalc,RendererType Type,Args&&... args)
{
refcalc|=_rendertype_caster(Type);
return _createRenderer(wnd,refcalc,args...);
}
// template<>
int _createRenderer(Window& wnd,int& refcalc,RendererType Type)
{
refcalc|=_rendertype_caster(Type);
return _createRenderer_Real(wnd,refcalc);
}
private:
std::shared_ptr<SDL_Renderer> _rnd;
int _createRenderer_Real(Window& wnd,Uint32 flags);
Uint32 _rendertype_caster(RendererType);
void _set(SDL_Renderer*);
void _clear();
SDL_Renderer* _get() const;
};
enum class FontStyle { Normal, Bold, Italic, UnderLine, StrikeThrough };
enum class FontHint { Normal, Light, Mono, None , Error };
class Font
{
public:
Font() = default;
Font(std::string FontFileName, size_t size) throw(ErrorViewer);
int use(std::string FontFileName, size_t size);
bool isReady() const;
bool isNormal() const;
bool isBold() const;
bool isItalic() const;
bool isUnderLine() const;
bool isStrikeThrough() const;
void setNormal();
void setBold(bool);
void setItalic(bool);
void setUnderLine(bool);
void setStrikeThrough(bool);
template<typename... Args>
void setFontStyle(FontStyle style,Args&&... args)
{
int fontcalc=0;
_setFontStyle(fontcalc,style,args...);
}
void setFontStyle(FontStyle style)
{
int fontcalc=0;
_setFontStyle(fontcalc,style);
}
std::vector<FontStyle> getFontStyles() const;
int getFontHeight() const;
int getFontAscent() const;
int getFontDescent() const;
int getFontLineSkip() const;
bool isFontKerning() const;
void setFontKerning(bool enableKerning);
long getFontFaceNum() const;
int getFontFaceIsFixedWidth() const;
std::string getFontFaceFamilyName() const;
std::string getFontFaceStyleName() const;
FontHint getFontHint() const;
void setFontHint(FontHint hint);
Rect sizeText(const std::string& Text) const throw (ErrorViewer);
Rect sizeUTF8(const std::string& Text) const throw (ErrorViewer);
Rect sizeUnicode(const uint16_t* Text) const throw (ErrorViewer);
/// Surface Rendering Functions.
Surface renderText(const std::string& Text, const RGBA& fg) const;
Surface renderTextWrapped(const std::string& Text, const RGBA& fg, size_t WrapLength) const;
Surface renderTextShaded(const std::string& Text, const RGBA& fg, const RGBA& bg) const;
Surface renderTextSolid(const std::string& Text, const RGBA& fg) const;
Surface renderUTF8(const std::string& Text, const RGBA& fg) const;
Surface renderUTF8Wrapped(const std::string& Text, const RGBA& fg, size_t WrapLength) const;
Surface renderUTF8Shaded(const std::string& Text, const RGBA& fg, const RGBA& bg) const;
Surface renderUTF8Solid(const std::string& Text, const RGBA& fg) const;
Surface renderUnicode(const uint16_t* Text,const RGBA& fg) const;
Surface renderUnicodeWrapped(const uint16_t* Text,const RGBA& fg,size_t WrapLength) const;
Surface renderUnicodeShaded(const uint16_t* Text,const RGBA& fg,const RGBA& bg) const;
Surface renderUnicodeSolid(const uint16_t* Text,const RGBA& fg) const;
/// Texture Rendering Functions.
Texture renderText(const Renderer& rnd, const std::string& Text, const RGBA& fg) const;
Texture renderTextWrapped(const Renderer& rnd, const std::string& Text, const RGBA& fg, size_t WrapLength) const;
Texture renderTextShaded(const Renderer& rnd, const std::string& Text, const RGBA& fg, const RGBA& bg) const;
Texture renderTextSolid(const Renderer& rnd, const std::string& Text, const RGBA& fg) const;
Texture renderUTF8(const Renderer& rnd, const std::string& Text, const RGBA& fg) const;
Texture renderUTF8Wrapped(const Renderer& rnd, const std::string& Text, const RGBA& fg, size_t WrapLength) const;
Texture renderUTF8Shaded(const Renderer& rnd, const std::string& Text, const RGBA& fg, const RGBA& bg) const;
Texture renderUTF8Solid(const Renderer& rnd, const std::string& Text, const RGBA& fg) const;
Texture renderUnicode(const Renderer& rnd,const uint16_t* Text,const RGBA& fg) const;
Texture renderUnicodeWrapped(const Renderer& rnd,const uint16_t* Text,const RGBA& fg,size_t WrapLength) const;
Texture renderUnicodeShaded(const Renderer& rnd,const uint16_t* Text,const RGBA& fg,const RGBA& bg) const;
Texture renderUnicodeSolid(const Renderer& rnd,const uint16_t* Text,const RGBA& fg) const;
void release();
protected:
template<typename... Args>
void _setFontStyle(int& fontcalc,FontStyle style,Args&&... args)
{
fontcalc|=_style_caster(style);
_setFontStyle(fontcalc,args...);
}
void _setFontStyle(int& fontcalc,FontStyle style)
{
fontcalc|=_style_caster(style);
_real_setFontStyle(fontcalc);
}
private:
void _real_setFontStyle(int);
int _style_caster(FontStyle);
std::shared_ptr<TTF_Font> _font;
void _set(TTF_Font*);
void _clear();
TTF_Font* _get() const;
};
enum class Platform { Unknown,Windows,MacOS,Linux,iOS,Android };
enum class PowerState { Unknown,OnBattery,NoBattery,Charging,Charged };
@ -578,155 +299,8 @@ namespace MiniEngine
};
};
Uint32 _global_timer_executor(Uint32 interval,void* param);
class Timer
{
public:
Timer();
/// void func(Uint32,...)
template<typename VoidCallable,typename... Args>
Timer(Uint32 interval,VoidCallable&& vcallable,Args&&... args) : Timer()
{
auto realCall=[&,vcallable](Uint32 ims)->Uint32{vcallable(ims,args...);return ims;};
auto pfunc=new std::function<Uint32(Uint32 interval)>(realCall);
_real_timer_call(_global_timer_executor,interval,pfunc);
}
/// Uint32 func(Uint32,...)
template<typename Callable,typename... Args>
Timer(Callable&& callable,Uint32 interval,Args&&... args) : Timer()
{
auto realCall=[&,callable](Uint32 ims)->Uint32{return callable(ims,args...);};
auto pfunc=new std::function<Uint32(Uint32 interval)>(realCall);
_real_timer_call(_global_timer_executor,interval,pfunc);
}
/// Restore For Capability
Timer(SDL_TimerCallback callback,Uint32 interval,void* param);
int enable();
int disable();
bool isenable() const;
void detach();
~Timer();
static void _delete_delegator(std::function<Uint32(Uint32)>* Delegator);
private:
void _real_timer_call(SDL_TimerCallback callback,Uint32 interval,void* param);
SDL_TimerCallback _callback;
Uint32 _interval;
void* _param;
SDL_TimerID id;
bool _enabled;
bool _detached;
/// Reserved Variable For Template variable Parameter
bool _delete_on_disable;
};
class AudioPlayer
{
public:
AudioPlayer();
~AudioPlayer();
private:
class _Audio
{
public:
_Audio();
~_Audio();
};
static _Audio* _sysAudio;
static int _sysAudioCounter;
};
/// Forward Declaration
class Music
{
public:
protected:
Music() = default;
private:
std::shared_ptr<Mix_Music> _music;
void _set(Mix_Music*);
void _clear();
Mix_Music* _get();
friend class MusicPlayer;
};
class MusicPlayer : public AudioPlayer
{
public:
static int GetDecoderNum();
static std::string GetDecoderName(int index);
Music loadMusic(std::string Filename) throw (ErrorViewer);
int play(Music music, int loops);
void pause();
void resume();
void rewind();
int stop();
int fadeIn(int loops, int ms);
int fadeOut(int ms);
bool isPlaying();
bool isPaused();
int isFading();
/// Experimental
static int SetMusicPosition(double position);
private:
Music m;
};
class Sound
{
public:
protected:
Sound() = default;
private:
std::shared_ptr<Mix_Chunk> _sound;
void _set(Mix_Chunk*);
void _clear();
Mix_Chunk* _get();
friend class SoundPlayer;
};
typedef int ChannelID;
class SoundPlayer : public AudioPlayer
{
public:
static int GetDecoderNum();
static std::string GetDecoderName(int index);
SoundPlayer(int Channels = 16);
Sound loadSound(std::string Filename) throw (ErrorViewer);
ChannelID playSound(Sound sound, int loops) throw (ErrorViewer);
ChannelID fadein(Sound sound, int loops, int ms) throw (ErrorViewer);
int fadeout(ChannelID id, int ms);
void pause(ChannelID id);
void resume(ChannelID id);
int stop(ChannelID id);
/// Experimental
int setPanning(ChannelID id,uint8_t left,uint8_t right);
int setPosition(ChannelID id,int16_t angle,uint8_t distance);
int setDistance(ChannelID id,uint8_t distance);
int setReverseStereo(ChannelID id,int flip);
/// Experimental: Direct Add/Remove Effect
int addEffect(ChannelID id,Mix_EffectFunc_t f, Mix_EffectDone_t d, void *arg);
int removeEffect(ChannelID id,Mix_EffectFunc_t f);
int removeAllEffect(ChannelID id);
};
class StringEngine
{

394
SDLWrapper/Font.cpp Normal file
View File

@ -0,0 +1,394 @@
#include "Font.h"
#include "begin_code.h"
void Font::_set(TTF_Font* p)
{
_font.reset(p,TTF_CloseFont);
}
void Font::_clear()
{
_font.reset();
}
TTF_Font* Font::_get() const
{
return _font.get();
}
Font::Font(std::string FontFileName, size_t size) throw(ErrorViewer)
{
if (use(FontFileName, size) != 0)
{
ErrorViewer e;
e.fetch();
throw e;
}
}
int Font::use(std::string FontFileName, size_t size)
{
TTF_Font* temp = TTF_OpenFont(FontFileName.c_str(), size);
if (temp == NULL) return -1;
_set(temp);
return 0;
}
bool Font::isReady() const
{
return (_get() != nullptr);
}
bool Font::isNormal() const
{
return !(TTF_GetFontStyle(_get()));
}
bool Font::isBold() const
{
return (TTF_GetFontStyle(_get()) & TTF_STYLE_BOLD );
}
bool Font::isItalic() const
{
return (TTF_GetFontStyle(_get()) & TTF_STYLE_ITALIC );
}
bool Font::isUnderLine() const
{
return (TTF_GetFontStyle(_get()) & TTF_STYLE_UNDERLINE );
}
bool Font::isStrikeThrough() const
{
return (TTF_GetFontStyle(_get()) & TTF_STYLE_STRIKETHROUGH );
}
void Font::setNormal()
{
_real_setFontStyle(TTF_STYLE_NORMAL);
}
void Font::setBold(bool enable)
{
if( enable!=isBold() )
{
_real_setFontStyle( TTF_GetFontStyle(_get()) | (enable?TTF_STYLE_BOLD:!TTF_STYLE_BOLD) );
}
}
void Font::setItalic(bool enable)
{
if( enable!=isItalic() )
{
_real_setFontStyle( TTF_GetFontStyle(_get()) | (enable?TTF_STYLE_ITALIC:!TTF_STYLE_ITALIC) );
}
}
void Font::setUnderLine(bool enable)
{
if( enable!=isUnderLine() )
{
_real_setFontStyle( TTF_GetFontStyle(_get()) | (enable?TTF_STYLE_UNDERLINE:!TTF_STYLE_UNDERLINE) );
}
}
void Font::setStrikeThrough(bool enable)
{
if( enable!=isStrikeThrough() )
{
_real_setFontStyle( TTF_GetFontStyle(_get()) | (enable?TTF_STYLE_STRIKETHROUGH:!TTF_STYLE_STRIKETHROUGH) );
}
}
void Font::_real_setFontStyle(int Style)
{
TTF_SetFontStyle(_get(),Style);
}
int Font::_style_caster(FontStyle style)
{
return _internal::getTTFFontStyleFromFontStyle(style);
}
std::vector<FontStyle> Font::getFontStyles() const
{
int styles=TTF_GetFontStyle(_get());
return _internal::getFontStyleVecFromMixedTTFFontStyle(styles);
}
int Font::getFontHeight() const
{
return TTF_FontHeight(_get());
}
int Font::getFontAscent() const
{
return TTF_FontAscent(_get());
}
int Font::getFontDescent() const
{
return TTF_FontDescent(_get());
}
int Font::getFontLineSkip() const
{
return TTF_FontLineSkip(_get());
}
bool Font::isFontKerning() const
{
return (TTF_GetFontKerning(_get())!=0);
}
void Font::setFontKerning(bool enableKerning)
{
TTF_SetFontKerning(_get(),enableKerning?1:0);
}
long Font::getFontFaceNum() const
{
return TTF_FontFaces(_get());
}
int Font::getFontFaceIsFixedWidth() const
{
return TTF_FontFaceIsFixedWidth(_get());
}
std::string Font::getFontFaceFamilyName() const
{
return std::string(TTF_FontFaceFamilyName(_get()));
}
std::string Font::getFontFaceStyleName() const
{
return std::string(TTF_FontFaceStyleName(_get()));
}
FontHint Font::getFontHint() const
{
switch(TTF_GetFontHinting(_get()))
{
case TTF_HINTING_NORMAL:
return FontHint::Normal;
case TTF_HINTING_LIGHT:
return FontHint::Light;
case TTF_HINTING_MONO:
return FontHint::Mono;
case TTF_HINTING_NONE:
return FontHint::None;
}
/// Return Error on default.
return FontHint::Error;
}
void Font::setFontHint(FontHint hint)
{
int v=0;
switch(hint)
{
case FontHint::Normal:
v=TTF_HINTING_NORMAL;
break;
case FontHint::Light:
v=TTF_HINTING_LIGHT;
break;
case FontHint::Mono:
v=TTF_HINTING_MONO;
break;
case FontHint::None:
v=TTF_HINTING_NONE;
break;
case FontHint::Error:
/// No Action on FontHint::Error.
return;
}
TTF_SetFontHinting(_get(),v);
}
Rect Font::sizeText(const std::string& Text) const throw (ErrorViewer)
{
int w=0,h=0;
if(TTF_SizeText(_get(),Text.c_str(),&w,&h)!=0)
{
/// Something might be wrong
throw ErrorViewer();
}
return Rect(0,0,w,h);
}
Rect Font::sizeUTF8(const std::string& Text) const throw (ErrorViewer)
{
int w=0,h=0;
if(TTF_SizeUTF8(_get(),Text.c_str(),&w,&h)!=0)
{
/// Something might be wrong
throw ErrorViewer();
}
return Rect(0,0,w,h);
}
Rect Font::sizeUnicode(const uint16_t* Text) const throw (ErrorViewer)
{
int w=0,h=0;
if(TTF_SizeUNICODE(_get(),Text,&w,&h)!=0)
{
/// Something might be wrong
throw ErrorViewer();
}
return Rect(0,0,w,h);
}
/// rendering surfaces...
Surface Font::renderText(const std::string& Text,const RGBA& fg) const
{
Surface surf;
surf._set(TTF_RenderText_Blended(_get(), Text.c_str(), fg.toSDLColor()));
return surf;
}
Surface Font::renderTextWrapped(const std::string& Text, const RGBA& fg, size_t WrapLength) const
{
Surface surf;
surf._set(TTF_RenderText_Blended_Wrapped(_get(), Text.c_str(), fg.toSDLColor(), WrapLength));
return surf;
}
Surface Font::renderTextShaded(const std::string& Text, const RGBA& fg,const RGBA& bg) const
{
Surface surf;
surf._set(TTF_RenderText_Shaded(_get(), Text.c_str(), fg.toSDLColor(), bg.toSDLColor()));
return surf;
}
Surface Font::renderTextSolid(const std::string& Text,const RGBA& fg) const
{
Surface surf;
surf._set(TTF_RenderText_Solid(_get(), Text.c_str(), fg.toSDLColor()));
return surf;
}
Surface Font::renderUTF8(const std::string& Text,const RGBA& fg) const
{
Surface surf;
surf._set(TTF_RenderUTF8_Blended(_get(), Text.c_str(), fg.toSDLColor()));
return surf;
}
Surface Font::renderUTF8Wrapped(const std::string& Text, const RGBA& fg, size_t WrapLength) const
{
Surface surf;
surf._set(TTF_RenderUTF8_Blended_Wrapped(_get(), Text.c_str(), fg.toSDLColor(), WrapLength));
return surf;
}
Surface Font::renderUTF8Shaded(const std::string& Text, const RGBA& fg,const RGBA& bg) const
{
Surface surf;
surf._set(TTF_RenderUTF8_Shaded(_get(), Text.c_str(), fg.toSDLColor(), bg.toSDLColor()));
return surf;
}
Surface Font::renderUTF8Solid(const std::string& Text,const RGBA& fg) const
{
Surface surf;
surf._set(TTF_RenderUTF8_Solid(_get(), Text.c_str(), fg.toSDLColor()));
return surf;
}
Surface Font::renderUnicode(const uint16_t* Text, const RGBA& fg) const
{
Surface surf;
surf._set(TTF_RenderUNICODE_Blended(_get(),Text,fg.toSDLColor()));
return surf;
}
Surface Font::renderUnicodeWrapped(const uint16_t* Text, const RGBA& fg, size_t WrapLength) const
{
Surface surf;
surf._set(TTF_RenderUNICODE_Blended_Wrapped(_get(),Text,fg.toSDLColor(),WrapLength));
return surf;
}
Surface Font::renderUnicodeShaded(const uint16_t* Text, const RGBA& fg, const RGBA& bg) const
{
Surface surf;
surf._set(TTF_RenderUNICODE_Shaded(_get(),Text,fg.toSDLColor(),bg.toSDLColor()));
return surf;
}
Surface Font::renderUnicodeSolid(const uint16_t* Text, const RGBA& fg) const
{
Surface surf;
surf._set(TTF_RenderUNICODE_Solid(_get(),Text,fg.toSDLColor()));
return surf;
}
/// rendering textures...
Texture Font::renderText(const Renderer& rnd, const std::string& Text, const RGBA& fg) const
{
return rnd.render(renderText(Text,fg));
}
Texture Font::renderTextWrapped(const Renderer& rnd, const std::string& Text, const RGBA& fg, size_t WrapLength) const
{
return rnd.render(renderTextWrapped(Text,fg,WrapLength));
}
Texture Font::renderTextShaded(const Renderer& rnd, const std::string& Text, const RGBA& fg, const RGBA& bg) const
{
return rnd.render(renderTextShaded(Text,fg,bg));
}
Texture Font::renderTextSolid(const Renderer& rnd, const std::string& Text, const RGBA& fg) const
{
return rnd.render(renderTextSolid(Text,fg));
}
Texture Font::renderUTF8(const Renderer& rnd, const std::string& Text, const RGBA& fg) const
{
return rnd.render(renderUTF8(Text,fg));
}
Texture Font::renderUTF8Wrapped(const Renderer& rnd, const std::string& Text, const RGBA& fg, size_t WrapLength) const
{
return rnd.render(renderUTF8Wrapped(Text,fg,WrapLength));
}
Texture Font::renderUTF8Shaded(const Renderer& rnd, const std::string& Text, const RGBA& fg, const RGBA& bg) const
{
return rnd.render(renderUTF8Shaded(Text,fg,bg));
}
Texture Font::renderUTF8Solid(const Renderer& rnd, const std::string& Text, const RGBA& fg) const
{
return rnd.render(renderUTF8Solid(Text,fg));
}
Texture Font::renderUnicode(const Renderer& rnd, const uint16_t* Text, const RGBA& fg) const
{
return rnd.render(renderUnicode(Text,fg));
}
Texture Font::renderUnicodeWrapped(const Renderer& rnd, const uint16_t* Text, const RGBA& fg, size_t WrapLength) const
{
return rnd.render(renderUnicodeWrapped(Text,fg,WrapLength));
}
Texture Font::renderUnicodeShaded(const Renderer& rnd, const uint16_t* Text, const RGBA& fg, const RGBA& bg) const
{
return rnd.render(renderUnicodeShaded(Text,fg,bg));
}
Texture Font::renderUnicodeSolid(const Renderer& rnd, const uint16_t* Text, const RGBA& fg) const
{
return rnd.render(renderUnicodeSolid(Text,fg));
}
void Font::release()
{
_clear();
}
#include "end_code.h"

115
SDLWrapper/Font.h Normal file
View File

@ -0,0 +1,115 @@
#pragma once
#include "include.h"
#include "begin_code.h"
class Font
{
public:
Font() = default;
Font(std::string FontFileName, size_t size) throw(ErrorViewer);
int use(std::string FontFileName, size_t size);
bool isReady() const;
bool isNormal() const;
bool isBold() const;
bool isItalic() const;
bool isUnderLine() const;
bool isStrikeThrough() const;
void setNormal();
void setBold(bool);
void setItalic(bool);
void setUnderLine(bool);
void setStrikeThrough(bool);
template<typename... Args>
void setFontStyle(FontStyle style,Args&&... args)
{
int fontcalc=0;
_setFontStyle(fontcalc,style,args...);
}
void setFontStyle(FontStyle style)
{
int fontcalc=0;
_setFontStyle(fontcalc,style);
}
std::vector<FontStyle> getFontStyles() const;
int getFontHeight() const;
int getFontAscent() const;
int getFontDescent() const;
int getFontLineSkip() const;
bool isFontKerning() const;
void setFontKerning(bool enableKerning);
long getFontFaceNum() const;
int getFontFaceIsFixedWidth() const;
std::string getFontFaceFamilyName() const;
std::string getFontFaceStyleName() const;
FontHint getFontHint() const;
void setFontHint(FontHint hint);
Rect sizeText(const std::string& Text) const throw (ErrorViewer);
Rect sizeUTF8(const std::string& Text) const throw (ErrorViewer);
Rect sizeUnicode(const uint16_t* Text) const throw (ErrorViewer);
/// Surface Rendering Functions.
Surface renderText(const std::string& Text, const RGBA& fg) const;
Surface renderTextWrapped(const std::string& Text, const RGBA& fg, size_t WrapLength) const;
Surface renderTextShaded(const std::string& Text, const RGBA& fg, const RGBA& bg) const;
Surface renderTextSolid(const std::string& Text, const RGBA& fg) const;
Surface renderUTF8(const std::string& Text, const RGBA& fg) const;
Surface renderUTF8Wrapped(const std::string& Text, const RGBA& fg, size_t WrapLength) const;
Surface renderUTF8Shaded(const std::string& Text, const RGBA& fg, const RGBA& bg) const;
Surface renderUTF8Solid(const std::string& Text, const RGBA& fg) const;
Surface renderUnicode(const uint16_t* Text,const RGBA& fg) const;
Surface renderUnicodeWrapped(const uint16_t* Text,const RGBA& fg,size_t WrapLength) const;
Surface renderUnicodeShaded(const uint16_t* Text,const RGBA& fg,const RGBA& bg) const;
Surface renderUnicodeSolid(const uint16_t* Text,const RGBA& fg) const;
/// Texture Rendering Functions.
Texture renderText(const Renderer& rnd, const std::string& Text, const RGBA& fg) const;
Texture renderTextWrapped(const Renderer& rnd, const std::string& Text, const RGBA& fg, size_t WrapLength) const;
Texture renderTextShaded(const Renderer& rnd, const std::string& Text, const RGBA& fg, const RGBA& bg) const;
Texture renderTextSolid(const Renderer& rnd, const std::string& Text, const RGBA& fg) const;
Texture renderUTF8(const Renderer& rnd, const std::string& Text, const RGBA& fg) const;
Texture renderUTF8Wrapped(const Renderer& rnd, const std::string& Text, const RGBA& fg, size_t WrapLength) const;
Texture renderUTF8Shaded(const Renderer& rnd, const std::string& Text, const RGBA& fg, const RGBA& bg) const;
Texture renderUTF8Solid(const Renderer& rnd, const std::string& Text, const RGBA& fg) const;
Texture renderUnicode(const Renderer& rnd,const uint16_t* Text,const RGBA& fg) const;
Texture renderUnicodeWrapped(const Renderer& rnd,const uint16_t* Text,const RGBA& fg,size_t WrapLength) const;
Texture renderUnicodeShaded(const Renderer& rnd,const uint16_t* Text,const RGBA& fg,const RGBA& bg) const;
Texture renderUnicodeSolid(const Renderer& rnd,const uint16_t* Text,const RGBA& fg) const;
void release();
protected:
template<typename... Args>
void _setFontStyle(int& fontcalc,FontStyle style,Args&&... args)
{
fontcalc|=_style_caster(style);
_setFontStyle(fontcalc,args...);
}
void _setFontStyle(int& fontcalc,FontStyle style)
{
fontcalc|=_style_caster(style);
_real_setFontStyle(fontcalc);
}
private:
void _real_setFontStyle(int);
int _style_caster(FontStyle);
std::shared_ptr<TTF_Font> _font;
void _set(TTF_Font*);
void _clear();
TTF_Font* _get() const;
};
#include "end_code.h"

267
SDLWrapper/Music.cpp Normal file
View File

@ -0,0 +1,267 @@
#include "Music.h"
#include "begin_code.h"
AudioPlayer::AudioPlayer()
{
if (!_sysAudioCounter)
{
_sysAudio = new _Audio;
}
++_sysAudioCounter;
}
AudioPlayer::~AudioPlayer()
{
--_sysAudioCounter;
if (!_sysAudioCounter)
{
delete _sysAudio;
}
}
AudioPlayer::_Audio::_Audio()
{
Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024);
}
AudioPlayer::_Audio::~_Audio()
{
Mix_CloseAudio();
}
void Music::_set(Mix_Music* p)//private
{
_music.reset(p,Mix_FreeMusic);
}
void Music::_clear()//private
{
_music.reset();
}
Mix_Music* Music::_get()//private
{
return _music.get();
}
//static
int MusicPlayer::GetDecoderNum()
{
return Mix_GetNumMusicDecoders();
}
//static
std::string MusicPlayer::GetDecoderName(int index)
{
return std::string(Mix_GetMusicDecoder(index));
}
Music MusicPlayer::loadMusic(std::string Filename) throw(ErrorViewer)
{
Mix_Music* temp = Mix_LoadMUS(Filename.c_str());
if (temp == nullptr)
{
ErrorViewer e;
e.fetch();
throw e;
}
Music m;
m._set(temp);
return m;
}
int MusicPlayer::play(Music music, int loops)
{
m = music;
return Mix_PlayMusic(m._get(), loops);
}
void MusicPlayer::pause()
{
Mix_PauseMusic();
}
void MusicPlayer::resume()
{
Mix_ResumeMusic();
}
void MusicPlayer::rewind()
{
Mix_RewindMusic();
}
int MusicPlayer::stop()
{
return Mix_HaltMusic();
}
int MusicPlayer::fadeIn(int loops, int ms)
{
return Mix_FadeInMusic(m._get(), loops, ms);
}
int MusicPlayer::fadeOut(int ms)
{
return Mix_FadeOutMusic(ms);
}
bool MusicPlayer::isPlaying()
{
return (Mix_PlayingMusic() == 1);
}
bool MusicPlayer::isPaused()
{
return (Mix_PausedMusic() == 1);
}
int MusicPlayer::isFading()
{
switch (Mix_FadingMusic())
{
case MIX_NO_FADING:
return 0;
case MIX_FADING_IN:
return 1;
case MIX_FADING_OUT:
return 2;
default:
return -1;
}
}
//static
int MusicPlayer::SetMusicPosition(double position)
{
return Mix_SetMusicPosition(position);
}
void Sound::_set(Mix_Chunk* p)
{
_sound.reset(p,Mix_FreeChunk);
}
void Sound::_clear()//private
{
_sound.reset();
}
Mix_Chunk* Sound::_get()
{
return _sound.get();
}
//static
int SoundPlayer::GetDecoderNum()
{
return Mix_GetNumChunkDecoders();
}
//static
std::string SoundPlayer::GetDecoderName(int index)
{
return std::string(Mix_GetChunkDecoder(index));
}
SoundPlayer::SoundPlayer(int Channels)
{
Mix_AllocateChannels(Channels);
}
Sound SoundPlayer::loadSound(std::string Filename) throw(ErrorViewer)
{
Mix_Chunk* temp = Mix_LoadWAV(Filename.c_str());
if (temp == NULL)
{
ErrorViewer e;
e.fetch();
throw e;
}
Sound s;
s._set(temp);
return s;
}
ChannelID SoundPlayer::playSound(Sound sound, int loops) throw(ErrorViewer)
{
ChannelID id;
if (-1 == (id = Mix_PlayChannel(-1, sound._get(), loops)))
{
ErrorViewer e;
e.fetch();
throw e;
}
return id;
}
ChannelID SoundPlayer::fadein(Sound sound, int loops, int ms) throw(ErrorViewer)
{
ChannelID id;
if (-1 == (id = Mix_FadeInChannel(-1, sound._get(), loops, ms)))
{
ErrorViewer e;
e.fetch();
throw e;
}
return id;
}
int SoundPlayer::fadeout(ChannelID id, int ms)
{
return Mix_FadeOutChannel(id, ms);
}
void SoundPlayer::pause(ChannelID id)
{
Mix_Pause(id);
}
void SoundPlayer::resume(ChannelID id)
{
Mix_Resume(id);
}
int SoundPlayer::stop(ChannelID id)
{
return Mix_HaltChannel(id);
}
int SoundPlayer::setPanning(ChannelID id, uint8_t left, uint8_t right)
{
return Mix_SetPanning(id,left,right);
}
int SoundPlayer::setPosition(ChannelID id, int16_t angle, uint8_t distance)
{
return Mix_SetPosition(id,angle,distance);
}
int SoundPlayer::setDistance(ChannelID id, uint8_t distance)
{
return Mix_SetDistance(id,distance);
}
int SoundPlayer::setReverseStereo(ChannelID id, int flip)
{
return Mix_SetReverseStereo(id,flip);
}
int SoundPlayer::addEffect(ChannelID id,Mix_EffectFunc_t f, Mix_EffectDone_t d, void* arg)
{
return Mix_RegisterEffect(id,f,d,arg);
}
int SoundPlayer::removeEffect(ChannelID id,Mix_EffectFunc_t f)
{
return Mix_UnregisterEffect(id,f);
}
int SoundPlayer::removeAllEffect(ChannelID id)
{
return Mix_UnregisterAllEffects(id);
}
AudioPlayer::_Audio* AudioPlayer::_sysAudio = nullptr;
int AudioPlayer::_sysAudioCounter = 0;
#include "end_code.h"

104
SDLWrapper/Music.h Normal file
View File

@ -0,0 +1,104 @@
#pragma once
#include "include.h"
#include "begin_code.h"
class AudioPlayer
{
public:
AudioPlayer();
~AudioPlayer();
private:
class _Audio
{
public:
_Audio();
~_Audio();
};
static _Audio* _sysAudio;
static int _sysAudioCounter;
};
/// Forward Declaration
class Music
{
public:
protected:
Music() = default;
private:
std::shared_ptr<Mix_Music> _music;
void _set(Mix_Music*);
void _clear();
Mix_Music* _get();
friend class MusicPlayer;
};
class MusicPlayer : public AudioPlayer
{
public:
static int GetDecoderNum();
static std::string GetDecoderName(int index);
Music loadMusic(std::string Filename) throw (ErrorViewer);
int play(Music music, int loops);
void pause();
void resume();
void rewind();
int stop();
int fadeIn(int loops, int ms);
int fadeOut(int ms);
bool isPlaying();
bool isPaused();
int isFading();
/// Experimental
static int SetMusicPosition(double position);
private:
Music m;
};
class Sound
{
public:
protected:
Sound() = default;
private:
std::shared_ptr<Mix_Chunk> _sound;
void _set(Mix_Chunk*);
void _clear();
Mix_Chunk* _get();
friend class SoundPlayer;
};
typedef int ChannelID;
class SoundPlayer : public AudioPlayer
{
public:
static int GetDecoderNum();
static std::string GetDecoderName(int index);
SoundPlayer(int Channels = 16);
Sound loadSound(std::string Filename) throw (ErrorViewer);
ChannelID playSound(Sound sound, int loops) throw (ErrorViewer);
ChannelID fadein(Sound sound, int loops, int ms) throw (ErrorViewer);
int fadeout(ChannelID id, int ms);
void pause(ChannelID id);
void resume(ChannelID id);
int stop(ChannelID id);
/// Experimental
int setPanning(ChannelID id,uint8_t left,uint8_t right);
int setPosition(ChannelID id,int16_t angle,uint8_t distance);
int setDistance(ChannelID id,uint8_t distance);
int setReverseStereo(ChannelID id,int flip);
/// Experimental: Direct Add/Remove Effect
int addEffect(ChannelID id,Mix_EffectFunc_t f, Mix_EffectDone_t d, void *arg);
int removeEffect(ChannelID id,Mix_EffectFunc_t f);
int removeAllEffect(ChannelID id);
};
#include "end_code.h"

View File

@ -0,0 +1,30 @@
#include "Point.h"
#include "begin_code.h"
Point::Point(int X, int Y)
{
x = X;
y = Y;
}
Point::Point()
{
x = y = 0;
}
SDL_Point Point::toSDLPoint() const
{
SDL_Point p;
p.x = x;
p.y = y;
return p;
}
bool Point::inRect(const Rect& rect) const
{
auto p = toSDLPoint();
auto r = rect.toSDLRect();
return ( SDL_PointInRect(&p, &r) == SDL_TRUE );
}
#include "end_code.h"

View File

@ -0,0 +1,13 @@
#pragma once
#include "include.h"
#include "begin_code.h"
class Point
{
public:
int x, y;
Point(int X, int Y);
Point();
SDL_Point toSDLPoint() const;
bool inRect(const Rect& rect) const;
};
#include "end_code.h"

399
SDLWrapper/Renderer.cpp Normal file
View File

@ -0,0 +1,399 @@
#include "Renderer.h"
#include "begin_code.h"
//private
void Renderer::_set(SDL_Renderer* p)
{
_rnd.reset(p,SDL_DestroyRenderer);
}
//private
void Renderer::_clear()
{
_rnd.reset();
}
//private
SDL_Renderer* Renderer::_get() const
{
return _rnd.get();
}
int Renderer::setColor(const RGBA& pack)
{
return SDL_SetRenderDrawColor(_get(), pack.r, pack.g, pack.b, pack.a);
}
RGBA Renderer::getColor() const
{
Uint8 r, g, b, a;
SDL_GetRenderDrawColor(_get(), &r, &g, &b, &a);
return RGBA(r, g, b, a);
}
int Renderer::setBlendMode(BlendMode mode)
{
return SDL_SetRenderDrawBlendMode(_get(), _internal::getSDLBlendModeFromBlendMode(mode));
}
BlendMode Renderer::getBlendMode() const
{
SDL_BlendMode temp;
SDL_GetRenderDrawBlendMode(_get(), &temp);
return _internal::getBlendModeFromSDLBlendMode(temp);
}
int Renderer::setTarget(Texture & t)
{
return SDL_SetRenderTarget(_get(), t._get());
}
int Renderer::setTarget()
{
return SDL_SetRenderTarget(_get(), nullptr);
}
Texture Renderer::getTarget()
{
Texture t;
t._set_no_delete(SDL_GetRenderTarget(_get()));
return t;
}
int Renderer::fillRect(const Rect& rect)
{
auto inr = rect.toSDLRect();
return SDL_RenderFillRect(_get(), &inr);
}
int Renderer::drawRect(const Rect& rect)
{
auto inr = rect.toSDLRect();
return SDL_RenderDrawRect(_get(), &inr);
}
int Renderer::drawPoint(const Point& p)
{
return SDL_RenderDrawPoint(_get(),p.x,p.y);
}
int Renderer::drawLine(const Point& A,const Point& B)
{
return SDL_RenderDrawLine(_get(),A.x,A.y,B.x,B.y);
}
int Renderer::fillRects(const SDL_Rect* pRectArray, int n)
{
return SDL_RenderFillRects(_get(),pRectArray,n);
}
int Renderer::drawRects(const SDL_Rect* pRectArray, int n)
{
return SDL_RenderDrawRects(_get(),pRectArray,n);
}
int Renderer::drawPoints(const SDL_Point* pPointArray, int n)
{
return SDL_RenderDrawPoints(_get(),pPointArray,n);
}
int Renderer::drawLines(const SDL_Point* pPointArray, int n)
{
return SDL_RenderDrawLines(_get(),pPointArray,n);
}
int Renderer::fillRects(const std::vector<SDL_Rect>& rectvec)
{
return fillRects(rectvec.data(),rectvec.size());
}
int Renderer::drawRects(const std::vector<SDL_Rect>& rectvec)
{
return drawRects(rectvec.data(),rectvec.size());
}
int Renderer::drawPoints(const std::vector<SDL_Point>& pointvec)
{
return drawPoints(pointvec.data(),pointvec.size());
}
int Renderer::drawLines(const std::vector<SDL_Point>& pointvec)
{
return drawLines(pointvec.data(),pointvec.size());
}
int Renderer::setScale(float scaleX, float scaleY)
{
return SDL_RenderSetScale(_get(),scaleX,scaleY);
}
std::tuple<float,float> Renderer::getScale() const
{
float sx,sy;
SDL_RenderGetScale(_get(),&sx,&sy);
return std::make_tuple(sx,sy);
}
int Renderer::setViewport(const Rect& viewport)
{
auto rect=viewport.toSDLRect();
return SDL_RenderSetViewport(_get(),&rect);
}
Rect Renderer::getViewport() const
{
SDL_Rect rect;
SDL_RenderGetViewport(_get(),&rect);
return Rect(rect);
}
int Renderer::setLogicalSize(int w, int h)
{
return SDL_RenderSetLogicalSize(_get(),w,h);
}
Rect Renderer::getLogicalSize() const
{
int w,h;
SDL_RenderGetLogicalSize(_get(),&w,&h);
return Rect(0,0,w,h);
}
int Renderer::setClipRect(const Rect& cliprect)
{
auto r=cliprect.toSDLRect();
return SDL_RenderSetClipRect(_get(),&r);
}
Rect Renderer::getClipRect() const
{
SDL_Rect r;
SDL_RenderGetClipRect(_get(),&r);
return Rect(r);
}
bool Renderer::isClipEnabled() const
{
return (SDL_RenderIsClipEnabled(_get())==SDL_TRUE);
}
Rect Renderer::getOutputSize() const
{
int w,h;
SDL_GetRendererOutputSize(_get(),&w,&h);
return Rect(0,0,w,h);
}
int Renderer::clear()
{
return SDL_RenderClear(_get());
}
void Renderer::update()
{
SDL_RenderPresent(_get());
}
int Renderer::copy(const Texture& t, const Rect& src, const Rect& dst)
{
SDL_Rect s = src.toSDLRect();
SDL_Rect d = dst.toSDLRect();
return SDL_RenderCopy(_get(), t._get(), &s, &d);
}
int Renderer::copyTo(const Texture& t, const Rect& dst)
{
SDL_Rect d = dst.toSDLRect();
return SDL_RenderCopy(_get(), t._get(), NULL, &d);
}
int Renderer::copyTo(const Texture& t, const Point& lupoint)
{
return copyTo(t, Rect(lupoint.x, lupoint.y, t.getw(), t.geth()));
}
int Renderer::copyFill(const Texture& t, const Rect& src)
{
SDL_Rect s = src.toSDLRect();
return SDL_RenderCopy(_get(), t._get(), &s, NULL);
}
int Renderer::copyFullFill(const Texture& t)
{
return SDL_RenderCopy(_get(), t._get(), NULL, NULL);
}
/// ----- Super Copy Extend ----- (Begin)
int Renderer::copy(const Texture& t, const Rect& src, const Rect& dst, double angle, FlipMode mode)
{
auto s=src.toSDLRect();
auto d=src.toSDLRect();
return SDL_RenderCopyEx(_get(),t._get(),&s,&d,angle,NULL,_internal::getSDLRendererFlipFromFlipMode(mode));
}
int Renderer::copyTo(const Texture& t, const Rect& dst, double angle, FlipMode mode)
{
auto d=dst.toSDLRect();
return SDL_RenderCopyEx(_get(),t._get(),NULL,&d,angle,NULL,_internal::getSDLRendererFlipFromFlipMode(mode));
}
int Renderer::copyTo(const Texture& t, const Point& lupoint, double angle, FlipMode mode)
{
return copyTo(t,Rect(lupoint.x,lupoint.y,t.getw(),t.geth()),angle,mode);
}
int Renderer::copyFill(const Texture& t, const Rect& src, double angle, FlipMode mode)
{
auto s=src.toSDLRect();
return SDL_RenderCopyEx(_get(),t._get(),&s,NULL,angle,NULL,_internal::getSDLRendererFlipFromFlipMode(mode));
}
int Renderer::copyFullFill(const Texture& t, double angle, FlipMode mode)
{
return SDL_RenderCopyEx(_get(),t._get(),NULL,NULL,angle,NULL,_internal::getSDLRendererFlipFromFlipMode(mode));
}
int Renderer::copy(const Texture& t, const Rect& src, const Rect& dst, const Point& centerPoint, double angle, FlipMode mode)
{
auto s=src.toSDLRect();
auto d=src.toSDLRect();
auto c=centerPoint.toSDLPoint();
return SDL_RenderCopyEx(_get(),t._get(),&s,&d,angle,&c,_internal::getSDLRendererFlipFromFlipMode(mode));
}
int Renderer::copyTo(const Texture& t, const Rect& dst, const Point& centerPoint, double angle, FlipMode mode)
{
auto d=dst.toSDLRect();
auto c=centerPoint.toSDLPoint();
return SDL_RenderCopyEx(_get(),t._get(),NULL,&d,angle,&c,_internal::getSDLRendererFlipFromFlipMode(mode));
}
int Renderer::copyTo(const Texture& t, const Point& lupoint, const Point& centerPoint, double angle, FlipMode mode)
{
return copyTo(t,lupoint,centerPoint,angle,mode);
}
int Renderer::copyFill(const Texture& t, const Rect& src, const Point& centerPoint, double angle, FlipMode mode)
{
auto s=src.toSDLRect();
auto c=centerPoint.toSDLPoint();
return SDL_RenderCopyEx(_get(),t._get(),&s,NULL,angle,&c,_internal::getSDLRendererFlipFromFlipMode(mode));
}
int Renderer::copyFullFill(const Texture& t, const Point& centerPoint, double angle, FlipMode mode)
{
auto c=centerPoint.toSDLPoint();
return SDL_RenderCopyEx(_get(),t._get(),NULL,NULL,angle,&c,_internal::getSDLRendererFlipFromFlipMode(mode));
}
/// ----- Super Copy Extend ----- (End)
int Renderer::supercopy(const Texture& t,
bool srcfull,const Rect& src,bool dstfull,const Rect& dst,
double angle,
bool haspoint,const Point& center,FlipMode mode)
{
SDL_Rect R1,R2;
SDL_Point P;
SDL_Rect* pR1=nullptr;
SDL_Rect* pR2=nullptr;
SDL_Point* pPoint=nullptr;
SDL_RendererFlip flip;
if(srcfull)
{
R1=src.toSDLRect();
pR1=&R1;
}
if(dstfull)
{
R2=dst.toSDLRect();
pR2=&R2;
}
if(haspoint)
{
P=center.toSDLPoint();
pPoint=&P;
}
flip=_internal::getSDLRendererFlipFromFlipMode(mode);
return SDL_RenderCopyEx(_get(),t._get(),pR1,pR2,angle,pPoint,flip);
}
Texture Renderer::render(const Surface& surf) const throw(ErrorViewer)
{
Texture t;
SDL_Texture* temp = SDL_CreateTextureFromSurface(_get(), surf._get());
if (temp == nullptr)
{
ErrorViewer e;
e.fetch();
throw e;
}
t._set(temp);
return t;
}
Texture Renderer::loadTexture(const std::string& FileName) const throw(ErrorViewer)
{
Texture t;
SDL_Texture* temp = IMG_LoadTexture(_get(), FileName.c_str());
if (temp == nullptr)
{
ErrorViewer e;
e.fetch();
throw e;
}
t._set(temp);
return t;
}
Texture Renderer::loadTextureRW(const RWOP& rwop) const throw (ErrorViewer)
{
Texture t;
SDL_Texture* temp=IMG_LoadTexture_RW(_get(),rwop._get(),0);
if (temp == nullptr)
{
ErrorViewer e;
e.fetch();
throw e;
}
t._set(temp);
return t;
}
Texture Renderer::createTexture(int Width, int Height) const throw(ErrorViewer)
{
SDL_Texture* temp = SDL_CreateTexture(_get(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, Width, Height);
if (temp == NULL)
{
ErrorViewer e;
e.fetch();
throw e;
}
Texture t;
t._set(temp);
return t;
}
bool Renderer::isRenderTargetSupported() const
{
return (SDL_RenderTargetSupported(_get())==SDL_TRUE);
}
bool Renderer::isReady() const
{
return (_get() != nullptr);
}
void Renderer::release()
{
_clear();
}
//static
int Renderer::GetDriversNum()
{
return SDL_GetNumRenderDrivers();
}
#include "end_code.h"

132
SDLWrapper/Renderer.h Normal file
View File

@ -0,0 +1,132 @@
#pragma once
#include "include.h"
#include "begin_code.h"
class Renderer
{
public:
Renderer() = default;
Renderer(Window& wnd,std::initializer_list<RendererType> RendererFlags = { RendererType::Accelerated,RendererType::TargetTexture }) throw (ErrorViewer);
~Renderer() = default;
/// If Renderer is current not ready, setRenderer will create Renderer.
/// Otherwise, setRenderer will fail.
int createRenderer(Window& wnd,RendererType Type)
{
int flagcalc=0;
return _createRenderer(wnd,flagcalc,Type);
}
template<typename... Args>
int createRenderer(Window& wnd,RendererType Type,Args&&... args)
{
int flagcalc=0;
return _createRenderer(wnd,flagcalc,Type,std::forward<RendererType>(args...));
}
int createRenderer(Window& wnd,std::initializer_list<RendererType>);
int setColor(const RGBA& pack);
RGBA getColor() const;
int setBlendMode(BlendMode mode);
BlendMode getBlendMode() const;
int setTarget(Texture& t);
int setTarget();
Texture getTarget();
int fillRect(const Rect& rect);
int drawRect(const Rect& rect);
int drawPoint(const Point& p);
int drawLine(const Point& A,const Point& B);
/// Experimental
int fillRects(const SDL_Rect* pRectArray,int n);
int drawRects(const SDL_Rect* pRectArray,int n);
int drawPoints(const SDL_Point* pPointArray,int n);
int drawLines(const SDL_Point* pPointArray,int n);
/// Experimental
int fillRects(const std::vector<SDL_Rect>& rectvec);
int drawRects(const std::vector<SDL_Rect>& rectvec);
int drawPoints(const std::vector<SDL_Point>& pointvec);
int drawLines(const std::vector<SDL_Point>& pointvec);
int setScale(float scaleX,float scaleY);
std::tuple<float,float> getScale() const;
int setViewport(const Rect& viewport);
Rect getViewport() const;
int setLogicalSize(int w,int h);
Rect getLogicalSize() const;
int setClipRect(const Rect& cliprect);
Rect getClipRect() const;
bool isClipEnabled() const;
Rect getOutputSize() const;
int clear();
void update();
int copy(const Texture& t, const Rect& src, const Rect& dst);
int copyTo(const Texture& t, const Rect& dst);
int copyTo(const Texture& t, const Point& lupoint);
int copyFill(const Texture& t, const Rect& src);
int copyFullFill(const Texture& t);
/// Super copy without center point.
int copy(const Texture& t, const Rect& src, const Rect& dst,double angle,FlipMode mode);
int copyTo(const Texture& t, const Rect& dst,double angle,FlipMode mode);
int copyTo(const Texture& t, const Point& lupoint,double angle,FlipMode mode);
int copyFill(const Texture& t, const Rect& src,double angle,FlipMode mode);
int copyFullFill(const Texture& t,double angle,FlipMode mode);
/// Super copy with center point
int copy(const Texture& t, const Rect& src, const Rect& dst,const Point& centerPoint,double angle,FlipMode mode);
int copyTo(const Texture& t, const Rect& dst,const Point& centerPoint,double angle,FlipMode mode);
int copyTo(const Texture& t, const Point& lupoint,const Point& centerPoint,double angle,FlipMode mode);
int copyFill(const Texture& t, const Rect& src,const Point& centerPoint,double angle,FlipMode mode);
int copyFullFill(const Texture& t,const Point& centerPoint,double angle,FlipMode mode);
/// Reserved for compatibility
int supercopy(const Texture& t,
bool srcfull,const Rect& src,bool dstfull,const Rect& dst,
double angle,
bool haspoint,const Point& center,FlipMode mode);
Texture render(const Surface& surf) const throw (ErrorViewer);
Texture loadTexture(const std::string& FileName) const throw(ErrorViewer);
Texture loadTextureRW(const RWOP& rwop) const throw(ErrorViewer);
Texture createTexture(int Width, int Height) const throw(ErrorViewer);
bool isRenderTargetSupported() const;
bool isReady() const;
void release();
/// Experimental
static int GetDriversNum();
protected:
template<typename... Args>
int _createRenderer(Window& wnd,int& refcalc,RendererType Type,Args&&... args)
{
refcalc|=_rendertype_caster(Type);
return _createRenderer(wnd,refcalc,args...);
}
// template<>
int _createRenderer(Window& wnd,int& refcalc,RendererType Type)
{
refcalc|=_rendertype_caster(Type);
return _createRenderer_Real(wnd,refcalc);
}
private:
std::shared_ptr<SDL_Renderer> _rnd;
int _createRenderer_Real(Window& wnd,Uint32 flags);
Uint32 _rendertype_caster(RendererType);
void _set(SDL_Renderer*);
void _clear();
SDL_Renderer* _get() const;
};
#include "end_code.h"

125
SDLWrapper/Texture.cpp Normal file
View File

@ -0,0 +1,125 @@
#include "Texture.h"
#include "begin_code.h"
//private
void Texture::_set(SDL_Texture* p)
{
_text.reset(p,SDL_DestroyTexture);
updateInfo();
}
//private
void Texture::_set_no_delete(SDL_Texture* p)
{
_text.reset(p,[](SDL_Texture*) {});
updateInfo();
}
//private
void Texture::_clear()
{
_text.reset();
updateInfo();
}
//private
SDL_Texture* Texture::_get() const
{
return _text.get();
}
Texture::Texture()
{
updateInfo();
}
Rect Texture::getSize()
{
return rect;
}
int Texture::getw() const
{
return rect.w;
}
int Texture::geth() const
{
return rect.h;
}
bool Texture::isReady() const
{
return (_get() != nullptr);
}
int Texture::setBlendMode(BlendMode mode)
{
return SDL_SetTextureBlendMode(_get(), _internal::getSDLBlendModeFromBlendMode(mode));
}
BlendMode Texture::getBlendMode() const
{
SDL_BlendMode temp;
SDL_GetTextureBlendMode(_get(), &temp);
return _internal::getBlendModeFromSDLBlendMode(temp);
}
/// Alpha: 0: Transparent 255: opaque
int Texture::setAlphaMode(int alpha)
{
Uint8 temp = std::max(std::min(alpha, 255), 0);
return SDL_SetTextureAlphaMod(_get(), temp);
}
int Texture::getAlphaMode() const
{
Uint8 temp;
SDL_GetTextureAlphaMod(_get(), &temp);
return temp;
}
ColorMode Texture::getColorMode() const
{
ColorMode pack;
Uint8 r, g, b;
SDL_GetTextureColorMod(_get(), &r, &g, &b);
pack.r = r;
pack.g = g;
pack.b = b;
return pack;
}
int Texture::setColorMode(ColorMode mode)
{
return SDL_SetTextureColorMod(_get(), mode.r, mode.g, mode.b);
}
RGBA Texture::getRGBA() const
{
return RGBA(getColorMode(), getAlphaMode());
}
void Texture::setRGBA(const RGBA& pack)
{
setColorMode(pack.toColorMode());
setAlphaMode(pack.a);
}
/// updateInfo() must be called after Texture is changed.
//protected
void Texture::updateInfo()
{
if(_get()==nullptr)
{
rect.x=rect.y=rect.w=rect.h=0;
}
SDL_QueryTexture(_get(), NULL, NULL, &rect.w, &rect.h);
rect.x = rect.y = 0;
}
void Texture::release()
{
_clear();
}
#include "end_code.h"

40
SDLWrapper/Texture.h Normal file
View File

@ -0,0 +1,40 @@
#pragma once
#include "include.h"
#include "begin_code.h"
class Texture
{
public:
Texture();
~Texture() = default;
Rect getSize();
int getw() const;
int geth() const;
bool isReady() const;
int setBlendMode(BlendMode mode);
BlendMode getBlendMode() const;
/// Alpha: 0: Transparent 255: opaque
int setAlphaMode(int alpha);
int getAlphaMode() const;
ColorMode getColorMode() const;
int setColorMode(ColorMode mode);
RGBA getRGBA() const;
void setRGBA(const RGBA& pack);
void release();
protected:
/// updateInfo() must be called after Texture is changed.
void updateInfo();
private:
std::shared_ptr<SDL_Texture> _text;
void _set(SDL_Texture*);
/// Just used for "SDL_GetRenderTarget"
void _set_no_delete(SDL_Texture*);
void _clear();
SDL_Texture* _get() const;
Rect rect;
friend class Renderer;
};
#include "end_code.h"

87
SDLWrapper/Timer.cpp Normal file
View File

@ -0,0 +1,87 @@
#include "Timer.h"
#include "begin_code.h"
/// Global Executor For class Timer
Uint32 _global_timer_executor(Uint32 interval,void* param)
{
auto p=reinterpret_cast<std::function<Uint32(Uint32 interval)>*>(param);
return (*p)(interval);
}
Timer::Timer()
{
_enabled=false;
_detached=false;
_delete_on_disable=false;
id=-1;
}
Timer::Timer(SDL_TimerCallback callback,Uint32 interval,void* param) : Timer()
{
_real_timer_call(callback,interval,param);
}
void Timer::_real_timer_call(SDL_TimerCallback callback,Uint32 interval,void* param)
{
_callback=callback;
_interval=interval;
_param=param;
}
int Timer::enable()
{
if(_enabled)
{
return -1;
}
else
{
id=SDL_AddTimer(_interval,_callback,_param);
if(id<0) return -2;
_enabled=true;
return 0;
}
}
int Timer::disable()
{
if(_enabled)
{
SDL_RemoveTimer(id);
_enabled=false;
id=-1;
_callback=nullptr;
if(_delete_on_disable)
{
_delete_delegator(reinterpret_cast<std::function<Uint32(Uint32 interval)>*>(_param));
_delete_on_disable=false;
}
_param=nullptr;
return 0;
}
else
{
return -1;
}
}
void Timer::detach()
{
_detached=true;
}
Timer::~Timer()
{
if(!_detached)
{
disable();
}
}
//static
void Timer::_delete_delegator(std::function<Uint32(Uint32)>* param)
{
delete param;
}
#include "end_code.h"

52
SDLWrapper/Timer.h Normal file
View File

@ -0,0 +1,52 @@
#pragma once
#include "includes.h"
#include "begin_code.h"
Uint32 _global_timer_executor(Uint32 interval,void* param);
class Timer
{
public:
Timer();
/// void func(Uint32,...)
template<typename VoidCallable,typename... Args>
Timer(Uint32 interval,VoidCallable&& vcallable,Args&&... args) : Timer()
{
auto realCall=[&,vcallable](Uint32 ims)->Uint32{vcallable(ims,args...); return ims;};
auto pfunc=new std::function<Uint32(Uint32 interval)>(realCall);
_real_timer_call(_global_timer_executor,interval,pfunc);
}
/// Uint32 func(Uint32,...)
template<typename Callable,typename... Args>
Timer(Callable&& callable,Uint32 interval,Args&&... args) : Timer()
{
auto realCall=[&,callable](Uint32 ims)->Uint32{return callable(ims,args...);};
auto pfunc=new std::function<Uint32(Uint32 interval)>(realCall);
_real_timer_call(_global_timer_executor,interval,pfunc);
}
/// Restore For Capability
Timer(SDL_TimerCallback callback,Uint32 interval,void* param);
int enable();
int disable();
bool isenable() const;
void detach();
~Timer();
static void _delete_delegator(std::function<Uint32(Uint32)>* Delegator);
private:
void _real_timer_call(SDL_TimerCallback callback,Uint32 interval,void* param);
SDL_TimerCallback _callback;
Uint32 _interval;
void* _param;
SDL_TimerID id;
bool _enabled;
bool _detached;
/// Reserved Variable For Template variable Parameter
bool _delete_on_disable;
};
#include "end_code.h"