loader.I 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. // Function: Loader::Results::Constructor
  16. // Access: Published
  17. // Description:
  18. ////////////////////////////////////////////////////////////////////
  19. INLINE Loader::Results::
  20. Results() {
  21. }
  22. ////////////////////////////////////////////////////////////////////
  23. // Function: Loader::Results::Copy Constructor
  24. // Access: Published
  25. // Description:
  26. ////////////////////////////////////////////////////////////////////
  27. INLINE Loader::Results::
  28. Results(const Loader::Results &copy) :
  29. _files(copy._files)
  30. {
  31. }
  32. ////////////////////////////////////////////////////////////////////
  33. // Function: Loader::Results::Copy Assignment Operator
  34. // Access: Published
  35. // Description:
  36. ////////////////////////////////////////////////////////////////////
  37. INLINE void Loader::Results::
  38. operator = (const Loader::Results &copy) {
  39. _files = copy._files;
  40. }
  41. ////////////////////////////////////////////////////////////////////
  42. // Function: Loader::Results::Destructor
  43. // Access: Published
  44. // Description:
  45. ////////////////////////////////////////////////////////////////////
  46. INLINE Loader::Results::
  47. ~Results() {
  48. }
  49. ////////////////////////////////////////////////////////////////////
  50. // Function: Loader::Results::clear
  51. // Access: Published
  52. // Description: Removes all the files from the list.
  53. ////////////////////////////////////////////////////////////////////
  54. INLINE void Loader::Results::
  55. clear() {
  56. _files.clear();
  57. }
  58. ////////////////////////////////////////////////////////////////////
  59. // Function: Loader::Results::get_num_files
  60. // Access: Published
  61. // Description: Returns the number of files on the result list.
  62. ////////////////////////////////////////////////////////////////////
  63. INLINE int Loader::Results::
  64. get_num_files() const {
  65. return _files.size();
  66. }
  67. ////////////////////////////////////////////////////////////////////
  68. // Function: Loader::Results::get_file
  69. // Access: Published
  70. // Description: Returns the nth file on the result list.
  71. ////////////////////////////////////////////////////////////////////
  72. INLINE const Filename &Loader::Results::
  73. get_file(int n) const {
  74. nassertr(n >= 0 && n < (int)_files.size(), _files[0]._path);
  75. return _files[n]._path;
  76. }
  77. ////////////////////////////////////////////////////////////////////
  78. // Function: Loader::Results::get_file_type
  79. // Access: Published
  80. // Description: Returns the file type of the nth file on the result
  81. // list.
  82. ////////////////////////////////////////////////////////////////////
  83. INLINE LoaderFileType *Loader::Results::
  84. get_file_type(int n) const {
  85. nassertr(n >= 0 && n < (int)_files.size(), NULL);
  86. return _files[n]._type;
  87. }
  88. ////////////////////////////////////////////////////////////////////
  89. // Function: Loader::Results::add_file
  90. // Access: Published
  91. // Description: Adds a new file to the result list.
  92. ////////////////////////////////////////////////////////////////////
  93. INLINE void Loader::Results::
  94. add_file(const Filename &file, LoaderFileType *type) {
  95. ConsiderFile cf;
  96. cf._path = file;
  97. cf._type = type;
  98. _files.push_back(cf);
  99. }
  100. ////////////////////////////////////////////////////////////////////
  101. // Function: Loader::set_task_manager
  102. // Access: Published
  103. // Description: Specifies the task manager that is used for
  104. // asynchronous loads. The default is the global task
  105. // manager.
  106. ////////////////////////////////////////////////////////////////////
  107. INLINE void Loader::
  108. set_task_manager(AsyncTaskManager *task_manager) {
  109. _task_manager = task_manager;
  110. }
  111. ////////////////////////////////////////////////////////////////////
  112. // Function: Loader::get_task_manager
  113. // Access: Published
  114. // Description: Returns the task manager that is used for
  115. // asynchronous loads.
  116. ////////////////////////////////////////////////////////////////////
  117. INLINE AsyncTaskManager *Loader::
  118. get_task_manager() const {
  119. return _task_manager;
  120. }
  121. ////////////////////////////////////////////////////////////////////
  122. // Function: Loader::set_task_chain
  123. // Access: Published
  124. // Description: Specifies the task chain that is used for
  125. // asynchronous loads. The default is the initial name
  126. // of the Loader object.
  127. ////////////////////////////////////////////////////////////////////
  128. INLINE void Loader::
  129. set_task_chain(const string &task_chain) {
  130. _task_chain = task_chain;
  131. }
  132. ////////////////////////////////////////////////////////////////////
  133. // Function: Loader::get_task_chain
  134. // Access: Published
  135. // Description: Returns the task chain that is used for
  136. // asynchronous loads.
  137. ////////////////////////////////////////////////////////////////////
  138. INLINE const string &Loader::
  139. get_task_chain() const {
  140. return _task_chain;
  141. }
  142. ////////////////////////////////////////////////////////////////////
  143. // Function: Loader::stop_threads
  144. // Access: Published
  145. // Description: Stop any threads used for asynchronous loads.
  146. ////////////////////////////////////////////////////////////////////
  147. INLINE void Loader::
  148. stop_threads() {
  149. PT(AsyncTaskChain) chain = _task_manager->find_task_chain(_task_chain);
  150. if (chain != (AsyncTaskChain *)NULL) {
  151. chain->stop_threads();
  152. }
  153. }
  154. ////////////////////////////////////////////////////////////////////
  155. // Function: Loader::remove
  156. // Access: Published
  157. // Description: Removes a pending asynchronous load request. Returns
  158. // true if successful, false otherwise.
  159. ////////////////////////////////////////////////////////////////////
  160. INLINE bool Loader::
  161. remove(AsyncTask *task) {
  162. return _task_manager->remove(task);
  163. }
  164. ////////////////////////////////////////////////////////////////////
  165. // Function: Loader::load_sync
  166. // Access: Published
  167. // Description: Loads the file immediately, waiting for it to
  168. // complete.
  169. //
  170. // If search is true, the file is searched for along the
  171. // model path; otherwise, only the exact filename is
  172. // loaded.
  173. ////////////////////////////////////////////////////////////////////
  174. INLINE PT(PandaNode) Loader::
  175. load_sync(const Filename &filename, const LoaderOptions &options) const {
  176. if (!_file_types_loaded) {
  177. load_file_types();
  178. }
  179. return load_file(filename, options);
  180. }
  181. ////////////////////////////////////////////////////////////////////
  182. // Function: Loader::load_async
  183. // Access: Published
  184. // Description: Begins an asynchronous load request. To use this
  185. // call, first create a new ModelLoadRequest object with
  186. // the filename you wish to load, and then add that
  187. // object to the Loader with load_async. This function
  188. // will return immediately, and the model will be loaded
  189. // in the background.
  190. //
  191. // To determine when the model has completely loaded,
  192. // you may poll request->is_ready() from time to time,
  193. // or set the done_event on the request object and
  194. // listen for that event. When the model is ready, you
  195. // may retrieve it via request->get_model().
  196. ////////////////////////////////////////////////////////////////////
  197. INLINE void Loader::
  198. load_async(AsyncTask *request) {
  199. request->set_task_chain(_task_chain);
  200. _task_manager->add(request);
  201. }
  202. ////////////////////////////////////////////////////////////////////
  203. // Function: Loader::get_global_ptr
  204. // Access: Published
  205. // Description: Returns a pointer to the global Loader. This is the
  206. // Loader that most code should use for loading models.
  207. ////////////////////////////////////////////////////////////////////
  208. INLINE Loader *Loader::
  209. get_global_ptr() {
  210. if (_global_ptr == (Loader *)NULL) {
  211. make_global_ptr();
  212. }
  213. return _global_ptr;
  214. }