Panda3D
 All Classes Functions Variables Enumerations
colorWriteAttrib.cxx
1 // Filename: colorWriteAttrib.cxx
2 // Created by: drose (04Mar02)
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 "colorWriteAttrib.h"
16 #include "graphicsStateGuardianBase.h"
17 #include "dcast.h"
18 #include "bamReader.h"
19 #include "bamWriter.h"
20 #include "datagram.h"
21 #include "datagramIterator.h"
22 
23 TypeHandle ColorWriteAttrib::_type_handle;
24 int ColorWriteAttrib::_attrib_slot;
25 
26 ////////////////////////////////////////////////////////////////////
27 // Function: ColorWriteAttrib::make
28 // Access: Published, Static
29 // Description: Constructs a new ColorWriteAttrib object.
30 ////////////////////////////////////////////////////////////////////
32 make(unsigned int channels) {
33  ColorWriteAttrib *attrib = new ColorWriteAttrib(channels);
34  return return_new(attrib);
35 }
36 
37 ////////////////////////////////////////////////////////////////////
38 // Function: ColorWriteAttrib::make_default
39 // Access: Published, Static
40 // Description: Returns a RenderAttrib that corresponds to whatever
41 // the standard default properties for render attributes
42 // of this type ought to be.
43 ////////////////////////////////////////////////////////////////////
45 make_default() {
46  return return_new(new ColorWriteAttrib);
47 }
48 
49 ////////////////////////////////////////////////////////////////////
50 // Function: ColorWriteAttrib::output
51 // Access: Public, Virtual
52 // Description:
53 ////////////////////////////////////////////////////////////////////
54 void ColorWriteAttrib::
55 output(ostream &out) const {
56  out << get_type() << ":";
57  if (_channels == 0) {
58  out << "off";
59  } else {
60  if ((_channels & C_red) != 0) {
61  out << "r";
62  }
63  if ((_channels & C_green) != 0) {
64  out << "g";
65  }
66  if ((_channels & C_blue) != 0) {
67  out << "b";
68  }
69  if ((_channels & C_alpha) != 0) {
70  out << "a";
71  }
72  }
73 }
74 
75 ////////////////////////////////////////////////////////////////////
76 // Function: ColorWriteAttrib::compare_to_impl
77 // Access: Protected, Virtual
78 // Description: Intended to be overridden by derived ColorWriteAttrib
79 // types to return a unique number indicating whether
80 // this ColorWriteAttrib is equivalent to the other one.
81 //
82 // This should return 0 if the two ColorWriteAttrib objects
83 // are equivalent, a number less than zero if this one
84 // should be sorted before the other one, and a number
85 // greater than zero otherwise.
86 //
87 // This will only be called with two ColorWriteAttrib
88 // objects whose get_type() functions return the same.
89 ////////////////////////////////////////////////////////////////////
90 int ColorWriteAttrib::
91 compare_to_impl(const RenderAttrib *other) const {
92  const ColorWriteAttrib *ta;
93  DCAST_INTO_R(ta, other, 0);
94  return (int)_channels - (int)ta->_channels;
95 }
96 
97 ////////////////////////////////////////////////////////////////////
98 // Function: ColorWriteAttrib::get_hash_impl
99 // Access: Protected, Virtual
100 // Description: Intended to be overridden by derived RenderAttrib
101 // types to return a unique hash for these particular
102 // properties. RenderAttribs that compare the same with
103 // compare_to_impl(), above, should return the same
104 // hash; RenderAttribs that compare differently should
105 // return a different hash.
106 ////////////////////////////////////////////////////////////////////
107 size_t ColorWriteAttrib::
108 get_hash_impl() const {
109  size_t hash = 0;
110  hash = int_hash::add_hash(hash, (int)_channels);
111  return hash;
112 }
113 
114 ////////////////////////////////////////////////////////////////////
115 // Function: ColorWriteAttrib::register_with_read_factory
116 // Access: Public, Static
117 // Description: Tells the BamReader how to create objects of type
118 // ColorWriteAttrib.
119 ////////////////////////////////////////////////////////////////////
122  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
123 }
124 
125 ////////////////////////////////////////////////////////////////////
126 // Function: ColorWriteAttrib::write_datagram
127 // Access: Public, Virtual
128 // Description: Writes the contents of this object to the datagram
129 // for shipping out to a Bam file.
130 ////////////////////////////////////////////////////////////////////
133  RenderAttrib::write_datagram(manager, dg);
134 
135  dg.add_uint8(_channels);
136 }
137 
138 ////////////////////////////////////////////////////////////////////
139 // Function: ColorWriteAttrib::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 ColorWriteAttrib is encountered
143 // in the Bam file. It should create the ColorWriteAttrib
144 // and extract its information from the file.
145 ////////////////////////////////////////////////////////////////////
146 TypedWritable *ColorWriteAttrib::
147 make_from_bam(const FactoryParams &params) {
148  ColorWriteAttrib *attrib = new ColorWriteAttrib;
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: ColorWriteAttrib::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 ColorWriteAttrib.
164 ////////////////////////////////////////////////////////////////////
165 void ColorWriteAttrib::
166 fillin(DatagramIterator &scan, BamReader *manager) {
167  RenderAttrib::fillin(scan, manager);
168 
169  _channels = scan.get_uint8();
170 }
void add_uint8(PN_uint8 value)
Adds an unsigned 8-bit integer to the datagram.
Definition: datagram.I:138
This is the base class for a number of render attributes (other than transform) that may be set on sc...
Definition: renderAttrib.h:60
Enables or disables writing to the color buffer.
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
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:73
PN_uint8 get_uint8()
Extracts an unsigned 8-bit integer.
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
static void register_with_read_factory()
Tells the BamReader how to create objects of type ColorWriteAttrib.
void register_factory(TypeHandle handle, CreateFunc *func)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:90
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:213
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