Merge pull request #10 from Kiritow/pre-merge

Update MiniEngine
This commit is contained in:
Kirigaya Kazuto 2017-06-23 22:56:01 +08:00 committed by GitHub
commit e87f513857
90 changed files with 17052 additions and 4504 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,739 +1,12 @@
#pragma once
#include "MiniEngine_Config.h"
#include <string>
#include <memory>
#include <functional>
#include <vector>
#include "SDLWrapper/IncludeAll.h"
#define _MINIENGINE_SDL_VERSION_ATLEAST(X,Y,Z) SDL_VERSION_ATLEAST(X,Y,Z)
namespace MiniEngine
{
class Rect
{
public:
int x, y, w, h;
Rect(int X, int Y, int W, int H);
Rect(const SDL_Rect&);
Rect();
SDL_Rect toSDLRect() const;
bool isEmpty();
bool operator == (const Rect&) const;
bool hasIntersection(const Rect&) const;
Rect getIntersection(const Rect&) const;
Rect getUnion(const Rect&) const;
};
class Point
{
public:
int x, y;
Point(int X, int Y);
Point();
SDL_Point toSDLPoint() const;
bool inRect(const Rect& rect) const;
};
class ColorMode
{
public:
int r, g, b;
ColorMode(int R, int G, int B);
ColorMode();
};
class RGBA
{
public:
int r, g, b, a;
RGBA(int R, int G, int B, int A);
RGBA(ColorMode mode, int A);
RGBA();
SDL_Color toSDLColor() const;
ColorMode toColorMode() const;
};
class NonCopyable
{
protected:
NonCopyable() = default;
~NonCopyable() = default;
NonCopyable(const NonCopyable&) = delete;
NonCopyable& operator = (const NonCopyable&) = delete;
};
class ErrorViewer : public std::exception
{
public:
void fetch();
std::string getError() const;
const char* what() const throw() override;
private:
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;
void release();
private:
std::shared_ptr<SDL_RWops> _op;
SDL_RWops* _get() const;
void _clear();
void _set(SDL_RWops*);
friend class Surface;
friend class Renderer;
};
enum class BlendMode { None,Blend,Add,Mod };
class Surface
{
public:
Surface()=default;
Surface(int width,int height,int depth,int Rmask,int Gmask,int Bmask,int Amask) throw(ErrorViewer);
Surface(int width,int height,int depth,RGBA colorPack) throw(ErrorViewer);
Surface(int width,int height,int depth,Uint32 surfaceFormat) throw(ErrorViewer);
Surface(const std::string& filename) throw(ErrorViewer);
Surface(const RWOP& rwop) throw(ErrorViewer);
~Surface() = default;
/// static functions
static Surface load(const std::string& filename);
static Surface load(const RWOP& rwop);
static Surface create(int width,int height,int depth,int Rmask,int Gmask,int Bmask,int Amask);
static Surface create(int width,int height,int depth,Uint32 surfaceFormat);
/// xxxAs will clear the current surface if loaded or created successfully.
int loadAs(const std::string& filename);
int loadAs(const RWOP& rwop);
int createAs(int width,int height,int depth,int Rmask,int Gmask,int Bmask,int Amask);
int createAs(int width,int height,int depth,Uint32 surfaceFormat);
int savePNG(const std::string& filename) const;
int getw() const;
int geth() const;
BlendMode getBlendMode() const;
int setBlendMode(BlendMode mode);
/// Rendering functions. Copy an external surface to this surface. So it has no constant attribute.
int blit(const Surface& s,const Rect& src,const Rect& dst);
int blitTo(const Surface& t, const Rect& dst);
int blitTo(const Surface& t, const Point& lupoint);
int blitFill(const Surface& t, const Rect& src);
int blitFullFill(const Surface& t);
int blitScaled(const Surface& s,const Rect& src,const Rect& dst);
int blitScaledTo(const Surface& t, const Rect& dst);
int blitScaledTo(const Surface& t, const Point& lupoint);
int blitScaledFill(const Surface& t, const Rect& src);
int blitScaledFullFill(const Surface& t);
int setClipRect(const Rect& clipRect);
Rect getClipRect() const;
void disableClipping();
int setAlphaMode(int alpha);
int getAlphaMode() const;
ColorMode getColorMode() const;
int setColorMode(ColorMode mode);
RGBA getRGBA() const;
void setRGBA(const RGBA& pack);
bool mustlock() const;
int lock();
void unlock();
bool isReady() const;
void release();
/// Experimental : Get SDL_Surface Pointer and then do anything you want!
/// In case you can do anything with this pointer, this function should not has constant attribute.
SDL_Surface* getRawPointer();
private:
std::shared_ptr<SDL_Surface> _surf;
void _set(SDL_Surface*);
void _set_no_delete(SDL_Surface*);
void _clear();
SDL_Surface* _get() const;
friend class Window;
friend class Renderer;
friend class Font;
friend class Cursor;
};
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*);
void _clear();
SDL_Texture* _get() const;
Rect rect;
friend class Renderer;
};
enum class RendererType { Software, Accelerated, PresentSync, TargetTexture };
enum class FlipMode { None, Horizontal, Vertical };
class Renderer
{
public:
Renderer() = default;
int setColor(const RGBA& pack);
RGBA getColor() const;
int setBlendMode(BlendMode mode);
BlendMode getBlendMode() const;
int setTarget(Texture& t);
int setTarget();
int fillRect(const Rect& rect);
int drawRect(const Rect& rect);
int drawPoint(const Point& p);
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);
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 isReady() const;
void release();
private:
std::shared_ptr<SDL_Renderer> _rnd;
void _set(SDL_Renderer*);
void _clear();
SDL_Renderer* _get() const;
friend class Window;
};
enum class SystemCursorType
{
Arrow, Ibeam, CrossHair,
Wait, WaitArrow,
SizeNWSE, SizeNESW, SizeWE, SizeNS, SizeAll,
No, Hand
};
class Cursor
{
public:
Cursor()=default;
Cursor(SystemCursorType);
Cursor(Surface surf,Point hotspot={0,0});
static Cursor GetActiveCursor();
static Cursor GetDefaultCursor();
static void setShow(bool);
static bool isShow();
void activate();
void release();
private:
std::shared_ptr<SDL_Cursor> _cur;
void _set(SDL_Cursor*);
void _set_no_delete(SDL_Cursor*);
SDL_Cursor* _get();
void _clear();
};
enum class MessageBoxType { Error, Warning, Information };
enum class WindowType
{
FullScreen, OpenGL, Shown, Hidden,
Borderless, Resizable, Minimized, Maximized,
InputGrabbed, InputFocus, MouseFocus,
FullScreenDesktop, Foreign, AllowHighDPI,
MouseCapture, AlwaysOnTop, SkipTaskBar,
Utility, ToolTip, PopUpMenu
};
class Window
{
public:
Window()=default;
Window(std::string Title, int Width, int Height,
std::initializer_list<RendererType> RendererFlags = { RendererType::Accelerated,RendererType::TargetTexture },
std::initializer_list<WindowType> WindowFlags = {WindowType::Shown} ,
int WindowPositionX=SDL_WINDOWPOS_CENTERED, int WindowPositionY=SDL_WINDOWPOS_CENTERED) throw(ErrorViewer);
Renderer getRenderer() const;
void setRenderer(RendererType Type)
{
int flagcalc=0;
_setRenderer(flagcalc,Type);
}
template<typename... Args>
void setRenderer(RendererType Type,Args&&... args)
{
int flagcalc=0;
_setRenderer(flagcalc,Type,std::forward<RendererType>(args...));
}
void setRenderer(std::initializer_list<RendererType>);
Rect getSize() const;
void setSize(const Rect& sizeRect);
void setSize(int w, int h);
Point getPosition() const;
void setPosition(int x, int y);
void setPosition(const Point& point);
void setTitle(const std::string& Title);
std::string getTitle() const;
void setGrab(bool isGrab);
bool getGrab() const;
#if _MINIENGINE_SDL_VERSION_ATLEAST(2,0,5)
/// SDL2.0.5 Required.
int setOpacity(float opacity);
float getOpacity() const;
#endif
/// FIXME: Not Implemented.
void setResizable(bool resizable);
/// Use UTF8 in Title and Message please.
int showSimpleMessageBox(MessageBoxType type,const std::string& Title,const std::string& Message) const;
void show();
void hide();
void raise();
void minimize();
void maximize();
void restore();
_DECL_DEPRECATED Surface getSurface();
bool isScreenKeyboardShown();
void release();
/// Experimental : Free current renderer.
/// This will cause all existing Renderer class throw an error on delete.
/// This function destroy current renderer (if exist) and all Renderer class will not be notified.
void resetRenderer();
protected:
template<typename... Args>
void _setRenderer(int& refcalc,RendererType Type,Args&&... args)
{
refcalc|=_render_caster(Type);
_setRenderer(refcalc,args...);
}
void _setRenderer(int& refcalc,RendererType Type)
{
refcalc|=_render_caster(Type);
_setRenderer_Real(refcalc);
}
private:
void _setRenderer_Real(Uint32 flags);
Uint32 _render_caster(RendererType);
std::shared_ptr<SDL_Window> _wnd;
void _set(SDL_Window*);
void _clear();
SDL_Window* _get() const;
Renderer _winrnd;
};
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 };
class LogSystem
{
static void v(const char* fmt,...);/// Verbose
static void d(const char* fmt,...);/// Debug
static void i(const char* fmt,...);/// Information
static void w(const char* fmt,...);/// Warning
static void e(const char* fmt,...);/// Error
static void critical(const char* fmt,...);/// Critical
};
class SharedLibrary
{
public:
SharedLibrary();
SharedLibrary(const std::string& Filename);
~SharedLibrary()=default;
int load(const std::string& Filename);
int unload();
template<typename ReturnType,typename... Arguments>
std::function<ReturnType(Arguments...)> get(const std::string& FunctionName)
{
return std::function<ReturnType(Arguments...)>(reinterpret_cast<ReturnType(*)(Arguments...)>(get(FunctionName)));
}
void* get(const std::string& FunctionName) const;
void release();
private:
void* _get() const;
void _set(void*);
void _clear();
std::shared_ptr<void> _obj;
};
class SDLSystem
{
public:
static int SDLInit();
static void SDLQuit();
static int IMGInit();
static void IMGQuit();
static int TTFInit();
static void TTFQuit();
static int MixInit();
static void MixQuit();
static void Init();
static void Quit();
static void Delay(int ms);
static PowerState GetPowerState();
static int GetPowerLifeLeft();
static int GetPowerPrecentageLeft();
static Platform GetPlatform();
static void StartTextInput();
static bool IsTextInputActive();
static void StopTextInput();
static bool HasScreenKeyboardSupport();
static std::tuple<int,int,int> getCompileVersion();
static std::tuple<int,int,int> getLinkedVersion();
class Android
{
public:
static std::string GetInternal();
static bool ExternalAvaliable();
static bool CanReadExternal();
static bool CanWriteExternal();
static std::string GetExternal();
static void* GetJNIEnv();
};
};
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);
};
class StringEngine
{
public:

View File

@ -13,7 +13,8 @@ void GetMD5Raw(unsigned char* buffer,unsigned int bufferLen,unsigned char* outbu
int GetCRC32(unsigned char* buffer,unsigned int bufferLen,uint32_t& out_CRCResult);
int CompareSurface(const Surface& surface1,const Surface& surface2,int allowableError);
/// Compare two surfaces. Currently, Surface::getRawPointer() does not has constant attribute.
int CompareSurface(Surface& surface1,Surface& surface2,int allowableError);
class UniRandom
{

View File

@ -1,32 +0,0 @@
#pragma once
#include "../config.h"
#include <functional>
//#include <SDL2/SDL_events.h>
namespace MiniEngine
{
class MouseEvent
{
private:
struct EventData
{
int x,y;
}
public:
int type;
EventData data;
};
class MouseEventListener
{
public:
MouseEventListener();
function<void(const MouseEvent&)> onButtonDown;
function<void(const MouseEvent&)> onButtonUp;
function<void(const MouseEvent&)> onMotion;
function<void(const MouseEvent&)> onWheel;
};
}/// End of namespace MiniEngine

View File

@ -1,47 +0,0 @@
#include "Widget.h"
using namespace Engine;
using namespace std;
namespace MiniEngine
{
/// Brush
#include "widget_brush.hpp"
struct SimpleProgressBar::impl
{
int percentage;
int w,h;
RGBA ucolor,lcolor;
};
SimpleProgressBar::SimpleProgressBar(int incw,int inch,RGBA upper_color,RGBA lower_color)
{
pimpl=new impl;
pimpl->w=incw;
pimpl->h=inch;
pimpl->ucolor=upper_color;
pimpl->lcolor=lower_color;
pimpl->percentage=0;
}
void SimpleProgressBar::DrawAt(Renderer& rnd,int x,int y)
{
RGBA prev=rnd.getColor();
if(pimpl->percentage)
{
rnd.setColor(pimpl->ucolor);
rnd.fillRect(Rect(x,y,pimpl->w*pimpl->percentage/100,pimpl->h));
}
if(pimpl->percentage-100)
{
rnd.setColor(pimpl->lcolor);
rnd.fillRect(Rect(x+pimpl->w*pimpl->percentage/100,y,pimpl->w*(100-pimpl->percentage)/100,pimpl->h));
}
rnd.setColor(prev);
}
void SimpleProgressBar::setPercentage(int iPercentage)
{
pimpl->percentage=min(max(0,iPercentage),100);
}
}/// End of namespace MiniEngine

View File

@ -1,89 +0,0 @@
#pragma once
#include "../config.h"
#include <string>
#define _MINI_ENGINE_IMPL_COPY_DECL(ClassName) _SDL_ENGINE_IMPL_COPY_DECL(ClassName)
#define _MINI_ENGINE_IMPL _SDL_ENGINE_IMPL
namespace MiniEngine
{
class Stage;
class Brush
{
public:
~Brush();
_MINI_ENGINE_IMPL_COPY_DECL(Brush)
void copy(Engine::Texture t,Engine::Rect src,Engine::Rect dst,bool autoZoom);
void copyTo(Engine::Texture t,Engine::Rect dst,bool autoZoom);
void copyFill(Engine::Texture t,Engine::Rect src);
void copyFullFill(Engine::Texture t);
public:
Brush(const Engine::Window& rnd,Engine::Rect DrawableArea);
private:
_MINI_ENGINE_IMPL
friend class Stage;
};
class Stage
{
public:
Stage();
~Stage();
};
class Drawable
{
public:
virtual int Draw(const Brush& brush)=0;
virtual Engine::Rect getSize()=0;
};
class BaseButton : public Drawable
{
public:
BaseButton();
BaseButton(const BaseButton&);
BaseButton& operator = (const BaseButton&);
BaseButton(BaseButton&&);
BaseButton& operator = (BaseButton&&);
virtual int Draw(const Brush& brush);
virtual Engine::Rect getSize();
virtual ~BaseButton();
private:
struct impl;
impl* pimpl;
};
class SimpleButton : public BaseButton
{
public:
/// Default Style
SimpleButton();
SimpleButton(std::string word,
Engine::RGBA color_word,
Engine::RGBA color_background,
Engine::RGBA color_highlight);
SimpleButton(const SimpleButton&);
SimpleButton& operator = (const SimpleButton&);
SimpleButton(SimpleButton&&);
SimpleButton& operator = (SimpleButton&&);
~SimpleButton();
};
class SimpleProgressBar
{
public:
SimpleProgressBar(int incw,int inch,Engine::RGBA upper_color,Engine::RGBA lower_color);
void DrawAt(Engine::Renderer& rnd,int x,int y);
void setPercentage(int iPercentage);
~SimpleProgressBar();
private:
struct impl;
impl* pimpl;
};
}/// End of namespace MiniEngine.

View File

@ -1,69 +0,0 @@
struct Brush::impl
{
Renderer rnd;
Rect area;
impl(const Renderer& incrnd) : rnd(incrnd)
{
}
};
Brush::Brush(const Window& wnd,Rect DrawableArea) /// Protected
{
pimpl=new impl(wnd.getRenderer());
pimpl->area=DrawableArea;
}
void Brush::copy(Texture t,Rect src,Rect dst,bool autoZoom)
{
dst.x+=pimpl->area.x;
dst.y+=pimpl->area.y;
if(dst.w>pimpl->area.w) dst.w=pimpl->area.w;
if(dst.h>pimpl->area.h) dst.h=pimpl->area.h;
if(autoZoom)
{
}
else
{
if(src.w>pimpl->area.w) src.w=pimpl->area.w;
if(src.h>pimpl->area.h) src.h=pimpl->area.h;
}
pimpl->rnd.copy(t,src,dst);
}
void Brush::copyTo(Texture t,Rect dst,bool autoZoom)
{
dst.x+=pimpl->area.x;
dst.y+=pimpl->area.y;
if(dst.w>pimpl->area.w) dst.w=pimpl->area.w;
if(dst.h>pimpl->area.h) dst.h=pimpl->area.h;
if(autoZoom)
{
pimpl->rnd.copyTo(t,dst);
}
else
{
int w=t.getw();
int h=t.geth();
if(w>pimpl->area.w) w=pimpl->area.w;
if(h>pimpl->area.h) h=pimpl->area.h;
Rect src(0,0,w,h);
pimpl->rnd.copy(t,src,dst);
}
}
void Brush::copyFill(Texture t,Rect src)
{
Rect dst=pimpl->area;
pimpl->rnd.copy(t,src,dst);
}
void Brush::copyFullFill(Texture t)
{
Rect dst=pimpl->area;
pimpl->rnd.copyTo(t,dst);
}
Brush::~Brush()
{
delete pimpl;
}

View File

@ -1,9 +0,0 @@
#pragma once
#include "mini_engine.h"
namespace App
{
/** Entrance of Application.
*/
void Main();
}

View File

@ -1,42 +0,0 @@
#pragma once
#include "config.h"
class InitManager_SDL
{
public:
InitManager_SDL();
~InitManager_SDL();
};
class InitManager_TTF
{
public:
InitManager_TTF();
~InitManager_TTF();
int openFont(const char* FileName,int Size);
int closeFont();
TTF_Font* font();
private:
TTF_Font* _font;
static int ctm;
};
class InitManager_IMG
{
public:
InitManager_IMG();
~InitManager_IMG();
SDL_Surface* loadimage(const char* FileName);
SDL_Texture* loadtexture(SDL_Renderer* rnd,const char* Filename);
};
class InitManager_Mix
{
public:
InitManager_Mix();
~InitManager_Mix();
};
extern InitManager_SDL* syssdl;
extern InitManager_IMG* sysimg;
extern InitManager_TTF* systtf;
extern InitManager_Mix* sysmix;

View File

@ -1,60 +0,0 @@
#pragma once
#include "config.h"
#include <memory>
#define _MUSIC_MANAGER_IMPL \
struct impl; \
std::shared_ptr<impl> pimpl;
/// Fwd Decl
class MusicPlayer;
class Music
{
public:
Music();
Music(const char* MusicFileName);
int load(const char* MusicFileName);
int unload();
bool ready();
~Music();
private:
_MUSIC_MANAGER_IMPL
friend class MusicPlayer;
};
class SoundEffect
{
public:
SoundEffect();
SoundEffect(const char* SoundEffectFileName);
int load(const char* SoundEffectFileName);
~SoundEffect();
private:
_MUSIC_MANAGER_IMPL
};
class MusicPlayer
{
public:
MusicPlayer(int freq=MIX_DEFAULT_FREQUENCY,Uint16 format=MIX_DEFAULT_FORMAT,int soundChannel=2,int chunkSize=1024);
~MusicPlayer();
int play();
int stop();
int add(Music& music,int times);
private:
static MusicPlayer* pInstance;
_MUSIC_MANAGER_IMPL
};
class SoundEffectPlayer
{
public:
SoundEffectPlayer();
~SoundEffectPlayer();
int play(SoundEffect& soundeffect,int times);
private:
_MUSIC_MANAGER_IMPL;
};

View File

@ -1,38 +0,0 @@
#pragma once
#ifdef __C4DROID__
#define _WINDOW_PROGRAM
#endif // __C4DROID__
#include <cstdio>
#include <cstdlib>
#include <cstring>
/// If you have configured SDL2 header files,
/// they will be used first.
#include "SDL2/SDL.h"
#undef main
#include "SDL2/SDL_ttf.h"
#include "SDL2/SDL_image.h"
#include "SDL2/SDL_mixer.h"
class NonCopyable
{
public:
NonCopyable()=default;
~NonCopyable()=default;
NonCopyable(const NonCopyable&)=delete;
NonCopyable& operator = (const NonCopyable&)=delete;
};
extern FILE* _log_fp;
#ifdef _WINDOW_PROGRAM
#define mlog_init() _log_fp=fopen("mini_engine_log.txt","w")
#define mlog(fmt,args...) do {if(_log_fp) {fprintf(_log_fp,fmt,##args);fprintf(_log_fp,"\n");fflush(_log_fp);} }while(0)
#else
#define mlog_init()
#define mlog(fmt,args...) printf(fmt,##args);printf("\n")
#endif

View File

@ -1,9 +0,0 @@
#pragma once
#include "basic_config.h"
#include "sdl_engine.h"
namespace Global
{
void ErrorQuit(const char* ErrorMessage);
}

View File

@ -1,22 +0,0 @@
#pragma once
#include "config.h"
/******************************************************************************/
/// InitManager
#include "InitManager.h"
/******************************************************************************/
/// MusicManager
#include "MusicManager.h"
/******************************************************************************/
/// Widget
#include "MiniEngine/Widget.h"
/******************************************************************************/
/// Application
#include "App.h"
void AllInit();
void AllQuit();

View File

@ -1,158 +0,0 @@
#pragma once
#include "basic_config.h"
SDL_Texture* RenderText(SDL_Renderer* rnd,TTF_Font* font,const char* Text,SDL_Color color,int* pw,int* ph);
SDL_Texture* RenderUTF8(SDL_Renderer* rnd,TTF_Font* font,const char* Text,SDL_Color color,int* pw,int* ph);
void TextureDraw(SDL_Renderer* rnd,SDL_Texture* texture,int dstx,int dsty);
bool isInRect(int x,int y,SDL_Rect rect);
bool isInRect(int x,int y,int LU_x,int LU_y,int RD_x,int RD_y);
void ClearMessageQueue();
int MyChangeDir(const char* DirName);
namespace Engine
{
class Rect
{
public:
Rect();
Rect(int incx,int incy,int incw,int inch);
SDL_Rect toSDLRect();
~Rect();
int x,y,w,h;
};
class RGBA
{
public:
RGBA();
RGBA(int incr,int incg,int incb,int inca);
~RGBA();
SDL_Color toSDLColor();
int r,g,b,a;
};
class Renderer;
class Texture;
class Font;
#define _SDL_ENGINE_IMPL_COPY_DECL(ClassName) \
ClassName(const ClassName&); \
ClassName(ClassName&&); \
ClassName& operator = (const ClassName&); \
ClassName& operator = (ClassName&&);
#define _SDL_ENGINE_IMPL \
struct impl; \
impl* pimpl;
#define DEFAULT_WIDTH 1024
#define DEFAULT_HEIGHT 768
class Window
{
public:
Window(int winw,int winh);
~Window();
_SDL_ENGINE_IMPL_COPY_DECL(Window);
Renderer getRenderer() const;
void resetRenderer();
Rect getSize();
void setSize(Rect r);
private:
_SDL_ENGINE_IMPL
};
class Surface
{
public:
~Surface();
_SDL_ENGINE_IMPL_COPY_DECL(Surface);
protected:
Surface();
private:
_SDL_ENGINE_IMPL
friend class Renderer;
friend class Font;
};
class Renderer
{
public:
virtual ~Renderer();
_SDL_ENGINE_IMPL_COPY_DECL(Renderer);
int clear();
void update();
int copy(Texture t,Rect src,Rect dst);
int copyTo(Texture t,Rect dst);
int copyFill(Texture t,Rect src);
int copyFullFill(Texture t);
Texture loadImage(const char* FileName);
Texture render(Surface surface);
int setColor(RGBA pack);
RGBA getColor(int* pStatus=nullptr);
int fillRect(Rect rect);
int drawRect(Rect rect);
/// Not Recommended
SDL_Renderer* getDirectRenderer();
protected:
Renderer();
private:
_SDL_ENGINE_IMPL
friend class Window;
};
class Texture
{
public:
~Texture();
int getw();
int geth();
_SDL_ENGINE_IMPL_COPY_DECL(Texture);
protected:
Texture();
private:
_SDL_ENGINE_IMPL
friend class Renderer;
};
class Font
{
public:
Font();
Font(const char* FontFileName,int sz);
int use(const char* FontFileName,int sz);
~Font();
_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);
Texture renderTextSolid(Renderer rnd,const char* Word,RGBA fg);
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:
_SDL_ENGINE_IMPL
};
class SDLSystem
{
public:
static void delay(int ms);
};
}/// End of namespace Engine

View File

@ -1,14 +0,0 @@
CXXFLAGS = -std=c++14 -Wall -O2 -D__C4DROID__ -Iinclude
LDFLAGS =
LDLIBS = -lSDL2_image -lSDL2_net -ltiff -ljpeg -lpng -lz -lSDL2_ttf -lfreetype -lSDL2_mixer -lSDL2_test -lsmpeg2 -lvorbisfile -lvorbis -logg -lstdc++ -lSDL2 -lEGL -lGLESv1_CM -lGLESv2 -landroid -Wl,--no-undefined -shared
PROG = program_name
OBJS = basic_config.o sdl_engine.o config.o InitManager.o mini_engine.o App.o main.o
all: $(PROG)
$(PROG): $(OBJS)
$(CXX) $(CXXFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ $(OBJS) `sdl2-config --cflags --libs`
clean:
rm -f $(PROG) $(OBJS)

View File

@ -1,44 +0,0 @@
#include "App.h"
using namespace Engine;
using namespace MiniEngine;
namespace App
{
/// Application Main Method
void Main()
{
Window wnd(1366,768);///16:9
Renderer rnd=wnd.getRenderer();
Font bigf;
if(bigf.use("msyh.ttf",72)<0)
{
mlog("Failed to open Font.");
return;
}
rnd.clear();
rnd.update();
MusicPlayer player;
Music m;
int ret=m.load("res/music.mp3");
printf("ret=%d\n",ret);
ret=player.add(m,-1);
printf("ret=%d\n",ret);
ret=player.play();
printf("ret=%d\n",ret);
printf("%s\n",Mix_GetError());
while(1) SDL_PollEvent(0);
/*
/// Sample Code of Brush
Brush b(wnd,Rect(wnd.getSize().w/2-50,wnd.getSize().h/2-50,100,100));
Texture t=rnd.loadImage("D:\\sample.png");
Rect dst(0,0,wnd.getSize().w,wnd.getSize().h);
b.copyTo(t,dst,false);
rnd.update();
SDL_Delay(2000);
*/
}
}

View File

@ -1,119 +0,0 @@
#include "InitManager.h"
InitManager_SDL::InitManager_SDL()
{
if(SDL_Init(SDL_INIT_EVERYTHING)<0)
{
#ifndef __C4DROID__
Global::ErrorQuit("Failed to init SDL2.");
#else
/// C4droid does not fully support SDL2,
/// so initializing everything crashes.
#endif
}
}
InitManager_SDL::~InitManager_SDL()
{
SDL_Quit();
}
InitManager_IMG::InitManager_IMG()
{
if(IMG_Init(IMG_INIT_JPG|IMG_INIT_PNG)<0)
{
Global::ErrorQuit("Failed to init SDL2 Image.");
}
}
SDL_Surface* InitManager_IMG::loadimage(const char* Filename)
{
return IMG_Load(Filename);
}
SDL_Texture* InitManager_IMG::loadtexture(SDL_Renderer* rnd,const char* Filename)
{
return IMG_LoadTexture(rnd,Filename);
}
InitManager_IMG::~InitManager_IMG()
{
IMG_Quit();
}
int InitManager_TTF::ctm=0;
InitManager_TTF::InitManager_TTF()
{
/// ~_~ check ctm and add it anyway
if(ctm++>0)
{
return;
}
if(TTF_Init()<0)
{
Global::ErrorQuit("Failed to init SDL2 TTF.");
}
_font=NULL;
}
int InitManager_TTF::openFont(const char* FileName,int Size)
{
_font=TTF_OpenFont(FileName,Size);
if(_font==NULL) return -1;
else return 0;
}
TTF_Font* InitManager_TTF::font()
{
return _font;
}
int InitManager_TTF::closeFont()
{
TTF_CloseFont(_font);
_font=NULL;
return 0;
}
InitManager_TTF::~InitManager_TTF()
{
/// Close Font anyway.
if(_font) closeFont();
/// ~_~ Firstly ctm=ctm-1, if ctm still > 0, then return ( TTF_Quit Not Executed )
if(--ctm>0)
{
return;
}
TTF_Quit();
}
InitManager_Mix::InitManager_Mix()
{
if(Mix_Init(MIX_INIT_MP3)<0)
{
Global::ErrorQuit("Failed to Init Mixer.");
}
/*
if(Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 1024)<0)
{
Global::ErrorQuit("Failed to OpenAudio");
}
Mix_AllocateChannels(32);
*/
}
InitManager_Mix::~InitManager_Mix()
{
//Mix_CloseAudio();
Mix_Quit();
}
InitManager_SDL* syssdl=NULL;
InitManager_IMG* sysimg=NULL;
InitManager_TTF* systtf=NULL;
InitManager_Mix* sysmix=NULL;

View File

@ -1,86 +0,0 @@
#include "MusicManager.h"
#include <memory>
#include <vector>
using namespace std;
struct Music::impl
{
friend class Music;
shared_ptr<Mix_Music> sMusic;
};
Music::Music()
{
pimpl.reset(new impl);
}
Music::Music(const char* MusicFileName) : Music()
{
load(MusicFileName);
}
int Music::load(const char* MusicFileName)
{
Mix_Music* ptemp=Mix_LoadMUS(MusicFileName);
if(ptemp==nullptr) return -1;
pimpl->sMusic.reset(ptemp,Mix_FreeMusic);
return 0;
}
int Music::unload()
{
printf("Unloaded.\n");
if(pimpl->sMusic.get())
{
printf("Reset to NULL\n");
pimpl->sMusic.reset();
return 0;
}
else return -2;
}
bool Music::ready()
{
return (pimpl->sMusic.get()!=nullptr);
}
Music::~Music()
{
}
struct MusicPlayer::impl
{
vector<pair<Music,int>> vec;
};
MusicPlayer::MusicPlayer(int freq,Uint16 format,int soundChannel,int chunkSize)
{
pimpl.reset(new impl);
if(pInstance) return;
Mix_OpenAudio(freq,format,soundChannel,chunkSize);
pInstance=this;
}
MusicPlayer::~MusicPlayer()
{
if(pInstance) Mix_CloseAudio();
pInstance=nullptr;
}
int MusicPlayer::play()
{
return Mix_PlayMusic(pimpl->vec.front().first.pimpl->sMusic.get(),pimpl->vec.front().second);
}
int MusicPlayer::add(Music& music,int times)
{
if(!music.ready()) return -1;
pimpl->vec.push_back(make_pair(music,times));
return 0;
}
MusicPlayer* MusicPlayer::pInstance=nullptr;
struct SoundEffect::impl
{
friend class SoundEffect;
shared_ptr<Mix_Chunk> sChunk;
};
SoundEffect::SoundEffect()
{
pimpl.reset(new impl);
}

View File

@ -1,3 +0,0 @@
#include "basic_config.h"
FILE* _log_fp=NULL;

View File

@ -1,10 +0,0 @@
#include "config.h"
namespace Global
{
void ErrorQuit(const char* ErrorMessage)
{
mlog(ErrorMessage);
exit(0);
}
}

View File

@ -1,13 +0,0 @@
#include "config.h"
#include "App.h"
int main()
{
/// Initialize SDL2...
AllInit();
/// Call Application Main
App::Main();
/// Clean Up SDL2
AllQuit();
return 0;
}

View File

@ -1,20 +0,0 @@
#include "mini_engine.h"
void AllInit()
{
mlog_init();
syssdl=new InitManager_SDL;
sysimg=new InitManager_IMG;
systtf=new InitManager_TTF;
sysmix=new InitManager_Mix;
}
void AllQuit()
{
delete sysmix;
delete systtf;
delete sysimg;
delete syssdl;
}

View File

@ -1,144 +0,0 @@
#include "sdl_engine.h"
#include "unistd.h"
#include <memory>
using namespace std;
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);
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;
}
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));
}
bool isInRect(int x,int y,int LU_x,int LU_y,int RD_x,int RD_y)
{
return ((x>=LU_x&&x<=RD_x)&&(y>=LU_y&&y<=RD_y));
}
void ClearMessageQueue()
{
/// Clear Message Queue
while(SDL_PeepEvents(NULL,1,SDL_GETEVENT,SDL_FIRSTEVENT,SDL_LASTEVENT));
}
SDL_Color color_white { 255,255,255 };
SDL_Color color_black { 0,0,0 };
int MyChangeDir(const char* DirName)
{
mlog("Change Dir to \"%s\"",DirName);
int ret=chdir(DirName);
mlog("Change Dir returns %d",ret);
return ret;
}
namespace Engine
{
/// Rect
#include "sdl_engine_rect.hpp"
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));
}
SDL_Window* getRawWindow()
{
return sWnd.get();
}
};
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
#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"
void SDLSystem::delay(int ms)
{
SDL_Delay(ms);
}
}/// End of namespace Engine

View File

@ -1,61 +0,0 @@
Font::Font()
{
pimpl=new impl;
}
Font::Font(const char* FontFileName,int sz) : Font()
{
use(FontFileName,sz);
}
Font::Font(const Font& inc) : Font()
{
*pimpl=*(inc.pimpl);
}
Font& Font::operator = (const Font& inc)
{
*pimpl=*(inc.pimpl);
return *this;
}
Font::Font(Font&& inc)
{
pimpl=inc.pimpl;
inc.pimpl=nullptr;
}
Font& Font::operator = (Font&& inc)
{
*pimpl=*(inc.pimpl);
inc.pimpl=nullptr;
return *this;
}
Font::~Font()
{
delete pimpl;
}
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;
}
Texture Font::renderText(Renderer rnd,const char* Word,RGBA fg)
{
Surface surf;
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,25 +0,0 @@
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()
{
}
SDL_Rect Rect::toSDLRect()
{
SDL_Rect rect;
rect.x=x;
rect.y=y;
rect.w=w;
rect.h=h;
return rect;
}

View File

@ -1,98 +0,0 @@
Renderer::Renderer()
{
pimpl=new impl;
}
Renderer::Renderer(const Renderer& inc) : Renderer()
{
*pimpl=*(inc.pimpl);
}
Renderer& Renderer::operator = (const Renderer& inc)
{
*pimpl=*(inc.pimpl);
return *this;
}
Renderer::Renderer(Renderer&& inc)
{
pimpl=inc.pimpl;
inc.pimpl=nullptr;
}
Renderer& Renderer::operator = (Renderer&& inc)
{
*pimpl=*(inc.pimpl);
inc.pimpl=nullptr;
return *this;
}
Renderer::~Renderer()
{
delete pimpl;
}
int Renderer::clear()
{
return SDL_RenderClear(pimpl->sRnd.get());
}
Texture Renderer::loadImage(const char* FileName)
{
Texture t;
t.pimpl->set(IMG_LoadTexture(pimpl->sRnd.get(),FileName));
return t;
}
Texture Renderer::render(Surface surface)
{
Texture t;
t.pimpl->set(SDL_CreateTextureFromSurface(pimpl->sRnd.get(),surface.pimpl->getRawSurface()));
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->getRawTexture(),&s,&d);
}
int Renderer::copyTo(Texture t,Rect dst)
{
SDL_Rect d=dst.toSDLRect();
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->getRawTexture(),&s,NULL);
}
int Renderer::copyFullFill(Texture t)
{
return SDL_RenderCopy(pimpl->sRnd.get(),t.pimpl->getRawTexture(),NULL,NULL);
}
int Renderer::setColor(RGBA pack)
{
return SDL_SetRenderDrawColor(pimpl->sRnd.get(),pack.r,pack.g,pack.b,pack.a);
}
RGBA Renderer::getColor(int* pstatus)
{
Uint8 r,g,b,a;
int ret=SDL_GetRenderDrawColor(pimpl->sRnd.get(),&r,&g,&b,&a);
RGBA pack(r,g,b,a);
if(pstatus) *pstatus=ret;
return pack;
}
int Renderer::fillRect(Rect rect)
{
auto inr=rect.toSDLRect();
return SDL_RenderFillRect(pimpl->sRnd.get(),&inr);
}
int Renderer::drawRect(Rect rect)
{
auto inr=rect.toSDLRect();
return SDL_RenderDrawRect(pimpl->sRnd.get(),&inr);
}

View File

@ -1,24 +0,0 @@
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()
{
}

View File

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

View File

@ -1,37 +0,0 @@
Texture::Texture()
{
pimpl=new impl;
}
Texture::Texture(const Texture& inc) : Texture()
{
*pimpl=*(inc.pimpl);
}
Texture& Texture::operator = (const Texture& inc)
{
*pimpl=*(inc.pimpl);
return *this;
}
Texture::Texture(Texture&& inc)
{
pimpl=inc.pimpl;
inc.pimpl=nullptr;
}
Texture& Texture::operator = (Texture&& inc)
{
*(pimpl)=*(inc.pimpl);
inc.pimpl=nullptr;
return *this;
}
int Texture::getw()
{
return pimpl->w;
}
int Texture::geth()
{
return pimpl->h;
}
Texture::~Texture()
{
delete pimpl;
}

View File

@ -1,55 +0,0 @@
Window::Window(int winw,int winh)
{
pimpl=new impl;
SDL_Window* wnd=SDL_CreateWindow("Engine",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,winw,winh,SDL_WINDOW_SHOWN);
pimpl->set(wnd);
}
Window::Window(const Window& inc)
{
pimpl=new impl;
*pimpl=*(inc.pimpl);
}
Window& Window::operator = (const Window& inc)
{
*pimpl=*(inc.pimpl);
return *this;
}
Window::Window(Window&& inc)
{
pimpl=inc.pimpl;
inc.pimpl=nullptr;
}
Window& Window::operator = (Window&& inc)
{
*pimpl=*(inc.pimpl);
inc.pimpl=nullptr;
return *this;
}
Window::~Window()
{
delete pimpl;
}
Renderer Window::getRenderer() const
{
return pimpl->rnd;
}
Rect Window::getSize()
{
int w,h;
SDL_GetWindowSize(pimpl->getRawWindow(),&w,&h);
Rect rect(0,0,w,h);
return rect;
}
void Window::setSize(Rect rect)
{
SDL_SetWindowSize(pimpl->getRawWindow(),rect.w,rect.h);
}

33
SDLWrapper/Audio.cpp Normal file
View File

@ -0,0 +1,33 @@
#include "Audio.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();
}
AudioPlayer::_Audio* AudioPlayer::_sysAudio = nullptr;
int AudioPlayer::_sysAudioCounter = 0;
#include "end_code.h"

20
SDLWrapper/Audio.h Normal file
View File

@ -0,0 +1,20 @@
#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;
};
#include "end_code.h"

14
SDLWrapper/ColorMode.cpp Normal file
View File

@ -0,0 +1,14 @@
#include "ColorMode.h"
#include "begin_code.h"
ColorMode::ColorMode(int R, int G, int B)
{
r = R;
g = G;
b = B;
}
ColorMode::ColorMode()
{
r = g = b = 0;
}
#include "end_code.h"

11
SDLWrapper/ColorMode.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
#include "include.h"
#include "begin_code.h"
class ColorMode
{
public:
int r, g, b;
ColorMode(int R, int G, int B);
ColorMode();
};
#include "end_code.h"

81
SDLWrapper/Cursor.cpp Normal file
View File

@ -0,0 +1,81 @@
#include "Cursor.h"
#include "_caster.h"
#include "begin_code.h"
//private
void Cursor::_set(SDL_Cursor* p)
{
_cur.reset(p,SDL_FreeCursor);
}
//private
void Cursor::_set_no_delete(SDL_Cursor* p)
{
_cur.reset(p,[](SDL_Cursor* p) {});
}
//private
SDL_Cursor* Cursor::_get()
{
return _cur.get();
}
//private
void Cursor::_clear()
{
_cur.reset();
}
Cursor::Cursor(Surface surf,Point hotspot)
{
Cursor ns;
SDL_Cursor* cursor=SDL_CreateColorCursor(surf._get(),hotspot.x,hotspot.y);
ns._set(cursor);
}
Cursor::Cursor(SystemCursorType type)
{
Cursor ns;
ns._set(SDL_CreateSystemCursor(_internal::getSDLSystemCursorFromSystemCursorType(type)));
}
//static
Cursor Cursor::GetActiveCursor()
{
Cursor ns;
ns._set_no_delete(SDL_GetCursor());
return ns;
}
//static
Cursor Cursor::GetDefaultCursor()
{
Cursor ns;
ns._set_no_delete(SDL_GetDefaultCursor());
return ns;
}
//static
bool Cursor::isShow()
{
return (SDL_ShowCursor(SDL_QUERY)==SDL_ENABLE);
}
//static
void Cursor::setShow(bool Settings)
{
SDL_ShowCursor(Settings?SDL_ENABLE:SDL_DISABLE);
}
void Cursor::release()
{
_clear();
}
void Cursor::activate()
{
if(_get()!=nullptr)
{
SDL_SetCursor(_get());
}
}
#include "end_code.h"

30
SDLWrapper/Cursor.h Normal file
View File

@ -0,0 +1,30 @@
#pragma once
#include "include.h"
#include "_SystemCursorType.h"
#include "Point.h"
#include "Surface.h"
#include "begin_code.h"
class Cursor
{
public:
Cursor()=default;
Cursor(SystemCursorType);
Cursor(Surface surf,Point hotspot= {0,0});
static Cursor GetActiveCursor();
static Cursor GetDefaultCursor();
static void setShow(bool);
static bool isShow();
void activate();
void release();
private:
std::shared_ptr<SDL_Cursor> _cur;
void _set(SDL_Cursor*);
void _set_no_delete(SDL_Cursor*);
SDL_Cursor* _get();
void _clear();
};
#include "end_code.h"

View File

@ -0,0 +1,18 @@
#include "ErrorViewer.h"
#include "include.h"
#include "begin_code.h"
void ErrorViewer::fetch()
{
str = SDL_GetError();
}
std::string ErrorViewer::getError() const
{
return str;
}
const char * ErrorViewer::what() const throw()
{
return str.c_str();
}
#include "end_code.h"

14
SDLWrapper/ErrorViewer.h Normal file
View File

@ -0,0 +1,14 @@
#pragma once
#include <exception>
#include <string>
#include "begin_code.h"
class ErrorViewer : public std::exception
{
public:
void fetch();
std::string getError() const;
const char* what() const throw() override;
private:
std::string str;
};
#include "end_code.h"

396
SDLWrapper/Font.cpp Normal file
View File

@ -0,0 +1,396 @@
#include "Font.h"
#include "_caster.h"
#include "begin_code.h"
// private
void Font::_set(TTF_Font* p)
{
_font.reset(p,TTF_CloseFont);
}
// private
void Font::_clear()
{
_font.reset();
}
// private
TTF_Font* Font::_get() const
{
return _font.get();
}
Font::Font(const std::string& FontFileName, size_t size) throw(ErrorViewer)
{
if (use(FontFileName, size) != 0)
{
ErrorViewer e;
e.fetch();
throw e;
}
}
int Font::use(const 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"

122
SDLWrapper/Font.h Normal file
View File

@ -0,0 +1,122 @@
#pragma once
#include "include.h"
#include "_FontStyle.h"
#include "_FontHint.h"
#include "Rect.h"
#include "Surface.h"
#include "Texture.h"
#include "Renderer.h"
#include <vector>
#include "begin_code.h"
class Font
{
public:
Font() = default;
Font(const std::string& FontFileName, size_t size) throw(ErrorViewer);
int use(const 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"

21
SDLWrapper/IncludeAll.h Normal file
View File

@ -0,0 +1,21 @@
#pragma once
/// Sorted by alphabet sequence. Some files are ignored.
#include "ColorMode.h"
#include "Cursor.h"
#include "ErrorViewer.h"
#include "Font.h"
#include "Log.h"
#include "MessageBox.h"
#include "Music.h"
#include "Point.h"
#include "Rect.h"
#include "Renderer.h"
#include "RGBA.h"
#include "RWOP.h"
#include "SDLSystem.h"
#include "SharedLibrary.h"
#include "Sound.h"
#include "Surface.h"
#include "Texture.h"
#include "Timer.h"
#include "Window.h"

50
SDLWrapper/Log.cpp Normal file
View File

@ -0,0 +1,50 @@
#include "Log.h"
#include "begin_code.h"
void LogSystem::d(const char* fmt,...)
{
va_list ap;
va_start(ap,fmt);
SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_DEBUG,fmt,ap);
va_end(ap);
}
void LogSystem::v(const char* fmt,...)
{
va_list ap;
va_start(ap,fmt);
SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_VERBOSE,fmt,ap);
va_end(ap);
}
void LogSystem::e(const char* fmt,...)
{
va_list ap;
va_start(ap,fmt);
SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_ERROR,fmt,ap);
va_end(ap);
}
void LogSystem::i(const char* fmt,...)
{
va_list ap;
va_start(ap,fmt);
SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_INFO,fmt,ap);
va_end(ap);
}
void LogSystem::w(const char* fmt,...)
{
va_list ap;
va_start(ap,fmt);
SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_WARN,fmt,ap);
va_end(ap);
}
void LogSystem::critical(const char* fmt,...)
{
va_list ap;
va_start(ap,fmt);
SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_CRITICAL,fmt,ap);
va_end(ap);
}
#include "end_code.h"

13
SDLWrapper/Log.h Normal file
View File

@ -0,0 +1,13 @@
#pragma once
#include "include.h"
#include "begin_code.h"
class LogSystem
{
static void v(const char* fmt,...);/// Verbose
static void d(const char* fmt,...);/// Debug
static void i(const char* fmt,...);/// Information
static void w(const char* fmt,...);/// Warning
static void e(const char* fmt,...);/// Error
static void critical(const char* fmt,...);/// Critical
};
#include "end_code.h"

66
SDLWrapper/MessageBox.cpp Normal file
View File

@ -0,0 +1,66 @@
#include "MessageBox.h"
#include "begin_code.h"
WindowMessageBoxButton::WindowMessageBoxButton()
{
_hitoption=0;
text="Button";
callback=[]() {};
}
WindowMessageBoxButton::WindowMessageBoxButton(const std::string& ButtonText,const std::function<void()>& CallbackFunc) : text(ButtonText)
{
_hitoption=0;
callback=CallbackFunc;
}
void WindowMessageBoxButton::setHitAsEscape(bool enable)
{
_hitoption=enable?1:0;
}
void WindowMessageBoxButton::setHitAsReturn(bool enable)
{
_hitoption=enable?2:0;
}
bool WindowMessageBoxButton::isHitAsEscape() const
{
return _hitoption==1;
}
bool WindowMessageBoxButton::isHitAsReturn() const
{
return _hitoption==2;
}
WindowMessageBox::WindowMessageBox()
{
boxtype=MessageBoxType::Information;
}
WindowMessageBox::WindowMessageBox(const std::string& Title,const std::string& Text,MessageBoxType BoxType,const std::function<void()>& DefaultCallback) : title(Title), text(Text)
{
boxtype=BoxType;
defaultcallback=DefaultCallback;
}
void WindowMessageBox::addButton(const WindowMessageBoxButton& button)
{
_vec.push_back(button);
}
int WindowMessageBox::getButtonNum() const
{
return _vec.size();
}
WindowMessageBoxButton& WindowMessageBox::getButton(int index)
{
return _vec.at(index);
}
const WindowMessageBoxButton& WindowMessageBox::getButtonConst(int index) const
{
return _vec.at(index);
}
#include "end_code.h"

45
SDLWrapper/MessageBox.h Normal file
View File

@ -0,0 +1,45 @@
#pragma once
#include "include.h"
#include "_MessageBoxType.h"
#include <string>
#include <functional>
#include <vector>
#include "begin_code.h"
class WindowMessageBoxButton
{
public:
WindowMessageBoxButton();
WindowMessageBoxButton(const std::string& ButtonText,const std::function<void()>& CallbackFunc=[]() {});
std::string text;
std::function<void()> callback;
/// Default: no hit option set.
void setHitAsReturn(bool);
void setHitAsEscape(bool);
bool isHitAsReturn() const;
bool isHitAsEscape() const;
private:
int _hitoption;
};
class WindowMessageBox
{
public:
WindowMessageBox();
WindowMessageBox(const std::string& Title,const std::string& Text,MessageBoxType BoxType=MessageBoxType::Information,const std::function<void()>& DefaultCallback=[]() {});
MessageBoxType boxtype;
std::string title;
std::string text;
std::function<void()> defaultcallback;
void addButton(const WindowMessageBoxButton& button);
int getButtonNum() const;
WindowMessageBoxButton& getButton(int index);
const WindowMessageBoxButton& getButtonConst(int index) const;
private:
std::vector<WindowMessageBoxButton> _vec;
};
#include "end_code.h"

112
SDLWrapper/Music.cpp Normal file
View File

@ -0,0 +1,112 @@
#include "Music.h"
#include "begin_code.h"
//private
void Music::_set(Mix_Music* p)
{
_music.reset(p,Mix_FreeMusic);
}
//private
void Music::_clear()
{
_music.reset();
}
//private
Mix_Music* Music::_get() const
{
return _music.get();
}
Music::Music(const std::string& Filename)
{
_set(Mix_LoadMUS(Filename.c_str()));
}
bool Music::isReady() const
{
return (_get()!=nullptr);
}
void Music::release()
{
_clear();
}
//static
int MusicPlayer::GetDecoderNum()
{
return Mix_GetNumMusicDecoders();
}
//static
std::string MusicPlayer::GetDecoderName(int index)
{
return std::string(Mix_GetMusicDecoder(index));
}
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() const
{
return (Mix_PlayingMusic() == 1);
}
bool MusicPlayer::isPaused() const
{
return (Mix_PausedMusic() == 1);
}
int MusicPlayer::isFading() const
{
switch (Mix_FadingMusic())
{
case MIX_NO_FADING:
return 0;
case MIX_FADING_IN:
return 1;
case MIX_FADING_OUT:
return 2;
default:
return -1;
}
}
int MusicPlayer::setPosition(double second)
{
return Mix_SetMusicPosition(second);
}
#include "end_code.h"

49
SDLWrapper/Music.h Normal file
View File

@ -0,0 +1,49 @@
#pragma once
#include "include.h"
#include <memory>
#include <string>
#include "ErrorViewer.h"
#include "Audio.h"
#include "__Noncopyable.h"
#include "begin_code.h"
/// Forward Declaration
class Music
{
public:
Music()=default;
Music(const std::string& Filename);
bool isReady() const;
void release();
private:
std::shared_ptr<Mix_Music> _music;
void _set(Mix_Music*);
void _clear();
Mix_Music* _get() const;
friend class MusicPlayer;
};
class MusicPlayer : public AudioPlayer, public NonCopyable
{
public:
static int GetDecoderNum();
static std::string GetDecoderName(int index);
/// Play Music. Loop: -1:Infinite, >0:Exact that time.
int play(Music music, int loops);
void pause();
void resume();
void rewind();
int setPosition(double second);
int stop();
int fadeIn(int loops, int ms);
int fadeOut(int ms);
bool isPlaying() const;
bool isPaused() const;
int isFading() const;
private:
Music m;
};
#include "end_code.h"

30
SDLWrapper/Point.cpp Normal file
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"

14
SDLWrapper/Point.h Normal file
View File

@ -0,0 +1,14 @@
#pragma once
#include "include.h"
#include "Rect.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"

38
SDLWrapper/RGBA.cpp Normal file
View File

@ -0,0 +1,38 @@
#include "RGBA.h"
#include "begin_code.h"
RGBA::RGBA(int R, int G, int B, int A)
{
r = R;
g = G;
b = B;
a = A;
}
RGBA::RGBA(ColorMode mode, int A)
{
r = mode.r;
g = mode.g;
b = mode.b;
a = A;
}
RGBA::RGBA()
{
r = g = b = a = 0;
}
SDL_Color RGBA::toSDLColor() const
{
SDL_Color c;
c.r = r;
c.g = g;
c.b = b;
c.a = a;
return c;
}
ColorMode RGBA::toColorMode() const
{
return ColorMode(r, g, b);
}
#include "end_code.h"

17
SDLWrapper/RGBA.h Normal file
View File

@ -0,0 +1,17 @@
#pragma once
#include "include.h"
#include "ColorMode.h"
#include "begin_code.h"
class RGBA
{
public:
int r, g, b, a;
RGBA(int R, int G, int B, int A);
RGBA(ColorMode mode, int A);
RGBA();
SDL_Color toSDLColor() const;
ColorMode toColorMode() const;
};
#include "end_code.h"

48
SDLWrapper/RWOP.cpp Normal file
View File

@ -0,0 +1,48 @@
#include "RWOP.h"
#include "begin_code.h"
// private
void RWOP::_set(SDL_RWops* p)
{
_op.reset(p,[](SDL_RWops* p)
{
SDL_RWclose(p);
});
}
// private
SDL_RWops* RWOP::_get() const
{
return _op.get();
}
void RWOP::_clear()
{
_op.reset();
}
RWOP::RWOP(FILE* fp,bool autoclose)
{
SDL_bool b=autoclose?SDL_TRUE:SDL_FALSE;
_set(SDL_RWFromFP(fp,b));
}
RWOP::RWOP(const std::string& filename,const std::string& openmode)
{
_set(SDL_RWFromFile(filename.c_str(),openmode.c_str()));
}
RWOP::RWOP(const void* mem,int size)
{
_set(SDL_RWFromConstMem(mem,size));
}
RWOP::RWOP(void* mem,int size)
{
_set(SDL_RWFromMem(mem,size));
}
void RWOP::release()
{
_clear();
}
#include "end_code.h"

26
SDLWrapper/RWOP.h Normal file
View File

@ -0,0 +1,26 @@
#pragma once
#include "include.h"
#include <cstdio>
#include <string>
#include <memory>
#include "begin_code.h"
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;
void release();
private:
std::shared_ptr<SDL_RWops> _op;
SDL_RWops* _get() const;
void _clear();
void _set(SDL_RWops*);
friend class Surface;
friend class Renderer;
};
#include "end_code.h"

72
SDLWrapper/Rect.cpp Normal file
View File

@ -0,0 +1,72 @@
#include "Rect.h"
#include "begin_code.h"
Rect::Rect(int X, int Y, int W, int H)
{
x = X;
y = Y;
w = W;
h = H;
}
// explicit
Rect::Rect(const SDL_Rect& r):Rect(r.x,r.y,r.w,r.h)
{
}
Rect::Rect()
{
x = y = w = h = 0;
}
SDL_Rect Rect::toSDLRect() const
{
SDL_Rect r;
r.x = x;
r.y = y;
r.w = w;
r.h = h;
return r;
}
bool Rect::isEmpty() const
{
SDL_Rect r=toSDLRect();
return SDL_RectEmpty(&r)==SDL_TRUE;
}
bool Rect::operator == (const Rect& r) const
{
SDL_Rect a=toSDLRect(),b=r.toSDLRect();
return SDL_RectEquals(&a,&b)==SDL_TRUE;
}
bool Rect::hasIntersection(const Rect& r) const
{
SDL_Rect a=toSDLRect(),b=r.toSDLRect();
return SDL_HasIntersection(&a,&b)==SDL_TRUE;
}
Rect Rect::getIntersection(const Rect& r) const
{
SDL_Rect a=toSDLRect(),b=r.toSDLRect(),c;
if(SDL_IntersectRect(&a,&b,&c)==SDL_TRUE)
{
return Rect(c);
}
else
{
return Rect();
}
}
Rect Rect::getUnion(const Rect& r) const
{
SDL_Rect a=toSDLRect(),b=r.toSDLRect(),c;
SDL_UnionRect(&a,&b,&c);//void
return Rect(c);
}
#include "end_code.h"

21
SDLWrapper/Rect.h Normal file
View File

@ -0,0 +1,21 @@
#pragma once
#include "include.h"
#include "begin_code.h"
class Rect
{
public:
int x, y, w, h;
Rect(int X, int Y, int W, int H);
explicit Rect(const SDL_Rect&);
Rect();
SDL_Rect toSDLRect() const;
bool isEmpty() const;
bool operator == (const Rect&) const;
bool hasIntersection(const Rect&) const;
Rect getIntersection(const Rect&) const;
Rect getUnion(const Rect&) const;
};
#include "end_code.h"

452
SDLWrapper/Renderer.cpp Normal file
View File

@ -0,0 +1,452 @@
#include "Renderer.h"
#include "_caster.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();
}
Renderer::Renderer(Window& wnd,std::initializer_list<RendererType> RendererFlags) throw (ErrorViewer)
{
if(createRenderer(wnd,RendererFlags)!=0)
{
throw ErrorViewer();
}
}
int Renderer::createRenderer(Window& wnd,std::initializer_list<RendererType> RendererFlags)
{
Uint32 flag = 0;
for (auto v : RendererFlags)
{
flag |= _rendertype_caster(v);
}
return _createRenderer_Real(wnd,flag);
}
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();
}
// private
Uint32 Renderer::_rendertype_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;
}
// private
int Renderer::_createRenderer_Real(Window& wnd,Uint32 flags)
{
SDL_Renderer* pSDLRnd=SDL_CreateRenderer(wnd._get(), -1, flags);
if(pSDLRnd!=nullptr)
{
_set(pSDLRnd);
return 0;
}
else
{
return -1;
}
}
#include "end_code.h"

138
SDLWrapper/Renderer.h Normal file
View File

@ -0,0 +1,138 @@
#pragma once
#include "include.h"
#include "_RendererType.h"
#include "_FlipMode.h"
#include "Window.h"
#include "Surface.h"
#include "Texture.h"
#include <initializer_list>
#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"

243
SDLWrapper/SDLSystem.cpp Normal file
View File

@ -0,0 +1,243 @@
#include "SDLSystem.h"
#include "begin_code.h"
//static
int SDLSystem::SDLInit()
{
return SDL_Init(SDL_INIT_EVERYTHING);
}
//static
void SDLSystem::SDLQuit()
{
SDL_Quit();
}
//static
int SDLSystem::IMGInit()
{
return IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG);
}
//static
void SDLSystem::IMGQuit()
{
IMG_Quit();
}
//static
int SDLSystem::TTFInit()
{
return TTF_Init();
}
//static
void SDLSystem::TTFQuit()
{
TTF_Quit();
}
//static
int SDLSystem::MixInit()
{
return Mix_Init(MIX_INIT_MP3);
}
//static
void SDLSystem::MixQuit()
{
Mix_Quit();
}
//static
void SDLSystem::Init()
{
SDLInit();
IMGInit();
TTFInit();
MixInit();
}
//static
void SDLSystem::Quit()
{
MixQuit();
TTFQuit();
IMGQuit();
SDLQuit();
}
//static
void SDLSystem::Delay(int ms)
{
SDL_Delay(ms);
}
//static
PowerState SDLSystem::GetPowerState()
{
SDL_PowerState ret=SDL_GetPowerInfo(NULL,NULL);
switch(ret)
{
case SDL_POWERSTATE_ON_BATTERY:
return PowerState::OnBattery;
case SDL_POWERSTATE_NO_BATTERY:
return PowerState::NoBattery;
case SDL_POWERSTATE_CHARGING:
return PowerState::Charging;
case SDL_POWERSTATE_CHARGED:
return PowerState::Charged;
case SDL_POWERSTATE_UNKNOWN:
default:
return PowerState::Unknown;
}
}
//static
int SDLSystem::GetPowerLifeLeft()
{
int i;
SDL_GetPowerInfo(&i,NULL);
return i;
}
//static
int SDLSystem::GetPowerPrecentageLeft()
{
int i;
SDL_GetPowerInfo(NULL,&i);
return i;
}
//static
Platform SDLSystem::GetPlatform()
{
std::string s(SDL_GetPlatform());
if(s=="Windows")
{
return Platform::Windows;
}
else if(s=="Mac OS X")
{
return Platform::MacOS;
}
else if(s=="Linux")
{
return Platform::Linux;
}
else if(s=="iOS")
{
return Platform::iOS;
}
else if(s=="Android")
{
return Platform::Android;
}
else
{
return Platform::Unknown;
}
}
//static
void SDLSystem::StartTextInput()
{
SDL_StartTextInput();
}
//static
bool SDLSystem::IsTextInputActive()
{
return SDL_IsTextInputActive()==SDL_TRUE;
}
//static
void SDLSystem::StopTextInput()
{
SDL_StopTextInput();
}
//static
bool SDLSystem::HasScreenKeyboardSupport()
{
return SDL_HasScreenKeyboardSupport()==SDL_TRUE;
}
//static
std::tuple<int,int,int> SDLSystem::GetSDLCompileVersion()
{
SDL_version ver;
SDL_VERSION(&ver);
return std::make_tuple(ver.major,ver.minor,ver.patch);
}
//static
std::tuple<int,int,int> SDLSystem::GetSDLLinkedVersion()
{
SDL_version ver;
SDL_GetVersion(&ver);
return std::make_tuple(ver.major,ver.minor,ver.patch);
}
//static
std::tuple<int,int,int> SDLSystem::GetIMGCompileVersion()
{
SDL_version ver;
SDL_IMAGE_VERSION(&ver);
return std::make_tuple(ver.major,ver.minor,ver.patch);
}
//static
std::tuple<int,int,int> SDLSystem::GetIMGLinkedVersion()
{
const SDL_version* ptr=IMG_Linked_Version();
return std::make_tuple(ptr->major,ptr->minor,ptr->patch);
}
//static
std::tuple<int,int,int> SDLSystem::GetMixCompileVersion()
{
SDL_version ver;
SDL_MIXER_VERSION(&ver);
return std::make_tuple(ver.major,ver.minor,ver.patch);
}
//static
std::tuple<int,int,int> SDLSystem::GetMixLinkedVersion()
{
const SDL_version* ptr=Mix_Linked_Version();
return std::make_tuple(ptr->major,ptr->minor,ptr->patch);
}
//static
std::tuple<int,int,int> SDLSystem::GetTTFCompileVersion()
{
SDL_version ver;
SDL_TTF_VERSION(&ver);
return std::make_tuple(ver.major,ver.minor,ver.patch);
}
//static
std::tuple<int,int,int> SDLSystem::GetTTFLinkedVersion()
{
const SDL_version* ptr=TTF_Linked_Version();
return std::make_tuple(ptr->major,ptr->minor,ptr->patch);
}
//static
int SDLSystem::GetCPUCount()
{
return SDL_GetCPUCount();
}
//static
int SDLSystem::GetCPUCacheLineSize()
{
return SDL_GetCPUCacheLineSize();
}
//static
int SDLSystem::GetSystemRAM()
{
return SDL_GetSystemRAM();
}
#include "end_code.h"

65
SDLWrapper/SDLSystem.h Normal file
View File

@ -0,0 +1,65 @@
#pragma once
#include "include.h"
#include "_PowerState.h"
#include "_Platform.h"
#include <tuple>
#include <string>
#include "begin_code.h"
class SDLSystem
{
public:
static int SDLInit();
static void SDLQuit();
static int IMGInit();
static void IMGQuit();
static int TTFInit();
static void TTFQuit();
static int MixInit();
static void MixQuit();
static void Init();
static void Quit();
static void Delay(int ms);
static PowerState GetPowerState();
static int GetPowerLifeLeft();
static int GetPowerPrecentageLeft();
static Platform GetPlatform();
static void StartTextInput();
static bool IsTextInputActive();
static void StopTextInput();
static bool HasScreenKeyboardSupport();
static std::tuple<int,int,int> GetSDLCompileVersion();
static std::tuple<int,int,int> GetSDLLinkedVersion();
static std::tuple<int,int,int> GetIMGCompileVersion();
static std::tuple<int,int,int> GetIMGLinkedVersion();
static std::tuple<int,int,int> GetMixCompileVersion();
static std::tuple<int,int,int> GetMixLinkedVersion();
static std::tuple<int,int,int> GetTTFCompileVersion();
static std::tuple<int,int,int> GetTTFLinkedVersion();
static int GetCPUCount();
static int GetCPUCacheLineSize();
/// RAM is calculated in MB.
static int GetSystemRAM();
class Android
{
public:
static std::string GetInternal();
static bool ExternalAvaliable();
static bool CanReadExternal();
static bool CanWriteExternal();
static std::string GetExternal();
static void* GetJNIEnv();
};
};
#include "end_code.h"

View File

@ -0,0 +1,67 @@
#include "SharedLibrary.h"
#include "begin_code.h"
//private
void* SharedLibrary::_get() const
{
return _obj.get();
}
//private
void SharedLibrary::_set(void* ptr)
{
_obj.reset(ptr,SDL_UnloadObject);
}
//private
void SharedLibrary::_clear()
{
_obj.reset();
}
SharedLibrary::SharedLibrary()
{
_obj=nullptr;
}
SharedLibrary::SharedLibrary(const std::string& Filename)
{
_obj=nullptr;
load(Filename);
}
int SharedLibrary::load(const std::string& Filename)
{
if(_get()!=nullptr) return -1; /// Loaded
else
{
void* ptr=SDL_LoadObject(Filename.c_str());
if(ptr)
{
_set(ptr);
return 0; /// Success
}
else return -2; /// Failed to load
}
}
int SharedLibrary::unload()
{
if(_get()!=nullptr)
{
_clear();
return 0; /// Success to unload
}
else return -1; /// Not Loaded.
}
void* SharedLibrary::get(const std::string& FunctionName) const
{
if(_get()==nullptr) return nullptr;
else return SDL_LoadFunction(_get(),FunctionName.c_str());
}
void SharedLibrary::release()
{
_clear();
}
#include "end_code.h"

View File

@ -0,0 +1,29 @@
#pragma once
#include "include.h"
#include <string>
#include <memory>
#include "begin_code.h"
class SharedLibrary
{
public:
SharedLibrary();
SharedLibrary(const std::string& Filename);
~SharedLibrary()=default;
int load(const std::string& Filename);
int unload();
template<typename ReturnType,typename... Arguments>
std::function<ReturnType(Arguments...)> get(const std::string& FunctionName)
{
return std::function<ReturnType(Arguments...)>(reinterpret_cast<ReturnType(*)(Arguments...)>(get(FunctionName)));
}
void* get(const std::string& FunctionName) const;
void release();
private:
void* _get() const;
void _set(void*);
void _clear();
std::shared_ptr<void> _obj;
};
#include "end_code.h"

168
SDLWrapper/Sound.cpp Normal file
View File

@ -0,0 +1,168 @@
#include "Sound.h"
#include "begin_code.h"
//private
void Sound::_set(Mix_Chunk* p)
{
_sound.reset(p,Mix_FreeChunk);
}
//private
void Sound::_clear()
{
_sound.reset();
}
//private
Mix_Chunk* Sound::_get() const
{
return _sound.get();
}
Sound::Sound(const std::string& WAVFilename)
{
_set(Mix_LoadWAV(WAVFilename.c_str()));
}
bool Sound::isReady() const
{
return (_get()!=nullptr);
}
void Sound::release()
{
_clear();
}
//private
void Channel::_set(int ChannelID)
{
_id=ChannelID;
}
//private
int Channel::_get() const
{
return _id;
}
//private
void Channel::_clear()
{
_id=-1;
}
//protected
Channel::Channel()
{
_id=-1;
}
Channel& Channel::playSound(Sound sound, int loops) throw (ErrorViewer)
{
int cret=Mix_PlayChannel(_get(),sound._get(),loops);
if(cret==-1)
{
ErrorViewer e;
e.fetch();
throw e;
}
_set(cret);
return *this;
}
Channel& Channel::fadeIn(Sound sound, int loops, int ms) throw (ErrorViewer)
{
int cret=Mix_FadeInChannel(_get(),sound._get(),loops,ms);
if(cret==-1)
{
ErrorViewer e;
e.fetch();
throw e;
}
_set(cret);
return *this;
}
int Channel::fadeOut(int ms)
{
return Mix_FadeOutChannel(_get(), ms);
}
void Channel::pause()
{
Mix_Pause(_get());
}
void Channel::resume()
{
Mix_Resume(_get());
}
int Channel::stop()
{
return Mix_HaltChannel(_get());
}
int Channel::setPanning(uint8_t left, uint8_t right)
{
return Mix_SetPanning(_get(),left,right);
}
int Channel::setPosition(int16_t angle, uint8_t distance)
{
return Mix_SetPosition(_get(),angle,distance);
}
int Channel::setDistance(uint8_t distance)
{
return Mix_SetDistance(_get(),distance);
}
int Channel::setReverseStereo(int flip)
{
return Mix_SetReverseStereo(_get(),flip);
}
int Channel::addEffect(Mix_EffectFunc_t f, Mix_EffectDone_t d, void* arg)
{
return Mix_RegisterEffect(_get(),f,d,arg);
}
int Channel::removeEffect(Mix_EffectFunc_t f)
{
return Mix_UnregisterEffect(_get(),f);
}
int Channel::removeAllEffect()
{
return Mix_UnregisterAllEffects(_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);
}
Channel SoundPlayer::playSound(Sound sound, int loops) throw(ErrorViewer)
{
Channel c;
c.playSound(sound,loops);
return c;
}
Channel SoundPlayer::fadeIn(Sound sound, int loops, int ms) throw(ErrorViewer)
{
Channel c;
c.fadeIn(sound,loops,ms);
return c;
}
#include "end_code.h"

66
SDLWrapper/Sound.h Normal file
View File

@ -0,0 +1,66 @@
#pragma once
#include "include.h"
#include <memory>
#include <string>
#include "Audio.h"
#include "ErrorViewer.h"
#include "begin_code.h"
class Sound
{
public:
Sound() = default;
Sound(const std::string& WAVFilename);
bool isReady() const;
void release();
private:
std::shared_ptr<Mix_Chunk> _sound;
void _set(Mix_Chunk*);
void _clear();
Mix_Chunk* _get() const;
friend class Channel;
};
class Channel
{
public:
Channel& playSound(Sound sound,int loops) throw (ErrorViewer);
Channel& fadeIn(Sound sound,int loops,int ms) throw (ErrorViewer);
int fadeOut(int ms);
void pause();
void resume();
int stop();
/// Experimental
int setPanning(uint8_t left,uint8_t right);
int setPosition(int16_t angle,uint8_t distance);
int setDistance(uint8_t distance);
int setReverseStereo(int flip);
/// Experimental: Direct Add/Remove Effect
int addEffect(Mix_EffectFunc_t f, Mix_EffectDone_t d, void *arg);
int removeEffect(Mix_EffectFunc_t f);
int removeAllEffect();
protected:
Channel();
private:
void _set(int ChannelID);
int _get() const;
void _clear();
int _id;
friend class SoundPlayer;
};
class SoundPlayer : public AudioPlayer
{
public:
static int GetDecoderNum();
static std::string GetDecoderName(int index);
SoundPlayer(int NumChannels = 16);
Channel playSound(Sound sound, int loops) throw (ErrorViewer);
Channel fadeIn(Sound sound, int loops, int ms) throw (ErrorViewer);
};
#include "end_code.h"

359
SDLWrapper/Surface.cpp Normal file
View File

@ -0,0 +1,359 @@
#include "Surface.h"
#include "_caster.h"
#include "begin_code.h"
//private
void Surface::_set(SDL_Surface* p)
{
_surf.reset(p,SDL_FreeSurface);
}
//private
void Surface::_clear()
{
_surf.reset();
}
//private
SDL_Surface* Surface::_get() const
{
return _surf.get();
}
//private
void Surface::_set_no_delete(SDL_Surface* p)
{
_surf.reset(p,[](SDL_Surface*) {});
}
Surface::Surface(int width,int height,int depth,int Rmask,int Gmask,int Bmask,int Amask) throw(ErrorViewer)
{
if(createAs(width,height,depth,Rmask,Gmask,Bmask,Amask)!=0)
{
ErrorViewer e;
e.fetch();
throw e;
}
}
Surface::Surface(int width,int height,int depth,RGBA maskPack) throw (ErrorViewer)
: Surface(width,height,depth,maskPack.r,maskPack.g,maskPack.b,maskPack.a)
{
}
Surface::Surface(int width,int height,int depth,Uint32 surfaceFormat) throw(ErrorViewer)
{
if(createAs(width,height,depth,surfaceFormat)!=0)
{
ErrorViewer e;
e.fetch();
throw e;
}
}
Surface::Surface(const std::string& filename) throw(ErrorViewer)
{
if(loadAs(filename)!=0)
{
ErrorViewer e;
e.fetch();
throw e;
}
}
Surface::Surface(const RWOP& rwop) throw (ErrorViewer)
{
if(loadAs(rwop)!=0)
{
ErrorViewer e;
e.fetch();
throw e;
}
}
//static
Surface Surface::load(const std::string& filename)
{
Surface s;
s.loadAs(filename);
return s;
}
//static
Surface Surface::load(const RWOP& rwop)
{
Surface s;
s.loadAs(rwop);
return s;
}
//static
Surface Surface::create(int width, int height, int depth, int Rmask, int Gmask, int Bmask, int Amask)
{
Surface s;
s.create(width,height,depth,Rmask,Gmask,Bmask,Amask);
return s;
}
//static
Surface Surface::create(int width, int height, int depth, Uint32 surfaceFormat)
{
Surface s;
s.create(width,height,depth,surfaceFormat);
return s;
}
int Surface::loadAs(const std::string& filename)
{
SDL_Surface* temp=IMG_Load(filename.c_str());
if(temp==nullptr)
{
return -1;
}
else
{
_set(temp);
return 0;
}
}
int Surface::loadAs(const RWOP& rwop)
{
SDL_Surface* temp=IMG_Load_RW(rwop._get(),0);
if(temp==nullptr)
{
return -1;
}
else
{
_set(temp);
return 0;
}
}
int Surface::createAs(int width,int height,int depth,int Rmask,int Gmask,int Bmask,int Amask)
{
SDL_Surface* temp=SDL_CreateRGBSurface(0,width,height,depth,Rmask,Gmask,Bmask,Amask);
if(temp==nullptr)
{
return -1;
}
else
{
_set(temp);
return 0;
}
}
int Surface::createAs(int width,int height,int depth,Uint32 surfaceFormat)
{
/// FIXME: This Function is available from SDL2.0.5. But the linker report a undefined reference.
/*
SDL_Surface* temp=SDL_CreateRGBSurfaceWithFormat(0,width,height,depth,surfaceFormat);
if(temp==nullptr)
{
return -1;
}
else
{
_set(temp);
return 0;
}
*/
SDL_SetError("[MiniEngine] SDL_CreateRGBSurfaceWithFormat Not Linked.");
return -1;
}
int Surface::getw() const
{
if(_get()!=nullptr)
{
return _get()->w;
}
else
{
/// return -1 on null surface pointer
return -1;
}
}
int Surface::geth() const
{
if(_get()!=nullptr)
{
return _get()->h;
}
else
{
/// return -1 on null surface pointer
return -1;
}
}
BlendMode Surface::getBlendMode() const
{
SDL_BlendMode temp;
/// FIXME: return value are ignored.
SDL_GetSurfaceBlendMode(_get(),&temp);
return _internal::getBlendModeFromSDLBlendMode(temp);
}
int Surface::setBlendMode(BlendMode mode)
{
return SDL_SetSurfaceBlendMode(_get(),_internal::getSDLBlendModeFromBlendMode(mode));
}
int Surface::savePNG(const std::string& filename) const
{
return IMG_SavePNG(_get(),filename.c_str());
}
int Surface::blit(const Surface& s,const Rect& src,const Rect& dst)
{
SDL_Rect rsrc=src.toSDLRect();
SDL_Rect rdst=dst.toSDLRect();
return SDL_BlitSurface(s._get(),&rsrc,_get(),&rdst);
}
int Surface::blitTo(const Surface& s,const Rect& dst)
{
SDL_Rect rdst=dst.toSDLRect();
return SDL_BlitSurface(s._get(),NULL,_get(),&rdst);
}
int Surface::blitTo(const Surface& s,const Point& lupoint)
{
return blitTo(s,Rect(lupoint.x,lupoint.y,s.getw(),s.geth()));
}
int Surface::blitFill(const Surface& s,const Rect& src)
{
SDL_Rect rsrc=src.toSDLRect();
return SDL_BlitSurface(s._get(),&rsrc,_get(),NULL);
}
int Surface::blitFullFill(const Surface& s)
{
return SDL_BlitSurface(s._get(),NULL,_get(),NULL);
}
int Surface::blitScaled(const Surface& s,const Rect& src,const Rect& dst)
{
SDL_Rect rsrc=src.toSDLRect();
SDL_Rect rdst=dst.toSDLRect();
return SDL_BlitScaled(s._get(),&rsrc,_get(),&rdst);
}
int Surface::blitScaledTo(const Surface& s,const Rect& dst)
{
SDL_Rect rdst=dst.toSDLRect();
return SDL_BlitScaled(s._get(),NULL,_get(),&rdst);
}
int Surface::blitScaledTo(const Surface& s,const Point& lupoint)
{
return blitScaledTo(s,Rect(lupoint.x,lupoint.y,s.getw(),s.geth()));
}
int Surface::blitScaledFill(const Surface& s,const Rect& src)
{
SDL_Rect rsrc=src.toSDLRect();
return SDL_BlitScaled(s._get(),&rsrc,_get(),NULL);
}
int Surface::blitScaledFullFill(const Surface& s)
{
return SDL_BlitScaled(s._get(),NULL,_get(),NULL);
}
int Surface::setClipRect(const Rect& clipRect)
{
auto m=clipRect.toSDLRect();
return (SDL_SetClipRect(_get(),&m) == SDL_TRUE) ? 0 : -1;
}
Rect Surface::getClipRect() const
{
SDL_Rect rect;
SDL_GetClipRect(_get(),&rect);
return Rect(rect);
}
void Surface::disableClipping()
{
SDL_SetClipRect(_get(),NULL);
}
int Surface::setAlphaMode(int alpha)
{
return SDL_SetSurfaceAlphaMod(_get(),alpha);
}
int Surface::getAlphaMode() const
{
Uint8 al;
SDL_GetSurfaceAlphaMod(_get(),&al);
return al;
}
int Surface::setColorMode(ColorMode mode)
{
return SDL_SetSurfaceColorMod(_get(),mode.r,mode.g,mode.b);
}
ColorMode Surface::getColorMode() const
{
Uint8 r,g,b;
SDL_GetSurfaceColorMod(_get(),&r,&g,&b);
ColorMode mode;
mode.r=r;
mode.g=g;
mode.b=b;
return mode;
}
void Surface::setRGBA(const RGBA& pack)
{
setColorMode(pack.toColorMode());
setAlphaMode(pack.a);
}
RGBA Surface::getRGBA() const
{
return RGBA(getColorMode(),getAlphaMode());
}
bool Surface::mustlock() const
{
return SDL_MUSTLOCK(_get());
}
int Surface::lock()
{
return SDL_LockSurface(_get());
}
void Surface::unlock()
{
SDL_UnlockSurface(_get());
}
bool Surface::isReady() const
{
return _get()!=nullptr;
}
void Surface::release()
{
_clear();
}
/// Experimental (Not constant)
SDL_Surface* Surface::getRawPointer()
{
return _get();
}
#include "end_code.h"

86
SDLWrapper/Surface.h Normal file
View File

@ -0,0 +1,86 @@
#pragma once
#include "include.h"
#include "_BlendMode.h"
#include "RGBA.h"
#include "Point.h"
#include "RWOP.h"
#include "ErrorViewer.h"
#include "begin_code.h"
class Surface
{
public:
Surface()=default;
Surface(int width,int height,int depth,int Rmask,int Gmask,int Bmask,int Amask) throw(ErrorViewer);
Surface(int width,int height,int depth,RGBA colorPack) throw(ErrorViewer);
Surface(int width,int height,int depth,Uint32 surfaceFormat) throw(ErrorViewer);
Surface(const std::string& filename) throw(ErrorViewer);
Surface(const RWOP& rwop) throw(ErrorViewer);
~Surface() = default;
/// static functions
static Surface load(const std::string& filename);
static Surface load(const RWOP& rwop);
static Surface create(int width,int height,int depth,int Rmask,int Gmask,int Bmask,int Amask);
static Surface create(int width,int height,int depth,Uint32 surfaceFormat);
/// xxxAs will clear the current surface if loaded or created successfully.
int loadAs(const std::string& filename);
int loadAs(const RWOP& rwop);
int createAs(int width,int height,int depth,int Rmask,int Gmask,int Bmask,int Amask);
int createAs(int width,int height,int depth,Uint32 surfaceFormat);
int savePNG(const std::string& filename) const;
int getw() const;
int geth() const;
BlendMode getBlendMode() const;
int setBlendMode(BlendMode mode);
/// Rendering functions. Copy an external surface to this surface. So it has no constant attribute.
int blit(const Surface& s,const Rect& src,const Rect& dst);
int blitTo(const Surface& t, const Rect& dst);
int blitTo(const Surface& t, const Point& lupoint);
int blitFill(const Surface& t, const Rect& src);
int blitFullFill(const Surface& t);
int blitScaled(const Surface& s,const Rect& src,const Rect& dst);
int blitScaledTo(const Surface& t, const Rect& dst);
int blitScaledTo(const Surface& t, const Point& lupoint);
int blitScaledFill(const Surface& t, const Rect& src);
int blitScaledFullFill(const Surface& t);
int setClipRect(const Rect& clipRect);
Rect getClipRect() const;
void disableClipping();
int setAlphaMode(int alpha);
int getAlphaMode() const;
ColorMode getColorMode() const;
int setColorMode(ColorMode mode);
RGBA getRGBA() const;
void setRGBA(const RGBA& pack);
bool mustlock() const;
int lock();
void unlock();
bool isReady() const;
void release();
/// Experimental : Get SDL_Surface Pointer and then do anything you want!
/// In case you can do anything with this pointer, this function should not has constant attribute.
SDL_Surface* getRawPointer();
private:
std::shared_ptr<SDL_Surface> _surf;
void _set(SDL_Surface*);
void _set_no_delete(SDL_Surface*);
void _clear();
SDL_Surface* _get() const;
friend class Window;
friend class Renderer;
friend class Font;
friend class Cursor;
};
#include "end_code.h"

126
SDLWrapper/Texture.cpp Normal file
View File

@ -0,0 +1,126 @@
#include "Texture.h"
#include "_caster.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"

45
SDLWrapper/Texture.h Normal file
View File

@ -0,0 +1,45 @@
#pragma once
#include "include.h"
#include "Rect.h"
#include "RGBA.h"
#include "ColorMode.h"
#include "_BlendMode.h"
#include <memory>
#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"

51
SDLWrapper/Timer.h Normal file
View File

@ -0,0 +1,51 @@
#pragma once
#include "include.h"
#include <functional>
#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"

250
SDLWrapper/Window.cpp Normal file
View File

@ -0,0 +1,250 @@
#include "Window.h"
#include "_caster.h"
#include "begin_code.h"
//private
void Window::_set(SDL_Window* p)
{
_wnd.reset(p,SDL_DestroyWindow);
}
//private
void Window::_clear()
{
_wnd.reset();
}
//private
SDL_Window* Window::_get() const
{
return _wnd.get();
}
Window::Window(std::string Title, int Width, int Height,
std::initializer_list<WindowType> WindowFlags, int WindowPositionX, int WindowPositionY) throw(ErrorViewer)
{
/// Calculate Window Flags
Uint32 windowFlag=0;
for(auto v:WindowFlags)
{
windowFlag|=_internal::getSDLWindowFlagsFromWindowType(v);
}
SDL_Window* temp = SDL_CreateWindow(Title.c_str(), WindowPositionX, WindowPositionY, Width, Height, windowFlag);
if (temp == NULL)
{
ErrorViewer e;
e.fetch();
throw e;
}
_set(temp);
}
Rect Window::getSize() const
{
int w, h;
SDL_GetWindowSize(_get(), &w, &h);
return Rect(0, 0, w, h);
}
void Window::setSize(const Rect& sizeRect)
{
setSize(sizeRect.w, sizeRect.h);
}
void Window::setSize(int w, int h)
{
SDL_SetWindowSize(_get(), w, h);
}
Point Window::getPosition() const
{
int x, y;
SDL_GetWindowPosition(_get(), &x, &y);
return Point(x, y);
}
void Window::setPosition(int x, int y)
{
SDL_SetWindowPosition(_get(), x, y);
}
void Window::setPosition(const Point& point)
{
SDL_SetWindowPosition(_get(), point.x, point.y);
}
void Window::setTitle(const std::string& Title)
{
SDL_SetWindowTitle(_get(), Title.c_str());
}
std::string Window::getTitle() const
{
return std::string(SDL_GetWindowTitle(_get()));
}
void Window::setGrab(bool isGrab)
{
SDL_SetWindowGrab(_get(),isGrab?SDL_TRUE:SDL_FALSE);
}
bool Window::getGrab() const
{
return (SDL_GetWindowGrab(_get())==SDL_TRUE)?true:false;
}
#if _MINIENGINE_SDL_VERSION_ATLEAST(2,0,5)
int Window::setOpacity(float opacity)
{
return SDL_SetWindowOpacity(_get(),opacity);
}
float Window::getOpacity() const
{
float op=-1;
SDL_GetWindowOpacity(_get(),&op);
return op;
}
#endif /// End of SDL2 2.0.5 Require.
/// FIXME: Not Implemented.
void Window::setResizable(bool resizable)
{
//SDL_SetWindowResizable(_get(), resizable?SDL_TRUE:SDL_FALSE);
}
void Window::show()
{
SDL_ShowWindow(_get());
}
void Window::hide()
{
SDL_HideWindow(_get());
}
void Window::raise()
{
SDL_RaiseWindow(_get());
}
void Window::minimize()
{
SDL_MinimizeWindow(_get());
}
void Window::maximize()
{
SDL_MaximizeWindow(_get());
}
void Window::restore()
{
SDL_RestoreWindow(_get());
}
_DECL_DEPRECATED Surface Window::getSurface()
{
SDL_Surface* temp = SDL_GetWindowSurface(_get());
Surface s;
/// Don't Free This Surface
s._set_no_delete(temp);
return s;
}
void Window::release()
{
_clear();
}
int Window::showSimpleMessageBox(MessageBoxType type,const std::string& Title,const std::string& Message) const
{
Uint32 flags=0;
switch(type)
{
case MessageBoxType::Error:
flags=SDL_MESSAGEBOX_ERROR;
break;
case MessageBoxType::Information:
flags=SDL_MESSAGEBOX_INFORMATION;
break;
case MessageBoxType::Warning:
flags=SDL_MESSAGEBOX_WARNING;
break;
}
return SDL_ShowSimpleMessageBox(flags,Title.c_str(),Message.c_str(),_get());
}
int Window::showMessageBox(const WindowMessageBox& box) const
{
SDL_MessageBoxData mboxdata;
mboxdata.title=box.title.c_str();
mboxdata.message=box.text.c_str();
mboxdata.window=_get();
mboxdata.colorScheme=nullptr;
mboxdata.numbuttons=box.getButtonNum();
SDL_MessageBoxButtonData* pButtonArr=(SDL_MessageBoxButtonData*)malloc(sizeof(SDL_MessageBoxButtonData)*(mboxdata.numbuttons));
if(pButtonArr==nullptr)
{
/// Failed to malloc
return -2;
}
for(int i=0; i<mboxdata.numbuttons; i++)
{
pButtonArr[i].buttonid=i+1;
pButtonArr[i].text=box.getButtonConst(i).text.c_str();
pButtonArr[i].flags=
(box.getButtonConst(i).isHitAsEscape()) ? SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT
:(
(box.getButtonConst(i).isHitAsReturn()) ?SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT
:0
);
}
mboxdata.buttons=pButtonArr;
switch(box.boxtype)
{
case MessageBoxType::Error:
mboxdata.flags=SDL_MESSAGEBOX_ERROR;
break;
case MessageBoxType::Information:
mboxdata.flags=SDL_MESSAGEBOX_INFORMATION;
break;
case MessageBoxType::Warning:
mboxdata.flags=SDL_MESSAGEBOX_WARNING;
break;
}
int clickret=-2;
/// Call SDL2 to show MessageBox.
int ret=SDL_ShowMessageBox(&mboxdata,&clickret);
if(ret==0)
{
/// Success.
if(clickret>=1)
{
/// If any button is clicked, call the callback function associated with it.
if(box.getButtonConst(clickret-1).callback)
{
box.getButtonConst(clickret-1).callback();
}
}
else
{
/// ... else, call the default callback
if(box.defaultcallback) box.defaultcallback();
}
}
/// Free allocated memory
free(pButtonArr);
return ret;
}
bool Window::isScreenKeyboardShown()
{
return SDL_IsScreenKeyboardShown(_get())==SDL_TRUE;
}
#include "end_code.h"

66
SDLWrapper/Window.h Normal file
View File

@ -0,0 +1,66 @@
#pragma once
#include "include.h"
#include "_WindowType.h"
#include "_MessageBoxType.h"
#include "ErrorViewer.h"
#include "MessageBox.h"
#include "Surface.h"
#include "begin_code.h"
class Window
{
public:
Window()=default;
Window(std::string Title, int Width, int Height,
std::initializer_list<WindowType> WindowFlags = {WindowType::Shown},
int WindowPositionX=SDL_WINDOWPOS_CENTERED, int WindowPositionY=SDL_WINDOWPOS_CENTERED) throw(ErrorViewer);
Rect getSize() const;
void setSize(const Rect& sizeRect);
void setSize(int w, int h);
Point getPosition() const;
void setPosition(int x, int y);
void setPosition(const Point& point);
void setTitle(const std::string& Title);
std::string getTitle() const;
void setGrab(bool isGrab);
bool getGrab() const;
#if _MINIENGINE_SDL_VERSION_ATLEAST(2,0,5)
/// SDL2.0.5 Required.
int setOpacity(float opacity);
float getOpacity() const;
#endif
/// FIXME: Not Implemented.
void setResizable(bool resizable);
/// Use UTF8 in Title and Message please.
int showSimpleMessageBox(MessageBoxType type,const std::string& Title,const std::string& Message) const;
int showMessageBox(const WindowMessageBox& box) const;
void show();
void hide();
void raise();
void minimize();
void maximize();
void restore();
_DECL_DEPRECATED Surface getSurface();
bool isScreenKeyboardShown();
void release();
private:
std::shared_ptr<SDL_Window> _wnd;
void _set(SDL_Window*);
void _clear();
SDL_Window* _get() const;
friend class Renderer;
};
#include "end_code.h"

10
SDLWrapper/_BlendMode.h Normal file
View File

@ -0,0 +1,10 @@
#pragma once
#include "begin_code.h"
enum class BlendMode
{
None,
Blend,
Add,
Mod
};
#include "end_code.h"

9
SDLWrapper/_FlipMode.h Normal file
View File

@ -0,0 +1,9 @@
#pragma once
#include "begin_code.h"
enum class FlipMode
{
None,
Horizontal,
Vertical
};
#include "end_code.h"

11
SDLWrapper/_FontHint.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
#include "begin_code.h"
enum class FontHint
{
Normal,
Light,
Mono,
None,
Error
};
#include "end_code.h"

11
SDLWrapper/_FontStyle.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
#include "begin_code.h"
enum class FontStyle
{
Normal,
Bold,
Italic,
UnderLine,
StrikeThrough
};
#include "end_code.h"

View File

@ -0,0 +1,9 @@
#pragma once
#include "begin_code.h"
enum class MessageBoxType
{
Error,
Warning,
Information
};
#include "end_code.h"

12
SDLWrapper/_Platform.h Normal file
View File

@ -0,0 +1,12 @@
#pragma once
#include "begin_code.h"
enum class Platform
{
Unknown,
Windows,
MacOS,
Linux,
iOS,
Android
};
#include "end_code.h"

11
SDLWrapper/_PowerState.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
#include "begin_code.h"
enum class PowerState
{
Unknown,
OnBattery,
NoBattery,
Charging,
Charged
};
#include "end_code.h"

View File

@ -0,0 +1,10 @@
#pragma once
#include "begin_code.h"
enum class RendererType
{
Software,
Accelerated,
PresentSync,
TargetTexture
};
#include "end_code.h"

View File

@ -0,0 +1,10 @@
#pragma once
#include "begin_code.h"
enum class SystemCursorType
{
Arrow, Ibeam, CrossHair,
Wait, WaitArrow,
SizeNWSE, SizeNESW, SizeWE, SizeNS, SizeAll,
No, Hand
};
#include "end_code.h"

12
SDLWrapper/_WindowType.h Normal file
View File

@ -0,0 +1,12 @@
#pragma once
#include "begin_code.h"
enum class WindowType
{
FullScreen, OpenGL, Shown, Hidden,
Borderless, Resizable, Minimized, Maximized,
InputGrabbed, InputFocus, MouseFocus,
FullScreenDesktop, Foreign, AllowHighDPI,
MouseCapture, AlwaysOnTop, SkipTaskBar,
Utility, ToolTip, PopUpMenu
};
#include "end_code.h"

View File

@ -0,0 +1,11 @@
#pragma once
#include "begin_code.h"
class NonCopyable
{
protected:
NonCopyable() = default;
~NonCopyable() = default;
NonCopyable(const NonCopyable&) = delete;
NonCopyable& operator = (const NonCopyable&) = delete;
};
#include "end_code.h"

10
SDLWrapper/__Optional.h Normal file
View File

@ -0,0 +1,10 @@
#pragma once
#include <experimental/optional>
#include "begin_code.h"
template<typename T>
using Optional = std::experimental::optional<T>;
using BadOptionalAccess = std::experimental::bad_optional_access;
constexpr std::experimental::nullopt_t NullOpt = std::experimental::nullopt;
#include "end_code.h"

219
SDLWrapper/_caster.cpp Normal file
View File

@ -0,0 +1,219 @@
#include "_caster.h"
#include "begin_code.h"
namespace _internal
{
BlendMode getBlendModeFromSDLBlendMode(SDL_BlendMode mode)
{
switch(mode)
{
case SDL_BLENDMODE_ADD:
return BlendMode::Add;
case SDL_BLENDMODE_BLEND:
return BlendMode::Blend;
case SDL_BLENDMODE_MOD:
return BlendMode::Mod;
case SDL_BLENDMODE_NONE:
default:/// return BlendMode::None on default.
return BlendMode::None;
}
}
SDL_BlendMode getSDLBlendModeFromBlendMode(BlendMode mode)
{
switch(mode)
{
case BlendMode::Add:
return SDL_BLENDMODE_ADD;
case BlendMode::Blend:
return SDL_BLENDMODE_BLEND;
case BlendMode::Mod:
return SDL_BLENDMODE_MOD;
case BlendMode::None:
default:/// return SDL_BLENDMODE_NONE on default.
return SDL_BLENDMODE_NONE;
}
}
/// FIXME: return SDL_WindowFlags or Uint32 ?
Uint32 getSDLWindowFlagsFromWindowType(WindowType type)
{
switch(type)
{
case WindowType::FullScreen:
return SDL_WINDOW_FULLSCREEN;
case WindowType::OpenGL:
return SDL_WINDOW_OPENGL;
case WindowType::Shown:
return SDL_WINDOW_SHOWN;
case WindowType::Hidden:
return SDL_WINDOW_HIDDEN;
case WindowType::Borderless:
return SDL_WINDOW_BORDERLESS;
case WindowType::Resizable:
return SDL_WINDOW_RESIZABLE;
case WindowType::Minimized:
return SDL_WINDOW_MINIMIZED;
case WindowType::Maximized:
return SDL_WINDOW_MAXIMIZED;
case WindowType::InputGrabbed:
return SDL_WINDOW_INPUT_GRABBED;
case WindowType::InputFocus:
return SDL_WINDOW_INPUT_FOCUS;
case WindowType::MouseFocus:
return SDL_WINDOW_MOUSE_FOCUS;
case WindowType::FullScreenDesktop:
return SDL_WINDOW_FULLSCREEN_DESKTOP;
case WindowType::Foreign:
return SDL_WINDOW_FOREIGN;
case WindowType::AllowHighDPI:
return SDL_WINDOW_ALLOW_HIGHDPI;
case WindowType::MouseCapture:
return SDL_WINDOW_MOUSE_CAPTURE;
#if _MINIENGINE_SDL_VERSION_ATLEAST(2,0,5) /// SDL 2.0.5 Required
case WindowType::AlwaysOnTop:
return SDL_WINDOW_ALWAYS_ON_TOP;
case WindowType::SkipTaskBar:
return SDL_WINDOW_SKIP_TASKBAR;
case WindowType::Utility:
return SDL_WINDOW_UTILITY;
case WindowType::ToolTip:
return SDL_WINDOW_TOOLTIP;
case WindowType::PopUpMenu:
return SDL_WINDOW_POPUP_MENU;
#endif // End of SDL2.0.5 Require
default:
return 0;/// Return 0 on default.
}
}
SystemCursorType getCursorTypeFromSDLSystemCursor(SDL_SystemCursor id)
{
switch(id)
{
case SDL_SYSTEM_CURSOR_ARROW:
return SystemCursorType::Arrow;
case SDL_SYSTEM_CURSOR_CROSSHAIR:
return SystemCursorType::CrossHair;
case SDL_SYSTEM_CURSOR_HAND:
return SystemCursorType::Hand;
case SDL_SYSTEM_CURSOR_IBEAM:
return SystemCursorType::Ibeam;
case SDL_SYSTEM_CURSOR_NO:
return SystemCursorType::No;
case SDL_SYSTEM_CURSOR_SIZEALL:
return SystemCursorType::SizeAll;
case SDL_SYSTEM_CURSOR_SIZENESW:
return SystemCursorType::SizeNESW;
case SDL_SYSTEM_CURSOR_SIZENS:
return SystemCursorType::SizeNS;
case SDL_SYSTEM_CURSOR_SIZENWSE:
return SystemCursorType::SizeNWSE;
case SDL_SYSTEM_CURSOR_SIZEWE:
return SystemCursorType::SizeWE;
case SDL_SYSTEM_CURSOR_WAIT:
return SystemCursorType::Wait;
case SDL_SYSTEM_CURSOR_WAITARROW:
return SystemCursorType::WaitArrow;
default:/// return SystemCursorType::Arrow on default.
return SystemCursorType::Arrow;
}
}
SDL_SystemCursor getSDLSystemCursorFromSystemCursorType(SystemCursorType type)
{
switch(type)
{
case SystemCursorType::Arrow:
return SDL_SYSTEM_CURSOR_ARROW;
case SystemCursorType::CrossHair:
return SDL_SYSTEM_CURSOR_CROSSHAIR;
case SystemCursorType::Hand:
return SDL_SYSTEM_CURSOR_HAND;
case SystemCursorType::Ibeam:
return SDL_SYSTEM_CURSOR_IBEAM;
case SystemCursorType::No:
return SDL_SYSTEM_CURSOR_NO;
case SystemCursorType::SizeAll:
return SDL_SYSTEM_CURSOR_SIZEALL;
case SystemCursorType::SizeNESW:
return SDL_SYSTEM_CURSOR_SIZENESW;
case SystemCursorType::SizeNS:
return SDL_SYSTEM_CURSOR_SIZENS;
case SystemCursorType::SizeNWSE:
return SDL_SYSTEM_CURSOR_SIZENWSE;
case SystemCursorType::SizeWE:
return SDL_SYSTEM_CURSOR_SIZEWE;
case SystemCursorType::Wait:
return SDL_SYSTEM_CURSOR_WAIT;
case SystemCursorType::WaitArrow:
return SDL_SYSTEM_CURSOR_WAITARROW;
default:/// return SDL_SYSTEM_CURSOR_ARROW on default.
return SDL_SYSTEM_CURSOR_ARROW;
}
}
int getTTFFontStyleFromFontStyle(FontStyle style)
{
switch(style)
{
case FontStyle::Bold:
return TTF_STYLE_BOLD;
case FontStyle::Italic:
return TTF_STYLE_ITALIC;
case FontStyle::Normal:
return TTF_STYLE_NORMAL;
case FontStyle::StrikeThrough:
return TTF_STYLE_STRIKETHROUGH;
case FontStyle::UnderLine:
return TTF_STYLE_UNDERLINE;
default:
return TTF_STYLE_NORMAL;
}
}
std::vector<FontStyle> getFontStyleVecFromMixedTTFFontStyle(int Mixed_TTF_Font_Style)
{
std::vector<FontStyle> vec;
if(Mixed_TTF_Font_Style&TTF_STYLE_BOLD)
{
vec.push_back(FontStyle::Bold);
}
if(Mixed_TTF_Font_Style&TTF_STYLE_ITALIC)
{
vec.push_back(FontStyle::Italic);
}
if(Mixed_TTF_Font_Style&TTF_STYLE_STRIKETHROUGH)
{
vec.push_back(FontStyle::StrikeThrough);
}
if(Mixed_TTF_Font_Style&TTF_STYLE_UNDERLINE)
{
vec.push_back(FontStyle::UnderLine);
}
if(vec.empty())
{
vec.push_back(FontStyle::Normal);
}
return vec;
}
SDL_RendererFlip getSDLRendererFlipFromFlipMode(FlipMode mode)
{
switch(mode)
{
case FlipMode::None:
return SDL_FLIP_NONE;
case FlipMode::Horizontal:
return SDL_FLIP_HORIZONTAL;
case FlipMode::Vertical:
return SDL_FLIP_VERTICAL;
default:
/// return non-flip mode on default.
return SDL_FLIP_NONE;
}
}
}/// End of namespace _internal
#include "end_code.h"

22
SDLWrapper/_caster.h Normal file
View File

@ -0,0 +1,22 @@
#pragma once
#include "include.h"
#include "_BlendMode.h"
#include "_WindowType.h"
#include "_SystemCursorType.h"
#include "_FontStyle.h"
#include "_FlipMode.h"
#include <vector>
#include "begin_code.h"
namespace _internal
{
BlendMode getBlendModeFromSDLBlendMode(SDL_BlendMode mode);
SDL_BlendMode getSDLBlendModeFromBlendMode(BlendMode mode);
/// FIXME: return SDL_WindowFlags or Uint32 ?
Uint32 getSDLWindowFlagsFromWindowType(WindowType type);
SystemCursorType getCursorTypeFromSDLSystemCursor(SDL_SystemCursor id);
SDL_SystemCursor getSDLSystemCursorFromSystemCursorType(SystemCursorType type);
int getTTFFontStyleFromFontStyle(FontStyle style);
std::vector<FontStyle> getFontStyleVecFromMixedTTFFontStyle(int Mixed_TTF_Font_Style);
SDL_RendererFlip getSDLRendererFlipFromFlipMode(FlipMode mode);
}/// End of namespace _internal
#include "end_code.h"

3
SDLWrapper/begin_code.h Normal file
View File

@ -0,0 +1,3 @@
/// Include this file at the beginning of MiniEngine headers and sources.
namespace MiniEngine
{

3
SDLWrapper/end_code.h Normal file
View File

@ -0,0 +1,3 @@
/// Include this file at the end of MiniEngine headers and sources.
} /// End of namespace MiniEngine
/// NOTICE: If you see an compile error here, there must be some unclosed "{" !

14
SDLWrapper/include.h Normal file
View File

@ -0,0 +1,14 @@
#pragma once
/// Include SDL Library Headers.
#include <SDL2/SDL.h>
#undef main
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_mixer.h>
/// Version Requiring Definition
#define _MINIENGINE_SDL_VERSION_ATLEAST(X,Y,Z) SDL_VERSION_ATLEAST(X,Y,Z)
/// Deprecated Definition
#define _DECL_DEPRECATED [[deprecated]]

13003
json/json.hpp Normal file

File diff suppressed because it is too large Load Diff