diff --git a/panda/src/rocket/rocketFileInterface.cxx b/panda/src/rocket/rocketFileInterface.cxx index 30ef03899f..f7d91ee76f 100644 --- a/panda/src/rocket/rocketFileInterface.cxx +++ b/panda/src/rocket/rocketFileInterface.cxx @@ -31,86 +31,118 @@ RocketFileInterface(VirtualFileSystem *vfs) : _vfs(vfs) { //////////////////////////////////////////////////////////////////// // Function: RocketFileInterface::Open // Access: Public -// Description: +// Description: //////////////////////////////////////////////////////////////////// Rocket::Core::FileHandle RocketFileInterface:: Open(const Rocket::Core::String& path) { rocket_cat.debug() << "Opening " << path.CString() << "\n"; Filename fn = Filename::from_os_specific(path.CString()); - void *ptr = (void*) _vfs->open_read_file(fn, true); - if (ptr == NULL) { - rocket_cat.error() << "Failed to open " << fn << "\n"; + PT(VirtualFile) file = _vfs->get_file(fn); + if (file == NULL) { + rocket_cat.error() << "Failed to find " << fn << "\n"; + return (Rocket::Core::FileHandle) NULL; } - // A FileHandle is actually just a void pointer - return (Rocket::Core::FileHandle) ptr; + istream *str = file->open_read_file(true); + if (str == NULL) { + rocket_cat.error() << "Failed to open " << fn << " for reading\n"; + return (Rocket::Core::FileHandle) NULL; + } + + VirtualFileHandle *handle = new VirtualFileHandle; + handle->_file = file; + handle->_stream = str; + + // A FileHandle is actually just a void pointer. + return (Rocket::Core::FileHandle) handle; } //////////////////////////////////////////////////////////////////// // Function: RocketFileInterface::Close // Access: Public -// Description: +// Description: //////////////////////////////////////////////////////////////////// void RocketFileInterface:: Close(Rocket::Core::FileHandle file) { - if ((istream*) file != (istream*) NULL) { - _vfs->close_read_file((istream*) file); + VirtualFileHandle *handle = (VirtualFileHandle*) file; + if (handle == NULL) { + return; } + + _vfs->close_read_file(handle->_stream); + delete handle; } //////////////////////////////////////////////////////////////////// // Function: RocketFileInterface::Read // Access: Public -// Description: +// Description: //////////////////////////////////////////////////////////////////// size_t RocketFileInterface:: Read(void* buffer, size_t size, Rocket::Core::FileHandle file) { - istream* const stream = (istream*) file; - if (stream == (istream*) NULL) { + VirtualFileHandle *handle = (VirtualFileHandle*) file; + if (handle == NULL) { return 0; } - stream->read((char*) buffer, size); - return stream->gcount(); + handle->_stream->read((char*) buffer, size); + return handle->_stream->gcount(); } //////////////////////////////////////////////////////////////////// // Function: RocketFileInterface::Seek // Access: Public -// Description: +// Description: //////////////////////////////////////////////////////////////////// bool RocketFileInterface:: Seek(Rocket::Core::FileHandle file, long offset, int origin) { - istream* stream = (istream*) file; - if (stream == (istream*) NULL) { + VirtualFileHandle *handle = (VirtualFileHandle*) file; + if (handle == NULL) { return false; } switch(origin) { case SEEK_SET: - stream->seekg(offset, ios::beg); + handle->_stream->seekg(offset, ios::beg); break; case SEEK_CUR: - stream->seekg(offset, ios::cur); + handle->_stream->seekg(offset, ios::cur); break; case SEEK_END: - stream->seekg(offset, ios::end); + handle->_stream->seekg(offset, ios::end); }; - return !stream->fail(); + return !handle->_stream->fail(); } //////////////////////////////////////////////////////////////////// // Function: RocketFileInterface::Tell -// Access: Public -// Description: +// Access: Public +// Description: //////////////////////////////////////////////////////////////////// size_t RocketFileInterface:: Tell(Rocket::Core::FileHandle file) { - if ((istream*) file == (istream*) NULL) { - return -1; + VirtualFileHandle *handle = (VirtualFileHandle*) file; + if (handle == NULL) { + return 0; } - return ((istream*) file)->tellg(); + + return handle->_stream->tellg(); +} + +//////////////////////////////////////////////////////////////////// +// Function: RocketFileInterface::Length +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +size_t RocketFileInterface:: +Length(Rocket::Core::FileHandle file) { + VirtualFileHandle *handle = (VirtualFileHandle*) file; + if (handle == NULL) { + return 0; + } + + return handle->_file->get_file_size(handle->_stream); } diff --git a/panda/src/rocket/rocketFileInterface.h b/panda/src/rocket/rocketFileInterface.h index b4c7f50d43..3014568e82 100644 --- a/panda/src/rocket/rocketFileInterface.h +++ b/panda/src/rocket/rocketFileInterface.h @@ -16,11 +16,16 @@ #define ROCKET_FILE_INTERFACE_H #include "config_rocket.h" - +#include "virtualFile.h" #include class VirtualFileSystem; +//////////////////////////////////////////////////////////////////// +// Class : RocketFileInterface +// Description : Implementation of FileInterface to allow libRocket +// to read files from the virtual file system. +//////////////////////////////////////////////////////////////////// class RocketFileInterface : public Rocket::Core::FileInterface { public: RocketFileInterface(VirtualFileSystem *vfs = NULL); @@ -33,7 +38,14 @@ public: bool Seek(Rocket::Core::FileHandle file, long offset, int origin); size_t Tell(Rocket::Core::FileHandle file); + size_t Length(Rocket::Core::FileHandle file); + protected: + struct VirtualFileHandle { + PT(VirtualFile) _file; + istream *_stream; + }; + VirtualFileSystem* _vfs; }; diff --git a/panda/src/rocket/rocketRenderInterface.cxx b/panda/src/rocket/rocketRenderInterface.cxx index 521c3f1216..473b1bad65 100644 --- a/panda/src/rocket/rocketRenderInterface.cxx +++ b/panda/src/rocket/rocketRenderInterface.cxx @@ -156,7 +156,7 @@ RenderGeometry(Rocket::Core::Vertex* vertices, int num_vertices, int* indices, i //////////////////////////////////////////////////////////////////// // Function: RocketRenderInterface::CompileGeometry -// Access: Protected +// Access: Protected // Description: Called by Rocket when it wants to compile geometry // it believes will be static for the forseeable future. //////////////////////////////////////////////////////////////////// diff --git a/panda/src/rocket/rocketSystemInterface.h b/panda/src/rocket/rocketSystemInterface.h index 3976830710..9233de94d9 100644 --- a/panda/src/rocket/rocketSystemInterface.h +++ b/panda/src/rocket/rocketSystemInterface.h @@ -20,6 +20,12 @@ #include #include +//////////////////////////////////////////////////////////////////// +// Class : RocketSystemInterface +// Description : This is an implementation of SystemInterface +// that redirects the log output to Panda's notify +// system. +//////////////////////////////////////////////////////////////////// class RocketSystemInterface : public Rocket::Core::SystemInterface { public: float GetElapsedTime();