Panda3D

Loading...
Searching...
No Matches
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"
15#include "graphicsStateGuardianBase.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 */
28CPT(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 */
38CPT(RenderAttrib) ColorWriteAttrib::
39 make_default() {
40 return return_new(new ColorWriteAttrib);
41}
42
43/**
44 *
45 */
46 void ColorWriteAttrib::
47output(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::
80compare_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::
92get_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 */
101 void ColorWriteAttrib::
102 register_with_read_factory() {
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 */
110 void ColorWriteAttrib::
111 write_datagram(BamWriter *manager, Datagram &dg) {
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::
123make_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::
139fillin(DatagramIterator &scan, BamReader *manager) {
140 RenderAttrib::fillin(scan, manager);
141
142 _channels = scan.get_uint8();
143}
bamReader.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
parse_params
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
bamWriter.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
BamReader
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition bamReader.h:110
BamReader::get_factory
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition bamReader.I:177
ColorWriteAttrib
Enables or disables writing to the color buffer.
Definition colorWriteAttrib.h:28
ColorWriteAttrib::make_default
static ConstPointerTo< RenderAttrib > make_default()
Returns a RenderAttrib that corresponds to whatever the standard default properties for render attrib...
Definition colorWriteAttrib.cxx:39
ColorWriteAttrib::register_with_read_factory
static void register_with_read_factory()
Tells the BamReader how to create objects of type ColorWriteAttrib.
Definition colorWriteAttrib.cxx:102
ColorWriteAttrib::write_datagram
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
Definition colorWriteAttrib.cxx:111
ColorWriteAttrib::make
static ConstPointerTo< RenderAttrib > make(unsigned int channels)
Constructs a new ColorWriteAttrib object.
Definition colorWriteAttrib.cxx:29
DatagramIterator
A class to retrieve the individual data elements previously stored in a Datagram.
Definition datagramIterator.h:30
DatagramIterator::get_uint8
uint8_t get_uint8()
Extracts an unsigned 8-bit integer.
Definition datagramIterator.I:72
Datagram
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition datagram.h:38
Datagram::add_uint8
void add_uint8(uint8_t value)
Adds an unsigned 8-bit integer to the datagram.
Definition datagram.I:50
FactoryParams
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition factoryParams.h:36
Factory::register_factory
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
RenderAttrib
This is the base class for a number of render attributes (other than transform) that may be set on sc...
Definition renderAttrib.h:51
RenderAttrib::write_datagram
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
Definition renderAttrib.cxx:527
TypeHandle
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
TypedWritable
Base class for objects that can be written to and read from Bam files.
Definition typedWritable.h:35
integer_hash< int >::add_hash
static size_t add_hash(size_t start, const int &key)
colorWriteAttrib.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
datagramIterator.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
datagram.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
dcast.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
graphicsStateGuardianBase.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.