Skip to content

Commit

Permalink
Use separate classes to handle object positions and nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
kcat committed Aug 2, 2015
1 parent 997071f commit 20a66a5
Show file tree
Hide file tree
Showing 12 changed files with 465 additions and 227 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ set(SRCS components/sdlutil/graphicswindow.cpp
components/dfosg/texloader.cpp
components/dfosg/meshloader.cpp
src/input/input.cpp
src/render/renderer.cpp
src/gui/gui.cpp
src/class/placeable.cpp
src/world/world.cpp
src/world/pitems.cpp
src/world/ditems.cpp
Expand Down Expand Up @@ -66,8 +68,10 @@ set(HDRS misc/sparsearray.hpp
components/dfosg/texloader.hpp
components/dfosg/meshloader.hpp
src/input/input.hpp
src/render/renderer.hpp
src/gui/iface.hpp
src/gui/gui.hpp
src/class/placeable.hpp
src/world/iface.hpp
src/world/world.hpp
src/world/itembase.hpp
Expand Down
12 changes: 9 additions & 3 deletions misc/sparsearray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define MISC_SPARSEARRAY_HPP

#include <vector>
#include <cstddef>


namespace Misc
{
Expand Down Expand Up @@ -89,15 +91,19 @@ class SparseArray {
typedef typename datalist_type::reverse_iterator reverse_iterator;
typedef typename datalist_type::const_reverse_iterator const_reverse_iterator;

SparseArray() { }
~SparseArray() { }
SparseArray() = default;
SparseArray(SparseArray<value_type>&&) = default;
SparseArray<value_type>& operator=(SparseArray<value_type>&&) = default;

void reserve(size_t size)
{
mIdxLookup.reserve(size);
mData.reserve(size);
}

bool empty() const { return mData.empty(); }
size_t size() const { return mData.size(); }

bool exists(size_t idx) const
{ return lookupKey(idx) != mIdxLookup.end(); }

Expand Down Expand Up @@ -150,7 +156,7 @@ class SparseArray {
if(iter == mIdxLookup.cend())
return;

mData.erase(std::distance(mIdxLookup.cbegin(), iter));
mData.erase(std::next(mData.cbegin(), std::distance(mIdxLookup.cbegin(), iter)));
mIdxLookup.erase(iter);
}

Expand Down
49 changes: 49 additions & 0 deletions src/class/placeable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

#include "placeable.hpp"

#include "render/renderer.hpp"


namespace DF
{

Placeable Placeable::sPlaceables;


void Placeable::deallocate(const size_t *ids, size_t count)
{
for(size_t i = 0;i < count;++i)
mPositions.erase(ids[i]);
}


void Placeable::setPos(size_t idx, const osg::Vec3f &pt, const osg::Vec3f &rot)
{
Position &pos = mPositions[idx];
pos.mRotation = rot;
pos.mPoint = pt;
Renderer::get().markDirty(idx, pos);
}

void Placeable::setRotate(size_t idx, const osg::Vec3f &rot)
{
Position &pos = mPositions[idx];
pos.mRotation = rot;
Renderer::get().markDirty(idx, pos);
}

void Placeable::setPoint(size_t idx, const osg::Vec3f &pt)
{
Position &pos = mPositions[idx];
pos.mPoint = pt;
Renderer::get().markDirty(idx, pos);
}

void Placeable::setLocalRot(size_t idx, const osg::Vec3f &rot)
{
Position &pos = mPositions[idx];
pos.mLocalRotation = rot;
Renderer::get().markDirty(idx, pos);
}

} // namespace DF
43 changes: 43 additions & 0 deletions src/class/placeable.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef CLASS_PLACEABLE_HPP
#define CLASS_PLACEABLE_HPP

#include <osg/Vec3>

#include "misc/sparsearray.hpp"


namespace DF
{

// Should probably be somewhere else...
struct Position {
osg::Vec3f mRotation;
osg::Vec3f mPoint;
osg::Vec3f mLocalRotation;
};

class Placeable {
static Placeable sPlaceables;

Misc::SparseArray<Position> mPositions;

public:
void deallocate(const size_t *ids, size_t count);
void deallocate(size_t idx) { return deallocate(&idx, 1); }

void setPos(size_t idx, const osg::Vec3f &pt, const osg::Vec3f &rot);
void setRotate(size_t idx, const osg::Vec3f &rot);
void setPoint(size_t idx, const osg::Vec3f &pt);
void setLocalRot(size_t idx, const osg::Vec3f &rot);

const Position &getPos(size_t idx)
{
return mPositions[idx];
};

static Placeable &get() { return sPlaceables; }
};

} // namespace DF

#endif /* CLASS_PLACEABLE_HPP */
62 changes: 62 additions & 0 deletions src/render/renderer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

#include "renderer.hpp"

#include <osg/MatrixTransform>

#include "class/placeable.hpp"


namespace DF
{

Renderer Renderer::sRenderer;


void Renderer::setNode(size_t idx, osg::MatrixTransform *node)
{
mBaseNodes[idx] = node;
}

void Renderer::remove(const size_t *ids, size_t count)
{
for(size_t i = 0;i < count;++i)
mBaseNodes.erase(ids[i]);
}


void Renderer::markDirty(size_t idx, const Position &pos)
{
if(mBaseNodes.exists(idx))
mDirtyNodes.push({mBaseNodes[idx], pos});
}

void Renderer::update()
{
while(!mDirtyNodes.empty())
{
const NodePosPair &nodepos = mDirtyNodes.top();

const Position &pos = nodepos.mPosition;
osg::Matrix mat;
mat.makeRotate(
pos.mRotation.x()*3.14159f/1024.0f, osg::Vec3f(1.0f, 0.0f, 0.0f),
-pos.mRotation.y()*3.14159f/1024.0f, osg::Vec3f(0.0f, 1.0f, 0.0f),
pos.mRotation.z()*3.14159f/1024.0f, osg::Vec3f(0.0f, 0.0f, 1.0f)
);
mat.postMultTranslate(osg::Vec3(pos.mPoint.x(), pos.mPoint.y(), pos.mPoint.z()));
mat.postMultRotate(osg::Quat(
pos.mLocalRotation.x()*3.14159f/1024.0f, osg::Vec3f(1.0f, 0.0f, 0.0f),
-pos.mLocalRotation.y()*3.14159f/1024.0f, osg::Vec3f(0.0f, 1.0f, 0.0f),
pos.mLocalRotation.z()*3.14159f/1024.0f, osg::Vec3f(0.0f, 0.0f, 1.0f)
));

osg::MatrixTransform *node = nodepos.mNode;
//node->setDataVariance(osg::Node::DYNAMIC);
node->setMatrix(mat);

mDirtyNodes.pop();
}
}


} // namespace DF
49 changes: 49 additions & 0 deletions src/render/renderer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#ifndef RENDER_RENDERER_HPP
#define RENDER_RENDERER_HPP

#include <queue>

#include <osg/ref_ptr>

#include "misc/sparsearray.hpp"

#include "class/placeable.hpp"


namespace osg
{
class MatrixTransform;
}

namespace DF
{

struct NodePosPair {
osg::ref_ptr<osg::MatrixTransform> mNode;
Position mPosition;

bool operator<(const NodePosPair &rhs) const
{ return mNode < rhs.mNode; }
};

class Renderer {
static Renderer sRenderer;

Misc::SparseArray<osg::ref_ptr<osg::MatrixTransform>> mBaseNodes;
std::priority_queue<NodePosPair> mDirtyNodes;

public:
void setNode(size_t idx, osg::MatrixTransform *node);

void remove(const size_t *ids, size_t count);

void markDirty(size_t idx, const Position &pos);

void update();

static Renderer &get() { return sRenderer; }
};

} // namespace DF

#endif /* RENDER_RENDERER_HPP */
Loading

0 comments on commit 20a66a5

Please sign in to comment.