Panda3D
loader.I
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file loader.I
10  * @author mike
11  * @date 1997-01-09
12  */
13 
14 /**
15  *
16  */
17 INLINE Loader::Results::
18 Results() {
19 }
20 
21 /**
22  *
23  */
24 INLINE Loader::Results::
25 Results(const Loader::Results &copy) :
26  _files(copy._files)
27 {
28 }
29 
30 /**
31  *
32  */
33 INLINE void Loader::Results::
34 operator = (const Loader::Results &copy) {
35  _files = copy._files;
36 }
37 
38 /**
39  *
40  */
41 INLINE Loader::Results::
42 ~Results() {
43 }
44 
45 /**
46  * Removes all the files from the list.
47  */
48 INLINE void Loader::Results::
49 clear() {
50  _files.clear();
51 }
52 
53 /**
54  * Returns the number of files on the result list.
55  */
56 INLINE int Loader::Results::
57 get_num_files() const {
58  return _files.size();
59 }
60 
61 /**
62  * Returns the nth file on the result list.
63  */
64 INLINE const Filename &Loader::Results::
65 get_file(int n) const {
66  nassertr(n >= 0 && n < (int)_files.size(), _files[0]._path);
67  return _files[n]._path;
68 }
69 
70 /**
71  * Returns the file type of the nth file on the result list.
72  */
74 get_file_type(int n) const {
75  nassertr(n >= 0 && n < (int)_files.size(), nullptr);
76  return _files[n]._type;
77 }
78 
79 /**
80  * Adds a new file to the result list.
81  */
82 INLINE void Loader::Results::
83 add_file(const Filename &file, LoaderFileType *type) {
84  ConsiderFile cf;
85  cf._path = file;
86  cf._type = type;
87  _files.push_back(cf);
88 }
89 
90 /**
91  * Specifies the task manager that is used for asynchronous loads. The
92  * default is the global task manager.
93  */
94 INLINE void Loader::
96  _task_manager = task_manager;
97 }
98 
99 /**
100  * Returns the task manager that is used for asynchronous loads.
101  */
104  return _task_manager;
105 }
106 
107 /**
108  * Specifies the task chain that is used for asynchronous loads. The default
109  * is the initial name of the Loader object.
110  */
111 INLINE void Loader::
112 set_task_chain(const std::string &task_chain) {
113  _task_chain = task_chain;
114 }
115 
116 /**
117  * Returns the task chain that is used for asynchronous loads.
118  */
119 INLINE const std::string &Loader::
120 get_task_chain() const {
121  return _task_chain;
122 }
123 
124 /**
125  * Stop any threads used for asynchronous loads.
126  */
127 INLINE void Loader::
129  PT(AsyncTaskChain) chain = _task_manager->find_task_chain(_task_chain);
130  if (chain != nullptr) {
131  chain->stop_threads();
132  }
133 }
134 
135 /**
136  * Removes a pending asynchronous load request. Returns true if successful,
137  * false otherwise.
138  * @deprecated use task.cancel() to cancel the request instead.
139  */
140 INLINE bool Loader::
142  return _task_manager->remove(task);
143 }
144 
145 /**
146  * Loads the file immediately, waiting for it to complete.
147  *
148  * If search is true, the file is searched for along the model path;
149  * otherwise, only the exact filename is loaded.
150  */
151 INLINE PT(PandaNode) Loader::
152 load_sync(const Filename &filename, const LoaderOptions &options) const {
153  if (!_file_types_loaded) {
154  load_file_types();
155  }
156  return load_file(filename, options);
157 }
158 
159 /**
160  * Begins an asynchronous load request. To use this call, first call
161  * make_async_request() to create a new ModelLoadRequest object with the
162  * filename you wish to load, and then add that object to the Loader with
163  * load_async. This function will return immediately, and the model will be
164  * loaded in the background.
165  *
166  * To determine when the model has completely loaded, you may poll
167  * request->is_ready() from time to time, or set the done_event on the request
168  * object and listen for that event. When the model is ready, you may
169  * retrieve it via request->get_model().
170  */
171 INLINE void Loader::
172 load_async(AsyncTask *request) {
173  request->set_task_chain(_task_chain);
174  _task_manager->add(request);
175 }
176 
177 /**
178  * Saves the file immediately, waiting for it to complete.
179  */
180 INLINE bool Loader::
181 save_sync(const Filename &filename, const LoaderOptions &options,
182  PandaNode *node) const {
183  if (!_file_types_loaded) {
184  load_file_types();
185  }
186  return save_file(filename, options, node);
187 }
188 
189 /**
190  * Begins an asynchronous save request. To use this call, first call
191  * make_async_save_request() to create a new ModelSaveRequest object with the
192  * filename you wish to load, and then add that object to the Loader with
193  * save_async. This function will return immediately, and the model will be
194  * loaded in the background.
195  *
196  * To determine when the model has completely loaded, you may poll
197  * request->is_ready() from time to time, or set the done_event on the request
198  * object and listen for that event. When the request is ready, you may
199  * retrieve the success or failure via request->get_success().
200  */
201 INLINE void Loader::
203  request->set_task_chain(_task_chain);
204  _task_manager->add(request);
205 }
206 
207 /**
208  * Returns a pointer to the global Loader. This is the Loader that most code
209  * should use for loading models.
210  */
211 INLINE Loader *Loader::
213  if (_global_ptr == nullptr) {
214  make_global_ptr();
215  }
216  return _global_ptr;
217 }
A basic node of the scene graph or data graph.
Definition: pandaNode.h:64
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:23
bool remove(AsyncTask *task)
Removes a pending asynchronous load request.
Definition: loader.I:141
A convenient class for loading models from disk, in bam or egg format (or any of a number of other fo...
Definition: loader.h:42
get_file
Returns the nth file on the result list.
Definition: loader.h:61
void set_task_manager(AsyncTaskManager *task_manager)
Specifies the task manager that is used for asynchronous loads.
Definition: loader.I:95
static Loader * get_global_ptr()
Returns a pointer to the global Loader.
Definition: loader.I:212
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:39
set_task_chain
Specifies the AsyncTaskChain on which this task will be running.
Definition: asyncTask.h:114
The AsyncTaskChain is a subset of the AsyncTaskManager.
void save_async(AsyncTask *request)
Begins an asynchronous save request.
Definition: loader.I:202
void clear()
Removes all the files from the list.
Definition: loader.I:49
void add_file(const Filename &file, LoaderFileType *type)
Adds a new file to the result list.
Definition: loader.I:83
get_file_type
Returns the file type of the nth file on the result list.
Definition: loader.h:63
This class represents a concrete task performed by an AsyncManager.
Definition: asyncTask.h:32
void stop_threads()
Stop any threads used for asynchronous loads.
Definition: loader.I:128
AsyncTaskManager * get_task_manager() const
Returns the task manager that is used for asynchronous loads.
Definition: loader.I:103
This is the base class for a family of scene-graph file types that the Loader supports.
void set_task_chain(const std::string &task_chain)
Specifies the task chain that is used for asynchronous loads.
Definition: loader.I:112
const std::string & get_task_chain() const
Returns the task chain that is used for asynchronous loads.
Definition: loader.I:120
bool save_sync(const Filename &filename, const LoaderOptions &options, PandaNode *node) const
Saves the file immediately, waiting for it to complete.
Definition: loader.I:181