diff --git a/SDK/include/NDK/BaseComponent.hpp b/SDK/include/NDK/BaseComponent.hpp index e21a1e38f..d9fdcba45 100644 --- a/SDK/include/NDK/BaseComponent.hpp +++ b/SDK/include/NDK/BaseComponent.hpp @@ -29,6 +29,7 @@ namespace Ndk virtual std::unique_ptr Clone() const = 0; + inline const EntityHandle& GetEntity() const; ComponentIndex GetIndex() const; inline static ComponentIndex GetMaxComponentIndex(); diff --git a/SDK/include/NDK/BaseComponent.inl b/SDK/include/NDK/BaseComponent.inl index 7abade990..0596eb589 100644 --- a/SDK/include/NDK/BaseComponent.inl +++ b/SDK/include/NDK/BaseComponent.inl @@ -19,11 +19,19 @@ namespace Ndk { } + /*! + * \brief Gets the entity owning this component + * \return A handle to the entity owning this component, may be invalid if no entity owns it. + */ + inline const EntityHandle& BaseComponent::GetEntity() const + { + return m_entity; + } + /*! * \brief Gets the index of the component * \return Index of the component */ - inline ComponentIndex BaseComponent::GetIndex() const { return m_componentIndex; diff --git a/build/scripts/actions/package.lua b/build/scripts/actions/package.lua index cbdae329c..bc86c4ba4 100644 --- a/build/scripts/actions/package.lua +++ b/build/scripts/actions/package.lua @@ -85,8 +85,8 @@ ACTION.Function = function () local exeFileExt local exeFilterFunc if (os.is("windows")) then - binFileMasks = {"**.dll"} - libFileMasks = {"**.lib", "**.a", "**.pdb"} + binFileMasks = {"**.dll", "**.pdb"} + libFileMasks = {"**.lib", "**.a"} exeFileExt = ".exe" exeFilterFunc = function (filePath) return true end else diff --git a/examples/Tut01/main.cpp b/examples/Tut01/main.cpp index b5e07074d..b1d962e1e 100644 --- a/examples/Tut01/main.cpp +++ b/examples/Tut01/main.cpp @@ -1,4 +1,4 @@ -// Sources pour https://github.com/DigitalPulseSoftware/NazaraEngine/wiki/(FR)-Tutoriel-01---Hello-World +// Sources pour https://github.com/DigitalPulseSoftware/NazaraEngine/wiki/(FR)-Tutoriel:-%5B01%5D-Hello-World #include #include diff --git a/examples/Tut02/build.lua b/examples/Tut02/build.lua new file mode 100644 index 000000000..dbb8a8c9f --- /dev/null +++ b/examples/Tut02/build.lua @@ -0,0 +1,11 @@ +EXAMPLE.Name = "Tut02_Events" + +EXAMPLE.EnableConsole = true + +EXAMPLE.Files = { + "main.cpp" +} + +EXAMPLE.Libraries = { + "NazaraSDK" +} diff --git a/examples/Tut02/main.cpp b/examples/Tut02/main.cpp new file mode 100644 index 000000000..83de624c6 --- /dev/null +++ b/examples/Tut02/main.cpp @@ -0,0 +1,50 @@ +// Sources pour https://github.com/DigitalPulseSoftware/NazaraEngine/wiki/(FR)-Tutoriel:-%5B02%5D-Gestion-des-événements + +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char* argv[]) +{ + Ndk::Application application(argc, argv); + + Nz::RenderWindow& mainWindow = application.AddWindow(); + mainWindow.Create(Nz::VideoMode(800, 600, 32), "Test"); + + mainWindow.EnableCloseOnQuit(false); + + Ndk::World& world = application.AddWorld(); + world.GetSystem().SetGlobalUp(Nz::Vector3f::Down()); + world.GetSystem().SetDefaultBackground(Nz::ColorBackground::New(Nz::Color(117, 122, 214))); + + Ndk::EntityHandle viewEntity = world.CreateEntity(); + viewEntity->AddComponent(); + + Ndk::CameraComponent& viewer = viewEntity->AddComponent(); + viewer.SetTarget(&mainWindow); + viewer.SetProjectionType(Nz::ProjectionType_Orthogonal); + + + Nz::EventHandler& eventHandler = mainWindow.GetEventHandler(); + eventHandler.OnKeyPressed.Connect([](const Nz::EventHandler*, const Nz::WindowEvent::KeyEvent& e) + { + std::cout << Nz::Keyboard::GetKeyName(e.code) << std::endl; + + // Profitons-en aussi pour nous donner un moyen de quitter le programme + if (e.code == Nz::Keyboard::Escape) + Ndk::Application::Instance()->Quit(); // Cette ligne casse la boucle Run() de l'application + }); + + + while (application.Run()) + { + mainWindow.Display(); + } + + return EXIT_SUCCESS; +} diff --git a/include/Nazara/Core/ObjectHandle.inl b/include/Nazara/Core/ObjectHandle.inl index 920d407a6..f4c70ae85 100644 --- a/include/Nazara/Core/ObjectHandle.inl +++ b/include/Nazara/Core/ObjectHandle.inl @@ -128,6 +128,9 @@ namespace Nz template void ObjectHandle::Reset(ObjectHandle&& handle) noexcept { + if (this == &handle) + return; + if (m_object) m_object->UnregisterHandle(this); diff --git a/include/Nazara/Lua/LuaInstance.hpp b/include/Nazara/Lua/LuaInstance.hpp index 5f27c76e6..39b0879a8 100644 --- a/include/Nazara/Lua/LuaInstance.hpp +++ b/include/Nazara/Lua/LuaInstance.hpp @@ -31,7 +31,7 @@ namespace Nz public: LuaInstance(); LuaInstance(const LuaInstance&) = delete; - inline LuaInstance(LuaInstance&& instance) noexcept; + LuaInstance(LuaInstance&& instance) noexcept; ~LuaInstance(); void ArgCheck(bool condition, unsigned int argNum, const char* error) const; @@ -173,7 +173,7 @@ namespace Nz void* ToUserdata(int index, const String& tname) const; LuaInstance& operator=(const LuaInstance&) = delete; - inline LuaInstance& operator=(LuaInstance&& instance) noexcept; + LuaInstance& operator=(LuaInstance&& instance) noexcept; static int GetIndexOfUpValue(int upValue); static LuaInstance* GetInstance(lua_State* state); diff --git a/include/Nazara/Lua/LuaInstance.inl b/include/Nazara/Lua/LuaInstance.inl index 9c54982e4..b30c7eca4 100644 --- a/include/Nazara/Lua/LuaInstance.inl +++ b/include/Nazara/Lua/LuaInstance.inl @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -14,18 +15,6 @@ namespace Nz { - inline LuaInstance::LuaInstance(LuaInstance&& instance) noexcept : - m_memoryLimit(instance.m_memoryLimit), - m_memoryUsage(instance.m_memoryUsage), - m_timeLimit(instance.m_timeLimit), - m_clock(std::move(instance.m_clock)), - m_lastError(std::move(instance.m_lastError)), - m_state(instance.m_state), - m_level(instance.m_level) - { - instance.m_state = nullptr; - } - inline lua_State* LuaInstance::GetInternalState() const { return m_state; @@ -51,21 +40,6 @@ namespace Nz return m_timeLimit; } - inline LuaInstance& LuaInstance::operator=(LuaInstance&& instance) noexcept - { - m_clock = std::move(instance.m_clock); - m_lastError = std::move(instance.m_lastError); - m_level = instance.m_level; - m_memoryLimit = instance.m_memoryLimit; - m_memoryUsage = instance.m_memoryUsage; - m_state = instance.m_state; - m_timeLimit = instance.m_timeLimit; - - instance.m_state = nullptr; - - return *this; - } - // Functions args inline unsigned int LuaImplQueryArg(const LuaInstance& instance, int index, bool* arg, TypeTag) { diff --git a/include/Nazara/Math/Algorithm.inl b/include/Nazara/Math/Algorithm.inl index 9a0eaabfb..612cdf952 100644 --- a/include/Nazara/Math/Algorithm.inl +++ b/include/Nazara/Math/Algorithm.inl @@ -97,6 +97,29 @@ namespace Nz return 0; } + + template /*constexpr*/ std::enable_if_t::value || !std::is_integral::value, bool> NumberEquals(T a, T b, T maxDifference) + { + if (b > a) + std::swap(a, b); + + T diff = a - b; + return diff <= maxDifference; + } + + template /*constexpr*/ std::enable_if_t::value && std::is_integral::value, bool> NumberEquals(T a, T b, T maxDifference) + { + if (b > a) + std::swap(a, b); + + if ((b < 0) && (a > std::numeric_limits::max() + b)) + return false; + + if ((b > 0) && (a < std::numeric_limits::min() + b)) + return false; + + return std::abs(a - b) <= maxDifference; + } } /*! @@ -565,11 +588,7 @@ namespace Nz //TODO: Mark as constexpr when supported by all major compilers /*constexpr*/ inline bool NumberEquals(T a, T b, T maxDifference) { - if (b > a) - std::swap(a, b); - - T diff = a - b; - return diff <= maxDifference; + return Detail::NumberEquals(a, b, maxDifference); } /*! diff --git a/include/Nazara/Physics2D/PhysWorld2D.hpp b/include/Nazara/Physics2D/PhysWorld2D.hpp index 52db98189..4873e5642 100644 --- a/include/Nazara/Physics2D/PhysWorld2D.hpp +++ b/include/Nazara/Physics2D/PhysWorld2D.hpp @@ -49,6 +49,8 @@ namespace Nz bool RaycastQuery(const Nz::Vector2f& from, const Nz::Vector2f& to, float radius, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, std::vector* hitInfos); bool RaycastQueryFirst(const Nz::Vector2f& from, const Nz::Vector2f& to, float radius, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, RaycastHit* hitInfo = nullptr); + void RegionQuery(const Nz::Rectf& boundingBox, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, std::vector* bodies); + void RegisterCallbacks(unsigned int collisionId, const Callback& callbacks); void RegisterCallbacks(unsigned int collisionIdA, unsigned int collisionIdB, const Callback& callbacks); diff --git a/src/Nazara/Lua/LuaInstance.cpp b/src/Nazara/Lua/LuaInstance.cpp index 5e23d0a86..380e7c48f 100644 --- a/src/Nazara/Lua/LuaInstance.cpp +++ b/src/Nazara/Lua/LuaInstance.cpp @@ -143,6 +143,20 @@ namespace Nz luaL_openlibs(m_state); } + LuaInstance::LuaInstance(LuaInstance&& instance) noexcept : + m_memoryLimit(instance.m_memoryLimit), + m_memoryUsage(instance.m_memoryUsage), + m_timeLimit(instance.m_timeLimit), + m_clock(std::move(instance.m_clock)), + m_lastError(std::move(instance.m_lastError)), + m_state(instance.m_state), + m_level(instance.m_level) + { + instance.m_state = nullptr; + + lua_setallocf(m_state, MemoryAllocator, this); + } + LuaInstance::~LuaInstance() { if (m_state) @@ -801,6 +815,24 @@ namespace Nz return luaL_testudata(m_state, index, tname.GetConstBuffer()); } + LuaInstance& LuaInstance::operator=(LuaInstance&& instance) noexcept + { + m_clock = std::move(instance.m_clock); + m_lastError = std::move(instance.m_lastError); + m_level = instance.m_level; + m_memoryLimit = instance.m_memoryLimit; + m_memoryUsage = instance.m_memoryUsage; + m_state = instance.m_state; + m_timeLimit = instance.m_timeLimit; + + instance.m_state = nullptr; + + // Update allocator pointer + lua_setallocf(m_state, MemoryAllocator, this); + + return *this; + } + int LuaInstance::GetIndexOfUpValue(int upValue) { return lua_upvalueindex(upValue); diff --git a/src/Nazara/Physics2D/PhysWorld2D.cpp b/src/Nazara/Physics2D/PhysWorld2D.cpp index e56749d66..0e22ae0af 100644 --- a/src/Nazara/Physics2D/PhysWorld2D.cpp +++ b/src/Nazara/Physics2D/PhysWorld2D.cpp @@ -83,9 +83,11 @@ namespace Nz bool PhysWorld2D::RaycastQuery(const Nz::Vector2f& from, const Nz::Vector2f& to, float radius, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, std::vector* hitInfos) { + using ResultType = decltype(hitInfos); + auto callback = [](cpShape* shape, cpVect point, cpVect normal, cpFloat alpha, void* data) { - std::vector* results = reinterpret_cast*>(data); + ResultType results = static_cast(data); RaycastHit hitInfo; hitInfo.fraction = alpha; @@ -133,6 +135,20 @@ namespace Nz } } + void PhysWorld2D::RegionQuery(const Nz::Rectf& boundingBox, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, std::vector* bodies) + { + using ResultType = decltype(bodies); + + auto callback = [] (cpShape* shape, void* data) + { + ResultType results = static_cast(data); + results->push_back(static_cast(cpShapeGetUserData(shape))); + }; + + cpShapeFilter filter = cpShapeFilterNew(collisionGroup, categoryMask, collisionMask); + cpSpaceBBQuery(m_handle, cpBBNew(boundingBox.x, boundingBox.y + boundingBox.height, boundingBox.x + boundingBox.width, boundingBox.y), filter, callback, bodies); + } + void PhysWorld2D::RegisterCallbacks(unsigned int collisionId, const Callback& callbacks) { InitCallbacks(cpSpaceAddWildcardHandler(m_handle, collisionId), callbacks);