From ff10424a331de62dbe461129cbca1be2abd3c1cc Mon Sep 17 00:00:00 2001 From: w1z7ard Date: Sat, 22 Sep 2007 19:20:32 +0000 Subject: [PATCH] modded preset chooser so random selection changes the position iterator other very minor non behavioral changes git-svn-id: https://projectm.svn.sourceforge.net/svnroot/projectm/trunk@472 6778bc44-b910-0410-a7a0-be141de4315d --- src/projectM-engine/Expr.cpp | 6 +++--- src/projectM-engine/PresetChooser.hpp | 26 +++++++++++++---------- src/projectM-engine/RingBuffer.hpp | 2 +- src/projectM-engine/console_interface.cpp | 6 ++++-- src/projectM-engine/projectM.cpp | 17 +++++++++------ 5 files changed, 34 insertions(+), 23 deletions(-) diff --git a/src/projectM-engine/Expr.cpp b/src/projectM-engine/Expr.cpp index 0549f568b..8d8193b41 100755 --- a/src/projectM-engine/Expr.cpp +++ b/src/projectM-engine/Expr.cpp @@ -56,11 +56,11 @@ float PrefunExpr::eval_prefun_expr ( int mesh_i, int mesh_j ) assert ( func_ptr ); - /* This is slightly less than safe, since - who knows if the passed argument is valid. For - speed purposes we'll go with this */ + float * arg_list = new float[this->num_args]; + assert(arg_list); + #ifdef EVAL_DEBUG_DOUBLE DWRITE ( "fn[" ); #endif diff --git a/src/projectM-engine/PresetChooser.hpp b/src/projectM-engine/PresetChooser.hpp index 935f6c9cc..f8ab486eb 100644 --- a/src/projectM-engine/PresetChooser.hpp +++ b/src/projectM-engine/PresetChooser.hpp @@ -109,7 +109,7 @@ public: /// \param WeightFuncstor a functor that returns a probability for each index (see UniformRandomFunctor) /// \returns an auto pointer of the newly allocated preset template - std::auto_ptr weightedRandom(const PresetInputs & presetInputs, PresetOutputs & presetOutputs, WeightFunctor & weightFunctor); + iterator weightedRandom(WeightFunctor & weightFunctor); /// Do a weighted sample given a weight functor and default construction (ie. element size) of the weight functor @@ -118,7 +118,7 @@ public: /// \param WeightFunctor a functor that returns a probability for each index (see UniformRandomFunctor) /// \returns an auto pointer of the newly allocated preset template - std::auto_ptr weightedRandom(const PresetInputs & presetInputs, PresetOutputs & preseOutputs); + iterator weightedRandom(); @@ -127,7 +127,7 @@ public: private: template - std::auto_ptr doWeightedSample(WeightFunctor & weightFunctor, const PresetInputs & presetInputs, PresetOutputs & presetOutputs); + iterator doWeightedSample(WeightFunctor & weightFunctor); const PresetLoader * m_presetLoader; }; @@ -191,20 +191,19 @@ inline PresetIterator PresetChooser::end() const { } template -std::auto_ptr PresetChooser::weightedRandom(const PresetInputs & presetInputs, PresetOutputs & presetOutputs, WeightFunctor & weightFunctor) { +typename PresetChooser::iterator PresetChooser::weightedRandom(WeightFunctor & weightFunctor) { return doWeightedSample(weightFunctor); } inline bool PresetChooser::empty() const { return m_presetLoader->getNumPresets() == 0; - } template -inline std::auto_ptr PresetChooser::weightedRandom(const PresetInputs & presetInputs, PresetOutputs & presetOutputs){ +inline PresetChooser::iterator PresetChooser::weightedRandom() { WeightFunctor weightFunctor(m_presetLoader->getNumPresets()); - return doWeightedSample(weightFunctor, presetInputs, presetOutputs); + return doWeightedSample(weightFunctor); } @@ -215,9 +214,9 @@ inline std::auto_ptr PresetChooser::directoryIndex(std::size_t index, co } template -inline std::auto_ptr PresetChooser::doWeightedSample(WeightFunctor & weightFunctor, const PresetInputs & presetInputs, PresetOutputs & presetOutputs) { +inline PresetChooser::iterator PresetChooser::doWeightedSample(WeightFunctor & weightFunctor) { + - #ifdef WIN32 // Choose a random bounded mass between 0 and 1 float cutoff = ((float)(rand())) / RAND_MAX; @@ -228,20 +227,25 @@ inline std::auto_ptr PresetChooser::doWeightedSample(WeightFunctor & wei float cutoff = ((float)(random())) / RAND_MAX; #endif +#ifdef MACOS + // Choose a random bounded mass between 0 and 1 + float cutoff = ((float)(rand())) / RAND_MAX; +#endif + // Sum up mass, stopping when cutoff is reached. This is the typical // weighted sampling algorithm. float mass = 0; for (PresetIterator pos = this->begin();pos != this->end() ; ++pos) { mass += weightFunctor(*pos); if (mass >= cutoff) - return pos.allocate(presetInputs, presetOutputs); + return pos; } // Just in case something slips through the cracks PresetIterator pos = this->end(); --pos; - return pos.allocate(presetInputs, presetOutputs); + return pos; } #endif diff --git a/src/projectM-engine/RingBuffer.hpp b/src/projectM-engine/RingBuffer.hpp index 02085f1ac..e0d06085d 100644 --- a/src/projectM-engine/RingBuffer.hpp +++ b/src/projectM-engine/RingBuffer.hpp @@ -59,7 +59,7 @@ public: } int current() { - return (current_element); + return (current_element % RING_BUFFER_SIZE); } }; #endif diff --git a/src/projectM-engine/console_interface.cpp b/src/projectM-engine/console_interface.cpp index f102beff4..b5509b0b9 100755 --- a/src/projectM-engine/console_interface.cpp +++ b/src/projectM-engine/console_interface.cpp @@ -186,8 +186,10 @@ void projectM::default_key_handler( projectMEvent event, projectMKeycode keycode if (m_presetChooser->empty()) break; - m_activePreset = m_presetChooser->weightedRandom - (this->presetInputs, this->presetOutputs); + *m_presetPos = m_presetChooser->weightedRandom(); + + m_activePreset = m_presetPos->allocate(this->presetInputs, this->presetOutputs); + assert(m_activePreset.get()); renderer->setPresetName(m_activePreset->presetName()); diff --git a/src/projectM-engine/projectM.cpp b/src/projectM-engine/projectM.cpp index 30de44fae..a5ebb552a 100755 --- a/src/projectM-engine/projectM.cpp +++ b/src/projectM-engine/projectM.cpp @@ -219,11 +219,14 @@ DLLEXPORT void projectM::renderFrame() presetInputs.progress=0.0; presetInputs.frame = 1; - m_activePreset2 = m_presetChooser->weightedRandom - (presetInputs, &m_activePreset->presetOutputs() == &presetOutputs ? presetOutputs2 : presetOutputs); + *m_presetPos = m_presetChooser->weightedRandom(); + + m_activePreset2 = m_presetPos->allocate + (presetInputs, &m_activePreset->presetOutputs() == &presetOutputs ? presetOutputs2 : presetOutputs); + assert(m_activePreset2.get()); renderer->setPresetName(m_activePreset2->presetName()); - + nohard=(int)(presetInputs.fps*3.5); smoothFrame = (int)(presetInputs.fps * smoothDuration); @@ -233,8 +236,10 @@ DLLEXPORT void projectM::renderFrame() { // printf("%f %d %d\n", beatDetect->bass-beatDetect->bass_old,this->frame,this->avgtime); printf("HARD CUT"); - m_activePreset = m_presetChooser->weightedRandom - (presetInputs, presetOutputs); + *m_presetPos = m_presetChooser->weightedRandom (); + + m_activePreset = m_presetPos->allocate(presetInputs, presetOutputs); + assert(m_activePreset.get()); nohard=presetInputs.fps*5; smoothFrame=0; @@ -278,7 +283,7 @@ DLLEXPORT void projectM::renderFrame() { if (smoothFrame == 1) { - m_activePreset = m_activePreset2; + m_activePreset = m_activePreset2; smoothFrame=0; printf("Smooth Finished\n"); }