1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
4 
5   This software is provided 'as-is', without any express or implied
6   warranty.  In no event will the authors be held liable for any damages
7   arising from the use of this software.
8 
9   Permission is granted to anyone to use this software for any purpose,
10   including commercial applications, and to alter it and redistribute it
11   freely, subject to the following restrictions:
12 
13   1. The origin of this software must not be misrepresented; you must not
14      claim that you wrote the original software. If you use this software
15      in a product, an acknowledgment in the product documentation would be
16      appreciated but is not required.
17   2. Altered source versions must be plainly marked as such, and must not be
18      misrepresented as being the original software.
19   3. This notice may not be removed or altered from any source distribution.
20 */
21 
22 /**
23  *  \file SDL_surface.h
24  *
25  *  Header file for ::SDL_surface definition and management functions.
26  */
27 
28 #ifndef _SDL_surface_h
29 #define _SDL_surface_h
30 
31 #include "SDL_stdinc.h"
32 #include "SDL_pixels.h"
33 #include "SDL_rect.h"
34 #include "SDL_blendmode.h"
35 #include "SDL_rwops.h"
36 
37 #include "begin_code.h"
38 /* Set up for C function definitions, even when using C++ */
39 #ifdef __cplusplus
40 /* *INDENT-OFF* */
41 extern "C" {
42 /* *INDENT-ON* */
43 #endif
44 
45 /**
46  *  \name Surface flags
47  *
48  *  These are the currently supported flags for the ::SDL_surface.
49  *
50  *  \internal
51  *  Used internally (read-only).
52  */
53 /*@{*/
54 #define SDL_PREALLOC        0x00000001  /**< Surface uses preallocated memory */
55 #define SDL_RLEACCEL        0x00000002  /**< Surface is RLE encoded */
56 #define SDL_DONTFREE        0x00000004  /**< Surface is referenced internally */
57 /*@}*//*Surface flags*/
58 
59 /**
60  *  Evaluates to true if the surface needs to be locked before access.
61  */
62 #define SDL_MUSTLOCK(S)	1
63                         /* XXX Emscripten: we always need to lock.
64                                (((S)->flags & SDL_RLEACCEL) != 0) */
65 
66 /**
67  * \brief A collection of pixels used in software blitting.
68  *
69  * \note  This structure should be treated as read-only, except for \c pixels,
70  *        which, if not NULL, contains the raw pixel data for the surface.
71  */
72 typedef struct SDL_Surface
73 {
74     Uint32 flags;               /**< Read-only */
75     SDL_PixelFormat *format;    /**< Read-only */
76     int w, h;                   /**< Read-only */
77     int pitch;                  /**< Read-only */
78     void *pixels;               /**< Read-write */
79 
80     /** Application data associated with the surface */
81     void *userdata;             /**< Read-write */
82 
83     /** information needed for surfaces requiring locks */
84     int locked;                 /**< Read-only */
85     void *lock_data;            /**< Read-only */
86 
87     /** clipping information */
88     SDL_Rect clip_rect;         /**< Read-only */
89 
90     /** info for fast blit mapping to other surfaces */
91     struct SDL_BlitMap *map;    /**< Private */
92 
93     /** Reference count -- used when freeing surface */
94     int refcount;               /**< Read-mostly */
95 } SDL_Surface;
96 
97 /**
98  * \brief The type of function used for surface blitting functions.
99  */
100 typedef int (*SDL_blit) (struct SDL_Surface * src, SDL_Rect * srcrect,
101                          struct SDL_Surface * dst, SDL_Rect * dstrect);
102 
103 /**
104  *  Allocate and free an RGB surface.
105  *
106  *  If the depth is 4 or 8 bits, an empty palette is allocated for the surface.
107  *  If the depth is greater than 8 bits, the pixel format is set using the
108  *  flags '[RGB]mask'.
109  *
110  *  If the function runs out of memory, it will return NULL.
111  *
112  *  \param flags The \c flags are obsolete and should be set to 0.
113  */
114 extern DECLSPEC SDL_Surface *SDLCALL SDL_CreateRGBSurface
115     (Uint32 flags, int width, int height, int depth,
116      Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask);
117 extern DECLSPEC SDL_Surface *SDLCALL SDL_CreateRGBSurfaceFrom(void *pixels,
118                                                               int width,
119                                                               int height,
120                                                               int depth,
121                                                               int pitch,
122                                                               Uint32 Rmask,
123                                                               Uint32 Gmask,
124                                                               Uint32 Bmask,
125                                                               Uint32 Amask);
126 extern DECLSPEC void SDLCALL SDL_FreeSurface(SDL_Surface * surface);
127 
128 /**
129  *  \brief Set the palette used by a surface.
130  *
131  *  \return 0, or -1 if the surface format doesn't use a palette.
132  *
133  *  \note A single palette can be shared with many surfaces.
134  */
135 extern DECLSPEC int SDLCALL SDL_SetSurfacePalette(SDL_Surface * surface,
136                                                   SDL_Palette * palette);
137 
138 /**
139  *  \brief Sets up a surface for directly accessing the pixels.
140  *
141  *  Between calls to SDL_LockSurface() / SDL_UnlockSurface(), you can write
142  *  to and read from \c surface->pixels, using the pixel format stored in
143  *  \c surface->format.  Once you are done accessing the surface, you should
144  *  use SDL_UnlockSurface() to release it.
145  *
146  *  Not all surfaces require locking.  If SDL_MUSTLOCK(surface) evaluates
147  *  to 0, then you can read and write to the surface at any time, and the
148  *  pixel format of the surface will not change.
149  *
150  *  No operating system or library calls should be made between lock/unlock
151  *  pairs, as critical system locks may be held during this time.
152  *
153  *  SDL_LockSurface() returns 0, or -1 if the surface couldn't be locked.
154  *
155  *  \sa SDL_UnlockSurface()
156  */
157 extern DECLSPEC int SDLCALL SDL_LockSurface(SDL_Surface * surface);
158 /** \sa SDL_LockSurface() */
159 extern DECLSPEC void SDLCALL SDL_UnlockSurface(SDL_Surface * surface);
160 
161 /**
162  *  Load a surface from a seekable SDL data stream (memory or file).
163  *
164  *  If \c freesrc is non-zero, the stream will be closed after being read.
165  *
166  *  The new surface should be freed with SDL_FreeSurface().
167  *
168  *  \return the new surface, or NULL if there was an error.
169  */
170 extern DECLSPEC SDL_Surface *SDLCALL SDL_LoadBMP_RW(SDL_RWops * src,
171                                                     int freesrc);
172 
173 /**
174  *  Load a surface from a file.
175  *
176  *  Convenience macro.
177  */
178 #define SDL_LoadBMP(file)	SDL_LoadBMP_RW(SDL_RWFromFile(file, "rb"), 1)
179 
180 /**
181  *  Save a surface to a seekable SDL data stream (memory or file).
182  *
183  *  If \c freedst is non-zero, the stream will be closed after being written.
184  *
185  *  \return 0 if successful or -1 if there was an error.
186  */
187 extern DECLSPEC int SDLCALL SDL_SaveBMP_RW
188     (SDL_Surface * surface, SDL_RWops * dst, int freedst);
189 
190 /**
191  *  Save a surface to a file.
192  *
193  *  Convenience macro.
194  */
195 #define SDL_SaveBMP(surface, file) \
196 		SDL_SaveBMP_RW(surface, SDL_RWFromFile(file, "wb"), 1)
197 
198 /**
199  *  \brief Sets the RLE acceleration hint for a surface.
200  *
201  *  \return 0 on success, or -1 if the surface is not valid
202  *
203  *  \note If RLE is enabled, colorkey and alpha blending blits are much faster,
204  *        but the surface must be locked before directly accessing the pixels.
205  */
206 extern DECLSPEC int SDLCALL SDL_SetSurfaceRLE(SDL_Surface * surface,
207                                               int flag);
208 
209 /**
210  *  \brief Sets the color key (transparent pixel) in a blittable surface.
211  *
212  *  \param surface The surface to update
213  *  \param flag Non-zero to enable colorkey and 0 to disable colorkey
214  *  \param key The transparent pixel in the native surface format
215  *
216  *  \return 0 on success, or -1 if the surface is not valid
217  */
218 extern DECLSPEC int SDLCALL SDL_SetColorKey(SDL_Surface * surface,
219                                             int flag, Uint32 key);
220 
221 /**
222  *  \brief Gets the color key (transparent pixel) in a blittable surface.
223  *
224  *  \param surface The surface to update
225  *  \param key A pointer filled in with the transparent pixel in the native
226  *             surface format
227  *
228  *  \return 0 on success, or -1 if the surface is not valid or colorkey is not
229  *          enabled.
230  */
231 extern DECLSPEC int SDLCALL SDL_GetColorKey(SDL_Surface * surface,
232                                             Uint32 * key);
233 
234 /**
235  *  \brief Set an additional color value used in blit operations.
236  *
237  *  \param surface The surface to update.
238  *  \param r The red color value multiplied into blit operations.
239  *  \param g The green color value multiplied into blit operations.
240  *  \param b The blue color value multiplied into blit operations.
241  *
242  *  \return 0 on success, or -1 if the surface is not valid.
243  *
244  *  \sa SDL_GetSurfaceColorMod()
245  */
246 extern DECLSPEC int SDLCALL SDL_SetSurfaceColorMod(SDL_Surface * surface,
247                                                    Uint8 r, Uint8 g, Uint8 b);
248 
249 
250 /**
251  *  \brief Get the additional color value used in blit operations.
252  *
253  *  \param surface The surface to query.
254  *  \param r A pointer filled in with the current red color value.
255  *  \param g A pointer filled in with the current green color value.
256  *  \param b A pointer filled in with the current blue color value.
257  *
258  *  \return 0 on success, or -1 if the surface is not valid.
259  *
260  *  \sa SDL_SetSurfaceColorMod()
261  */
262 extern DECLSPEC int SDLCALL SDL_GetSurfaceColorMod(SDL_Surface * surface,
263                                                    Uint8 * r, Uint8 * g,
264                                                    Uint8 * b);
265 
266 /**
267  *  \brief Set an additional alpha value used in blit operations.
268  *
269  *  \param surface The surface to update.
270  *  \param alpha The alpha value multiplied into blit operations.
271  *
272  *  \return 0 on success, or -1 if the surface is not valid.
273  *
274  *  \sa SDL_GetSurfaceAlphaMod()
275  */
276 extern DECLSPEC int SDLCALL SDL_SetSurfaceAlphaMod(SDL_Surface * surface,
277                                                    Uint8 alpha);
278 
279 /**
280  *  \brief Get the additional alpha value used in blit operations.
281  *
282  *  \param surface The surface to query.
283  *  \param alpha A pointer filled in with the current alpha value.
284  *
285  *  \return 0 on success, or -1 if the surface is not valid.
286  *
287  *  \sa SDL_SetSurfaceAlphaMod()
288  */
289 extern DECLSPEC int SDLCALL SDL_GetSurfaceAlphaMod(SDL_Surface * surface,
290                                                    Uint8 * alpha);
291 
292 /**
293  *  \brief Set the blend mode used for blit operations.
294  *
295  *  \param surface The surface to update.
296  *  \param blendMode ::SDL_BlendMode to use for blit blending.
297  *
298  *  \return 0 on success, or -1 if the parameters are not valid.
299  *
300  *  \sa SDL_GetSurfaceBlendMode()
301  */
302 extern DECLSPEC int SDLCALL SDL_SetSurfaceBlendMode(SDL_Surface * surface,
303                                                     SDL_BlendMode blendMode);
304 
305 /**
306  *  \brief Get the blend mode used for blit operations.
307  *
308  *  \param surface   The surface to query.
309  *  \param blendMode A pointer filled in with the current blend mode.
310  *
311  *  \return 0 on success, or -1 if the surface is not valid.
312  *
313  *  \sa SDL_SetSurfaceBlendMode()
314  */
315 extern DECLSPEC int SDLCALL SDL_GetSurfaceBlendMode(SDL_Surface * surface,
316                                                     SDL_BlendMode *blendMode);
317 
318 /**
319  *  Sets the clipping rectangle for the destination surface in a blit.
320  *
321  *  If the clip rectangle is NULL, clipping will be disabled.
322  *
323  *  If the clip rectangle doesn't intersect the surface, the function will
324  *  return SDL_FALSE and blits will be completely clipped.  Otherwise the
325  *  function returns SDL_TRUE and blits to the surface will be clipped to
326  *  the intersection of the surface area and the clipping rectangle.
327  *
328  *  Note that blits are automatically clipped to the edges of the source
329  *  and destination surfaces.
330  */
331 extern DECLSPEC SDL_bool SDLCALL SDL_SetClipRect(SDL_Surface * surface,
332                                                  const SDL_Rect * rect);
333 
334 /**
335  *  Gets the clipping rectangle for the destination surface in a blit.
336  *
337  *  \c rect must be a pointer to a valid rectangle which will be filled
338  *  with the correct values.
339  */
340 extern DECLSPEC void SDLCALL SDL_GetClipRect(SDL_Surface * surface,
341                                              SDL_Rect * rect);
342 
343 /**
344  *  Creates a new surface of the specified format, and then copies and maps
345  *  the given surface to it so the blit of the converted surface will be as
346  *  fast as possible.  If this function fails, it returns NULL.
347  *
348  *  The \c flags parameter is passed to SDL_CreateRGBSurface() and has those
349  *  semantics.  You can also pass ::SDL_RLEACCEL in the flags parameter and
350  *  SDL will try to RLE accelerate colorkey and alpha blits in the resulting
351  *  surface.
352  */
353 extern DECLSPEC SDL_Surface *SDLCALL SDL_ConvertSurface
354     (SDL_Surface * src, SDL_PixelFormat * fmt, Uint32 flags);
355 extern DECLSPEC SDL_Surface *SDLCALL SDL_ConvertSurfaceFormat
356     (SDL_Surface * src, Uint32 pixel_format, Uint32 flags);
357 
358 /**
359  * \brief Copy a block of pixels of one format to another format
360  */
361 extern DECLSPEC int SDLCALL SDL_ConvertPixels(int width, int height,
362                                               Uint32 src_format,
363                                               const void * src, int src_pitch,
364                                               Uint32 dst_format,
365                                               void * dst, int dst_pitch);
366 
367 /**
368  *  Performs a fast fill of the given rectangle with \c color.
369  *
370  *  If \c rect is NULL, the whole surface will be filled with \c color.
371  *
372  *  The color should be a pixel of the format used by the surface, and
373  *  can be generated by the SDL_MapRGB() function.
374  *
375  *  \return 0 on success, or -1 on error.
376  */
377 extern DECLSPEC int SDLCALL SDL_FillRect
378     (SDL_Surface * dst, const SDL_Rect * rect, Uint32 color);
379 extern DECLSPEC int SDLCALL SDL_FillRects
380     (SDL_Surface * dst, const SDL_Rect * rects, int count, Uint32 color);
381 
382 /**
383  *  Performs a fast blit from the source surface to the destination surface.
384  *
385  *  This assumes that the source and destination rectangles are
386  *  the same size.  If either \c srcrect or \c dstrect are NULL, the entire
387  *  surface (\c src or \c dst) is copied.  The final blit rectangles are saved
388  *  in \c srcrect and \c dstrect after all clipping is performed.
389  *
390  *  \return If the blit is successful, it returns 0, otherwise it returns -1.
391  *
392  *  The blit function should not be called on a locked surface.
393  *
394  *  The blit semantics for surfaces with and without alpha and colorkey
395  *  are defined as follows:
396  *  \verbatim
397     RGBA->RGB:
398       SDL_SRCALPHA set:
399         alpha-blend (using alpha-channel).
400         SDL_SRCCOLORKEY ignored.
401       SDL_SRCALPHA not set:
402         copy RGB.
403         if SDL_SRCCOLORKEY set, only copy the pixels matching the
404         RGB values of the source colour key, ignoring alpha in the
405         comparison.
406 
407     RGB->RGBA:
408       SDL_SRCALPHA set:
409         alpha-blend (using the source per-surface alpha value);
410         set destination alpha to opaque.
411       SDL_SRCALPHA not set:
412         copy RGB, set destination alpha to source per-surface alpha value.
413       both:
414         if SDL_SRCCOLORKEY set, only copy the pixels matching the
415         source colour key.
416 
417     RGBA->RGBA:
418       SDL_SRCALPHA set:
419         alpha-blend (using the source alpha channel) the RGB values;
420         leave destination alpha untouched. [Note: is this correct?]
421         SDL_SRCCOLORKEY ignored.
422       SDL_SRCALPHA not set:
423         copy all of RGBA to the destination.
424         if SDL_SRCCOLORKEY set, only copy the pixels matching the
425         RGB values of the source colour key, ignoring alpha in the
426        comparison.
427 
428     RGB->RGB:
429       SDL_SRCALPHA set:
430         alpha-blend (using the source per-surface alpha value).
431       SDL_SRCALPHA not set:
432         copy RGB.
433       both:
434         if SDL_SRCCOLORKEY set, only copy the pixels matching the
435         source colour key.
436     \endverbatim
437  *
438  *  You should call SDL_BlitSurface() unless you know exactly how SDL
439  *  blitting works internally and how to use the other blit functions.
440  */
441 #define SDL_BlitSurface SDL_UpperBlit
442 
443 /**
444  *  This is the public blit function, SDL_BlitSurface(), and it performs
445  *  rectangle validation and clipping before passing it to SDL_LowerBlit()
446  */
447 extern DECLSPEC int SDLCALL SDL_UpperBlit
448     (SDL_Surface * src, const SDL_Rect * srcrect,
449      SDL_Surface * dst, SDL_Rect * dstrect);
450 
451 /**
452  *  This is a semi-private blit function and it performs low-level surface
453  *  blitting only.
454  */
455 extern DECLSPEC int SDLCALL SDL_LowerBlit
456     (SDL_Surface * src, SDL_Rect * srcrect,
457      SDL_Surface * dst, SDL_Rect * dstrect);
458 
459 /**
460  *  \brief Perform a fast, low quality, stretch blit between two surfaces of the
461  *         same pixel format.
462  *
463  *  \note This function uses a static buffer, and is not thread-safe.
464  */
465 extern DECLSPEC int SDLCALL SDL_SoftStretch(SDL_Surface * src,
466                                             const SDL_Rect * srcrect,
467                                             SDL_Surface * dst,
468                                             const SDL_Rect * dstrect);
469 
470 #define SDL_BlitScaled SDL_UpperBlitScaled
471 
472 /**
473  *  This is the public scaled blit function, SDL_BlitScaled(), and it performs
474  *  rectangle validation and clipping before passing it to SDL_LowerBlitScaled()
475  */
476 extern DECLSPEC int SDLCALL SDL_UpperBlitScaled
477     (SDL_Surface * src, const SDL_Rect * srcrect,
478     SDL_Surface * dst, SDL_Rect * dstrect);
479 
480 /**
481  *  This is a semi-private blit function and it performs low-level surface
482  *  scaled blitting only.
483  */
484 extern DECLSPEC int SDLCALL SDL_LowerBlitScaled
485     (SDL_Surface * src, SDL_Rect * srcrect,
486     SDL_Surface * dst, SDL_Rect * dstrect);
487 
488 
489 /* Ends C function definitions when using C++ */
490 #ifdef __cplusplus
491 /* *INDENT-OFF* */
492 }
493 /* *INDENT-ON* */
494 #endif
495 #include "close_code.h"
496 
497 #endif /* _SDL_surface_h */
498 
499 /* vi: set ts=4 sw=4 expandtab: */
500