machine, and I certainly haven't tried to do it with Visual Studio. You're
on your own if you go that route.
-
-
#include <map>
#include <vector>
+#include <Moof/Error.hh>
#include <Moof/Manager.hh>
#include <Moof/Log.hh>
#include <Moof/Script.hh>
void init(const std::string& name)
{
Mf::Script script;
- std::string filePath = Animation::getPath(name);
+ std::string path(name);
+
+ if (!Animation::getPath(path))
+ {
+ Mf::Error(Mf::Error::RESOURCE_NOT_FOUND).raise();
+ }
script.importBaseLibrary();
importLogFunctions(script);
importAnimationBindings(script);
- if (script.doFile(filePath) != Mf::Script::SUCCESS)
+ if (script.doFile(path) != Mf::Script::SUCCESS)
{
std::string str;
script[-1].get(str);
* the "animations" subdirectory of any of the search directories.
*/
-std::string Animation::getPath(const std::string& name)
+bool Animation::getPath(std::string& name)
{
- return Mf::Resource::getPath("animations/" + name + ".lua");
+ return Mf::Resource::getPath(name, "animations/", "lua");
}
void update(Mf::Scalar t, Mf::Scalar dt);
unsigned getFrame() const;
- static std::string getPath(const std::string& name);
+ static bool getPath(std::string& name);
};
mState.script.importStandardLibraries();
importLogFunctions(mState.script);
- std::string loaderPath = Scene::getPath("loader");
- if (loaderPath == "")
+ std::string path("loader");
+ if (!Scene::getPath(path))
{
throw Mf::Error(Mf::Error::RESOURCE_NOT_FOUND, "loader");
}
- Mf::Script::Result status = mState.script.doFile(loaderPath);
+ Mf::Script::Result status = mState.script.doFile(path);
if (status != Mf::Script::SUCCESS)
{
std::string str;
void GameLayer::setProjection(Mf::Scalar width, Mf::Scalar height)
{
- mState.camera.setProjection(cml::rad(45.0), width / height, 1.0, 200.0);
+ mState.camera.setProjection(cml::rad(45.0),
+ width / height,
+ SCALAR(1.0), SCALAR(200.0));
}
// 3. $HOME/.yoinkrc
// 4. YOINKRC (environment)
- std::string path = Mf::Resource::getPath("yoinkrc");
+ std::string path("yoinkrc");
+ Mf::Resource::getPath(path);
#if !defined(_WIN32)
path += ":/etc/yoinkrc";
#include <Moof/View.hh>
-namespace Mf {
-class Settings;
-class View;
+namespace Mf
+{
+ class Settings;
+ class View;
}
+
class Main;
typedef boost::shared_ptr<Main> MainP;
NONE = 0, // -
ALC_INIT, // description
FASTEVENTS_INIT, // description
- OPENAL_INIT, // description
FILE_NOT_FOUND, // path of missing file
+ OPENAL_INIT, // description
RESOURCE_NOT_FOUND, // name of missing resource
SCRIPT_ERROR, // description
SDL_INIT, // description
}
virtual ~Error() throw() {}
- void init(unsigned code, const std::string& what = "")
+ void init(unsigned code = NONE, const std::string& what = "")
{
- mWhat[sizeof(mWhat)-1] = '\0';
strncpy(mWhat, what.c_str(), sizeof(mWhat)-1);
+ mWhat[sizeof(mWhat)-1] = '\0';
mCode = code;
}
}
- bool init(const std::string& name, bool flipped = false)
+ void init(const std::string& name, bool flipped = false)
{
- std::string path = Image::getPath(name);
+ std::string path(name);
logInfo << "opening image file " << path << std::endl;
- FILE* fp = fopen(path.c_str(), "rb");
- if (!fp) return false;
+ FILE* fp = Image::openFile(path);
+ if (!fp) return;
png_byte signature[8];
size_t bytesRead;
pngInfo ? &pngInfo : 0,
pngInfoEnd ? &pngInfoEnd : 0);
fclose(fp);
-
- return mContext;
}
}
-std::string Image::getPath(const std::string& name)
+bool Image::getPath(std::string& name)
{
- if (boost::find_last(name, ".png"))
- {
- return Resource::getPath(name);
- }
- else
- {
- std::string path("images/");
- path += name;
- path += ".png";
- return Resource::getPath(path);
- }
+ return Resource::getPath(name, "images/", "png");
+}
+
+FILE* Image::openFile(std::string& name)
+{
+ return Resource::openFile(name, "images/", "png");
}
void setAsIcon() const;
- static std::string getPath(const std::string& name);
+ static bool getPath(std::string& name);
private:
+ static FILE* openFile(std::string& name);
+
class Impl;
boost::shared_ptr<Impl> mImpl;
};
"%s", text2.c_str());
gtk_window_set_title(GTK_WINDOW(dialog), title.c_str());
- std::string iconPath = Resource::getPath(PACKAGE".png");
- GdkPixbuf* iconPixbuf = gdk_pixbuf_new_from_file(iconPath.c_str(),
- NULL);
- gtk_window_set_icon(GTK_WINDOW(dialog), iconPixbuf);
+ std::string iconPath(PACKAGE".png");
+ if (Resource::getPath(iconPath))
+ {
+ GdkPixbuf* iconPixbuf = gdk_pixbuf_new_from_file(iconPath.c_str(),
+ NULL);
+ gtk_window_set_icon(GTK_WINDOW(dialog), iconPixbuf);
+ }
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
dialog.setInformativeText(text2.c_str());
dialog.setStandardButtons(QMessageBox::Close);
- std::string iconPath = Resource::getPath(PACKAGE".png");
- QIcon icon(iconPath.c_str());
- dialog.setWindowIcon(icon);
+ std::string iconPath(PACKAGE".png");
+ if (Resource::getPath(iconPath))
+ {
+ QIcon icon(iconPath.c_str());
+ dialog.setWindowIcon(icon);
+ }
dialog.exec();
*
**************************************************************************/
-#include <unistd.h>
-
#include <boost/algorithm/string.hpp>
#include "Log.hh"
std::vector<std::string> Resource::gSearchPaths;
-void Resource::addSearchPaths(const std::string& path)
+void Resource::addSearchPaths(const std::string& paths)
{
- std::vector<std::string> paths;
- boost::split(paths, path, boost::is_any_of(":"));
+ std::vector<std::string> pathList;
+ boost::split(pathList, paths, boost::is_any_of(":"));
- addSearchPaths(paths);
+ addSearchPaths(pathList);
}
-void Resource::addSearchPaths(const std::vector<std::string>& path)
+void Resource::addSearchPaths(const std::vector<std::string>& pathList)
{
std::vector<std::string>::const_iterator it;
-
- for (it = path.begin(); it != path.end(); ++it)
+ for (it = pathList.begin(); it != pathList.end(); ++it)
{
- std::string onePath(*it);
+ std::string path(*it);
- ASSERT(!onePath.empty() && "empty search path string");
+ ASSERT(!path.empty() && "empty search path string");
// add a slash if there isn't one already
- if (*onePath.rbegin() != '/')
- {
- onePath += '/';
- }
+ if (*path.rbegin() != '/') path += '/';
#if defined(_WIN32)
- boost::replace_all(onePath, "/", "\\");
+ boost::replace_all(path, "/", "\\");
#endif
- gSearchPaths.push_back(onePath);
- logInfo << "added search path " << onePath << std::endl;
+ gSearchPaths.push_back(path);
+ logInfo << "added search path " << path << std::endl;
}
}
-std::string Resource::getPath(const std::string& name)
+std::string Resource::getPath(const std::string& path,
+ const std::string& prefix,
+ const std::string& extension)
{
- std::vector<std::string>::iterator it;
+ std::string realPath(path);
+ if (getPath(realPath, prefix, extension)) return realPath;
+
+ return std::string();
+}
- std::string path(name);
+bool Resource::getPath(std::string& path,
+ const std::string& prefix,
+ const std::string& extension)
+{
+ FILE* file = openFile(path, prefix, extension);
+ if (file)
+ {
+ fclose(file);
+ return true;
+ }
+ return false;
+}
+
+FILE* Resource::openFile(std::string& path,
+ std::string prefix,
+ const std::string& extension,
+ const std::string& mode)
+{
#if defined(_WIN32)
+ // Windows always has to be the odd one.
boost::replace_all(path, "/", "\\");
+ boost::replace_all(prefix, "/", "\\");
#endif
+ std::vector<std::string> preList;
+ boost::split(preList, prefix, boost::is_any_of(":"));
+ std::vector<std::string> postList;
+ boost::split(postList, extension, boost::is_any_of(":"));
+
+ std::vector<std::string>::iterator it;
for (it = gSearchPaths.begin(); it != gSearchPaths.end(); ++it)
{
- std::string fullPath(*it);
- fullPath += path;
+ std::vector<std::string>::iterator jt;
+ for (jt = preList.begin(); jt != preList.end(); ++jt)
+ {
+ std::vector<std::string>::iterator kt;
+ for (kt = postList.begin(); kt != postList.end(); ++kt)
+ {
+ std::string realPath(*it);
+ realPath += *jt;
+ realPath += path;
+ realPath += ".";
+ realPath += *kt;
+
+ FILE* file = fopen(realPath.c_str(), mode.c_str());
+ if (file)
+ {
+ path = realPath;
+ return file;
+ }
+ }
+ }
- if (access(fullPath.c_str(), R_OK) == 0) return fullPath;
- }
+ // check path relative to search path
+ std::string realPath(*it);
+ realPath += path;
- logWarning << "cannot find resource " << name << std::endl;
+ FILE* file = fopen(realPath.c_str(), mode.c_str());
+ if (file)
+ {
+ path = realPath;
+ return file;
+ }
+ }
- // empty string
- return std::string();
+ // last ditch effort; maybe it's already a path to a valid resource
+ return fopen(path.c_str(), mode.c_str());
}
* Interface for textures, sounds, and other types of resources.
*/
+#include <cstdio>
#include <string>
#include <vector>
* @param directory Path to a directory.
*/
- static void addSearchPaths(const std::string& path);
- static void addSearchPaths(const std::vector<std::string>& path);
+ static void addSearchPaths(const std::string& paths);
+ static void addSearchPaths(const std::vector<std::string>& pathList);
/**
* Get the path to a resource of a given name.
* @return The first path found which resolves to a file.
*/
- static std::string getPath(const std::string& name);
+ static std::string getPath(const std::string& path,
+ const std::string& prefix = "",
+ const std::string& extension = "");
+
+ static bool getPath(std::string& path,
+ const std::string& prefix = "",
+ const std::string& extension = "");
+
+ static FILE* openFile(std::string& path,
+ std::string prefix = "",
+ const std::string& extension = "",
+ const std::string& mode = "rb");
private:
mOggStream.datasource = 0;
}
- std::string path = Sound::getPath(name);
- int result = ov_fopen((char*)path.c_str(), &mOggStream);
+ std::string path(name);
+ if (!Sound::getPath(path))
+ {
+ Error(Error::RESOURCE_NOT_FOUND, path).raise();
+ }
- if (result < 0)
+ if (ov_fopen((char*)path.c_str(), &mOggStream) < 0)
{
- logWarning << "couldn't load sound: " << path << std::endl;
Error(Error::UNKNOWN_AUDIO_FORMAT, path).raise();
}
}
-std::string Sound::getPath(const std::string& name)
+bool Sound::getPath(std::string& name)
{
- if (boost::find_last(name, ".ogg"))
- {
- return Resource::getPath(name);
- }
- else
- {
- std::string path("sounds/");
- path += name;
- path += ".ogg";
- return Resource::getPath(path);
- }
+ return Resource::getPath(name, "sounds/", "ogg");
}
static void setListenerOrientation(const Vector3& forward,
const Vector3& up);
- static std::string getPath(const std::string& name);
+ static bool getPath(std::string& name);
protected:
void init(const std::string& name)
{
- std::string path = Texture::getPath(name);
+ std::string path(name);
+
+ Texture::getPath(path);
mImage = Image::alloc(path);
if (!mImage->isValid())
GLuint Texture::Impl::gObject = 0;
-Texture::Texture(const std::string& name) :
- Image(Texture::getPath(name)),
+Texture::Texture(const std::string& name) : // TODO hmm..
+ Image(name),
// pass through
mImpl(Texture::Impl::getInstance(name)) {}
}
-std::string Texture::getPath(const std::string& name)
+bool Texture::getPath(std::string& name)
{
- if (boost::find_last(name, ".png"))
- {
- return Resource::getPath(name);
- }
- else
- {
- std::string path("textures/");
- path += name;
- path += ".png";
- return Resource::getPath(path);
- }
+ return Resource::getPath(name, "textures/", "png");
}
Orientation what) const;
- static std::string getPath(const std::string& name);
+ static bool getPath(std::string& name);
private:
Mf::Script::Result load(Mf::Settings& settings, Mf::Script& script)
{
- std::string filePath = Scene::getPath(getName());
- if (filePath == "")
+ std::string path(getName());
+ if (!Scene::getPath(path))
{
script.push("the scene file could not be found");
return Mf::Script::FILE_ERROR;
}
importSceneBindings(settings, script);
- return script.doFile(filePath);
+ return script.doFile(path);
}
}
-std::string Scene::getPath(const std::string& name)
+bool Scene::getPath(std::string& name)
{
- return Mf::Resource::getPath("scenes/" + name + ".lua");
+ return Mf::Resource::getPath(name, "scenes/", "lua");
}
std::list<Mf::Ray<2>::Contact>& hits) const;
bool checkForCollision(Character& character);
- static std::string getPath(const std::string& name);
+ static bool getPath(std::string& name);
};
case SDL_KEYUP:
if (event.key.keysym.sym == SDLK_ESCAPE)
{
- parent().removeChild(this);
+ parent().stop();
return true;
}