Panda3D
transparencyAttrib.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 transparencyAttrib.cxx
10  * @author drose
11  * @date 2002-02-28
12  */
13 
14 #include "transparencyAttrib.h"
16 #include "dcast.h"
17 #include "bamReader.h"
18 #include "bamWriter.h"
19 #include "datagram.h"
20 #include "datagramIterator.h"
21 
22 TypeHandle TransparencyAttrib::_type_handle;
23 int TransparencyAttrib::_attrib_slot;
24 
25 /**
26  * Constructs a new TransparencyAttrib object.
27  */
28 CPT(RenderAttrib) TransparencyAttrib::
29 make(TransparencyAttrib::Mode mode) {
30  TransparencyAttrib *attrib = new TransparencyAttrib(mode);
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) TransparencyAttrib::
39 make_default() {
40  return return_new(new TransparencyAttrib);
41 }
42 
43 /**
44  *
45  */
46 void TransparencyAttrib::
47 output(std::ostream &out) const {
48  out << get_type() << ":";
49  switch (get_mode()) {
50  case M_none:
51  out << "none";
52  break;
53 
54  case M_alpha:
55  out << "alpha";
56  break;
57 
58  case M_premultiplied_alpha:
59  out << "premultiplied alpha";
60  break;
61 
62  case M_multisample:
63  out << "multisample";
64  break;
65 
66  case M_multisample_mask:
67  out << "multisample mask";
68  break;
69 
70  case M_binary:
71  out << "binary";
72  break;
73 
74  case M_dual:
75  out << "dual";
76  break;
77  }
78 }
79 
80 /**
81  * Intended to be overridden by derived TransparencyAttrib types to return a
82  * unique number indicating whether this TransparencyAttrib is equivalent to
83  * the other one.
84  *
85  * This should return 0 if the two TransparencyAttrib objects are equivalent,
86  * a number less than zero if this one should be sorted before the other one,
87  * and a number greater than zero otherwise.
88  *
89  * This will only be called with two TransparencyAttrib objects whose
90  * get_type() functions return the same.
91  */
92 int TransparencyAttrib::
93 compare_to_impl(const RenderAttrib *other) const {
94  const TransparencyAttrib *ta = (const TransparencyAttrib *)other;
95 
96  return (int)_mode - (int)ta->_mode;
97 }
98 
99 /**
100  * Intended to be overridden by derived RenderAttrib types to return a unique
101  * hash for these particular properties. RenderAttribs that compare the same
102  * with compare_to_impl(), above, should return the same hash; RenderAttribs
103  * that compare differently should return a different hash.
104  */
105 size_t TransparencyAttrib::
106 get_hash_impl() const {
107  size_t hash = 0;
108  hash = int_hash::add_hash(hash, (int)_mode);
109  return hash;
110 }
111 
112 /**
113  * Tells the BamReader how to create objects of type TransparencyAttrib.
114  */
117  BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
118 }
119 
120 /**
121  * Writes the contents of this object to the datagram for shipping out to a
122  * Bam file.
123  */
125 write_datagram(BamWriter *manager, Datagram &dg) {
126  RenderAttrib::write_datagram(manager, dg);
127 
128  dg.add_int8(_mode);
129 }
130 
131 /**
132  * This function is called by the BamReader's factory when a new object of
133  * type TransparencyAttrib is encountered in the Bam file. It should create
134  * the TransparencyAttrib and extract its information from the file.
135  */
136 TypedWritable *TransparencyAttrib::
137 make_from_bam(const FactoryParams &params) {
139  DatagramIterator scan;
140  BamReader *manager;
141 
142  parse_params(params, scan, manager);
143  attrib->fillin(scan, manager);
144 
145  return attrib;
146 }
147 
148 /**
149  * This internal function is called by make_from_bam to read in all of the
150  * relevant data from the BamFile for the new TransparencyAttrib.
151  */
152 void TransparencyAttrib::
153 fillin(DatagramIterator &scan, BamReader *manager) {
154  RenderAttrib::fillin(scan, manager);
155 
156  _mode = (Mode)scan.get_int8();
157 }
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
A class to retrieve the individual data elements previously stored in a Datagram.
int8_t get_int8()
Extracts a signed 8-bit integer.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
void add_int8(int8_t value)
Adds a signed 8-bit integer to the datagram.
Definition: datagram.I:42
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
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
This is the base class for a number of render attributes (other than transform) that may be set on sc...
Definition: renderAttrib.h:51
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 controls the enabling of transparency.
static void register_with_read_factory()
Tells the BamReader how to create objects of type TransparencyAttrib.
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
get_mode
Returns the transparency mode.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
static size_t add_hash(size_t start, const Key &key)
Adds the indicated key into a running hash.
Definition: stl_compares.I:101
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
CPT(RenderAttrib) TransparencyAttrib
Constructs a new TransparencyAttrib object.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.