Skip to content

Instantly share code, notes, and snippets.

@Shaptic
Shaptic / QuadTreeSplit.hpp
Last active December 15, 2015 12:09
CQuadTree::Split()
void CQuadTree::Split(QTNode* pNode)
{
// Children already exist? This shouldn't happen.
if(pNode->pChildren != nullptr)
{
// Delete the actual nodes.
for(size_t i = 0; i < 4; ++i) delete pNode->pChildren[i];
// Then delete the pointer array to the nodes.
delete[] pNode->pChildren;
@Shaptic
Shaptic / QuadTreeInsert.cpp
Last active December 15, 2015 12:09
CQuadTree::RInsert()
// The one used by the public, all it does is start
// the recursive algorithm with the root.
bool CQuadTree::Insert(CEntity* pEnt)
{
// If inserted, we want to add to the whole internal list of
// objects, which is used later in CQuadTree::Update().
if(this->RInsert(pBody, &m_Root))
{
mp_allBodies.push_back(pBody);
return true;
@Shaptic
Shaptic / QuadTreeRemove.cpp
Last active December 15, 2015 12:09
CQuadTree::RRemove()
bool CQuadTree::RRemove(CEntity* pEnt, QTNode* pStart)
{
// Leaf node?
if(pStart->pChildren == nullptr)
{
// Iterate over the entities and remove a match (if any).
for(auto i : pStart->nodeObjects)
{
// Is this the object we are looking for?
if(*i == pEnt)
@Shaptic
Shaptic / QuadTreeCollides.cpp
Last active December 15, 2015 12:09
CQuadTree::RCollides()
CEntity* CQuadTree::RCollides(const CEnity* pBody,
const QTNode* pStart) const
{
// Is leaf?
if(pStart->pChildren == nullptr)
{
// Iterate over each object in the node and check for
// collision. We also compare the objects to make sure
// we don't count a collision of an object with itself,
// which would obviously make no sense.
@Shaptic
Shaptic / QuadTreeUpdate.cpp
Last active December 15, 2015 12:18
CQuadTree::Update()
void CQuadTree::Update()
{
// Iterate over every object in the tree.
// This is why we stored them locally in Insert()
for(size_t i = 0; i < mp_allBodies.size(); ++i)
{
// Object has moved.
if(mp_allBodies[i]->needs_update)
{
// Remove the reference to the object from its current node.
@Shaptic
Shaptic / LevelSplit.cpp
Last active December 15, 2015 12:28
Split an IronClad level into accessible tiles for AI path-finding.
#include "IronClad/Math/Math.hpp"
#include "IronClad/Entity/RigidBody.hpp"
using ic::math;
using ic::obj;
// Splits level into accessible 32x32 rectangles.
std::vector<rect_t> partition(const ic::CLevel& Level)
{
const std::vector<CRigidBody*>& allEntities =
@Shaptic
Shaptic / QTreeTest.cpp
Last active December 18, 2015 05:49
Testing IronClad quad tree insertion of 1000000 objects and collision with one.
#include <sys/timeb.h>
#include "IronClad.hpp"
using namespace ic;
// For tracking time.
uint32_t now()
{
timeb t;
ftime(&t);
@Shaptic
Shaptic / Option.hpp
Last active December 18, 2015 19:39
Generic option class that can act as any 'plain-old-data' type.
#include <string>
#include <ostream>
// This is in a different header, but we include it here
// for completeness-sake.
namespace math
{
static inline
bool compf(const float a, const float b,
const float threshold = 0.0001)
@Shaptic
Shaptic / Option.cpp
Created June 21, 2013 21:44
Implementation of the header gist.
#include "Option.hpp"
COption::COption(){}
COption::COption(const string_t& value) :
m_value(value) {}
COption::COption(const COption& Opt) :
m_value(Opt.m_value) {}
@Shaptic
Shaptic / Settings.cpp
Created June 21, 2013 22:26
operator[] of the Settings module.
// In the CSettings.hpp class definition:
std::map<
#ifndef _DEBUG
uint32_t,
#else
string_t,
#endif // _DEBUG
COption> m_Options;
COption& CSettings::operator[](const string_t& opt)