drawAPI.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. 
  2. // zlib open source license
  3. //
  4. // Copyright (c) 2017 to 2020 David Forsgren Piuva
  5. //
  6. // This software is provided 'as-is', without any express or implied
  7. // warranty. In no event will the authors be held liable for any damages
  8. // arising from the use of this software.
  9. //
  10. // Permission is granted to anyone to use this software for any purpose,
  11. // including commercial applications, and to alter it and redistribute it
  12. // freely, subject to the following restrictions:
  13. //
  14. // 1. The origin of this software must not be misrepresented; you must not
  15. // claim that you wrote the original software. If you use this software
  16. // in a product, an acknowledgment in the product documentation would be
  17. // appreciated but is not required.
  18. //
  19. // 2. Altered source versions must be plainly marked as such, and must not be
  20. // misrepresented as being the original software.
  21. //
  22. // 3. This notice may not be removed or altered from any source
  23. // distribution.
  24. #ifndef DFPSR_API_DRAW
  25. #define DFPSR_API_DRAW
  26. #include "types.h"
  27. namespace dsr {
  28. // Drawing shapes
  29. void draw_rectangle(ImageU8& image, const IRect& bound, int color);
  30. void draw_rectangle(ImageF32& image, const IRect& bound, float color);
  31. void draw_rectangle(ImageRgbaU8& image, const IRect& bound, const ColorRgbaI32& color);
  32. void draw_line(ImageU8& image, int32_t x1, int32_t y1, int32_t x2, int32_t y2, int color);
  33. void draw_line(ImageF32& image, int32_t x1, int32_t y1, int32_t x2, int32_t y2, float color);
  34. void draw_line(ImageRgbaU8& image, int32_t x1, int32_t y1, int32_t x2, int32_t y2, const ColorRgbaI32& color);
  35. // Drawing images
  36. // Draw an image to another image
  37. // All image types can draw to their own format
  38. // All image types can draw to RgbaU8
  39. // All monochrome types can draw to each other
  40. // The source and target images can be sub-images from the same atlas but only if the sub-regions are not overlapping
  41. void draw_copy(ImageRgbaU8& target, const ImageRgbaU8& source, int32_t left = 0, int32_t top = 0);
  42. void draw_copy(ImageU8& target, const ImageU8& source, int32_t left = 0, int32_t top = 0);
  43. void draw_copy(ImageU16& target, const ImageU16& source, int32_t left = 0, int32_t top = 0);
  44. void draw_copy(ImageF32& target, const ImageF32& source, int32_t left = 0, int32_t top = 0);
  45. void draw_copy(ImageRgbaU8& target, const ImageU8& source, int32_t left = 0, int32_t top = 0);
  46. void draw_copy(ImageRgbaU8& target, const ImageU16& source, int32_t left = 0, int32_t top = 0);
  47. void draw_copy(ImageRgbaU8& target, const ImageF32& source, int32_t left = 0, int32_t top = 0);
  48. void draw_copy(ImageU8& target, const ImageF32& source, int32_t left = 0, int32_t top = 0);
  49. void draw_copy(ImageU8& target, const ImageU16& source, int32_t left = 0, int32_t top = 0);
  50. void draw_copy(ImageU16& target, const ImageU8& source, int32_t left = 0, int32_t top = 0);
  51. void draw_copy(ImageU16& target, const ImageF32& source, int32_t left = 0, int32_t top = 0);
  52. void draw_copy(ImageF32& target, const ImageU8& source, int32_t left = 0, int32_t top = 0);
  53. void draw_copy(ImageF32& target, const ImageU16& source, int32_t left = 0, int32_t top = 0);
  54. // Draw one RGBA image to another using alpha filtering
  55. // Target alpha does no affect RGB blending, in case that it contains padding for opaque targets
  56. // If you really want to draw to a transparent layer, this method should not be used
  57. void draw_alphaFilter(ImageRgbaU8& target, const ImageRgbaU8& source, int32_t left = 0, int32_t top = 0);
  58. // Draw one RGBA image to another using the alpha channel as height
  59. // sourceAlphaOffset is added to non-zero heights from source alpha
  60. // Writes each source pixel who's alpha value is greater than the target's
  61. // Zero alpha can be used as a mask, because no source value can be below zero in unsigned color formats
  62. void draw_maxAlpha(ImageRgbaU8& target, const ImageRgbaU8& source, int32_t left = 0, int32_t top = 0, int32_t sourceAlphaOffset = 0);
  63. // Draw between multiple images using a height buffer
  64. // Each source pixel is drawn where the source height's pixel exceeds the target height's pixel
  65. // Including the source height pixel, so that the drawn object occludes the following objects below it
  66. // Can be used for isometric top-down and side-scroller games with heavy graphical effects
  67. // A usually contains color pixels
  68. // B usually contains surface normals for light effects
  69. // 16-bit integer depth buffers:
  70. // Fully deterministic overlaps
  71. // Source height zero is treated as invisible even if sourceHeightOffset adds to the height
  72. // It's recommended to let the target height buffer use 32768 as height zero to allow placing things on negative locations
  73. void draw_higher(
  74. ImageU16& targetHeight, const ImageU16& sourceHeight,
  75. int32_t left = 0, int32_t top = 0, int32_t sourceHeightOffset = 0
  76. );
  77. void draw_higher(ImageU16& targetHeight, const ImageU16& sourceHeight,
  78. ImageRgbaU8& targetA, const ImageRgbaU8& sourceA,
  79. int32_t left = 0, int32_t top = 0, int32_t sourceHeightOffset = 0
  80. );
  81. void draw_higher(ImageU16& targetHeight, const ImageU16& sourceHeight,
  82. ImageRgbaU8& targetA, const ImageRgbaU8& sourceA,
  83. ImageRgbaU8& targetB, const ImageRgbaU8& sourceB,
  84. int32_t left = 0, int32_t top = 0, int32_t sourceHeightOffset = 0
  85. );
  86. // 32-bit floating-point depth buffers
  87. // Source height negative infinity is used for invisible pixels
  88. // Negative infinity is expressed using -std::numeric_limits<float>::infinity() from limits.h
  89. // Same pixel size as in ImageRgbaU8 to make aligned reading easier when used together with colors
  90. // Floats allow doing light calculations directly without having to perform expensive conversions from integers
  91. void draw_higher(
  92. ImageF32& targetHeight, const ImageF32& sourceHeight,
  93. int32_t left = 0, int32_t top = 0, float sourceHeightOffset = 0
  94. );
  95. void draw_higher(ImageF32& targetHeight, const ImageF32& sourceHeight,
  96. ImageRgbaU8& targetA, const ImageRgbaU8& sourceA,
  97. int32_t left = 0, int32_t top = 0, float sourceHeightOffset = 0
  98. );
  99. void draw_higher(ImageF32& targetHeight, const ImageF32& sourceHeight,
  100. ImageRgbaU8& targetA, const ImageRgbaU8& sourceA,
  101. ImageRgbaU8& targetB, const ImageRgbaU8& sourceB,
  102. int32_t left = 0, int32_t top = 0, float sourceHeightOffset = 0
  103. );
  104. // Draw one RGBA image to another using alpha clipping
  105. // Source is solid where alpha is greater than threshold, which can be used for animations
  106. void draw_alphaClip(ImageRgbaU8& target, const ImageRgbaU8& source, int32_t left = 0, int32_t top = 0, int32_t threshold = 127);
  107. // Draw a uniform color using a grayscale silhouette as the alpha channel
  108. void draw_silhouette(ImageRgbaU8& target, const ImageU8& silhouette, const ColorRgbaI32& color, int32_t left = 0, int32_t top = 0);
  109. }
  110. #endif