2 // Copyright (C) 2006 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 3 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, see <http://www.gnu.org/licenses/>.
17 #include "physfs/physfs_sdl.hpp"
24 #include "util/log.hpp"
26 #if SDL_VERSION_ATLEAST(1,3,0)
27 typedef size_t st_read_t;
28 typedef long st_seek_t;
30 typedef int st_read_t;
31 typedef int st_seek_t;
34 static st_seek_t funcSeek(struct SDL_RWops* context,
35 st_seek_t offset, int whence)
37 PHYSFS_file* file = (PHYSFS_file*) context->hidden.unknown.data1;
41 res = PHYSFS_seek(file, offset);
44 res = PHYSFS_seek(file, PHYSFS_tell(file) + offset);
47 res = PHYSFS_seek(file, PHYSFS_fileLength(file) + offset);
55 log_warning << "Error seeking in file: " << PHYSFS_getLastError() << std::endl;
59 return (st_seek_t) PHYSFS_tell(file);
62 static st_read_t funcRead(struct SDL_RWops* context, void* ptr,
63 st_read_t size, st_read_t maxnum)
65 PHYSFS_file* file = (PHYSFS_file*) context->hidden.unknown.data1;
67 st_read_t res = PHYSFS_read(file, ptr, size, maxnum);
71 static int funcClose(struct SDL_RWops* context)
73 PHYSFS_file* file = (PHYSFS_file*) context->hidden.unknown.data1;
81 SDL_RWops* get_physfs_SDLRWops(const std::string& filename)
83 // check this as PHYSFS seems to be buggy and still returns a
84 // valid pointer in this case
86 throw std::runtime_error("Couldn't open file: empty filename");
89 PHYSFS_file* file = (PHYSFS_file*) PHYSFS_openRead(filename.c_str());
91 std::stringstream msg;
92 msg << "Couldn't open '" << filename << "': "
93 << PHYSFS_getLastError();
94 throw std::runtime_error(msg.str());
97 SDL_RWops* ops = new SDL_RWops();
99 ops->hidden.unknown.data1 = file;
100 ops->seek = funcSeek;
101 ops->read = funcRead;
103 ops->close = funcClose;