You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3810 lines
98 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. SDL2_gfxPrimitives.c: graphics primitives for SDL2 renderers
  3. Copyright (C) 2012-2014 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. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <math.h>
  23. #include <string.h>
  24. #include "SDL2_gfxPrimitives.h"
  25. #include "SDL2_rotozoom.h"
  26. #include "SDL2_gfxPrimitives_font.h"
  27. /* ---- Structures */
  28. /*!
  29. \brief The structure passed to the internal Bresenham iterator.
  30. */
  31. typedef struct {
  32. Sint16 x, y;
  33. int dx, dy, s1, s2, swapdir, error;
  34. Uint32 count;
  35. } SDL2_gfxBresenhamIterator;
  36. /*!
  37. \brief The structure passed to the internal Murphy iterator.
  38. */
  39. typedef struct {
  40. SDL_Renderer *renderer;
  41. int u, v; /* delta x , delta y */
  42. int ku, kt, kv, kd; /* loop constants */
  43. int oct2;
  44. int quad4;
  45. Sint16 last1x, last1y, last2x, last2y, first1x, first1y, first2x, first2y, tempx, tempy;
  46. } SDL2_gfxMurphyIterator;
  47. /* ---- Pixel */
  48. /*!
  49. \brief Draw pixel in currently set color.
  50. \param renderer The renderer to draw on.
  51. \param x X (horizontal) coordinate of the pixel.
  52. \param y Y (vertical) coordinate of the pixel.
  53. \returns Returns 0 on success, -1 on failure.
  54. */
  55. int pixel(SDL_Renderer *renderer, Sint16 x, Sint16 y)
  56. {
  57. return SDL_RenderDrawPoint(renderer, x, y);
  58. }
  59. /*!
  60. \brief Draw pixel with blending enabled if a<255.
  61. \param renderer The renderer to draw on.
  62. \param x X (horizontal) coordinate of the pixel.
  63. \param y Y (vertical) coordinate of the pixel.
  64. \param color The color value of the pixel to draw (0xRRGGBBAA).
  65. \returns Returns 0 on success, -1 on failure.
  66. */
  67. int pixelColor(SDL_Renderer * renderer, Sint16 x, Sint16 y, Uint32 color)
  68. {
  69. Uint8 *c = (Uint8 *)&color;
  70. return pixelRGBA(renderer, x, y, c[0], c[1], c[2], c[3]);
  71. }
  72. /*!
  73. \brief Draw pixel with blending enabled if a<255.
  74. \param renderer The renderer to draw on.
  75. \param x X (horizontal) coordinate of the pixel.
  76. \param y Y (vertical) coordinate of the pixel.
  77. \param r The red color value of the pixel to draw.
  78. \param g The green color value of the pixel to draw.
  79. \param b The blue color value of the pixel to draw.
  80. \param a The alpha value of the pixel to draw.
  81. \returns Returns 0 on success, -1 on failure.
  82. */
  83. int pixelRGBA(SDL_Renderer * renderer, Sint16 x, Sint16 y, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  84. {
  85. int result = 0;
  86. result |= SDL_SetRenderDrawBlendMode(renderer, (a == 255) ? SDL_BLENDMODE_NONE : SDL_BLENDMODE_BLEND);
  87. result |= SDL_SetRenderDrawColor(renderer, r, g, b, a);
  88. result |= SDL_RenderDrawPoint(renderer, x, y);
  89. return result;
  90. }
  91. /*!
  92. \brief Draw pixel with blending enabled and using alpha weight on color.
  93. \param renderer The renderer to draw on.
  94. \param x The horizontal coordinate of the pixel.
  95. \param y The vertical position of the pixel.
  96. \param r The red color value of the pixel to draw.
  97. \param g The green color value of the pixel to draw.
  98. \param b The blue color value of the pixel to draw.
  99. \param a The alpha value of the pixel to draw.
  100. \param weight The weight multiplied into the alpha value of the pixel.
  101. \returns Returns 0 on success, -1 on failure.
  102. */
  103. int pixelRGBAWeight(SDL_Renderer * renderer, Sint16 x, Sint16 y, Uint8 r, Uint8 g, Uint8 b, Uint8 a, Uint32 weight)
  104. {
  105. /*
  106. * Modify Alpha by weight
  107. */
  108. Uint32 ax = a;
  109. ax = ((ax * weight) >> 8);
  110. if (ax > 255) {
  111. a = 255;
  112. } else {
  113. a = (Uint8)(ax & 0x000000ff);
  114. }
  115. return pixelRGBA(renderer, x, y, r, g, b, a);
  116. }
  117. /* ---- Hline */
  118. /*!
  119. \brief Draw horizontal line in currently set color
  120. \param renderer The renderer to draw on.
  121. \param x1 X coordinate of the first point (i.e. left) of the line.
  122. \param x2 X coordinate of the second point (i.e. right) of the line.
  123. \param y Y coordinate of the points of the line.
  124. \returns Returns 0 on success, -1 on failure.
  125. */
  126. int hline(SDL_Renderer * renderer, Sint16 x1, Sint16 x2, Sint16 y)
  127. {
  128. return SDL_RenderDrawLine(renderer, x1, y, x2, y);;
  129. }
  130. /*!
  131. \brief Draw horizontal line with blending.
  132. \param renderer The renderer to draw on.
  133. \param x1 X coordinate of the first point (i.e. left) of the line.
  134. \param x2 X coordinate of the second point (i.e. right) of the line.
  135. \param y Y coordinate of the points of the line.
  136. \param color The color value of the line to draw (0xRRGGBBAA).
  137. \returns Returns 0 on success, -1 on failure.
  138. */
  139. int hlineColor(SDL_Renderer * renderer, Sint16 x1, Sint16 x2, Sint16 y, Uint32 color)
  140. {
  141. Uint8 *c = (Uint8 *)&color;
  142. return hlineRGBA(renderer, x1, x2, y, c[0], c[1], c[2], c[3]);
  143. }
  144. /*!
  145. \brief Draw horizontal line with blending.
  146. \param renderer The renderer to draw on.
  147. \param x1 X coordinate of the first point (i.e. left) of the line.
  148. \param x2 X coordinate of the second point (i.e. right) of the line.
  149. \param y Y coordinate of the points of the line.
  150. \param r The red value of the line to draw.
  151. \param g The green value of the line to draw.
  152. \param b The blue value of the line to draw.
  153. \param a The alpha value of the line to draw.
  154. \returns Returns 0 on success, -1 on failure.
  155. */
  156. int hlineRGBA(SDL_Renderer * renderer, Sint16 x1, Sint16 x2, Sint16 y, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  157. {
  158. int result = 0;
  159. result |= SDL_SetRenderDrawBlendMode(renderer, (a == 255) ? SDL_BLENDMODE_NONE : SDL_BLENDMODE_BLEND);
  160. result |= SDL_SetRenderDrawColor(renderer, r, g, b, a);
  161. result |= SDL_RenderDrawLine(renderer, x1, y, x2, y);
  162. return result;
  163. }
  164. /* ---- Vline */
  165. /*!
  166. \brief Draw vertical line in currently set color
  167. \param renderer The renderer to draw on.
  168. \param x X coordinate of points of the line.
  169. \param y1 Y coordinate of the first point (i.e. top) of the line.
  170. \param y2 Y coordinate of the second point (i.e. bottom) of the line.
  171. \returns Returns 0 on success, -1 on failure.
  172. */
  173. int vline(SDL_Renderer * renderer, Sint16 x, Sint16 y1, Sint16 y2)
  174. {
  175. return SDL_RenderDrawLine(renderer, x, y1, x, y2);;
  176. }
  177. /*!
  178. \brief Draw vertical line with blending.
  179. \param renderer The renderer to draw on.
  180. \param x X coordinate of the points of the line.
  181. \param y1 Y coordinate of the first point (i.e. top) of the line.
  182. \param y2 Y coordinate of the second point (i.e. bottom) of the line.
  183. \param color The color value of the line to draw (0xRRGGBBAA).
  184. \returns Returns 0 on success, -1 on failure.
  185. */
  186. int vlineColor(SDL_Renderer * renderer, Sint16 x, Sint16 y1, Sint16 y2, Uint32 color)
  187. {
  188. Uint8 *c = (Uint8 *)&color;
  189. return vlineRGBA(renderer, x, y1, y2, c[0], c[1], c[2], c[3]);
  190. }
  191. /*!
  192. \brief Draw vertical line with blending.
  193. \param renderer The renderer to draw on.
  194. \param x X coordinate of the points of the line.
  195. \param y1 Y coordinate of the first point (i.e. top) of the line.
  196. \param y2 Y coordinate of the second point (i.e. bottom) of the line.
  197. \param r The red value of the line to draw.
  198. \param g The green value of the line to draw.
  199. \param b The blue value of the line to draw.
  200. \param a The alpha value of the line to draw.
  201. \returns Returns 0 on success, -1 on failure.
  202. */
  203. int vlineRGBA(SDL_Renderer * renderer, Sint16 x, Sint16 y1, Sint16 y2, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  204. {
  205. int result = 0;
  206. result |= SDL_SetRenderDrawBlendMode(renderer, (a == 255) ? SDL_BLENDMODE_NONE : SDL_BLENDMODE_BLEND);
  207. result |= SDL_SetRenderDrawColor(renderer, r, g, b, a);
  208. result |= SDL_RenderDrawLine(renderer, x, y1, x, y2);
  209. return result;
  210. }
  211. /* ---- Rectangle */
  212. /*!
  213. \brief Draw rectangle with blending.
  214. \param renderer The renderer to draw on.
  215. \param x1 X coordinate of the first point (i.e. top right) of the rectangle.
  216. \param y1 Y coordinate of the first point (i.e. top right) of the rectangle.
  217. \param x2 X coordinate of the second point (i.e. bottom left) of the rectangle.
  218. \param y2 Y coordinate of the second point (i.e. bottom left) of the rectangle.
  219. \param color The color value of the rectangle to draw (0xRRGGBBAA).
  220. \returns Returns 0 on success, -1 on failure.
  221. */
  222. int rectangleColor(SDL_Renderer * renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color)
  223. {
  224. Uint8 *c = (Uint8 *)&color;
  225. return rectangleRGBA(renderer, x1, y1, x2, y2, c[0], c[1], c[2], c[3]);
  226. }
  227. /*!
  228. \brief Draw rectangle with blending.
  229. \param renderer The renderer to draw on.
  230. \param x1 X coordinate of the first point (i.e. top right) of the rectangle.
  231. \param y1 Y coordinate of the first point (i.e. top right) of the rectangle.
  232. \param x2 X coordinate of the second point (i.e. bottom left) of the rectangle.
  233. \param y2 Y coordinate of the second point (i.e. bottom left) of the rectangle.
  234. \param r The red value of the rectangle to draw.
  235. \param g The green value of the rectangle to draw.
  236. \param b The blue value of the rectangle to draw.
  237. \param a The alpha value of the rectangle to draw.
  238. \returns Returns 0 on success, -1 on failure.
  239. */
  240. int rectangleRGBA(SDL_Renderer * renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  241. {
  242. int result;
  243. Sint16 tmp;
  244. SDL_Rect rect;
  245. /*
  246. * Test for special cases of straight lines or single point
  247. */
  248. if (x1 == x2) {
  249. if (y1 == y2) {
  250. return (pixelRGBA(renderer, x1, y1, r, g, b, a));
  251. } else {
  252. return (vlineRGBA(renderer, x1, y1, y2, r, g, b, a));
  253. }
  254. } else {
  255. if (y1 == y2) {
  256. return (hlineRGBA(renderer, x1, x2, y1, r, g, b, a));
  257. }
  258. }
  259. /*
  260. * Swap x1, x2 if required
  261. */
  262. if (x1 > x2) {
  263. tmp = x1;
  264. x1 = x2;
  265. x2 = tmp;
  266. }
  267. /*
  268. * Swap y1, y2 if required
  269. */
  270. if (y1 > y2) {
  271. tmp = y1;
  272. y1 = y2;
  273. y2 = tmp;
  274. }
  275. /*
  276. * Create destination rect
  277. */
  278. rect.x = x1;
  279. rect.y = y1;
  280. rect.w = x2 - x1;
  281. rect.h = y2 - y1;
  282. /*
  283. * Draw
  284. */
  285. result = 0;
  286. result |= SDL_SetRenderDrawBlendMode(renderer, (a == 255) ? SDL_BLENDMODE_NONE : SDL_BLENDMODE_BLEND);
  287. result |= SDL_SetRenderDrawColor(renderer, r, g, b, a);
  288. result |= SDL_RenderDrawRect(renderer, &rect);
  289. return result;
  290. }
  291. /* ---- Rounded Rectangle */
  292. /*!
  293. \brief Draw rounded-corner rectangle with blending.
  294. \param renderer The renderer to draw on.
  295. \param x1 X coordinate of the first point (i.e. top right) of the rectangle.
  296. \param y1 Y coordinate of the first point (i.e. top right) of the rectangle.
  297. \param x2 X coordinate of the second point (i.e. bottom left) of the rectangle.
  298. \param y2 Y coordinate of the second point (i.e. bottom left) of the rectangle.
  299. \param rad The radius of the corner arc.
  300. \param color The color value of the rectangle to draw (0xRRGGBBAA).
  301. \returns Returns 0 on success, -1 on failure.
  302. */
  303. int roundedRectangleColor(SDL_Renderer * renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 rad, Uint32 color)
  304. {
  305. Uint8 *c = (Uint8 *)&color;
  306. return roundedRectangleRGBA(renderer, x1, y1, x2, y2, rad, c[0], c[1], c[2], c[3]);
  307. }
  308. /*!
  309. \brief Draw rounded-corner rectangle with blending.
  310. \param renderer The renderer to draw on.
  311. \param x1 X coordinate of the first point (i.e. top right) of the rectangle.
  312. \param y1 Y coordinate of the first point (i.e. top right) of the rectangle.
  313. \param x2 X coordinate of the second point (i.e. bottom left) of the rectangle.
  314. \param y2 Y coordinate of the second point (i.e. bottom left) of the rectangle.
  315. \param rad The radius of the corner arc.
  316. \param r The red value of the rectangle to draw.
  317. \param g The green value of the rectangle to draw.
  318. \param b The blue value of the rectangle to draw.
  319. \param a The alpha value of the rectangle to draw.
  320. \returns Returns 0 on success, -1 on failure.
  321. */
  322. int roundedRectangleRGBA(SDL_Renderer * renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 rad, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  323. {
  324. int result = 0;
  325. Sint16 tmp;
  326. Sint16 w, h;
  327. Sint16 xx1, xx2;
  328. Sint16 yy1, yy2;
  329. /*
  330. * Check renderer
  331. */
  332. if (renderer == NULL)
  333. {
  334. return -1;
  335. }
  336. /*
  337. * Check radius vor valid range
  338. */
  339. if (rad < 0) {
  340. return -1;
  341. }
  342. /*
  343. * Special case - no rounding
  344. */
  345. if (rad <= 1) {
  346. return rectangleRGBA(renderer, x1, y1, x2, y2, r, g, b, a);
  347. }
  348. /*
  349. * Test for special cases of straight lines or single point
  350. */
  351. if (x1 == x2) {
  352. if (y1 == y2) {
  353. return (pixelRGBA(renderer, x1, y1, r, g, b, a));
  354. } else {
  355. return (vlineRGBA(renderer, x1, y1, y2, r, g, b, a));
  356. }
  357. } else {
  358. if (y1 == y2) {
  359. return (hlineRGBA(renderer, x1, x2, y1, r, g, b, a));
  360. }
  361. }
  362. /*
  363. * Swap x1, x2 if required
  364. */
  365. if (x1 > x2) {
  366. tmp = x1;
  367. x1 = x2;
  368. x2 = tmp;
  369. }
  370. /*
  371. * Swap y1, y2 if required
  372. */
  373. if (y1 > y2) {
  374. tmp = y1;
  375. y1 = y2;
  376. y2 = tmp;
  377. }
  378. /*
  379. * Calculate width&height
  380. */
  381. w = x2 - x1;
  382. h = y2 - y1;
  383. /*
  384. * Maybe adjust radius
  385. */
  386. if ((rad * 2) > w)
  387. {
  388. rad = w / 2;
  389. }
  390. if ((rad * 2) > h)
  391. {
  392. rad = h / 2;
  393. }
  394. /*
  395. * Draw corners
  396. */
  397. xx1 = x1 + rad;
  398. xx2 = x2 - rad;
  399. yy1 = y1 + rad;
  400. yy2 = y2 - rad;
  401. result |= arcRGBA(renderer, xx1, yy1, rad, 180, 270, r, g, b, a);
  402. result |= arcRGBA(renderer, xx2, yy1, rad, 270, 360, r, g, b, a);
  403. result |= arcRGBA(renderer, xx1, yy2, rad, 90, 180, r, g, b, a);
  404. result |= arcRGBA(renderer, xx2, yy2, rad, 0, 90, r, g, b, a);
  405. /*
  406. * Draw lines
  407. */
  408. if (xx1 <= xx2) {
  409. result |= hlineRGBA(renderer, xx1, xx2, y1, r, g, b, a);
  410. result |= hlineRGBA(renderer, xx1, xx2, y2, r, g, b, a);
  411. }
  412. if (yy1 <= yy2) {
  413. result |= vlineRGBA(renderer, x1, yy1, yy2, r, g, b, a);
  414. result |= vlineRGBA(renderer, x2, yy1, yy2, r, g, b, a);
  415. }
  416. return result;
  417. }
  418. /* ---- Rounded Box */
  419. /*!
  420. \brief Draw rounded-corner box (filled rectangle) with blending.
  421. \param renderer The renderer to draw on.
  422. \param x1 X coordinate of the first point (i.e. top right) of the box.
  423. \param y1 Y coordinate of the first point (i.e. top right) of the box.
  424. \param x2 X coordinate of the second point (i.e. bottom left) of the box.
  425. \param y2 Y coordinate of the second point (i.e. bottom left) of the box.
  426. \param rad The radius of the corner arcs of the box.
  427. \param color The color value of the box to draw (0xRRGGBBAA).
  428. \returns Returns 0 on success, -1 on failure.
  429. */
  430. int roundedBoxColor(SDL_Renderer * renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 rad, Uint32 color)
  431. {
  432. Uint8 *c = (Uint8 *)&color;
  433. return roundedBoxRGBA(renderer, x1, y1, x2, y2, rad, c[0], c[1], c[2], c[3]);
  434. }
  435. /*!
  436. \brief Draw rounded-corner box (filled rectangle) with blending.
  437. \param renderer The renderer to draw on.
  438. \param x1 X coordinate of the first point (i.e. top right) of the box.
  439. \param y1 Y coordinate of the first point (i.e. top right) of the box.
  440. \param x2 X coordinate of the second point (i.e. bottom left) of the box.
  441. \param y2 Y coordinate of the second point (i.e. bottom left) of the box.
  442. \param rad The radius of the corner arcs of the box.
  443. \param r The red value of the box to draw.
  444. \param g The green value of the box to draw.
  445. \param b The blue value of the box to draw.
  446. \param a The alpha value of the box to draw.
  447. \returns Returns 0 on success, -1 on failure.
  448. */
  449. int roundedBoxRGBA(SDL_Renderer * renderer, Sint16 x1, Sint16 y1, Sint16 x2,
  450. Sint16 y2, Sint16 rad, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  451. {
  452. int result;
  453. Sint16 w, h, r2, tmp;
  454. Sint16 cx = 0;
  455. Sint16 cy = rad;
  456. Sint16 ocx = (Sint16) 0xffff;
  457. Sint16 ocy = (Sint16) 0xffff;
  458. Sint16 df = 1 - rad;
  459. Sint16 d_e = 3;
  460. Sint16 d_se = -2 * rad + 5;
  461. Sint16 xpcx, xmcx, xpcy, xmcy;
  462. Sint16 ypcy, ymcy, ypcx, ymcx;
  463. Sint16 x, y, dx, dy;
  464. /*
  465. * Check destination renderer
  466. */
  467. if (renderer == NULL)
  468. {
  469. return -1;
  470. }
  471. /*
  472. * Check radius vor valid range
  473. */
  474. if (rad < 0) {
  475. return -1;
  476. }
  477. /*
  478. * Special case - no rounding
  479. */
  480. if (rad <= 1) {
  481. return boxRGBA(renderer, x1, y1, x2, y2, r, g, b, a);
  482. }
  483. /*
  484. * Test for special cases of straight lines or single point
  485. */
  486. if (x1 == x2) {
  487. if (y1 == y2) {
  488. return (pixelRGBA(renderer, x1, y1, r, g, b, a));
  489. } else {
  490. return (vlineRGBA(renderer, x1, y1, y2, r, g, b, a));
  491. }
  492. } else {
  493. if (y1 == y2) {
  494. return (hlineRGBA(renderer, x1, x2, y1, r, g, b, a));
  495. }
  496. }
  497. /*
  498. * Swap x1, x2 if required
  499. */
  500. if (x1 > x2) {
  501. tmp = x1;
  502. x1 = x2;
  503. x2 = tmp;
  504. }
  505. /*
  506. * Swap y1, y2 if required
  507. */
  508. if (y1 > y2) {
  509. tmp = y1;
  510. y1 = y2;
  511. y2 = tmp;
  512. }
  513. /*
  514. * Calculate width&height
  515. */
  516. w = x2 - x1 + 1;
  517. h = y2 - y1 + 1;
  518. /*
  519. * Maybe adjust radius
  520. */
  521. r2 = rad + rad;
  522. if (r2 > w)
  523. {
  524. rad = w / 2;
  525. r2 = rad + rad;
  526. }
  527. if (r2 > h)
  528. {
  529. rad = h / 2;
  530. }
  531. /* Setup filled circle drawing for corners */
  532. x = x1 + rad;
  533. y = y1 + rad;
  534. dx = x2 - x1 - rad - rad;
  535. dy = y2 - y1 - rad - rad;
  536. /*
  537. * Set color
  538. */
  539. result = 0;
  540. result |= SDL_SetRenderDrawBlendMode(renderer, (a == 255) ? SDL_BLENDMODE_NONE : SDL_BLENDMODE_BLEND);
  541. result |= SDL_SetRenderDrawColor(renderer, r, g, b, a);
  542. /*
  543. * Draw corners
  544. */
  545. do {
  546. xpcx = x + cx;
  547. xmcx = x - cx;
  548. xpcy = x + cy;
  549. xmcy = x - cy;
  550. if (ocy != cy) {
  551. if (cy > 0) {
  552. ypcy = y + cy;
  553. ymcy = y - cy;
  554. result |= hline(renderer, xmcx, xpcx + dx, ypcy + dy);
  555. result |= hline(renderer, xmcx, xpcx + dx, ymcy);
  556. } else {
  557. result |= hline(renderer, xmcx, xpcx + dx, y);
  558. }
  559. ocy = cy;
  560. }
  561. if (ocx != cx) {
  562. if (cx != cy) {
  563. if (cx > 0) {
  564. ypcx = y + cx;
  565. ymcx = y - cx;
  566. result |= hline(renderer, xmcy, xpcy + dx, ymcx);
  567. result |= hline(renderer, xmcy, xpcy + dx, ypcx + dy);
  568. } else {
  569. result |= hline(renderer, xmcy, xpcy + dx, y);
  570. }
  571. }
  572. ocx = cx;
  573. }
  574. /*
  575. * Update
  576. */
  577. if (df < 0) {
  578. df += d_e;
  579. d_e += 2;
  580. d_se += 2;
  581. } else {
  582. df += d_se;
  583. d_e += 2;
  584. d_se += 4;
  585. cy--;
  586. }
  587. cx++;
  588. } while (cx <= cy);
  589. /* Inside */
  590. if (dx > 0 && dy > 0) {
  591. result |= boxRGBA(renderer, x1, y1 + rad + 1, x2, y2 - rad, r, g, b, a);
  592. }
  593. return (result);
  594. }
  595. /* ---- Box */
  596. /*!
  597. \brief Draw box (filled rectangle) with blending.
  598. \param renderer The renderer to draw on.
  599. \param x1 X coordinate of the first point (i.e. top right) of the box.
  600. \param y1 Y coordinate of the first point (i.e. top right) of the box.
  601. \param x2 X coordinate of the second point (i.e. bottom left) of the box.
  602. \param y2 Y coordinate of the second point (i.e. bottom left) of the box.
  603. \param color The color value of the box to draw (0xRRGGBBAA).
  604. \returns Returns 0 on success, -1 on failure.
  605. */
  606. int boxColor(SDL_Renderer * renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color)
  607. {
  608. Uint8 *c = (Uint8 *)&color;
  609. return boxRGBA(renderer, x1, y1, x2, y2, c[0], c[1], c[2], c[3]);
  610. }
  611. /*!
  612. \brief Draw box (filled rectangle) with blending.
  613. \param renderer The renderer to draw on.
  614. \param x1 X coordinate of the first point (i.e. top right) of the box.
  615. \param y1 Y coordinate of the first point (i.e. top right) of the box.
  616. \param x2 X coordinate of the second point (i.e. bottom left) of the box.
  617. \param y2 Y coordinate of the second point (i.e. bottom left) of the box.
  618. \param r The red value of the box to draw.
  619. \param g The green value of the box to draw.
  620. \param b The blue value of the box to draw.
  621. \param a The alpha value of the box to draw.
  622. \returns Returns 0 on success, -1 on failure.
  623. */
  624. int boxRGBA(SDL_Renderer * renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  625. {
  626. int result;
  627. Sint16 tmp;
  628. SDL_Rect rect;
  629. /*
  630. * Test for special cases of straight lines or single point
  631. */
  632. if (x1 == x2) {
  633. if (y1 == y2) {
  634. return (pixelRGBA(renderer, x1, y1, r, g, b, a));
  635. } else {
  636. return (vlineRGBA(renderer, x1, y1, y2, r, g, b, a));
  637. }
  638. } else {
  639. if (y1 == y2) {
  640. return (hlineRGBA(renderer, x1, x2, y1, r, g, b, a));
  641. }
  642. }
  643. /*
  644. * Swap x1, x2 if required
  645. */
  646. if (x1 > x2) {
  647. tmp = x1;
  648. x1 = x2;
  649. x2 = tmp;
  650. }
  651. /*
  652. * Swap y1, y2 if required
  653. */
  654. if (y1 > y2) {
  655. tmp = y1;
  656. y1 = y2;
  657. y2 = tmp;
  658. }
  659. /*
  660. * Create destination rect
  661. */
  662. rect.x = x1;
  663. rect.y = y1;
  664. rect.w = x2 - x1 + 1;
  665. rect.h = y2 - y1 + 1;
  666. /*
  667. * Draw
  668. */
  669. result = 0;
  670. result |= SDL_SetRenderDrawBlendMode(renderer, (a == 255) ? SDL_BLENDMODE_NONE : SDL_BLENDMODE_BLEND);
  671. result |= SDL_SetRenderDrawColor(renderer, r, g, b, a);
  672. result |= SDL_RenderFillRect(renderer, &rect);
  673. return result;
  674. }
  675. /* ----- Line */
  676. /*!
  677. \brief Draw line with alpha blending using the currently set color.
  678. \param renderer The renderer to draw on.
  679. \param x1 X coordinate of the first point of the line.
  680. \param y1 Y coordinate of the first point of the line.
  681. \param x2 X coordinate of the second point of the line.
  682. \param y2 Y coordinate of the second point of the line.
  683. \returns Returns 0 on success, -1 on failure.
  684. */
  685. int line(SDL_Renderer * renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2)
  686. {
  687. /*
  688. * Draw
  689. */
  690. return SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
  691. }
  692. /*!
  693. \brief Draw line with alpha blending.
  694. \param renderer The renderer to draw on.
  695. \param x1 X coordinate of the first point of the line.
  696. \param y1 Y coordinate of the first point of the line.
  697. \param x2 X coordinate of the second point of the line.
  698. \param y2 Y coordinate of the seond point of the line.
  699. \param color The color value of the line to draw (0xRRGGBBAA).
  700. \returns Returns 0 on success, -1 on failure.
  701. */
  702. int lineColor(SDL_Renderer * renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color)
  703. {
  704. Uint8 *c = (Uint8 *)&color;
  705. return lineRGBA(renderer, x1, y1, x2, y2, c[0], c[1], c[2], c[3]);
  706. }
  707. /*!
  708. \brief Draw line with alpha blending.
  709. \param renderer The renderer to draw on.
  710. \param x1 X coordinate of the first point of the line.
  711. \param y1 Y coordinate of the first point of the line.
  712. \param x2 X coordinate of the second point of the line.
  713. \param y2 Y coordinate of the second point of the line.
  714. \param r The red value of the line to draw.
  715. \param g The green value of the line to draw.
  716. \param b The blue value of the line to draw.
  717. \param a The alpha value of the line to draw.
  718. \returns Returns 0 on success, -1 on failure.
  719. */
  720. int lineRGBA(SDL_Renderer * renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  721. {
  722. /*
  723. * Draw
  724. */
  725. int result = 0;
  726. result |= SDL_SetRenderDrawBlendMode(renderer, (a == 255) ? SDL_BLENDMODE_NONE : SDL_BLENDMODE_BLEND);
  727. result |= SDL_SetRenderDrawColor(renderer, r, g, b, a);
  728. result |= SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
  729. return result;
  730. }
  731. /* ---- AA Line */
  732. #define AAlevels 256
  733. #define AAbits 8
  734. /*!
  735. \brief Internal function to draw anti-aliased line with alpha blending and endpoint control.
  736. This implementation of the Wu antialiasing code is based on Mike Abrash's
  737. DDJ article which was reprinted as Chapter 42 of his Graphics Programming
  738. Black Book, but has been optimized to work with SDL and utilizes 32-bit
  739. fixed-point arithmetic by A. Schiffler. The endpoint control allows the
  740. supression to draw the last pixel useful for rendering continous aa-lines
  741. with alpha<255.
  742. \param renderer The renderer to draw on.
  743. \param x1 X coordinate of the first point of the aa-line.
  744. \param y1 Y coordinate of the first point of the aa-line.
  745. \param x2 X coordinate of the second point of the aa-line.
  746. \param y2 Y coordinate of the second point of the aa-line.
  747. \param r The red value of the aa-line to draw.
  748. \param g The green value of the aa-line to draw.
  749. \param b The blue value of the aa-line to draw.
  750. \param a The alpha value of the aa-line to draw.
  751. \param draw_endpoint Flag indicating if the endpoint should be drawn; draw if non-zero.
  752. \returns Returns 0 on success, -1 on failure.
  753. */
  754. int _aalineRGBA(SDL_Renderer * renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint8 r, Uint8 g, Uint8 b, Uint8 a, int draw_endpoint)
  755. {
  756. Sint32 xx0, yy0, xx1, yy1;
  757. int result;
  758. Uint32 intshift, erracc, erradj;
  759. Uint32 erracctmp, wgt, wgtcompmask;
  760. int dx, dy, tmp, xdir, y0p1, x0pxdir;
  761. /*
  762. * Keep on working with 32bit numbers
  763. */
  764. xx0 = x1;
  765. yy0 = y1;
  766. xx1 = x2;
  767. yy1 = y2;
  768. /*
  769. * Reorder points to make dy positive
  770. */
  771. if (yy0 > yy1) {
  772. tmp = yy0;
  773. yy0 = yy1;
  774. yy1 = tmp;
  775. tmp = xx0;
  776. xx0 = xx1;
  777. xx1 = tmp;
  778. }
  779. /*
  780. * Calculate distance
  781. */
  782. dx = xx1 - xx0;
  783. dy = yy1 - yy0;
  784. /*
  785. * Adjust for negative dx and set xdir
  786. */
  787. if (dx >= 0) {
  788. xdir = 1;
  789. } else {
  790. xdir = -1;
  791. dx = (-dx);
  792. }
  793. /*
  794. * Check for special cases
  795. */
  796. if (dx == 0) {
  797. /*
  798. * Vertical line
  799. */
  800. if (draw_endpoint)
  801. {
  802. return (vlineRGBA(renderer, x1, y1, y2, r, g, b, a));
  803. } else {
  804. if (dy > 0) {
  805. return (vlineRGBA(renderer, x1, yy0, yy0+dy, r, g, b, a));
  806. } else {
  807. return (pixelRGBA(renderer, x1, y1, r, g, b, a));
  808. }
  809. }
  810. } else if (dy == 0) {
  811. /*
  812. * Horizontal line
  813. */
  814. if (draw_endpoint)
  815. {
  816. return (hlineRGBA(renderer, x1, x2, y1, r, g, b, a));
  817. } else {
  818. if (dx > 0) {
  819. return (hlineRGBA(renderer, xx0, xx0+(xdir*dx), y1, r, g, b, a));
  820. } else {
  821. return (pixelRGBA(renderer, x1, y1, r, g, b, a));
  822. }
  823. }
  824. } else if ((dx == dy) && (draw_endpoint)) {
  825. /*
  826. * Diagonal line (with endpoint)
  827. */
  828. return (lineRGBA(renderer, x1, y1, x2, y2, r, g, b, a));
  829. }
  830. /*
  831. * Line is not horizontal, vertical or diagonal (with endpoint)
  832. */
  833. result = 0;
  834. /*
  835. * Zero accumulator
  836. */
  837. erracc = 0;
  838. /*
  839. * # of bits by which to shift erracc to get intensity level
  840. */
  841. intshift = 32 - AAbits;
  842. /*
  843. * Mask used to flip all bits in an intensity weighting
  844. */
  845. wgtcompmask = AAlevels - 1;
  846. /*
  847. * Draw the initial pixel in the foreground color
  848. */
  849. result |= pixelRGBA(renderer, x1, y1, r, g, b, a);
  850. /*
  851. * x-major or y-major?
  852. */
  853. if (dy > dx) {
  854. /*
  855. * y-major. Calculate 16-bit fixed point fractional part of a pixel that
  856. * X advances every time Y advances 1 pixel, truncating the result so that
  857. * we won't overrun the endpoint along the X axis
  858. */
  859. /*
  860. * Not-so-portable version: erradj = ((Uint64)dx << 32) / (Uint64)dy;
  861. */
  862. erradj = ((dx << 16) / dy) << 16;
  863. /*
  864. * draw all pixels other than the first and last
  865. */
  866. x0pxdir = xx0 + xdir;
  867. while (--dy) {
  868. erracctmp = erracc;
  869. erracc += erradj;
  870. if (erracc <= erracctmp) {
  871. /*
  872. * rollover in error accumulator, x coord advances
  873. */
  874. xx0 = x0pxdir;
  875. x0pxdir += xdir;
  876. }
  877. yy0++; /* y-major so always advance Y */
  878. /*
  879. * the AAbits most significant bits of erracc give us the intensity
  880. * weighting for this pixel, and the complement of the weighting for
  881. * the paired pixel.
  882. */
  883. wgt = (erracc >> intshift) & 255;
  884. result |= pixelRGBAWeight (renderer, xx0, yy0, r, g, b, a, 255 - wgt);
  885. result |= pixelRGBAWeight (renderer, x0pxdir, yy0, r, g, b, a, wgt);
  886. }
  887. } else {
  888. /*
  889. * x-major line. Calculate 16-bit fixed-point fractional part of a pixel
  890. * that Y advances each time X advances 1 pixel, truncating the result so
  891. * that we won't overrun the endpoint along the X axis.
  892. */
  893. /*
  894. * Not-so-portable version: erradj = ((Uint64)dy << 32) / (Uint64)dx;
  895. */
  896. erradj = ((dy << 16) / dx) << 16;
  897. /*
  898. * draw all pixels other than the first and last
  899. */
  900. y0p1 = yy0 + 1;
  901. while (--dx) {
  902. erracctmp = erracc;
  903. erracc += erradj;
  904. if (erracc <= erracctmp) {
  905. /*
  906. * Accumulator turned over, advance y
  907. */
  908. yy0 = y0p1;
  909. y0p1++;
  910. }
  911. xx0 += xdir; /* x-major so always advance X */
  912. /*
  913. * the AAbits most significant bits of erracc give us the intensity
  914. * weighting for this pixel, and the complement of the weighting for
  915. * the paired pixel.
  916. */
  917. wgt = (erracc >> intshift) & 255;
  918. result |= pixelRGBAWeight (renderer, xx0, yy0, r, g, b, a, 255 - wgt);
  919. result |= pixelRGBAWeight (renderer, xx0, y0p1, r, g, b, a, wgt);
  920. }
  921. }
  922. /*
  923. * Do we have to draw the endpoint
  924. */
  925. if (draw_endpoint) {
  926. /*
  927. * Draw final pixel, always exactly intersected by the line and doesn't
  928. * need to be weighted.
  929. */
  930. result |= pixelRGBA (renderer, x2, y2, r, g, b, a);
  931. }
  932. return (result);
  933. }
  934. /*!
  935. \brief Draw anti-aliased line with alpha blending.
  936. \param renderer The renderer to draw on.
  937. \param x1 X coordinate of the first point of the aa-line.
  938. \param y1 Y coordinate of the first point of the aa-line.
  939. \param x2 X coordinate of the second point of the aa-line.
  940. \param y2 Y coordinate of the second point of the aa-line.
  941. \param color The color value of the aa-line to draw (0xRRGGBBAA).
  942. \returns Returns 0 on success, -1 on failure.
  943. */
  944. int aalineColor(SDL_Renderer * renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color)
  945. {
  946. Uint8 *c = (Uint8 *)&color;
  947. return _aalineRGBA(renderer, x1, y1, x2, y2, c[0], c[1], c[2], c[3], 1);
  948. }
  949. /*!
  950. \brief Draw anti-aliased line with alpha blending.
  951. \param renderer The renderer to draw on.
  952. \param x1 X coordinate of the first point of the aa-line.
  953. \param y1 Y coordinate of the first point of the aa-line.
  954. \param x2 X coordinate of the second point of the aa-line.
  955. \param y2 Y coordinate of the second point of the aa-line.
  956. \param r The red value of the aa-line to draw.
  957. \param g The green value of the aa-line to draw.
  958. \param b The blue value of the aa-line to draw.
  959. \param a The alpha value of the aa-line to draw.
  960. \returns Returns 0 on success, -1 on failure.
  961. */
  962. int aalineRGBA(SDL_Renderer * renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  963. {
  964. return _aalineRGBA(renderer, x1, y1, x2, y2, r, g, b, a, 1);
  965. }
  966. /* ----- Circle */
  967. /*!
  968. \brief Draw circle with blending.
  969. \param renderer The renderer to draw on.
  970. \param x X coordinate of the center of the circle.
  971. \param y Y coordinate of the center of the circle.
  972. \param rad Radius in pixels of the circle.
  973. \param color The color value of the circle to draw (0xRRGGBBAA).
  974. \returns Returns 0 on success, -1 on failure.
  975. */
  976. int circleColor(SDL_Renderer * renderer, Sint16 x, Sint16 y, Sint16 rad, Uint32 color)
  977. {
  978. Uint8 *c = (Uint8 *)&color;
  979. return ellipseRGBA(renderer, x, y, rad, rad, c[0], c[1], c[2], c[3]);
  980. }
  981. /*!
  982. \brief Draw circle with blending.
  983. \param renderer The renderer to draw on.
  984. \param x X coordinate of the center of the circle.
  985. \param y Y coordinate of the center of the circle.
  986. \param rad Radius in pixels of the circle.
  987. \param r The red value of the circle to draw.
  988. \param g The green value of the circle to draw.
  989. \param b The blue value of the circle to draw.
  990. \param a The alpha value of the circle to draw.
  991. \returns Returns 0 on success, -1 on failure.
  992. */
  993. int circleRGBA(SDL_Renderer * renderer, Sint16 x, Sint16 y, Sint16 rad, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  994. {
  995. return ellipseRGBA(renderer, x, y, rad, rad, r, g, b, a);
  996. }
  997. /* ----- Arc */
  998. /*!
  999. \brief Arc with blending.
  1000. \param renderer The renderer to draw on.
  1001. \param x X coordinate of the center of the arc.
  1002. \param y Y coordinate of the center of the arc.
  1003. \param rad Radius in pixels of the arc.
  1004. \param start Starting radius in degrees of the arc. 0 degrees is down, increasing counterclockwise.
  1005. \param end Ending radius in degrees of the arc. 0 degrees is down, increasing counterclockwise.
  1006. \param color The color value of the arc to draw (0xRRGGBBAA).
  1007. \returns Returns 0 on success, -1 on failure.
  1008. */
  1009. int arcColor(SDL_Renderer * renderer, Sint16 x, Sint16 y, Sint16 rad, Sint16 start, Sint16 end, Uint32 color)
  1010. {
  1011. Uint8 *c = (Uint8 *)&color;
  1012. return arcRGBA(renderer, x, y, rad, start, end, c[0], c[1], c[2], c[3]);
  1013. }
  1014. /*!
  1015. \brief Arc with blending.
  1016. \param renderer The renderer to draw on.
  1017. \param x X coordinate of the center of the arc.
  1018. \param y Y coordinate of the center of the arc.
  1019. \param rad Radius in pixels of the arc.
  1020. \param start Starting radius in degrees of the arc. 0 degrees is down, increasing counterclockwise.
  1021. \param end Ending radius in degrees of the arc. 0 degrees is down, increasing counterclockwise.
  1022. \param r The red value of the arc to draw.
  1023. \param g The green value of the arc to draw.
  1024. \param b The blue value of the arc to draw.
  1025. \param a The alpha value of the arc to draw.
  1026. \returns Returns 0 on success, -1 on failure.
  1027. */
  1028. /* TODO: rewrite algorithm; arc endpoints are not always drawn */
  1029. int arcRGBA(SDL_Renderer * renderer, Sint16 x, Sint16 y, Sint16 rad, Sint16 start, Sint16 end, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  1030. {
  1031. int result;
  1032. Sint16 cx = 0;
  1033. Sint16 cy = rad;
  1034. Sint16 df = 1 - rad;
  1035. Sint16 d_e = 3;
  1036. Sint16 d_se = -2 * rad + 5;
  1037. Sint16 xpcx, xmcx, xpcy, xmcy;
  1038. Sint16 ypcy, ymcy, ypcx, ymcx;
  1039. Uint8 drawoct;
  1040. int startoct, endoct, oct, stopval_start = 0, stopval_end = 0;
  1041. double dstart, dend, temp = 0.;
  1042. /*
  1043. * Sanity check radius
  1044. */
  1045. if (rad < 0) {
  1046. return (-1);
  1047. }
  1048. /*
  1049. * Special case for rad=0 - draw a point
  1050. */
  1051. if (rad == 0) {
  1052. return (pixelRGBA(renderer, x, y, r, g, b, a));
  1053. }
  1054. /*
  1055. Octant labeling
  1056. \ 5 | 6 /
  1057. \ | /
  1058. 4 \ | / 7
  1059. \|/
  1060. ------+------ +x
  1061. /|\
  1062. 3 / | \ 0
  1063. / | \
  1064. / 2 | 1 \
  1065. +y
  1066. Initially reset bitmask to 0x00000000
  1067. the set whether or not to keep drawing a given octant.
  1068. For example: 0x00111100 means we're drawing in octants 2-5
  1069. */
  1070. drawoct = 0;
  1071. /*
  1072. * Fixup angles
  1073. */
  1074. start %= 360;
  1075. end %= 360;
  1076. /* 0 <= start & end < 360; note that sometimes start > end - if so, arc goes back through 0. */
  1077. while (start < 0) start += 360;
  1078. while (end < 0) end += 360;
  1079. start %= 360;
  1080. end %= 360;
  1081. /* now, we find which octants we're drawing in. */
  1082. startoct = start / 45;
  1083. endoct = end / 45;
  1084. oct = startoct - 1;
  1085. /* stopval_start, stopval_end; what values of cx to stop at. */
  1086. do {
  1087. oct = (oct + 1) % 8;
  1088. if (oct == startoct) {
  1089. /* need to compute stopval_start for this octant. Look at picture above if this is unclear */
  1090. dstart = (double)start;
  1091. switch (oct)
  1092. {
  1093. case 0:
  1094. case 3:
  1095. temp = sin(dstart * M_PI / 180.);
  1096. break;
  1097. case 1:
  1098. case 6:
  1099. temp = cos(dstart * M_PI / 180.);
  1100. break;
  1101. case 2:
  1102. case 5:
  1103. temp = -cos(dstart * M_PI / 180.);
  1104. break;
  1105. case 4:
  1106. case 7:
  1107. temp = -sin(dstart * M_PI / 180.);
  1108. break;
  1109. }
  1110. temp *= rad;
  1111. stopval_start = (int)temp;
  1112. /*
  1113. This isn't arbitrary, but requires graph paper to explain well.
  1114. The basic idea is that we're always changing drawoct after we draw, so we
  1115. stop immediately after we render the last sensible pixel at x = ((int)temp).
  1116. and whether to draw in this octant initially
  1117. */
  1118. if (oct % 2) drawoct |= (1 << oct); /* this is basically like saying drawoct[oct] = true, if drawoct were a bool array */
  1119. else drawoct &= 255 - (1 << oct); /* this is basically like saying drawoct[oct] = false */
  1120. }
  1121. if (oct == endoct) {
  1122. /* need to compute stopval_end for this octant */
  1123. dend = (double)end;
  1124. switch (oct)
  1125. {
  1126. case 0:
  1127. case 3:
  1128. temp = sin(dend * M_PI / 180);
  1129. break;
  1130. case 1:
  1131. case 6:
  1132. temp = cos(dend * M_PI / 180);
  1133. break;
  1134. case 2:
  1135. case 5:
  1136. temp = -cos(dend * M_PI / 180);
  1137. break;
  1138. case 4:
  1139. case 7:
  1140. temp = -sin(dend * M_PI / 180);
  1141. break;
  1142. }
  1143. temp *= rad;
  1144. stopval_end = (int)temp;
  1145. /* and whether to draw in this octant initially */
  1146. if (startoct == endoct) {
  1147. /* note: we start drawing, stop, then start again in this case */
  1148. /* otherwise: we only draw in this octant, so initialize it to false, it will get set back to true */
  1149. if (start > end) {
  1150. /* unfortunately, if we're in the same octant and need to draw over the whole circle, */
  1151. /* we need to set the rest to true, because the while loop will end at the bottom. */
  1152. drawoct = 255;
  1153. } else {
  1154. drawoct &= 255 - (1 << oct);
  1155. }
  1156. }
  1157. else if (oct % 2) drawoct &= 255 - (1 << oct);
  1158. else drawoct |= (1 << oct);
  1159. } else if (oct != startoct) { /* already verified that it's != endoct */
  1160. drawoct |= (1 << oct); /* draw this entire segment */
  1161. }
  1162. } while (oct != endoct);
  1163. /* so now we have what octants to draw and when to draw them. all that's left is the actual raster code. */
  1164. /*
  1165. * Set color
  1166. */
  1167. result = 0;
  1168. result |= SDL_SetRenderDrawBlendMode(renderer, (a == 255) ? SDL_BLENDMODE_NONE : SDL_BLENDMODE_BLEND);
  1169. result |= SDL_SetRenderDrawColor(renderer, r, g, b, a);
  1170. /*
  1171. * Draw arc
  1172. */
  1173. do {
  1174. ypcy = y + cy;
  1175. ymcy = y - cy;
  1176. if (cx > 0) {
  1177. xpcx = x + cx;
  1178. xmcx = x - cx;
  1179. /* always check if we're drawing a certain octant before adding a pixel to that octant. */
  1180. if (drawoct & 4) result |= pixel(renderer, xmcx, ypcy);
  1181. if (drawoct & 2) result |= pixel(renderer, xpcx, ypcy);
  1182. if (drawoct & 32) result |= pixel(renderer, xmcx, ymcy);
  1183. if (drawoct & 64) result |= pixel(renderer, xpcx, ymcy);
  1184. } else {
  1185. if (drawoct & 96) result |= pixel(renderer, x, ymcy);
  1186. if (drawoct & 6) result |= pixel(renderer, x, ypcy);
  1187. }
  1188. xpcy = x + cy;
  1189. xmcy = x - cy;
  1190. if (cx > 0 && cx != cy) {
  1191. ypcx = y + cx;
  1192. ymcx = y - cx;
  1193. if (drawoct & 8) result |= pixel(renderer, xmcy, ypcx);
  1194. if (drawoct & 1) result |= pixel(renderer, xpcy, ypcx);
  1195. if (drawoct & 16) result |= pixel(renderer, xmcy, ymcx);
  1196. if (drawoct & 128) result |= pixel(renderer, xpcy, ymcx);
  1197. } else if (cx == 0) {
  1198. if (drawoct & 24) result |= pixel(renderer, xmcy, y);
  1199. if (drawoct & 129) result |= pixel(renderer, xpcy, y);
  1200. }
  1201. /*
  1202. * Update whether we're drawing an octant
  1203. */
  1204. if (stopval_start == cx) {
  1205. /* works like an on-off switch. */
  1206. /* This is just in case start & end are in the same octant. */
  1207. if (drawoct & (1 << startoct)) drawoct &= 255 - (1 << startoct);
  1208. else drawoct |= (1 << startoct);
  1209. }
  1210. if (stopval_end == cx) {
  1211. if (drawoct & (1 << endoct)) drawoct &= 255 - (1 << endoct);
  1212. else drawoct |= (1 << endoct);
  1213. }
  1214. /*
  1215. * Update pixels
  1216. */
  1217. if (df < 0) {
  1218. df += d_e;
  1219. d_e += 2;
  1220. d_se += 2;
  1221. } else {
  1222. df += d_se;
  1223. d_e += 2;
  1224. d_se += 4;
  1225. cy--;
  1226. }
  1227. cx++;
  1228. } while (cx <= cy);
  1229. return (result);
  1230. }
  1231. /* ----- AA Circle */
  1232. /*!
  1233. \brief Draw anti-aliased circle with blending.
  1234. \param renderer The renderer to draw on.
  1235. \param x X coordinate of the center of the aa-circle.
  1236. \param y Y coordinate of the center of the aa-circle.
  1237. \param rad Radius in pixels of the aa-circle.
  1238. \param color The color value of the aa-circle to draw (0xRRGGBBAA).
  1239. \returns Returns 0 on success, -1 on failure.
  1240. */
  1241. int aacircleColor(SDL_Renderer * renderer, Sint16 x, Sint16 y, Sint16 rad, Uint32 color)
  1242. {
  1243. Uint8 *c = (Uint8 *)&color;
  1244. return aaellipseRGBA(renderer, x, y, rad, rad, c[0], c[1], c[2], c[3]);
  1245. }
  1246. /*!
  1247. \brief Draw anti-aliased circle with blending.
  1248. \param renderer The renderer to draw on.
  1249. \param x X coordinate of the center of the aa-circle.
  1250. \param y Y coordinate of the center of the aa-circle.
  1251. \param rad Radius in pixels of the aa-circle.
  1252. \param r The red value of the aa-circle to draw.
  1253. \param g The green value of the aa-circle to draw.
  1254. \param b The blue value of the aa-circle to draw.
  1255. \param a The alpha value of the aa-circle to draw.
  1256. \returns Returns 0 on success, -1 on failure.
  1257. */
  1258. int aacircleRGBA(SDL_Renderer * renderer, Sint16 x, Sint16 y, Sint16 rad, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  1259. {
  1260. /*
  1261. * Draw
  1262. */
  1263. return aaellipseRGBA(renderer, x, y, rad, rad, r, g, b, a);
  1264. }
  1265. /* ----- Ellipse */
  1266. /*!
  1267. \brief Internal function to draw pixels or lines in 4 quadrants.
  1268. \param renderer The renderer to draw on.
  1269. \param x X coordinate of the center of the quadrant.
  1270. \param y Y coordinate of the center of the quadrant.
  1271. \param dx X offset in pixels of the corners of the quadrant.
  1272. \param dy Y offset in pixels of the corners of the quadrant.
  1273. \param f Flag indicating if the quadrant should be filled (1) or not (0).
  1274. \returns Returns 0 on success, -1 on failure.
  1275. */
  1276. int _drawQuadrants(SDL_Renderer * renderer, Sint16 x, Sint16 y, Sint16 dx, Sint16 dy, Sint32 f)
  1277. {
  1278. int result = 0;
  1279. Sint16 xpdx, xmdx;
  1280. Sint16 ypdy, ymdy;
  1281. if (dx == 0) {
  1282. if (dy == 0) {
  1283. result |= pixel(renderer, x, y);
  1284. } else {
  1285. ypdy = y + dy;
  1286. ymdy = y - dy;
  1287. if (f) {
  1288. result |= vline(renderer, x, ymdy, ypdy);
  1289. } else {
  1290. result |= pixel(renderer, x, ypdy);
  1291. result |= pixel(renderer, x, ymdy);
  1292. }
  1293. }
  1294. } else {
  1295. xpdx = x + dx;
  1296. xmdx = x - dx;
  1297. ypdy = y + dy;
  1298. ymdy = y - dy;
  1299. if (f) {
  1300. result |= vline(renderer, xpdx, ymdy, ypdy);
  1301. result |= vline(renderer, xmdx, ymdy, ypdy);
  1302. } else {
  1303. result |= pixel(renderer, xpdx, ypdy);
  1304. result |= pixel(renderer, xmdx, ypdy);
  1305. result |= pixel(renderer, xpdx, ymdy);
  1306. result |= pixel(renderer, xmdx, ymdy);
  1307. }
  1308. }
  1309. return result;
  1310. }
  1311. /*!
  1312. \brief Internal function to draw ellipse or filled ellipse with blending.
  1313. \param renderer The renderer to draw on.
  1314. \param x X coordinate of the center of the ellipse.
  1315. \param y Y coordinate of the center of the ellipse.
  1316. \param rx Horizontal radius in pixels of the ellipse.
  1317. \param ry Vertical radius in pixels of the ellipse.
  1318. \param r The red value of the ellipse to draw.
  1319. \param g The green value of the ellipse to draw.
  1320. \param b The blue value of the ellipse to draw.
  1321. \param a The alpha value of the ellipse to draw.
  1322. \param f Flag indicating if the ellipse should be filled (1) or not (0).
  1323. \returns Returns 0 on success, -1 on failure.
  1324. */
  1325. #define DEFAULT_ELLIPSE_OVERSCAN 4
  1326. int _ellipseRGBA(SDL_Renderer * renderer, Sint16 x, Sint16 y, Sint16 rx, Sint16 ry, Uint8 r, Uint8 g, Uint8 b, Uint8 a, Sint32 f)
  1327. {
  1328. int result;
  1329. Sint32 rxi, ryi;
  1330. Sint32 rx2, ry2, rx22, ry22;
  1331. Sint32 error;
  1332. Sint32 curX, curY, curXp1, curYm1;
  1333. Sint32 scrX, scrY, oldX, oldY;
  1334. Sint32 deltaX, deltaY;
  1335. Sint32 ellipseOverscan;
  1336. /*
  1337. * Sanity check radii
  1338. */
  1339. if ((rx < 0) || (ry < 0)) {
  1340. return (-1);
  1341. }
  1342. /*
  1343. * Set color
  1344. */
  1345. result = 0;
  1346. result |= SDL_SetRenderDrawBlendMode(renderer, (a == 255) ? SDL_BLENDMODE_NONE : SDL_BLENDMODE_BLEND);
  1347. result |= SDL_SetRenderDrawColor(renderer, r, g, b, a);
  1348. /*
  1349. * Special cases for rx=0 and/or ry=0: draw a hline/vline/pixel
  1350. */
  1351. if (rx == 0) {
  1352. if (ry == 0) {
  1353. return (pixel(renderer, x, y));
  1354. } else {
  1355. return (vline(renderer, x, y - ry, y + ry));
  1356. }
  1357. } else {
  1358. if (ry == 0) {
  1359. return (hline(renderer, x - rx, x + rx, y));
  1360. }
  1361. }
  1362. /*
  1363. * Adjust overscan
  1364. */
  1365. rxi = rx;
  1366. ryi = ry;
  1367. if (rxi >= 512 || ryi >= 512)
  1368. {
  1369. ellipseOverscan = DEFAULT_ELLIPSE_OVERSCAN / 4;
  1370. }
  1371. else if (rxi >= 256 || ryi >= 256)
  1372. {
  1373. ellipseOverscan = DEFAULT_ELLIPSE_OVERSCAN / 2;
  1374. }
  1375. else
  1376. {
  1377. ellipseOverscan = DEFAULT_ELLIPSE_OVERSCAN / 1;
  1378. }
  1379. /*
  1380. * Top/bottom center points.
  1381. */
  1382. oldX = scrX = 0;
  1383. oldY = scrY = ryi;
  1384. result |= _drawQuadrants(renderer, x, y, 0, ry, f);
  1385. /* Midpoint ellipse algorithm with overdraw */
  1386. rxi *= ellipseOverscan;
  1387. ryi *= ellipseOverscan;
  1388. rx2 = rxi * rxi;
  1389. rx22 = rx2 + rx2;
  1390. ry2 = ryi * ryi;
  1391. ry22 = ry2 + ry2;
  1392. curX = 0;
  1393. curY = ryi;
  1394. deltaX = 0;
  1395. deltaY = rx22 * curY;
  1396. /* Points in segment 1 */
  1397. error = ry2 - rx2 * ryi + rx2 / 4;
  1398. while (deltaX <= deltaY)
  1399. {
  1400. curX++;
  1401. deltaX += ry22;
  1402. error += deltaX + ry2;
  1403. if (error >= 0)
  1404. {
  1405. curY--;
  1406. deltaY -= rx22;
  1407. error -= deltaY;
  1408. }
  1409. scrX = curX / ellipseOverscan;
  1410. scrY = curY / ellipseOverscan;
  1411. if ((scrX != oldX && scrY == oldY) || (scrX != oldX && scrY != oldY)) {
  1412. result |= _drawQuadrants(renderer, x, y, scrX, scrY, f);
  1413. oldX = scrX;
  1414. oldY = scrY;
  1415. }
  1416. }
  1417. /* Points in segment 2 */
  1418. if (curY > 0)
  1419. {
  1420. curXp1 = curX + 1;
  1421. curYm1 = curY - 1;
  1422. error = ry2 * curX * curXp1 + ((ry2 + 3) / 4) + rx2 * curYm1 * curYm1 - rx2 * ry2;
  1423. while (curY > 0)
  1424. {
  1425. curY--;
  1426. deltaY -= rx22;
  1427. error += rx2;
  1428. error -= deltaY;
  1429. if (error <= 0)
  1430. {
  1431. curX++;
  1432. deltaX += ry22;
  1433. error += deltaX;
  1434. }
  1435. scrX = curX / ellipseOverscan;
  1436. scrY = curY / ellipseOverscan;
  1437. if ((scrX != oldX && scrY == oldY) || (scrX != oldX && scrY != oldY)) {
  1438. oldY--;
  1439. for (;oldY >= scrY; oldY--) {
  1440. result |= _drawQuadrants(renderer, x, y, scrX, oldY, f);
  1441. /* prevent overdraw */
  1442. if (f) {
  1443. oldY = scrY - 1;
  1444. }
  1445. }
  1446. oldX = scrX;
  1447. oldY = scrY;
  1448. }
  1449. }
  1450. /* Remaining points in vertical */
  1451. if (!f) {
  1452. oldY--;
  1453. for (;oldY >= 0; oldY--) {
  1454. result |= _drawQuadrants(renderer, x, y, scrX, oldY, f);
  1455. }
  1456. }
  1457. }
  1458. return (result);
  1459. }
  1460. /*!
  1461. \brief Draw ellipse with blending.
  1462. \param renderer The renderer to draw on.
  1463. \param x X coordinate of the center of the ellipse.
  1464. \param y Y coordinate of the center of the ellipse.
  1465. \param rx Horizontal radius in pixels of the ellipse.
  1466. \param ry Vertical radius in pixels of the ellipse.
  1467. \param color The color value of the ellipse to draw (0xRRGGBBAA).
  1468. \returns Returns 0 on success, -1 on failure.
  1469. */
  1470. int ellipseColor(SDL_Renderer * renderer, Sint16 x, Sint16 y, Sint16 rx, Sint16 ry, Uint32 color)
  1471. {
  1472. Uint8 *c = (Uint8 *)&color;
  1473. return _ellipseRGBA(renderer, x, y, rx, ry, c[0], c[1], c[2], c[3], 0);
  1474. }
  1475. /*!
  1476. \brief Draw ellipse with blending.
  1477. \param renderer The renderer to draw on.
  1478. \param x X coordinate of the center of the ellipse.
  1479. \param y Y coordinate of the center of the ellipse.
  1480. \param rx Horizontal radius in pixels of the ellipse.
  1481. \param ry Vertical radius in pixels of the ellipse.
  1482. \param r The red value of the ellipse to draw.
  1483. \param g The green value of the ellipse to draw.
  1484. \param b The blue value of the ellipse to draw.
  1485. \param a The alpha value of the ellipse to draw.
  1486. \returns Returns 0 on success, -1 on failure.
  1487. */
  1488. int ellipseRGBA(SDL_Renderer * renderer, Sint16 x, Sint16 y, Sint16 rx, Sint16 ry, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  1489. {
  1490. return _ellipseRGBA(renderer, x, y, rx, ry, r, g, b, a, 0);
  1491. }
  1492. /* ----- Filled Circle */
  1493. /*!
  1494. \brief Draw filled circle with blending.
  1495. \param renderer The renderer to draw on.
  1496. \param x X coordinate of the center of the filled circle.
  1497. \param y Y coordinate of the center of the filled circle.
  1498. \param rad Radius in pixels of the filled circle.
  1499. \param color The color value of the filled circle to draw (0xRRGGBBAA).
  1500. \returns Returns 0 on success, -1 on failure.
  1501. */
  1502. int filledCircleColor(SDL_Renderer * renderer, Sint16 x, Sint16 y, Sint16 rad, Uint32 color)
  1503. {
  1504. Uint8 *c = (Uint8 *)&color;
  1505. return filledEllipseRGBA(renderer, x, y, rad, rad, c[0], c[1], c[2], c[3]);
  1506. }
  1507. /*!
  1508. \brief Draw filled circle with blending.
  1509. \param renderer The renderer to draw on.
  1510. \param x X coordinate of the center of the filled circle.
  1511. \param y Y coordinate of the center of the filled circle.
  1512. \param rad Radius in pixels of the filled circle.
  1513. \param r The red value of the filled circle to draw.
  1514. \param g The green value of the filled circle to draw.
  1515. \param b The blue value of the filled circle to draw.
  1516. \param a The alpha value of the filled circle to draw.
  1517. \returns Returns 0 on success, -1 on failure.
  1518. */
  1519. int filledCircleRGBA(SDL_Renderer * renderer, Sint16 x, Sint16 y, Sint16 rad, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  1520. {
  1521. return _ellipseRGBA(renderer, x, y, rad, rad, r, g ,b, a, 1);
  1522. }
  1523. /* ----- AA Ellipse */
  1524. /* Windows targets do not have lrint, so provide a local inline version */
  1525. #if defined(_MSC_VER)
  1526. /* Detect 64bit and use intrinsic version */
  1527. #ifdef _M_X64
  1528. #include <emmintrin.h>
  1529. static __inline long
  1530. lrint(float f)
  1531. {
  1532. return _mm_cvtss_si32(_mm_load_ss(&f));
  1533. }
  1534. #elif defined(_M_IX86)
  1535. __inline long int
  1536. lrint (double flt)
  1537. {
  1538. int intgr;
  1539. _asm
  1540. {
  1541. fld flt
  1542. fistp intgr
  1543. };
  1544. return intgr;
  1545. }
  1546. #elif defined(_M_ARM)
  1547. #include <armintr.h>
  1548. #pragma warning(push)
  1549. #pragma warning(disable: 4716)
  1550. __declspec(naked) long int
  1551. lrint (double flt)
  1552. {
  1553. __emit(0xEC410B10); // fmdrr d0, r0, r1
  1554. __emit(0xEEBD0B40); // ftosid s0, d0
  1555. __emit(0xEE100A10); // fmrs r0, s0
  1556. __emit(0xE12FFF1E); // bx lr
  1557. }
  1558. #pragma warning(pop)
  1559. #else
  1560. #error lrint needed for MSVC on non X86/AMD64/ARM targets.
  1561. #endif
  1562. #endif
  1563. /*!
  1564. \brief Draw anti-aliased ellipse with blending.
  1565. \param renderer The renderer to draw on.
  1566. \param x X coordinate of the center of the aa-ellipse.
  1567. \param y Y coordinate of the center of the aa-ellipse.
  1568. \param rx Horizontal radius in pixels of the aa-ellipse.
  1569. \param ry Vertical radius in pixels of the aa-ellipse.
  1570. \param color The color value of the aa-ellipse to draw (0xRRGGBBAA).
  1571. \returns Returns 0 on success, -1 on failure.
  1572. */
  1573. int aaellipseColor(SDL_Renderer * renderer, Sint16 x, Sint16 y, Sint16 rx, Sint16 ry, Uint32 color)
  1574. {
  1575. Uint8 *c = (Uint8 *)&color;
  1576. return aaellipseRGBA(renderer, x, y, rx, ry, c[0], c[1], c[2], c[3]);
  1577. }
  1578. /*!
  1579. \brief Draw anti-aliased ellipse with blending.
  1580. \param renderer The renderer to draw on.
  1581. \param x X coordinate of the center of the aa-ellipse.
  1582. \param y Y coordinate of the center of the aa-ellipse.
  1583. \param rx Horizontal radius in pixels of the aa-ellipse.
  1584. \param ry Vertical radius in pixels of the aa-ellipse.
  1585. \param r The red value of the aa-ellipse to draw.
  1586. \param g The green value of the aa-ellipse to draw.
  1587. \param b The blue value of the aa-ellipse to draw.
  1588. \param a The alpha value of the aa-ellipse to draw.
  1589. \returns Returns 0 on success, -1 on failure.
  1590. */
  1591. int aaellipseRGBA(SDL_Renderer * renderer, Sint16 x, Sint16 y, Sint16 rx, Sint16 ry, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  1592. {
  1593. int result;
  1594. int i;
  1595. int a2, b2, ds, dt, dxt, t, s, d;
  1596. Sint16 xp, yp, xs, ys, dyt, od, xx, yy, xc2, yc2;
  1597. float cp;
  1598. double sab;
  1599. Uint8 weight, iweight;
  1600. /*
  1601. * Sanity check radii
  1602. */
  1603. if ((rx < 0) || (ry < 0)) {
  1604. return (-1);
  1605. }
  1606. /*
  1607. * Special cases for rx=0 and/or ry=0: draw a hline/vline/pixel
  1608. */
  1609. if (rx == 0) {
  1610. if (ry == 0) {
  1611. return (pixelRGBA(renderer, x, y, r, g, b, a));
  1612. } else {
  1613. return (vlineRGBA(renderer, x, y - ry, y + ry, r, g, b, a));
  1614. }
  1615. } else {
  1616. if (ry == 0) {
  1617. return (hlineRGBA(renderer, x - rx, x + rx, y, r, g, b, a));
  1618. }
  1619. }
  1620. /* Variable setup */
  1621. a2 = rx * rx;
  1622. b2 = ry * ry;
  1623. ds = 2 * a2;
  1624. dt = 2 * b2;
  1625. xc2 = 2 * x;
  1626. yc2 = 2 * y;
  1627. sab = sqrt((double)(a2 + b2));
  1628. od = (Sint16)lrint(sab*0.01) + 1; /* introduce some overdraw */
  1629. dxt = (Sint16)lrint((double)a2 / sab) + od;
  1630. t = 0;
  1631. s = -2 * a2 * ry;
  1632. d = 0;
  1633. xp = x;
  1634. yp = y - ry;
  1635. /* Draw */
  1636. result = 0;
  1637. result |= SDL_SetRenderDrawBlendMode(renderer, (a == 255) ? SDL_BLENDMODE_NONE : SDL_BLENDMODE_BLEND);
  1638. /* "End points" */
  1639. result |= pixelRGBA(renderer, xp, yp, r, g, b, a);
  1640. result |= pixelRGBA(renderer, xc2 - xp, yp, r, g, b, a);
  1641. result |= pixelRGBA(renderer, xp, yc2 - yp, r, g, b, a);
  1642. result |= pixelRGBA(renderer, xc2 - xp, yc2 - yp, r, g, b, a);
  1643. for (i = 1; i <= dxt; i++) {
  1644. xp--;
  1645. d += t - b2;
  1646. if (d >= 0)
  1647. ys = yp - 1;
  1648. else if ((d - s - a2) > 0) {
  1649. if ((2 * d - s - a2) >= 0)
  1650. ys = yp + 1;
  1651. else {
  1652. ys = yp;
  1653. yp++;
  1654. d -= s + a2;
  1655. s += ds;
  1656. }
  1657. } else {
  1658. yp++;
  1659. ys = yp + 1;
  1660. d -= s + a2;
  1661. s += ds;
  1662. }
  1663. t -= dt;
  1664. /* Calculate alpha */
  1665. if (s != 0) {
  1666. cp = (float) abs(d) / (float) abs(s);
  1667. if (cp > 1.0) {
  1668. cp = 1.0;
  1669. }
  1670. } else {
  1671. cp = 1.0;
  1672. }
  1673. /* Calculate weights */
  1674. weight = (Uint8) (cp * 255);
  1675. iweight = 255 - weight;
  1676. /* Upper half */
  1677. xx = xc2 - xp;
  1678. result |= pixelRGBAWeight(renderer, xp, yp, r, g, b, a, iweight);
  1679. result |= pixelRGBAWeight(renderer, xx, yp, r, g, b, a, iweight);
  1680. result |= pixelRGBAWeight(renderer, xp, ys, r, g, b, a, weight);
  1681. result |= pixelRGBAWeight(renderer, xx, ys, r, g, b, a, weight);
  1682. /* Lower half */
  1683. yy = yc2 - yp;
  1684. result |= pixelRGBAWeight(renderer, xp, yy, r, g, b, a, iweight);
  1685. result |= pixelRGBAWeight(renderer, xx, yy, r, g, b, a, iweight);
  1686. yy = yc2 - ys;
  1687. result |= pixelRGBAWeight(renderer, xp, yy, r, g, b, a, weight);
  1688. result |= pixelRGBAWeight(renderer, xx, yy, r, g, b, a, weight);
  1689. }
  1690. /* Replaces original approximation code dyt = abs(yp - yc); */
  1691. dyt = (Sint16)lrint((double)b2 / sab ) + od;
  1692. for (i = 1; i <= dyt; i++) {
  1693. yp++;
  1694. d -= s + a2;
  1695. if (d <= 0)
  1696. xs = xp + 1;
  1697. else if ((d + t - b2) < 0) {
  1698. if ((2 * d + t - b2) <= 0)
  1699. xs = xp - 1;
  1700. else {
  1701. xs = xp;
  1702. xp--;
  1703. d += t - b2;
  1704. t -= dt;
  1705. }
  1706. } else {
  1707. xp--;
  1708. xs = xp - 1;
  1709. d += t - b2;
  1710. t -= dt;
  1711. }
  1712. s += ds;
  1713. /* Calculate alpha */
  1714. if (t != 0) {
  1715. cp = (float) abs(d) / (float) abs(t);
  1716. if (cp > 1.0) {
  1717. cp = 1.0;
  1718. }
  1719. } else {
  1720. cp = 1.0;
  1721. }
  1722. /* Calculate weight */
  1723. weight = (Uint8) (cp * 255);
  1724. iweight = 255 - weight;
  1725. /* Left half */
  1726. xx = xc2 - xp;
  1727. yy = yc2 - yp;
  1728. result |= pixelRGBAWeight(renderer, xp, yp, r, g, b, a, iweight);
  1729. result |= pixelRGBAWeight(renderer, xx, yp, r, g, b, a, iweight);
  1730. result |= pixelRGBAWeight(renderer, xp, yy, r, g, b, a, iweight);
  1731. result |= pixelRGBAWeight(renderer, xx, yy, r, g, b, a, iweight);
  1732. /* Right half */
  1733. xx = xc2 - xs;
  1734. result |= pixelRGBAWeight(renderer, xs, yp, r, g, b, a, weight);
  1735. result |= pixelRGBAWeight(renderer, xx, yp, r, g, b, a, weight);
  1736. result |= pixelRGBAWeight(renderer, xs, yy, r, g, b, a, weight);
  1737. result |= pixelRGBAWeight(renderer, xx, yy, r, g, b, a, weight);
  1738. }
  1739. return (result);
  1740. }
  1741. /* ---- Filled Ellipse */
  1742. /*!
  1743. \brief Draw filled ellipse with blending.
  1744. \param renderer The renderer to draw on.
  1745. \param x X coordinate of the center of the filled ellipse.
  1746. \param y Y coordinate of the center of the filled ellipse.
  1747. \param rx Horizontal radius in pixels of the filled ellipse.
  1748. \param ry Vertical radius in pixels of the filled ellipse.
  1749. \param color The color value of the filled ellipse to draw (0xRRGGBBAA).
  1750. \returns Returns 0 on success, -1 on failure.
  1751. */
  1752. int filledEllipseColor(SDL_Renderer * renderer, Sint16 x, Sint16 y, Sint16 rx, Sint16 ry, Uint32 color)
  1753. {
  1754. Uint8 *c = (Uint8 *)&color;
  1755. return _ellipseRGBA(renderer, x, y, rx, ry, c[0], c[1], c[2], c[3], 1);
  1756. }
  1757. /*!
  1758. \brief Draw filled ellipse with blending.
  1759. \param renderer The renderer to draw on.
  1760. \param x X coordinate of the center of the filled ellipse.
  1761. \param y Y coordinate of the center of the filled ellipse.
  1762. \param rx Horizontal radius in pixels of the filled ellipse.
  1763. \param ry Vertical radius in pixels of the filled ellipse.
  1764. \param r The red value of the filled ellipse to draw.
  1765. \param g The green value of the filled ellipse to draw.
  1766. \param b The blue value of the filled ellipse to draw.
  1767. \param a The alpha value of the filled ellipse to draw.
  1768. \returns Returns 0 on success, -1 on failure.
  1769. */
  1770. int filledEllipseRGBA(SDL_Renderer * renderer, Sint16 x, Sint16 y, Sint16 rx, Sint16 ry, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  1771. {
  1772. return _ellipseRGBA(renderer, x, y, rx, ry, r, g, b, a, 1);
  1773. }
  1774. /* ----- Pie */
  1775. /*!
  1776. \brief Internal float (low-speed) pie-calc implementation by drawing polygons.
  1777. Note: Determines vertex array and uses polygon or filledPolygon drawing routines to render.
  1778. \param renderer The renderer to draw on.
  1779. \param x X coordinate of the center of the pie.
  1780. \param y Y coordinate of the center of the pie.
  1781. \param rad Radius in pixels of the pie.
  1782. \param start Starting radius in degrees of the pie.
  1783. \param end Ending radius in degrees of the pie.
  1784. \param r The red value of the pie to draw.
  1785. \param g The green value of the pie to draw.
  1786. \param b The blue value of the pie to draw.
  1787. \param a The alpha value of the pie to draw.
  1788. \param filled Flag indicating if the pie should be filled (=1) or not (=0).
  1789. \returns Returns 0 on success, -1 on failure.
  1790. */
  1791. /* TODO: rewrite algorithm; pie is not always accurate */
  1792. int _pieRGBA(SDL_Renderer * renderer, Sint16 x, Sint16 y, Sint16 rad, Sint16 start, Sint16 end, Uint8 r, Uint8 g, Uint8 b, Uint8 a, Uint8 filled)
  1793. {
  1794. int result;
  1795. double angle, start_angle, end_angle;
  1796. double deltaAngle;
  1797. double dr;
  1798. int numpoints, i;
  1799. Sint16 *vx, *vy;
  1800. /*
  1801. * Sanity check radii
  1802. */
  1803. if (rad < 0) {
  1804. return (-1);
  1805. }
  1806. /*
  1807. * Fixup angles
  1808. */
  1809. start = start % 360;
  1810. end = end % 360;
  1811. /*
  1812. * Special case for rad=0 - draw a point
  1813. */
  1814. if (rad == 0) {
  1815. return (pixelRGBA(renderer, x, y, r, g, b, a));
  1816. }
  1817. /*
  1818. * Variable setup
  1819. */
  1820. dr = (double) rad;
  1821. deltaAngle = 3.0 / dr;
  1822. start_angle = (double) start *(2.0 * M_PI / 360.0);
  1823. end_angle = (double) end *(2.0 * M_PI / 360.0);
  1824. if (start > end) {
  1825. end_angle += (2.0 * M_PI);
  1826. }
  1827. /* We will always have at least 2 points */
  1828. numpoints = 2;
  1829. /* Count points (rather than calculating it) */
  1830. angle = start_angle;
  1831. while (angle < end_angle) {
  1832. angle += deltaAngle;
  1833. numpoints++;
  1834. }
  1835. /* Allocate combined vertex array */
  1836. vx = vy = (Sint16 *) malloc(2 * sizeof(Uint16) * numpoints);
  1837. if (vx == NULL) {
  1838. return (-1);
  1839. }
  1840. /* Update point to start of vy */
  1841. vy += numpoints;
  1842. /* Center */
  1843. vx[0] = x;
  1844. vy[0] = y;
  1845. /* First vertex */
  1846. angle = start_angle;
  1847. vx[1] = x + (int) (dr * cos(angle));
  1848. vy[1] = y + (int) (dr * sin(angle));
  1849. if (numpoints<3)
  1850. {
  1851. result = lineRGBA(renderer, vx[0], vy[0], vx[1], vy[1], r, g, b, a);
  1852. }
  1853. else
  1854. {
  1855. /* Calculate other vertices */
  1856. i = 2;
  1857. angle = start_angle;
  1858. while (angle < end_angle) {
  1859. angle += deltaAngle;
  1860. if (angle>end_angle)
  1861. {
  1862. angle = end_angle;
  1863. }
  1864. vx[i] = x + (int) (dr * cos(angle));
  1865. vy[i] = y + (int) (dr * sin(angle));
  1866. i++;
  1867. }
  1868. /* Draw */
  1869. if (filled) {
  1870. result = filledPolygonRGBA(renderer, vx, vy, numpoints, r, g, b, a);
  1871. } else {
  1872. result = polygonRGBA(renderer, vx, vy, numpoints, r, g, b, a);
  1873. }
  1874. }
  1875. /* Free combined vertex array */
  1876. free(vx);
  1877. return (result);
  1878. }
  1879. /*!
  1880. \brief Draw pie (outline) with alpha blending.
  1881. \param renderer The renderer to draw on.
  1882. \param x X coordinate of the center of the pie.
  1883. \param y Y coordinate of the center of the pie.
  1884. \param rad Radius in pixels of the pie.
  1885. \param start Starting radius in degrees of the pie.
  1886. \param end Ending radius in degrees of the pie.
  1887. \param color The color value of the pie to draw (0xRRGGBBAA).
  1888. \returns Returns 0 on success, -1 on failure.
  1889. */
  1890. int pieColor(SDL_Renderer * renderer, Sint16 x, Sint16 y, Sint16 rad,
  1891. Sint16 start, Sint16 end, Uint32 color)
  1892. {
  1893. Uint8 *c = (Uint8 *)&color;
  1894. return _pieRGBA(renderer, x, y, rad, start, end, c[0], c[1], c[2], c[3], 0);
  1895. }
  1896. /*!
  1897. \brief Draw pie (outline) with alpha blending.
  1898. \param renderer The renderer to draw on.
  1899. \param x X coordinate of the center of the pie.
  1900. \param y Y coordinate of the center of the pie.
  1901. \param rad Radius in pixels of the pie.
  1902. \param start Starting radius in degrees of the pie.
  1903. \param end Ending radius in degrees of the pie.
  1904. \param r The red value of the pie to draw.
  1905. \param g The green value of the pie to draw.
  1906. \param b The blue value of the pie to draw.
  1907. \param a The alpha value of the pie to draw.
  1908. \returns Returns 0 on success, -1 on failure.
  1909. */
  1910. int pieRGBA(SDL_Renderer * renderer, Sint16 x, Sint16 y, Sint16 rad,
  1911. Sint16 start, Sint16 end, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  1912. {
  1913. return _pieRGBA(renderer, x, y, rad, start, end, r, g, b, a, 0);
  1914. }
  1915. /*!
  1916. \brief Draw filled pie with alpha blending.
  1917. \param renderer The renderer to draw on.
  1918. \param x X coordinate of the center of the filled pie.
  1919. \param y Y coordinate of the center of the filled pie.
  1920. \param rad Radius in pixels of the filled pie.
  1921. \param start Starting radius in degrees of the filled pie.
  1922. \param end Ending radius in degrees of the filled pie.
  1923. \param color The color value of the filled pie to draw (0xRRGGBBAA).
  1924. \returns Returns 0 on success, -1 on failure.
  1925. */
  1926. int filledPieColor(SDL_Renderer * renderer, Sint16 x, Sint16 y, Sint16 rad, Sint16 start, Sint16 end, Uint32 color)
  1927. {
  1928. Uint8 *c = (Uint8 *)&color;
  1929. return _pieRGBA(renderer, x, y, rad, start, end, c[0], c[1], c[2], c[3], 1);
  1930. }
  1931. /*!
  1932. \brief Draw filled pie with alpha blending.
  1933. \param renderer The renderer to draw on.
  1934. \param x X coordinate of the center of the filled pie.
  1935. \param y Y coordinate of the center of the filled pie.
  1936. \param rad Radius in pixels of the filled pie.
  1937. \param start Starting radius in degrees of the filled pie.
  1938. \param end Ending radius in degrees of the filled pie.
  1939. \param r The red value of the filled pie to draw.
  1940. \param g The green value of the filled pie to draw.
  1941. \param b The blue value of the filled pie to draw.
  1942. \param a The alpha value of the filled pie to draw.
  1943. \returns Returns 0 on success, -1 on failure.
  1944. */
  1945. int filledPieRGBA(SDL_Renderer * renderer, Sint16 x, Sint16 y, Sint16 rad,
  1946. Sint16 start, Sint16 end, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  1947. {
  1948. return _pieRGBA(renderer, x, y, rad, start, end, r, g, b, a, 1);
  1949. }
  1950. /* ------ Trigon */
  1951. /*!
  1952. \brief Draw trigon (triangle outline) with alpha blending.
  1953. Note: Creates vertex array and uses polygon routine to render.
  1954. \param renderer The renderer to draw on.
  1955. \param x1 X coordinate of the first point of the trigon.
  1956. \param y1 Y coordinate of the first point of the trigon.
  1957. \param x2 X coordinate of the second point of the trigon.
  1958. \param y2 Y coordinate of the second point of the trigon.
  1959. \param x3 X coordinate of the third point of the trigon.
  1960. \param y3 Y coordinate of the third point of the trigon.
  1961. \param color The color value of the trigon to draw (0xRRGGBBAA).
  1962. \returns Returns 0 on success, -1 on failure.
  1963. */
  1964. int trigonColor(SDL_Renderer * renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3, Uint32 color)
  1965. {
  1966. Sint16 vx[3];
  1967. Sint16 vy[3];
  1968. vx[0]=x1;
  1969. vx[1]=x2;
  1970. vx[2]=x3;
  1971. vy[0]=y1;
  1972. vy[1]=y2;
  1973. vy[2]=y3;
  1974. return(polygonColor(renderer,vx,vy,3,color));
  1975. }
  1976. /*!
  1977. \brief Draw trigon (triangle outline) with alpha blending.
  1978. \param renderer The renderer to draw on.
  1979. \param x1 X coordinate of the first point of the trigon.
  1980. \param y1 Y coordinate of the first point of the trigon.
  1981. \param x2 X coordinate of the second point of the trigon.
  1982. \param y2 Y coordinate of the second point of the trigon.
  1983. \param x3 X coordinate of the third point of the trigon.
  1984. \param y3 Y coordinate of the third point of the trigon.
  1985. \param r The red value of the trigon to draw.
  1986. \param g The green value of the trigon to draw.
  1987. \param b The blue value of the trigon to draw.
  1988. \param a The alpha value of the trigon to draw.
  1989. \returns Returns 0 on success, -1 on failure.
  1990. */
  1991. int trigonRGBA(SDL_Renderer * renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3,
  1992. Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  1993. {
  1994. Sint16 vx[3];
  1995. Sint16 vy[3];
  1996. vx[0]=x1;
  1997. vx[1]=x2;
  1998. vx[2]=x3;
  1999. vy[0]=y1;
  2000. vy[1]=y2;
  2001. vy[2]=y3;
  2002. return(polygonRGBA(renderer,vx,vy,3,r,g,b,a));
  2003. }
  2004. /* ------ AA-Trigon */
  2005. /*!
  2006. \brief Draw anti-aliased trigon (triangle outline) with alpha blending.
  2007. Note: Creates vertex array and uses aapolygon routine to render.
  2008. \param renderer The renderer to draw on.
  2009. \param x1 X coordinate of the first point of the aa-trigon.
  2010. \param y1 Y coordinate of the first point of the aa-trigon.
  2011. \param x2 X coordinate of the second point of the aa-trigon.
  2012. \param y2 Y coordinate of the second point of the aa-trigon.
  2013. \param x3 X coordinate of the third point of the aa-trigon.
  2014. \param y3 Y coordinate of the third point of the aa-trigon.
  2015. \param color The color value of the aa-trigon to draw (0xRRGGBBAA).
  2016. \returns Returns 0 on success, -1 on failure.
  2017. */
  2018. int aatrigonColor(SDL_Renderer * renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3, Uint32 color)
  2019. {
  2020. Sint16 vx[3];
  2021. Sint16 vy[3];
  2022. vx[0]=x1;
  2023. vx[1]=x2;
  2024. vx[2]=x3;
  2025. vy[0]=y1;
  2026. vy[1]=y2;
  2027. vy[2]=y3;
  2028. return(aapolygonColor(renderer,vx,vy,3,color));
  2029. }
  2030. /*!
  2031. \brief Draw anti-aliased trigon (triangle outline) with alpha blending.
  2032. \param renderer The renderer to draw on.
  2033. \param x1 X coordinate of the first point of the aa-trigon.
  2034. \param y1 Y coordinate of the first point of the aa-trigon.
  2035. \param x2 X coordinate of the second point of the aa-trigon.
  2036. \param y2 Y coordinate of the second point of the aa-trigon.
  2037. \param x3 X coordinate of the third point of the aa-trigon.
  2038. \param y3 Y coordinate of the third point of the aa-trigon.
  2039. \param r The red value of the aa-trigon to draw.
  2040. \param g The green value of the aa-trigon to draw.
  2041. \param b The blue value of the aa-trigon to draw.
  2042. \param a The alpha value of the aa-trigon to draw.
  2043. \returns Returns 0 on success, -1 on failure.
  2044. */
  2045. int aatrigonRGBA(SDL_Renderer * renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3,
  2046. Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  2047. {
  2048. Sint16 vx[3];
  2049. Sint16 vy[3];
  2050. vx[0]=x1;
  2051. vx[1]=x2;
  2052. vx[2]=x3;
  2053. vy[0]=y1;
  2054. vy[1]=y2;
  2055. vy[2]=y3;
  2056. return(aapolygonRGBA(renderer,vx,vy,3,r,g,b,a));
  2057. }
  2058. /* ------ Filled Trigon */
  2059. /*!
  2060. \brief Draw filled trigon (triangle) with alpha blending.
  2061. Note: Creates vertex array and uses aapolygon routine to render.
  2062. \param renderer The renderer to draw on.
  2063. \param x1 X coordinate of the first point of the filled trigon.
  2064. \param y1 Y coordinate of the first point of the filled trigon.
  2065. \param x2 X coordinate of the second point of the filled trigon.
  2066. \param y2 Y coordinate of the second point of the filled trigon.
  2067. \param x3 X coordinate of the third point of the filled trigon.
  2068. \param y3 Y coordinate of the third point of the filled trigon.
  2069. \param color The color value of the filled trigon to draw (0xRRGGBBAA).
  2070. \returns Returns 0 on success, -1 on failure.
  2071. */
  2072. int filledTrigonColor(SDL_Renderer * renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3, Uint32 color)
  2073. {
  2074. Sint16 vx[3];
  2075. Sint16 vy[3];
  2076. vx[0]=x1;
  2077. vx[1]=x2;
  2078. vx[2]=x3;
  2079. vy[0]=y1;
  2080. vy[1]=y2;
  2081. vy[2]=y3;
  2082. return(filledPolygonColor(renderer,vx,vy,3,color));
  2083. }
  2084. /*!
  2085. \brief Draw filled trigon (triangle) with alpha blending.
  2086. Note: Creates vertex array and uses aapolygon routine to render.
  2087. \param renderer The renderer to draw on.
  2088. \param x1 X coordinate of the first point of the filled trigon.
  2089. \param y1 Y coordinate of the first point of the filled trigon.
  2090. \param x2 X coordinate of the second point of the filled trigon.
  2091. \param y2 Y coordinate of the second point of the filled trigon.
  2092. \param x3 X coordinate of the third point of the filled trigon.
  2093. \param y3 Y coordinate of the third point of the filled trigon.
  2094. \param r The red value of the filled trigon to draw.
  2095. \param g The green value of the filled trigon to draw.
  2096. \param b The blue value of the filled trigon to draw.
  2097. \param a The alpha value of the filled trigon to draw.
  2098. \returns Returns 0 on success, -1 on failure.
  2099. */
  2100. int filledTrigonRGBA(SDL_Renderer * renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3,
  2101. Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  2102. {
  2103. Sint16 vx[3];
  2104. Sint16 vy[3];
  2105. vx[0]=x1;
  2106. vx[1]=x2;
  2107. vx[2]=x3;
  2108. vy[0]=y1;
  2109. vy[1]=y2;
  2110. vy[2]=y3;
  2111. return(filledPolygonRGBA(renderer,vx,vy,3,r,g,b,a));
  2112. }
  2113. /* ---- Polygon */
  2114. /*!
  2115. \brief Draw polygon with alpha blending.
  2116. \param renderer The renderer to draw on.
  2117. \param vx Vertex array containing X coordinates of the points of the polygon.
  2118. \param vy Vertex array containing Y coordinates of the points of the polygon.
  2119. \param n Number of points in the vertex array. Minimum number is 3.
  2120. \param color The color value of the polygon to draw (0xRRGGBBAA).
  2121. \returns Returns 0 on success, -1 on failure.
  2122. */
  2123. int polygonColor(SDL_Renderer * renderer, const Sint16 * vx, const Sint16 * vy, int n, Uint32 color)
  2124. {
  2125. Uint8 *c = (Uint8 *)&color;
  2126. return polygonRGBA(renderer, vx, vy, n, c[0], c[1], c[2], c[3]);
  2127. }
  2128. /*!
  2129. \brief Draw polygon with the currently set color and blend mode.
  2130. \param renderer The renderer to draw on.
  2131. \param vx Vertex array containing X coordinates of the points of the polygon.
  2132. \param vy Vertex array containing Y coordinates of the points of the polygon.
  2133. \param n Number of points in the vertex array. Minimum number is 3.
  2134. \returns Returns 0 on success, -1 on failure.
  2135. */
  2136. int polygon(SDL_Renderer * renderer, const Sint16 * vx, const Sint16 * vy, int n)
  2137. {
  2138. /*
  2139. * Draw
  2140. */
  2141. int result = 0;
  2142. int i, nn;
  2143. SDL_Point* points;
  2144. /*
  2145. * Vertex array NULL check
  2146. */
  2147. if (vx == NULL) {
  2148. return (-1);
  2149. }
  2150. if (vy == NULL) {
  2151. return (-1);
  2152. }
  2153. /*
  2154. * Sanity check
  2155. */
  2156. if (n < 3) {
  2157. return (-1);
  2158. }
  2159. /*
  2160. * Create array of points
  2161. */
  2162. nn = n + 1;
  2163. points = (SDL_Point*)malloc(sizeof(SDL_Point) * nn);
  2164. if (points == NULL)
  2165. {
  2166. return -1;
  2167. }
  2168. for (i=0; i<n; i++)
  2169. {
  2170. points[i].x = vx[i];
  2171. points[i].y = vy[i];
  2172. }
  2173. points[n].x = vx[0];
  2174. points[n].y = vy[0];
  2175. /*
  2176. * Draw
  2177. */
  2178. result |= SDL_RenderDrawLines(renderer, points, nn);
  2179. free(points);
  2180. return (result);
  2181. }
  2182. /*!
  2183. \brief Draw polygon with alpha blending.
  2184. \param renderer The renderer to draw on.
  2185. \param vx Vertex array containing X coordinates of the points of the polygon.
  2186. \param vy Vertex array containing Y coordinates of the points of the polygon.
  2187. \param n Number of points in the vertex array. Minimum number is 3.
  2188. \param r The red value of the polygon to draw.
  2189. \param g The green value of the polygon to draw.
  2190. \param b The blue value of the polygon to draw.
  2191. \param a The alpha value of the polygon to draw.
  2192. \returns Returns 0 on success, -1 on failure.
  2193. */
  2194. int polygonRGBA(SDL_Renderer * renderer, const Sint16 * vx, const Sint16 * vy, int n, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  2195. {
  2196. /*
  2197. * Draw
  2198. */
  2199. int result;
  2200. const Sint16 *x1, *y1, *x2, *y2;
  2201. /*
  2202. * Vertex array NULL check
  2203. */
  2204. if (vx == NULL) {
  2205. return (-1);
  2206. }
  2207. if (vy == NULL) {
  2208. return (-1);
  2209. }
  2210. /*
  2211. * Sanity check
  2212. */
  2213. if (n < 3) {
  2214. return (-1);
  2215. }
  2216. /*
  2217. * Pointer setup
  2218. */
  2219. x1 = x2 = vx;
  2220. y1 = y2 = vy;
  2221. x2++;
  2222. y2++;
  2223. /*
  2224. * Set color
  2225. */
  2226. result = 0;
  2227. result |= SDL_SetRenderDrawBlendMode(renderer, (a == 255) ? SDL_BLENDMODE_NONE : SDL_BLENDMODE_BLEND);
  2228. result |= SDL_SetRenderDrawColor(renderer, r, g, b, a);
  2229. /*
  2230. * Draw
  2231. */
  2232. result |= polygon(renderer, vx, vy, n);
  2233. return (result);
  2234. }
  2235. /* ---- AA-Polygon */
  2236. /*!
  2237. \brief Draw anti-aliased polygon with alpha blending.
  2238. \param renderer The renderer to draw on.
  2239. \param vx Vertex array containing X coordinates of the points of the aa-polygon.
  2240. \param vy Vertex array containing Y coordinates of the points of the aa-polygon.
  2241. \param n Number of points in the vertex array. Minimum number is 3.
  2242. \param color The color value of the aa-polygon to draw (0xRRGGBBAA).
  2243. \returns Returns 0 on success, -1 on failure.
  2244. */
  2245. int aapolygonColor(SDL_Renderer * renderer, const Sint16 * vx, const Sint16 * vy, int n, Uint32 color)
  2246. {
  2247. Uint8 *c = (Uint8 *)&color;
  2248. return aapolygonRGBA(renderer, vx, vy, n, c[0], c[1], c[2], c[3]);
  2249. }
  2250. /*!
  2251. \brief Draw anti-aliased polygon with alpha blending.
  2252. \param renderer The renderer to draw on.
  2253. \param vx Vertex array containing X coordinates of the points of the aa-polygon.
  2254. \param vy Vertex array containing Y coordinates of the points of the aa-polygon.
  2255. \param n Number of points in the vertex array. Minimum number is 3.
  2256. \param r The red value of the aa-polygon to draw.
  2257. \param g The green value of the aa-polygon to draw.
  2258. \param b The blue value of the aa-polygon to draw.
  2259. \param a The alpha value of the aa-polygon to draw.
  2260. \returns Returns 0 on success, -1 on failure.
  2261. */
  2262. int aapolygonRGBA(SDL_Renderer * renderer, const Sint16 * vx, const Sint16 * vy, int n, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  2263. {
  2264. int result;
  2265. int i;
  2266. const Sint16 *x1, *y1, *x2, *y2;
  2267. /*
  2268. * Vertex array NULL check
  2269. */
  2270. if (vx == NULL) {
  2271. return (-1);
  2272. }
  2273. if (vy == NULL) {
  2274. return (-1);
  2275. }
  2276. /*
  2277. * Sanity check
  2278. */
  2279. if (n < 3) {
  2280. return (-1);
  2281. }
  2282. /*
  2283. * Pointer setup
  2284. */
  2285. x1 = x2 = vx;
  2286. y1 = y2 = vy;
  2287. x2++;
  2288. y2++;
  2289. /*
  2290. * Draw
  2291. */
  2292. result = 0;
  2293. for (i = 1; i < n; i++) {
  2294. result |= _aalineRGBA(renderer, *x1, *y1, *x2, *y2, r, g, b, a, 0);
  2295. x1 = x2;
  2296. y1 = y2;
  2297. x2++;
  2298. y2++;
  2299. }
  2300. result |= _aalineRGBA(renderer, *x1, *y1, *vx, *vy, r, g, b, a, 0);
  2301. return (result);
  2302. }
  2303. /* ---- Filled Polygon */
  2304. /*!
  2305. \brief Internal helper qsort callback functions used in filled polygon drawing.
  2306. \param a The surface to draw on.
  2307. \param b Vertex array containing X coordinates of the points of the polygon.
  2308. \returns Returns 0 if a==b, a negative number if a<b or a positive number if a>b.
  2309. */
  2310. int _gfxPrimitivesCompareInt(const void *a, const void *b)
  2311. {
  2312. return (*(const int *) a) - (*(const int *) b);
  2313. }
  2314. /*!
  2315. \brief Global vertex array to use if optional parameters are not given in filledPolygonMT calls.
  2316. Note: Used for non-multithreaded (default) operation of filledPolygonMT.
  2317. */
  2318. static int *gfxPrimitivesPolyIntsGlobal = NULL;
  2319. /*!
  2320. \brief Flag indicating if global vertex array was already allocated.
  2321. Note: Used for non-multithreaded (default) operation of filledPolygonMT.
  2322. */
  2323. static int gfxPrimitivesPolyAllocatedGlobal = 0;
  2324. /*!
  2325. \brief Draw filled polygon with alpha blending (multi-threaded capable).
  2326. Note: The last two parameters are optional; but are required for multithreaded operation.
  2327. \param renderer The renderer to draw on.
  2328. \param vx Vertex array containing X coordinates of the points of the filled polygon.
  2329. \param vy Vertex array containing Y coordinates of the points of the filled polygon.
  2330. \param n Number of points in the vertex array. Minimum number is 3.
  2331. \param r The red value of the filled polygon to draw.
  2332. \param g The green value of the filled polygon to draw.
  2333. \param b The blue value of the filled polygon to draw.
  2334. \param a The alpha value of the filled polygon to draw.
  2335. \param polyInts Preallocated, temporary vertex array used for sorting vertices. Required for multithreaded operation; set to NULL otherwise.
  2336. \param polyAllocated Flag indicating if temporary vertex array was allocated. Required for multithreaded operation; set to NULL otherwise.
  2337. \returns Returns 0 on success, -1 on failure.
  2338. */
  2339. int filledPolygonRGBAMT(SDL_Renderer * renderer, const Sint16 * vx, const Sint16 * vy, int n, Uint8 r, Uint8 g, Uint8 b, Uint8 a, int **polyInts, int *polyAllocated)
  2340. {
  2341. int result;
  2342. int i;
  2343. int y, xa, xb;
  2344. int miny, maxy;
  2345. int x1, y1;
  2346. int x2, y2;
  2347. int ind1, ind2;
  2348. int ints;
  2349. int *gfxPrimitivesPolyInts = NULL;
  2350. int *gfxPrimitivesPolyIntsNew = NULL;
  2351. int gfxPrimitivesPolyAllocated = 0;
  2352. /*
  2353. * Vertex array NULL check
  2354. */
  2355. if (vx == NULL) {
  2356. return (-1);
  2357. }
  2358. if (vy == NULL) {
  2359. return (-1);
  2360. }
  2361. /*
  2362. * Sanity check number of edges
  2363. */
  2364. if (n < 3) {
  2365. return -1;
  2366. }
  2367. /*
  2368. * Map polygon cache
  2369. */
  2370. if ((polyInts==NULL) || (polyAllocated==NULL)) {
  2371. /* Use global cache */
  2372. gfxPrimitivesPolyInts = gfxPrimitivesPolyIntsGlobal;
  2373. gfxPrimitivesPolyAllocated = gfxPrimitivesPolyAllocatedGlobal;
  2374. } else {
  2375. /* Use local cache */
  2376. gfxPrimitivesPolyInts = *polyInts;
  2377. gfxPrimitivesPolyAllocated = *polyAllocated;
  2378. }
  2379. /*
  2380. * Allocate temp array, only grow array
  2381. */
  2382. if (!gfxPrimitivesPolyAllocated) {
  2383. gfxPrimitivesPolyInts = (int *) malloc(sizeof(int) * n);
  2384. gfxPrimitivesPolyAllocated = n;
  2385. } else {
  2386. if (gfxPrimitivesPolyAllocated < n) {
  2387. gfxPrimitivesPolyIntsNew = (int *) realloc(gfxPrimitivesPolyInts, sizeof(int) * n);
  2388. if (!gfxPrimitivesPolyIntsNew) {
  2389. if (!gfxPrimitivesPolyInts) {
  2390. free(gfxPrimitivesPolyInts);
  2391. gfxPrimitivesPolyInts = NULL;
  2392. }
  2393. gfxPrimitivesPolyAllocated = 0;
  2394. } else {
  2395. gfxPrimitivesPolyInts = gfxPrimitivesPolyIntsNew;
  2396. gfxPrimitivesPolyAllocated = n;
  2397. }
  2398. }
  2399. }
  2400. /*
  2401. * Check temp array
  2402. */
  2403. if (gfxPrimitivesPolyInts==NULL) {
  2404. gfxPrimitivesPolyAllocated = 0;
  2405. }
  2406. /*
  2407. * Update cache variables
  2408. */
  2409. if ((polyInts==NULL) || (polyAllocated==NULL)) {
  2410. gfxPrimitivesPolyIntsGlobal = gfxPrimitivesPolyInts;
  2411. gfxPrimitivesPolyAllocatedGlobal = gfxPrimitivesPolyAllocated;
  2412. } else {
  2413. *polyInts = gfxPrimitivesPolyInts;
  2414. *polyAllocated = gfxPrimitivesPolyAllocated;
  2415. }
  2416. /*
  2417. * Check temp array again
  2418. */
  2419. if (gfxPrimitivesPolyInts==NULL) {
  2420. return(-1);
  2421. }
  2422. /*
  2423. * Determine Y maxima
  2424. */
  2425. miny = vy[0];
  2426. maxy = vy[0];
  2427. for (i = 1; (i < n); i++) {
  2428. if (vy[i] < miny) {
  2429. miny = vy[i];
  2430. } else if (vy[i] > maxy) {
  2431. maxy = vy[i];
  2432. }
  2433. }
  2434. /*
  2435. * Draw, scanning y
  2436. */
  2437. result = 0;
  2438. for (y = miny; (y <= maxy); y++) {
  2439. ints = 0;
  2440. for (i = 0; (i < n); i++) {
  2441. if (!i) {
  2442. ind1 = n - 1;
  2443. ind2 = 0;
  2444. } else {
  2445. ind1 = i - 1;
  2446. ind2 = i;
  2447. }
  2448. y1 = vy[ind1];
  2449. y2 = vy[ind2];
  2450. if (y1 < y2) {
  2451. x1 = vx[ind1];
  2452. x2 = vx[ind2];
  2453. } else if (y1 > y2) {
  2454. y2 = vy[ind1];
  2455. y1 = vy[ind2];
  2456. x2 = vx[ind1];
  2457. x1 = vx[ind2];
  2458. } else {
  2459. continue;
  2460. }
  2461. if ( ((y >= y1) && (y < y2)) || ((y == maxy) && (y > y1) && (y <= y2)) ) {
  2462. gfxPrimitivesPolyInts[ints++] = ((65536 * (y - y1)) / (y2 - y1)) * (x2 - x1) + (65536 * x1);
  2463. }
  2464. }
  2465. qsort(gfxPrimitivesPolyInts, ints, sizeof(int), _gfxPrimitivesCompareInt);
  2466. /*
  2467. * Set color
  2468. */
  2469. result = 0;
  2470. result |= SDL_SetRenderDrawBlendMode(renderer, (a == 255) ? SDL_BLENDMODE_NONE : SDL_BLENDMODE_BLEND);
  2471. result |= SDL_SetRenderDrawColor(renderer, r, g, b, a);
  2472. for (i = 0; (i < ints); i += 2) {
  2473. xa = gfxPrimitivesPolyInts[i] + 1;
  2474. xa = (xa >> 16) + ((xa & 32768) >> 15);
  2475. xb = gfxPrimitivesPolyInts[i+1] - 1;
  2476. xb = (xb >> 16) + ((xb & 32768) >> 15);
  2477. result |= hline(renderer, xa, xb, y);
  2478. }
  2479. }
  2480. return (result);
  2481. }
  2482. /*!
  2483. \brief Draw filled polygon with alpha blending.
  2484. \param renderer The renderer to draw on.
  2485. \param vx Vertex array containing X coordinates of the points of the filled polygon.
  2486. \param vy Vertex array containing Y coordinates of the points of the filled polygon.
  2487. \param n Number of points in the vertex array. Minimum number is 3.
  2488. \param color The color value of the filled polygon to draw (0xRRGGBBAA).
  2489. \returns Returns 0 on success, -1 on failure.
  2490. */
  2491. int filledPolygonColor(SDL_Renderer * renderer, const Sint16 * vx, const Sint16 * vy, int n, Uint32 color)
  2492. {
  2493. Uint8 *c = (Uint8 *)&color;
  2494. return filledPolygonRGBAMT(renderer, vx, vy, n, c[0], c[1], c[2], c[3], NULL, NULL);
  2495. }
  2496. /*!
  2497. \brief Draw filled polygon with alpha blending.
  2498. \param renderer The renderer to draw on.
  2499. \param vx Vertex array containing X coordinates of the points of the filled polygon.
  2500. \param vy Vertex array containing Y coordinates of the points of the filled polygon.
  2501. \param n Number of points in the vertex array. Minimum number is 3.
  2502. \param r The red value of the filled polygon to draw.
  2503. \param g The green value of the filled polygon to draw.
  2504. \param b The blue value of the filed polygon to draw.
  2505. \param a The alpha value of the filled polygon to draw.
  2506. \returns Returns 0 on success, -1 on failure.
  2507. */
  2508. int filledPolygonRGBA(SDL_Renderer * renderer, const Sint16 * vx, const Sint16 * vy, int n, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  2509. {
  2510. return filledPolygonRGBAMT(renderer, vx, vy, n, r, g, b, a, NULL, NULL);
  2511. }
  2512. /* ---- Textured Polygon */
  2513. /*!
  2514. \brief Internal function to draw a textured horizontal line.
  2515. \param renderer The renderer to draw on.
  2516. \param x1 X coordinate of the first point (i.e. left) of the line.
  2517. \param x2 X coordinate of the second point (i.e. right) of the line.
  2518. \param y Y coordinate of the points of the line.
  2519. \param texture The texture to retrieve color information from.
  2520. \param texture_w The width of the texture.
  2521. \param texture_h The height of the texture.
  2522. \param texture_dx The X offset for the texture lookup.
  2523. \param texture_dy The Y offset for the textured lookup.
  2524. \returns Returns 0 on success, -1 on failure.
  2525. */
  2526. int _HLineTextured(SDL_Renderer *renderer, Sint16 x1, Sint16 x2, Sint16 y, SDL_Texture *texture, int texture_w, int texture_h, int texture_dx, int texture_dy)
  2527. {
  2528. Sint16 w;
  2529. Sint16 xtmp;
  2530. int result = 0;
  2531. int texture_x_walker;
  2532. int texture_y_start;
  2533. SDL_Rect source_rect,dst_rect;
  2534. int pixels_written,write_width;
  2535. /*
  2536. * Swap x1, x2 if required to ensure x1<=x2
  2537. */
  2538. if (x1 > x2) {
  2539. xtmp = x1;
  2540. x1 = x2;
  2541. x2 = xtmp;
  2542. }
  2543. /*
  2544. * Calculate width to draw
  2545. */
  2546. w = x2 - x1 + 1;
  2547. /*
  2548. * Determine where in the texture we start drawing
  2549. */
  2550. texture_x_walker = (x1 - texture_dx) % texture_w;
  2551. if (texture_x_walker < 0){
  2552. texture_x_walker = texture_w + texture_x_walker ;
  2553. }
  2554. texture_y_start = (y + texture_dy) % texture_h;
  2555. if (texture_y_start < 0){
  2556. texture_y_start = texture_h + texture_y_start;
  2557. }
  2558. /* setup the source rectangle; we are only drawing one horizontal line */
  2559. source_rect.y = texture_y_start;
  2560. source_rect.x = texture_x_walker;
  2561. source_rect.h = 1;
  2562. /* we will draw to the current y */
  2563. dst_rect.y = y;
  2564. dst_rect.h = 1;
  2565. /* if there are enough pixels left in the current row of the texture */
  2566. /* draw it all at once */
  2567. if (w <= texture_w -texture_x_walker){
  2568. source_rect.w = w;
  2569. source_rect.x = texture_x_walker;
  2570. dst_rect.x= x1;
  2571. dst_rect.w = source_rect.w;
  2572. result = (SDL_RenderCopy(renderer, texture, &source_rect, &dst_rect) == 0);
  2573. } else {
  2574. /* we need to draw multiple times */
  2575. /* draw the first segment */
  2576. pixels_written = texture_w - texture_x_walker;
  2577. source_rect.w = pixels_written;
  2578. source_rect.x = texture_x_walker;
  2579. dst_rect.x= x1;
  2580. dst_rect.w = source_rect.w;
  2581. result |= (SDL_RenderCopy(renderer, texture, &source_rect, &dst_rect) == 0);
  2582. write_width = texture_w;
  2583. /* now draw the rest */
  2584. /* set the source x to 0 */
  2585. source_rect.x = 0;
  2586. while (pixels_written < w){
  2587. if (write_width >= w - pixels_written) {
  2588. write_width = w - pixels_written;
  2589. }
  2590. source_rect.w = write_width;
  2591. dst_rect.x = x1 + pixels_written;
  2592. dst_rect.w = source_rect.w;
  2593. result |= (SDL_RenderCopy(renderer, texture, &source_rect, &dst_rect) == 0);
  2594. pixels_written += write_width;
  2595. }
  2596. }
  2597. return result;
  2598. }
  2599. /*!
  2600. \brief Draws a polygon filled with the given texture (Multi-Threading Capable).
  2601. \param renderer The renderer to draw on.
  2602. \param vx array of x vector components
  2603. \param vy array of x vector components
  2604. \param n the amount of vectors in the vx and vy array
  2605. \param texture the sdl surface to use to fill the polygon
  2606. \param texture_dx the offset of the texture relative to the screeen. If you move the polygon 10 pixels
  2607. to the left and want the texture to apear the same you need to increase the texture_dx value
  2608. \param texture_dy see texture_dx
  2609. \param polyInts Preallocated temp array storage for vertex sorting (used for multi-threaded operation)
  2610. \param polyAllocated Flag indicating oif the temp array was allocated (used for multi-threaded operation)
  2611. \returns Returns 0 on success, -1 on failure.
  2612. */
  2613. int texturedPolygonMT(SDL_Renderer *renderer, const Sint16 * vx, const Sint16 * vy, int n,
  2614. SDL_Surface * texture, int texture_dx, int texture_dy, int **polyInts, int *polyAllocated)
  2615. {
  2616. int result;
  2617. int i;
  2618. int y, xa, xb;
  2619. int minx,maxx,miny, maxy;
  2620. int x1, y1;
  2621. int x2, y2;
  2622. int ind1, ind2;
  2623. int ints;
  2624. int *gfxPrimitivesPolyInts = NULL;
  2625. int *gfxPrimitivesPolyIntsTemp = NULL;
  2626. int gfxPrimitivesPolyAllocated = 0;
  2627. SDL_Texture *textureAsTexture = NULL;
  2628. /*
  2629. * Sanity check number of edges
  2630. */
  2631. if (n < 3) {
  2632. return -1;
  2633. }
  2634. /*
  2635. * Map polygon cache
  2636. */
  2637. if ((polyInts==NULL) || (polyAllocated==NULL)) {
  2638. /* Use global cache */
  2639. gfxPrimitivesPolyInts = gfxPrimitivesPolyIntsGlobal;
  2640. gfxPrimitivesPolyAllocated = gfxPrimitivesPolyAllocatedGlobal;
  2641. } else {
  2642. /* Use local cache */
  2643. gfxPrimitivesPolyInts = *polyInts;
  2644. gfxPrimitivesPolyAllocated = *polyAllocated;
  2645. }
  2646. /*
  2647. * Allocate temp array, only grow array
  2648. */
  2649. if (!gfxPrimitivesPolyAllocated) {
  2650. gfxPrimitivesPolyInts = (int *) malloc(sizeof(int) * n);
  2651. gfxPrimitivesPolyAllocated = n;
  2652. } else {
  2653. if (gfxPrimitivesPolyAllocated < n) {
  2654. gfxPrimitivesPolyIntsTemp = (int *) realloc(gfxPrimitivesPolyInts, sizeof(int) * n);
  2655. if (gfxPrimitivesPolyIntsTemp == NULL) {
  2656. /* Realloc failed - keeps original memory block, but fails this operation */
  2657. return(-1);
  2658. }
  2659. gfxPrimitivesPolyInts = gfxPrimitivesPolyIntsTemp;
  2660. gfxPrimitivesPolyAllocated = n;
  2661. }
  2662. }
  2663. /*
  2664. * Check temp array
  2665. */
  2666. if (gfxPrimitivesPolyInts==NULL) {
  2667. gfxPrimitivesPolyAllocated = 0;
  2668. }
  2669. /*
  2670. * Update cache variables
  2671. */
  2672. if ((polyInts==NULL) || (polyAllocated==NULL)) {
  2673. gfxPrimitivesPolyIntsGlobal = gfxPrimitivesPolyInts;
  2674. gfxPrimitivesPolyAllocatedGlobal = gfxPrimitivesPolyAllocated;
  2675. } else {
  2676. *polyInts = gfxPrimitivesPolyInts;
  2677. *polyAllocated = gfxPrimitivesPolyAllocated;
  2678. }
  2679. /*
  2680. * Check temp array again
  2681. */
  2682. if (gfxPrimitivesPolyInts==NULL) {
  2683. return(-1);
  2684. }
  2685. /*
  2686. * Determine X,Y minima,maxima
  2687. */
  2688. miny = vy[0];
  2689. maxy = vy[0];
  2690. minx = vx[0];
  2691. maxx = vx[0];
  2692. for (i = 1; (i < n); i++) {
  2693. if (vy[i] < miny) {
  2694. miny = vy[i];
  2695. } else if (vy[i] > maxy) {
  2696. maxy = vy[i];
  2697. }
  2698. if (vx[i] < minx) {
  2699. minx = vx[i];
  2700. } else if (vx[i] > maxx) {
  2701. maxx = vx[i];
  2702. }
  2703. }
  2704. /* Create texture for drawing */
  2705. textureAsTexture = SDL_CreateTextureFromSurface(renderer, texture);
  2706. if (textureAsTexture == NULL)
  2707. {
  2708. return -1;
  2709. }
  2710. SDL_SetTextureBlendMode(textureAsTexture, SDL_BLENDMODE_BLEND);
  2711. /*
  2712. * Draw, scanning y
  2713. */
  2714. result = 0;
  2715. for (y = miny; (y <= maxy); y++) {
  2716. ints = 0;
  2717. for (i = 0; (i < n); i++) {
  2718. if (!i) {
  2719. ind1 = n - 1;
  2720. ind2 = 0;
  2721. } else {
  2722. ind1 = i - 1;
  2723. ind2 = i;
  2724. }
  2725. y1 = vy[ind1];
  2726. y2 = vy[ind2];
  2727. if (y1 < y2) {
  2728. x1 = vx[ind1];
  2729. x2 = vx[ind2];
  2730. } else if (y1 > y2) {
  2731. y2 = vy[ind1];
  2732. y1 = vy[ind2];
  2733. x2 = vx[ind1];
  2734. x1 = vx[ind2];
  2735. } else {
  2736. continue;
  2737. }
  2738. if ( ((y >= y1) && (y < y2)) || ((y == maxy) && (y > y1) && (y <= y2)) ) {
  2739. gfxPrimitivesPolyInts[ints++] = ((65536 * (y - y1)) / (y2 - y1)) * (x2 - x1) + (65536 * x1);
  2740. }
  2741. }
  2742. qsort(gfxPrimitivesPolyInts, ints, sizeof(int), _gfxPrimitivesCompareInt);
  2743. for (i = 0; (i < ints); i += 2) {
  2744. xa = gfxPrimitivesPolyInts[i] + 1;
  2745. xa = (xa >> 16) + ((xa & 32768) >> 15);
  2746. xb = gfxPrimitivesPolyInts[i+1] - 1;
  2747. xb = (xb >> 16) + ((xb & 32768) >> 15);
  2748. result |= _HLineTextured(renderer, xa, xb, y, textureAsTexture, texture->w, texture->h, texture_dx, texture_dy);
  2749. }
  2750. }
  2751. SDL_RenderPresent(renderer);
  2752. SDL_DestroyTexture(textureAsTexture);
  2753. return (result);
  2754. }
  2755. /*!
  2756. \brief Draws a polygon filled with the given texture.
  2757. This standard version is calling multithreaded versions with NULL cache parameters.
  2758. \param renderer The renderer to draw on.
  2759. \param vx array of x vector components
  2760. \param vy array of x vector components
  2761. \param n the amount of vectors in the vx and vy array
  2762. \param texture the sdl surface to use to fill the polygon
  2763. \param texture_dx the offset of the texture relative to the screeen. if you move the polygon 10 pixels
  2764. to the left and want the texture to apear the same you need to increase the texture_dx value
  2765. \param texture_dy see texture_dx
  2766. \returns Returns 0 on success, -1 on failure.
  2767. */
  2768. int texturedPolygon(SDL_Renderer *renderer, const Sint16 * vx, const Sint16 * vy, int n, SDL_Surface *texture, int texture_dx, int texture_dy)
  2769. {
  2770. /*
  2771. * Draw
  2772. */
  2773. return (texturedPolygonMT(renderer, vx, vy, n, texture, texture_dx, texture_dy, NULL, NULL));
  2774. }
  2775. /* ---- Character */
  2776. /*!
  2777. \brief Global cache for NxM pixel font textures created at runtime.
  2778. */
  2779. static SDL_Texture *gfxPrimitivesFont[256];
  2780. /*!
  2781. \brief Pointer to the current font data. Default is a 8x8 pixel internal font.
  2782. */
  2783. static const unsigned char *currentFontdata = gfxPrimitivesFontdata;
  2784. /*!
  2785. \brief Width of the current font. Default is 8.
  2786. */
  2787. static Uint32 charWidth = 8;
  2788. /*!
  2789. \brief Height of the current font. Default is 8.
  2790. */
  2791. static Uint32 charHeight = 8;
  2792. /*!
  2793. \brief Width for rendering. Autocalculated.
  2794. */
  2795. static Uint32 charWidthLocal = 8;
  2796. /*!
  2797. \brief Height for rendering. Autocalculated.
  2798. */
  2799. static Uint32 charHeightLocal = 8;
  2800. /*!
  2801. \brief Pitch of the current font in bytes. Default is 1.
  2802. */
  2803. static Uint32 charPitch = 1;
  2804. /*!
  2805. \brief Characters 90deg clockwise rotations. Default is 0. Max is 3.
  2806. */
  2807. static Uint32 charRotation = 0;
  2808. /*!
  2809. \brief Character data size in bytes of the current font. Default is 8.
  2810. */
  2811. static Uint32 charSize = 8;
  2812. /*!
  2813. \brief Sets or resets the current global font data.
  2814. The font data array is organized in follows:
  2815. [fontdata] = [character 0][character 1]...[character 255] where
  2816. [character n] = [byte 1 row 1][byte 2 row 1]...[byte {pitch} row 1][byte 1 row 2] ...[byte {pitch} row height] where
  2817. [byte n] = [bit 0]...[bit 7] where
  2818. [bit n] = [0 for transparent pixel|1 for colored pixel]
  2819. \param fontdata Pointer to array of font data. Set to NULL, to reset global font to the default 8x8 font.
  2820. \param cw Width of character in bytes. Ignored if fontdata==NULL.
  2821. \param ch Height of character in bytes. Ignored if fontdata==NULL.
  2822. */
  2823. void gfxPrimitivesSetFont(const void *fontdata, Uint32 cw, Uint32 ch)
  2824. {
  2825. int i;
  2826. if ((fontdata) && (cw) && (ch)) {
  2827. currentFontdata = (unsigned char *)fontdata;
  2828. charWidth = cw;
  2829. charHeight = ch;
  2830. } else {
  2831. currentFontdata = gfxPrimitivesFontdata;
  2832. charWidth = 8;
  2833. charHeight = 8;
  2834. }
  2835. charPitch = (charWidth+7)/8;
  2836. charSize = charPitch * charHeight;
  2837. /* Maybe flip width/height for rendering */
  2838. if ((charRotation==1) || (charRotation==3))
  2839. {
  2840. charWidthLocal = charHeight;
  2841. charHeightLocal = charWidth;
  2842. }
  2843. else
  2844. {
  2845. charWidthLocal = charWidth;
  2846. charHeightLocal = charHeight;
  2847. }
  2848. /* Clear character cache */
  2849. for (i = 0; i < 256; i++) {
  2850. if (gfxPrimitivesFont[i]) {
  2851. SDL_DestroyTexture(gfxPrimitivesFont[i]);
  2852. gfxPrimitivesFont[i] = NULL;
  2853. }
  2854. }
  2855. }
  2856. /*!
  2857. \brief Sets current global font character rotation steps.
  2858. Default is 0 (no rotation). 1 = 90deg clockwise. 2 = 180deg clockwise. 3 = 270deg clockwise.
  2859. Changing the rotation, will reset the character cache.
  2860. \param rotation Number of 90deg clockwise steps to rotate
  2861. */
  2862. void gfxPrimitivesSetFontRotation(Uint32 rotation)
  2863. {
  2864. int i;
  2865. rotation = rotation & 3;
  2866. if (charRotation != rotation)
  2867. {
  2868. /* Store rotation */
  2869. charRotation = rotation;
  2870. /* Maybe flip width/height for rendering */
  2871. if ((charRotation==1) || (charRotation==3))
  2872. {
  2873. charWidthLocal = charHeight;
  2874. charHeightLocal = charWidth;
  2875. }
  2876. else
  2877. {
  2878. charWidthLocal = charWidth;
  2879. charHeightLocal = charHeight;
  2880. }
  2881. /* Clear character cache */
  2882. for (i = 0; i < 256; i++) {
  2883. if (gfxPrimitivesFont[i]) {
  2884. SDL_DestroyTexture(gfxPrimitivesFont[i]);
  2885. gfxPrimitivesFont[i] = NULL;
  2886. }
  2887. }
  2888. }
  2889. }
  2890. /*!
  2891. \brief Draw a character of the currently set font.
  2892. \param renderer The Renderer to draw on.
  2893. \param x X (horizontal) coordinate of the upper left corner of the character.
  2894. \param y Y (vertical) coordinate of the upper left corner of the character.
  2895. \param c The character to draw.
  2896. \param r The red value of the character to draw.
  2897. \param g The green value of the character to draw.
  2898. \param b The blue value of the character to draw.
  2899. \param a The alpha value of the character to draw.
  2900. \returns Returns 0 on success, -1 on failure.
  2901. */
  2902. int characterRGBA(SDL_Renderer *renderer, Sint16 x, Sint16 y, char c, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  2903. {
  2904. SDL_Rect srect;
  2905. SDL_Rect drect;
  2906. int result;
  2907. Uint32 ix, iy;
  2908. const unsigned char *charpos;
  2909. Uint8 *curpos;
  2910. Uint8 patt, mask;
  2911. Uint8 *linepos;
  2912. Uint32 pitch;
  2913. SDL_Surface *character;
  2914. SDL_Surface *rotatedCharacter;
  2915. Uint32 ci;
  2916. /*
  2917. * Setup source rectangle
  2918. */
  2919. srect.x = 0;
  2920. srect.y = 0;
  2921. srect.w = charWidthLocal;
  2922. srect.h = charHeightLocal;
  2923. /*
  2924. * Setup destination rectangle
  2925. */
  2926. drect.x = x;
  2927. drect.y = y;
  2928. drect.w = charWidthLocal;
  2929. drect.h = charHeightLocal;
  2930. /* Character index in cache */
  2931. ci = (unsigned char) c;
  2932. /*
  2933. * Create new charWidth x charHeight bitmap surface if not already present.
  2934. * Might get rotated later.
  2935. */
  2936. if (gfxPrimitivesFont[ci] == NULL) {
  2937. /*
  2938. * Redraw character into surface
  2939. */
  2940. character = SDL_CreateRGBSurface(SDL_SWSURFACE,
  2941. charWidth, charHeight, 32,
  2942. 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
  2943. if (character == NULL) {
  2944. return (-1);
  2945. }
  2946. charpos = currentFontdata + ci * charSize;
  2947. linepos = (Uint8 *)character->pixels;
  2948. pitch = character->pitch;
  2949. /*
  2950. * Drawing loop
  2951. */
  2952. patt = 0;
  2953. for (iy = 0; iy < charHeight; iy++) {
  2954. mask = 0x00;
  2955. curpos = linepos;
  2956. for (ix = 0; ix < charWidth; ix++) {
  2957. if (!(mask >>= 1)) {
  2958. patt = *charpos++;
  2959. mask = 0x80;
  2960. }
  2961. if (patt & mask) {
  2962. *(Uint32 *)curpos = 0xffffffff;
  2963. } else {
  2964. *(Uint32 *)curpos = 0;
  2965. }
  2966. curpos += 4;
  2967. }
  2968. linepos += pitch;
  2969. }
  2970. /* Maybe rotate and replace cached image */
  2971. if (charRotation>0)
  2972. {
  2973. rotatedCharacter = rotateSurface90Degrees(character, charRotation);
  2974. SDL_FreeSurface(character);
  2975. character = rotatedCharacter;
  2976. }
  2977. /* Convert temp surface into texture */
  2978. gfxPrimitivesFont[ci] = SDL_CreateTextureFromSurface(renderer, character);
  2979. SDL_FreeSurface(character);
  2980. /*
  2981. * Check pointer
  2982. */
  2983. if (gfxPrimitivesFont[ci] == NULL) {
  2984. return (-1);
  2985. }
  2986. }
  2987. /*
  2988. * Set color
  2989. */
  2990. result = 0;
  2991. result |= SDL_SetTextureColorMod(gfxPrimitivesFont[ci], r, g, b);
  2992. result |= SDL_SetTextureAlphaMod(gfxPrimitivesFont[ci], a);
  2993. /*
  2994. * Draw texture onto destination
  2995. */
  2996. result |= SDL_RenderCopy(renderer, gfxPrimitivesFont[ci], &srect, &drect);
  2997. return (result);
  2998. }
  2999. /*!
  3000. \brief Draw a character of the currently set font.
  3001. \param renderer The renderer to draw on.
  3002. \param x X (horizontal) coordinate of the upper left corner of the character.
  3003. \param y Y (vertical) coordinate of the upper left corner of the character.
  3004. \param c The character to draw.
  3005. \param color The color value of the character to draw (0xRRGGBBAA).
  3006. \returns Returns 0 on success, -1 on failure.
  3007. */
  3008. int characterColor(SDL_Renderer * renderer, Sint16 x, Sint16 y, char c, Uint32 color)
  3009. {
  3010. Uint8 *co = (Uint8 *)&color;
  3011. return characterRGBA(renderer, x, y, c, co[0], co[1], co[2], co[3]);
  3012. }
  3013. /*!
  3014. \brief Draw a string in the currently set font.
  3015. The spacing between consequtive characters in the string is the fixed number of pixels
  3016. of the character width of the current global font.
  3017. \param renderer The renderer to draw on.
  3018. \param x X (horizontal) coordinate of the upper left corner of the string.
  3019. \param y Y (vertical) coordinate of the upper left corner of the string.
  3020. \param s The string to draw.
  3021. \param color The color value of the string to draw (0xRRGGBBAA).
  3022. \returns Returns 0 on success, -1 on failure.
  3023. */
  3024. int stringColor(SDL_Renderer * renderer, Sint16 x, Sint16 y, const char *s, Uint32 color)
  3025. {
  3026. Uint8 *c = (Uint8 *)&color;
  3027. return stringRGBA(renderer, x, y, s, c[0], c[1], c[2], c[3]);
  3028. }
  3029. /*!
  3030. \brief Draw a string in the currently set font.
  3031. \param renderer The renderer to draw on.
  3032. \param x X (horizontal) coordinate of the upper left corner of the string.
  3033. \param y Y (vertical) coordinate of the upper left corner of the string.
  3034. \param s The string to draw.
  3035. \param r The red value of the string to draw.
  3036. \param g The green value of the string to draw.
  3037. \param b The blue value of the string to draw.
  3038. \param a The alpha value of the string to draw.
  3039. \returns Returns 0 on success, -1 on failure.
  3040. */
  3041. int stringRGBA(SDL_Renderer * renderer, Sint16 x, Sint16 y, const char *s, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  3042. {
  3043. int result = 0;
  3044. Sint16 curx = x;
  3045. Sint16 cury = y;
  3046. const char *curchar = s;
  3047. while (*curchar && !result) {
  3048. result |= characterRGBA(renderer, curx, cury, *curchar, r, g, b, a);
  3049. switch (charRotation)
  3050. {
  3051. case 0:
  3052. curx += charWidthLocal;
  3053. break;
  3054. case 2:
  3055. curx -= charWidthLocal;
  3056. break;
  3057. case 1:
  3058. cury += charHeightLocal;
  3059. break;
  3060. case 3:
  3061. cury -= charHeightLocal;
  3062. break;
  3063. }
  3064. curchar++;
  3065. }
  3066. return (result);
  3067. }
  3068. /* ---- Bezier curve */
  3069. /*!
  3070. \brief Internal function to calculate bezier interpolator of data array with ndata values at position 't'.
  3071. \param data Array of values.
  3072. \param ndata Size of array.
  3073. \param t Position for which to calculate interpolated value. t should be between [0, ndata].
  3074. \returns Interpolated value at position t, value[0] when t<0, value[n-1] when t>n.
  3075. */
  3076. double _evaluateBezier (double *data, int ndata, double t)
  3077. {
  3078. double mu, result;
  3079. int n,k,kn,nn,nkn;
  3080. double blend,muk,munk;
  3081. /* Sanity check bounds */
  3082. if (t<0.0) {
  3083. return(data[0]);
  3084. }
  3085. if (t>=(double)ndata) {
  3086. return(data[ndata-1]);
  3087. }
  3088. /* Adjust t to the range 0.0 to 1.0 */
  3089. mu=t/(double)ndata;
  3090. /* Calculate interpolate */
  3091. n=ndata-1;
  3092. result=0.0;
  3093. muk = 1;
  3094. munk = pow(1-mu,(double)n);
  3095. for (k=0;k<=n;k++) {
  3096. nn = n;
  3097. kn = k;
  3098. nkn = n - k;
  3099. blend = muk * munk;
  3100. muk *= mu;
  3101. munk /= (1-mu);
  3102. while (nn >= 1) {
  3103. blend *= nn;
  3104. nn--;
  3105. if (kn > 1) {
  3106. blend /= (double)kn;
  3107. kn--;
  3108. }
  3109. if (nkn > 1) {
  3110. blend /= (double)nkn;
  3111. nkn--;
  3112. }
  3113. }
  3114. result += data[k] * blend;
  3115. }
  3116. return (result);
  3117. }
  3118. /*!
  3119. \brief Draw a bezier curve with alpha blending.
  3120. \param renderer The renderer to draw on.
  3121. \param vx Vertex array containing X coordinates of the points of the bezier curve.
  3122. \param vy Vertex array containing Y coordinates of the points of the bezier curve.
  3123. \param n Number of points in the vertex array. Minimum number is 3.
  3124. \param s Number of steps for the interpolation. Minimum number is 2.
  3125. \param color The color value of the bezier curve to draw (0xRRGGBBAA).
  3126. \returns Returns 0 on success, -1 on failure.
  3127. */
  3128. int bezierColor(SDL_Renderer * renderer, const Sint16 * vx, const Sint16 * vy, int n, int s, Uint32 color)
  3129. {
  3130. Uint8 *c = (Uint8 *)&color;
  3131. return bezierRGBA(renderer, vx, vy, n, s, c[0], c[1], c[2], c[3]);
  3132. }
  3133. /*!
  3134. \brief Draw a bezier curve with alpha blending.
  3135. \param renderer The renderer to draw on.
  3136. \param vx Vertex array containing X coordinates of the points of the bezier curve.
  3137. \param vy Vertex array containing Y coordinates of the points of the bezier curve.
  3138. \param n Number of points in the vertex array. Minimum number is 3.
  3139. \param s Number of steps for the interpolation. Minimum number is 2.
  3140. \param r The red value of the bezier curve to draw.
  3141. \param g The green value of the bezier curve to draw.
  3142. \param b The blue value of the bezier curve to draw.
  3143. \param a The alpha value of the bezier curve to draw.
  3144. \returns Returns 0 on success, -1 on failure.
  3145. */
  3146. int bezierRGBA(SDL_Renderer * renderer, const Sint16 * vx, const Sint16 * vy, int n, int s, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  3147. {
  3148. int result;
  3149. int i;
  3150. double *x, *y, t, stepsize;
  3151. Sint16 x1, y1, x2, y2;
  3152. /*
  3153. * Sanity check
  3154. */
  3155. if (n < 3) {
  3156. return (-1);
  3157. }
  3158. if (s < 2) {
  3159. return (-1);
  3160. }
  3161. /*
  3162. * Variable setup
  3163. */
  3164. stepsize=(double)1.0/(double)s;
  3165. /* Transfer vertices into float arrays */
  3166. if ((x=(double *)malloc(sizeof(double)*(n+1)))==NULL) {
  3167. return(-1);
  3168. }
  3169. if ((y=(double *)malloc(sizeof(double)*(n+1)))==NULL) {
  3170. free(x);
  3171. return(-1);
  3172. }
  3173. for (i=0; i<n; i++) {
  3174. x[i]=(double)vx[i];
  3175. y[i]=(double)vy[i];
  3176. }
  3177. x[n]=(double)vx[0];
  3178. y[n]=(double)vy[0];
  3179. /*
  3180. * Set color
  3181. */
  3182. result = 0;
  3183. result |= SDL_SetRenderDrawBlendMode(renderer, (a == 255) ? SDL_BLENDMODE_NONE : SDL_BLENDMODE_BLEND);
  3184. result |= SDL_SetRenderDrawColor(renderer, r, g, b, a);
  3185. /*
  3186. * Draw
  3187. */
  3188. t=0.0;
  3189. x1=(Sint16)lrint(_evaluateBezier(x,n+1,t));
  3190. y1=(Sint16)lrint(_evaluateBezier(y,n+1,t));
  3191. for (i = 0; i <= (n*s); i++) {
  3192. t += stepsize;
  3193. x2=(Sint16)_evaluateBezier(x,n,t);
  3194. y2=(Sint16)_evaluateBezier(y,n,t);
  3195. result |= line(renderer, x1, y1, x2, y2);
  3196. x1 = x2;
  3197. y1 = y2;
  3198. }
  3199. /* Clean up temporary array */
  3200. free(x);
  3201. free(y);
  3202. return (result);
  3203. }
  3204. /*!
  3205. \brief Draw a thick line with alpha blending.
  3206. \param renderer The renderer to draw on.
  3207. \param x1 X coordinate of the first point of the line.
  3208. \param y1 Y coordinate of the first point of the line.
  3209. \param x2 X coordinate of the second point of the line.
  3210. \param y2 Y coordinate of the second point of the line.
  3211. \param width Width of the line in pixels. Must be >0.
  3212. \param color The color value of the line to draw (0xRRGGBBAA).
  3213. \returns Returns 0 on success, -1 on failure.
  3214. */
  3215. int thickLineColor(SDL_Renderer *renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint8 width, Uint32 color)
  3216. {
  3217. Uint8 *c = (Uint8 *)&color;
  3218. return thickLineRGBA(renderer, x1, y1, x2, y2, width, c[0], c[1], c[2], c[3]);
  3219. }
  3220. /*!
  3221. \brief Draw a thick line with alpha blending.
  3222. \param renderer The renderer to draw on.
  3223. \param x1 X coordinate of the first point of the line.
  3224. \param y1 Y coordinate of the first point of the line.
  3225. \param x2 X coordinate of the second point of the line.
  3226. \param y2 Y coordinate of the second point of the line.
  3227. \param width Width of the line in pixels. Must be >0.
  3228. \param r The red value of the character to draw.
  3229. \param g The green value of the character to draw.
  3230. \param b The blue value of the character to draw.
  3231. \param a The alpha value of the character to draw.
  3232. \returns Returns 0 on success, -1 on failure.
  3233. */
  3234. int thickLineRGBA(SDL_Renderer *renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint8 width, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  3235. {
  3236. int wh;
  3237. double dx, dy, dx1, dy1, dx2, dy2;
  3238. double l, wl2, nx, ny, ang, adj;
  3239. Sint16 px[4], py[4];
  3240. if (renderer == NULL) {
  3241. return -1;
  3242. }
  3243. if (width < 1) {
  3244. return -1;
  3245. }
  3246. /* Special case: thick "point" */
  3247. if ((x1 == x2) && (y1 == y2)) {
  3248. wh = width / 2;
  3249. return boxRGBA(renderer, x1 - wh, y1 - wh, x2 + width, y2 + width, r, g, b, a);
  3250. }
  3251. /* Special case: width == 1 */
  3252. if (width == 1) {
  3253. return lineRGBA(renderer, x1, y1, x2, y2, r, g, b, a);
  3254. }
  3255. /* Calculate offsets for sides */
  3256. dx = (double)(x2 - x1);
  3257. dy = (double)(y2 - y1);
  3258. l = SDL_sqrt(dx*dx + dy*dy);
  3259. ang = SDL_atan2(dx, dy);
  3260. adj = 0.1 + 0.9 * SDL_fabs(SDL_cos(2.0 * ang));
  3261. wl2 = ((double)width - adj)/(2.0 * l);
  3262. nx = dx * wl2;
  3263. ny = dy * wl2;
  3264. /* Build polygon */
  3265. dx1 = (double)x1;
  3266. dy1 = (double)y1;
  3267. dx2 = (double)x2;
  3268. dy2 = (double)y2;
  3269. px[0] = (Sint16)(dx1 + ny);
  3270. px[1] = (Sint16)(dx1 - ny);
  3271. px[2] = (Sint16)(dx2 - ny);
  3272. px[3] = (Sint16)(dx2 + ny);
  3273. py[0] = (Sint16)(dy1 - nx);
  3274. py[1] = (Sint16)(dy1 + nx);
  3275. py[2] = (Sint16)(dy2 + nx);
  3276. py[3] = (Sint16)(dy2 - nx);
  3277. /* Draw polygon */
  3278. return filledPolygonRGBA(renderer, px, py, 4, r, g, b, a);
  3279. }