You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

554 line
20 KiB

  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
  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 Returns whether the surface has a color key
  224. *
  225. * \return SDL_TRUE if the surface has a color key, or SDL_FALSE if the surface is NULL or has no color key
  226. */
  227. extern DECLSPEC SDL_bool SDLCALL SDL_HasColorKey(SDL_Surface * surface);
  228. /**
  229. * \brief Gets the color key (transparent pixel) in a blittable surface.
  230. *
  231. * \param surface The surface to update
  232. * \param key A pointer filled in with the transparent pixel in the native
  233. * surface format
  234. *
  235. * \return 0 on success, or -1 if the surface is not valid or colorkey is not
  236. * enabled.
  237. */
  238. extern DECLSPEC int SDLCALL SDL_GetColorKey(SDL_Surface * surface,
  239. Uint32 * key);
  240. /**
  241. * \brief Set an additional color value used in blit operations.
  242. *
  243. * \param surface The surface to update.
  244. * \param r The red color value multiplied into blit operations.
  245. * \param g The green color value multiplied into blit operations.
  246. * \param b The blue color value multiplied into blit operations.
  247. *
  248. * \return 0 on success, or -1 if the surface is not valid.
  249. *
  250. * \sa SDL_GetSurfaceColorMod()
  251. */
  252. extern DECLSPEC int SDLCALL SDL_SetSurfaceColorMod(SDL_Surface * surface,
  253. Uint8 r, Uint8 g, Uint8 b);
  254. /**
  255. * \brief Get the additional color value used in blit operations.
  256. *
  257. * \param surface The surface to query.
  258. * \param r A pointer filled in with the current red color value.
  259. * \param g A pointer filled in with the current green color value.
  260. * \param b A pointer filled in with the current blue color value.
  261. *
  262. * \return 0 on success, or -1 if the surface is not valid.
  263. *
  264. * \sa SDL_SetSurfaceColorMod()
  265. */
  266. extern DECLSPEC int SDLCALL SDL_GetSurfaceColorMod(SDL_Surface * surface,
  267. Uint8 * r, Uint8 * g,
  268. Uint8 * b);
  269. /**
  270. * \brief Set an additional alpha value used in blit operations.
  271. *
  272. * \param surface The surface to update.
  273. * \param alpha The alpha value multiplied into blit operations.
  274. *
  275. * \return 0 on success, or -1 if the surface is not valid.
  276. *
  277. * \sa SDL_GetSurfaceAlphaMod()
  278. */
  279. extern DECLSPEC int SDLCALL SDL_SetSurfaceAlphaMod(SDL_Surface * surface,
  280. Uint8 alpha);
  281. /**
  282. * \brief Get the additional alpha value used in blit operations.
  283. *
  284. * \param surface The surface to query.
  285. * \param alpha A pointer filled in with the current alpha value.
  286. *
  287. * \return 0 on success, or -1 if the surface is not valid.
  288. *
  289. * \sa SDL_SetSurfaceAlphaMod()
  290. */
  291. extern DECLSPEC int SDLCALL SDL_GetSurfaceAlphaMod(SDL_Surface * surface,
  292. Uint8 * alpha);
  293. /**
  294. * \brief Set the blend mode used for blit operations.
  295. *
  296. * \param surface The surface to update.
  297. * \param blendMode ::SDL_BlendMode to use for blit blending.
  298. *
  299. * \return 0 on success, or -1 if the parameters are not valid.
  300. *
  301. * \sa SDL_GetSurfaceBlendMode()
  302. */
  303. extern DECLSPEC int SDLCALL SDL_SetSurfaceBlendMode(SDL_Surface * surface,
  304. SDL_BlendMode blendMode);
  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. * Sets the clipping rectangle for the destination surface in a blit.
  319. *
  320. * If the clip rectangle is NULL, clipping will be disabled.
  321. *
  322. * If the clip rectangle doesn't intersect the surface, the function will
  323. * return SDL_FALSE and blits will be completely clipped. Otherwise the
  324. * function returns SDL_TRUE and blits to the surface will be clipped to
  325. * the intersection of the surface area and the clipping rectangle.
  326. *
  327. * Note that blits are automatically clipped to the edges of the source
  328. * and destination surfaces.
  329. */
  330. extern DECLSPEC SDL_bool SDLCALL SDL_SetClipRect(SDL_Surface * surface,
  331. const SDL_Rect * rect);
  332. /**
  333. * Gets the clipping rectangle for the destination surface in a blit.
  334. *
  335. * \c rect must be a pointer to a valid rectangle which will be filled
  336. * with the correct values.
  337. */
  338. extern DECLSPEC void SDLCALL SDL_GetClipRect(SDL_Surface * surface,
  339. SDL_Rect * rect);
  340. /*
  341. * Creates a new surface identical to the existing surface
  342. */
  343. extern DECLSPEC SDL_Surface *SDLCALL SDL_DuplicateSurface(SDL_Surface * surface);
  344. /**
  345. * Creates a new surface of the specified format, and then copies and maps
  346. * the given surface to it so the blit of the converted surface will be as
  347. * fast as possible. If this function fails, it returns NULL.
  348. *
  349. * The \c flags parameter is passed to SDL_CreateRGBSurface() and has those
  350. * semantics. You can also pass ::SDL_RLEACCEL in the flags parameter and
  351. * SDL will try to RLE accelerate colorkey and alpha blits in the resulting
  352. * surface.
  353. */
  354. extern DECLSPEC SDL_Surface *SDLCALL SDL_ConvertSurface
  355. (SDL_Surface * src, const SDL_PixelFormat * fmt, Uint32 flags);
  356. extern DECLSPEC SDL_Surface *SDLCALL SDL_ConvertSurfaceFormat
  357. (SDL_Surface * src, Uint32 pixel_format, Uint32 flags);
  358. /**
  359. * \brief Copy a block of pixels of one format to another format
  360. *
  361. * \return 0 on success, or -1 if there was an error
  362. */
  363. extern DECLSPEC int SDLCALL SDL_ConvertPixels(int width, int height,
  364. Uint32 src_format,
  365. const void * src, int src_pitch,
  366. Uint32 dst_format,
  367. void * dst, int dst_pitch);
  368. /**
  369. * Performs a fast fill of the given rectangle with \c color.
  370. *
  371. * If \c rect is NULL, the whole surface will be filled with \c color.
  372. *
  373. * The color should be a pixel of the format used by the surface, and
  374. * can be generated by the SDL_MapRGB() function.
  375. *
  376. * \return 0 on success, or -1 on error.
  377. */
  378. extern DECLSPEC int SDLCALL SDL_FillRect
  379. (SDL_Surface * dst, const SDL_Rect * rect, Uint32 color);
  380. extern DECLSPEC int SDLCALL SDL_FillRects
  381. (SDL_Surface * dst, const SDL_Rect * rects, int count, Uint32 color);
  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 blending and colorkey
  395. * are defined as follows:
  396. * \verbatim
  397. RGBA->RGB:
  398. Source surface blend mode set to SDL_BLENDMODE_BLEND:
  399. alpha-blend (using the source alpha-channel and per-surface alpha)
  400. SDL_SRCCOLORKEY ignored.
  401. Source surface blend mode set to SDL_BLENDMODE_NONE:
  402. copy RGB.
  403. if SDL_SRCCOLORKEY set, only copy the pixels matching the
  404. RGB values of the source color key, ignoring alpha in the
  405. comparison.
  406. RGB->RGBA:
  407. Source surface blend mode set to SDL_BLENDMODE_BLEND:
  408. alpha-blend (using the source per-surface alpha)
  409. Source surface blend mode set to SDL_BLENDMODE_NONE:
  410. copy RGB, set destination alpha to source per-surface alpha value.
  411. both:
  412. if SDL_SRCCOLORKEY set, only copy the pixels matching the
  413. source color key.
  414. RGBA->RGBA:
  415. Source surface blend mode set to SDL_BLENDMODE_BLEND:
  416. alpha-blend (using the source alpha-channel and per-surface alpha)
  417. SDL_SRCCOLORKEY ignored.
  418. Source surface blend mode set to SDL_BLENDMODE_NONE:
  419. copy all of RGBA to the destination.
  420. if SDL_SRCCOLORKEY set, only copy the pixels matching the
  421. RGB values of the source color key, ignoring alpha in the
  422. comparison.
  423. RGB->RGB:
  424. Source surface blend mode set to SDL_BLENDMODE_BLEND:
  425. alpha-blend (using the source per-surface alpha)
  426. Source surface blend mode set to SDL_BLENDMODE_NONE:
  427. copy RGB.
  428. both:
  429. if SDL_SRCCOLORKEY set, only copy the pixels matching the
  430. source color key.
  431. \endverbatim
  432. *
  433. * You should call SDL_BlitSurface() unless you know exactly how SDL
  434. * blitting works internally and how to use the other blit functions.
  435. */
  436. #define SDL_BlitSurface SDL_UpperBlit
  437. /**
  438. * This is the public blit function, SDL_BlitSurface(), and it performs
  439. * rectangle validation and clipping before passing it to SDL_LowerBlit()
  440. */
  441. extern DECLSPEC int SDLCALL SDL_UpperBlit
  442. (SDL_Surface * src, const SDL_Rect * srcrect,
  443. SDL_Surface * dst, SDL_Rect * dstrect);
  444. /**
  445. * This is a semi-private blit function and it performs low-level surface
  446. * blitting only.
  447. */
  448. extern DECLSPEC int SDLCALL SDL_LowerBlit
  449. (SDL_Surface * src, SDL_Rect * srcrect,
  450. SDL_Surface * dst, SDL_Rect * dstrect);
  451. /**
  452. * \brief Perform a fast, low quality, stretch blit between two surfaces of the
  453. * same pixel format.
  454. *
  455. * \note This function uses a static buffer, and is not thread-safe.
  456. */
  457. extern DECLSPEC int SDLCALL SDL_SoftStretch(SDL_Surface * src,
  458. const SDL_Rect * srcrect,
  459. SDL_Surface * dst,
  460. const SDL_Rect * dstrect);
  461. #define SDL_BlitScaled SDL_UpperBlitScaled
  462. /**
  463. * This is the public scaled blit function, SDL_BlitScaled(), and it performs
  464. * rectangle validation and clipping before passing it to SDL_LowerBlitScaled()
  465. */
  466. extern DECLSPEC int SDLCALL SDL_UpperBlitScaled
  467. (SDL_Surface * src, const SDL_Rect * srcrect,
  468. SDL_Surface * dst, SDL_Rect * dstrect);
  469. /**
  470. * This is a semi-private blit function and it performs low-level surface
  471. * scaled blitting only.
  472. */
  473. extern DECLSPEC int SDLCALL SDL_LowerBlitScaled
  474. (SDL_Surface * src, SDL_Rect * srcrect,
  475. SDL_Surface * dst, SDL_Rect * dstrect);
  476. /**
  477. * \brief Set the YUV conversion mode
  478. */
  479. extern DECLSPEC void SDLCALL SDL_SetYUVConversionMode(SDL_YUV_CONVERSION_MODE mode);
  480. /**
  481. * \brief Get the YUV conversion mode
  482. */
  483. extern DECLSPEC SDL_YUV_CONVERSION_MODE SDLCALL SDL_GetYUVConversionMode(void);
  484. /**
  485. * \brief Get the YUV conversion mode, returning the correct mode for the resolution when the current conversion mode is SDL_YUV_CONVERSION_AUTOMATIC
  486. */
  487. extern DECLSPEC SDL_YUV_CONVERSION_MODE SDLCALL SDL_GetYUVConversionModeForResolution(int width, int height);
  488. /* Ends C function definitions when using C++ */
  489. #ifdef __cplusplus
  490. }
  491. #endif
  492. #include "close_code.h"
  493. #endif /* SDL_surface_h_ */
  494. /* vi: set ts=4 sw=4 expandtab: */