4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include <unison/vfs/stream.hpp>
21 #include <unison/vfs/sdl/Utils.hpp>
25 SDL_RWops* get_physfs_SDLRWops(const std::string& filename)
27 // check this as PHYSFS seems to be buggy and still returns a
28 // valid pointer in this case
30 throw std::runtime_error("Couldn't open file: empty filename");
32 return Unison::VFS::SDL::Utils::open_physfs_in(filename);
38 #include "physfs_sdl.hpp"
49 static int funcSeek(struct SDL_RWops* context, int offset, int whence)
51 PHYSFS_file* file = (PHYSFS_file*) context->hidden.unknown.data1;
55 res = PHYSFS_seek(file, offset);
58 res = PHYSFS_seek(file, PHYSFS_tell(file) + offset);
61 res = PHYSFS_seek(file, PHYSFS_fileLength(file) + offset);
69 log_warning << "Error seeking in file: " << PHYSFS_getLastError() << std::endl;
73 return (int) PHYSFS_tell(file);
76 static int funcRead(struct SDL_RWops* context, void* ptr, int size, int maxnum)
78 PHYSFS_file* file = (PHYSFS_file*) context->hidden.unknown.data1;
80 int res = PHYSFS_read(file, ptr, size, maxnum);
84 static int funcClose(struct SDL_RWops* context)
86 PHYSFS_file* file = (PHYSFS_file*) context->hidden.unknown.data1;
94 SDL_RWops* get_physfs_SDLRWops(const std::string& filename)
96 // check this as PHYSFS seems to be buggy and still returns a
97 // valid pointer in this case
99 throw std::runtime_error("Couldn't open file: empty filename");
102 PHYSFS_file* file = (PHYSFS_file*) PHYSFS_openRead(filename.c_str());
104 std::stringstream msg;
105 msg << "Couldn't open '" << filename << "': "
106 << PHYSFS_getLastError();
107 throw std::runtime_error(msg.str());
110 SDL_RWops* ops = new SDL_RWops();
112 ops->hidden.unknown.data1 = file;
113 ops->seek = funcSeek;
114 ops->read = funcRead;
116 ops->close = funcClose;