src/physfs/physfs_sdl.cpp: Add SDL 1.3 compatibility code.
[supertux.git] / src / physfs / physfs_sdl.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
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.
8 //
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.
13 //
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/>.
16
17 #include "physfs/physfs_sdl.hpp"
18
19 #include <physfs.h>
20 #include <sstream>
21 #include <stdexcept>
22 #include <assert.h>
23
24 #include "util/log.hpp"
25
26 #if SDL_VERSION_ATLEAST(1,3,0)
27 typedef size_t st_read_t;
28 typedef long st_seek_t;
29 #else
30 typedef int st_read_t;
31 typedef int st_seek_t;
32 #endif
33
34 static st_seek_t funcSeek(struct SDL_RWops* context,
35     st_seek_t offset, int whence)
36 {
37   PHYSFS_file* file = (PHYSFS_file*) context->hidden.unknown.data1;
38   st_seek_t res;
39   switch(whence) {
40     case SEEK_SET:
41       res = PHYSFS_seek(file, offset);
42       break;
43     case SEEK_CUR:
44       res = PHYSFS_seek(file, PHYSFS_tell(file) + offset);
45       break;
46     case SEEK_END:
47       res = PHYSFS_seek(file, PHYSFS_fileLength(file) + offset);
48       break;
49     default:
50       res = 0;
51       assert(false);
52       break;
53   }
54   if(res == 0) {
55     log_warning << "Error seeking in file: " << PHYSFS_getLastError() << std::endl;
56     return -1;
57   }
58
59   return (st_seek_t) PHYSFS_tell(file);
60 }
61
62 static st_read_t funcRead(struct SDL_RWops* context, void* ptr,
63     st_read_t size, st_read_t maxnum)
64 {
65   PHYSFS_file* file = (PHYSFS_file*) context->hidden.unknown.data1;
66
67   st_read_t res = PHYSFS_read(file, ptr, size, maxnum);
68   return res;
69 }
70
71 static int funcClose(struct SDL_RWops* context)
72 {
73   PHYSFS_file* file = (PHYSFS_file*) context->hidden.unknown.data1;
74
75   PHYSFS_close(file);
76   delete context;
77
78   return 0;
79 }
80
81 SDL_RWops* get_physfs_SDLRWops(const std::string& filename)
82 {
83   // check this as PHYSFS seems to be buggy and still returns a
84   // valid pointer in this case
85   if(filename == "") {
86     throw std::runtime_error("Couldn't open file: empty filename");
87   }
88
89   PHYSFS_file* file = (PHYSFS_file*) PHYSFS_openRead(filename.c_str());
90   if(!file) {
91     std::stringstream msg;
92     msg << "Couldn't open '" << filename << "': "
93         << PHYSFS_getLastError();
94     throw std::runtime_error(msg.str());
95   }
96
97   SDL_RWops* ops = new SDL_RWops();
98   ops->type = 0;
99   ops->hidden.unknown.data1 = file;
100   ops->seek = funcSeek;
101   ops->read = funcRead;
102   ops->write = 0;
103   ops->close = funcClose;
104   return ops;
105 }
106
107 /* EOF */