1 /+
2 +            Copyright 2022 – 2023 Aya Partridge
3 +          Copyright 2018 - 2022 Michael D. Parker
4 + Distributed under the Boost Software License, Version 1.0.
5 +     (See accompanying file LICENSE_1_0.txt or copy at
6 +           http://www.boost.org/LICENSE_1_0.txt)
7 +/
8 module sdl.surface;
9 
10 import bindbc.sdl.config;
11 import bindbc.sdl.codegen;
12 
13 import sdl.blendmode: SDL_BlendMode;
14 import sdl.rect: SDL_Rect;
15 import sdl.rwops;
16 import sdl.pixels: SDL_Palette, SDL_PixelFormat;
17 import sdl.stdinc: SDL_bool;
18 
19 enum{
20 	SDL_SWSURFACE  = 0x0000_0000,
21 	SDL_PREALLOC   = 0x0000_0001,
22 	SDL_RLEACCEL   = 0x0000_0002,
23 	SDL_DONTFREE   = 0x0000_0004,
24 }
25 
26 pragma(inline, true) bool SDL_MUSTLOCK(const(SDL_Surface)* S) @nogc nothrow pure{
27 	return (S.flags & SDL_RLEACCEL) != 0;
28 }
29 
30 struct SDL_BlitMap;
31 
32 struct SDL_Surface{
33 	int flags;
34 	SDL_PixelFormat* format;
35 	int w, h;
36 	int pitch;
37 	void* pixels;
38 	
39 	void* userdata;
40 	
41 	int locked;
42 	
43 	static if(sdlSupport >= SDLSupport.v2_0_14){
44 		void* list_blitmap;
45 	}else{
46 		void* lock_data;
47 	}
48 	
49 	SDL_Rect clip_rect;
50 	
51 	SDL_BlitMap* map;
52 	
53 	int refcount;
54 }
55 
56 alias SDL_blit = extern(C) int function(SDL_Surface* src, SDL_Rect* srcrect, SDL_Surface* dst, SDL_Rect* dstrect) nothrow;
57 
58 static if(sdlSupport >= SDLSupport.v2_0_8){
59 	alias SDL_YUV_CONVERSION_MODE = int;
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 }
67 
68 pragma(inline, true) @nogc nothrow{
69 	SDL_Surface* SDL_LoadBMP(const(char)* file){
70 		return SDL_LoadBMP_RW(SDL_RWFromFile(file, "rb"), 1);
71 	}
72 
73 	int SDL_SaveBMP(SDL_Surface* surface, const(char)* file){
74 		return SDL_SaveBMP_RW(surface, SDL_RWFromFile(file, "wb"), 1);
75 	}
76 }
77 
78 mixin(joinFnBinds((){
79 	string[][] ret;
80 	ret ~= makeFnBinds([
81 		[q{SDL_Surface*}, q{SDL_CreateRGBSurface}, q{uint flags, int width, int height, int depth, uint Rmask, uint Gmask, uint Bmask, uint Amask}],
82 		[q{SDL_Surface*}, q{SDL_CreateRGBSurfaceFrom}, q{void* pixels, int width, int height, int depth, int pitch, uint Rmask, uint Gmask, uint Bmask, uint Amask}],
83 		[q{void}, q{SDL_FreeSurface}, q{SDL_Surface* surface}],
84 		[q{int}, q{SDL_SetSurfacePalette}, q{SDL_Surface* surface, SDL_Palette* palette}],
85 		[q{int}, q{SDL_LockSurface}, q{SDL_Surface* surface}],
86 		[q{int}, q{SDL_UnlockSurface}, q{SDL_Surface* surface}],
87 		[q{SDL_Surface*}, q{SDL_LoadBMP_RW}, q{SDL_RWops* src, int freesrc}],
88 		[q{int}, q{SDL_SaveBMP_RW}, q{SDL_Surface* surface, SDL_RWops* dst, int freedst}],
89 		[q{int}, q{SDL_SetSurfaceRLE}, q{SDL_Surface* surface, int flag}],
90 		[q{int}, q{SDL_SetColorKey}, q{SDL_Surface* surface, int flag, uint key}],
91 		[q{int}, q{SDL_GetColorKey}, q{SDL_Surface* surface, uint* key}],
92 		[q{int}, q{SDL_SetSurfaceColorMod}, q{SDL_Surface* surface, ubyte r, ubyte g, ubyte b}],
93 		[q{int}, q{SDL_GetSurfaceColorMod}, q{SDL_Surface* surface, ubyte* r, ubyte* g, ubyte* b}],
94 		[q{int}, q{SDL_SetSurfaceAlphaMod}, q{SDL_Surface* surface, ubyte alpha}],
95 		[q{int}, q{SDL_GetSurfaceAlphaMod}, q{SDL_Surface* surface, ubyte* alpha}],
96 		[q{int}, q{SDL_SetSurfaceBlendMode}, q{SDL_Surface* surface, SDL_BlendMode blendMode}],
97 		[q{int}, q{SDL_GetSurfaceBlendMode}, q{SDL_Surface* surface, SDL_BlendMode* blendMode}],
98 		[q{SDL_bool}, q{SDL_SetClipRect}, q{SDL_Surface* surface, const(SDL_Rect)* rect}],
99 		[q{void}, q{SDL_GetClipRect}, q{SDL_Surface* surface, SDL_Rect* rect}],
100 		[q{SDL_Surface*}, q{SDL_ConvertSurface}, q{SDL_Surface* surface, const(SDL_PixelFormat)* fmt, uint flags}],
101 		[q{SDL_Surface*}, q{SDL_ConvertSurfaceFormat}, q{SDL_Surface* surface,uint pixel_format, uint flags}],
102 		[q{int}, q{SDL_ConvertPixels}, q{int width, int height, uint src_format, const(void)* src, int src_pitch, uint dst_format, void* dst, int dst_pitch}],
103 		[q{int}, q{SDL_FillRect}, q{SDL_Surface* surface, const(SDL_Rect)* rect, uint color}],
104 		[q{int}, q{SDL_FillRects}, q{SDL_Surface* surface, const(SDL_Rect)* rects, int count, uint color}],
105 		[q{int}, q{SDL_UpperBlit}, q{SDL_Surface* src, const(SDL_Rect)* srcrect, SDL_Surface* dst, SDL_Rect* dstrect}],
106 		[q{int}, q{SDL_LowerBlit}, q{SDL_Surface* src, SDL_Rect* srcrect, SDL_Surface* dst, SDL_Rect* dstrect}],
107 		[q{int}, q{SDL_SoftStretch}, q{SDL_Surface* src, const(SDL_Rect)* srcrect, SDL_Surface* dst, const(SDL_Rect)* dstrect}],
108 		[q{int}, q{SDL_UpperBlitScaled}, q{SDL_Surface* src, const(SDL_Rect)* srcrect, SDL_Surface* dst, SDL_Rect* dstrect}],
109 		[q{int}, q{SDL_LowerBlitScaled}, q{SDL_Surface* src, SDL_Rect* srcrect, SDL_Surface* dst, SDL_Rect* dstrect}],
110 	]);
111 	ret ~= [q{
112 	alias SDL_BlitSurface = SDL_UpperBlit;
113 	alias SDL_BlitScaled = SDL_UpperBlitScaled;
114 	}];
115 	
116 	static if(sdlSupport >= SDLSupport.v2_0_5){
117 		ret ~= makeFnBinds([
118 			[q{SDL_Surface*}, q{SDL_CreateRGBSurfaceWithFormat}, q{uint flags, int width, int height, int depth, uint format}],
119 			[q{SDL_Surface*}, q{SDL_CreateRGBSurfaceWithFormatFrom}, q{void* pixels, int width, int height, int depth, int pitch, uint format}],
120 		]);
121 	}
122 	static if(sdlSupport >= SDLSupport.v2_0_5){
123 		ret ~= makeFnBinds([
124 			[q{SDL_Surface*}, q{SDL_DuplicateSurface}, q{SDL_Surface* surface}],
125 		]);
126 	}
127 	static if(sdlSupport >= SDLSupport.v2_0_8){
128 		ret ~= makeFnBinds([
129 			[q{void}, q{SDL_SetYUVConversionMode}, q{SDL_YUV_CONVERSION_MODE mode}],
130 			[q{SDL_YUV_CONVERSION_MODE}, q{SDL_GetYUVConversionMode}, q{}],
131 			[q{SDL_YUV_CONVERSION_MODE}, q{SDL_GetYUVConversionModeForResolution}, q{int width, int height}],
132 		]);
133 	}
134 	static if(sdlSupport >= SDLSupport.v2_0_9){
135 		ret ~= makeFnBinds([
136 			[q{SDL_bool}, q{SDL_HasColorKey}, q{SDL_Surface* surface}],
137 		]);
138 	}
139 	static if(sdlSupport >= SDLSupport.v2_0_16){
140 		ret ~= makeFnBinds([
141 			[q{int}, q{SDL_SoftStretchLinear}, q{SDL_Surface* src, const(SDL_Rect)* srcrect, SDL_Surface* dst, const(SDL_Rect)* dstrect}],
142 		]);
143 	}
144 	static if(sdlSupport >= SDLSupport.v2_0_18){
145 		ret ~= makeFnBinds([
146 			[q{int}, q{SDL_PremultiplyAlpha}, q{int width, int height, uint src_format, const(void)* src, int src_pitch, uint dst_format, void* dst, int dst_pitch}],
147 		]);
148 	}
149 	return ret;
150 }()));
151 
152 alias SDL_SetColourKey = SDL_SetColorKey;
153 alias SDL_GetColourKey = SDL_GetColorKey;
154 alias SDL_SetSurfaceColourMod = SDL_SetSurfaceColorMod;
155 alias SDL_GetSurfaceColourMod = SDL_GetSurfaceColorMod;
156 static if(sdlSupport >= SDLSupport.v2_0_9){
157 	alias SDL_HasColourKey = SDL_HasColorKey;
158 }