Panda3D
 All Classes Functions Variables Enumerations
fltTrans.cxx
1 // Filename: fltTrans.cxx
2 // Created by: drose (11Apr01)
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 "fltTrans.h"
16 
17 #include "fltHeader.h"
18 #include "pystub.h"
19 
20 ////////////////////////////////////////////////////////////////////
21 // Function: FltTrans::Constructor
22 // Access: Public
23 // Description:
24 ////////////////////////////////////////////////////////////////////
25 FltTrans::
26 FltTrans() :
27  WithOutputFile(true, false, true)
28 {
29  // Indicate the extension name we expect the user to supply for
30  // output files.
31  _preferred_extension = ".flt";
32 
33  set_program_brief("apply various operations to a MultiGen .flt file");
34  set_program_description
35  ("This program reads a MultiGen OpenFlight (.flt) file and writes an "
36  "essentially equivalent .flt file, to the file specified with -o (or "
37  "as the second parameter). Some simple operations may be performed.");
38 
39  clear_runlines();
40  add_runline("[opts] input.flt output.flt");
41  add_runline("[opts] -o output.flt input.flt");
42 
43  add_path_replace_options();
44  add_path_store_options();
45 
46  add_option
47  ("v", "version", 0,
48  "Upgrade (or downgrade) the flt file to the indicated version. This "
49  "may not be completely correct for all version-to-version combinations.",
50  &FltTrans::dispatch_double, &_got_new_version, &_new_version);
51 
52  add_option
53  ("o", "filename", 0,
54  "Specify the filename to which the resulting .flt file will be written. "
55  "If this option is omitted, the last parameter name is taken to be the "
56  "name of the output file.",
57  &FltTrans::dispatch_filename, &_got_output_filename, &_output_filename);
58 }
59 
60 
61 ////////////////////////////////////////////////////////////////////
62 // Function: FltTrans::run
63 // Access: Public
64 // Description:
65 ////////////////////////////////////////////////////////////////////
66 void FltTrans::
67 run() {
68  if (_got_new_version) {
69  int new_version = (int)floor(_new_version * 100.0 + 0.5);
70  if (new_version < FltHeader::min_flt_version() ||
71  new_version > FltHeader::max_flt_version()) {
72  nout << "Cannot write flt files of version " << new_version / 100.0
73  << ". This program only understands how to write flt files between version "
74  << FltHeader::min_flt_version() / 100.0 << " and "
75  << FltHeader::max_flt_version() / 100.0 << ".\n";
76  exit(1);
77  }
78  }
79 
80  PT(FltHeader) header = new FltHeader(_path_replace);
81 
82  nout << "Reading " << _input_filename << "\n";
83  FltError result = header->read_flt(_input_filename);
84  if (result != FE_ok) {
85  nout << "Unable to read: " << result << "\n";
86  exit(1);
87  }
88 
89  if (header->check_version()) {
90  nout << "Version is " << header->get_flt_version() / 100.0 << "\n";
91  }
92 
93  if (_got_new_version) {
94  int new_version = (int)floor(_new_version * 100.0 + 0.5);
95  header->set_flt_version(new_version);
96  }
97 
98  header->apply_converted_filenames();
99 
100  result = header->write_flt(get_output());
101  if (result != FE_ok) {
102  nout << "Unable to write: " << result << "\n";
103  exit(1);
104  }
105 
106  nout << "Successfully written.\n";
107 }
108 
109 
110 ////////////////////////////////////////////////////////////////////
111 // Function: FltTrans::handle_args
112 // Access: Protected, Virtual
113 // Description:
114 ////////////////////////////////////////////////////////////////////
115 bool FltTrans::
116 handle_args(ProgramBase::Args &args) {
117  if (!check_last_arg(args, 1)) {
118  return false;
119  }
120 
121  if (args.empty()) {
122  nout << "You must specify the .flt file to read on the command line.\n";
123  return false;
124 
125  } else if (args.size() != 1) {
126  nout << "You must specify only one .flt file to read on the command line.\n";
127  return false;
128  }
129 
130  _input_filename = args[0];
131 
132  return true;
133 }
134 
135 
136 int main(int argc, char *argv[]) {
137  // A call to pystub() to force libpystub.so to be linked in.
138  pystub();
139 
140  FltTrans prog;
141  prog.parse_command_line(argc, argv);
142  prog.run();
143  return 0;
144 }
A program to read a flt file and write an equivalent flt file, possibly performing some minor operati...
Definition: fltTrans.h:29
virtual void parse_command_line(int argc, char **argv)
Dispatches on each of the options on the command line, and passes the remaining parameters to handle_...
static int max_flt_version()
Returns the latest flt version number that this codebase is known to support (times 100)...
Definition: fltHeader.cxx:403
This is the first bead in the file, the top of the bead hierarchy, and the primary interface to readi...
Definition: fltHeader.h:48
static int min_flt_version()
Returns the earliest flt version number that this codebase supports (times 100).
Definition: fltHeader.cxx:391
This is the bare functionality (intended to be inherited from along with ProgramBase or some derivati...
ostream & get_output()
Returns an output stream that corresponds to the user&#39;s intended egg file output–either stdout...