summaryrefslogtreecommitdiffstats
path: root/libdimensionxx/object.cpp
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@gmail.com>2009-07-16 01:16:33 +0000
committerTavian Barnes <tavianator@gmail.com>2009-07-16 01:16:33 +0000
commit6b4dc860466ce4794b346533162291046a6ee96c (patch)
tree2ce3f61711ea6a5688462f605a4e5913e1cb7a4e /libdimensionxx/object.cpp
parentc3619e541564d5133a3ccdaeb79588d37d46a3db (diff)
downloaddimension-6b4dc860466ce4794b346533162291046a6ee96c.tar.xz
New C++ wrapper for dmnsn_texture*.
Diffstat (limited to 'libdimensionxx/object.cpp')
-rw-r--r--libdimensionxx/object.cpp94
1 files changed, 79 insertions, 15 deletions
diff --git a/libdimensionxx/object.cpp b/libdimensionxx/object.cpp
index 407935a..c36a37f 100644
--- a/libdimensionxx/object.cpp
+++ b/libdimensionxx/object.cpp
@@ -22,7 +22,69 @@
namespace Dimension
{
- // Virtual destructor
+ // Construct an intersection object
+ Intersection::Intersection(const Line& ray, double t, const Texture& texture)
+ : m_intersection(new dmnsn_intersection*(dmnsn_new_intersection())),
+ m_ray(ray), m_texture(texture)
+ {
+ dmnsn()->t = t;
+ }
+
+ // Wrap an existing intersection - don't release() one of these
+ Intersection::Intersection(dmnsn_intersection *intersection)
+ : m_intersection(new dmnsn_intersection*(intersection)),
+ m_ray(intersection->ray),
+ m_texture(const_cast<dmnsn_texture*>(intersection->texture))
+ { }
+
+ // Delete an intersection
+ Intersection::~Intersection() {
+ if (m_intersection && m_intersection.unique()) {
+ dmnsn_delete_intersection(dmnsn());
+ }
+ }
+
+ dmnsn_intersection*
+ Intersection::dmnsn()
+ {
+ if (!m_intersection) {
+ throw Dimension_Error("Attempt to access released intersection.");
+ }
+ return *m_intersection;
+ }
+
+ const dmnsn_intersection*
+ Intersection::dmnsn() const
+ {
+ if (!m_intersection) {
+ throw Dimension_Error("Attempt to access released intersection.");
+ }
+ return *m_intersection;
+ }
+
+ dmnsn_intersection*
+ Intersection::release()
+ {
+ if (!m_intersection) {
+ throw Dimension_Error("Attempt to release previously released"
+ " intersection.");
+ }
+
+ if (!m_intersection.unique()) {
+ throw Dimension_Error("Attempt to release non-unique intersection.");
+ }
+
+ dmnsn_intersection* intersection = dmnsn();
+ m_intersection.reset();
+ return intersection;
+ }
+
+ // Manual constructor
+ Object::Object(dmnsn_object* object)
+ : m_object(new dmnsn_object*(object))
+ { }
+
+ // Virtual Object destructor
Object::~Object()
{
if (unique()) {
@@ -45,10 +107,10 @@ namespace Dimension
}
// Intersection list for the line l
- Array<double>
- Object::intersections(const Line& l)
+ Intersection
+ Object::intersection(const Line& l)
{
- return Array<double>((*dmnsn()->intersections_fn)(dmnsn(), l.dmnsn()));
+ return Intersection((*dmnsn()->intersection_fn)(dmnsn(), l.dmnsn()));
}
// Whether the point `point' is inside the object
@@ -69,6 +131,12 @@ namespace Dimension
return *m_object;
}
+ Object*
+ Object::copy() const
+ {
+ return new Object(*this);
+ }
+
// Return a const version of the wrapped canvas
const dmnsn_object*
Object::dmnsn() const
@@ -90,11 +158,6 @@ namespace Dimension
: m_object(object.m_object)
{ }
- // Protected manual constructor
- Object::Object(dmnsn_object* object)
- : m_object(new dmnsn_object*(object))
- { }
-
// Is m_object unique?
bool
Object::unique() const
@@ -110,12 +173,13 @@ namespace Dimension
}
// Custom object callbacks
- namespace {
- dmnsn_array *
- intersections_fn(const dmnsn_object *object, dmnsn_line line)
+ namespace
+ {
+ dmnsn_intersection *
+ intersection_fn(const dmnsn_object *object, dmnsn_line line)
{
Custom_Object* cobject = reinterpret_cast<Custom_Object*>(object->ptr);
- return cobject->intersections(Line(line)).release();
+ return cobject->intersection(Line(line)).release();
}
int
@@ -131,8 +195,8 @@ namespace Dimension
: Object(dmnsn_new_object())
{
dmnsn()->ptr = this;
- dmnsn()->intersections_fn = &intersections_fn;
- dmnsn()->inside_fn = &inside_fn;
+ dmnsn()->intersection_fn = &intersection_fn;
+ dmnsn()->inside_fn = &inside_fn;
}
// Delete the object