SDL_gfxBlitFunc.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. SDL_gfxBlitFunc.h: custom blitters
  3. Copyright (C) 2001-2012 Andreas Schiffler
  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
  17. distribution.
  18. Andreas Schiffler -- aschiffler at ferzkopp dot net
  19. */
  20. #ifndef _SDL_gfxBlitFunc_h
  21. #define _SDL_gfxBlitFunc_h
  22. /* Set up for C function definitions, even when using C++ */
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include "SDL.h"
  29. #include "SDL_video.h"
  30. extern const unsigned int GFX_ALPHA_ADJUST_ARRAY[256];
  31. /* ---- Function Prototypes */
  32. #ifdef _MSC_VER
  33. # if defined(DLL_EXPORT) && !defined(LIBSDL_GFX_DLL_IMPORT)
  34. # define SDL_GFXBLITFUNC_SCOPE __declspec(dllexport)
  35. # else
  36. # ifdef LIBSDL_GFX_DLL_IMPORT
  37. # define SDL_GFXBLITFUNC_SCOPE __declspec(dllimport)
  38. # endif
  39. # endif
  40. #endif
  41. #ifndef SDL_GFXBLITFUNC_SCOPE
  42. # define SDL_GFXBLITFUNC_SCOPE extern
  43. #endif
  44. SDL_GFXBLITFUNC_SCOPE int SDL_gfxBlitRGBA(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect);
  45. SDL_GFXBLITFUNC_SCOPE int SDL_gfxSetAlpha(SDL_Surface *src, Uint8 a);
  46. SDL_GFXBLITFUNC_SCOPE int SDL_gfxMultiplyAlpha(SDL_Surface *src, Uint8 a);
  47. /* -------- Macros */
  48. /* Define SDL macros locally as a substitute for an #include "SDL_blit.h", */
  49. /* which doesn't work since the include file doesn't get installed. */
  50. /*!
  51. \brief The structure passed to the low level blit functions.
  52. */
  53. typedef struct {
  54. Uint8 *s_pixels;
  55. int s_width;
  56. int s_height;
  57. int s_skip;
  58. Uint8 *d_pixels;
  59. int d_width;
  60. int d_height;
  61. int d_skip;
  62. void *aux_data;
  63. SDL_PixelFormat *src;
  64. Uint8 *table;
  65. SDL_PixelFormat *dst;
  66. } SDL_gfxBlitInfo;
  67. /*!
  68. \brief Unwrap RGBA values from a pixel using mask, shift and loss for surface.
  69. */
  70. #define GFX_RGBA_FROM_PIXEL(pixel, fmt, r, g, b, a) \
  71. { \
  72. r = ((pixel&fmt->Rmask)>>fmt->Rshift)<<fmt->Rloss; \
  73. g = ((pixel&fmt->Gmask)>>fmt->Gshift)<<fmt->Gloss; \
  74. b = ((pixel&fmt->Bmask)>>fmt->Bshift)<<fmt->Bloss; \
  75. a = ((pixel&fmt->Amask)>>fmt->Ashift)<<fmt->Aloss; \
  76. }
  77. /*!
  78. \brief Disassemble buffer pointer into a pixel and separate RGBA values.
  79. */
  80. #define GFX_DISASSEMBLE_RGBA(buf, bpp, fmt, pixel, r, g, b, a) \
  81. do { \
  82. pixel = *((Uint32 *)(buf)); \
  83. GFX_RGBA_FROM_PIXEL(pixel, fmt, r, g, b, a); \
  84. pixel &= ~fmt->Amask; \
  85. } while(0)
  86. /*!
  87. \brief Wrap a pixel from RGBA values using mask, shift and loss for surface.
  88. */
  89. #define GFX_PIXEL_FROM_RGBA(pixel, fmt, r, g, b, a) \
  90. { \
  91. pixel = ((r>>fmt->Rloss)<<fmt->Rshift)| \
  92. ((g>>fmt->Gloss)<<fmt->Gshift)| \
  93. ((b>>fmt->Bloss)<<fmt->Bshift)| \
  94. ((a<<fmt->Aloss)<<fmt->Ashift); \
  95. }
  96. /*!
  97. \brief Assemble pixel into buffer pointer from separate RGBA values.
  98. */
  99. #define GFX_ASSEMBLE_RGBA(buf, bpp, fmt, r, g, b, a) \
  100. { \
  101. Uint32 pixel; \
  102. \
  103. GFX_PIXEL_FROM_RGBA(pixel, fmt, r, g, b, a); \
  104. *((Uint32 *)(buf)) = pixel; \
  105. }
  106. /*!
  107. \brief Blend the RGB values of two pixels based on a source alpha value.
  108. */
  109. #define GFX_ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB) \
  110. do { \
  111. dR = (((sR-dR)*(A))/255)+dR; \
  112. dG = (((sG-dG)*(A))/255)+dG; \
  113. dB = (((sB-dB)*(A))/255)+dB; \
  114. } while(0)
  115. /*!
  116. \brief 4-times unrolled DUFFs loop.
  117. This is a very useful loop for optimizing blitters.
  118. */
  119. #define GFX_DUFFS_LOOP4(pixel_copy_increment, width) \
  120. { int n = (width+3)/4; \
  121. switch (width & 3) { \
  122. case 0: do { pixel_copy_increment; \
  123. case 3: pixel_copy_increment; \
  124. case 2: pixel_copy_increment; \
  125. case 1: pixel_copy_increment; \
  126. } while ( --n > 0 ); \
  127. } \
  128. }
  129. /* Ends C function definitions when using C++ */
  130. #ifdef __cplusplus
  131. }
  132. #endif
  133. #endif /* _SDL_gfxBlitFunc_h */