SDL_surface.h 17 KB

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