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"
30 static int funcSeek(struct SDL_RWops* context, int offset, int whence)
32 PHYSFS_file* file = (PHYSFS_file*) context->hidden.unknown.data1;
36 res = PHYSFS_seek(file, offset);
39 res = PHYSFS_seek(file, PHYSFS_tell(file) + offset);
42 res = PHYSFS_seek(file, PHYSFS_fileLength(file) + offset);
50 std::cerr << "Error seeking in file: " << PHYSFS_getLastError() << "\n";
54 return (int) PHYSFS_tell(file);
57 static int funcRead(struct SDL_RWops* context, void* ptr, int size, int maxnum)
59 PHYSFS_file* file = (PHYSFS_file*) context->hidden.unknown.data1;
61 int res = PHYSFS_read(file, ptr, size, maxnum);
65 static int funcClose(struct SDL_RWops* context)
67 PHYSFS_file* file = (PHYSFS_file*) context->hidden.unknown.data1;
75 SDL_RWops* get_physfs_SDLRWops(const std::string& filename)
77 PHYSFS_file* file = (PHYSFS_file*) PHYSFS_openRead(filename.c_str());
79 std::stringstream msg;
80 msg << "Couldn't open '" << filename << "': "
81 << PHYSFS_getLastError();
82 throw std::runtime_error(msg.str());
85 SDL_RWops* ops = new SDL_RWops();
87 ops->hidden.unknown.data1 = file;
91 ops->close = funcClose;