From db5446bb7849338f7583f699a069b2a22445cd5d Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Wed, 17 Oct 2018 00:16:26 +0800 Subject: [PATCH] fix: minor fixes --- core/e2daction.h | 12 ++++--- core/e2dcomponent.h | 10 ++++-- core/e2devent.h | 12 ++++--- core/e2dimpl.h | 13 +++++-- core/e2dmacros.h | 42 +++++------------------ core/e2dmodule.h | 10 ++++-- core/e2dobject.h | 11 ++++-- core/e2dtool.h | 10 ++++-- core/e2dtransition.h | 11 ++++-- core/e2dutil.h | 10 ++++-- core/modules/Graphics.cpp | 12 ++++--- core/tools/File.cpp | 8 ++--- core/tools/Music.cpp | 64 ++++++++++++++++++++++++----------- project/vs2017/Easy2D.vcxproj | 2 +- 14 files changed, 136 insertions(+), 91 deletions(-) diff --git a/core/e2daction.h b/core/e2daction.h index 5f898b49..1dea1480 100644 --- a/core/e2daction.h +++ b/core/e2daction.h @@ -18,26 +18,26 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -#pragma once +#ifndef __E2D_ACTION_H__ +#define __E2D_ACTION_H__ + + #include "e2dutil.h" #include "e2dobject.h" namespace easy2d { - class Node; class Loop; class Sequence; class Spawn; - class ActionManager; // 基础动作 class Action : public Ref { - friend class ActionManager; friend class Loop; friend class Sequence; friend class Spawn; @@ -818,5 +818,7 @@ namespace easy2d Animation * animation_; }; +} // end of easy2d namespace -} \ No newline at end of file + +#endif // __E2D_ACTION_H__ diff --git a/core/e2dcomponent.h b/core/e2dcomponent.h index dbf0b46f..2d2f2cb4 100644 --- a/core/e2dcomponent.h +++ b/core/e2dcomponent.h @@ -18,13 +18,15 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -#pragma once +#ifndef __E2D_COMPONENT_H__ +#define __E2D_COMPONENT_H__ + + #include "e2dobject.h" namespace easy2d { - class Button : public Node { @@ -175,5 +177,7 @@ namespace easy2d std::vector buttons_; }; +} // end of easy2d namespace -} + +#endif // !__E2D_COMPONENT_H__ diff --git a/core/e2devent.h b/core/e2devent.h index d7d39b00..985380a7 100644 --- a/core/e2devent.h +++ b/core/e2devent.h @@ -18,13 +18,14 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -#pragma once +#ifndef __E2D_EVENT_H__ +#define __E2D_EVENT_H__ + + #include "e2dutil.h" namespace easy2d { - - // 按键消息 class KeyEvent { @@ -121,4 +122,7 @@ namespace easy2d LPARAM l_param_; }; -} \ No newline at end of file +} // end of easy2d namespace + + +#endif // __E2D_EVENT_H__ diff --git a/core/e2dimpl.h b/core/e2dimpl.h index b28c6caa..34aecf47 100644 --- a/core/e2dimpl.h +++ b/core/e2dimpl.h @@ -18,11 +18,15 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -#pragma once +#ifndef __E2D_IMPL_H__ +#define __E2D_IMPL_H__ + + #include "e2dutil.h" namespace easy2d { + // 文字渲染器 class TextRenderer : public IDWriteTextRenderer @@ -77,7 +81,7 @@ namespace easy2d BOOL IsSideways, BOOL IsRightToLeft, IUnknown* clientDrawingEffect - ); + ); STDMETHOD(IsPixelSnappingDisabled)( __maybenull void* clientDrawingContext, @@ -150,4 +154,7 @@ namespace easy2d } } -} +} // end of easy2d namespace + + +#endif // __E2D_IMPL_H__ diff --git a/core/e2dmacros.h b/core/e2dmacros.h index 91f3de95..2ecb9266 100644 --- a/core/e2dmacros.h +++ b/core/e2dmacros.h @@ -18,7 +18,9 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -#pragma once +#ifndef __E2D_MACROS_H__ +#define __E2D_MACROS_H__ + #ifndef WINVER # define WINVER 0x0700 // Allow use of features specific to Windows 7 or later @@ -137,48 +139,20 @@ #ifndef E2D_WARNING # if defined( DEBUG ) || defined( _DEBUG ) -# define E2D_WARNING(msg, ...) do { OutputDebugStringExW(L"Warning: " _CRT_WIDE(#msg) L"\n", __VA_ARGS__); } while(0) +# define E2D_WARNING(msg) do { ::OutputDebugStringW(L"Warning: " _CRT_WIDE(msg) L"\r\n"); } while(0) # else -# define E2D_WARNING(msg, ...) ((void)0) +# define E2D_WARNING(msg) ((void)0) # endif #endif #ifndef E2D_WARNING_IF # if defined( DEBUG ) || defined( _DEBUG ) -# define E2D_WARNING_IF(exp, msg, ...) do { if (exp) { OutputDebugStringExW(L"Warning: " _CRT_WIDE(#msg) L"\n", __VA_ARGS__); } } while(0) +# define E2D_WARNING_IF(exp, msg) do { if (exp) { ::OutputDebugStringW(L"Warning: " _CRT_WIDE(msg) L"\r\n"); } } while(0) # else -# define E2D_WARNING_IF(exp, msg, ...) ((void)0) +# define E2D_WARNING_IF(exp, msg) ((void)0) # endif #endif -void OutputDebugStringExW(LPCWSTR pszOutput, ...) -{ - va_list args = NULL; - va_start(args, pszOutput); - - size_t nLen = _vscwprintf(pszOutput, args) + 1; - const wchar_t* psBuffer = new wchar_t[nLen]; - _vsnwprintf_s(psBuffer, nLen, nLen, pszOutput, args); - - va_end(args); - - OutputDebugStringW(psBuffer); - delete [] psBuffer; -} - -void OutputDebugStringExA(LPCSTR pszOutput, ...) -{ - va_list args = NULL; - va_start(args, pszOutput); - - size_t nLen = _vscprintf(pszOutput, args) + 1; - const char* psBuffer = new char[nLen]; - _vsnprintf_s(psBuffer, nLen, nLen, pszOutput, args); - - va_end(args); - - OutputDebugStringA(psBuffer); - delete [] psBuffer; -} +#endif // __E2D_MACROS_H__ diff --git a/core/e2dmodule.h b/core/e2dmodule.h index 9ab21539..2aad77cf 100644 --- a/core/e2dmodule.h +++ b/core/e2dmodule.h @@ -18,7 +18,10 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -#pragma once +#ifndef __E2D_MODULE_H__ +#define __E2D_MODULE_H__ + + #include "e2dimpl.h" #include "e2dobject.h" #include "e2dtransition.h" @@ -321,4 +324,7 @@ namespace easy2d Transition* transition_; }; -} +} // end of easy2d namespace + + +#endif // __E2D_MODULE_H__ diff --git a/core/e2dobject.h b/core/e2dobject.h index ec27db7c..eb781583 100644 --- a/core/e2dobject.h +++ b/core/e2dobject.h @@ -18,7 +18,10 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -#pragma once +#ifndef __E2D_OBJECT_H__ +#define __E2D_OBJECT_H__ + + #include "e2dutil.h" #include "e2devent.h" @@ -26,7 +29,6 @@ namespace easy2d { - // 图片 class Image : public Ref @@ -1092,4 +1094,7 @@ namespace easy2d ID2D1StrokeStyle * stroke_style_; }; -} +} // end of easy2d namespace + + +#endif // __E2D_OBJECT_H__ diff --git a/core/e2dtool.h b/core/e2dtool.h index dcaf2525..94f42fc6 100644 --- a/core/e2dtool.h +++ b/core/e2dtool.h @@ -18,7 +18,10 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -#pragma once +#ifndef __E2D_TOOL_H__ +#define __E2D_TOOL_H__ + + #include "e2dutil.h" #include "e2dimpl.h" @@ -374,4 +377,7 @@ namespace easy2d static const String& GetExeFilePath(); }; -} \ No newline at end of file +} // end of easy2d namespace + + +#endif // __E2D_TOOL_H__ diff --git a/core/e2dtransition.h b/core/e2dtransition.h index 97a0598e..67bb3256 100644 --- a/core/e2dtransition.h +++ b/core/e2dtransition.h @@ -18,13 +18,15 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -#pragma once +#ifndef __E2D_TRANSITION_H__ +#define __E2D_TRANSITION_H__ + + #include "e2dutil.h" namespace easy2d { - class Game; class Scene; @@ -193,4 +195,7 @@ namespace easy2d float rotation_; }; -} \ No newline at end of file +} // end of easy2d namespace + + +#endif // __E2D_TRANSITION_H__ diff --git a/core/e2dutil.h b/core/e2dutil.h index 37d07db9..fa0a2fba 100644 --- a/core/e2dutil.h +++ b/core/e2dutil.h @@ -18,13 +18,15 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -#pragma once +#ifndef __E2D_UTIL_H__ +#define __E2D_UTIL_H__ + + #include "e2dmacros.h" namespace easy2d { - // 方向 enum class Direction : int { @@ -676,5 +678,7 @@ namespace easy2d } } +} // end of easy2d namespace -} + +#endif // __E2D_UTIL_H__ diff --git a/core/modules/Graphics.cpp b/core/modules/Graphics.cpp index a7fd2db5..f41df0d2 100644 --- a/core/modules/Graphics.cpp +++ b/core/modules/Graphics.cpp @@ -314,8 +314,12 @@ ID2D1StrokeStyle * easy2d::Graphics::GetRoundStrokeStyle() float easy2d::Graphics::GetDpi() { - HDC hdc = ::GetDC(0); - int dpi = ::GetDeviceCaps(hdc, LOGPIXELSX); - ::ReleaseDC(0, hdc); - return static_cast(dpi); + static float dpi = -1; + if (dpi < 0) + { + HDC hdc = ::GetDC(0); + dpi = static_cast(::GetDeviceCaps(hdc, LOGPIXELSX)); + ::ReleaseDC(0, hdc); + } + return dpi; } diff --git a/core/tools/File.cpp b/core/tools/File.cpp index f093a3b0..a46f55bf 100644 --- a/core/tools/File.cpp +++ b/core/tools/File.cpp @@ -120,11 +120,11 @@ easy2d::File easy2d::File::Extract(const Resource& res, const String& dest_file_ return std::move(file); // 查找资源文件中、加载资源到内存、得到资源大小 - HRSRC res = ::FindResource(NULL, MAKEINTRESOURCE(res.id), static_cast(res.type)); - HGLOBAL res_data = ::LoadResource(NULL, res); - DWORD res_size = ::SizeofResource(NULL, res); + HRSRC res_info = ::FindResource(NULL, MAKEINTRESOURCE(res.id), static_cast(res.type)); + HGLOBAL res_data = ::LoadResource(NULL, res_info); + DWORD res_size = ::SizeofResource(NULL, res_info); - if (res && res_data && res_size) + if (res_info && res_data && res_size) { // 写入文件 DWORD written_bytes = 0; diff --git a/core/tools/Music.cpp b/core/tools/Music.cpp index d8872de1..7dc79213 100644 --- a/core/tools/Music.cpp +++ b/core/tools/Music.cpp @@ -22,16 +22,32 @@ #include "..\e2dmodule.h" -inline bool TraceError(wchar_t* prompt) +namespace { - E2D_WARNING("Music error: %s failed!", prompt); - return false; -} + void OutputDebugStringExW(LPCWSTR pszOutput, ...) + { + va_list args = NULL; + va_start(args, pszOutput); -inline bool TraceError(wchar_t* prompt, HRESULT hr) -{ - E2D_WARNING("Music error: %s (%#X)", prompt, hr); - return false; + size_t nLen = _vscwprintf(pszOutput, args) + 1; + wchar_t* psBuffer = new wchar_t[nLen]; + _vsnwprintf_s(psBuffer, nLen, nLen, pszOutput, args); + + va_end(args); + + ::OutputDebugStringW(psBuffer); + delete[] psBuffer; + } + + inline void TraceError(LPCWSTR output) + { + OutputDebugStringExW(L"Music error: %s failed!\r\n", output); + } + + inline void TraceError(LPCWSTR output, HRESULT hr) + { + OutputDebugStringExW(L"Music error: %s (%#X)\r\n", output, hr); + } } @@ -99,35 +115,40 @@ namespace easy2d res_info = FindResourceW(HINST_THISCOMPONENT, res_name, res_type); if (res_info == nullptr) { - return TraceError(L"FindResource"); + TraceError(L"FindResource"); + return false; } res_data = LoadResource(HINST_THISCOMPONENT, res_info); if (res_data == nullptr) { - return TraceError(L"LoadResource"); + TraceError(L"LoadResource"); + return false; } res_size = SizeofResource(HINST_THISCOMPONENT, res_info); if (res_size == 0) { - return TraceError(L"SizeofResource"); + TraceError(L"SizeofResource"); + return false; } res = LockResource(res_data); if (res == nullptr) { - return TraceError(L"LockResource"); + TraceError(L"LockResource"); + return false; } - stream = ::SHCreateMemStream( + stream = SHCreateMemStream( static_cast(res), static_cast(res_size) ); if (stream == nullptr) { - return TraceError(L"SHCreateMemStream"); + TraceError(L"SHCreateMemStream"); + return false; } if (SUCCEEDED(hr)) @@ -359,7 +380,7 @@ bool easy2d::Music::Load(const easy2d::String & file_path) File music_file; if (!music_file.Open(file_path)) { - E2D_WARNING("Music::Load error: File not found."); + E2D_WARNING("Media file not found."); return false; } @@ -381,7 +402,8 @@ bool easy2d::Music::Load(const easy2d::String & file_path) delete[] wave_data_; wave_data_ = nullptr; } - return TraceError(L"Create source voice error", hr); + TraceError(L"Create source voice error", hr); + return false; } opened_ = true; @@ -409,7 +431,8 @@ bool easy2d::Music::Load(const Resource& res) delete[] wave_data_; wave_data_ = nullptr; } - return TraceError(L"Create source voice error", hr); + TraceError(L"Create source voice error", hr); + return false; } opened_ = true; @@ -420,13 +443,13 @@ bool easy2d::Music::Play(int loop_count) { if (!opened_) { - E2D_WARNING("Music::Play Failed: Music must be opened first!"); + E2D_WARNING("Music must be opened first!"); return false; } if (voice_ == nullptr) { - E2D_WARNING("Music::Play Failed: IXAudio2SourceVoice Null pointer exception!"); + E2D_WARNING("IXAudio2SourceVoice Null pointer exception!"); return false; } @@ -456,7 +479,8 @@ bool easy2d::Music::Play(int loop_count) HRESULT hr; if (FAILED(hr = voice_->SubmitSourceBuffer(&buffer))) { - return TraceError(L"Submitting source buffer error", hr); + TraceError(L"Submitting source buffer error", hr); + return false; } hr = voice_->Start(0); diff --git a/project/vs2017/Easy2D.vcxproj b/project/vs2017/Easy2D.vcxproj index 6fedf7cd..6b67da54 100644 --- a/project/vs2017/Easy2D.vcxproj +++ b/project/vs2017/Easy2D.vcxproj @@ -101,7 +101,7 @@ Disabled WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) false - EditAndContinue + None false true