From e795dbe22b5529309a59f13a87af80b094fac344 Mon Sep 17 00:00:00 2001 From: w1z7ard Date: Thu, 28 Jun 2007 06:10:57 +0000 Subject: [PATCH] added preset loader class, some work on other preset utils git-svn-id: https://projectm.svn.sourceforge.net/svnroot/projectm/personal/carm/dev-1.0@226 6778bc44-b910-0410-a7a0-be141de4315d --- src/projectM-engine/PresetFilters.hpp | 6 +- src/projectM-engine/PresetLoader.cpp | 29 +++++ src/projectM-engine/PresetLoader.hpp | 54 ++++++++ src/projectM-engine/PresetSwitcher.cpp | 170 +++++++++++++++++++++++++ 4 files changed, 256 insertions(+), 3 deletions(-) create mode 100644 src/projectM-engine/PresetLoader.cpp create mode 100644 src/projectM-engine/PresetLoader.hpp diff --git a/src/projectM-engine/PresetFilters.hpp b/src/projectM-engine/PresetFilters.hpp index 49dcfa7ca..f68472c26 100644 --- a/src/projectM-engine/PresetFilters.hpp +++ b/src/projectM-engine/PresetFilters.hpp @@ -17,7 +17,7 @@ public: ; } - Preset & getMergedPreset() { + PresetOutputs & getMergedOutputs() { return m_presetAB; } @@ -27,12 +27,12 @@ public: private: float m_alpha; - Preset & m_presetAB; + PresetOutputs & m_presetAB; }; template -static void merge(const Preset & presetA, const Preset & presetB, MergeFunctor & functor); +static void merge(const PresetOutput & outputsA, const Preset & outputsB, MergeFunctor & functor); diff --git a/src/projectM-engine/PresetLoader.cpp b/src/projectM-engine/PresetLoader.cpp new file mode 100644 index 000000000..4465f84c6 --- /dev/null +++ b/src/projectM-engine/PresetLoader.cpp @@ -0,0 +1,29 @@ +// +// C++ Implementation: PresetLoader +// +// Description: +// +// +// Author: Carmelo Piccione , (C) 2007 +// +// Copyright: See COPYING file that comes with this distribution +// +// +#include "PresetLoader.hpp" +#include +extern "C" { +#include +} + +PresetLoader::PresetLoader(std::string pathame) { + + // Initialize a directory watcher + #ifdef LINUX + if ((m_notifyFD = inotify_init()) == -1) { + std::cerr << "[PresetLoader] failed to initialize inotify instance (error " << errno << std::endl; + } + #endif + + +} + diff --git a/src/projectM-engine/PresetLoader.hpp b/src/projectM-engine/PresetLoader.hpp new file mode 100644 index 000000000..1ba5d636a --- /dev/null +++ b/src/projectM-engine/PresetLoader.hpp @@ -0,0 +1,54 @@ +#ifndef __PRESET_LOADER_HPP +#define __PRESET_LOADER_HPP + +#include "Preset.hpp" + +#ifdef LINUX +extern "C" { + #include +} +#endif + +#include "StaticArray.hpp" + +class PresetLoader { + + + public: + /** Initializes the preset loader with the target directory specified */ + PresetLoader(std::string pathname); + + /** Destructor will remove all alllocated presets */ + ~PresetLoader(); + + /** Load a preset by indexing the collection of presets from a directory */ + auto_ptr loadPreset(unsigned int index); + auto_ptr loadPreset(std::string filename); + + +// void unloadPreset(unsigned int index); +// void unloadPreset(Preset & preset); + + /** Returns the number of presets in the active directory */ + std::size_t getNumPresets(); + + + /** Sets the directory where the loader will search for files */ + void setScanDirectory(std::string pathname); + + /** Clears the preset cache forceably. This cache gets larger on each prseset load- the user of + this class must manually flush out the buffer. + @idea boost shared pointers would be useful here! */ + void flushCache(); + + private: + int m_notifyFD; + const std::string m_dirname; + StaticArray cachedPresets; + + + +}; + + +#endif \ No newline at end of file diff --git a/src/projectM-engine/PresetSwitcher.cpp b/src/projectM-engine/PresetSwitcher.cpp index d199abd5a..ce4ebf94e 100644 --- a/src/projectM-engine/PresetSwitcher.cpp +++ b/src/projectM-engine/PresetSwitcher.cpp @@ -206,6 +206,176 @@ int PresetSwitcher::switchPreset(switch_mode_t switch_mode, int cut_type) { } + +int PresetSwitcher::switchPreset(switch_mode_t switch_mode, int cut_type) { + + Preset * new_preset = 0; + + int switch_index; + int sindex = 0; + int slen = 0; + + DWRITE( "switchPreset(): in\n" ); + DWRITE( "switchPreset(): %s\n", presetURL ); + + switch (switch_mode) { + case ALPHA_NEXT: + preset_index = switch_index = preset_index + 1; + break; + case ALPHA_PREVIOUS: + preset_index = switch_index = preset_index - 1; + break; + case RANDOM_NEXT: + switch_index = rand(); + break; + case RESTART_ACTIVE: + switch_index = preset_index; + break; + default: + return PROJECTM_FAILURE; + } + + DWRITE( "switch_index: %d\n", switch_index ); + + // iterate through the presetURL directory looking for the next entry + { + struct dirent** entries; + int dir_size = scandir(presetURL, &entries, /* is_valid_extension */ NULL, alphasort); + DWRITE( "dir_size: %d\n", dir_size ); + if (dir_size > 0) { + int i; + + DWRITE( "nentries: %d\n", dir_size ); + + switch_index %= dir_size; + if (switch_index < 0) switch_index += dir_size; + for (i = 0; i < dir_size; ++i) { + if (switch_index == i) { + // matching entry + const size_t len = strlen(presetURL); + char* path = (char *) malloc(len + strlen(entries[i]->d_name) + 2); + if (path) { + strcpy(path, presetURL); + if (len && ((path[len - 1] != '/')||(path[len - 1] != '\\'))) { +#ifdef WIN32 + strcat(path + len, "\\"); +#else + strcat(path + len, "/"); +#endif + } + strcat(path + len, entries[i]->d_name); + + new_preset = Preset::load_preset(path); + free(path); + + // we must keep iterating to free the remaining entries + } + } + free(entries[i]); + } + free(entries); + } + } + +#ifdef WIN32 + new_preset = Preset::load_preset( "c:\\tmp\\projectM-1.00\\presets_test\\C.milk" ); +#else +// new_preset = Preset::load_preset( "/Users/descarte/tmp/projectM-1.00/presets_test/B.milk" ); +// new_preset = NULL; +#endif + + if (!new_preset) { + switchToIdlePreset(); + return PROJECTM_ERROR; + } + + + /* Closes a preset currently loaded, if any */ + if ((Preset::active_preset != NULL) && (Preset::active_preset != Preset::idle_preset)) { + Preset::active_preset->close_preset(); + } + + /* Sets global Preset::active_preset pointer */ + Preset::active_preset = new_preset; + +#ifndef PANTS + /** Split out the preset name from the path */ + slen = strlen( new_preset->file_path ); + sindex = slen; + while ( new_preset->file_path[sindex] != WIN32_PATH_SEPARATOR && + new_preset->file_path[sindex] != UNIX_PATH_SEPARATOR && sindex > 0 ) { + sindex--; + } + sindex++; + if ( presetName != NULL ) { + free( presetName ); + presetName = NULL; + } + presetName = (char *)wipemalloc( sizeof( char ) * (slen - sindex + 1) ); + strncpy( presetName, new_preset->file_path + sindex, slen - sindex ); + presetName[slen - sindex] = '\0'; +#endif + + /* Reinitialize the engine variables to sane defaults */ + projectM_resetengine(); + + /* Add any missing initial conditions */ + load_init_conditions(); + + /* Add any missing initial conditions for each wave */ + Preset::active_preset->load_custom_wave_init_conditions(); + +/* Add any missing initial conditions for each shape */ + Preset::active_preset->load_custom_shape_init_conditions(); + + /* Need to evaluate the initial conditions once */ + Preset::active_preset->evalInitConditions(); + Preset::active_preset->evalCustomWaveInitConditions(); + Preset::active_preset->evalCustomShapeInitConditions(); + // evalInitPerFrameEquations(); + return PROJECTM_SUCCESS; +} + +/* Loads a specific preset by absolute path */ +int projectM::loadPresetByFile(char * filename) { + + Preset * new_preset; + + /* Finally, load the preset using its actual path */ + if ((new_preset = Preset::load_preset(filename)) == NULL) { +#ifdef PRESET_DEBUG + printf("loadPresetByFile: failed to load preset!\n"); +#endif + return PROJECTM_ERROR; + } + + /* Closes a preset currently loaded, if any */ + if ((Preset::active_preset != NULL) && (Preset::active_preset != Preset::idle_preset)) + Preset::active_preset->close_preset(); + + /* Sets active preset global pointer */ + Preset::active_preset = new_preset; + + /* Reinitialize engine variables */ + projectM_resetengine(); + + + /* Add any missing initial conditions for each wave */ + Preset::active_preset->load_custom_wave_init_conditions(); + + /* Add any missing initial conditions for each wave */ + Preset::active_preset->load_custom_shape_init_conditions(); + + /* Add any missing initial conditions */ + load_init_conditions(); + + /* Need to do this once for menu */ + Preset::active_preset->evalInitConditions(); + // evalPerFrameInitEquations(); + + return PROJECTM_SUCCESS; +} + /* Returns nonzero if string 'name' contains .milk or (the better) .prjm extension. Not a very strong function currently */ int PresetSwitcher::is_valid_extension(const struct dirent* ent) {