SDL_surface.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2016 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 (*SDL_blit) (struct SDL_Surface * src, SDL_Rect * srcrect,
  82. struct SDL_Surface * dst, SDL_Rect * dstrect);
  83. /**
  84. * Allocate and free an RGB surface.
  85. *
  86. * If the depth is 4 or 8 bits, an empty palette is allocated for the surface.
  87. * If the depth is greater than 8 bits, the pixel format is set using the
  88. * flags '[RGB]mask'.
  89. *
  90. * If the function runs out of memory, it will return NULL.
  91. *
  92. * \param flags The \c flags are obsolete and should be set to 0.
  93. * \param width The width in pixels of the surface to create.
  94. * \param height The height in pixels of the surface to create.
  95. * \param depth The depth in bits of the surface to create.
  96. * \param Rmask The red mask of the surface to create.
  97. * \param Gmask The green mask of the surface to create.
  98. * \param Bmask The blue mask of the surface to create.
  99. * \param Amask The alpha mask of the surface to create.
  100. */
  101. extern DECLSPEC SDL_Surface *SDLCALL SDL_CreateRGBSurface
  102. (Uint32 flags, int width, int height, int depth,
  103. Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask);
  104. extern DECLSPEC SDL_Surface *SDLCALL SDL_CreateRGBSurfaceFrom(void *pixels,
  105. int width,
  106. int height,
  107. int depth,
  108. int pitch,
  109. Uint32 Rmask,
  110. Uint32 Gmask,
  111. Uint32 Bmask,
  112. Uint32 Amask);
  113. extern DECLSPEC void SDLCALL SDL_FreeSurface(SDL_Surface * surface);
  114. /**
  115. * \brief Set the palette used by a surface.
  116. *
  117. * \return 0, or -1 if the surface format doesn't use a palette.
  118. *
  119. * \note A single palette can be shared with many surfaces.
  120. */
  121. extern DECLSPEC int SDLCALL SDL_SetSurfacePalette(SDL_Surface * surface,
  122. SDL_Palette * palette);
  123. /**
  124. * \brief Sets up a surface for directly accessing the pixels.
  125. *
  126. * Between calls to SDL_LockSurface() / SDL_UnlockSurface(), you can write
  127. * to and read from \c surface->pixels, using the pixel format stored in
  128. * \c surface->format. Once you are done accessing the surface, you should
  129. * use SDL_UnlockSurface() to release it.
  130. *
  131. * Not all surfaces require locking. If SDL_MUSTLOCK(surface) evaluates
  132. * to 0, then you can read and write to the surface at any time, and the
  133. * pixel format of the surface will not change.
  134. *
  135. * No operating system or library calls should be made between lock/unlock
  136. * pairs, as critical system locks may be held during this time.
  137. *
  138. * SDL_LockSurface() returns 0, or -1 if the surface couldn't be locked.
  139. *
  140. * \sa SDL_UnlockSurface()
  141. */
  142. extern DECLSPEC int SDLCALL SDL_LockSurface(SDL_Surface * surface);
  143. /** \sa SDL_LockSurface() */
  144. extern DECLSPEC void SDLCALL SDL_UnlockSurface(SDL_Surface * surface);
  145. /**
  146. * Load a surface from a seekable SDL data stream (memory or file).
  147. *
  148. * If \c freesrc is non-zero, the stream will be closed after being read.
  149. *
  150. * The new surface should be freed with SDL_FreeSurface().
  151. *
  152. * \return the new surface, or NULL if there was an error.
  153. */
  154. extern DECLSPEC SDL_Surface *SDLCALL SDL_LoadBMP_RW(SDL_RWops * src,
  155. int freesrc);
  156. /**
  157. * Load a surface from a file.
  158. *
  159. * Convenience macro.
  160. */
  161. #define SDL_LoadBMP(file) SDL_LoadBMP_RW(SDL_RWFromFile(file, "rb"), 1)
  162. /**
  163. * Save a surface to a seekable SDL data stream (memory or file).
  164. *
  165. * Surfaces with a 24-bit, 32-bit and paletted 8-bit format get saved in the
  166. * BMP directly. Other RGB formats with 8-bit or higher get converted to a
  167. * 24-bit surface or, if they have an alpha mask or a colorkey, to a 32-bit
  168. * surface before they are saved. YUV and paletted 1-bit and 4-bit formats are
  169. * not supported.
  170. *
  171. * If \c freedst is non-zero, the stream will be closed after being written.
  172. *
  173. * \return 0 if successful or -1 if there was an error.
  174. */
  175. extern DECLSPEC int SDLCALL SDL_SaveBMP_RW
  176. (SDL_Surface * surface, SDL_RWops * dst, int freedst);
  177. /**
  178. * Save a surface to a file.
  179. *
  180. * Convenience macro.
  181. */
  182. #define SDL_SaveBMP(surface, file) \
  183. SDL_SaveBMP_RW(surface, SDL_RWFromFile(file, "wb"), 1)
  184. /**
  185. * \brief Sets the RLE acceleration hint for a surface.
  186. *
  187. * \return 0 on success, or -1 if the surface is not valid
  188. *
  189. * \note If RLE is enabled, colorkey and alpha blending blits are much faster,
  190. * but the surface must be locked before directly accessing the pixels.
  191. */
  192. extern DECLSPEC int SDLCALL SDL_SetSurfaceRLE(SDL_Surface * surface,
  193. int flag);
  194. /**
  195. * \brief Sets the color key (transparent pixel) in a blittable surface.
  196. *
  197. * \param surface The surface to update
  198. * \param flag Non-zero to enable colorkey and 0 to disable colorkey
  199. * \param key The transparent pixel in the native surface format
  200. *
  201. * \return 0 on success, or -1 if the surface is not valid
  202. *
  203. * You can pass SDL_RLEACCEL to enable RLE accelerated blits.
  204. */
  205. extern DECLSPEC int SDLCALL SDL_SetColorKey(SDL_Surface * surface,
  206. int flag, Uint32 key);
  207. /**
  208. * \brief Gets the color key (transparent pixel) in a blittable surface.
  209. *
  210. * \param surface The surface to update
  211. * \param key A pointer filled in with the transparent pixel in the native
  212. * surface format
  213. *
  214. * \return 0 on success, or -1 if the surface is not valid or colorkey is not
  215. * enabled.
  216. */
  217. extern DECLSPEC int SDLCALL SDL_GetColorKey(SDL_Surface * surface,
  218. Uint32 * key);
  219. /**
  220. * \brief Set an additional color value used in blit operations.
  221. *
  222. * \param surface The surface to update.
  223. * \param r The red color value multiplied into blit operations.
  224. * \param g The green color value multiplied into blit operations.
  225. * \param b The blue color value multiplied into blit operations.
  226. *
  227. * \return 0 on success, or -1 if the surface is not valid.
  228. *
  229. * \sa SDL_GetSurfaceColorMod()
  230. */
  231. extern DECLSPEC int SDLCALL SDL_SetSurfaceColorMod(SDL_Surface * surface,
  232. Uint8 r, Uint8 g, Uint8 b);
  233. /**
  234. * \brief Get the additional color value used in blit operations.
  235. *
  236. * \param surface The surface to query.
  237. * \param r A pointer filled in with the current red color value.
  238. * \param g A pointer filled in with the current green color value.
  239. * \param b A pointer filled in with the current blue color value.
  240. *
  241. * \return 0 on success, or -1 if the surface is not valid.
  242. *
  243. * \sa SDL_SetSurfaceColorMod()
  244. */
  245. extern DECLSPEC int SDLCALL SDL_GetSurfaceColorMod(SDL_Surface * surface,
  246. Uint8 * r, Uint8 * g,
  247. Uint8 * b);
  248. /**
  249. * \brief Set an additional alpha value used in blit operations.
  250. *
  251. * \param surface The surface to update.
  252. * \param alpha The alpha value multiplied into blit operations.
  253. *
  254. * \return 0 on success, or -1 if the surface is not valid.
  255. *
  256. * \sa SDL_GetSurfaceAlphaMod()
  257. */
  258. extern DECLSPEC int SDLCALL SDL_SetSurfaceAlphaMod(SDL_Surface * surface,
  259. Uint8 alpha);
  260. /**
  261. * \brief Get the additional alpha value used in blit operations.
  262. *
  263. * \param surface The surface to query.
  264. * \param alpha A pointer filled in with the current alpha value.
  265. *
  266. * \return 0 on success, or -1 if the surface is not valid.
  267. *
  268. * \sa SDL_SetSurfaceAlphaMod()
  269. */
  270. extern DECLSPEC int SDLCALL SDL_GetSurfaceAlphaMod(SDL_Surface * surface,
  271. Uint8 * alpha);
  272. /**
  273. * \brief Set the blend mode used for blit operations.
  274. *
  275. * \param surface The surface to update.
  276. * \param blendMode ::SDL_BlendMode to use for blit blending.
  277. *
  278. * \return 0 on success, or -1 if the parameters are not valid.
  279. *
  280. * \sa SDL_GetSurfaceBlendMode()
  281. */
  282. extern DECLSPEC int SDLCALL SDL_SetSurfaceBlendMode(SDL_Surface * surface,
  283. SDL_BlendMode blendMode);
  284. /**
  285. * \brief Get the blend mode used for blit operations.
  286. *
  287. * \param surface The surface to query.
  288. * \param blendMode A pointer filled in with the current blend mode.
  289. *
  290. * \return 0 on success, or -1 if the surface is not valid.
  291. *
  292. * \sa SDL_SetSurfaceBlendMode()
  293. */
  294. extern DECLSPEC int SDLCALL SDL_GetSurfaceBlendMode(SDL_Surface * surface,
  295. SDL_BlendMode *blendMode);
  296. /**
  297. * Sets the clipping rectangle for the destination surface in a blit.
  298. *
  299. * If the clip rectangle is NULL, clipping will be disabled.
  300. *
  301. * If the clip rectangle doesn't intersect the surface, the function will
  302. * return SDL_FALSE and blits will be completely clipped. Otherwise the
  303. * function returns SDL_TRUE and blits to the surface will be clipped to
  304. * the intersection of the surface area and the clipping rectangle.
  305. *
  306. * Note that blits are automatically clipped to the edges of the source
  307. * and destination surfaces.
  308. */
  309. extern DECLSPEC SDL_bool SDLCALL SDL_SetClipRect(SDL_Surface * surface,
  310. const SDL_Rect * rect);
  311. /**
  312. * Gets the clipping rectangle for the destination surface in a blit.
  313. *
  314. * \c rect must be a pointer to a valid rectangle which will be filled
  315. * with the correct values.
  316. */
  317. extern DECLSPEC void SDLCALL SDL_GetClipRect(SDL_Surface * surface,
  318. SDL_Rect * rect);
  319. /**
  320. * Creates a new surface of the specified format, and then copies and maps
  321. * the given surface to it so the blit of the converted surface will be as
  322. * fast as possible. If this function fails, it returns NULL.
  323. *
  324. * The \c flags parameter is passed to SDL_CreateRGBSurface() and has those
  325. * semantics. You can also pass ::SDL_RLEACCEL in the flags parameter and
  326. * SDL will try to RLE accelerate colorkey and alpha blits in the resulting
  327. * surface.
  328. */
  329. extern DECLSPEC SDL_Surface *SDLCALL SDL_ConvertSurface
  330. (SDL_Surface * src, const SDL_PixelFormat * fmt, Uint32 flags);
  331. extern DECLSPEC SDL_Surface *SDLCALL SDL_ConvertSurfaceFormat
  332. (SDL_Surface * src, Uint32 pixel_format, Uint32 flags);
  333. /**
  334. * \brief Copy a block of pixels of one format to another format
  335. *
  336. * \return 0 on success, or -1 if there was an error
  337. */
  338. extern DECLSPEC int SDLCALL SDL_ConvertPixels(int width, int height,
  339. Uint32 src_format,
  340. const void * src, int src_pitch,
  341. Uint32 dst_format,
  342. void * dst, int dst_pitch);
  343. /**
  344. * Performs a fast fill of the given rectangle with \c color.
  345. *
  346. * If \c rect is NULL, the whole surface will be filled with \c color.
  347. *
  348. * The color should be a pixel of the format used by the surface, and
  349. * can be generated by the SDL_MapRGB() function.
  350. *
  351. * \return 0 on success, or -1 on error.
  352. */
  353. extern DECLSPEC int SDLCALL SDL_FillRect
  354. (SDL_Surface * dst, const SDL_Rect * rect, Uint32 color);
  355. extern DECLSPEC int SDLCALL SDL_FillRects
  356. (SDL_Surface * dst, const SDL_Rect * rects, int count, Uint32 color);
  357. /**
  358. * Performs a fast blit from the source surface to the destination surface.
  359. *
  360. * This assumes that the source and destination rectangles are
  361. * the same size. If either \c srcrect or \c dstrect are NULL, the entire
  362. * surface (\c src or \c dst) is copied. The final blit rectangles are saved
  363. * in \c srcrect and \c dstrect after all clipping is performed.
  364. *
  365. * \return If the blit is successful, it returns 0, otherwise it returns -1.
  366. *
  367. * The blit function should not be called on a locked surface.
  368. *
  369. * The blit semantics for surfaces with and without blending and colorkey
  370. * are defined as follows:
  371. * \verbatim
  372. RGBA->RGB:
  373. Source surface blend mode set to SDL_BLENDMODE_BLEND:
  374. alpha-blend (using the source alpha-channel and per-surface alpha)
  375. SDL_SRCCOLORKEY ignored.
  376. Source surface blend mode set to SDL_BLENDMODE_NONE:
  377. copy RGB.
  378. if SDL_SRCCOLORKEY set, only copy the pixels matching the
  379. RGB values of the source color key, ignoring alpha in the
  380. comparison.
  381. RGB->RGBA:
  382. Source surface blend mode set to SDL_BLENDMODE_BLEND:
  383. alpha-blend (using the source per-surface alpha)
  384. Source surface blend mode set to SDL_BLENDMODE_NONE:
  385. copy RGB, set destination alpha to source per-surface alpha value.
  386. both:
  387. if SDL_SRCCOLORKEY set, only copy the pixels matching the
  388. source color key.
  389. RGBA->RGBA:
  390. Source surface blend mode set to SDL_BLENDMODE_BLEND:
  391. alpha-blend (using the source alpha-channel and per-surface alpha)
  392. SDL_SRCCOLORKEY ignored.
  393. Source surface blend mode set to SDL_BLENDMODE_NONE:
  394. copy all of RGBA to the destination.
  395. if SDL_SRCCOLORKEY set, only copy the pixels matching the
  396. RGB values of the source color key, ignoring alpha in the
  397. comparison.
  398. RGB->RGB:
  399. Source surface blend mode set to SDL_BLENDMODE_BLEND:
  400. alpha-blend (using the source per-surface alpha)
  401. Source surface blend mode set to SDL_BLENDMODE_NONE:
  402. copy RGB.
  403. both:
  404. if SDL_SRCCOLORKEY set, only copy the pixels matching the
  405. source color key.
  406. \endverbatim
  407. *
  408. * You should call SDL_BlitSurface() unless you know exactly how SDL
  409. * blitting works internally and how to use the other blit functions.
  410. */
  411. #define SDL_BlitSurface SDL_UpperBlit
  412. /**
  413. * This is the public blit function, SDL_BlitSurface(), and it performs
  414. * rectangle validation and clipping before passing it to SDL_LowerBlit()
  415. */
  416. extern DECLSPEC int SDLCALL SDL_UpperBlit
  417. (SDL_Surface * src, const SDL_Rect * srcrect,
  418. SDL_Surface * dst, SDL_Rect * dstrect);
  419. /**
  420. * This is a semi-private blit function and it performs low-level surface
  421. * blitting only.
  422. */
  423. extern DECLSPEC int SDLCALL SDL_LowerBlit
  424. (SDL_Surface * src, SDL_Rect * srcrect,
  425. SDL_Surface * dst, SDL_Rect * dstrect);
  426. /**
  427. * \brief Perform a fast, low quality, stretch blit between two surfaces of the
  428. * same pixel format.
  429. *
  430. * \note This function uses a static buffer, and is not thread-safe.
  431. */
  432. extern DECLSPEC int SDLCALL SDL_SoftStretch(SDL_Surface * src,
  433. const SDL_Rect * srcrect,
  434. SDL_Surface * dst,
  435. const SDL_Rect * dstrect);
  436. #define SDL_BlitScaled SDL_UpperBlitScaled
  437. /**
  438. * This is the public scaled blit function, SDL_BlitScaled(), and it performs
  439. * rectangle validation and clipping before passing it to SDL_LowerBlitScaled()
  440. */
  441. extern DECLSPEC int SDLCALL SDL_UpperBlitScaled
  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. * scaled blitting only.
  447. */
  448. extern DECLSPEC int SDLCALL SDL_LowerBlitScaled
  449. (SDL_Surface * src, SDL_Rect * srcrect,
  450. SDL_Surface * dst, SDL_Rect * dstrect);
  451. /* Ends C function definitions when using C++ */
  452. #ifdef __cplusplus
  453. }
  454. #endif
  455. #include "close_code.h"
  456. #endif /* _SDL_surface_h */
  457. /* vi: set ts=4 sw=4 expandtab: */