Panda3D
cullBinAttrib.cxx
1 // Filename: cullBinAttrib.cxx
2 // Created by: drose (01Mar02)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "cullBinAttrib.h"
16 #include "bamReader.h"
17 #include "bamWriter.h"
18 #include "datagram.h"
19 #include "datagramIterator.h"
20 
21 TypeHandle CullBinAttrib::_type_handle;
22 int CullBinAttrib::_attrib_slot;
23 
24 ////////////////////////////////////////////////////////////////////
25 // Function: CullBinAttrib::make
26 // Access: Published, Static
27 // Description: Constructs a new CullBinAttrib assigning geometry
28 // into the named bin. If the bin name is the empty
29 // string, the default bin is used.
30 //
31 // The draw_order specifies further ordering information
32 // which is relevant only to certain kinds of bins (in
33 // particular CullBinFixed type bins).
34 ////////////////////////////////////////////////////////////////////
35 CPT(RenderAttrib) CullBinAttrib::
36 make(const string &bin_name, int draw_order) {
37  CullBinAttrib *attrib = new CullBinAttrib;
38  attrib->_bin_name = bin_name;
39  attrib->_draw_order = draw_order;
40  return return_new(attrib);
41 }
42 
43 ////////////////////////////////////////////////////////////////////
44 // Function: CullBinAttrib::make_default
45 // Access: Published, Static
46 // Description: Returns a RenderAttrib that corresponds to whatever
47 // the standard default properties for render attributes
48 // of this type ought to be.
49 ////////////////////////////////////////////////////////////////////
50 CPT(RenderAttrib) CullBinAttrib::
51 make_default() {
52  return return_new(new CullBinAttrib);
53 }
54 
55 ////////////////////////////////////////////////////////////////////
56 // Function: CullBinAttrib::output
57 // Access: Public, Virtual
58 // Description:
59 ////////////////////////////////////////////////////////////////////
60 void CullBinAttrib::
61 output(ostream &out) const {
62  out << get_type() << ":";
63  if (_bin_name.empty()) {
64  out << "(default)";
65  } else {
66  out << _bin_name << "," << _draw_order;
67  }
68 }
69 
70 ////////////////////////////////////////////////////////////////////
71 // Function: CullBinAttrib::compare_to_impl
72 // Access: Protected, Virtual
73 // Description: Intended to be overridden by derived CullBinAttrib
74 // types to return a unique number indicating whether
75 // this CullBinAttrib is equivalent to the other one.
76 //
77 // This should return 0 if the two CullBinAttrib objects
78 // are equivalent, a number less than zero if this one
79 // should be sorted before the other one, and a number
80 // greater than zero otherwise.
81 //
82 // This will only be called with two CullBinAttrib
83 // objects whose get_type() functions return the same.
84 ////////////////////////////////////////////////////////////////////
85 int CullBinAttrib::
86 compare_to_impl(const RenderAttrib *other) const {
87  const CullBinAttrib *ta;
88  DCAST_INTO_R(ta, other, 0);
89  if (_draw_order != ta->_draw_order) {
90  return _draw_order - ta->_draw_order;
91  }
92  return strcmp(_bin_name.c_str(), ta->_bin_name.c_str());
93 }
94 
95 ////////////////////////////////////////////////////////////////////
96 // Function: CullBinAttrib::get_hash_impl
97 // Access: Protected, Virtual
98 // Description: Intended to be overridden by derived RenderAttrib
99 // types to return a unique hash for these particular
100 // properties. RenderAttribs that compare the same with
101 // compare_to_impl(), above, should return the same
102 // hash; RenderAttribs that compare differently should
103 // return a different hash.
104 ////////////////////////////////////////////////////////////////////
105 size_t CullBinAttrib::
106 get_hash_impl() const {
107  size_t hash = 0;
108  hash = int_hash::add_hash(hash, _draw_order);
109  hash = string_hash::add_hash(hash, _bin_name);
110  return hash;
111 }
112 
113 ////////////////////////////////////////////////////////////////////
114 // Function: CullBinAttrib::register_with_read_factory
115 // Access: Public, Static
116 // Description: Tells the BamReader how to create objects of type
117 // CullBinAttrib.
118 ////////////////////////////////////////////////////////////////////
119 void CullBinAttrib::
121  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
122 }
123 
124 ////////////////////////////////////////////////////////////////////
125 // Function: CullBinAttrib::write_datagram
126 // Access: Public, Virtual
127 // Description: Writes the contents of this object to the datagram
128 // for shipping out to a Bam file.
129 ////////////////////////////////////////////////////////////////////
130 void CullBinAttrib::
132  RenderAttrib::write_datagram(manager, dg);
133 
134  dg.add_string(_bin_name);
135  dg.add_int32(_draw_order);
136 }
137 
138 ////////////////////////////////////////////////////////////////////
139 // Function: CullBinAttrib::make_from_bam
140 // Access: Protected, Static
141 // Description: This function is called by the BamReader's factory
142 // when a new object of type CullBinAttrib is encountered
143 // in the Bam file. It should create the CullBinAttrib
144 // and extract its information from the file.
145 ////////////////////////////////////////////////////////////////////
146 TypedWritable *CullBinAttrib::
147 make_from_bam(const FactoryParams &params) {
148  CullBinAttrib *attrib = new CullBinAttrib;
149  DatagramIterator scan;
150  BamReader *manager;
151 
152  parse_params(params, scan, manager);
153  attrib->fillin(scan, manager);
154 
155  return attrib;
156 }
157 
158 ////////////////////////////////////////////////////////////////////
159 // Function: CullBinAttrib::fillin
160 // Access: Protected
161 // Description: This internal function is called by make_from_bam to
162 // read in all of the relevant data from the BamFile for
163 // the new CullBinAttrib.
164 ////////////////////////////////////////////////////////////////////
165 void CullBinAttrib::
166 fillin(DatagramIterator &scan, BamReader *manager) {
167  RenderAttrib::fillin(scan, manager);
168 
169  _bin_name = scan.get_string();
170  _draw_order = scan.get_int32();
171 }
void add_string(const string &str)
Adds a variable-length string to the datagram.
Definition: datagram.I:351
This is the base class for a number of render attributes (other than transform) that may be set on sc...
Definition: renderAttrib.h:60
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:122
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:37
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:73
PN_int32 get_int32()
Extracts a signed 32-bit integer.
string get_string()
Extracts a variable-length string.
static void register_with_read_factory()
Tells the BamReader how to create objects of type CullBinAttrib.
static size_t add_hash(size_t start, const Key &key)
Adds the indicated key into a running hash.
Definition: stl_compares.I:122
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
void register_factory(TypeHandle handle, CreateFunc *func)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:90
Assigns geometry to a particular bin by name.
Definition: cullBinAttrib.h:30
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:213
void add_int32(PN_int32 value)
Adds a signed 32-bit integer to the datagram.
Definition: datagram.I:159
A class to retrieve the individual data elements previously stored in a Datagram. ...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43
static size_t add_hash(size_t start, const Key &key)
Adds the elements of the indicated key into a running hash.
Definition: stl_compares.I:205
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.