@@ -47,6 +47,44 @@ class CompareLightPriorities {
4747 }
4848};
4949
50+ /* *
51+ * Use LightAttrib::make() to construct a new LightAttrib object. The copy
52+ * constructor is only defined to facilitate methods like add_on_light().
53+ */
54+ LightAttrib::
55+ LightAttrib (const LightAttrib ©) :
56+ _on_lights(copy._on_lights),
57+ _off_lights(copy._off_lights),
58+ _off_all_lights(copy._off_all_lights),
59+ _sort_seq(UpdateSeq::old())
60+ {
61+ // Increase the attrib_ref of all the lights in this attribute.
62+ Lights::const_iterator it;
63+ for (it = _on_lights.begin (); it != _on_lights.end (); ++it) {
64+ Light *lobj = (*it).node ()->as_light ();
65+ nassertd (lobj != nullptr ) continue ;
66+ lobj->attrib_ref ();
67+ }
68+ }
69+
70+ /* *
71+ * Destructor.
72+ */
73+ LightAttrib::
74+ ~LightAttrib () {
75+ // Call attrib_unref() on all on lights.
76+ Lights::const_iterator it;
77+ for (it = _on_lights.begin (); it != _on_lights.end (); ++it) {
78+ const NodePath &np = *it;
79+ if (!np.is_empty ()) {
80+ Light *lobj = np.node ()->as_light ();
81+ if (lobj != nullptr ) {
82+ lobj->attrib_unref ();
83+ }
84+ }
85+ }
86+ }
87+
5088/* *
5189 * Constructs a new LightAttrib object that turns on (or off, according to op)
5290 * the indicated light(s).
@@ -376,14 +414,17 @@ make_all_off() {
376414 */
377415CPT (RenderAttrib) LightAttrib::
378416add_on_light(const NodePath &light) const {
379- nassertr (!light.is_empty () && light.node ()->as_light () != (Light *)NULL , this );
417+ nassertr (!light.is_empty (), this );
418+ Light *lobj = light.node ()->as_light ();
419+ nassertr (lobj != nullptr , this );
420+
380421 LightAttrib *attrib = new LightAttrib (*this );
381- attrib->_on_lights .insert (light);
382- attrib->_off_lights .erase (light);
383422
384423 pair<Lights::iterator, bool > insert_result =
385424 attrib->_on_lights .insert (Lights::value_type (light));
386425 if (insert_result.second ) {
426+ lobj->attrib_ref ();
427+
387428 // Also ensure it is removed from the off_lights list.
388429 attrib->_off_lights .erase (light);
389430 }
@@ -397,9 +438,14 @@ add_on_light(const NodePath &light) const {
397438 */
398439CPT (RenderAttrib) LightAttrib::
399440remove_on_light(const NodePath &light) const {
400- nassertr (!light.is_empty () && light.node ()->as_light () != (Light *)NULL , this );
441+ nassertr (!light.is_empty (), this );
442+ Light *lobj = light.node ()->as_light ();
443+ nassertr (lobj != nullptr , this );
444+
401445 LightAttrib *attrib = new LightAttrib (*this );
402- attrib->_on_lights .erase (light);
446+ if (attrib->_on_lights .erase (light)) {
447+ lobj->attrib_unref ();
448+ }
403449 return return_new (attrib);
404450}
405451
@@ -409,12 +455,17 @@ remove_on_light(const NodePath &light) const {
409455 */
410456CPT (RenderAttrib) LightAttrib::
411457add_off_light(const NodePath &light) const {
412- nassertr (!light.is_empty () && light.node ()->as_light () != (Light *)NULL , this );
458+ nassertr (!light.is_empty (), this );
459+ Light *lobj = light.node ()->as_light ();
460+ nassertr (lobj != nullptr , this );
461+
413462 LightAttrib *attrib = new LightAttrib (*this );
414463 if (!_off_all_lights) {
415464 attrib->_off_lights .insert (light);
416465 }
417- attrib->_on_lights .erase (light);
466+ if (attrib->_on_lights .erase (light)) {
467+ lobj->attrib_unref ();
468+ }
418469 return return_new (attrib);
419470}
420471
@@ -960,6 +1011,10 @@ finalize(BamReader *manager) {
9601011 // If it's in the registry, replace it.
9611012 _on_lights[i] = areg->get_node (n);
9621013 }
1014+
1015+ Light *lobj = _on_lights[i].node ()->as_light ();
1016+ nassertd (lobj != nullptr ) continue ;
1017+ lobj->attrib_ref ();
9631018 }
9641019
9651020 } else {
@@ -992,10 +1047,15 @@ finalize(BamReader *manager) {
9921047 if (n != -1 ) {
9931048 // If it's in the registry, add that NodePath.
9941049 _on_lights.push_back (areg->get_node (n));
1050+ node = _on_lights.back ().node ();
9951051 } else {
9961052 // Otherwise, add any arbitrary NodePath. Complain if it's ambiguous.
9971053 _on_lights.push_back (NodePath (node));
9981054 }
1055+
1056+ Light *lobj = node->as_light ();
1057+ nassertd (lobj != nullptr ) continue ;
1058+ lobj->attrib_ref ();
9991059 }
10001060 }
10011061
0 commit comments