mirror of
https://github.com/Kiritow/MiniEngine.git
synced 2024-03-22 13:11:22 +08:00
commit
c422bbb57c
808
MiniEngine.cpp
808
MiniEngine.cpp
File diff suppressed because it is too large
Load Diff
364
MiniEngine.h
364
MiniEngine.h
|
@ -5,6 +5,8 @@
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#define _MINIENGINE_SDL_VERSION_ATLEAST(X,Y,Z) SDL_VERSION_ATLEAST(X,Y,Z)
|
||||||
|
|
||||||
namespace MiniEngine
|
namespace MiniEngine
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -18,9 +20,9 @@ namespace MiniEngine
|
||||||
SDL_Rect toSDLRect() const;
|
SDL_Rect toSDLRect() const;
|
||||||
bool isEmpty();
|
bool isEmpty();
|
||||||
bool operator == (const Rect&) const;
|
bool operator == (const Rect&) const;
|
||||||
bool hasIntersection(const Rect&);
|
bool hasIntersection(const Rect&) const;
|
||||||
Rect getIntersection(const Rect&);
|
Rect getIntersection(const Rect&) const;
|
||||||
Rect getUnion(const Rect&);
|
Rect getUnion(const Rect&) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Point
|
class Point
|
||||||
|
@ -29,8 +31,8 @@ namespace MiniEngine
|
||||||
int x, y;
|
int x, y;
|
||||||
Point(int X, int Y);
|
Point(int X, int Y);
|
||||||
Point();
|
Point();
|
||||||
SDL_Point toSDLPoint();
|
SDL_Point toSDLPoint() const;
|
||||||
bool inRect(Rect rect);
|
bool inRect(const Rect& rect) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
class ColorMode
|
class ColorMode
|
||||||
|
@ -48,8 +50,8 @@ namespace MiniEngine
|
||||||
RGBA(int R, int G, int B, int A);
|
RGBA(int R, int G, int B, int A);
|
||||||
RGBA(ColorMode mode, int A);
|
RGBA(ColorMode mode, int A);
|
||||||
RGBA();
|
RGBA();
|
||||||
SDL_Color toSDLColor();
|
SDL_Color toSDLColor() const;
|
||||||
ColorMode toColorMode();
|
ColorMode toColorMode() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
class NonCopyable
|
class NonCopyable
|
||||||
|
@ -65,7 +67,7 @@ namespace MiniEngine
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void fetch();
|
void fetch();
|
||||||
std::string getError();
|
std::string getError() const;
|
||||||
const char* what() const throw() override;
|
const char* what() const throw() override;
|
||||||
private:
|
private:
|
||||||
std::string str;
|
std::string str;
|
||||||
|
@ -84,9 +86,10 @@ namespace MiniEngine
|
||||||
void release();
|
void release();
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<SDL_RWops> _op;
|
std::shared_ptr<SDL_RWops> _op;
|
||||||
SDL_RWops* _get();
|
SDL_RWops* _get() const;
|
||||||
void _clear();
|
void _clear();
|
||||||
void _set(SDL_RWops*);
|
void _set(SDL_RWops*);
|
||||||
|
friend class Surface;
|
||||||
friend class Renderer;
|
friend class Renderer;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -95,48 +98,73 @@ namespace MiniEngine
|
||||||
class Surface
|
class Surface
|
||||||
{
|
{
|
||||||
public:
|
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;
|
~Surface() = default;
|
||||||
int savePNG(const std::string& filename);
|
|
||||||
int getw();
|
/// static functions
|
||||||
int geth();
|
static Surface load(const std::string& filename);
|
||||||
BlendMode getBlendMode();
|
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);
|
int setBlendMode(BlendMode mode);
|
||||||
|
|
||||||
int blit(Surface s,Rect src,Rect dst);
|
/// Rendering functions. Copy an external surface to this surface. So it has no constant attribute.
|
||||||
int blitTo(Surface t, Rect dst);
|
int blit(const Surface& s,const Rect& src,const Rect& dst);
|
||||||
int blitTo(Surface t, Point lupoint);
|
int blitTo(const Surface& t, const Rect& dst);
|
||||||
int blitFill(Surface t, Rect src);
|
int blitTo(const Surface& t, const Point& lupoint);
|
||||||
int blitFullFill(Surface t);
|
int blitFill(const Surface& t, const Rect& src);
|
||||||
|
int blitFullFill(const Surface& t);
|
||||||
|
|
||||||
int blitScaled(Surface s,Rect src,Rect dst);
|
int blitScaled(const Surface& s,const Rect& src,const Rect& dst);
|
||||||
int blitScaledTo(Surface t, Rect dst);
|
int blitScaledTo(const Surface& t, const Rect& dst);
|
||||||
int blitScaledTo(Surface t, Point lupoint);
|
int blitScaledTo(const Surface& t, const Point& lupoint);
|
||||||
int blitScaledFill(Surface t, Rect src);
|
int blitScaledFill(const Surface& t, const Rect& src);
|
||||||
int blitScaledFullFill(Surface t);
|
int blitScaledFullFill(const Surface& t);
|
||||||
|
|
||||||
|
int setClipRect(const Rect& clipRect);
|
||||||
|
Rect getClipRect() const;
|
||||||
|
void disableClipping();
|
||||||
|
|
||||||
int setAlphaMode(int alpha);
|
int setAlphaMode(int alpha);
|
||||||
int getAlphaMode();
|
int getAlphaMode() const;
|
||||||
|
|
||||||
ColorMode getColorMode();
|
ColorMode getColorMode() const;
|
||||||
int setColorMode(ColorMode mode);
|
int setColorMode(ColorMode mode);
|
||||||
RGBA getRGBA();
|
RGBA getRGBA() const;
|
||||||
void setRGBA(RGBA pack);
|
void setRGBA(const RGBA& pack);
|
||||||
|
|
||||||
bool mustlock();
|
bool mustlock() const;
|
||||||
int lock();
|
int lock();
|
||||||
void unlock();
|
void unlock();
|
||||||
|
|
||||||
static Surface createSurface(int width,int height,int depth,int Rmask,int Gmask,int Bmask,int Amask) throw(ErrorViewer);
|
bool isReady() const;
|
||||||
|
|
||||||
void release();
|
void release();
|
||||||
protected:
|
|
||||||
Surface() = default;
|
/// 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:
|
private:
|
||||||
std::shared_ptr<SDL_Surface> _surf;
|
std::shared_ptr<SDL_Surface> _surf;
|
||||||
void _set(SDL_Surface*);
|
void _set(SDL_Surface*);
|
||||||
|
void _set_no_delete(SDL_Surface*);
|
||||||
void _clear();
|
void _clear();
|
||||||
SDL_Surface* _get();
|
SDL_Surface* _get() const;
|
||||||
std::shared_ptr<SDL_Surface>& _getex();
|
|
||||||
|
|
||||||
friend class Window;
|
friend class Window;
|
||||||
friend class Renderer;
|
friend class Renderer;
|
||||||
|
@ -150,19 +178,19 @@ namespace MiniEngine
|
||||||
Texture();
|
Texture();
|
||||||
~Texture() = default;
|
~Texture() = default;
|
||||||
Rect getSize();
|
Rect getSize();
|
||||||
int getw();
|
int getw() const;
|
||||||
int geth();
|
int geth() const;
|
||||||
bool isReady();
|
bool isReady() const;
|
||||||
int setBlendMode(BlendMode mode);
|
int setBlendMode(BlendMode mode);
|
||||||
BlendMode getBlendMode();
|
BlendMode getBlendMode() const;
|
||||||
/// Alpha: 0: Transparent 255: opaque
|
/// Alpha: 0: Transparent 255: opaque
|
||||||
int setAlphaMode(int alpha);
|
int setAlphaMode(int alpha);
|
||||||
int getAlphaMode();
|
int getAlphaMode() const;
|
||||||
|
|
||||||
ColorMode getColorMode();
|
ColorMode getColorMode() const;
|
||||||
int setColorMode(ColorMode mode);
|
int setColorMode(ColorMode mode);
|
||||||
RGBA getRGBA();
|
RGBA getRGBA() const;
|
||||||
void setRGBA(RGBA pack);
|
void setRGBA(const RGBA& pack);
|
||||||
|
|
||||||
void release();
|
void release();
|
||||||
protected:
|
protected:
|
||||||
|
@ -172,7 +200,7 @@ namespace MiniEngine
|
||||||
std::shared_ptr<SDL_Texture> _text;
|
std::shared_ptr<SDL_Texture> _text;
|
||||||
void _set(SDL_Texture*);
|
void _set(SDL_Texture*);
|
||||||
void _clear();
|
void _clear();
|
||||||
SDL_Texture* _get();
|
SDL_Texture* _get() const;
|
||||||
Rect rect;
|
Rect rect;
|
||||||
friend class Renderer;
|
friend class Renderer;
|
||||||
};
|
};
|
||||||
|
@ -185,44 +213,45 @@ namespace MiniEngine
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Renderer() = default;
|
Renderer() = default;
|
||||||
int setColor(RGBA pack);
|
int setColor(const RGBA& pack);
|
||||||
RGBA getColor();
|
RGBA getColor() const;
|
||||||
int setBlendMode(BlendMode mode);
|
int setBlendMode(BlendMode mode);
|
||||||
BlendMode getBlendMode();
|
BlendMode getBlendMode() const;
|
||||||
|
|
||||||
int setTarget(Texture& t);
|
int setTarget(Texture& t);
|
||||||
int setTarget();
|
int setTarget();
|
||||||
|
|
||||||
int fillRect(Rect rect);
|
int fillRect(const Rect& rect);
|
||||||
int drawRect(Rect rect);
|
int drawRect(const Rect& rect);
|
||||||
int drawPoint(Point p);
|
int drawPoint(const Point& p);
|
||||||
|
|
||||||
int clear();
|
int clear();
|
||||||
void update();
|
void update();
|
||||||
|
|
||||||
int copy(Texture t, Rect src, Rect dst);
|
int copy(const Texture& t, const Rect& src, const Rect& dst);
|
||||||
int copyTo(Texture t, Rect dst);
|
int copyTo(const Texture& t, const Rect& dst);
|
||||||
int copyTo(Texture t, Point lupoint);
|
int copyTo(const Texture& t, const Point& lupoint);
|
||||||
int copyFill(Texture t, Rect src);
|
int copyFill(const Texture& t, const Rect& src);
|
||||||
int copyFullFill(Texture t);
|
int copyFullFill(const Texture& t);
|
||||||
|
|
||||||
int supercopy(Texture t,bool srcfull,Rect src,bool dstfull,Rect dst,double angle,bool haspoint,Point center,FlipMode mode);
|
int supercopy(const Texture& t,
|
||||||
|
bool srcfull,const Rect& src,bool dstfull,const Rect& dst,
|
||||||
|
double angle,
|
||||||
|
bool haspoint,const Point& center,FlipMode mode);
|
||||||
|
|
||||||
Surface loadSurface(std::string FileName) throw(ErrorViewer);
|
Texture render(const Surface& surf) const throw (ErrorViewer);
|
||||||
Surface loadSurfaceRW(RWOP rwop) throw(ErrorViewer);
|
Texture loadTexture(const std::string& FileName) const throw(ErrorViewer);
|
||||||
Texture render(Surface surf) throw (ErrorViewer);
|
Texture loadTextureRW(const RWOP& rwop) const throw(ErrorViewer);
|
||||||
Texture loadTexture(std::string FileName) throw(ErrorViewer);
|
Texture createTexture(int Width, int Height) const throw(ErrorViewer);
|
||||||
Texture loadTextureRW(RWOP rwop) throw(ErrorViewer);
|
|
||||||
Texture createTexture(int Width, int Height) throw(ErrorViewer);
|
|
||||||
|
|
||||||
bool isReady();
|
bool isReady() const;
|
||||||
|
|
||||||
void release();
|
void release();
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<SDL_Renderer> _rnd;
|
std::shared_ptr<SDL_Renderer> _rnd;
|
||||||
void _set(SDL_Renderer*);
|
void _set(SDL_Renderer*);
|
||||||
void _clear();
|
void _clear();
|
||||||
SDL_Renderer* _get();
|
SDL_Renderer* _get() const;
|
||||||
|
|
||||||
friend class Window;
|
friend class Window;
|
||||||
};
|
};
|
||||||
|
@ -238,13 +267,14 @@ namespace MiniEngine
|
||||||
class Cursor
|
class Cursor
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static Cursor CreateSystemCursor(SystemCursorType);
|
Cursor()=default;
|
||||||
static Cursor CreateCursor(Surface surf,Point hotspot={0,0});
|
Cursor(SystemCursorType);
|
||||||
|
Cursor(Surface surf,Point hotspot={0,0});
|
||||||
|
|
||||||
static Cursor GetActiveCursor();
|
static Cursor GetActiveCursor();
|
||||||
static Cursor GetDefaultCursor();
|
static Cursor GetDefaultCursor();
|
||||||
|
|
||||||
static void show(bool);
|
static void setShow(bool);
|
||||||
static bool isShow();
|
static bool isShow();
|
||||||
|
|
||||||
void activate();
|
void activate();
|
||||||
|
@ -273,6 +303,7 @@ namespace MiniEngine
|
||||||
class Window
|
class Window
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
Window()=default;
|
||||||
Window(std::string Title, int Width, int Height,
|
Window(std::string Title, int Width, int Height,
|
||||||
std::initializer_list<RendererType> RendererFlags = { RendererType::Accelerated,RendererType::TargetTexture },
|
std::initializer_list<RendererType> RendererFlags = { RendererType::Accelerated,RendererType::TargetTexture },
|
||||||
std::initializer_list<WindowType> WindowFlags = {WindowType::Shown} ,
|
std::initializer_list<WindowType> WindowFlags = {WindowType::Shown} ,
|
||||||
|
@ -281,39 +312,44 @@ namespace MiniEngine
|
||||||
|
|
||||||
void setRenderer(RendererType Type)
|
void setRenderer(RendererType Type)
|
||||||
{
|
{
|
||||||
_internal_rndflagcalc=0;
|
int flagcalc=0;
|
||||||
_setRenderer(Type);
|
_setRenderer(flagcalc,Type);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
void setRenderer(RendererType Type,Args&&... args)
|
void setRenderer(RendererType Type,Args&&... args)
|
||||||
{
|
{
|
||||||
_internal_rndflagcalc=0;
|
int flagcalc=0;
|
||||||
_setRenderer(Type,std::forward<RendererType>(args...));
|
_setRenderer(flagcalc,Type,std::forward<RendererType>(args...));
|
||||||
}
|
}
|
||||||
|
|
||||||
void setRenderer(std::initializer_list<RendererType>);
|
void setRenderer(std::initializer_list<RendererType>);
|
||||||
|
|
||||||
Rect getSize();
|
Rect getSize() const;
|
||||||
void setSize(Rect sizeRect);
|
void setSize(const Rect& sizeRect);
|
||||||
void setSize(int w, int h);
|
void setSize(int w, int h);
|
||||||
|
|
||||||
Rect getPosition();
|
Point getPosition() const;
|
||||||
void setPosition(int x, int y);
|
void setPosition(int x, int y);
|
||||||
/// FIXME: Use class Point or class Rect ?
|
void setPosition(const Point& point);
|
||||||
void setPosition(Point point);
|
|
||||||
|
|
||||||
|
void setTitle(const std::string& Title);
|
||||||
|
std::string getTitle() const;
|
||||||
|
|
||||||
void setTitle(std::string Title);
|
void setGrab(bool isGrab);
|
||||||
std::string getTitle();
|
bool getGrab() const;
|
||||||
|
|
||||||
void setGrab(bool);
|
#if _MINIENGINE_SDL_VERSION_ATLEAST(2,0,5)
|
||||||
bool getGrab();
|
/// SDL2.0.5 Required.
|
||||||
|
int setOpacity(float opacity);
|
||||||
|
float getOpacity() const;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/// FIXME: Not Implemented.
|
||||||
void setResizable(bool resizable);
|
void setResizable(bool resizable);
|
||||||
|
|
||||||
/// Use UTF8 in Title and Message please.
|
/// Use UTF8 in Title and Message please.
|
||||||
int showSimpleMessageBox(MessageBoxType type,std::string Title,std::string Message);
|
int showSimpleMessageBox(MessageBoxType type,const std::string& Title,const std::string& Message) const;
|
||||||
|
|
||||||
void show();
|
void show();
|
||||||
void hide();
|
void hide();
|
||||||
|
@ -324,48 +360,55 @@ namespace MiniEngine
|
||||||
|
|
||||||
_DECL_DEPRECATED Surface getSurface();
|
_DECL_DEPRECATED Surface getSurface();
|
||||||
|
|
||||||
|
bool isScreenKeyboardShown();
|
||||||
|
|
||||||
void release();
|
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:
|
protected:
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
void _setRenderer(RendererType Type,Args&&... args)
|
void _setRenderer(int& refcalc,RendererType Type,Args&&... args)
|
||||||
{
|
{
|
||||||
_internal_rndflagcalc|=_render_caster(Type);
|
refcalc|=_render_caster(Type);
|
||||||
_setRenderer(args...);
|
_setRenderer(refcalc,args...);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _setRenderer(RendererType Type)
|
void _setRenderer(int& refcalc,RendererType Type)
|
||||||
{
|
{
|
||||||
_internal_rndflagcalc|=_render_caster(Type);
|
refcalc|=_render_caster(Type);
|
||||||
_setRenderer_Real(_internal_rndflagcalc);
|
_setRenderer_Real(refcalc);
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
void _setRenderer_Real(Uint32 flags);
|
void _setRenderer_Real(Uint32 flags);
|
||||||
Uint32 _internal_rndflagcalc;
|
|
||||||
Uint32 _render_caster(RendererType);
|
Uint32 _render_caster(RendererType);
|
||||||
|
|
||||||
std::shared_ptr<SDL_Window> _wnd;
|
std::shared_ptr<SDL_Window> _wnd;
|
||||||
void _set(SDL_Window*);
|
void _set(SDL_Window*);
|
||||||
void _clear();
|
void _clear();
|
||||||
SDL_Window* _get();
|
SDL_Window* _get() const;
|
||||||
|
|
||||||
Renderer winrnd;
|
Renderer _winrnd;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum class FontStyle { Normal, Bold, Italic, UnderLine, StrikeThrough };
|
||||||
|
enum class FontHint { Normal, Light, Mono, None , Error };
|
||||||
|
|
||||||
class Font
|
class Font
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
enum class Style { Normal, Bold, Italic, UnderLine, StrikeThrough };
|
|
||||||
|
|
||||||
Font() = default;
|
Font() = default;
|
||||||
Font(std::string FontFileName, int size) throw(ErrorViewer);
|
Font(std::string FontFileName, size_t size) throw(ErrorViewer);
|
||||||
int use(std::string FontFileName, int size);
|
int use(std::string FontFileName, size_t size);
|
||||||
bool isReady();
|
bool isReady() const;
|
||||||
|
|
||||||
bool isNormal();
|
bool isNormal() const;
|
||||||
bool isBold();
|
bool isBold() const;
|
||||||
bool isItalic();
|
bool isItalic() const;
|
||||||
bool isUnderLine();
|
bool isUnderLine() const;
|
||||||
bool isStrikeThrough();
|
bool isStrikeThrough() const;
|
||||||
|
|
||||||
void setNormal();
|
void setNormal();
|
||||||
void setBold(bool);
|
void setBold(bool);
|
||||||
|
@ -374,65 +417,95 @@ namespace MiniEngine
|
||||||
void setStrikeThrough(bool);
|
void setStrikeThrough(bool);
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
void setFontStyle(Style style,Args&&... args)
|
void setFontStyle(FontStyle style,Args&&... args)
|
||||||
{
|
{
|
||||||
int fontcalc=0;
|
int fontcalc=0;
|
||||||
_setFontStyle(fontcalc,style,args...);
|
_setFontStyle(fontcalc,style,args...);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setFontStyle(Style style)
|
void setFontStyle(FontStyle style)
|
||||||
{
|
{
|
||||||
int fontcalc=0;
|
int fontcalc=0;
|
||||||
_setFontStyle(fontcalc,style);
|
_setFontStyle(fontcalc,style);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<Style> getFontStyles();
|
std::vector<FontStyle> getFontStyles() const;
|
||||||
|
|
||||||
Rect sizeText(const std::string& Text) throw (ErrorViewer);
|
int getFontHeight() const;
|
||||||
Rect sizeUTF8(const std::string& Text) throw (ErrorViewer);
|
int getFontAscent() const;
|
||||||
|
int getFontDescent() const;
|
||||||
|
int getFontLineSkip() const;
|
||||||
|
|
||||||
Surface renderText(std::string Text, RGBA fg);
|
bool isFontKerning() const;
|
||||||
Surface renderTextWrapped(std::string Text, RGBA fg, int WrapLength);
|
void setFontKerning(bool enableKerning);
|
||||||
Surface renderTextShaded(std::string Text, RGBA fg, RGBA bg);
|
|
||||||
Surface renderTextSolid(std::string Text, RGBA fg);
|
|
||||||
|
|
||||||
Surface renderUTF8(std::string Text, RGBA fg);
|
long getFontFaceNum() const;
|
||||||
Surface renderUTF8Wrapped(std::string Text, RGBA fg, int WrapLength);
|
int getFontFaceIsFixedWidth() const;
|
||||||
Surface renderUTF8Shaded(std::string Text, RGBA fg, RGBA bg);
|
std::string getFontFaceFamilyName() const;
|
||||||
Surface renderUTF8Solid(std::string Text, RGBA fg);
|
std::string getFontFaceStyleName() const;
|
||||||
|
|
||||||
Texture renderText(Renderer rnd, std::string Text, RGBA fg);
|
FontHint getFontHint() const;
|
||||||
Texture renderTextWrapped(Renderer rnd, std::string Text, RGBA fg, int WrapLength);
|
void setFontHint(FontHint hint);
|
||||||
Texture renderTextShaded(Renderer rnd, std::string Text, RGBA fg, RGBA bg);
|
|
||||||
Texture renderTextSolid(Renderer rnd, std::string Text, RGBA fg);
|
|
||||||
|
|
||||||
Texture renderUTF8(Renderer rnd, std::string Text, RGBA fg);
|
|
||||||
Texture renderUTF8Wrapped(Renderer rnd, std::string Text, RGBA fg, int WrapLength);
|
Rect sizeText(const std::string& Text) const throw (ErrorViewer);
|
||||||
Texture renderUTF8Shaded(Renderer rnd, std::string Text, RGBA fg, RGBA bg);
|
Rect sizeUTF8(const std::string& Text) const throw (ErrorViewer);
|
||||||
Texture renderUTF8Solid(Renderer rnd, std::string Text, RGBA fg);
|
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();
|
void release();
|
||||||
protected:
|
protected:
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
void _setFontStyle(int& fontcalc,Style style,Args&&... args)
|
void _setFontStyle(int& fontcalc,FontStyle style,Args&&... args)
|
||||||
{
|
{
|
||||||
fontcalc|=_style_caster(style);
|
fontcalc|=_style_caster(style);
|
||||||
_setFontStyle(fontcalc,args...);
|
_setFontStyle(fontcalc,args...);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _setFontStyle(int& fontcalc,Style style)
|
void _setFontStyle(int& fontcalc,FontStyle style)
|
||||||
{
|
{
|
||||||
fontcalc|=_style_caster(style);
|
fontcalc|=_style_caster(style);
|
||||||
_real_setFontStyle(fontcalc);
|
_real_setFontStyle(fontcalc);
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
void _real_setFontStyle(int);
|
void _real_setFontStyle(int);
|
||||||
int _style_caster(Style);
|
int _style_caster(FontStyle);
|
||||||
|
|
||||||
std::shared_ptr<TTF_Font> _font;
|
std::shared_ptr<TTF_Font> _font;
|
||||||
void _set(TTF_Font*);
|
void _set(TTF_Font*);
|
||||||
void _clear();
|
void _clear();
|
||||||
TTF_Font* _get();
|
TTF_Font* _get() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class Platform { Unknown,Windows,MacOS,Linux,iOS,Android };
|
enum class Platform { Unknown,Windows,MacOS,Linux,iOS,Android };
|
||||||
|
@ -453,13 +526,20 @@ namespace MiniEngine
|
||||||
public:
|
public:
|
||||||
SharedLibrary();
|
SharedLibrary();
|
||||||
SharedLibrary(const std::string& Filename);
|
SharedLibrary(const std::string& Filename);
|
||||||
~SharedLibrary();
|
~SharedLibrary()=default;
|
||||||
int load(const std::string& Filename);
|
int load(const std::string& Filename);
|
||||||
int unload();
|
int unload();
|
||||||
void* get(const std::string& FunctionName);
|
|
||||||
|
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();
|
void release();
|
||||||
private:
|
private:
|
||||||
void* _get();
|
void* _get() const;
|
||||||
void _set(void*);
|
void _set(void*);
|
||||||
void _clear();
|
void _clear();
|
||||||
std::shared_ptr<void> _obj;
|
std::shared_ptr<void> _obj;
|
||||||
|
@ -489,8 +569,14 @@ namespace MiniEngine
|
||||||
static Platform GetPlatform();
|
static Platform GetPlatform();
|
||||||
|
|
||||||
static void StartTextInput();
|
static void StartTextInput();
|
||||||
|
static bool IsTextInputActive();
|
||||||
static void StopTextInput();
|
static void StopTextInput();
|
||||||
|
|
||||||
|
static bool HasScreenKeyboardSupport();
|
||||||
|
|
||||||
|
static std::tuple<int,int,int> getCompileVersion();
|
||||||
|
static std::tuple<int,int,int> getLinkedVersion();
|
||||||
|
|
||||||
class Android
|
class Android
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -533,7 +619,7 @@ namespace MiniEngine
|
||||||
|
|
||||||
int enable();
|
int enable();
|
||||||
int disable();
|
int disable();
|
||||||
bool isenable();
|
bool isenable() const;
|
||||||
void detach();
|
void detach();
|
||||||
~Timer();
|
~Timer();
|
||||||
|
|
||||||
|
@ -587,6 +673,9 @@ namespace MiniEngine
|
||||||
class MusicPlayer : public AudioPlayer
|
class MusicPlayer : public AudioPlayer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
static int GetDecoderNum();
|
||||||
|
static std::string GetDecoderName(int index);
|
||||||
|
|
||||||
Music loadMusic(std::string Filename) throw (ErrorViewer);
|
Music loadMusic(std::string Filename) throw (ErrorViewer);
|
||||||
|
|
||||||
int play(Music music, int loops);
|
int play(Music music, int loops);
|
||||||
|
@ -601,6 +690,9 @@ namespace MiniEngine
|
||||||
bool isPaused();
|
bool isPaused();
|
||||||
int isFading();
|
int isFading();
|
||||||
|
|
||||||
|
/// Experimental
|
||||||
|
static int SetMusicPosition(double position);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Music m;
|
Music m;
|
||||||
};
|
};
|
||||||
|
@ -623,6 +715,9 @@ namespace MiniEngine
|
||||||
class SoundPlayer : public AudioPlayer
|
class SoundPlayer : public AudioPlayer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
static int GetDecoderNum();
|
||||||
|
static std::string GetDecoderName(int index);
|
||||||
|
|
||||||
SoundPlayer(int Channels = 16);
|
SoundPlayer(int Channels = 16);
|
||||||
Sound loadSound(std::string Filename) throw (ErrorViewer);
|
Sound loadSound(std::string Filename) throw (ErrorViewer);
|
||||||
ChannelID playSound(Sound sound, int loops) throw (ErrorViewer);
|
ChannelID playSound(Sound sound, int loops) throw (ErrorViewer);
|
||||||
|
@ -631,6 +726,12 @@ namespace MiniEngine
|
||||||
void pause(ChannelID id);
|
void pause(ChannelID id);
|
||||||
void resume(ChannelID id);
|
void resume(ChannelID id);
|
||||||
int stop(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
|
class StringEngine
|
||||||
|
@ -650,6 +751,13 @@ namespace MiniEngine
|
||||||
impl* pimpl;
|
impl* pimpl;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
int SetClipboardText(const std::string& str);
|
||||||
|
std::string GetClipboardText();
|
||||||
|
bool HasClipboardText();
|
||||||
|
|
||||||
|
/// Experimental - For Experts: Use SDL ScanCode
|
||||||
|
bool GetScanKeyState(SDL_Scancode);
|
||||||
|
|
||||||
}/// End of namespace MiniEngine
|
}/// End of namespace MiniEngine
|
||||||
|
|
||||||
std::string UTF8ToGBK(std::string UTF8String);
|
std::string UTF8ToGBK(std::string UTF8String);
|
||||||
|
|
|
@ -7,12 +7,12 @@ namespace MiniEngine
|
||||||
#if defined(__ANDROID__) && __ANDROID__
|
#if defined(__ANDROID__) && __ANDROID__
|
||||||
std::string SDLSystem::Android::GetInternal()
|
std::string SDLSystem::Android::GetInternal()
|
||||||
{
|
{
|
||||||
return string(SDL_AndroidGetInternalStoragePath());
|
return std::string(SDL_AndroidGetInternalStoragePath());
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string SDLSystem::Android::GetExternal()
|
std::string SDLSystem::Android::GetExternal()
|
||||||
{
|
{
|
||||||
return string(SDL_AndroidGetExternalStoragePath());
|
return std::string(SDL_AndroidGetExternalStoragePath());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SDLSystem::Android::CanReadExternal()
|
bool SDLSystem::Android::CanReadExternal()
|
||||||
|
|
|
@ -35,6 +35,16 @@ bool HasEvent(_SDLEventType_ EventTypeMin,_SDLEventType_ EventTypeMax)
|
||||||
return ( SDL_HasEvents(EventTypeMin,EventTypeMax)==SDL_TRUE );
|
return ( SDL_HasEvents(EventTypeMin,EventTypeMax)==SDL_TRUE );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_SDLEventType_ RegisterEvent(int howMuch)
|
||||||
|
{
|
||||||
|
return SDL_RegisterEvents(howMuch);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsValidEventType(_SDLEventType_ EventType)
|
||||||
|
{
|
||||||
|
return (EventType > SDL_FIRSTEVENT) && (EventType < SDL_LASTEVENT);
|
||||||
|
}
|
||||||
|
|
||||||
bool operator == (const LooperID& a,const LooperID& b)
|
bool operator == (const LooperID& a,const LooperID& b)
|
||||||
{
|
{
|
||||||
return a._type_id==b._type_id && a._looper_cnt==b._looper_cnt ;
|
return a._type_id==b._type_id && a._looper_cnt==b._looper_cnt ;
|
||||||
|
|
|
@ -17,6 +17,8 @@ bool HasEvent(_SDLEventType_ EventTypeMin,_SDLEventType_ EventTypeMax);
|
||||||
bool EnableEvent(_SDLEventType_ EventType);
|
bool EnableEvent(_SDLEventType_ EventType);
|
||||||
bool DisableEvent(_SDLEventType_ EventType);
|
bool DisableEvent(_SDLEventType_ EventType);
|
||||||
bool IsEventEnabled(_SDLEventType_ EventType);
|
bool IsEventEnabled(_SDLEventType_ EventType);
|
||||||
|
_SDLEventType_ RegisterEvent(int howMuch);
|
||||||
|
bool IsValidEventType(_SDLEventType_ EventType);
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#include "sqlite/sqlite3.h"
|
#include "sqlite/sqlite3.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
namespace MiniEngine
|
namespace MiniEngine
|
||||||
{
|
{
|
||||||
|
|
100
MiniEngine_Test.cpp
Normal file
100
MiniEngine_Test.cpp
Normal file
|
@ -0,0 +1,100 @@
|
||||||
|
#include "MiniEngine_Test.h"
|
||||||
|
#include <SDL2/SDL_test.h>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
namespace MiniEngine
|
||||||
|
{
|
||||||
|
|
||||||
|
namespace Test
|
||||||
|
{
|
||||||
|
|
||||||
|
void GetMD5Raw(unsigned char* buffer,unsigned int bufferLen,unsigned char* outbuff)
|
||||||
|
{
|
||||||
|
SDLTest_Md5Context ct;
|
||||||
|
SDLTest_Md5Init(&ct);
|
||||||
|
SDLTest_Md5Update(&ct,buffer,bufferLen);
|
||||||
|
SDLTest_Md5Final(&ct);
|
||||||
|
memcpy(outbuff,ct.digest,16);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string GetMD5(unsigned char* buffer,unsigned int bufferLen)
|
||||||
|
{
|
||||||
|
unsigned char buff[16];
|
||||||
|
char tmp[8];
|
||||||
|
GetMD5Raw(buffer,bufferLen,buff);
|
||||||
|
std::string str;
|
||||||
|
for(int i=0;i<16;i++)
|
||||||
|
{
|
||||||
|
sprintf(tmp,"%02x",buff[i]);
|
||||||
|
str.append(tmp);
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Notice: SDLTest_crc32Calc is an undefined symbol. So we must use these 3 functions.
|
||||||
|
int GetCRC32(unsigned char* buffer,unsigned int bufferLen,uint32_t& out_CRCResult)
|
||||||
|
{
|
||||||
|
uint32_t result;
|
||||||
|
SDLTest_Crc32Context ct;
|
||||||
|
SDLTest_Crc32Init(&ct);
|
||||||
|
|
||||||
|
int ret=-2;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
if (SDLTest_Crc32CalcStart(&ct,&result))
|
||||||
|
{
|
||||||
|
ret=-1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (SDLTest_Crc32CalcBuffer(&ct, buffer, bufferLen, &result))
|
||||||
|
{
|
||||||
|
ret=-1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (SDLTest_Crc32CalcEnd(&ct, &result))
|
||||||
|
{
|
||||||
|
ret=-1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
ret=0;
|
||||||
|
out_CRCResult=result;
|
||||||
|
break;
|
||||||
|
}while(0);
|
||||||
|
|
||||||
|
SDLTest_Crc32Done(&ct);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Compare two surfaces. Currently, Surface::getRawPointer() does not has constant attribute.
|
||||||
|
int CompareSurface(Surface& surface1, Surface& surface2, int allowableError)
|
||||||
|
{
|
||||||
|
return SDLTest_CompareSurfaces(surface1.getRawPointer(),surface2.getRawPointer(),allowableError);
|
||||||
|
}
|
||||||
|
|
||||||
|
//private
|
||||||
|
struct UniRandom::_impl
|
||||||
|
{
|
||||||
|
SDLTest_RandomContext context;
|
||||||
|
};
|
||||||
|
|
||||||
|
UniRandom::UniRandom()
|
||||||
|
{
|
||||||
|
_sp.reset(new _impl);
|
||||||
|
SDLTest_RandomInitTime(&(_sp.get()->context));
|
||||||
|
}
|
||||||
|
|
||||||
|
UniRandom::UniRandom(unsigned int A, unsigned int B)
|
||||||
|
{
|
||||||
|
_sp.reset(new _impl);
|
||||||
|
SDLTest_RandomInit(&(_sp.get()->context),A,B);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t UniRandom::get()
|
||||||
|
{
|
||||||
|
return SDLTest_Random(&(_sp.get()->context));
|
||||||
|
}
|
||||||
|
|
||||||
|
}/// End of namespace MiniEngine::Test
|
||||||
|
|
||||||
|
}/// End of namespace MiniEngine
|
36
MiniEngine_Test.h
Normal file
36
MiniEngine_Test.h
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
#pragma once
|
||||||
|
#include "MiniEngine.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace MiniEngine
|
||||||
|
{
|
||||||
|
|
||||||
|
namespace Test
|
||||||
|
{
|
||||||
|
|
||||||
|
std::string GetMD5(unsigned char* buffer,unsigned int bufferLen);
|
||||||
|
void GetMD5Raw(unsigned char* buffer,unsigned int bufferLen,unsigned char* outbuff);
|
||||||
|
|
||||||
|
int GetCRC32(unsigned char* buffer,unsigned int bufferLen,uint32_t& out_CRCResult);
|
||||||
|
|
||||||
|
int CompareSurface(const Surface& surface1,const Surface& surface2,int allowableError);
|
||||||
|
|
||||||
|
class UniRandom
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/// Default Constructor is based on system current time.
|
||||||
|
UniRandom();
|
||||||
|
UniRandom(unsigned int A,unsigned int B);
|
||||||
|
uint32_t get();
|
||||||
|
private:
|
||||||
|
struct _impl;
|
||||||
|
std::shared_ptr<_impl> _sp;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Not Implied : SDLTest_RandomAsciiString,SDLTest_RandomAsciiStringOfSize cause link error.
|
||||||
|
std::string GetRandomString();
|
||||||
|
std::string GetRandomString(size_t length);
|
||||||
|
|
||||||
|
}/// End of namespace MiniEngine::Test
|
||||||
|
|
||||||
|
}/// End of namespace MiniEngine
|
|
@ -3,118 +3,83 @@ using namespace std;
|
||||||
|
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
|
||||||
//GBK编码转换到UTF8编码
|
int _utf8_to_gb(const char* src, char* dst, int len)
|
||||||
int _GBKToUTF8(unsigned char * lpGBKStr,unsigned char * lpUTF8Str,int nUTF8StrLen)
|
|
||||||
{
|
{
|
||||||
wchar_t * lpUnicodeStr = NULL;
|
int ret = 0;
|
||||||
int nRetLen = 0;
|
WCHAR* strA;
|
||||||
|
int i= MultiByteToWideChar(CP_UTF8, 0, src, -1, NULL, 0);
|
||||||
if(!lpGBKStr) //如果GBK字符串为NULL则出错退出
|
if (i <= 0) {
|
||||||
return 0;
|
return -1;
|
||||||
|
|
||||||
nRetLen = MultiByteToWideChar(CP_ACP,0,(char *)lpGBKStr,-1,NULL,0); //获取转换到Unicode编码后所需要的字符空间长度
|
|
||||||
lpUnicodeStr = new WCHAR[nRetLen + 1]; //为Unicode字符串空间
|
|
||||||
nRetLen = ::MultiByteToWideChar(CP_ACP,0,(char *)lpGBKStr,-1,lpUnicodeStr,nRetLen); //转换到Unicode编码
|
|
||||||
if(!nRetLen) //转换失败则出错退出
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
nRetLen = ::WideCharToMultiByte(CP_UTF8,0,lpUnicodeStr,-1,NULL,0,NULL,0); //获取转换到UTF8编码后所需要的字符空间长度
|
|
||||||
|
|
||||||
if(!lpUTF8Str) //输出缓冲区为空则返回转换后需要的空间大小
|
|
||||||
{
|
|
||||||
if(lpUnicodeStr)
|
|
||||||
delete []lpUnicodeStr;
|
|
||||||
return nRetLen;
|
|
||||||
}
|
}
|
||||||
|
strA = (WCHAR*)malloc(i * 2);
|
||||||
if(nUTF8StrLen < nRetLen) //如果输出缓冲区长度不够则退出
|
MultiByteToWideChar(CP_UTF8, 0, src, -1, strA, i);
|
||||||
{
|
i = WideCharToMultiByte(CP_ACP, 0, strA, -1, NULL, 0, NULL, NULL);
|
||||||
if(lpUnicodeStr)
|
if (len >= i) {
|
||||||
delete []lpUnicodeStr;
|
ret = WideCharToMultiByte(CP_ACP, 0, strA, -1, dst, i, NULL, NULL);
|
||||||
|
dst[i] = 0;
|
||||||
|
}
|
||||||
|
if (ret <= 0) {
|
||||||
|
free(strA);
|
||||||
|
return -2;
|
||||||
|
}
|
||||||
|
free( strA );
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
nRetLen = ::WideCharToMultiByte(CP_UTF8,0,lpUnicodeStr,-1,(char *)lpUTF8Str,nUTF8StrLen,NULL,NULL); //转换到UTF8编码
|
int _gb_to_utf8(const char* src, char* dst, int len)
|
||||||
|
{
|
||||||
if(lpUnicodeStr)
|
int ret = 0;
|
||||||
delete []lpUnicodeStr;
|
WCHAR* strA;
|
||||||
|
int i= MultiByteToWideChar(CP_ACP, 0, src, -1, NULL, 0);
|
||||||
return nRetLen;
|
if (i <= 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
strA = (WCHAR*)malloc(i * 2);
|
||||||
|
MultiByteToWideChar(CP_ACP, 0, src, -1, strA, i);
|
||||||
|
i = WideCharToMultiByte(CP_UTF8, 0, strA, -1, NULL, 0, NULL, NULL);
|
||||||
|
if (len >= i) {
|
||||||
|
ret = WideCharToMultiByte(CP_UTF8, 0, strA, -1, dst, i, NULL, NULL);
|
||||||
|
dst[i] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// UTF8编码转换到GBK编码
|
if (ret <= 0) {
|
||||||
int _UTF8ToGBK(unsigned char * lpUTF8Str,unsigned char * lpGBKStr,int nGBKStrLen)
|
free(strA);
|
||||||
{
|
return -2;
|
||||||
wchar_t * lpUnicodeStr = NULL;
|
|
||||||
int nRetLen = 0;
|
|
||||||
|
|
||||||
if(!lpUTF8Str) //如果UTF8字符串为NULL则出错退出
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
nRetLen = ::MultiByteToWideChar(CP_UTF8,0,(char *)lpUTF8Str,-1,NULL,0); //获取转换到Unicode编码后所需要的字符空间长度
|
|
||||||
lpUnicodeStr = new WCHAR[nRetLen + 1]; //为Unicode字符串空间
|
|
||||||
nRetLen = ::MultiByteToWideChar(CP_UTF8,0,(char *)lpUTF8Str,-1,lpUnicodeStr,nRetLen); //转换到Unicode编码
|
|
||||||
if(!nRetLen) //转换失败则出错退出
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
nRetLen = ::WideCharToMultiByte(CP_ACP,0,lpUnicodeStr,-1,NULL,0,NULL,NULL); //获取转换到GBK编码后所需要的字符空间长度
|
|
||||||
|
|
||||||
if(!lpGBKStr) //输出缓冲区为空则返回转换后需要的空间大小
|
|
||||||
{
|
|
||||||
if(lpUnicodeStr)
|
|
||||||
delete []lpUnicodeStr;
|
|
||||||
return nRetLen;
|
|
||||||
}
|
}
|
||||||
|
free(strA);
|
||||||
if(nGBKStrLen < nRetLen) //如果输出缓冲区长度不够则退出
|
|
||||||
{
|
|
||||||
if(lpUnicodeStr)
|
|
||||||
delete []lpUnicodeStr;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
nRetLen = ::WideCharToMultiByte(CP_ACP,0,lpUnicodeStr,-1,(char *)lpGBKStr,nRetLen,NULL,NULL); //转换到GBK编码
|
|
||||||
|
|
||||||
if(lpUnicodeStr)
|
|
||||||
delete []lpUnicodeStr;
|
|
||||||
|
|
||||||
return nRetLen;
|
|
||||||
}
|
|
||||||
|
|
||||||
string UTF8ToGBK(string UTF8String)
|
string UTF8ToGBK(string UTF8String)
|
||||||
{
|
{
|
||||||
int sz=UTF8String.size()*2/3+256;
|
int sz=UTF8String.size()*2/3+256;
|
||||||
auto utf8str=new unsigned char[sz];
|
auto gbkstr=new char[sz];
|
||||||
memset(utf8str,0,sz);
|
|
||||||
memcpy(utf8str,UTF8String.c_str(),UTF8String.size());
|
|
||||||
|
|
||||||
auto gbkstr=new unsigned char[sz];
|
|
||||||
memset(gbkstr,0,sz);
|
memset(gbkstr,0,sz);
|
||||||
|
|
||||||
_UTF8ToGBK(utf8str,gbkstr,sz);
|
if(_utf8_to_gb(UTF8String.c_str(),gbkstr,sz)!=0)
|
||||||
|
{
|
||||||
|
return "[MiniEngine] UT8ToGBK Convert Failed";
|
||||||
|
}
|
||||||
|
|
||||||
string s((char*)gbkstr);
|
string s((char*)gbkstr);
|
||||||
|
|
||||||
delete[] gbkstr;
|
delete[] gbkstr;
|
||||||
delete[] utf8str;
|
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
string GBKToUTF8(string GBKString)
|
string GBKToUTF8(string GBKString)
|
||||||
{
|
{
|
||||||
int sz=GBKString.size()*3/2+32;
|
int sz=GBKString.size()*3/2+32;
|
||||||
auto gbkstr=new unsigned char[sz];
|
auto utf8str=new char[sz];
|
||||||
memset(gbkstr,0,sz);
|
|
||||||
memcpy(gbkstr,GBKString.c_str(),GBKString.size());
|
|
||||||
|
|
||||||
auto utf8str=new unsigned char[sz];
|
|
||||||
memset(utf8str,0,sz);
|
memset(utf8str,0,sz);
|
||||||
|
|
||||||
_GBKToUTF8(gbkstr,utf8str,sz);
|
if(_gb_to_utf8(GBKString.c_str(),utf8str,sz)!=0)
|
||||||
|
{
|
||||||
|
return "[MiniEngine] GBKToUTF8 Convert Failed";
|
||||||
|
}
|
||||||
|
|
||||||
string s((char*)utf8str);
|
string s(utf8str);
|
||||||
|
|
||||||
delete[] gbkstr;
|
|
||||||
delete[] utf8str;
|
delete[] utf8str;
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user