Panda3D
 All Classes Functions Variables Enumerations
fltVertexList.cxx
1 // Filename: fltVertexList.cxx
2 // Created by: drose (25Aug00)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "fltVertexList.h"
16 #include "fltRecordReader.h"
17 #include "fltRecordWriter.h"
18 #include "fltHeader.h"
19 
20 TypeHandle FltVertexList::_type_handle;
21 
22 ////////////////////////////////////////////////////////////////////
23 // Function: FltVertexList::Constructor
24 // Access: Public
25 // Description:
26 ////////////////////////////////////////////////////////////////////
27 FltVertexList::
28 FltVertexList(FltHeader *header) : FltRecord(header) {
29 }
30 
31 ////////////////////////////////////////////////////////////////////
32 // Function: FltVertexList::get_num_vertices
33 // Access: Public
34 // Description: Returns the number of vertices in this vertex list.
35 ////////////////////////////////////////////////////////////////////
38  return _vertices.size();
39 }
40 
41 ////////////////////////////////////////////////////////////////////
42 // Function: FltVertexList::get_vertex
43 // Access: Public
44 // Description: Returns the nth vertex of this vertex list.
45 ////////////////////////////////////////////////////////////////////
47 get_vertex(int n) const {
48  nassertr(n >= 0 && n < (int)_vertices.size(), 0);
49  return _vertices[n];
50 }
51 
52 ////////////////////////////////////////////////////////////////////
53 // Function: FltVertexList::clear_vertices
54 // Access: Public
55 // Description: Removes all vertices from this vertex list.
56 ////////////////////////////////////////////////////////////////////
57 void FltVertexList::
59  _vertices.clear();
60 }
61 
62 ////////////////////////////////////////////////////////////////////
63 // Function: FltVertexList::add_vertex
64 // Access: Public
65 // Description: Adds a new vertex to the end of the vertex list.
66 ////////////////////////////////////////////////////////////////////
67 void FltVertexList::
69  _header->add_vertex(vertex);
70  _vertices.push_back(vertex);
71 }
72 
73 ////////////////////////////////////////////////////////////////////
74 // Function: FltVertexList::output
75 // Access: Public
76 // Description: Writes a quick one-line description of the record, but
77 // not its children. This is a human-readable
78 // description, primarily for debugging; to write a flt
79 // file, use FltHeader::write_flt().
80 ////////////////////////////////////////////////////////////////////
81 void FltVertexList::
82 output(ostream &out) const {
83  out << _vertices.size() << " vertices";
84 }
85 
86 ////////////////////////////////////////////////////////////////////
87 // Function: FltVertexList::extract_record
88 // Access: Protected, Virtual
89 // Description: Fills in the information in this bead based on the
90 // information given in the indicated datagram, whose
91 // opcode has already been read. Returns true on
92 // success, false if the datagram is invalid.
93 ////////////////////////////////////////////////////////////////////
94 bool FltVertexList::
95 extract_record(FltRecordReader &reader) {
96  if (!FltRecord::extract_record(reader)) {
97  return false;
98  }
99 
100  nassertr(reader.get_opcode() == FO_vertex_list, false);
101  DatagramIterator &iterator = reader.get_iterator();
102 
103  _vertices.clear();
104  while (iterator.get_remaining_size() >= 4) {
105  int vertex_offset = iterator.get_be_int32();
106  _vertices.push_back(_header->get_vertex_by_offset(vertex_offset));
107  }
108 
109  check_remaining_size(iterator);
110  return true;
111 }
112 
113 ////////////////////////////////////////////////////////////////////
114 // Function: FltVertexList::build_record
115 // Access: Protected, Virtual
116 // Description: Fills up the current record on the FltRecordWriter with
117 // data for this record, but does not advance the
118 // writer. Returns true on success, false if there is
119 // some error.
120 ////////////////////////////////////////////////////////////////////
121 bool FltVertexList::
122 build_record(FltRecordWriter &writer) const {
123  if (!FltRecord::build_record(writer)) {
124  return false;
125  }
126 
127  writer.set_opcode(FO_vertex_list);
128  Datagram &datagram = writer.update_datagram();
129 
130  Vertices::const_iterator vi;
131  for (vi = _vertices.begin(); vi != _vertices.end(); ++vi) {
132  datagram.add_be_uint32(_header->get_offset_by_vertex(*vi));
133  }
134 
135  return true;
136 }
FltVertex * get_vertex(int n) const
Returns the nth vertex of this vertex list.
This class writes a sequence of FltRecords to an ostream, handling opcode and size counts properly...
This class turns an istream into a sequence of FltRecords by reading a sequence of Datagrams and extr...
DatagramIterator & get_iterator()
Returns an iterator suitable for extracting data from the current record.
This is the first bead in the file, the top of the bead hierarchy, and the primary interface to readi...
Definition: fltHeader.h:48
void add_vertex(FltVertex *vertex)
Adds a new vertex to the end of the vertex list.
int get_num_vertices() const
Returns the number of vertices in this vertex list.
The base class for all kinds of records in a MultiGen OpenFlight file.
Definition: fltRecord.h:40
virtual void output(ostream &out) const
Writes a quick one-line description of the record, but not its children.
Represents a single vertex in the vertex palette.
Definition: fltVertex.h:35
void clear_vertices()
Removes all vertices from this vertex list.
void check_remaining_size(const DatagramIterator &di, const string &name=string()) const
Checks that the iterator has no bytes left, as it should at the end of a successfully read record...
Definition: fltRecord.cxx:313
FltOpcode get_opcode() const
Returns the opcode associated with the current record.
A class to retrieve the individual data elements previously stored in a Datagram. ...
void add_be_uint32(PN_uint32 value)
Adds an unsigned 32-bit big-endian integer to the datagram.
Definition: datagram.I:303
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
void set_opcode(FltOpcode opcode)
Sets the opcode associated with the current record.
Datagram & update_datagram()
Returns a modifiable reference to the datagram associated with the current record.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43