diff --git a/core/base/ActionCombined.cpp b/core/base/ActionCombined.cpp index 83fe5812..8931f5e1 100644 --- a/core/base/ActionCombined.cpp +++ b/core/base/ActionCombined.cpp @@ -32,8 +32,7 @@ namespace easy2d , times_(0) , total_times_(times) { - if (!action) - logs::Warningln("Loop action contains a null action"); + E2D_ASSERT(action && "Loop action contains a null action"); action_ = action; } diff --git a/core/base/ActionManager.cpp b/core/base/ActionManager.cpp index a7726fe2..6b618a1e 100644 --- a/core/base/ActionManager.cpp +++ b/core/base/ActionManager.cpp @@ -43,8 +43,7 @@ namespace easy2d void ActionManager::AddAction(spAction const& action) { - if (!action) - logs::Warningln("AddAction failed, action is nullptr"); + E2D_ASSERT(action && "AddAction failed, NULL pointer exception"); if (action) { diff --git a/core/base/EventDispatcher.cpp b/core/base/EventDispatcher.cpp index 17a122d4..427c2b29 100644 --- a/core/base/EventDispatcher.cpp +++ b/core/base/EventDispatcher.cpp @@ -42,8 +42,7 @@ namespace easy2d void EventDispatcher::AddListener(spEventListener const & listener) { - if (!listener) - logs::Warningln("AddListener failed, action is nullptr"); + E2D_ASSERT(listener && "AddListener failed, NULL pointer exception"); if (listener) { diff --git a/core/base/Frames.cpp b/core/base/Frames.cpp index b140b9e8..636231ec 100644 --- a/core/base/Frames.cpp +++ b/core/base/Frames.cpp @@ -57,8 +57,7 @@ namespace easy2d void Frames::Add(spImage const& frame) { - if (!frame) - logs::Warningln("Frames::Add failed, frame is nullptr."); + E2D_ASSERT(frame && "Frames::Add failed, NULL pointer exception"); if (frame) { diff --git a/core/base/Game.cpp b/core/base/Game.cpp index b51873cc..f2aa48a3 100644 --- a/core/base/Game.cpp +++ b/core/base/Game.cpp @@ -156,8 +156,7 @@ namespace easy2d void Game::EnterScene(spScene const & scene) { - if (!scene) - logs::Warningln("Game::EnterScene failed, scene is nullptr"); + E2D_ASSERT(scene && "Game::EnterScene failed, NULL pointer exception"); if (curr_scene_ == scene || next_scene_ == scene) return; @@ -450,22 +449,13 @@ namespace easy2d LRESULT CALLBACK Game::WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { - LRESULT result = 0; - bool was_handled = false; - Game * game = reinterpret_cast( static_cast(::GetWindowLongW(hwnd, GWLP_USERDATA)) ); - if (game) - { - was_handled = game->HandleMessage(hwnd, msg, wparam, lparam); - } + if (game && game->HandleMessage(hwnd, msg, wparam, lparam)) + return 0; - if (!was_handled) - { - result = ::DefWindowProcW(hwnd, msg, wparam, lparam); - } - return result; + return ::DefWindowProcW(hwnd, msg, wparam, lparam); } } diff --git a/core/base/GeometryNode.cpp b/core/base/GeometryNode.cpp index 255d7a45..955b34c2 100644 --- a/core/base/GeometryNode.cpp +++ b/core/base/GeometryNode.cpp @@ -71,7 +71,11 @@ namespace easy2d if (geometry_ && geometry_->geo_) { auto graphics = Graphics::Instance(); - graphics->SetTransform(geometry_->GetTransformMatrix() * GetTransformMatrix()); + + if (geometry_->GetTransformMatrix().IsIdentity()) + graphics->SetTransform(GetTransformMatrix()); + else + graphics->SetTransform(geometry_->GetTransformMatrix() * GetTransformMatrix()); graphics->FillGeometry( geometry_->geo_, diff --git a/core/base/Node.cpp b/core/base/Node.cpp index 050f3235..c1beaabd 100644 --- a/core/base/Node.cpp +++ b/core/base/Node.cpp @@ -388,8 +388,7 @@ namespace easy2d void Node::AddChild(spNode const& child, int z_order) { - if (!child) - logs::Warningln("Node::AddChild failed, child is nullptr"); + E2D_ASSERT(child && "Node::AddChild failed, NULL pointer exception"); if (child) { @@ -472,8 +471,7 @@ namespace easy2d bool Node::RemoveChild(spNode const& child) { - if (!child) - logs::Warningln("Node::RemoveChild failed, child is nullptr"); + E2D_ASSERT(child && "Node::RemoveChild failed, NULL pointer exception"); if (children_.IsEmpty()) return false; diff --git a/core/base/TaskManager.cpp b/core/base/TaskManager.cpp index 8d99c269..fad15594 100644 --- a/core/base/TaskManager.cpp +++ b/core/base/TaskManager.cpp @@ -42,8 +42,7 @@ namespace easy2d void TaskManager::AddTask(spTask const& task) { - if (!task) - logs::Warningln("AddTask failed, task is nullptr"); + E2D_ASSERT(task && "AddTask failed, NULL pointer exception"); if (task) { diff --git a/core/base/base.hpp b/core/base/base.hpp index f7136ff7..89d55905 100644 --- a/core/base/base.hpp +++ b/core/base/base.hpp @@ -24,7 +24,6 @@ #include "ObjectBase.h" #include "intrusive/SmartPointer.hpp" #include "d2dres.hpp" -#include #ifndef E2D_DECLARE_SMART_PTR #define E2D_DECLARE_SMART_PTR(class_name)\ @@ -92,4 +91,20 @@ namespace easy2d E2D_DECLARE_NS_SMART_PTR(ui, Button); E2D_DECLARE_NS_SMART_PTR(ui, Menu); + + + template + inline Dest* SafeCast(Src* ptr) + { + if (!ptr) + return nullptr; + +#ifdef E2D_DEBUG + Dest* cast = dynamic_cast(ptr); + E2D_ASSERT(cast); + return cast; +#endif + + return static_cast(ptr); + } } diff --git a/core/base/intrusive/List.hpp b/core/base/intrusive/List.hpp index 99e85c66..35104804 100644 --- a/core/base/intrusive/List.hpp +++ b/core/base/intrusive/List.hpp @@ -19,7 +19,7 @@ // THE SOFTWARE. #pragma once -#include +#include "../macros.h" #include #undef DEBUG_CHECK_LIST @@ -156,7 +156,7 @@ namespace easy2d while (tmp != child) { if (tmp == last_) - throw std::logic_error("The node to be removed is not in this list"); + E2D_ASSERT(false && "The node to be removed is not in this list"); tmp = tmp->next_; } #endif @@ -218,14 +218,14 @@ namespace easy2d for (size_t i = 0; i < size; ++i) { if (i == 0) - temp_vec[i]->prev_ = ItemType(); + temp_vec[i]->prev_ = nullptr; else { temp_vec[i]->prev_ = temp_vec[i - 1]; temp_vec[i - 1]->next_ = temp_vec[i]; } if (i == size - 1) - temp_vec[i]->next_ = ItemType(); + temp_vec[i]->next_ = nullptr; else { temp_vec[i]->next_ = temp_vec[i + 1]; @@ -257,13 +257,11 @@ namespace easy2d if (p) { - if (p->prev_ != tmp) - throw std::logic_error("Check list failed"); + E2D_ASSERT(p->prev_ == tmp && "Check list failed"); } else { - if (tmp != last_) - throw std::logic_error("Check list failed"); + E2D_ASSERT(tmp == last_ && "Check list failed"); } } while (p); } diff --git a/core/base/intrusive/SmartPointer.hpp b/core/base/intrusive/SmartPointer.hpp index 8c3f92c8..aeb06c02 100644 --- a/core/base/intrusive/SmartPointer.hpp +++ b/core/base/intrusive/SmartPointer.hpp @@ -22,14 +22,6 @@ #include "../macros.h" #include -#ifndef E2D_INTRUSIVE_PTR_ASSERT -# ifdef E2D_DEBUG -# define E2D_INTRUSIVE_PTR_ASSERT(expr, msg) do { if (!(expr)) throw std::runtime_error(msg); } while(0); -# else -# define E2D_INTRUSIVE_PTR_ASSERT __noop -# endif -#endif - namespace easy2d { namespace intrusive @@ -82,19 +74,19 @@ namespace easy2d inline Type* operator ->() const { - E2D_INTRUSIVE_PTR_ASSERT(ptr_ != nullptr, "Invalid pointer"); + E2D_ASSERT(ptr_ != nullptr, "Invalid pointer"); return ptr_; } inline Type& operator *() const { - E2D_INTRUSIVE_PTR_ASSERT(ptr_ != nullptr, "Invalid pointer"); + E2D_ASSERT(ptr_ != nullptr, "Invalid pointer"); return *ptr_; } inline Type** operator &() { - E2D_INTRUSIVE_PTR_ASSERT(ptr_ == nullptr, "Memory leak"); + E2D_ASSERT(ptr_ == nullptr, "Memory leak"); return &ptr_; } diff --git a/core/base/macros.h b/core/base/macros.h index 0f45d509..63a3da77 100644 --- a/core/base/macros.h +++ b/core/base/macros.h @@ -82,6 +82,7 @@ #include // C++ RunTime Header Files +#include #include #include #include @@ -97,3 +98,12 @@ # define E2D_NOEXCEPT throw() # define E2D_CONSTEXPR const #endif + + +#ifndef E2D_ASSERT +# ifdef E2D_DEBUG +# define E2D_ASSERT(expr) if (!(expr)) { ::OutputDebugStringA("[easy2d] Assert failed: " #expr "\n"); abort(); } +# else +# define E2D_ASSERT __noop +# endif +#endif