SDL_surface.h 20 KB

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