1#region License
2/* SDL2# - C# Wrapper for SDL2
3 *
4 * Copyright (c) 2013-2021 Ethan Lee.
5 *
6 * This software is provided 'as-is', without any express or implied warranty.
7 * In no event will the authors be held liable for any damages arising from
8 * the use of this software.
9 *
10 * Permission is granted to anyone to use this software for any purpose,
11 * including commercial applications, and to alter it and redistribute it
12 * freely, subject to the following restrictions:
13 *
14 * 1. The origin of this software must not be misrepresented; you must not
15 * claim that you wrote the original software. If you use this software in a
16 * product, an acknowledgment in the product documentation would be
17 * appreciated but is not required.
18 *
19 * 2. Altered source versions must be plainly marked as such, and must not be
20 * misrepresented as being the original software.
21 *
22 * 3. This notice may not be removed or altered from any source distribution.
23 *
24 * Ethan "flibitijibibo" Lee <flibitijibibo@flibitijibibo.com>
25 *
26 */
27#endregion
28
29#region Using Statements
30using System;
31using System.Runtime.InteropServices;
32#endregion
33
34namespace SDL2
35{
36 public static class SDL_image
37 {
38 #region SDL2# Variables
39
40 /* Used by DllImport to load the native library. */
41 private const string nativeLibName = "SDL2_image";
42
43 #endregion
44
45 #region SDL_image.h
46
47 /* Similar to the headers, this is the version we're expecting to be
48 * running with. You will likely want to check this somewhere in your
49 * program!
50 */
51 public const int SDL_IMAGE_MAJOR_VERSION = 2;
52 public const int SDL_IMAGE_MINOR_VERSION = 0;
53 public const int SDL_IMAGE_PATCHLEVEL = 6;
54
55 [Flags]
56 public enum IMG_InitFlags
57 {
58 IMG_INIT_JPG = 0x00000001,
59 IMG_INIT_PNG = 0x00000002,
60 IMG_INIT_TIF = 0x00000004,
61 IMG_INIT_WEBP = 0x00000008
62 }
63
64 public static void SDL_IMAGE_VERSION(out SDL.SDL_version X)
65 {
66 X.major = SDL_IMAGE_MAJOR_VERSION;
67 X.minor = SDL_IMAGE_MINOR_VERSION;
68 X.patch = SDL_IMAGE_PATCHLEVEL;
69 }
70
71 [DllImport(nativeLibName, EntryPoint = "IMG_Linked_Version", CallingConvention = CallingConvention.Cdecl)]
72 private static extern IntPtr INTERNAL_IMG_Linked_Version();
73 public static SDL.SDL_version IMG_Linked_Version()
74 {
75 SDL.SDL_version result;
76 IntPtr result_ptr = INTERNAL_IMG_Linked_Version();
77 result = SDL.PtrToStructure<SDL.SDL_version>(
78 result_ptr
79 );
80 return result;
81 }
82
83 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
84 public static extern int IMG_Init(IMG_InitFlags flags);
85
86 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
87 public static extern void IMG_Quit();
88
89 /* IntPtr refers to an SDL_Surface* */
90 [DllImport(nativeLibName, EntryPoint = "IMG_Load", CallingConvention = CallingConvention.Cdecl)]
91 private static extern unsafe IntPtr INTERNAL_IMG_Load(
92 byte* file
93 );
94 public static unsafe IntPtr IMG_Load(string file)
95 {
96 byte* utf8File = SDL.Utf8EncodeHeap(file);
97 IntPtr handle = INTERNAL_IMG_Load(
98 utf8File
99 );
100 Marshal.FreeHGlobal((IntPtr) utf8File);
101 return handle;
102 }
103
104 /* src refers to an SDL_RWops*, IntPtr to an SDL_Surface* */
105 /* THIS IS A PUBLIC RWops FUNCTION! */
106 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
107 public static extern IntPtr IMG_Load_RW(
108 IntPtr src,
109 int freesrc
110 );
111
112 /* src refers to an SDL_RWops*, IntPtr to an SDL_Surface* */
113 /* THIS IS A PUBLIC RWops FUNCTION! */
114 [DllImport(nativeLibName, EntryPoint = "IMG_LoadTyped_RW", CallingConvention = CallingConvention.Cdecl)]
115 private static extern unsafe IntPtr INTERNAL_IMG_LoadTyped_RW(
116 IntPtr src,
117 int freesrc,
118 byte* type
119 );
120 public static unsafe IntPtr IMG_LoadTyped_RW(
121 IntPtr src,
122 int freesrc,
123 string type
124 ) {
125 int utf8TypeBufSize = SDL.Utf8Size(type);
126 byte* utf8Type = stackalloc byte[utf8TypeBufSize];
127 return INTERNAL_IMG_LoadTyped_RW(
128 src,
129 freesrc,
130 SDL.Utf8Encode(type, utf8Type, utf8TypeBufSize)
131 );
132 }
133
134 /* IntPtr refers to an SDL_Texture*, renderer to an SDL_Renderer* */
135 [DllImport(nativeLibName, EntryPoint = "IMG_LoadTexture", CallingConvention = CallingConvention.Cdecl)]
136 private static extern unsafe IntPtr INTERNAL_IMG_LoadTexture(
137 IntPtr renderer,
138 byte* file
139 );
140 public static unsafe IntPtr IMG_LoadTexture(
141 IntPtr renderer,
142 string file
143 ) {
144 byte* utf8File = SDL.Utf8EncodeHeap(file);
145 IntPtr handle = INTERNAL_IMG_LoadTexture(
146 renderer,
147 utf8File
148 );
149 Marshal.FreeHGlobal((IntPtr) utf8File);
150 return handle;
151 }
152
153 /* renderer refers to an SDL_Renderer*.
154 * src refers to an SDL_RWops*.
155 * IntPtr to an SDL_Texture*.
156 */
157 /* THIS IS A PUBLIC RWops FUNCTION! */
158 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
159 public static extern IntPtr IMG_LoadTexture_RW(
160 IntPtr renderer,
161 IntPtr src,
162 int freesrc
163 );
164
165 /* renderer refers to an SDL_Renderer*.
166 * src refers to an SDL_RWops*.
167 * IntPtr to an SDL_Texture*.
168 */
169 /* THIS IS A PUBLIC RWops FUNCTION! */
170 [DllImport(nativeLibName, EntryPoint = "IMG_LoadTextureTyped_RW", CallingConvention = CallingConvention.Cdecl)]
171 private static extern unsafe IntPtr INTERNAL_IMG_LoadTextureTyped_RW(
172 IntPtr renderer,
173 IntPtr src,
174 int freesrc,
175 byte* type
176 );
177 public static unsafe IntPtr IMG_LoadTextureTyped_RW(
178 IntPtr renderer,
179 IntPtr src,
180 int freesrc,
181 string type
182 ) {
183 byte* utf8Type = SDL.Utf8EncodeHeap(type);
184 IntPtr handle = INTERNAL_IMG_LoadTextureTyped_RW(
185 renderer,
186 src,
187 freesrc,
188 utf8Type
189 );
190 Marshal.FreeHGlobal((IntPtr) utf8Type);
191 return handle;
192 }
193
194 /* IntPtr refers to an SDL_Surface* */
195 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
196 public static extern IntPtr IMG_ReadXPMFromArray(
197 [In()] [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPStr)]
198 string[] xpm
199 );
200
201 /* surface refers to an SDL_Surface* */
202 [DllImport(nativeLibName, EntryPoint = "IMG_SavePNG", CallingConvention = CallingConvention.Cdecl)]
203 private static extern unsafe int INTERNAL_IMG_SavePNG(
204 IntPtr surface,
205 byte* file
206 );
207 public static unsafe int IMG_SavePNG(IntPtr surface, string file)
208 {
209 byte* utf8File = SDL.Utf8EncodeHeap(file);
210 int result = INTERNAL_IMG_SavePNG(
211 surface,
212 utf8File
213 );
214 Marshal.FreeHGlobal((IntPtr) utf8File);
215 return result;
216 }
217
218 /* surface refers to an SDL_Surface*, dst to an SDL_RWops* */
219 /* THIS IS A PUBLIC RWops FUNCTION! */
220 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
221 public static extern int IMG_SavePNG_RW(
222 IntPtr surface,
223 IntPtr dst,
224 int freedst
225 );
226
227 /* surface refers to an SDL_Surface* */
228 [DllImport(nativeLibName, EntryPoint = "IMG_SaveJPG", CallingConvention = CallingConvention.Cdecl)]
229 private static extern unsafe int INTERNAL_IMG_SaveJPG(
230 IntPtr surface,
231 byte* file,
232 int quality
233 );
234 public static unsafe int IMG_SaveJPG(IntPtr surface, string file, int quality)
235 {
236 byte* utf8File = SDL.Utf8EncodeHeap(file);
237 int result = INTERNAL_IMG_SaveJPG(
238 surface,
239 utf8File,
240 quality
241 );
242 Marshal.FreeHGlobal((IntPtr) utf8File);
243 return result;
244 }
245
246 /* surface refers to an SDL_Surface*, dst to an SDL_RWops* */
247 /* THIS IS A PUBLIC RWops FUNCTION! */
248 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
249 public static extern int IMG_SaveJPG_RW(
250 IntPtr surface,
251 IntPtr dst,
252 int freedst,
253 int quality
254 );
255
256 public static string IMG_GetError()
257 {
258 return SDL.SDL_GetError();
259 }
260
261 public static void IMG_SetError(string fmtAndArglist)
262 {
263 SDL.SDL_SetError(fmtAndArglist);
264 }
265
266 #region Animated Image Support
267
268 /* This region is only available in 2.0.6 or higher. */
269
270 public struct IMG_Animation
271 {
272 public int w;
273 public int h;
274 public IntPtr frames; /* SDL_Surface** */
275 public IntPtr delays; /* int* */
276 }
277
278 /* IntPtr refers to an IMG_Animation* */
279 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
280 public static extern IntPtr IMG_LoadAnimation(
281 [In()] [MarshalAs(UnmanagedType.LPStr)]
282 string file
283 );
284
285 /* IntPtr refers to an IMG_Animation*, src to an SDL_RWops* */
286 /* THIS IS A PUBLIC RWops FUNCTION! */
287 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
288 public static extern IntPtr IMG_LoadAnimation_RW(
289 IntPtr src,
290 int freesrc
291 );
292
293 /* IntPtr refers to an IMG_Animation*, src to an SDL_RWops* */
294 /* THIS IS A PUBLIC RWops FUNCTION! */
295 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
296 public static extern IntPtr IMG_LoadAnimationTyped_RW(
297 IntPtr src,
298 int freesrc,
299 [In()] [MarshalAs(UnmanagedType.LPStr)]
300 string type
301 );
302
303 /* anim refers to an IMG_Animation* */
304 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
305 public static extern void IMG_FreeAnimation(IntPtr anim);
306
307 /* IntPtr refers to an IMG_Animation*, src to an SDL_RWops* */
308 /* THIS IS A PUBLIC RWops FUNCTION! */
309 [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)]
310 public static extern IntPtr IMG_LoadGIFAnimation_RW(IntPtr src);
311
312 #endregion
313
314 #endregion
315 }
316}