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.sdlsurface;
8 
9 import bindbc.sdl.config;
10 import bindbc.sdl.bind.sdlblendmode : SDL_BlendMode;
11 import bindbc.sdl.bind.sdlrect : SDL_Rect;
12 import bindbc.sdl.bind.sdlrwops;
13 import bindbc.sdl.bind.sdlpixels : SDL_Palette, SDL_PixelFormat;
14 import bindbc.sdl.bind.sdlstdinc : SDL_bool;
15 
16 enum {
17     SDL_SWSURFACE = 0,
18     SDL_PREALLOC = 0x00000001,
19     SDL_RLEACCEL = 0x00000002,
20     SDL_DONTFREE = 0x00000004,
21 }
22 
23 @nogc nothrow pure
24 bool SDL_MUSTLOCK(const(SDL_Surface)* S)
25 {
26     pragma(inline, true);
27     return (S.flags & SDL_RLEACCEL) != 0;
28 }
29 
30 struct SDL_BlitMap;
31 struct SDL_Surface {
32     int flags;
33     SDL_PixelFormat* format;
34     int w, h;
35     int pitch;
36     void* pixels;
37     void* userdata;
38     int locked;
39     void* lock_data;
40     SDL_Rect clip_rect;
41     SDL_BlitMap* map;
42     int refcount;
43 }
44 
45 extern(C) nothrow alias SDL_blit = int function(SDL_Surface* src, SDL_Rect* srcrect, SDL_Surface* dst, SDL_Rect* dstrect);
46 
47 @nogc nothrow {
48     SDL_Surface* SDL_LoadBMP(const(char)* file) {
49         pragma(inline, true);
50         return SDL_LoadBMP_RW(SDL_RWFromFile(file,"rb"),1);
51     }
52 
53     int SDL_SaveBMP(SDL_Surface* surface,const(char)* file) {
54         pragma(inline, true);
55         return SDL_SaveBMP_RW(surface,SDL_RWFromFile(file,"wb"),1);
56     }
57 }
58 
59 static if(sdlSupport >= SDLSupport.sdl208) {
60     enum SDL_YUV_CONVERSION_MODE {
61         SDL_YUV_CONVERSION_JPEG,
62         SDL_YUV_CONVERSION_BT601,
63         SDL_YUV_CONVERSION_BT709,
64         SDL_YUV_CONVERSION_AUTOMATIC,
65     }
66     mixin(expandEnum!SDL_YUV_CONVERSION_MODE);
67 }
68 
69 static if(staticBinding) {
70     extern(C) @nogc nothrow {
71         SDL_Surface* SDL_CreateRGBSurface(uint,int,int,int,uint,uint,uint,uint);
72         SDL_Surface* SDL_CreateRGBSurfaceFrom(void*,int,int,int,int,uint,uint,uint,uint);
73         void SDL_FreeSurface(SDL_Surface*);
74         int SDL_SetSurfacePalette(SDL_Surface*,SDL_Palette*);
75         int SDL_LockSurface(SDL_Surface*);
76         int SDL_UnlockSurface(SDL_Surface*);
77         SDL_Surface* SDL_LoadBMP_RW(SDL_RWops*,int);
78         int SDL_SaveBMP_RW(SDL_Surface*,SDL_RWops*,int);
79         int SDL_SetSurfaceRLE(SDL_Surface*,int);
80         int SDL_SetColorKey(SDL_Surface*,int,uint);
81         int SDL_GetColorKey(SDL_Surface*,uint*);
82         int SDL_SetSurfaceColorMod(SDL_Surface*,ubyte,ubyte,ubyte);
83         int SDL_GetSurfaceColorMod(SDL_Surface*,ubyte*,ubyte*,ubyte*);
84         int SDL_SetSurfaceAlphaMod(SDL_Surface*,ubyte);
85         int SDL_GetSurfaceAlphaMod(SDL_Surface*,ubyte*);
86         int SDL_SetSurfaceBlendMode(SDL_Surface*,SDL_BlendMode);
87         int SDL_GetSurfaceBlendMode(SDL_Surface*,SDL_BlendMode*);
88         SDL_bool SDL_SetClipRect(SDL_Surface*,const(SDL_Rect)*);
89         void SDL_GetClipRect(SDL_Surface*,SDL_Rect*);
90         SDL_Surface* SDL_ConvertSurface(SDL_Surface*,const(SDL_PixelFormat)*,uint);
91         SDL_Surface* SDL_ConvertSurfaceFormat(SDL_Surface*,uint,uint);
92         int SDL_ConvertPixels(int,int,uint,const(void)*,int,uint,void*,int);
93         int SDL_FillRect(SDL_Surface*,const(SDL_Rect)*,uint);
94         int SDL_FillRects(SDL_Surface*,const(SDL_Rect)*,int,uint);
95         int SDL_UpperBlit(SDL_Surface*,const(SDL_Rect)*,SDL_Surface*,SDL_Rect*);
96         int SDL_LowerBlit(SDL_Surface*,SDL_Rect*,SDL_Surface*,SDL_Rect*);
97         int SDL_SoftStretch(SDL_Surface*,const(SDL_Rect)*,SDL_Surface*,const(SDL_Rect)*);
98         int SDL_UpperBlitScaled(SDL_Surface*,const(SDL_Rect)*,SDL_Surface*,SDL_Rect*);
99         int SDL_LowerBlitScaled(SDL_Surface*,SDL_Rect*,SDL_Surface*,SDL_Rect*);
100 
101         alias SDL_BlitSurface = SDL_UpperBlit;
102         alias SDL_BlitScaled = SDL_UpperBlitScaled;
103 
104         static if(sdlSupport >= SDLSupport.sdl205) {
105             SDL_Surface* SDL_CreateRGBSurfaceWithFormat(uint,int,int,int,uint);
106             SDL_Surface* SDL_CreateRGBSurfaceWithFormatFrom(void*,int,int,int,int,uint);
107         }
108         static if(sdlSupport >= SDLSupport.sdl205) {
109             SDL_Surface* SDL_DuplicateSurface(SDL_Surface*);
110         }
111         static if(sdlSupport >= SDLSupport.sdl208) {
112             void SDL_SetYUVConversionMode(SDL_YUV_CONVERSION_MODE);
113             SDL_YUV_CONVERSION_MODE SDL_GetYUVConversionMode();
114             SDL_YUV_CONVERSION_MODE SDL_GetYUVConversionModeForResolution(int,int);
115         }
116         static if(sdlSupport >= SDLSupport.sdl209) {
117             SDL_bool SDL_HasColorKey(SDL_Surface*);
118         }
119     }
120 }
121 else {
122     extern(C) @nogc nothrow {alias pSDL_CreateRGBSurface = SDL_Surface* function(uint,int,int,int,uint,uint,uint,uint);
123         alias pSDL_CreateRGBSurfaceFrom = SDL_Surface* function(void*,int,int,int,int,uint,uint,uint,uint);
124         alias pSDL_FreeSurface = void function(SDL_Surface*);
125         alias pSDL_SetSurfacePalette = int function(SDL_Surface*,SDL_Palette*);
126         alias pSDL_LockSurface = int function(SDL_Surface*);
127         alias pSDL_UnlockSurface = int function(SDL_Surface*);
128         alias pSDL_LoadBMP_RW = SDL_Surface* function(SDL_RWops*,int);
129         alias pSDL_SaveBMP_RW = int function(SDL_Surface*,SDL_RWops*,int);
130         alias pSDL_SetSurfaceRLE = int function(SDL_Surface*,int);
131         alias pSDL_SetColorKey = int function(SDL_Surface*,int,uint);
132         alias pSDL_GetColorKey = int function(SDL_Surface*,uint*);
133         alias pSDL_SetSurfaceColorMod = int function(SDL_Surface*,ubyte,ubyte,ubyte);
134         alias pSDL_GetSurfaceColorMod = int function(SDL_Surface*,ubyte*,ubyte*,ubyte*);
135         alias pSDL_SetSurfaceAlphaMod = int function(SDL_Surface*,ubyte);
136         alias pSDL_GetSurfaceAlphaMod = int function(SDL_Surface*,ubyte*);
137         alias pSDL_SetSurfaceBlendMode = int function(SDL_Surface*,SDL_BlendMode);
138         alias pSDL_GetSurfaceBlendMode = int function(SDL_Surface*,SDL_BlendMode*);
139         alias pSDL_SetClipRect = SDL_bool function(SDL_Surface*,const(SDL_Rect)*);
140         alias pSDL_GetClipRect = void function(SDL_Surface*,SDL_Rect*);
141         alias pSDL_ConvertSurface = SDL_Surface* function(SDL_Surface*,const(SDL_PixelFormat)*,uint);
142         alias pSDL_ConvertSurfaceFormat = SDL_Surface* function(SDL_Surface*,uint,uint);
143         alias pSDL_ConvertPixels = int function(int,int,uint,const(void)*,int,uint,void*,int);
144         alias pSDL_FillRect = int function(SDL_Surface*,const(SDL_Rect)*,uint);
145         alias pSDL_FillRects = int function(SDL_Surface*,const(SDL_Rect)*,int,uint);
146         alias pSDL_UpperBlit = int function(SDL_Surface*,const(SDL_Rect)*,SDL_Surface*,SDL_Rect*);
147         alias pSDL_LowerBlit = int function(SDL_Surface*,SDL_Rect*,SDL_Surface*,SDL_Rect*);
148         alias pSDL_SoftStretch = int function(SDL_Surface*,const(SDL_Rect)*,SDL_Surface*,const(SDL_Rect)*);
149         alias pSDL_UpperBlitScaled = int function(SDL_Surface*,const(SDL_Rect)*,SDL_Surface*,SDL_Rect*);
150         alias pSDL_LowerBlitScaled = int function(SDL_Surface*,SDL_Rect*,SDL_Surface*,SDL_Rect*);
151 
152         alias SDL_BlitSurface = SDL_UpperBlit;
153         alias SDL_BlitScaled = SDL_UpperBlitScaled;
154     }
155 
156     __gshared {
157         pSDL_CreateRGBSurface SDL_CreateRGBSurface;
158         pSDL_CreateRGBSurfaceFrom SDL_CreateRGBSurfaceFrom;
159         pSDL_FreeSurface SDL_FreeSurface;
160         pSDL_SetSurfacePalette SDL_SetSurfacePalette;
161         pSDL_LockSurface SDL_LockSurface;
162         pSDL_UnlockSurface SDL_UnlockSurface;
163         pSDL_LoadBMP_RW SDL_LoadBMP_RW;
164         pSDL_SaveBMP_RW SDL_SaveBMP_RW;
165         pSDL_SetSurfaceRLE SDL_SetSurfaceRLE;
166         pSDL_SetColorKey SDL_SetColorKey;
167         pSDL_GetColorKey SDL_GetColorKey;
168         pSDL_SetSurfaceColorMod SDL_SetSurfaceColorMod;
169         pSDL_GetSurfaceColorMod SDL_GetSurfaceColorMod;
170         pSDL_SetSurfaceAlphaMod SDL_SetSurfaceAlphaMod;
171         pSDL_GetSurfaceAlphaMod SDL_GetSurfaceAlphaMod;
172         pSDL_SetSurfaceBlendMode SDL_SetSurfaceBlendMode;
173         pSDL_GetSurfaceBlendMode SDL_GetSurfaceBlendMode;
174         pSDL_SetClipRect SDL_SetClipRect;
175         pSDL_GetClipRect SDL_GetClipRect;
176         pSDL_ConvertSurface SDL_ConvertSurface;
177         pSDL_ConvertSurfaceFormat SDL_ConvertSurfaceFormat;
178         pSDL_ConvertPixels SDL_ConvertPixels;
179         pSDL_FillRect SDL_FillRect;
180         pSDL_FillRects SDL_FillRects;
181         pSDL_UpperBlit SDL_UpperBlit;
182         pSDL_LowerBlit SDL_LowerBlit;
183         pSDL_SoftStretch SDL_SoftStretch;
184         pSDL_UpperBlitScaled SDL_UpperBlitScaled;
185         pSDL_LowerBlitScaled SDL_LowerBlitScaled;
186     }
187 
188     static if(sdlSupport >= SDLSupport.sdl205) {
189         extern(C) @nogc nothrow {
190             alias pSDL_CreateRGBSurfaceWithFormat = SDL_Surface* function(uint,int,int,int,uint);
191             alias pSDL_CreateRGBSurfaceWithFormatFrom = SDL_Surface* function(void*,int,int,int,int,uint);
192         }
193 
194         __gshared {
195             pSDL_CreateRGBSurfaceWithFormat SDL_CreateRGBSurfaceWithFormat;
196             pSDL_CreateRGBSurfaceWithFormatFrom SDL_CreateRGBSurfaceWithFormatFrom;
197         }
198     }
199 
200     static if(sdlSupport >= SDLSupport.sdl205) {
201         extern(C) @nogc nothrow {
202             alias pSDL_DuplicateSurface = SDL_Surface* function(SDL_Surface*);
203         }
204 
205         __gshared {
206             pSDL_DuplicateSurface SDL_DuplicateSurface;
207         }
208     }
209 
210     static if(sdlSupport >= SDLSupport.sdl208) {
211         extern(C) @nogc nothrow {
212             alias pSDL_SetYUVConversionMode = void function(SDL_YUV_CONVERSION_MODE);
213             alias pSDL_GetYUVConversionMode = SDL_YUV_CONVERSION_MODE function();
214             alias pSDL_GetYUVConversionModeForResolution = SDL_YUV_CONVERSION_MODE function(int,int);
215         }
216 
217         __gshared {
218             pSDL_SetYUVConversionMode SDL_SetYUVConversionMode;
219             pSDL_GetYUVConversionMode SDL_GetYUVConversionMode;
220             pSDL_GetYUVConversionModeForResolution SDL_GetYUVConversionModeForResolution;
221         }
222     }
223 
224     static if(sdlSupport >= SDLSupport.sdl209) {
225         extern(C) @nogc nothrow {
226             alias pSDL_HasColorKey = SDL_bool function(SDL_Surface*);
227         }
228 
229         __gshared {
230             pSDL_HasColorKey SDL_HasColorKey;
231         }
232     }
233 }