20130427
[gdash.git] / src / framework / shadermanager.cpp
blobad10075c31dbfe4a60e2f152b6a83a4813511c4a
1 /*
2 * Copyright (c) 2007-2013, Czirkos Zoltan http://code.google.com/p/gdash/
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
19 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
20 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 #include "config.h"
26 #include <glib.h>
28 #include "misc/logger.hpp"
29 #include "misc/printf.hpp"
30 #include "misc/autogfreeptr.hpp"
31 #include "framework/shadermanager.hpp"
32 #include "settings.hpp"
35 static void add_dir_to_shaders(std::vector<std::string> &shaders, const char *directory_name) {
36 gd_debug(CPrintf("checking dir %s for shaders") % directory_name);
38 GDir *dir = g_dir_open(directory_name, 0, NULL);
39 /* silently ignore unable-to-open directories */
40 if (!dir)
41 return;
42 char const *name;
43 while ((name = g_dir_read_name(dir)) != NULL) {
44 AutoGFreePtr<char> filename(g_build_filename(directory_name, name, NULL));
45 AutoGFreePtr<char> lower(g_ascii_strdown(filename, -1));
46 if ((g_str_has_suffix(lower, ".shader") && g_file_test(filename, G_FILE_TEST_IS_REGULAR))) {
47 gd_debug(CPrintf("found shader %s") % filename);
48 shaders.push_back((char*) filename);
51 g_dir_close(dir);
55 /* will create a list of file names which can be used as themes. */
56 /* the first item will be an empty string to represent the default, built-in theme. */
57 void load_shaders_list(std::vector<std::string> &shaders, int &shadernum) {
58 shaders.clear();
60 shaders.push_back(""); /* this symbolizes the empty shader */
61 for (unsigned i = 0; i < gd_shaders_dirs.size(); ++i)
62 add_dir_to_shaders(shaders, gd_shaders_dirs[i].c_str());
64 /* find the current shader. */
65 std::vector<std::string>::iterator it =
66 find(shaders.begin(), shaders.end(), gd_shader);
67 /* if the current shader is not in the list, put it into the list */
68 if (it == shaders.end()) {
69 shaders.push_back(gd_shader);
70 shadernum = shaders.size() - 1;
71 } else {
72 shadernum = it - shaders.begin();