SDL_surface.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 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. * If \c freedst is non-zero, the stream will be closed after being written.
  166. *
  167. * \return 0 if successful or -1 if there was an error.
  168. */
  169. extern DECLSPEC int SDLCALL SDL_SaveBMP_RW
  170. (SDL_Surface * surface, SDL_RWops * dst, int freedst);
  171. /**
  172. * Save a surface to a file.
  173. *
  174. * Convenience macro.
  175. */
  176. #define SDL_SaveBMP(surface, file) \
  177. SDL_SaveBMP_RW(surface, SDL_RWFromFile(file, "wb"), 1)
  178. /**
  179. * \brief Sets the RLE acceleration hint for a surface.
  180. *
  181. * \return 0 on success, or -1 if the surface is not valid
  182. *
  183. * \note If RLE is enabled, colorkey and alpha blending blits are much faster,
  184. * but the surface must be locked before directly accessing the pixels.
  185. */
  186. extern DECLSPEC int SDLCALL SDL_SetSurfaceRLE(SDL_Surface * surface,
  187. int flag);
  188. /**
  189. * \brief Sets the color key (transparent pixel) in a blittable surface.
  190. *
  191. * \param surface The surface to update
  192. * \param flag Non-zero to enable colorkey and 0 to disable colorkey
  193. * \param key The transparent pixel in the native surface format
  194. *
  195. * \return 0 on success, or -1 if the surface is not valid
  196. *
  197. * You can pass SDL_RLEACCEL to enable RLE accelerated blits.
  198. */
  199. extern DECLSPEC int SDLCALL SDL_SetColorKey(SDL_Surface * surface,
  200. int flag, Uint32 key);
  201. /**
  202. * \brief Gets the color key (transparent pixel) in a blittable surface.
  203. *
  204. * \param surface The surface to update
  205. * \param key A pointer filled in with the transparent pixel in the native
  206. * surface format
  207. *
  208. * \return 0 on success, or -1 if the surface is not valid or colorkey is not
  209. * enabled.
  210. */
  211. extern DECLSPEC int SDLCALL SDL_GetColorKey(SDL_Surface * surface,
  212. Uint32 * key);
  213. /**
  214. * \brief Set an additional color value used in blit operations.
  215. *
  216. * \param surface The surface to update.
  217. * \param r The red color value multiplied into blit operations.
  218. * \param g The green color value multiplied into blit operations.
  219. * \param b The blue color value multiplied into blit operations.
  220. *
  221. * \return 0 on success, or -1 if the surface is not valid.
  222. *
  223. * \sa SDL_GetSurfaceColorMod()
  224. */
  225. extern DECLSPEC int SDLCALL SDL_SetSurfaceColorMod(SDL_Surface * surface,
  226. Uint8 r, Uint8 g, Uint8 b);
  227. /**
  228. * \brief Get the additional color value used in blit operations.
  229. *
  230. * \param surface The surface to query.
  231. * \param r A pointer filled in with the current red color value.
  232. * \param g A pointer filled in with the current green color value.
  233. * \param b A pointer filled in with the current blue color value.
  234. *
  235. * \return 0 on success, or -1 if the surface is not valid.
  236. *
  237. * \sa SDL_SetSurfaceColorMod()
  238. */
  239. extern DECLSPEC int SDLCALL SDL_GetSurfaceColorMod(SDL_Surface * surface,
  240. Uint8 * r, Uint8 * g,
  241. Uint8 * b);
  242. /**
  243. * \brief Set an additional alpha value used in blit operations.
  244. *
  245. * \param surface The surface to update.
  246. * \param alpha The alpha value multiplied into blit operations.
  247. *
  248. * \return 0 on success, or -1 if the surface is not valid.
  249. *
  250. * \sa SDL_GetSurfaceAlphaMod()
  251. */
  252. extern DECLSPEC int SDLCALL SDL_SetSurfaceAlphaMod(SDL_Surface * surface,
  253. Uint8 alpha);
  254. /**
  255. * \brief Get the additional alpha value used in blit operations.
  256. *
  257. * \param surface The surface to query.
  258. * \param alpha A pointer filled in with the current alpha value.
  259. *
  260. * \return 0 on success, or -1 if the surface is not valid.
  261. *
  262. * \sa SDL_SetSurfaceAlphaMod()
  263. */
  264. extern DECLSPEC int SDLCALL SDL_GetSurfaceAlphaMod(SDL_Surface * surface,
  265. Uint8 * alpha);
  266. /**
  267. * \brief Set the blend mode used for blit operations.
  268. *
  269. * \param surface The surface to update.
  270. * \param blendMode ::SDL_BlendMode to use for blit blending.
  271. *
  272. * \return 0 on success, or -1 if the parameters are not valid.
  273. *
  274. * \sa SDL_GetSurfaceBlendMode()
  275. */
  276. extern DECLSPEC int SDLCALL SDL_SetSurfaceBlendMode(SDL_Surface * surface,
  277. SDL_BlendMode blendMode);
  278. /**
  279. * \brief Get the blend mode used for blit operations.
  280. *
  281. * \param surface The surface to query.
  282. * \param blendMode A pointer filled in with the current blend mode.
  283. *
  284. * \return 0 on success, or -1 if the surface is not valid.
  285. *
  286. * \sa SDL_SetSurfaceBlendMode()
  287. */
  288. extern DECLSPEC int SDLCALL SDL_GetSurfaceBlendMode(SDL_Surface * surface,
  289. SDL_BlendMode *blendMode);
  290. /**
  291. * Sets the clipping rectangle for the destination surface in a blit.
  292. *
  293. * If the clip rectangle is NULL, clipping will be disabled.
  294. *
  295. * If the clip rectangle doesn't intersect the surface, the function will
  296. * return SDL_FALSE and blits will be completely clipped. Otherwise the
  297. * function returns SDL_TRUE and blits to the surface will be clipped to
  298. * the intersection of the surface area and the clipping rectangle.
  299. *
  300. * Note that blits are automatically clipped to the edges of the source
  301. * and destination surfaces.
  302. */
  303. extern DECLSPEC SDL_bool SDLCALL SDL_SetClipRect(SDL_Surface * surface,
  304. const SDL_Rect * rect);
  305. /**
  306. * Gets the clipping rectangle for the destination surface in a blit.
  307. *
  308. * \c rect must be a pointer to a valid rectangle which will be filled
  309. * with the correct values.
  310. */
  311. extern DECLSPEC void SDLCALL SDL_GetClipRect(SDL_Surface * surface,
  312. SDL_Rect * rect);
  313. /**
  314. * Creates a new surface of the specified format, and then copies and maps
  315. * the given surface to it so the blit of the converted surface will be as
  316. * fast as possible. If this function fails, it returns NULL.
  317. *
  318. * The \c flags parameter is passed to SDL_CreateRGBSurface() and has those
  319. * semantics. You can also pass ::SDL_RLEACCEL in the flags parameter and
  320. * SDL will try to RLE accelerate colorkey and alpha blits in the resulting
  321. * surface.
  322. */
  323. extern DECLSPEC SDL_Surface *SDLCALL SDL_ConvertSurface
  324. (SDL_Surface * src, const SDL_PixelFormat * fmt, Uint32 flags);
  325. extern DECLSPEC SDL_Surface *SDLCALL SDL_ConvertSurfaceFormat
  326. (SDL_Surface * src, Uint32 pixel_format, Uint32 flags);
  327. /**
  328. * \brief Copy a block of pixels of one format to another format
  329. *
  330. * \return 0 on success, or -1 if there was an error
  331. */
  332. extern DECLSPEC int SDLCALL SDL_ConvertPixels(int width, int height,
  333. Uint32 src_format,
  334. const void * src, int src_pitch,
  335. Uint32 dst_format,
  336. void * dst, int dst_pitch);
  337. /**
  338. * Performs a fast fill of the given rectangle with \c color.
  339. *
  340. * If \c rect is NULL, the whole surface will be filled with \c color.
  341. *
  342. * The color should be a pixel of the format used by the surface, and
  343. * can be generated by the SDL_MapRGB() function.
  344. *
  345. * \return 0 on success, or -1 on error.
  346. */
  347. extern DECLSPEC int SDLCALL SDL_FillRect
  348. (SDL_Surface * dst, const SDL_Rect * rect, Uint32 color);
  349. extern DECLSPEC int SDLCALL SDL_FillRects
  350. (SDL_Surface * dst, const SDL_Rect * rects, int count, Uint32 color);
  351. /**
  352. * Performs a fast blit from the source surface to the destination surface.
  353. *
  354. * This assumes that the source and destination rectangles are
  355. * the same size. If either \c srcrect or \c dstrect are NULL, the entire
  356. * surface (\c src or \c dst) is copied. The final blit rectangles are saved
  357. * in \c srcrect and \c dstrect after all clipping is performed.
  358. *
  359. * \return If the blit is successful, it returns 0, otherwise it returns -1.
  360. *
  361. * The blit function should not be called on a locked surface.
  362. *
  363. * The blit semantics for surfaces with and without blending and colorkey
  364. * are defined as follows:
  365. * \verbatim
  366. RGBA->RGB:
  367. Source surface blend mode set to SDL_BLENDMODE_BLEND:
  368. alpha-blend (using the source alpha-channel and per-surface alpha)
  369. SDL_SRCCOLORKEY ignored.
  370. Source surface blend mode set to SDL_BLENDMODE_NONE:
  371. copy RGB.
  372. if SDL_SRCCOLORKEY set, only copy the pixels matching the
  373. RGB values of the source color key, ignoring alpha in the
  374. comparison.
  375. RGB->RGBA:
  376. Source surface blend mode set to SDL_BLENDMODE_BLEND:
  377. alpha-blend (using the source per-surface alpha)
  378. Source surface blend mode set to SDL_BLENDMODE_NONE:
  379. copy RGB, set destination alpha to source per-surface alpha value.
  380. both:
  381. if SDL_SRCCOLORKEY set, only copy the pixels matching the
  382. source color key.
  383. RGBA->RGBA:
  384. Source surface blend mode set to SDL_BLENDMODE_BLEND:
  385. alpha-blend (using the source alpha-channel and per-surface alpha)
  386. SDL_SRCCOLORKEY ignored.
  387. Source surface blend mode set to SDL_BLENDMODE_NONE:
  388. copy all of RGBA to the destination.
  389. if SDL_SRCCOLORKEY set, only copy the pixels matching the
  390. RGB values of the source color key, ignoring alpha in the
  391. comparison.
  392. RGB->RGB:
  393. Source surface blend mode set to SDL_BLENDMODE_BLEND:
  394. alpha-blend (using the source per-surface alpha)
  395. Source surface blend mode set to SDL_BLENDMODE_NONE:
  396. copy RGB.
  397. both:
  398. if SDL_SRCCOLORKEY set, only copy the pixels matching the
  399. source color key.
  400. \endverbatim
  401. *
  402. * You should call SDL_BlitSurface() unless you know exactly how SDL
  403. * blitting works internally and how to use the other blit functions.
  404. */
  405. #define SDL_BlitSurface SDL_UpperBlit
  406. /**
  407. * This is the public blit function, SDL_BlitSurface(), and it performs
  408. * rectangle validation and clipping before passing it to SDL_LowerBlit()
  409. */
  410. extern DECLSPEC int SDLCALL SDL_UpperBlit
  411. (SDL_Surface * src, const SDL_Rect * srcrect,
  412. SDL_Surface * dst, SDL_Rect * dstrect);
  413. /**
  414. * This is a semi-private blit function and it performs low-level surface
  415. * blitting only.
  416. */
  417. extern DECLSPEC int SDLCALL SDL_LowerBlit
  418. (SDL_Surface * src, SDL_Rect * srcrect,
  419. SDL_Surface * dst, SDL_Rect * dstrect);
  420. /**
  421. * \brief Perform a fast, low quality, stretch blit between two surfaces of the
  422. * same pixel format.
  423. *
  424. * \note This function uses a static buffer, and is not thread-safe.
  425. */
  426. extern DECLSPEC int SDLCALL SDL_SoftStretch(SDL_Surface * src,
  427. const SDL_Rect * srcrect,
  428. SDL_Surface * dst,
  429. const SDL_Rect * dstrect);
  430. #define SDL_BlitScaled SDL_UpperBlitScaled
  431. /**
  432. * This is the public scaled blit function, SDL_BlitScaled(), and it performs
  433. * rectangle validation and clipping before passing it to SDL_LowerBlitScaled()
  434. */
  435. extern DECLSPEC int SDLCALL SDL_UpperBlitScaled
  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. * scaled blitting only.
  441. */
  442. extern DECLSPEC int SDLCALL SDL_LowerBlitScaled
  443. (SDL_Surface * src, SDL_Rect * srcrect,
  444. SDL_Surface * dst, SDL_Rect * dstrect);
  445. /* Ends C function definitions when using C++ */
  446. #ifdef __cplusplus
  447. }
  448. #endif
  449. #include "close_code.h"
  450. #endif /* _SDL_surface_h */
  451. /* vi: set ts=4 sw=4 expandtab: */