2 Copyright (C) 2005 Matthias Braun <matze@braunis.de>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "physfs_sdl.hpp"
31 static int funcSeek(struct SDL_RWops* context, int offset, int whence)
33 PHYSFS_file* file = (PHYSFS_file*) context->hidden.unknown.data1;
37 res = PHYSFS_seek(file, offset);
40 res = PHYSFS_seek(file, PHYSFS_tell(file) + offset);
43 res = PHYSFS_seek(file, PHYSFS_fileLength(file) + offset);
51 msg_warning << "Error seeking in file: " << PHYSFS_getLastError() << std::endl;
55 return (int) PHYSFS_tell(file);
58 static int funcRead(struct SDL_RWops* context, void* ptr, int size, int maxnum)
60 PHYSFS_file* file = (PHYSFS_file*) context->hidden.unknown.data1;
62 int res = PHYSFS_read(file, ptr, size, maxnum);
66 static int funcClose(struct SDL_RWops* context)
68 PHYSFS_file* file = (PHYSFS_file*) context->hidden.unknown.data1;
76 SDL_RWops* get_physfs_SDLRWops(const std::string& filename)
78 PHYSFS_file* file = (PHYSFS_file*) PHYSFS_openRead(filename.c_str());
80 std::stringstream msg;
81 msg << "Couldn't open '" << filename << "': "
82 << PHYSFS_getLastError();
83 throw std::runtime_error(msg.str());
86 SDL_RWops* ops = new SDL_RWops();
88 ops->hidden.unknown.data1 = file;
92 ops->close = funcClose;