diff --git a/CMakeLists.txt b/CMakeLists.txt index 0c63dd0..346d9ea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,7 @@ find_package(Boost 1.35.0 REQUIRED) add_definitions( ${PNG_DEFINITIONS} + -DWITH_VERBOSE_COLLIDER -DWITH_VERBOSE_OBS_MANAGER ) @@ -63,10 +64,12 @@ add_executable(${PROJECT_NAME} src/platformsystem.cpp src/movers/moverworld.cpp src/line.cpp + src/collider.cpp ) target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARIES} physfs ${PNG_LIBRARIES} + pthread ) diff --git a/src/collider.cpp b/src/collider.cpp new file mode 100644 index 0000000..9bb9a17 --- /dev/null +++ b/src/collider.cpp @@ -0,0 +1,101 @@ +/* + Copyright 2014 Michele "King_DuckZ" Santullo + + This file is part of CloonelJump. + + CloonelJump is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + CloonelJump is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with CloonelJump. If not, see . +*/ + +#include "collider.hpp" +#include +#include +#include +#include +#include +#include + +#if defined(WITH_VERBOSE_COLLIDER) && !defined(NDEBUG) +#define VERBOSE_COLLIDER +#include +#endif + +namespace cloonel { + struct Collider::LocalData { + LocalData ( void ); + ~LocalData ( void ) noexcept = default; + + std::thread th; + bool stop; + volatile bool started; + }; + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + Collider::LocalData::LocalData() : + th(), + stop(false), + started(false) + { + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + Collider::Collider() : + m_localData(new LocalData) + { + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + Collider::~Collider() noexcept { + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + void Collider::BeginCollisionChecks() { + if (not m_localData->started) { + m_localData->started = true; + m_localData->th = std::thread(std::bind(&Collider::RunCollisionTests, this)); + } + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + void Collider::StopCollisionChecks() { +#if defined(VERBOSE_COLLIDER) + std::cout << "Stopping collision checks...\n"; +#endif + m_localData->stop = true; + m_localData->th.join(); + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + void Collider::RunCollisionTests() { +#if defined(VERBOSE_COLLIDER) + std::cout << "Collision checks started\n"; +#endif + assert(not m_localData->stop); + do { + } while (not m_localData->stop); + m_localData->stop = false; +#if defined(VERBOSE_COLLIDER) + std::cout << "Collision checks stopped\n"; +#endif + } +} //namespace cloonel + +#if defined(VERBOSE_COLLIDER) +#undef VERBOSE_COLLIDER +#endif diff --git a/src/collider.hpp b/src/collider.hpp new file mode 100644 index 0000000..ac2dcd3 --- /dev/null +++ b/src/collider.hpp @@ -0,0 +1,46 @@ +/* + Copyright 2014 Michele "King_DuckZ" Santullo + + This file is part of CloonelJump. + + CloonelJump is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + CloonelJump is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with CloonelJump. If not, see . +*/ + +#ifndef id7E6024372BF34999A913A36B6EAB736B +#define id7E6024372BF34999A913A36B6EAB736B + +#include "observersmanager.hpp" +#include + +namespace cloonel { + class HorzCollisionBar; + + class Collider { + public: + Collider ( void ); + ~Collider ( void ) noexcept; + + void BeginCollisionChecks ( void ); + void StopCollisionChecks ( void ); + + private: + struct LocalData; + + void RunCollisionTests ( void ); + + const std::unique_ptr m_localData; + }; +} //namespace cloonel + +#endif diff --git a/src/gameplaysceneclassic.cpp b/src/gameplaysceneclassic.cpp index 5ccbfc4..1b1c43a 100644 --- a/src/gameplaysceneclassic.cpp +++ b/src/gameplaysceneclassic.cpp @@ -32,6 +32,7 @@ #include #include #include +#include namespace cloonel { namespace { @@ -96,11 +97,20 @@ namespace cloonel { const float jumpPower = halfRefHeight * 1.29f; m_moverSine->SetPower(jumpPower); + + m_collider.BeginCollisionChecks(); } ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- void GameplaySceneClassic::Destroy() noexcept { + try { + m_collider.StopCollisionChecks(); + } + catch (const std::system_error e) { + std::cerr << "An error occurred while stopping the Collider: " << e.what() << "\n"; + } + //Destroy in reverse creation order m_platforms = std::move(std::unique_ptr(nullptr)); m_wallpaper = std::move(std::unique_ptr(nullptr)); @@ -108,6 +118,7 @@ namespace cloonel { m_moverLeftRight = std::move(std::unique_ptr(nullptr)); m_player = std::move(std::unique_ptr(nullptr)); m_moverSine = std::move(std::unique_ptr(nullptr)); + } ///-------------------------------------------------------------------------- diff --git a/src/gameplaysceneclassic.hpp b/src/gameplaysceneclassic.hpp index 31485f2..b10947c 100644 --- a/src/gameplaysceneclassic.hpp +++ b/src/gameplaysceneclassic.hpp @@ -21,6 +21,7 @@ #define idF6FF1F57C36842DC9B20E2F55C507C2E #include "gameplayscene.hpp" +#include "collider.hpp" #include namespace cloonel { @@ -44,6 +45,7 @@ namespace cloonel { private: virtual void OnPreUpdate ( void ); + Collider m_collider; std::unique_ptr m_player; std::unique_ptr m_moverSine; std::unique_ptr m_moverLeftRight;