Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / QuadTree.hpp
Last active December 15, 2015 12:08
Bare-bones QuadTree class.
// See this here: https://gist.github.com/Ruskiy69/5257421
#include "gists/QTreeNode.hpp"
class CQuadTree
{
public:
// Provide the starting dimensions to the root node.
// This will likely be something like Window::GetWidth()
// and Window::GetHeight() in a game.
CQuadTree(const uint16_t w, const uint16_t h);
@Shaptic
Shaptic / QTreeNode.hpp
Last active December 15, 2015 11:59
Bare-bones quad tree node structure.
#include <list>
#include <cstdint>
// A generic object class (supporting collision) that is within the nodes.
// See here: https://github.com/Ruskiy69/IronClad/blob/master/Source/include/Entity/RigidBody.hpp
// For a real definition of the objects I use.
class CQuadTree;
class CEntity;
// This is a rectangle representation containing x, y position, and
@Shaptic
Shaptic / Animation.cpp
Created February 10, 2013 22:31
Swap out sprite sheets on the fly.
void CAnimation::SwapSpriteSheet(const AnimationHeader& Header)
{
// New texture for the renderer and new dimensions for the shader.
m_Mesh.GetSurfaces()[0]->pMaterial->pTexture = Header.pTexture;
m_TexcDim = math::vector2_t(1.f / Header.columns, 1.f / Header.rows);
m_SheetDetails = Header;
m_loops_done = 0;
// Adjust shader for new texture dimensions.
gfx::CShaderPair* pShader = m_Mesh.GetSurfaces()[0]->pMaterial->pShader;
@Shaptic
Shaptic / Animation.cpp
Created February 10, 2013 22:23
Iterating over the current sprite sheet.
bool CAnimation::NextSprite()
{
// Stay within bounds!
if(++m_active >= this->GetAnimationCount())
{
++m_loops_done;
m_active = 0;
}
// Adjust for current texture
@Shaptic
Shaptic / Animate.fs
Last active December 12, 2015 09:28
#version 330 core
uniform sampler2D texture;
uniform vec2 tc_offset;
uniform vec2 tc_start;
smooth in vec2 fs_texc;
smooth in vec4 fs_color;
out vec4 out_color;