1 
2 //          Copyright Michael D. Parker 2018.
3 // Distributed under the Boost Software License, Version 1.0.
4 //    (See accompanying file LICENSE_1_0.txt or copy at
5 //          http://www.boost.org/LICENSE_1_0.txt)
6 
7 module bindbc.sdl.bind.sdlrwops;
8 
9 //import core.stdc.stdio : FILE;
10 
11 struct FILE
12 {   
13 }
14 
15 import bindbc.sdl.config;
16 import bindbc.sdl.bind.sdlstdinc : SDL_bool;
17 
18 enum : uint {
19     SDL_RWOPS_UNKNOWN = 0,
20     SDL_RWOPS_WINFILE = 1,
21     SDL_RWOPS_STDFILE = 2,
22     SDL_RWOPS_JNIFILE = 3,
23     SDL_RWOPS_MEMORY = 4,
24     SDL_RWOPS_MEMORY_RO = 5,
25 }
26 
27 struct SDL_RWops {
28     extern(C) @nogc nothrow {
29         long function(SDL_RWops*) size;
30         long function(SDL_RWops*, long, int) seek;
31         size_t function(SDL_RWops*, void*, size_t, size_t) read;
32         size_t function(SDL_RWops*, const(void)*, size_t, size_t) write;
33         int function(SDL_RWops*) close;
34     }
35 
36     uint type;
37 
38     union Hidden {
39         // version(Android)
40         version(Windows) {
41             struct Windowsio {
42                 int append;
43                 void* h;
44                 struct Buffer {
45                     void* data;
46                     size_t size;
47                     size_t left;
48                 }
49                 Buffer buffer;
50             }
51             Windowsio windowsio;
52         }
53 
54         struct Stdio {
55             int autoclose;
56             FILE* fp;
57         }
58         Stdio stdio;
59 
60         struct Mem {
61             ubyte* base;
62             ubyte* here;
63             ubyte* stop;
64         }
65         Mem mem;
66 
67         struct Unknown {
68             void* data1;
69             void* data2;
70         }
71         Unknown unknown;
72     }
73     Hidden hidden;
74 }
75 
76 enum {
77     RW_SEEK_SET = 0,
78     RW_SEEK_CUR = 1,
79     RW_SEEK_END = 2,
80 }
81 
82 static if(sdlSupport < SDLSupport.sdl2010) {
83     @nogc nothrow {
84         long SDL_RWsize(SDL_RWops* ctx) { return ctx.size(ctx); }
85         long SDL_RWseek(SDL_RWops* ctx, long offset, int whence) { return ctx.seek(ctx, offset, whence); }
86         long SDL_RWtell(SDL_RWops* ctx) { return ctx.seek(ctx, 0, RW_SEEK_CUR); }
87         size_t SDL_RWread(SDL_RWops* ctx, void* ptr, size_t size, size_t n) { return ctx.read(ctx, ptr, size, n); }
88         size_t SDL_RWwrite(SDL_RWops* ctx, const(void)* ptr, size_t size, size_t n) { return ctx.write(ctx, ptr, size, n); }
89         int SDL_RWclose(SDL_RWops* ctx) { return ctx.close(ctx); }
90     }
91 }
92 
93 static if(sdlSupport >= SDLSupport.sdl206) {
94     @nogc nothrow
95     void* SDL_LoadFile(const(char)* filename, size_t datasize) {
96         pragma(inline, true);
97         return SDL_LoadFile_RW(SDL_RWFromFile(filename, "rb"), datasize, 1);
98     }
99 }
100 
101 version(BindSDL_Static)  {
102     extern(C) @nogc nothrow {
103         SDL_RWops* SDL_RWFromFile(const(char)*,const(char)*);
104         SDL_RWops* SDL_RWFromFP(FILE*,SDL_bool);
105         SDL_RWops* SDL_RWFromMem(void*,int);
106         SDL_RWops* SDL_RWFromConstMem(const(void)*,int);
107         SDL_RWops* SDL_AllocRW();
108         void SDL_FreeRW(SDL_RWops*);
109         ubyte SDL_ReadU8(SDL_RWops*);
110         ushort SDL_ReadLE16(SDL_RWops*);
111         ushort SDL_ReadBE16(SDL_RWops*);
112         uint SDL_ReadLE32(SDL_RWops*);
113         uint SDL_ReadBE32(SDL_RWops*);
114         ulong SDL_ReadLE64(SDL_RWops*);
115         ulong SDL_ReadBE64(SDL_RWops*);
116         size_t SDL_WriteU8(SDL_RWops*,ubyte);
117         size_t SDL_WriteLE16(SDL_RWops*,ushort);
118         size_t SDL_WriteBE16(SDL_RWops*,ushort);
119         size_t SDL_WriteLE32(SDL_RWops*,uint);
120         size_t SDL_WriteBE32(SDL_RWops*,uint);
121         size_t SDL_WriteLE64(SDL_RWops*,ulong);
122         size_t SDL_WriteBE64(SDL_RWops*,ulong);
123 
124         static if(sdlSupport >= SDLSupport.sdl206) {
125             void* SDL_LoadFile_RW(SDL_RWops*,size_t,int);
126         }
127         static if(sdlSupport >= SDLSupport.sdl2010) {
128             long SDL_RWsize(SDL_RWops*);
129             long SDL_RWseek(SDL_RWops*,long,int);
130             long SDL_RWtell(SDL_RWops*);
131             size_t SDL_RWread(SDL_RWops*,void*,size_t,size_t);
132             size_t SDL_RWwrite(SDL_RWops*,const(void)*,size_t,size_t);
133             int SDL_RWclose(SDL_RWops*);
134         }
135     }
136 }
137 else {
138     extern(C) @nogc nothrow {
139         alias pSDL_RWFromFile = SDL_RWops* function(const(char)*,const(char)*);
140         alias pSDL_RWFromFP = SDL_RWops* function(FILE*,SDL_bool);
141         alias pSDL_RWFromMem = SDL_RWops* function(void*,int);
142         alias pSDL_RWFromConstMem = SDL_RWops* function(const(void)*,int);
143         alias pSDL_AllocRW = SDL_RWops* function();
144         alias pSDL_FreeRW = void function(SDL_RWops*);
145         alias pSDL_ReadU8 = ubyte function(SDL_RWops*);
146         alias pSDL_ReadLE16 = ushort function(SDL_RWops*);
147         alias pSDL_ReadBE16 = ushort function(SDL_RWops*);
148         alias pSDL_ReadLE32 = uint function(SDL_RWops*);
149         alias pSDL_ReadBE32 = uint function(SDL_RWops*);
150         alias pSDL_ReadLE64 = ulong function(SDL_RWops*);
151         alias pSDL_ReadBE64 = ulong function(SDL_RWops*);
152         alias pSDL_WriteU8 = size_t function(SDL_RWops*,ubyte);
153         alias pSDL_WriteLE16 = size_t function(SDL_RWops*,ushort);
154         alias pSDL_WriteBE16 = size_t function(SDL_RWops*,ushort);
155         alias pSDL_WriteLE32 = size_t function(SDL_RWops*,uint);
156         alias pSDL_WriteBE32 = size_t function(SDL_RWops*,uint);
157         alias pSDL_WriteLE64 = size_t function(SDL_RWops*,ulong);
158         alias pSDL_WriteBE64 = size_t function(SDL_RWops*,ulong);
159     }
160     __gshared {
161         pSDL_RWFromFile SDL_RWFromFile;
162         pSDL_RWFromFP SDL_RWFromFP;
163         pSDL_RWFromMem SDL_RWFromMem;
164         pSDL_RWFromConstMem SDL_RWFromConstMem;
165         pSDL_AllocRW SDL_AllocRW;
166         pSDL_FreeRW SDL_FreeRW;
167         pSDL_ReadU8 SDL_ReadU8;
168         pSDL_ReadLE16 SDL_ReadLE16;
169         pSDL_ReadBE16 SDL_ReadBE16;
170         pSDL_ReadLE32 SDL_ReadLE32;
171         pSDL_ReadBE32 SDL_ReadBE32;
172         pSDL_ReadLE64 SDL_ReadLE64;
173         pSDL_ReadBE64 SDL_ReadBE64;
174         pSDL_WriteU8 SDL_WriteU8;
175         pSDL_WriteLE16 SDL_WriteLE16;
176         pSDL_WriteBE16 SDL_WriteBE16;
177         pSDL_WriteLE32 SDL_WriteLE32;
178         pSDL_WriteBE32 SDL_WriteBE32;
179         pSDL_WriteLE64 SDL_WriteLE64;
180         pSDL_WriteBE64 SDL_WriteBE64;
181     }
182     static if(sdlSupport >= SDLSupport.sdl206) {
183         extern(C) @nogc nothrow {
184             alias pSDL_LoadFile_RW = void* function(SDL_RWops*,size_t,int);
185         }
186         __gshared {
187             pSDL_LoadFile_RW SDL_LoadFile_RW;
188         }
189     }
190     static if(sdlSupport >= SDLSupport.sdl2010) {
191         extern(C) @nogc nothrow {
192             alias pSDL_RWsize = long function(SDL_RWops*);
193             alias pSDL_RWseek = long function(SDL_RWops*,long,int);
194             alias pSDL_RWtell = long function(SDL_RWops*);
195             alias pSDL_RWread = size_t function(SDL_RWops*,void*,size_t,size_t);
196             alias pSDL_RWwrite = size_t function(SDL_RWops*,const(void)*,size_t,size_t);
197             alias pSDL_RWclose = int function(SDL_RWops*);
198         }
199         __gshared {
200             pSDL_RWsize SDL_RWsize;
201             pSDL_RWseek SDL_RWseek;
202             pSDL_RWtell SDL_RWtell;
203             pSDL_RWread SDL_RWread;
204             pSDL_RWwrite SDL_RWwrite;
205             pSDL_RWclose SDL_RWclose;
206         }
207     }
208 }