/xbmc/visualizations/XBMCProjectM/libprojectM/Preset.hpp

http://github.com/xbmc/xbmc · C++ Header · 248 lines · 126 code · 63 blank · 59 comment · 10 complexity · 70b9c43c6097ee510221ede0419c146a MD5 · raw file

  1. /**
  2. * projectM -- Milkdrop-esque visualisation SDK
  3. * Copyright (C)2003-2007 projectM Team
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. * See 'LICENSE.txt' included within this release
  19. *
  20. */
  21. /**
  22. * $Id$
  23. *
  24. * Preset
  25. *
  26. * $Log$
  27. */
  28. #ifndef _PRESET_HPP
  29. #define _PRESET_HPP
  30. #include "Common.hpp"
  31. #include <string>
  32. #include <cassert>
  33. #include <map>
  34. #define PRESET_DEBUG 0 /* 0 for no debugging, 1 for normal, 2 for insane */
  35. #include "CustomShape.hpp"
  36. #include "CustomWave.hpp"
  37. #include "Expr.hpp"
  38. #include "PerPixelEqn.hpp"
  39. #include "PerFrameEqn.hpp"
  40. #include "BuiltinParams.hpp"
  41. #include "PresetFrameIO.hpp"
  42. #include "InitCond.hpp"
  43. class CustomWave;
  44. class CustomShape;
  45. class InitCond;
  46. class Preset
  47. {
  48. protected:
  49. public:
  50. /// Load a preset by filename with input and output buffers specified.
  51. /// \param absoluteFilePath the absolute file path of a preset to load from the file system
  52. /// \param presetName a descriptive name for the preset. Usually just the file name
  53. /// \param presetInputs a reference to read only projectM engine variables
  54. /// \param presetOutputs initialized and filled with data parsed from a preset
  55. Preset(const std::string & absoluteFilePath, const std::string & presetName, PresetInputs & presetInputs, PresetOutputs & presetOutputs);
  56. /// Load a preset from an input stream with input and output buffers specified.
  57. /// \param in an already initialized input stream to read the preset file from
  58. /// \param presetName a descriptive name for the preset. Usually just the file name
  59. /// \param presetInputs a reference to read only projectM engine variables
  60. /// \param presetOutputs initialized and filled with data parsed from a preset
  61. Preset(std::istream & in, const std::string & presetName, PresetInputs & presetInputs, PresetOutputs & presetOutputs);
  62. ~Preset();
  63. /// Evaluates the preset for a frame given the current values of preset inputs / outputs
  64. /// All calculated values are stored in the associated preset outputs instance
  65. void evaluateFrame();
  66. /// All "builtin" parameters for this preset. Anything *but* user defined parameters and
  67. /// custom waves / shapes objects go here.
  68. /// @bug encapsulate
  69. BuiltinParams builtinParams;
  70. /// Used by parser to find/create custom waves and shapes. May be refactored
  71. template <class CustomObject>
  72. static CustomObject * find_custom_object(int id, std::vector<CustomObject*> & customObjects);
  73. int per_pixel_eqn_string_index;
  74. int per_frame_eqn_string_index;
  75. int per_frame_init_eqn_string_index;
  76. int per_frame_eqn_count,
  77. per_frame_init_eqn_count;
  78. /// Used by parser
  79. /// @bug refactor
  80. int add_per_pixel_eqn( char *name, GenExpr *gen_expr );
  81. /// Accessor method to retrieve the absolute file path of the loaded preset
  82. /// \returns a file path string
  83. std::string absoluteFilePath() const
  84. {
  85. return m_absoluteFilePath;
  86. }
  87. /// Accessor method for the preset outputs instance associated with this preset
  88. /// \returns A preset output instance with values computed from most recent evaluateFrame()
  89. PresetOutputs & presetOutputs() const
  90. {
  91. return m_presetOutputs;
  92. }
  93. PresetInputs & presetInputs() const
  94. {
  95. return m_presetInputs;
  96. }
  97. /// Sets the descriptive name for this preset (typically the file name)
  98. /// \param theValue the new preset name to assign to the preset
  99. void setPresetName ( const std::string& theValue )
  100. {
  101. m_presetName = theValue;
  102. }
  103. /// Gets the descriptive name for this preset (typically the file name)
  104. /// \returns the name of the preset
  105. std::string presetName() const
  106. {
  107. return m_presetName;
  108. }
  109. /// @bug encapsulate
  110. PresetOutputs::cwave_container customWaves;
  111. PresetOutputs::cshape_container customShapes;
  112. /// @bug encapsulate
  113. /* Data structures that contain equation and initial condition information */
  114. std::vector<PerFrameEqn*> per_frame_eqn_tree; /* per frame equations */
  115. std::map<int, PerPixelEqn*> per_pixel_eqn_tree; /* per pixel equation tree */
  116. std::map<std::string,InitCond*> per_frame_init_eqn_tree; /* per frame initial equations */
  117. std::map<std::string,InitCond*> init_cond_tree; /* initial conditions */
  118. std::map<std::string,Param*> user_param_tree; /* user parameter splay tree */
  119. private:
  120. // The absolute file path of the preset
  121. std::string m_absoluteFilePath;
  122. // The absolute path of the preset
  123. std::string m_absolutePath;
  124. // The name for the preset. Usually the file name, but in theory anything goes
  125. std::string m_presetName;
  126. void initialize(const std::string & pathname);
  127. void initialize(std::istream & in);
  128. int loadPresetFile(const std::string & pathname);
  129. void loadBuiltinParamsUnspecInitConds();
  130. void loadCustomWaveUnspecInitConds();
  131. void loadCustomShapeUnspecInitConds();
  132. void evalCustomWavePerFrameEquations();
  133. void evalCustomShapePerFrameEquations();
  134. void evalPerFrameInitEquations();
  135. void evalCustomWaveInitConditions();
  136. void evalCustomShapeInitConditions();
  137. void evalPerPixelEqns();
  138. void evalPerFrameEquations();
  139. void initialize_PerPixelMeshes();
  140. int readIn(std::istream & fs);
  141. void preloadInitialize();
  142. void postloadInitialize();
  143. PresetOutputs & m_presetOutputs;
  144. PresetInputs & m_presetInputs; // added for gx, gy reference.
  145. template <class CustomObject>
  146. void transfer_q_variables(std::vector<CustomObject*> & customObjects);
  147. };
  148. template <class CustomObject>
  149. void Preset::transfer_q_variables(std::vector<CustomObject*> & customObjects)
  150. {
  151. CustomObject * custom_object;
  152. for (typename std::vector<CustomObject*>::iterator pos = customObjects.begin(); pos != customObjects.end();++pos) {
  153. custom_object = *pos;
  154. custom_object->q1 = m_presetOutputs.q1;
  155. custom_object->q2 = m_presetOutputs.q2;
  156. custom_object->q3 = m_presetOutputs.q3;
  157. custom_object->q4 = m_presetOutputs.q4;
  158. custom_object->q5 = m_presetOutputs.q5;
  159. custom_object->q6 = m_presetOutputs.q6;
  160. custom_object->q7 = m_presetOutputs.q7;
  161. custom_object->q8 = m_presetOutputs.q8;
  162. }
  163. }
  164. template <class CustomObject>
  165. CustomObject * Preset::find_custom_object(int id, std::vector<CustomObject*> & customObjects)
  166. {
  167. CustomObject * custom_object = NULL;
  168. for (typename std::vector<CustomObject*>::iterator pos = customObjects.begin(); pos != customObjects.end();++pos) {
  169. if ((*pos)->id == id) {
  170. custom_object = *pos;
  171. break;
  172. }
  173. }
  174. if (custom_object == NULL)
  175. {
  176. if ((custom_object = new CustomObject(id)) == NULL)
  177. {
  178. return NULL;
  179. }
  180. customObjects.push_back(custom_object);
  181. }
  182. assert(custom_object);
  183. return custom_object;
  184. }
  185. #endif /** !_PRESET_HPP */