00001 // Filename: colorWriteAttrib.cxx 00002 // Created by: drose (04Mar02) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "colorWriteAttrib.h" 00016 #include "graphicsStateGuardianBase.h" 00017 #include "dcast.h" 00018 #include "bamReader.h" 00019 #include "bamWriter.h" 00020 #include "datagram.h" 00021 #include "datagramIterator.h" 00022 00023 TypeHandle ColorWriteAttrib::_type_handle; 00024 int ColorWriteAttrib::_attrib_slot; 00025 00026 //////////////////////////////////////////////////////////////////// 00027 // Function: ColorWriteAttrib::make 00028 // Access: Published, Static 00029 // Description: Constructs a new ColorWriteAttrib object. 00030 //////////////////////////////////////////////////////////////////// 00031 CPT(RenderAttrib) ColorWriteAttrib:: 00032 make(unsigned int channels) { 00033 ColorWriteAttrib *attrib = new ColorWriteAttrib(channels); 00034 return return_new(attrib); 00035 } 00036 00037 //////////////////////////////////////////////////////////////////// 00038 // Function: ColorWriteAttrib::make_default 00039 // Access: Published, Static 00040 // Description: Returns a RenderAttrib that corresponds to whatever 00041 // the standard default properties for render attributes 00042 // of this type ought to be. 00043 //////////////////////////////////////////////////////////////////// 00044 CPT(RenderAttrib) ColorWriteAttrib:: 00045 make_default() { 00046 return return_new(new ColorWriteAttrib); 00047 } 00048 00049 //////////////////////////////////////////////////////////////////// 00050 // Function: ColorWriteAttrib::output 00051 // Access: Public, Virtual 00052 // Description: 00053 //////////////////////////////////////////////////////////////////// 00054 void ColorWriteAttrib:: 00055 output(ostream &out) const { 00056 out << get_type() << ":"; 00057 if (_channels == 0) { 00058 out << "off"; 00059 } else { 00060 if ((_channels & C_red) != 0) { 00061 out << "r"; 00062 } 00063 if ((_channels & C_green) != 0) { 00064 out << "g"; 00065 } 00066 if ((_channels & C_blue) != 0) { 00067 out << "b"; 00068 } 00069 if ((_channels & C_alpha) != 0) { 00070 out << "a"; 00071 } 00072 } 00073 } 00074 00075 //////////////////////////////////////////////////////////////////// 00076 // Function: ColorWriteAttrib::compare_to_impl 00077 // Access: Protected, Virtual 00078 // Description: Intended to be overridden by derived ColorWriteAttrib 00079 // types to return a unique number indicating whether 00080 // this ColorWriteAttrib is equivalent to the other one. 00081 // 00082 // This should return 0 if the two ColorWriteAttrib objects 00083 // are equivalent, a number less than zero if this one 00084 // should be sorted before the other one, and a number 00085 // greater than zero otherwise. 00086 // 00087 // This will only be called with two ColorWriteAttrib 00088 // objects whose get_type() functions return the same. 00089 //////////////////////////////////////////////////////////////////// 00090 int ColorWriteAttrib:: 00091 compare_to_impl(const RenderAttrib *other) const { 00092 const ColorWriteAttrib *ta; 00093 DCAST_INTO_R(ta, other, 0); 00094 return (int)_channels - (int)ta->_channels; 00095 } 00096 00097 //////////////////////////////////////////////////////////////////// 00098 // Function: ColorWriteAttrib::get_hash_impl 00099 // Access: Protected, Virtual 00100 // Description: Intended to be overridden by derived RenderAttrib 00101 // types to return a unique hash for these particular 00102 // properties. RenderAttribs that compare the same with 00103 // compare_to_impl(), above, should return the same 00104 // hash; RenderAttribs that compare differently should 00105 // return a different hash. 00106 //////////////////////////////////////////////////////////////////// 00107 size_t ColorWriteAttrib:: 00108 get_hash_impl() const { 00109 size_t hash = 0; 00110 hash = int_hash::add_hash(hash, (int)_channels); 00111 return hash; 00112 } 00113 00114 //////////////////////////////////////////////////////////////////// 00115 // Function: ColorWriteAttrib::register_with_read_factory 00116 // Access: Public, Static 00117 // Description: Tells the BamReader how to create objects of type 00118 // ColorWriteAttrib. 00119 //////////////////////////////////////////////////////////////////// 00120 void ColorWriteAttrib:: 00121 register_with_read_factory() { 00122 BamReader::get_factory()->register_factory(get_class_type(), make_from_bam); 00123 } 00124 00125 //////////////////////////////////////////////////////////////////// 00126 // Function: ColorWriteAttrib::write_datagram 00127 // Access: Public, Virtual 00128 // Description: Writes the contents of this object to the datagram 00129 // for shipping out to a Bam file. 00130 //////////////////////////////////////////////////////////////////// 00131 void ColorWriteAttrib:: 00132 write_datagram(BamWriter *manager, Datagram &dg) { 00133 RenderAttrib::write_datagram(manager, dg); 00134 00135 dg.add_uint8(_channels); 00136 } 00137 00138 //////////////////////////////////////////////////////////////////// 00139 // Function: ColorWriteAttrib::make_from_bam 00140 // Access: Protected, Static 00141 // Description: This function is called by the BamReader's factory 00142 // when a new object of type ColorWriteAttrib is encountered 00143 // in the Bam file. It should create the ColorWriteAttrib 00144 // and extract its information from the file. 00145 //////////////////////////////////////////////////////////////////// 00146 TypedWritable *ColorWriteAttrib:: 00147 make_from_bam(const FactoryParams ¶ms) { 00148 ColorWriteAttrib *attrib = new ColorWriteAttrib; 00149 DatagramIterator scan; 00150 BamReader *manager; 00151 00152 parse_params(params, scan, manager); 00153 attrib->fillin(scan, manager); 00154 00155 return attrib; 00156 } 00157 00158 //////////////////////////////////////////////////////////////////// 00159 // Function: ColorWriteAttrib::fillin 00160 // Access: Protected 00161 // Description: This internal function is called by make_from_bam to 00162 // read in all of the relevant data from the BamFile for 00163 // the new ColorWriteAttrib. 00164 //////////////////////////////////////////////////////////////////// 00165 void ColorWriteAttrib:: 00166 fillin(DatagramIterator &scan, BamReader *manager) { 00167 RenderAttrib::fillin(scan, manager); 00168 00169 _channels = scan.get_uint8(); 00170 }