Panda3D
 All Classes Functions Variables Enumerations
loader.I
1 // Filename: loader.I
2 // Created by: mike (09Jan97)
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 
16 ////////////////////////////////////////////////////////////////////
17 // Function: Loader::Results::Constructor
18 // Access: Published
19 // Description:
20 ////////////////////////////////////////////////////////////////////
21 INLINE Loader::Results::
22 Results() {
23 }
24 
25 ////////////////////////////////////////////////////////////////////
26 // Function: Loader::Results::Copy Constructor
27 // Access: Published
28 // Description:
29 ////////////////////////////////////////////////////////////////////
30 INLINE Loader::Results::
31 Results(const Loader::Results &copy) :
32  _files(copy._files)
33 {
34 }
35 
36 ////////////////////////////////////////////////////////////////////
37 // Function: Loader::Results::Copy Assignment Operator
38 // Access: Published
39 // Description:
40 ////////////////////////////////////////////////////////////////////
41 INLINE void Loader::Results::
42 operator = (const Loader::Results &copy) {
43  _files = copy._files;
44 }
45 
46 ////////////////////////////////////////////////////////////////////
47 // Function: Loader::Results::Destructor
48 // Access: Published
49 // Description:
50 ////////////////////////////////////////////////////////////////////
51 INLINE Loader::Results::
52 ~Results() {
53 }
54 
55 ////////////////////////////////////////////////////////////////////
56 // Function: Loader::Results::clear
57 // Access: Published
58 // Description: Removes all the files from the list.
59 ////////////////////////////////////////////////////////////////////
60 INLINE void Loader::Results::
61 clear() {
62  _files.clear();
63 }
64 
65 ////////////////////////////////////////////////////////////////////
66 // Function: Loader::Results::get_num_files
67 // Access: Published
68 // Description: Returns the number of files on the result list.
69 ////////////////////////////////////////////////////////////////////
70 INLINE int Loader::Results::
71 get_num_files() const {
72  return _files.size();
73 }
74 
75 ////////////////////////////////////////////////////////////////////
76 // Function: Loader::Results::get_file
77 // Access: Published
78 // Description: Returns the nth file on the result list.
79 ////////////////////////////////////////////////////////////////////
80 INLINE const Filename &Loader::Results::
81 get_file(int n) const {
82  nassertr(n >= 0 && n < (int)_files.size(), _files[0]._path);
83  return _files[n]._path;
84 }
85 
86 ////////////////////////////////////////////////////////////////////
87 // Function: Loader::Results::get_file_type
88 // Access: Published
89 // Description: Returns the file type of the nth file on the result
90 // list.
91 ////////////////////////////////////////////////////////////////////
93 get_file_type(int n) const {
94  nassertr(n >= 0 && n < (int)_files.size(), NULL);
95  return _files[n]._type;
96 }
97 
98 ////////////////////////////////////////////////////////////////////
99 // Function: Loader::Results::add_file
100 // Access: Published
101 // Description: Adds a new file to the result list.
102 ////////////////////////////////////////////////////////////////////
103 INLINE void Loader::Results::
104 add_file(const Filename &file, LoaderFileType *type) {
105  ConsiderFile cf;
106  cf._path = file;
107  cf._type = type;
108  _files.push_back(cf);
109 }
110 
111 ////////////////////////////////////////////////////////////////////
112 // Function: Loader::set_task_manager
113 // Access: Published
114 // Description: Specifies the task manager that is used for
115 // asynchronous loads. The default is the global task
116 // manager.
117 ////////////////////////////////////////////////////////////////////
118 INLINE void Loader::
120  _task_manager = task_manager;
121 }
122 
123 ////////////////////////////////////////////////////////////////////
124 // Function: Loader::get_task_manager
125 // Access: Published
126 // Description: Returns the task manager that is used for
127 // asynchronous loads.
128 ////////////////////////////////////////////////////////////////////
131  return _task_manager;
132 }
133 
134 ////////////////////////////////////////////////////////////////////
135 // Function: Loader::set_task_chain
136 // Access: Published
137 // Description: Specifies the task chain that is used for
138 // asynchronous loads. The default is the initial name
139 // of the Loader object.
140 ////////////////////////////////////////////////////////////////////
141 INLINE void Loader::
142 set_task_chain(const string &task_chain) {
143  _task_chain = task_chain;
144 }
145 
146 ////////////////////////////////////////////////////////////////////
147 // Function: Loader::get_task_chain
148 // Access: Published
149 // Description: Returns the task chain that is used for
150 // asynchronous loads.
151 ////////////////////////////////////////////////////////////////////
152 INLINE const string &Loader::
153 get_task_chain() const {
154  return _task_chain;
155 }
156 
157 ////////////////////////////////////////////////////////////////////
158 // Function: Loader::stop_threads
159 // Access: Published
160 // Description: Stop any threads used for asynchronous loads.
161 ////////////////////////////////////////////////////////////////////
162 INLINE void Loader::
164  PT(AsyncTaskChain) chain = _task_manager->find_task_chain(_task_chain);
165  if (chain != (AsyncTaskChain *)NULL) {
166  chain->stop_threads();
167  }
168 }
169 
170 ////////////////////////////////////////////////////////////////////
171 // Function: Loader::remove
172 // Access: Published
173 // Description: Removes a pending asynchronous load request. Returns
174 // true if successful, false otherwise.
175 ////////////////////////////////////////////////////////////////////
176 INLINE bool Loader::
178  return _task_manager->remove(task);
179 }
180 
181 ////////////////////////////////////////////////////////////////////
182 // Function: Loader::load_sync
183 // Access: Published
184 // Description: Loads the file immediately, waiting for it to
185 // complete.
186 //
187 // If search is true, the file is searched for along the
188 // model path; otherwise, only the exact filename is
189 // loaded.
190 ////////////////////////////////////////////////////////////////////
191 INLINE PT(PandaNode) Loader::
192 load_sync(const Filename &filename, const LoaderOptions &options) const {
193  if (!_file_types_loaded) {
194  load_file_types();
195  }
196  return load_file(filename, options);
197 }
198 
199 ////////////////////////////////////////////////////////////////////
200 // Function: Loader::load_async
201 // Access: Published
202 // Description: Begins an asynchronous load request. To use this
203 // call, first call make_async_request() to create a new
204 // ModelLoadRequest object with the filename you wish to
205 // load, and then add that object to the Loader with
206 // load_async. This function will return immediately,
207 // and the model will be loaded in the background.
208 //
209 // To determine when the model has completely loaded,
210 // you may poll request->is_ready() from time to time,
211 // or set the done_event on the request object and
212 // listen for that event. When the model is ready, you
213 // may retrieve it via request->get_model().
214 ////////////////////////////////////////////////////////////////////
215 INLINE void Loader::
217  request->set_task_chain(_task_chain);
218  _task_manager->add(request);
219 }
220 
221 ////////////////////////////////////////////////////////////////////
222 // Function: Loader::save_sync
223 // Access: Published
224 // Description: Saves the file immediately, waiting for it to
225 // complete.
226 ////////////////////////////////////////////////////////////////////
227 INLINE bool Loader::
228 save_sync(const Filename &filename, const LoaderOptions &options,
229  PandaNode *node) const {
230  if (!_file_types_loaded) {
231  load_file_types();
232  }
233  return save_file(filename, options, node);
234 }
235 
236 ////////////////////////////////////////////////////////////////////
237 // Function: Loader::save_async
238 // Access: Published
239 // Description: Begins an asynchronous save request. To use this
240 // call, first call make_async_save_request() to create
241 // a new ModelSaveRequest object with the filename you
242 // wish to load, and then add that object to the Loader
243 // with save_async. This function will return
244 // immediately, and the model will be loaded in the
245 // background.
246 //
247 // To determine when the model has completely loaded,
248 // you may poll request->is_ready() from time to time,
249 // or set the done_event on the request object and
250 // listen for that event. When the request is ready,
251 // you may retrieve the success or failure via
252 // request->get_success().
253 ////////////////////////////////////////////////////////////////////
254 INLINE void Loader::
256  request->set_task_chain(_task_chain);
257  _task_manager->add(request);
258 }
259 
260 ////////////////////////////////////////////////////////////////////
261 // Function: Loader::get_global_ptr
262 // Access: Published
263 // Description: Returns a pointer to the global Loader. This is the
264 // Loader that most code should use for loading models.
265 ////////////////////////////////////////////////////////////////////
266 INLINE Loader *Loader::
268  if (_global_ptr == (Loader *)NULL) {
269  make_global_ptr();
270  }
271  return _global_ptr;
272 }
A basic node of the scene graph or data graph.
Definition: pandaNode.h:72
A class to manage a loose queue of isolated tasks, which can be performed either synchronously (in th...
Specifies parameters that may be passed to the loader.
Definition: loaderOptions.h:26
bool remove(AsyncTask *task)
Removes a pending asynchronous load request.
Definition: loader.I:177
A convenient class for loading models from disk, in bam or egg format (or any of a number of other fo...
Definition: loader.h:47
void set_task_manager(AsyncTaskManager *task_manager)
Specifies the task manager that is used for asynchronous loads.
Definition: loader.I:119
static Loader * get_global_ptr()
Returns a pointer to the global Loader.
Definition: loader.I:267
LoaderFileType * get_file_type(int n) const
Returns the file type of the nth file on the result list.
Definition: loader.I:93
const Filename & get_file(int n) const
Returns the nth file on the result list.
Definition: loader.I:81
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
const string & get_task_chain() const
Returns the task chain that is used for asynchronous loads.
Definition: loader.I:153
void load_async(AsyncTask *request)
Begins an asynchronous load request.
Definition: loader.I:216
The AsyncTaskChain is a subset of the AsyncTaskManager.
void save_async(AsyncTask *request)
Begins an asynchronous save request.
Definition: loader.I:255
void clear()
Removes all the files from the list.
Definition: loader.I:61
void set_task_chain(const string &chain_name)
Specifies the AsyncTaskChain on which this task will be running.
Definition: asyncTask.cxx:277
void add_file(const Filename &file, LoaderFileType *type)
Adds a new file to the result list.
Definition: loader.I:104
AsyncTaskManager * get_task_manager() const
Returns the task manager that is used for asynchronous loads.
Definition: loader.I:130
This class represents a concrete task performed by an AsyncManager.
Definition: asyncTask.h:43
void stop_threads()
Stop any threads used for asynchronous loads.
Definition: loader.I:163
This is the base class for a family of scene-graph file types that the Loader supports.
bool save_sync(const Filename &filename, const LoaderOptions &options, PandaNode *node) const
Saves the file immediately, waiting for it to complete.
Definition: loader.I:228
void set_task_chain(const string &task_chain)
Specifies the task chain that is used for asynchronous loads.
Definition: loader.I:142
int get_num_files() const
Returns the number of files on the result list.
Definition: loader.I:71