From 9786aea48a0ba466892e1c2461ae682b4f996221 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Fri, 21 Apr 2017 10:52:45 +0800 Subject: [PATCH 01/17] Add Grab setter and getter to class Window Grab: When input is grabbed the mouse is confined to the window. If the caller enables a grab while another window is currently grabbed, the other window loses its grab in favor of the caller's window. --- MiniEngine.cpp | 10 ++++++++++ MiniEngine.h | 3 +++ 2 files changed, 13 insertions(+) diff --git a/MiniEngine.cpp b/MiniEngine.cpp index 8303ab8..e3d1478 100644 --- a/MiniEngine.cpp +++ b/MiniEngine.cpp @@ -829,6 +829,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); diff --git a/MiniEngine.h b/MiniEngine.h index da5154a..176094a 100644 --- a/MiniEngine.h +++ b/MiniEngine.h @@ -268,6 +268,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. From bf3fc19eaa0ccecf35f3eae22e9e600aa239c680 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Fri, 21 Apr 2017 11:25:08 +0800 Subject: [PATCH 02/17] Add class Cursor --- MiniEngine.cpp | 113 +++++++++++++++++++++++++++++++++++++++++++++++++ MiniEngine.h | 28 ++++++++++++ 2 files changed, 141 insertions(+) diff --git a/MiniEngine.cpp b/MiniEngine.cpp index e3d1478..1d09c2f 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) @@ -735,6 +801,53 @@ 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 + bool Cursor::isShow() + { + return (SDL_ShowCursor(SDL_QUERY)==SDL_ENABLE); + } + + //static + void Cursor::show(bool Settings) + { + SDL_ShowCursor(Settings?SDL_ENABLE:SDL_DISABLE); + } + bool Renderer::isReady() { return (_get() != nullptr); diff --git a/MiniEngine.h b/MiniEngine.h index 176094a..d31942b 100644 --- a/MiniEngine.h +++ b/MiniEngine.h @@ -150,6 +150,7 @@ namespace MiniEngine friend class Window; friend class Renderer; friend class Font; + friend class Cursor; }; class Texture @@ -232,6 +233,33 @@ 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(); + 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 From 67369a1defeb2ee11d7e3cb60994f5f89c9726a8 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Fri, 21 Apr 2017 13:20:21 +0800 Subject: [PATCH 03/17] Change previous SQLDB::exec(...callback...) to SQLDB::exec_raw --- MiniEngine_SQLite.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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(); From 9e055884dc7639bf49bd301350982f27c428972c Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Fri, 21 Apr 2017 22:59:25 +0800 Subject: [PATCH 04/17] Add implements of some methods in class Cursor --- MiniEngine.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/MiniEngine.cpp b/MiniEngine.cpp index 1d09c2f..d14822d 100644 --- a/MiniEngine.cpp +++ b/MiniEngine.cpp @@ -836,6 +836,22 @@ namespace MiniEngine 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() { From 76010ebd43d8d18ba22da2275d437a876aaa7c5c Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Sat, 22 Apr 2017 10:33:18 +0800 Subject: [PATCH 05/17] Add Cursor activating method. --- MiniEngine.cpp | 5 +++++ MiniEngine.h | 2 ++ 2 files changed, 7 insertions(+) diff --git a/MiniEngine.cpp b/MiniEngine.cpp index d14822d..7f2e4fc 100644 --- a/MiniEngine.cpp +++ b/MiniEngine.cpp @@ -864,6 +864,11 @@ namespace MiniEngine SDL_ShowCursor(Settings?SDL_ENABLE:SDL_DISABLE); } + void Cursor::activate() + { + SDL_SetCursor(_get()); + } + bool Renderer::isReady() { return (_get() != nullptr); diff --git a/MiniEngine.h b/MiniEngine.h index d31942b..d62b3f1 100644 --- a/MiniEngine.h +++ b/MiniEngine.h @@ -252,6 +252,8 @@ namespace MiniEngine static void show(bool); static bool isShow(); + + void activate(); private: std::shared_ptr _cur; void _set(SDL_Cursor*); From 01a8eb27b17c0836f8cc8c2db858633c3b8a57dd Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Sat, 22 Apr 2017 10:33:43 +0800 Subject: [PATCH 06/17] Fix Compile Error --- MiniEngine_SQLite.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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); } From 89048bba72109b700b9800e58f06e7a3ca048eaa Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Sat, 29 Apr 2017 11:32:19 +0800 Subject: [PATCH 07/17] Add makefile for C4droid build --- makefile | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 makefile diff --git a/makefile b/makefile new file mode 100644 index 0000000..507ec04 --- /dev/null +++ b/makefile @@ -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 From c2bb8ff190369426e64c67805563814c6b4e3fc9 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Mon, 1 May 2017 20:25:51 +0800 Subject: [PATCH 08/17] Add Document --- doc/html/index.html | 391 ++++++++++++++++++++++++++++++++++++++++++ doc/markdown/index.md | 43 +++++ 2 files changed, 434 insertions(+) create mode 100644 doc/html/index.html create mode 100644 doc/markdown/index.md 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/markdown/index.md b/doc/markdown/index.md new file mode 100644 index 0000000..bbf37e7 --- /dev/null +++ b/doc/markdown/index.md @@ -0,0 +1,43 @@ +# 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 From c94ae16f77c3454e02b369b513ece4aaacfeffc5 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Mon, 1 May 2017 20:41:13 +0800 Subject: [PATCH 09/17] Add Document For class Rect --- doc/html/rect.html | 391 ++++++++++++++++++++++++++++++++++++++++++ doc/markdown/index.md | 2 +- doc/markdown/rect.md | 20 +++ 3 files changed, 412 insertions(+), 1 deletion(-) create mode 100644 doc/html/rect.html create mode 100644 doc/markdown/rect.md 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 index bbf37e7..9181bfc 100644 --- a/doc/markdown/index.md +++ b/doc/markdown/index.md @@ -3,7 +3,7 @@ ## 应用到项目中 ## MiniEngine核心部分 ### 类列表 -Rect +[Rect](rect.md) Point ColorMode RGBA 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表示 From d43ecd498179ab82c9489e8b8af39c3f7cd43b0b Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Tue, 2 May 2017 12:14:34 +0800 Subject: [PATCH 10/17] Add makefile for linux --- makefile => makefile_c4 | 0 makefile_linux | 14 ++++++++++++++ 2 files changed, 14 insertions(+) rename makefile => makefile_c4 (100%) create mode 100644 makefile_linux diff --git a/makefile b/makefile_c4 similarity index 100% rename from makefile rename to makefile_c4 diff --git a/makefile_linux b/makefile_linux new file mode 100644 index 0000000..4944cb2 --- /dev/null +++ b/makefile_linux @@ -0,0 +1,14 @@ +CXXFLAGS = -std=c++14 -Wall -O2 -D__C4DROID__ -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) From 71c6b36a1c0b6ea68d063cbe001400fd50d4910f Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Tue, 2 May 2017 12:18:40 +0800 Subject: [PATCH 11/17] Update makefile_linux Change Command Line Define to __LINUX__ --- makefile_linux | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/makefile_linux b/makefile_linux index 4944cb2..85a1c68 100644 --- a/makefile_linux +++ b/makefile_linux @@ -1,4 +1,4 @@ -CXXFLAGS = -std=c++14 -Wall -O2 -D__C4DROID__ -Iinclude +CXXFLAGS = -std=c++14 -Wall -O2 -D__LINUX__ -Iinclude LDFLAGS = LDLIBS = -lstdc++ -lSDL2_image -lSDL2_net -lSDL2_ttf -lSDL2_mixer -lSDL2_test -lSDL2 -shared From 87b262325f068749e598cda6435c217bb2b4e470 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Tue, 2 May 2017 12:26:17 +0800 Subject: [PATCH 12/17] use [[deprecated]] instead of attribute --- MiniEngine.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/MiniEngine.h b/MiniEngine.h index d62b3f1..7898a89 100644 --- a/MiniEngine.h +++ b/MiniEngine.h @@ -20,8 +20,7 @@ #include #include -#define _DECL_DEPRECATED __declspec(deprecated) -#define _DECL_DEPRECATED_MSG(InfoString) __declspec(deprecated(InfoString)) +#define _DECL_DEPRECATED [[deprecated]] namespace MiniEngine { From 65c8b2e6efbe4218947679103c64c63fccad95e8 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Tue, 2 May 2017 12:59:57 +0800 Subject: [PATCH 13/17] Add Minimum Event Support --- MiniEngine_Event.cpp | 20 ++++++++++++++++++++ MiniEngine_Event.h | 7 +++++++ 2 files changed, 27 insertions(+) diff --git a/MiniEngine_Event.cpp b/MiniEngine_Event.cpp index d129e87..16b8e29 100644 --- a/MiniEngine_Event.cpp +++ b/MiniEngine_Event.cpp @@ -1 +1,21 @@ #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)); +} diff --git a/MiniEngine_Event.h b/MiniEngine_Event.h index 54ae487..8c92434 100644 --- a/MiniEngine_Event.h +++ b/MiniEngine_Event.h @@ -1,2 +1,9 @@ #pragma once #include "MiniEngine.h" + +typedef SDL_Event Event; + +int PollEvent(Event& refEvent); +int WaitEvent(Event& refEvent); +int WaitEventTimeout(Event& refEvent,int ms); +int PushEvent(const Event& refEvent); From 64ec1cfa42c0cfe99db6c1f125c2f940bc134fcf Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Tue, 2 May 2017 13:37:03 +0800 Subject: [PATCH 14/17] Add Event Looper --- MiniEngine_Event.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++ MiniEngine_Event.h | 27 +++++++++++++++++++ 2 files changed, 91 insertions(+) diff --git a/MiniEngine_Event.cpp b/MiniEngine_Event.cpp index 16b8e29..4e874c5 100644 --- a/MiniEngine_Event.cpp +++ b/MiniEngine_Event.cpp @@ -19,3 +19,67 @@ int PushEvent(const Event& refEvent) { return SDL_PushEvent(const_cast(&refEvent)); } + + +Looper::Looper() +{ + _update=_running=true; +} +void Looper::add(decltype(Event::type) event_type,const std::function& event_callback) +{ + _evmap[event_type].push_back(event_callback); +} +void Looper::add(decltype(Event::type) event_type,const std::function& event_callback) +{ + _evmap[event_type].push_back([&](Looper& lp,Event& ev)->int{return event_callback(ev);}); +} +void Looper::add(decltype(Event::type) event_type,const std::function& event_callback) +{ + _evmap[event_type].push_back([&](Looper& lp,Event& ev)->int{event_callback(lp,ev); return 0;}); +} +void Looper::add(decltype(Event::type) event_type,const std::function& event_callback) +{ + _evmap[event_type].push_back([&](Looper& lp,Event& ev)->int{event_callback(ev); return 0;}); +} +void Looper::dispatch() +{ + for(auto callback:_evmap[_e.type]) + { + if(callback(*this,_e)) break; + } +} +void Looper::run() +{ + while(_running) + { + while(!_update&&WaitEvent(_e)) + { + dispatch(); + } + + _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(); +} diff --git a/MiniEngine_Event.h b/MiniEngine_Event.h index 8c92434..aa3a4f6 100644 --- a/MiniEngine_Event.h +++ b/MiniEngine_Event.h @@ -1,5 +1,7 @@ #pragma once #include "MiniEngine.h" +#include +#include typedef SDL_Event Event; @@ -7,3 +9,28 @@ int PollEvent(Event& refEvent); int WaitEvent(Event& refEvent); int WaitEventTimeout(Event& refEvent,int ms); int PushEvent(const Event& refEvent); + +class Looper +{ +public: + Looper(); + /// If Callback does not return 0, then stop transferring this event. + void add(decltype(Event::type) event_type,const std::function& event_callback); + /// If Callback does not return 0, then stop transferring this event. (Discards Looper) + void add(decltype(Event::type) event_type,const std::function& event_callback); + /// If Callback has no return (void), then we assume it return 0. + void add(decltype(Event::type) event_type,const std::function& event_callback); + /// If Callback has no return (void), then we assume it return 0. (Discards Looper) + void add(decltype(Event::type) event_type,const std::function& event_callback); + void dispatch(); + void run(); + Event GetLastEvent(); + void needupdate(); + void needstop(); + void stop(); + void reset(); +private: + Event _e; + bool _running,_update; + std::map>> _evmap; +}; From d18fcf385d680d8e392484339e38c03f89d57330 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Tue, 2 May 2017 19:05:48 +0800 Subject: [PATCH 15/17] Rewrite Event Looper --- MiniEngine_Event.cpp | 129 +++++++++++++++++++++++++++++++++++++++---- MiniEngine_Event.h | 44 ++++++++++++--- 2 files changed, 155 insertions(+), 18 deletions(-) diff --git a/MiniEngine_Event.cpp b/MiniEngine_Event.cpp index 4e874c5..92469ca 100644 --- a/MiniEngine_Event.cpp +++ b/MiniEngine_Event.cpp @@ -20,32 +20,140 @@ 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; } -void Looper::add(decltype(Event::type) event_type,const std::function& event_callback) +LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) { - _evmap[event_type].push_back(event_callback); + _evmap[event_type].push_front(std::make_pair(_loop_cnt,event_callback)); + + LooperID id; + id._looper_cnt=_loop_cnt; + id._type_id=event_type; + + ++_loop_cnt; + + return id; } -void Looper::add(decltype(Event::type) event_type,const std::function& event_callback) +LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) { - _evmap[event_type].push_back([&](Looper& lp,Event& ev)->int{return event_callback(ev);}); + _evmap[event_type].push_front(std::make_pair(_loop_cnt,[&](Looper& lp,Event& ev)->int{return event_callback(ev);})); + + LooperID id; + id._looper_cnt=_loop_cnt; + id._type_id=event_type; + + ++_loop_cnt; + + return id; } -void Looper::add(decltype(Event::type) event_type,const std::function& event_callback) +LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) { - _evmap[event_type].push_back([&](Looper& lp,Event& ev)->int{event_callback(lp,ev); return 0;}); + _evmap[event_type].push_front(std::make_pair(_loop_cnt,[&](Looper& lp,Event& ev)->int{return event_callback(lp);})); + + LooperID id; + id._looper_cnt=_loop_cnt; + id._type_id=event_type; + + ++_loop_cnt; + + return id; } -void Looper::add(decltype(Event::type) event_type,const std::function& event_callback) +LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) { - _evmap[event_type].push_back([&](Looper& lp,Event& ev)->int{event_callback(ev); return 0;}); + _evmap[event_type].push_front(std::make_pair(_loop_cnt,[&](Looper& lp,Event& ev)->int{return event_callback();})); + + LooperID id; + id._looper_cnt=_loop_cnt; + id._type_id=event_type; + + ++_loop_cnt; + + return id; } + +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; +} + void Looper::dispatch() { - for(auto callback:_evmap[_e.type]) + for(auto callbackPack:_evmap[_e.type]) { - if(callback(*this,_e)) break; + if(callbackPack.second(*this,_e)) break; } } void Looper::run() @@ -82,4 +190,5 @@ void Looper::reset() _running=true; _update=true; _evmap.clear(); + _loop_cnt=0; } diff --git a/MiniEngine_Event.h b/MiniEngine_Event.h index aa3a4f6..85ac06f 100644 --- a/MiniEngine_Event.h +++ b/MiniEngine_Event.h @@ -10,18 +10,45 @@ 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. - void add(decltype(Event::type) event_type,const std::function& event_callback); - /// If Callback does not return 0, then stop transferring this event. (Discards Looper) - void add(decltype(Event::type) event_type,const std::function& event_callback); - /// If Callback has no return (void), then we assume it return 0. - void add(decltype(Event::type) event_type,const std::function& event_callback); - /// If Callback has no return (void), then we assume it return 0. (Discards Looper) - void add(decltype(Event::type) 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 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(); @@ -32,5 +59,6 @@ public: private: Event _e; bool _running,_update; - std::map>> _evmap; + std::map<_SDLEventType_,std::list>>> _evmap; + int _loop_cnt; }; From 2ecddc44e79682f84e864a22018603d0dd0312b4 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Tue, 2 May 2017 20:51:56 +0800 Subject: [PATCH 16/17] Add Poller Add implement of Looper::operator - --- MiniEngine_Event.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ MiniEngine_Event.h | 15 ++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/MiniEngine_Event.cpp b/MiniEngine_Event.cpp index 92469ca..3f42628 100644 --- a/MiniEngine_Event.cpp +++ b/MiniEngine_Event.cpp @@ -29,6 +29,7 @@ Looper::Looper() { _update=_running=true; _loop_cnt=0; + updater=[](){}; } LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) { @@ -149,6 +150,11 @@ bool Looper::remove(const LooperID& looperid) return false; } +bool Looper::operator -(const LooperID& looperid) +{ + return remove(looperid); +} + void Looper::dispatch() { for(auto callbackPack:_evmap[_e.type]) @@ -165,6 +171,8 @@ void Looper::run() dispatch(); } + updater(); + _update=false; } } @@ -191,4 +199,36 @@ void Looper::reset() _update=true; _evmap.clear(); _loop_cnt=0; + updater=[](){}; +} + +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 update. + if(!pollret) idler(); + if(_update) + { + updater(); + _update=false; + } + } } diff --git a/MiniEngine_Event.h b/MiniEngine_Event.h index 85ac06f..91217b2 100644 --- a/MiniEngine_Event.h +++ b/MiniEngine_Event.h @@ -56,9 +56,22 @@ public: void needstop(); void stop(); void reset(); -private: + + std::function updater; + +protected: 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; +}; From f4c629393b2bcb2a3b92c5472dde20c74840ca93 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Tue, 2 May 2017 22:38:37 +0800 Subject: [PATCH 17/17] Fix compile warning generated by VC. Add LooperWithTime. Fix Bug: Change Lambda Capture from "&" (Keep Reference) to "=" (Copy). Before this fix, the following code will cause a runtime error: int AppMain() { Window wnd("Title", 1280, 768); Renderer rnd = wnd.getRenderer(); Looper lp; lp + make_pair(SDL_QUIT, []() {printf("Quit!\n");}); lp.run(); return 0; } Now, the bug is fixed~! --- MiniEngine.cpp | 6 +-- MiniEngine.h | 3 ++ MiniEngine_Event.cpp | 97 ++++++++++++++++++++++++++------------------ MiniEngine_Event.h | 12 ++++++ 4 files changed, 75 insertions(+), 43 deletions(-) diff --git a/MiniEngine.cpp b/MiniEngine.cpp index 7f2e4fc..63c4618 100644 --- a/MiniEngine.cpp +++ b/MiniEngine.cpp @@ -164,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) @@ -1582,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 7898a89..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 diff --git a/MiniEngine_Event.cpp b/MiniEngine_Event.cpp index 3f42628..6f39595 100644 --- a/MiniEngine_Event.cpp +++ b/MiniEngine_Event.cpp @@ -34,67 +34,39 @@ Looper::Looper() LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) { _evmap[event_type].push_front(std::make_pair(_loop_cnt,event_callback)); - - LooperID id; - id._looper_cnt=_loop_cnt; - id._type_id=event_type; - - ++_loop_cnt; - - return id; + 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);})); - - LooperID id; - id._looper_cnt=_loop_cnt; - id._type_id=event_type; - - ++_loop_cnt; - - return id; + _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);})); - - LooperID id; - id._looper_cnt=_loop_cnt; - id._type_id=event_type; - - ++_loop_cnt; - - return id; + _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();})); - - LooperID id; - id._looper_cnt=_loop_cnt; - id._type_id=event_type; - - ++_loop_cnt; - - return id; + _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;})); + 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;})); + 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;})); + 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;})); + 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) @@ -202,6 +174,16 @@ void Looper::reset() 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=[](){}; @@ -223,7 +205,7 @@ void Poller::run() dispatch(); } - /// If pollret is not 0 (new event requests update), or pollret is 0 (No New Event) but Idle function requests update, then call update. + /// 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) { @@ -232,3 +214,38 @@ void Poller::run() } } } + +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 91217b2..4884bd5 100644 --- a/MiniEngine_Event.h +++ b/MiniEngine_Event.h @@ -60,6 +60,7 @@ public: std::function updater; protected: + LooperID _getNextID(const _SDLEventType_ & event_type); Event _e; bool _running,_update; std::map<_SDLEventType_,std::list>>> _evmap; @@ -75,3 +76,14 @@ public: 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