/project/jni/sdl-1.3/src/render/software/SDL_drawline.c

https://github.com/aichunyu/FFPlayer · C · 214 lines · 165 code · 22 blank · 27 comment · 57 complexity · 9a82bfb4dde8867bf4ef37fd7498896a MD5 · raw file

  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2012 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. #include "SDL_config.h"
  19. #if !SDL_RENDER_DISABLED
  20. #include "SDL_draw.h"
  21. #include "SDL_drawline.h"
  22. #include "SDL_drawpoint.h"
  23. static void
  24. SDL_DrawLine1(SDL_Surface * dst, int x1, int y1, int x2, int y2, Uint32 color,
  25. SDL_bool draw_end)
  26. {
  27. if (y1 == y2) {
  28. //HLINE(Uint8, DRAW_FASTSETPIXEL1, draw_end);
  29. int length;
  30. int pitch = (dst->pitch / dst->format->BytesPerPixel);
  31. Uint8 *pixel;
  32. if (x1 <= x2) {
  33. pixel = (Uint8 *)dst->pixels + y1 * pitch + x1;
  34. length = draw_end ? (x2-x1+1) : (x2-x1);
  35. } else {
  36. pixel = (Uint8 *)dst->pixels + y1 * pitch + x2;
  37. if (!draw_end) {
  38. ++pixel;
  39. }
  40. length = draw_end ? (x1-x2+1) : (x1-x2);
  41. }
  42. SDL_memset(pixel, color, length);
  43. } else if (x1 == x2) {
  44. VLINE(Uint8, DRAW_FASTSETPIXEL1, draw_end);
  45. } else if (ABS(x1 - x2) == ABS(y1 - y2)) {
  46. DLINE(Uint8, DRAW_FASTSETPIXEL1, draw_end);
  47. } else {
  48. BLINE(x1, y1, x2, y2, DRAW_FASTSETPIXELXY1, draw_end);
  49. }
  50. }
  51. static void
  52. SDL_DrawLine2(SDL_Surface * dst, int x1, int y1, int x2, int y2, Uint32 color,
  53. SDL_bool draw_end)
  54. {
  55. if (y1 == y2) {
  56. HLINE(Uint16, DRAW_FASTSETPIXEL2, draw_end);
  57. } else if (x1 == x2) {
  58. VLINE(Uint16, DRAW_FASTSETPIXEL2, draw_end);
  59. } else if (ABS(x1 - x2) == ABS(y1 - y2)) {
  60. DLINE(Uint16, DRAW_FASTSETPIXEL2, draw_end);
  61. } else {
  62. Uint8 _r, _g, _b, _a;
  63. const SDL_PixelFormat * fmt = dst->format;
  64. SDL_GetRGBA(color, fmt, &_r, &_g, &_b, &_a);
  65. if (fmt->Rmask == 0x7C00) {
  66. AALINE(x1, y1, x2, y2,
  67. DRAW_FASTSETPIXELXY2, DRAW_SETPIXELXY_BLEND_RGB555,
  68. draw_end);
  69. } else if (fmt->Rmask == 0xF800) {
  70. AALINE(x1, y1, x2, y2,
  71. DRAW_FASTSETPIXELXY2, DRAW_SETPIXELXY_BLEND_RGB565,
  72. draw_end);
  73. } else {
  74. AALINE(x1, y1, x2, y2,
  75. DRAW_FASTSETPIXELXY2, DRAW_SETPIXELXY2_BLEND_RGB,
  76. draw_end);
  77. }
  78. }
  79. }
  80. static void
  81. SDL_DrawLine4(SDL_Surface * dst, int x1, int y1, int x2, int y2, Uint32 color,
  82. SDL_bool draw_end)
  83. {
  84. if (y1 == y2) {
  85. HLINE(Uint32, DRAW_FASTSETPIXEL4, draw_end);
  86. } else if (x1 == x2) {
  87. VLINE(Uint32, DRAW_FASTSETPIXEL4, draw_end);
  88. } else if (ABS(x1 - x2) == ABS(y1 - y2)) {
  89. DLINE(Uint32, DRAW_FASTSETPIXEL4, draw_end);
  90. } else {
  91. Uint8 _r, _g, _b, _a;
  92. const SDL_PixelFormat * fmt = dst->format;
  93. SDL_GetRGBA(color, fmt, &_r, &_g, &_b, &_a);
  94. if (fmt->Rmask == 0x00FF0000) {
  95. if (!fmt->Amask) {
  96. AALINE(x1, y1, x2, y2,
  97. DRAW_FASTSETPIXELXY4, DRAW_SETPIXELXY_BLEND_RGB888,
  98. draw_end);
  99. } else {
  100. AALINE(x1, y1, x2, y2,
  101. DRAW_FASTSETPIXELXY4, DRAW_SETPIXELXY_BLEND_ARGB8888,
  102. draw_end);
  103. }
  104. } else {
  105. AALINE(x1, y1, x2, y2,
  106. DRAW_FASTSETPIXELXY4, DRAW_SETPIXELXY4_BLEND_RGB,
  107. draw_end);
  108. }
  109. }
  110. }
  111. typedef void (*DrawLineFunc) (SDL_Surface * dst,
  112. int x1, int y1, int x2, int y2,
  113. Uint32 color, SDL_bool draw_end);
  114. static DrawLineFunc
  115. SDL_CalculateDrawLineFunc(const SDL_PixelFormat * fmt)
  116. {
  117. switch (fmt->BytesPerPixel) {
  118. case 1:
  119. if (fmt->BitsPerPixel < 8) {
  120. break;
  121. }
  122. return SDL_DrawLine1;
  123. case 2:
  124. return SDL_DrawLine2;
  125. case 4:
  126. return SDL_DrawLine4;
  127. }
  128. return NULL;
  129. }
  130. int
  131. SDL_DrawLine(SDL_Surface * dst, int x1, int y1, int x2, int y2, Uint32 color)
  132. {
  133. DrawLineFunc func;
  134. if (!dst) {
  135. SDL_SetError("SDL_DrawLine(): Passed NULL destination surface");
  136. return -1;
  137. }
  138. func = SDL_CalculateDrawLineFunc(dst->format);
  139. if (!func) {
  140. SDL_SetError("SDL_DrawLine(): Unsupported surface format");
  141. return -1;
  142. }
  143. /* Perform clipping */
  144. /* FIXME: We don't actually want to clip, as it may change line slope */
  145. if (!SDL_IntersectRectAndLine(&dst->clip_rect, &x1, &y1, &x2, &y2)) {
  146. return 0;
  147. }
  148. func(dst, x1, y1, x2, y2, color, SDL_TRUE);
  149. return 0;
  150. }
  151. int
  152. SDL_DrawLines(SDL_Surface * dst, const SDL_Point * points, int count,
  153. Uint32 color)
  154. {
  155. int i;
  156. int x1, y1;
  157. int x2, y2;
  158. SDL_bool draw_end;
  159. DrawLineFunc func;
  160. if (!dst) {
  161. SDL_SetError("SDL_DrawLines(): Passed NULL destination surface");
  162. return -1;
  163. }
  164. func = SDL_CalculateDrawLineFunc(dst->format);
  165. if (!func) {
  166. SDL_SetError("SDL_DrawLines(): Unsupported surface format");
  167. return -1;
  168. }
  169. for (i = 1; i < count; ++i) {
  170. x1 = points[i-1].x;
  171. y1 = points[i-1].y;
  172. x2 = points[i].x;
  173. y2 = points[i].y;
  174. /* Perform clipping */
  175. /* FIXME: We don't actually want to clip, as it may change line slope */
  176. if (!SDL_IntersectRectAndLine(&dst->clip_rect, &x1, &y1, &x2, &y2)) {
  177. continue;
  178. }
  179. /* Draw the end if it was clipped */
  180. draw_end = (x2 != points[i].x || y2 != points[i].y);
  181. func(dst, x1, y1, x2, y2, color, draw_end);
  182. }
  183. if (points[0].x != points[count-1].x || points[0].y != points[count-1].y) {
  184. SDL_DrawPoint(dst, points[count-1].x, points[count-1].y, color);
  185. }
  186. return 0;
  187. }
  188. #endif /* !SDL_RENDER_DISABLED */
  189. /* vi: set ts=4 sw=4 expandtab: */