Panda3D
colorWriteAttrib.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file colorWriteAttrib.cxx
10  * @author drose
11  * @date 2002-03-04
12  */
13 
14 #include "colorWriteAttrib.h"
16 #include "dcast.h"
17 #include "bamReader.h"
18 #include "bamWriter.h"
19 #include "datagram.h"
20 #include "datagramIterator.h"
21 
22 TypeHandle ColorWriteAttrib::_type_handle;
23 int ColorWriteAttrib::_attrib_slot;
24 
25 /**
26  * Constructs a new ColorWriteAttrib object.
27  */
28 CPT(RenderAttrib) ColorWriteAttrib::
29 make(unsigned int channels) {
30  ColorWriteAttrib *attrib = new ColorWriteAttrib(channels);
31  return return_new(attrib);
32 }
33 
34 /**
35  * Returns a RenderAttrib that corresponds to whatever the standard default
36  * properties for render attributes of this type ought to be.
37  */
38 CPT(RenderAttrib) ColorWriteAttrib::
39 make_default() {
40  return return_new(new ColorWriteAttrib);
41 }
42 
43 /**
44  *
45  */
46 void ColorWriteAttrib::
47 output(std::ostream &out) const {
48  out << get_type() << ":";
49  if (_channels == 0) {
50  out << "off";
51  } else {
52  if ((_channels & C_red) != 0) {
53  out << "r";
54  }
55  if ((_channels & C_green) != 0) {
56  out << "g";
57  }
58  if ((_channels & C_blue) != 0) {
59  out << "b";
60  }
61  if ((_channels & C_alpha) != 0) {
62  out << "a";
63  }
64  }
65 }
66 
67 /**
68  * Intended to be overridden by derived ColorWriteAttrib types to return a
69  * unique number indicating whether this ColorWriteAttrib is equivalent to the
70  * other one.
71  *
72  * This should return 0 if the two ColorWriteAttrib objects are equivalent, a
73  * number less than zero if this one should be sorted before the other one,
74  * and a number greater than zero otherwise.
75  *
76  * This will only be called with two ColorWriteAttrib objects whose get_type()
77  * functions return the same.
78  */
79 int ColorWriteAttrib::
80 compare_to_impl(const RenderAttrib *other) const {
81  const ColorWriteAttrib *ta = (const ColorWriteAttrib *)other;
82  return (int)_channels - (int)ta->_channels;
83 }
84 
85 /**
86  * Intended to be overridden by derived RenderAttrib types to return a unique
87  * hash for these particular properties. RenderAttribs that compare the same
88  * with compare_to_impl(), above, should return the same hash; RenderAttribs
89  * that compare differently should return a different hash.
90  */
91 size_t ColorWriteAttrib::
92 get_hash_impl() const {
93  size_t hash = 0;
94  hash = int_hash::add_hash(hash, (int)_channels);
95  return hash;
96 }
97 
98 /**
99  * Tells the BamReader how to create objects of type ColorWriteAttrib.
100  */
103  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
104 }
105 
106 /**
107  * Writes the contents of this object to the datagram for shipping out to a
108  * Bam file.
109  */
112  RenderAttrib::write_datagram(manager, dg);
113 
114  dg.add_uint8(_channels);
115 }
116 
117 /**
118  * This function is called by the BamReader's factory when a new object of
119  * type ColorWriteAttrib is encountered in the Bam file. It should create the
120  * ColorWriteAttrib and extract its information from the file.
121  */
122 TypedWritable *ColorWriteAttrib::
123 make_from_bam(const FactoryParams &params) {
124  ColorWriteAttrib *attrib = new ColorWriteAttrib;
125  DatagramIterator scan;
126  BamReader *manager;
127 
128  parse_params(params, scan, manager);
129  attrib->fillin(scan, manager);
130 
131  return attrib;
132 }
133 
134 /**
135  * This internal function is called by make_from_bam to read in all of the
136  * relevant data from the BamFile for the new ColorWriteAttrib.
137  */
138 void ColorWriteAttrib::
139 fillin(DatagramIterator &scan, BamReader *manager) {
140  RenderAttrib::fillin(scan, manager);
141 
142  _channels = scan.get_uint8();
143 }
This is the base class for a number of render attributes (other than transform) that may be set on sc...
Definition: renderAttrib.h:51
uint8_t get_uint8()
Extracts an unsigned 8-bit integer.
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:110
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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:63
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
static size_t add_hash(size_t start, const Key &key)
Adds the indicated key into a running hash.
Definition: stl_compares.I:101
void parse_params(const FactoryParams &params, DatagramIterator &scan, BamReader *&manager)
Takes in a FactoryParams, passed from a WritableFactory into any TypedWritable's make function,...
Definition: bamReader.I:275
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
CPT(RenderAttrib) ColorWriteAttrib
Constructs a new ColorWriteAttrib object.
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void register_factory(TypeHandle handle, CreateFunc *func, void *user_data=nullptr)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:73
static void register_with_read_factory()
Tells the BamReader how to create objects of type ColorWriteAttrib.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
void add_uint8(uint8_t value)
Adds an unsigned 8-bit integer to the datagram.
Definition: datagram.I:50
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:81
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.