diff --git a/MiniEngine.cpp b/MiniEngine.cpp index 8303ab8..63c4618 100644 --- a/MiniEngine.cpp +++ b/MiniEngine.cpp @@ -50,6 +50,72 @@ namespace MiniEngine return SDL_BLENDMODE_NONE; } } + + 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; + } + } }/// End of namespace _internal Rect::Rect(int X, int Y, int W, int H) @@ -98,7 +164,7 @@ namespace MiniEngine { auto p = toSDLPoint(); auto r = rect.toSDLRect(); - return SDL_PointInRect(&p, &r); + return ( SDL_PointInRect(&p, &r) == SDL_TRUE ); } ColorMode::ColorMode(int R, int G, int B) @@ -735,6 +801,74 @@ namespace MiniEngine return t; } + //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(); + } + + //static + Cursor Cursor::CreateCursor(Surface surf,Point hotspot) + { + Cursor ns; + SDL_Cursor* cursor=SDL_CreateColorCursor(surf._get(),hotspot.x,hotspot.y); + ns._set(cursor); + return ns; + } + + //static + Cursor Cursor::CreateSystemCursor(SystemCursorType type) + { + Cursor ns; + ns._set(SDL_CreateSystemCursor(_internal::getSDLSystemCursorFromSystemCursorType(type))); + return ns; + } + + //static + Cursor Cursor::GetActiveCursor() + { + Cursor ns; + ns._set(SDL_GetCursor()); + return ns; + } + + //static + Cursor Cursor::GetDefaultCursor() + { + Cursor ns; + ns._set(SDL_GetDefaultCursor()); + return ns; + } + + //static + bool Cursor::isShow() + { + return (SDL_ShowCursor(SDL_QUERY)==SDL_ENABLE); + } + + //static + void Cursor::show(bool Settings) + { + SDL_ShowCursor(Settings?SDL_ENABLE:SDL_DISABLE); + } + + void Cursor::activate() + { + SDL_SetCursor(_get()); + } + bool Renderer::isReady() { return (_get() != nullptr); @@ -829,6 +963,16 @@ namespace MiniEngine return std::string(SDL_GetWindowTitle(_get())); } + void Window::setGrab(bool isGrab) + { + SDL_SetWindowGrab(_get(),isGrab?SDL_TRUE:SDL_FALSE); + } + + bool Window::getGrab() + { + return (SDL_GetWindowGrab(_get())==SDL_TRUE)?true:false; + } + void Window::setResizable(bool resizable) { //SDL_SetWindowResizable(_get(), resizable?SDL_TRUE:SDL_FALSE); @@ -1438,12 +1582,12 @@ namespace MiniEngine bool MusicPlayer::isPlaying() { - return Mix_PlayingMusic(); + return (Mix_PlayingMusic() == 1); } bool MusicPlayer::isPaused() { - return Mix_PausedMusic(); + return (Mix_PausedMusic() == 1); } int MusicPlayer::isFading() diff --git a/MiniEngine.h b/MiniEngine.h index da5154a..6412c3b 100644 --- a/MiniEngine.h +++ b/MiniEngine.h @@ -7,6 +7,9 @@ #include #include #include + +/// VC++ does not implied C++ exception. Use this to ignore compile warning on this. +#pragma warning (disable:4290) #else /// CodeBlocks (MinGW Compiler) #include @@ -20,8 +23,7 @@ #include #include -#define _DECL_DEPRECATED __declspec(deprecated) -#define _DECL_DEPRECATED_MSG(InfoString) __declspec(deprecated(InfoString)) +#define _DECL_DEPRECATED [[deprecated]] namespace MiniEngine { @@ -150,6 +152,7 @@ namespace MiniEngine friend class Window; friend class Renderer; friend class Font; + friend class Cursor; }; class Texture @@ -232,6 +235,35 @@ namespace MiniEngine friend class Window; }; + enum class SystemCursorType + { + Arrow, Ibeam, CrossHair, + Wait, WaitArrow, + SizeNWSE, SizeNESW, SizeWE, SizeNS, SizeAll, + No, Hand + }; + + class Cursor + { + public: + static Cursor CreateSystemCursor(SystemCursorType); + static Cursor CreateCursor(Surface surf,Point hotspot={0,0}); + + static Cursor GetActiveCursor(); + static Cursor GetDefaultCursor(); + + static void show(bool); + static bool isShow(); + + void activate(); + private: + std::shared_ptr _cur; + void _set(SDL_Cursor*); + void _set_no_delete(SDL_Cursor*); + SDL_Cursor* _get(); + void _clear(); + }; + enum class MessageBoxType { Error, Warning, Information }; class Window @@ -268,6 +300,9 @@ namespace MiniEngine void setTitle(std::string Title); std::string getTitle(); + void setGrab(bool); + bool getGrab(); + void setResizable(bool resizable); /// Use UTF8 in Title and Message please. diff --git a/MiniEngine_Event.cpp b/MiniEngine_Event.cpp index d129e87..6f39595 100644 --- a/MiniEngine_Event.cpp +++ b/MiniEngine_Event.cpp @@ -1 +1,251 @@ #include "MiniEngine_Event.h" + +int PollEvent(Event& refEvent) +{ + return SDL_PollEvent(&refEvent); +} + +int WaitEvent(Event& refEvent) +{ + return SDL_WaitEvent(&refEvent); +} + +int WaitEventTimeout(Event& refEvent,int ms) +{ + return SDL_WaitEventTimeout(&refEvent,ms); +} + +int PushEvent(const Event& refEvent) +{ + return SDL_PushEvent(const_cast(&refEvent)); +} + +bool operator == (const LooperID& a,const LooperID& b) +{ + return a._type_id==b._type_id && a._looper_cnt==b._looper_cnt ; +} + +Looper::Looper() +{ + _update=_running=true; + _loop_cnt=0; + updater=[](){}; +} +LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) +{ + _evmap[event_type].push_front(std::make_pair(_loop_cnt,event_callback)); + return _getNextID(event_type); +} +LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) +{ + _evmap[event_type].push_front(std::make_pair(_loop_cnt,[=](Looper& lp,Event& ev)->int{return event_callback(ev);})); + return _getNextID(event_type); +} +LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) +{ + _evmap[event_type].push_front(std::make_pair(_loop_cnt,[=](Looper& lp,Event& ev)->int{return event_callback(lp);})); + return _getNextID(event_type); +} +LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) +{ + _evmap[event_type].push_front(std::make_pair(_loop_cnt,[=](Looper& lp,Event& ev)->int{return event_callback();})); + return _getNextID(event_type); +} + +LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) +{ + return add(event_type,std::function([=](Looper& lp,Event& ev)->int{event_callback(lp,ev); return 0;})); +} +LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) +{ + return add(event_type,std::function([=](Looper& lp,Event& ev)->int{event_callback(ev); return 0;})); +} +LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) +{ + return add(event_type,std::function([=](Looper& lp,Event& ev)->int{event_callback(lp); return 0;})); +} +LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) +{ + return add(event_type,std::function([=](Looper& lp,Event& ev)->int{event_callback();return 0;})); +} + +LooperID Looper::operator + (const std::pair<_SDLEventType_,std::function>& event_callback) +{ + return add(event_callback.first,event_callback.second); +} +LooperID Looper::operator + (const std::pair<_SDLEventType_,std::function>& event_callback) +{ + return add(event_callback.first,event_callback.second); +} +LooperID Looper::operator + (const std::pair<_SDLEventType_,std::function>& event_callback) +{ + return add(event_callback.first,event_callback.second); +} +LooperID Looper::operator + (const std::pair<_SDLEventType_,std::function>& event_callback) +{ + return add(event_callback.first,event_callback.second); +} + +LooperID Looper::operator + (const std::pair<_SDLEventType_,std::function>& event_callback) +{ + return add(event_callback.first,event_callback.second); +} +LooperID Looper::operator + (const std::pair<_SDLEventType_,std::function>& event_callback) +{ + return add(event_callback.first,event_callback.second); +} +LooperID Looper::operator + (const std::pair<_SDLEventType_,std::function>& event_callback) +{ + return add(event_callback.first,event_callback.second); +} +LooperID Looper::operator + (const std::pair<_SDLEventType_,std::function>& event_callback) +{ + return add(event_callback.first,event_callback.second); +} + + +bool Looper::remove(const LooperID& looperid) +{ + for(auto beginIter=_evmap[looperid._type_id].begin(), + endIter=_evmap[looperid._type_id].end(), + iter=beginIter; + iter!=endIter; + ++iter) + { + if(iter->first==looperid._looper_cnt) + { + _evmap[looperid._type_id].erase(iter); + return true; + } + } + + return false; +} + +bool Looper::operator -(const LooperID& looperid) +{ + return remove(looperid); +} + +void Looper::dispatch() +{ + for(auto callbackPack:_evmap[_e.type]) + { + if(callbackPack.second(*this,_e)) break; + } +} +void Looper::run() +{ + while(_running) + { + while(!_update&&WaitEvent(_e)) + { + dispatch(); + } + + updater(); + + _update=false; + } +} +Event Looper::GetLastEvent() +{ + return _e; +} +void Looper::needupdate() +{ + _update=true; +} +void Looper::needstop() +{ + _running=false; +} +void Looper::stop() +{ + needstop(); + needupdate(); +} +void Looper::reset() +{ + _running=true; + _update=true; + _evmap.clear(); + _loop_cnt=0; + updater=[](){}; +} + +LooperID Looper::_getNextID(const _SDLEventType_& event_type) +{ + LooperID id; + id._looper_cnt = _loop_cnt; + id._type_id = event_type; + + ++_loop_cnt; + return id; +} + +Poller::Poller() +{ + idler=[](){}; +} + +void Poller::reset() +{ + Looper::reset(); + idler=[](){}; +} + +void Poller::run() +{ + int pollret=1; + while(_running) + { + while(!_update&&(pollret=PollEvent(_e))) + { + dispatch(); + } + + /// If pollret is not 0 (new event requests update), or pollret is 0 (No New Event) but Idle function requests update, then call updater. + if(!pollret) idler(); + if(_update) + { + updater(); + _update=false; + } + } +} + +LooperWithTime::LooperWithTime(int Timeout_ms) +{ + _timeout_ms = Timeout_ms; +} + +void LooperWithTime::setTimeout(int ms) +{ + _timeout_ms = ms; +} + +int LooperWithTime::getTimeout() const +{ + return _timeout_ms; +} + +void LooperWithTime::run() +{ + int timeret = 1; + while (_running) + { + while (!_update&&(timeret=WaitEventTimeout(_e, _timeout_ms))) + { + dispatch(); + } + + /// If timeret is not 0 (new event request update), or timeret is 0 (Time out) but Idle function requests update, then call updater. + if (!timeret) idler(); + if (_update) + { + updater(); + _update = false; + } + } +} diff --git a/MiniEngine_Event.h b/MiniEngine_Event.h index 54ae487..4884bd5 100644 --- a/MiniEngine_Event.h +++ b/MiniEngine_Event.h @@ -1,2 +1,89 @@ #pragma once #include "MiniEngine.h" +#include +#include + +typedef SDL_Event Event; + +int PollEvent(Event& refEvent); +int WaitEvent(Event& refEvent); +int WaitEventTimeout(Event& refEvent,int ms); +int PushEvent(const Event& refEvent); + +typedef struct +{ + decltype(Event::type) _type_id; + int _looper_cnt; +}LooperID; + +bool operator == (const LooperID&,const LooperID&); + +class Looper +{ +public: + typedef decltype(Event::type) _SDLEventType_; + Looper(); + /// If Callback does not return 0, then stop transferring this event. + LooperID add(_SDLEventType_ event_type,const std::function& event_callback); + LooperID add(_SDLEventType_ event_type,const std::function& event_callback); + LooperID add(_SDLEventType_ event_type,const std::function& event_callback); + LooperID add(_SDLEventType_ event_type,const std::function& event_callback); + + /// If Callback has no return (void), then we assume it returns 0. + LooperID add(_SDLEventType_ event_type,const std::function& event_callback); + LooperID add(_SDLEventType_ event_type,const std::function& event_callback); + LooperID add(_SDLEventType_ event_type,const std::function& event_callback); + LooperID add(_SDLEventType_ event_type,const std::function& event_callback); + + LooperID operator + (const std::pair<_SDLEventType_,std::function>& event_callback); + LooperID operator + (const std::pair<_SDLEventType_,std::function>& event_callback); + LooperID operator + (const std::pair<_SDLEventType_,std::function>& event_callback); + LooperID operator + (const std::pair<_SDLEventType_,std::function>& event_callback); + + LooperID operator + (const std::pair<_SDLEventType_,std::function>& event_callback); + LooperID operator + (const std::pair<_SDLEventType_,std::function>& event_callback); + LooperID operator + (const std::pair<_SDLEventType_,std::function>& event_callback); + LooperID operator + (const std::pair<_SDLEventType_,std::function>& event_callback); + + bool remove(const LooperID& looperid); + + bool operator - (const LooperID& looperid); + + void dispatch(); + void run(); + Event GetLastEvent(); + void needupdate(); + void needstop(); + void stop(); + void reset(); + + std::function updater; + +protected: + LooperID _getNextID(const _SDLEventType_ & event_type); + Event _e; + bool _running,_update; + std::map<_SDLEventType_,std::list>>> _evmap; + int _loop_cnt; +}; + +class Poller : public Looper +{ +public: + Poller(); + void run(); + void reset(); + + std::function idler; +}; + +class LooperWithTime : public Poller +{ +public: + LooperWithTime(int Timeout_ms=10); + void setTimeout(int ms); + int getTimeout() const; + void run(); +protected: + int _timeout_ms; +}; \ No newline at end of file diff --git a/MiniEngine_SQLite.cpp b/MiniEngine_SQLite.cpp index 31e1089..9248233 100644 --- a/MiniEngine_SQLite.cpp +++ b/MiniEngine_SQLite.cpp @@ -81,7 +81,7 @@ int SQLDB::exec(const std::string& SQLCommand) return _exec_real(SQLCommand,nullptr,nullptr); } -int SQLDB::exec(const std::string& SQLCommand,SQLCallback callback,void* param) +int SQLDB::exec_raw(const std::string& SQLCommand,SQLCallback callback,void* param) { return _exec_real(SQLCommand,callback,param); } diff --git a/MiniEngine_SQLite.h b/MiniEngine_SQLite.h index 5c97aa5..cc3a507 100644 --- a/MiniEngine_SQLite.h +++ b/MiniEngine_SQLite.h @@ -51,8 +51,7 @@ public: int exec(const std::string& SQLCommand); - /// Reserved For Capability - int exec(const std::string& SQLCommand,SQLCallback callback,void* param); + int exec_raw(const std::string& SQLCommand,SQLCallback callback,void* param); const char* getErrorMsg(); void clearError(); diff --git a/doc/html/index.html b/doc/html/index.html new file mode 100644 index 0000000..b177d83 --- /dev/null +++ b/doc/html/index.html @@ -0,0 +1,391 @@ + + + + + + + +
+

MiniEngine 帮助

+

简介

+

应用到项目中

+

MiniEngine核心部分

+

类列表

+

Rect +Point +ColorMode +RGBA +NonCopyable +ErrorViewer +RWOP +Surface +Texture +Renderer +Cursor +Window +Font +LogSystem +SDLSystem +Timer +AudioPlayer +Music +MusicPlayer +Sound +SoundPlayer +StringEngine

+

枚举列表

+

BlendMode +RendererType +FlipMode +SystemCursorType +Platform +PowerState

+

全局函数列表

+

UTF8ToGBK +GBKToUTF8 +canread +canwrite +isexist +canexecute +GetArgc +GetArgv

+ +
+ + \ No newline at end of file diff --git a/doc/html/rect.html b/doc/html/rect.html new file mode 100644 index 0000000..3264340 --- /dev/null +++ b/doc/html/rect.html @@ -0,0 +1,391 @@ + + + + + + + +
+

Rect 矩形类

+

成员

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
类型名称描述
intx矩形左上角的x坐标
inty矩形左上角的y坐标
intw矩形宽度
inth矩形高度
+

方法

+

Rect()

+
+

默认构造.长宽为0且坐标为(0,0)

+

Rect(int X,int Y,int W,int H)

+
+

指定参数的构造

+

SDL_Rect Rect::toSDLRect()

+
+

将当前矩形转换为SDL_Rect表示

+ +
+ + \ No newline at end of file diff --git a/doc/markdown/index.md b/doc/markdown/index.md new file mode 100644 index 0000000..9181bfc --- /dev/null +++ b/doc/markdown/index.md @@ -0,0 +1,43 @@ +# MiniEngine 帮助 +## 简介 +## 应用到项目中 +## MiniEngine核心部分 +### 类列表 +[Rect](rect.md) +Point +ColorMode +RGBA +NonCopyable +ErrorViewer +RWOP +Surface +Texture +Renderer +Cursor +Window +Font +LogSystem +SDLSystem +Timer +AudioPlayer +Music +MusicPlayer +Sound +SoundPlayer +StringEngine +### 枚举列表 +BlendMode +RendererType +FlipMode +SystemCursorType +Platform +PowerState +### 全局函数列表 +UTF8ToGBK +GBKToUTF8 +canread +canwrite +isexist +canexecute +GetArgc +GetArgv diff --git a/doc/markdown/rect.md b/doc/markdown/rect.md new file mode 100644 index 0000000..c85bb2f --- /dev/null +++ b/doc/markdown/rect.md @@ -0,0 +1,20 @@ +# Rect 矩形类 +## 成员 +| 类型 | 名称 | 描述 | +| --- | --- | --- | +| int | x | 矩形左上角的x坐标 | +| int | y | 矩形左上角的y坐标 | +| int | w | 矩形宽度 | +| int | h | 矩形高度 | +## 方法 +Rect() +> +默认构造.长宽为0且坐标为(0,0) + +Rect(int X,int Y,int W,int H) +> +指定参数的构造 + +SDL_Rect Rect::toSDLRect() +> +将当前矩形转换为SDL_Rect表示 diff --git a/makefile_c4 b/makefile_c4 new file mode 100644 index 0000000..507ec04 --- /dev/null +++ b/makefile_c4 @@ -0,0 +1,14 @@ +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 = MiniEngine.o MiniEngine_Android.o MiniEngine_Event.o MiniEngine_Widget.o sqlite/sqlite3.o MiniEngine_SQLite.o + +all: $(PROG) + +$(PROG): $(OBJS) + $(CXX) $(CXXFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ $(OBJS) `sdl2-config --cflags --libs` + +clean: + rm -f $(PROG) $(OBJS) \ No newline at end of file diff --git a/makefile_linux b/makefile_linux new file mode 100644 index 0000000..85a1c68 --- /dev/null +++ b/makefile_linux @@ -0,0 +1,14 @@ +CXXFLAGS = -std=c++14 -Wall -O2 -D__LINUX__ -Iinclude +LDFLAGS = +LDLIBS = -lstdc++ -lSDL2_image -lSDL2_net -lSDL2_ttf -lSDL2_mixer -lSDL2_test -lSDL2 -shared + +PROG = program_name +OBJS = MiniEngine.o MiniEngine_Android.o MiniEngine_Event.o MiniEngine_Widget.o sqlite/sqlite3.o MiniEngine_SQLite.o + +all: $(PROG) + +$(PROG): $(OBJS) + $(CXX) $(CXXFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ $(OBJS) `sdl2-config --cflags --libs` + +clean: + rm -f $(PROG) $(OBJS)