00001 // Filename: fltInstanceRef.cxx 00002 // Created by: drose (30Aug00) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved 00008 // 00009 // All use of this software is subject to the terms of the Panda 3d 00010 // Software license. You should have received a copy of this license 00011 // along with this source code; you will also find a current copy of 00012 // the license at http://www.panda3d.org/license.txt . 00013 // 00014 // To contact the maintainers of this program write to 00015 // panda3d@yahoogroups.com . 00016 // 00017 //////////////////////////////////////////////////////////////////// 00018 00019 #include "fltInstanceRef.h" 00020 #include "fltRecordReader.h" 00021 #include "fltRecordWriter.h" 00022 #include "fltInstanceDefinition.h" 00023 #include "fltHeader.h" 00024 00025 TypeHandle FltInstanceRef::_type_handle; 00026 00027 //////////////////////////////////////////////////////////////////// 00028 // Function: FltInstanceRef::Constructor 00029 // Access: Public 00030 // Description: 00031 //////////////////////////////////////////////////////////////////// 00032 FltInstanceRef:: 00033 FltInstanceRef(FltHeader *header) : FltBead(header) { 00034 _instance_index = 0; 00035 } 00036 00037 //////////////////////////////////////////////////////////////////// 00038 // Function: FltInstanceRef::get_instance 00039 // Access: Public 00040 // Description: Returns the instance subtree referenced by this node, 00041 // or NULL if the reference is invalid. 00042 //////////////////////////////////////////////////////////////////// 00043 FltInstanceDefinition *FltInstanceRef:: 00044 get_instance() const { 00045 return _header->get_instance(_instance_index); 00046 } 00047 00048 //////////////////////////////////////////////////////////////////// 00049 // Function: FltInstanceRef::write 00050 // Access: Public 00051 // Description: Writes a multiple-line description of the record and 00052 // all of its children. This is a human-readable 00053 // description, primarily for debugging; to write a flt 00054 // file, use FltHeader::write_flt(). 00055 //////////////////////////////////////////////////////////////////// 00056 void FltInstanceRef:: 00057 write(ostream &out, int indent_level) const { 00058 indent(out, indent_level) << "instance"; 00059 FltInstanceDefinition *def = _header->get_instance(_instance_index); 00060 if (def != (FltInstanceDefinition *)NULL) { 00061 def->write_children(out, indent_level + 2); 00062 indent(out, indent_level) << "}\n"; 00063 } else { 00064 out << "\n"; 00065 } 00066 } 00067 00068 //////////////////////////////////////////////////////////////////// 00069 // Function: FltInstanceRef::extract_record 00070 // Access: Protected, Virtual 00071 // Description: Fills in the information in this bead based on the 00072 // information given in the indicated datagram, whose 00073 // opcode has already been read. Returns true on 00074 // success, false if the datagram is invalid. 00075 //////////////////////////////////////////////////////////////////// 00076 bool FltInstanceRef:: 00077 extract_record(FltRecordReader &reader) { 00078 if (!FltBead::extract_record(reader)) { 00079 return false; 00080 } 00081 00082 nassertr(reader.get_opcode() == FO_instance_ref, false); 00083 DatagramIterator &iterator = reader.get_iterator(); 00084 00085 iterator.skip_bytes(2); 00086 _instance_index = iterator.get_be_int16(); 00087 00088 check_remaining_size(iterator); 00089 return true; 00090 } 00091 00092 //////////////////////////////////////////////////////////////////// 00093 // Function: FltInstanceRef::write_record_and_children 00094 // Access: Protected, Virtual 00095 // Description: Writes this record out to the flt file, along with all 00096 // of its ancillary records and children records. Returns 00097 // FE_ok on success, or something else on error. 00098 //////////////////////////////////////////////////////////////////// 00099 FltError FltInstanceRef:: 00100 write_record_and_children(FltRecordWriter &writer) const { 00101 // First, make sure our instance definition has already been written. 00102 FltError result = writer.write_instance_def(_header, _instance_index); 00103 if (result != FE_ok) { 00104 return result; 00105 } 00106 00107 // Then write out our own record. 00108 return FltBead::write_record_and_children(writer); 00109 } 00110 00111 //////////////////////////////////////////////////////////////////// 00112 // Function: FltInstanceRef::build_record 00113 // Access: Protected, Virtual 00114 // Description: Fills up the current record on the FltRecordWriter with 00115 // data for this record, but does not advance the 00116 // writer. Returns true on success, false if there is 00117 // some error. 00118 //////////////////////////////////////////////////////////////////// 00119 bool FltInstanceRef:: 00120 build_record(FltRecordWriter &writer) const { 00121 if (!FltBead::build_record(writer)) { 00122 return false; 00123 } 00124 00125 writer.set_opcode(FO_instance_ref); 00126 Datagram &datagram = writer.update_datagram(); 00127 00128 datagram.pad_bytes(2); 00129 datagram.add_be_int16(_instance_index); 00130 00131 return true; 00132 }