1 
2 //          Copyright 2018 - 2022 Michael D. Parker
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 flags, int width, int height, int depth, uint Rmask, uint Gmask, uint Bmask, uint Amask);
72         SDL_Surface* SDL_CreateRGBSurfaceFrom(void* pixels, int width, int height, int depth, int pitch, uint Rmask, uint Gmask, uint Bmask, uint Amask);
73         void SDL_FreeSurface(SDL_Surface* surface);
74         int SDL_SetSurfacePalette(SDL_Surface* surface, SDL_Palette* palette);
75         int SDL_LockSurface(SDL_Surface* surface);
76         int SDL_UnlockSurface(SDL_Surface* surface);
77         SDL_Surface* SDL_LoadBMP_RW(SDL_RWops* src, int freesrc);
78         int SDL_SaveBMP_RW(SDL_Surface* surface, SDL_RWops* dst, int freedst);
79         int SDL_SetSurfaceRLE(SDL_Surface* surface, int flag);
80         int SDL_SetColorKey(SDL_Surface* surface, int flag, uint key);
81         int SDL_GetColorKey(SDL_Surface* surface, uint* key);
82         int SDL_SetSurfaceColorMod(SDL_Surface* surface, ubyte r, ubyte g, ubyte b);
83         int SDL_GetSurfaceColorMod(SDL_Surface* surface, ubyte* r, ubyte* g, ubyte* b);
84         int SDL_SetSurfaceAlphaMod(SDL_Surface* surface, ubyte alpha);
85         int SDL_GetSurfaceAlphaMod(SDL_Surface* surface, ubyte* alpha);
86         int SDL_SetSurfaceBlendMode(SDL_Surface* surface, SDL_BlendMode blendMode);
87         int SDL_GetSurfaceBlendMode(SDL_Surface* surface, SDL_BlendMode* blendMode);
88         SDL_bool SDL_SetClipRect(SDL_Surface* surface, const(SDL_Rect)* rect);
89         void SDL_GetClipRect(SDL_Surface* surface, SDL_Rect* rect);
90         SDL_Surface* SDL_ConvertSurface(SDL_Surface* surface, const(SDL_PixelFormat)* fmt, uint flags);
91         SDL_Surface* SDL_ConvertSurfaceFormat(SDL_Surface* surface,uint pixel_format, uint flags);
92         int SDL_ConvertPixels(int width, int height, uint src_format, const(void)* src, int src_pitch, uint dst_format, void* dst, int dst_pitch);
93         int SDL_FillRect(SDL_Surface* surface, const(SDL_Rect)* rect, uint color);
94         int SDL_FillRects(SDL_Surface* surface, const(SDL_Rect)* rects, int count, uint color);
95         int SDL_UpperBlit(SDL_Surface* src, const(SDL_Rect)* srcrect, SDL_Surface* dst, SDL_Rect* dstrect);
96         int SDL_LowerBlit(SDL_Surface* src, SDL_Rect* srcrect, SDL_Surface* dst, SDL_Rect* dstrect);
97         int SDL_SoftStretch(SDL_Surface* src, const(SDL_Rect)* srcrect, SDL_Surface* dst, const(SDL_Rect)* dstrect);
98         int SDL_UpperBlitScaled(SDL_Surface* src, const(SDL_Rect)* srcrect, SDL_Surface* dst, SDL_Rect* dstrect);
99         int SDL_LowerBlitScaled(SDL_Surface* src, SDL_Rect* srcrect, SDL_Surface* dst, SDL_Rect* dstrect);
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 flags, int width, int height, int depth, uint format);
106             SDL_Surface* SDL_CreateRGBSurfaceWithFormatFrom(void* pixels, int width, int height, int depth, int pitch, uint format);
107         }
108         static if(sdlSupport >= SDLSupport.sdl205) {
109             SDL_Surface* SDL_DuplicateSurface(SDL_Surface* surface);
110         }
111         static if(sdlSupport >= SDLSupport.sdl208) {
112             void SDL_SetYUVConversionMode(SDL_YUV_CONVERSION_MODE mode);
113             SDL_YUV_CONVERSION_MODE SDL_GetYUVConversionMode();
114             SDL_YUV_CONVERSION_MODE SDL_GetYUVConversionModeForResolution(int width, int height);
115         }
116         static if(sdlSupport >= SDLSupport.sdl209) {
117             SDL_bool SDL_HasColorKey(SDL_Surface* surface);
118         }
119         static if(sdlSupport >= SDLSupport.sdl2016) {
120             int SDL_SoftStretchLinear(SDL_Surface* src, const(SDL_Rect)* srcrect, SDL_Surface* dst, const(SDL_Rect)* dstrect);
121         }
122         static if(sdlSupport >= SDLSupport.sdl2018) {
123             int SDL_PremultiplyAlpha(int width, int height, uint src_format, const(void)* src, int src_pitch, uint dst_format, void* dst, int dst_pitch);
124         }
125     }
126 }
127 else {
128     extern(C) @nogc nothrow {alias pSDL_CreateRGBSurface = SDL_Surface* function(uint flags, int width, int height, int depth, uint Rmask, uint Gmask, uint Bmask, uint Amask);
129         alias pSDL_CreateRGBSurfaceFrom = SDL_Surface* function(void* pixels, int width, int height, int depth, int pitch, uint Rmask, uint Gmask, uint Bmask, uint Amask);
130         alias pSDL_FreeSurface = void function(SDL_Surface* surface);
131         alias pSDL_SetSurfacePalette = int function(SDL_Surface* surface, SDL_Palette* palette);
132         alias pSDL_LockSurface = int function(SDL_Surface* surface);
133         alias pSDL_UnlockSurface = int function(SDL_Surface* surface);
134         alias pSDL_LoadBMP_RW = SDL_Surface* function(SDL_RWops* src, int freesrc);
135         alias pSDL_SaveBMP_RW = int function(SDL_Surface* surface, SDL_RWops* dst, int freedst);
136         alias pSDL_SetSurfaceRLE = int function(SDL_Surface* surface, int flag);
137         alias pSDL_SetColorKey = int function(SDL_Surface* surface, int flag, uint key);
138         alias pSDL_GetColorKey = int function(SDL_Surface* surface, uint* key);
139         alias pSDL_SetSurfaceColorMod = int function(SDL_Surface* surface, ubyte r, ubyte g, ubyte b);
140         alias pSDL_GetSurfaceColorMod = int function(SDL_Surface* surface, ubyte* r, ubyte* g, ubyte* b);
141         alias pSDL_SetSurfaceAlphaMod = int function(SDL_Surface* surface, ubyte alpha);
142         alias pSDL_GetSurfaceAlphaMod = int function(SDL_Surface* surface, ubyte* alpha);
143         alias pSDL_SetSurfaceBlendMode = int function(SDL_Surface* surface, SDL_BlendMode blendMode);
144         alias pSDL_GetSurfaceBlendMode = int function(SDL_Surface* surface, SDL_BlendMode* blendMode);
145         alias pSDL_SetClipRect = SDL_bool function(SDL_Surface* surface, const(SDL_Rect)* rect);
146         alias pSDL_GetClipRect = void function(SDL_Surface* surface, SDL_Rect* rect);
147         alias pSDL_ConvertSurface = SDL_Surface* function(SDL_Surface* surface, const(SDL_PixelFormat)* fmt, uint flags);
148         alias pSDL_ConvertSurfaceFormat = SDL_Surface* function(SDL_Surface* surface,uint pixel_format, uint flags);
149         alias pSDL_ConvertPixels = int function(int width, int height, uint src_format, const(void)* src, int src_pitch, uint dst_format, void* dst, int dst_pitch);
150         alias pSDL_FillRect = int function(SDL_Surface* surface, const(SDL_Rect)* rect, uint color);
151         alias pSDL_FillRects = int function(SDL_Surface* surface, const(SDL_Rect)* rects, int count, uint color);
152         alias pSDL_UpperBlit = int function(SDL_Surface* src, const(SDL_Rect)* srcrect, SDL_Surface* dst, SDL_Rect* dstrect);
153         alias pSDL_LowerBlit = int function(SDL_Surface* src, SDL_Rect* srcrect, SDL_Surface* dst, SDL_Rect* dstrect);
154         alias pSDL_SoftStretch = int function(SDL_Surface* src, const(SDL_Rect)* srcrect, SDL_Surface* dst, const(SDL_Rect)* dstrect);
155         alias pSDL_UpperBlitScaled = int function(SDL_Surface* src, const(SDL_Rect)* srcrect, SDL_Surface* dst, SDL_Rect* dstrect);
156         alias pSDL_LowerBlitScaled = int function(SDL_Surface* src, SDL_Rect* srcrect, SDL_Surface* dst, SDL_Rect* dstrect);
157 
158         alias SDL_BlitSurface = SDL_UpperBlit;
159         alias SDL_BlitScaled = SDL_UpperBlitScaled;
160     }
161 
162     __gshared {
163         pSDL_CreateRGBSurface SDL_CreateRGBSurface;
164         pSDL_CreateRGBSurfaceFrom SDL_CreateRGBSurfaceFrom;
165         pSDL_FreeSurface SDL_FreeSurface;
166         pSDL_SetSurfacePalette SDL_SetSurfacePalette;
167         pSDL_LockSurface SDL_LockSurface;
168         pSDL_UnlockSurface SDL_UnlockSurface;
169         pSDL_LoadBMP_RW SDL_LoadBMP_RW;
170         pSDL_SaveBMP_RW SDL_SaveBMP_RW;
171         pSDL_SetSurfaceRLE SDL_SetSurfaceRLE;
172         pSDL_SetColorKey SDL_SetColorKey;
173         pSDL_GetColorKey SDL_GetColorKey;
174         pSDL_SetSurfaceColorMod SDL_SetSurfaceColorMod;
175         pSDL_GetSurfaceColorMod SDL_GetSurfaceColorMod;
176         pSDL_SetSurfaceAlphaMod SDL_SetSurfaceAlphaMod;
177         pSDL_GetSurfaceAlphaMod SDL_GetSurfaceAlphaMod;
178         pSDL_SetSurfaceBlendMode SDL_SetSurfaceBlendMode;
179         pSDL_GetSurfaceBlendMode SDL_GetSurfaceBlendMode;
180         pSDL_SetClipRect SDL_SetClipRect;
181         pSDL_GetClipRect SDL_GetClipRect;
182         pSDL_ConvertSurface SDL_ConvertSurface;
183         pSDL_ConvertSurfaceFormat SDL_ConvertSurfaceFormat;
184         pSDL_ConvertPixels SDL_ConvertPixels;
185         pSDL_FillRect SDL_FillRect;
186         pSDL_FillRects SDL_FillRects;
187         pSDL_UpperBlit SDL_UpperBlit;
188         pSDL_LowerBlit SDL_LowerBlit;
189         pSDL_SoftStretch SDL_SoftStretch;
190         pSDL_UpperBlitScaled SDL_UpperBlitScaled;
191         pSDL_LowerBlitScaled SDL_LowerBlitScaled;
192     }
193 
194     static if(sdlSupport >= SDLSupport.sdl205) {
195         extern(C) @nogc nothrow {
196             alias pSDL_CreateRGBSurfaceWithFormat = SDL_Surface* function(uint flags, int width, int height, int depth, uint format);
197             alias pSDL_CreateRGBSurfaceWithFormatFrom = SDL_Surface* function(void* pixels, int width, int height, int depth, int pitch, uint format);
198         }
199 
200         __gshared {
201             pSDL_CreateRGBSurfaceWithFormat SDL_CreateRGBSurfaceWithFormat;
202             pSDL_CreateRGBSurfaceWithFormatFrom SDL_CreateRGBSurfaceWithFormatFrom;
203         }
204     }
205 
206     static if(sdlSupport >= SDLSupport.sdl205) {
207         extern(C) @nogc nothrow {
208             alias pSDL_DuplicateSurface = SDL_Surface* function(SDL_Surface* surface);
209         }
210 
211         __gshared {
212             pSDL_DuplicateSurface SDL_DuplicateSurface;
213         }
214     }
215 
216     static if(sdlSupport >= SDLSupport.sdl208) {
217         extern(C) @nogc nothrow {
218             alias pSDL_SetYUVConversionMode = void function(SDL_YUV_CONVERSION_MODE mode);
219             alias pSDL_GetYUVConversionMode = SDL_YUV_CONVERSION_MODE function();
220             alias pSDL_GetYUVConversionModeForResolution = SDL_YUV_CONVERSION_MODE function(int width, int height);
221         }
222 
223         __gshared {
224             pSDL_SetYUVConversionMode SDL_SetYUVConversionMode;
225             pSDL_GetYUVConversionMode SDL_GetYUVConversionMode;
226             pSDL_GetYUVConversionModeForResolution SDL_GetYUVConversionModeForResolution;
227         }
228     }
229 
230     static if(sdlSupport >= SDLSupport.sdl209) {
231         extern(C) @nogc nothrow {
232             alias pSDL_HasColorKey = SDL_bool function(SDL_Surface* surface);
233         }
234 
235         __gshared {
236             pSDL_HasColorKey SDL_HasColorKey;
237         }
238     }
239 
240     static if(sdlSupport >= SDLSupport.sdl2016) {
241         extern(C) @nogc nothrow {
242             alias pSDL_SoftStretchLinear = int function(SDL_Surface* src, const(SDL_Rect)* srcrect, SDL_Surface* dst, const(SDL_Rect)* dstrect);
243         }
244 
245         __gshared {
246             pSDL_SoftStretchLinear SDL_SoftStretchLinear;
247         }
248     }
249 
250     static if(sdlSupport >= SDLSupport.sdl2018) {
251         extern(C) @nogc nothrow {
252             alias pSDL_PremultiplyAlpha = int function(int width, int height, uint src_format, const(void)* src, int src_pitch, uint dst_format, void* dst, int dst_pitch);
253         }
254 
255         __gshared {
256             pSDL_PremultiplyAlpha SDL_PremultiplyAlpha;
257         }
258     }
259 }