A still empty Collider class that runs in a separate thread.

This commit is contained in:
King_DuckZ 2014-07-17 16:54:26 +02:00
parent 250600d8b2
commit eced8388f2
5 changed files with 163 additions and 0 deletions

View file

@ -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
)

101
src/collider.cpp Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#include "collider.hpp"
#include <thread>
#include <atomic>
#include <vector>
#include <functional>
#include <ciso646>
#include <cassert>
#if defined(WITH_VERBOSE_COLLIDER) && !defined(NDEBUG)
#define VERBOSE_COLLIDER
#include <iostream>
#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

46
src/collider.hpp Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef id7E6024372BF34999A913A36B6EAB736B
#define id7E6024372BF34999A913A36B6EAB736B
#include "observersmanager.hpp"
#include <memory>
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<LocalData> m_localData;
};
} //namespace cloonel
#endif

View file

@ -32,6 +32,7 @@
#include <algorithm>
#include <SDL2/SDL_scancode.h>
#include <ciso646>
#include <system_error>
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<PlatformSystem>(nullptr));
m_wallpaper = std::move(std::unique_ptr<TiledWallpaper>(nullptr));
@ -108,6 +118,7 @@ namespace cloonel {
m_moverLeftRight = std::move(std::unique_ptr<MoverLeftRight>(nullptr));
m_player = std::move(std::unique_ptr<Character>(nullptr));
m_moverSine = std::move(std::unique_ptr<MoverSine>(nullptr));
}
///--------------------------------------------------------------------------

View file

@ -21,6 +21,7 @@
#define idF6FF1F57C36842DC9B20E2F55C507C2E
#include "gameplayscene.hpp"
#include "collider.hpp"
#include <memory>
namespace cloonel {
@ -44,6 +45,7 @@ namespace cloonel {
private:
virtual void OnPreUpdate ( void );
Collider m_collider;
std::unique_ptr<Character> m_player;
std::unique_ptr<MoverSine> m_moverSine;
std::unique_ptr<MoverLeftRight> m_moverLeftRight;