SDL_surface.h 20 KB

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