void init(const std::string& name)
{
moof::script script;
- std::string path(name);
+ std::string path = moof::resource::find_file("animations/"+name, "lua");
- if (!resource::find(path))
- {
- throw std::runtime_error("cannot find resource " + name);
- }
+ //if (!resource::find(path))
+ //{
+ //throw std::runtime_error("cannot find resource " + name);
+ //}
script.import_base_library();
moof::log::import(script);
std::map<std::string,Data::Sequence>::iterator it;
it = mData->mSequences.find(name);
-
if (it != mData->mSequences.end())
{
mCurrentSequence = &(*it).second;
state_.script.import_standard_libraries();
moof::log::import(state_.script);
- std::string path("loader");
- if (!moof::resource::find(path))
+ std::string path = moof::resource::find_file("scenes/loader.lua");
+ if (path.empty())
{
throw std::runtime_error("cannot find scene loader script");
}
GameLayer::GameLayer()
{
moof::log_info("about to load sound resource...");
- music_.sample("NightFusionIntro.ogg");
+ music_.sample("sounds/NightFusionIntro.ogg");
music_.loop(true);
- music_.enqueue("NightFusionLoop.ogg");
+ music_.enqueue("sounds/NightFusionLoop.ogg");
music_.position(moof::vector3(10.0, 5.0, 0.0));
mThinkTimer.init(boost::bind(&GameLayer::thinkTimer, this),
{
bool isMute = false;
settings().get("nomusic", isMute);
- if (!isMute) music_.stream();
+ music_.stream();
loadSceneLoader();
advanceScene(settings()); // load the first scene
}
-#include <moof/image.hh>
-#include <moof/backend.hh>
-#include <moof/sprite.hh>
-
-
int main(int argc, char* argv[])
{
- //moof::backend backend;
- //moof::resource::add_search_paths(Main::getSearchPath());
-
- //moof::image hey("textures/AlienWarrior.png");
-
- //return 0;
-
+ moof::backend backend;
// FIXME: This is temporary.
moof::timer reloadTimer(boost::bind(&moof::resource::reload_as_needed),
//iconPath = moof::resource::find_file(iconPath);
//moof::image icon(iconPath);
//icon.set_as_icon();
- moof::image_handle icon(PACKAGE".png");
+ moof::image_handle icon(PACKAGE, "png");
if (icon) icon->set_as_icon();
+ else moof::log_error("no icon loaded");
+ icon.unload();
class moof::video::attributes attributes(settings);
moof::video video(PACKAGE_STRING, attributes);
moof::script::status load(moof::settings& settings, moof::script& script)
{
- std::string path(name());
- if (!resource::find(path))
+ std::string path = moof::resource::find_file("scenes/"+name(), "lua");
+ if (path.empty())
{
script.push("the scene file could not be found");
return moof::script::file_error;
char name[128];
SDL_VideoDriverName(name, sizeof(name));
log_info << "initialized SDL; using video driver `"
- << name << "'" << std::endl;
+ << name << "'" << std::endl;
}
if (FE_Init() != 0)
{
if (--impl::retain_count == 0)
{
+ log_info("shutting down SDL...");
FE_Quit();
SDL_Quit();
tile_width_(1),
tile_height_(1)
{
- FILE* fp = resource::open_file(path);
+ FILE* fp = fopen(path.c_str(), "rb");
if (!fp) throw std::runtime_error("image not found at " + path);
png_byte signature[8];
image_resource_loader()
{
- resource::register_type<image>("png");
+ resource::register_type<image>("png", "textures");
}
~image_resource_loader()
namespace moof {
-void resource::print_types()
-{
-}
-void resource::manage_loader(const std::string& extension, loader_ptr& loader, bool set)
+bool resource::call_registry(const std::string& extension,
+ loader_ptr& loader,
+ registry_action action)
{
- static type_lookup lookup;
+ static type_lookup table;
- if (loader || set)
+ switch (action)
{
- lookup[extension] = loader;
- }
- else
- {
- std::map<std::string,loader_ptr>::iterator it;
- it = lookup.find(extension);
- if (it != lookup.end()) loader = (*it).second;
+ case set:
+ {
+ if (loader) table[extension] = loader;
+ else table.erase(extension);
+ break;
+ }
+
+ case lookup:
+ {
+ std::map<std::string,loader_ptr>::iterator it;
+ it = table.find(extension);
+ if (it != table.end()) loader = (*it).second;
+ break;
+ }
}
+
+ return loader;
}
static std::string search_paths_;
typedef boost::weak_ptr<resource> resource_weakptr;
static hash<std::string,resource_weakptr,hash_function> resource_table_;
-// static member
-//resource::type_lookup_ptr resource::type_lookup_;
-//resource::type_lookup resource::type_lookup_;
+static hash<std::string,std::string,hash_function> prefix_table_;
#ifdef USE_HOTLOADING
}
-resource_ptr resource::load(const std::string& path)
+resource_ptr resource::load(const std::string& name,
+ const std::string& ext)
{
- std::string extension = stlplus::extension_part(path);
+ return load_with_path(find_file(name, ext));
+}
- std::string path1 = path;
- if (!find(path1))
- {
- log_error("trying to load missing resource:", path1);
- return resource_ptr();
- }
+resource_ptr resource::load(const std::string& name)
+{
+ return load_with_path(find_file(name));
+}
+
+
+resource_ptr resource::load_with_path(const std::string& path)
+{
+ std::string extension = stlplus::extension_part(path);
hash<std::string,resource_weakptr,hash_function>::iterator it;
- it = resource_table_.find(path1);
+ it = resource_table_.find(path);
if (it != resource_table_.end())
{
resource_weakptr rsrc = (*it).second;
}
loader_ptr loader;
- manage_loader(extension, loader);
+ call_registry(extension, loader, lookup);
if (loader)
{
- resource_ptr rsrc(loader->load(path1));
- rsrc->set_loader(path1, loader);
- resource_table_[path1] = rsrc;
+ resource_ptr rsrc(loader->load(path));
+ rsrc->set_loader(path, loader);
+ resource_table_[path] = rsrc;
#ifdef USE_HOTLOADING
- int wd = inotify_add_watch(monitor_fd_, path1.c_str(), IN_MODIFY);
+ int wd = inotify_add_watch(monitor_fd_, path.c_str(), IN_MODIFY);
rsrc->set_watch_descriptor(wd);
- monitor_lookup_[wd] = path1;
+ monitor_lookup_[wd] = path;
#endif
log_info("loaded", rsrc.get());
return rsrc;
}
+ log_warning("cannot load resource of unknown type:", path);
return resource_ptr();
}
return find_file(path) != "";
}
+
std::string resource::find_file(const std::string& name)
{
- //log_info("looking for", name, "in", search_paths_);
- //return stlplus::lookup(name, search_paths_, ":");
-
std::vector<std::string> paths;
boost::split(paths, search_paths_, boost::is_any_of(":"));
+ std::string ext = stlplus::extension_part(name);
+ std::string prefix("hi");
+
+ loader_ptr loader;
+ call_registry(ext, loader, lookup);
+ if (loader) prefix = loader->prefix();
+
+ log_info("find_file:", ext, prefix);
+
std::vector<std::string>::iterator it;
for (it = paths.begin(); it != paths.end(); ++it)
{
- *it += "/";
- *it += name;
- log_info("looking for", name, "in", *it);
- if (stlplus::file_exists(*it)) return *it;
+ std::string path = stlplus::create_filespec(*it, name);
+ log_info("looking for", name, "at", path);
+ if (stlplus::file_exists(path)) return path;
+
+ // try it with the prefix added
+ if (!prefix.empty())
+ {
+ *it = stlplus::create_filespec(*it, prefix);
+ path = stlplus::create_filespec(*it, name);
+ log_info("looking for", name, "at", path);
+ if (stlplus::file_exists(path)) return path;
+ }
}
+
+ log_error("cannot find resource file:", name);
return std::string();
}
+std::string resource::find_file(const std::string& name,
+ const std::string& ext)
+{
+ std::string actual_ext = stlplus::extension_part(name);
+ if (actual_ext != ext)
+ {
+ return find_file(stlplus::create_filename(name, ext));
+ }
+ return find_file(name);
+}
+
FILE* resource::open_file(const std::string& path, const std::string& mode)
{
return fopen(find_file(path).c_str(), mode.c_str());
{
public:
- // FIXME: this won't be necessary once the existing code is modified to
+ // XXX: this won't be necessary once the existing code is modified to
// use the resource handles
resource() {}
* \param path The name of the resource to find. Upon successful
* return, this is changed to an absolute path to the resource.
* \return True if a path to a resource was found, false otherwise.
+ * XXX this is legacy
*/
static bool find(const std::string& file);
static std::string find_file(const std::string& name);
+ static std::string find_file(const std::string& name,
+ const std::string& ext);
+
/**
* Get the path to a resource of a given name and open it if a resource
* was found.
* return, this is changed to an absolute path to the resource.
* \param mode The open mode.
* \return The FILE* if the resource was found, 0 otherwise.
+ * XXX deprecated
*/
static FILE* open_file(const std::string& path,
const std::string& mode = "rb");
* \param extension The file extension.
*/
template <class T>
- static void register_type(const std::string& extension)
+ static void register_type(const std::string& extension,
+ const std::string& prefix = "")
{
- //if (!type_lookup_) type_lookup_ = type_lookup_ptr(new type_lookup);
- loader_ptr loader(new specific_loader<T>);
- //(*type_lookup_)[extension] = loader;
- //type_lookup_[extension] = loader;
- manage_loader(extension, loader, true);
+ loader_ptr loader(new specific_loader<T>(prefix));
+ printf("registered type with prefix %s", loader->prefix().c_str());
+ call_registry(extension, loader, set);
}
/**
*/
static void unregister_type(const std::string& extension)
{
- //type_lookup_.erase(extension);
- //type_lookup_->erase(extension);
loader_ptr loader;
- manage_loader(extension, loader, true);
+ call_registry(extension, loader, set);
}
- static resource_ptr load(const std::string& path);
+ static resource_ptr load(const std::string& name);
+ static resource_ptr load(const std::string& name,
+ const std::string& ext);
static resource_ptr reload(std::string& path);
*/
static int reload_as_needed();
- static void print_types();
-
private:
+ static resource_ptr load_with_path(const std::string& path);
+
class loader
{
public:
+ //loader() {}
+ loader(const std::string& prefix) :
+ prefix_(prefix) {}
+
virtual ~loader() {}
virtual resource* load(const std::string& path)
{
return 0;
}
+
+ const std::string& prefix() const
+ {
+ return prefix_;
+ }
+
+
+ private:
+
+ std::string prefix_;
};
typedef boost::shared_ptr<loader> loader_ptr;
{
public:
+ //specific_loader() {}
+ specific_loader(const std::string& prefix) :
+ loader(prefix) {}
+
virtual resource* load(const std::string& path)
{
log_info("loading resource of type ", typeid(T).name());
//typedef boost::shared_ptr<type_lookup> type_lookup_ptr;
//static type_lookup_ptr type_lookup_;
//static type_lookup type_lookup_;
+
+ enum registry_action
+ {
+ lookup,
+ set
+ };
- static void manage_loader(const std::string& extension, loader_ptr& loader, bool set = false);
+ static bool call_registry(const std::string& extension,
+ loader_ptr& loader,
+ registry_action action);
#ifdef USE_HOTLOADING
int wd_;
resource_handle(resource_ptr ptr) :
resource_(ptr) {}
- explicit resource_handle(const std::string& path) :
- resource_(resource::load(path)) {}
+ explicit resource_handle(const std::string& name) :
+ resource_(resource::load(name)) {}
+
+ resource_handle(const std::string& name, const std::string& ext) :
+ resource_(resource::load(name, ext)) {}
+
/**
}
+ /**
+ * Unload the resource associated with this handle.
+ */
+ void unload()
+ {
+ resource_ = resource_ptr();
+ }
+
+
private:
resource_ptr resource_;
#define BUF_SIZE (4096)
#endif
-#define NUM_BUFFERS (16)
+#define NUM_BUFFERS (8)
namespace moof {
alcMakeContextCurrent(0);
alcDestroyContext(al_context);
alcCloseDevice(al_device);
- log_info("unloaded sound device ALSA");
}
}
sound_resource_loader()
{
- resource::register_type<sound_resource>("ogg");
+ resource::register_type<sound_resource>("ogg", "sounds");
}
~sound_resource_loader()
alBufferData(buffer_, format, data, size, freq);
retain_counts_[buffer_] = 1;
- log_warning("ctor buffer:", buffer_);
}
buffer(const buffer& buf)
{
alSourceQueueBuffers(source, 1, &buffer_);
retain();
- log_warning("queued buffer:", buffer_);
}
}
{
ALuint buf = (ALuint)-1;
alSourceUnqueueBuffers(source, 1, &buf);
- log_warning("unqueued buffer:", buf);
return buffer(buf);
}
void set(ALuint source) const
{
- log_warning("set buffer:", buffer_);
if (*this) alSourcei(source, AL_BUFFER, buffer_);
}
{
alDeleteBuffers(1, &buffer_);
retain_counts_.erase(it);
- log_warning("kill buffer:", buffer_);
}
}
sound_resource(const std::string& path)
{
- log_info("audio path is", path);
if (ov_fopen((char*)path.c_str(), &file_) < 0)
{
throw std::runtime_error("problem reading audio: " + path);
}
else if (result == 0 && size > 0)
{
- log_info("loaded", size, "bytes from vorbis");
vorbis_info* info = ov_info(&file_, section);
buffer_ = buffer(data, size,
get_audio_format(info), info->rate);
buf = buffer_;
- log_info("this section is", section);
- log_info("audio format is", get_audio_format(info));
- log_info("audio freq is", info->rate);
return true;
}
else
if (result > 0)
{
- log_info("loaded", result, "bytes from vorbis");
vorbis_info* info = ov_info(&file_, section);
buf = buffer(data, result, get_audio_format(info), info->rate);
sample = ov_pcm_tell(&file_);
- log_info("this section is", section);
- log_info("next sample is", sample);
- log_info("audio format is", get_audio_format(info));
- log_info("audio freq is", info->rate);
return true;
}
init();
}
- impl(const std::string& path)
+ impl(const std::string& name)
{
- log_info("sound::impl constructor");
init();
- enqueue(path);
+ enqueue(name);
}
void init()
sample_ = 0;
alGenSources(1, &source_);
- log_error("alGenSources:", alGetError());
ALfloat zero[] = {0.0f, 0.0f, 0.0f};
alSourcef(source_, AL_PITCH, 1.0f);
alSourcef(source_, AL_GAIN, 1.0f);
alSourcefv(source_, AL_POSITION, zero);
alSourcefv(source_, AL_VELOCITY, zero);
- log_error("init:", alGetError());
}
~impl()
if (handle->read(buf))
{
- log_info("playing source...");
buf.set(source_);
alSourcei(source_, AL_LOOPING, is_looping_);
alSourcePlay(source_);
if (!is_playing_)
{
alSourcei(source_, AL_LOOPING, false);
- log_error("set not looping:", alGetError());
sound_handle handle = queue_.front();
ALint queued = 0;
alGetSourcei(source_, AL_BUFFERS_QUEUED, &queued);
- log_info("buffers queued:", queued);
}
}
0.01, timer::repeat);
}
- log_info("streaming source...");
alSourcePlay(source_);
- log_error("playing:", alGetError());
is_playing_ = true;
}
// begin the next buffer in the queue
handle->read(buf, sample_);
buf.queue(source_);
- log_info("loading new buffer");
}
else if (is_looping_)
{
// reload the same buffer
- log_info("looping same buffer");
-
queue_.push_back(handle);
handle->read(buf, sample_);
buf.queue(source_);
}
- void sample(const std::string& path)
+ void sample(const std::string& name)
{
stop();
alSourcei(source_, AL_BUFFER, AL_NONE);
queue_.clear();
- enqueue(path);
+ enqueue(name);
}
- void enqueue(const std::string& path)
+ void enqueue(const std::string& name)
{
- sound_handle handle = resource::load(path);
+ sound_handle handle = resource::load(name, "ogg");
queue_.push_back(handle);
}
sound::sound(const std::string& path) :
// pass through
- impl_(new sound::impl(path))
-{
- log_info("sound constructor");
-}
+ impl_(new sound::impl(path)) {}
void sound::sample(const std::string& path)
public:
sound();
- explicit sound(const std::string& path);
+ explicit sound(const std::string& name);
- void sample(const std::string& path);
- void enqueue(const std::string& path);
+ void sample(const std::string& name);
+ void enqueue(const std::string& name);
void play();
void stream();
#include <boost/algorithm/string.hpp>
#include <boost/bind.hpp>
+#include <stlplus/portability/file_system.hpp>
#include "dispatcher.hh"
#include "log.hh"
namespace moof {
-sprite::sprite(const std::string& path, int tile)
+sprite::sprite(const std::string& name, int tile)
{
- image_ = resource::load(path);
- image_->tile_coordinates(tile, tile_);
+ image(name);
+ sprite::tile(tile);
}
sprite::sprite(const image_handle& image, int tile) :
image_(image)
{
- image_->tile_coordinates(tile, tile_);
+ sprite::tile(tile);
}
sprite::sprite(const sprite& sprite, int tile)
}
-void sprite::image(const std::string& path)
+void sprite::image(const std::string& name)
{
- image_ = resource::load(path);
- // FIXME what about tiles?
+ image_ = resource::load(name, "png");
}
void sprite::tile(int tile)
public:
sprite() {}
- explicit sprite(const std::string& path, int tile = image::no_tile);
+ explicit sprite(const std::string& name, int tile = image::no_tile);
explicit sprite(const image_handle& image, int tile = image::no_tile);
explicit sprite(const sprite& sprite, int tile = image::no_tile);
- void image(const std::string& path);
+ void image(const std::string& name);
void tile(int tile);
void bind() const;