0e911efdd81c2ac5c681f1e38d95489e5d6e1e2a
[supertux.git] / src / unison / src / vfs / sdl / Utils.cpp
1 //          Copyright Timothy Goya 2007.
2 // Distributed under the Boost Software License, Version 1.0.
3 //    (See accompanying file LICENSE_1_0.txt or copy at
4 //          http://www.boost.org/LICENSE_1_0.txt)
5
6 #include <unison/vfs/sdl/Utils.hpp>
7
8 #include <fstream>
9 #include <assert.h>
10
11 #include <physfs.h>
12 #include "SDL.h"
13
14 namespace
15 {
16    int rwops_seek(SDL_RWops *context, int offset, int whence)
17    {
18       PHYSFS_File *file = reinterpret_cast<PHYSFS_File *>(context->hidden.unknown.data1);
19       int res = 0;
20       switch(whence) {
21           case SEEK_SET:
22               res = PHYSFS_seek(file, offset);
23               break;
24           case SEEK_CUR:
25               res = PHYSFS_seek(file, PHYSFS_tell(file) + offset);
26               break;
27           case SEEK_END:
28               res = PHYSFS_seek(file, PHYSFS_fileLength(file) + offset);
29               break;
30           default:
31               assert(0);
32               break;
33       }
34
35       return (int) PHYSFS_tell(file);
36    }
37
38    int rwops_read(SDL_RWops *context, void *ptr, int size, int maxnum)
39    {
40       PHYSFS_File *file = reinterpret_cast<PHYSFS_File *>(context->hidden.unknown.data1);
41
42       int res = PHYSFS_read(file, ptr, size, maxnum);
43       return res;
44    }
45
46    int rwops_close(SDL_RWops *context)
47    {
48       PHYSFS_File *file = reinterpret_cast<PHYSFS_File *>(context->hidden.unknown.data1);
49
50       PHYSFS_close(file);
51       delete context;
52
53       return 0;
54    }
55 }
56
57
58 namespace Unison
59 {
60    namespace VFS
61    {
62       namespace SDL
63       {
64          SDL_RWops *Utils::open_physfs_in(const std::string &filename)
65          {
66             PHYSFS_File *file = PHYSFS_openRead(filename.c_str());
67             assert(file);
68             SDL_RWops* ops = new SDL_RWops;
69             ops->type = 0;
70             ops->hidden.unknown.data1 = file;
71             ops->seek = rwops_seek;
72             ops->read = rwops_read;
73             ops->write = 0; 
74             ops->close = rwops_close;
75             return ops;
76          }
77       }
78    }
79 }