drawAPI.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. // ------------------------ Below is untested! ------------------------ //
  29. // Drawing shapes
  30. void draw_rectangle(ImageU8& image, const IRect& bound, int color);
  31. void draw_rectangle(ImageF32& image, const IRect& bound, float color);
  32. void draw_rectangle(ImageRgbaU8& image, const IRect& bound, const ColorRgbaI32& color);
  33. void draw_line(ImageU8& image, int32_t x1, int32_t y1, int32_t x2, int32_t y2, int color);
  34. void draw_line(ImageF32& image, int32_t x1, int32_t y1, int32_t x2, int32_t y2, float color);
  35. void draw_line(ImageRgbaU8& image, int32_t x1, int32_t y1, int32_t x2, int32_t y2, const ColorRgbaI32& color);
  36. // Drawing images
  37. // Draw an image to another image
  38. // All image types can draw to their own format
  39. // All image types can draw to RgbaU8
  40. // All monochrome types can draw to each other
  41. // The source and target images can be sub-images from the same atlas but only if the sub-regions are not overlapping
  42. void draw_copy(ImageRgbaU8& target, const ImageRgbaU8& source, int32_t left = 0, int32_t top = 0);
  43. void draw_copy(ImageU8& target, const ImageU8& source, int32_t left = 0, int32_t top = 0);
  44. void draw_copy(ImageU16& target, const ImageU16& source, int32_t left = 0, int32_t top = 0);
  45. void draw_copy(ImageF32& target, const ImageF32& source, int32_t left = 0, int32_t top = 0);
  46. void draw_copy(ImageRgbaU8& target, const ImageU8& source, int32_t left = 0, int32_t top = 0);
  47. void draw_copy(ImageRgbaU8& target, const ImageU16& source, int32_t left = 0, int32_t top = 0);
  48. void draw_copy(ImageRgbaU8& target, const ImageF32& source, int32_t left = 0, int32_t top = 0);
  49. void draw_copy(ImageU8& target, const ImageF32& source, int32_t left = 0, int32_t top = 0);
  50. void draw_copy(ImageU8& target, const ImageU16& source, int32_t left = 0, int32_t top = 0);
  51. void draw_copy(ImageU16& target, const ImageU8& source, int32_t left = 0, int32_t top = 0);
  52. void draw_copy(ImageU16& target, const ImageF32& source, int32_t left = 0, int32_t top = 0);
  53. void draw_copy(ImageF32& target, const ImageU8& source, int32_t left = 0, int32_t top = 0);
  54. void draw_copy(ImageF32& target, const ImageU16& source, int32_t left = 0, int32_t top = 0);
  55. // Draw one RGBA image to another using alpha filtering
  56. // Target alpha does no affect RGB blending, in case that it contains padding for opaque targets
  57. // If you really want to draw to a transparent layer, this method should not be used
  58. void draw_alphaFilter(ImageRgbaU8& target, const ImageRgbaU8& source, int32_t left = 0, int32_t top = 0);
  59. // Draw one RGBA image to another using the alpha channel as height
  60. // sourceAlphaOffset is added to non-zero heights from source alpha
  61. // Writes each source pixel who's alpha value is greater than the target's
  62. // Zero alpha can be used as a mask, because no source value can be below zero in unsigned color formats
  63. void draw_maxAlpha(ImageRgbaU8& target, const ImageRgbaU8& source, int32_t left = 0, int32_t top = 0, int32_t sourceAlphaOffset = 0);
  64. // Draw between multiple images using a height buffer
  65. // Each source pixel is drawn where the source height's pixel exceeds the target height's pixel
  66. // Including the source height pixel, so that the drawn object occludes the following objects below it
  67. // Can be used for isometric top-down and side-scroller games with heavy graphical effects
  68. // A usually contains color pixels
  69. // B usually contains surface normals for light effects
  70. // 16-bit integer depth buffers:
  71. // Fully deterministic overlaps
  72. // Source height zero is treated as invisible even if sourceHeightOffset adds to the height
  73. // It's recommended to let the target height buffer use 32768 as height zero to allow placing things on negative locations
  74. void draw_higher(
  75. ImageU16& targetHeight, const ImageU16& sourceHeight,
  76. int32_t left = 0, int32_t top = 0, int32_t sourceHeightOffset = 0
  77. );
  78. void draw_higher(ImageU16& targetHeight, const ImageU16& sourceHeight,
  79. ImageRgbaU8& targetA, const ImageRgbaU8& sourceA,
  80. int32_t left = 0, int32_t top = 0, int32_t sourceHeightOffset = 0
  81. );
  82. void draw_higher(ImageU16& targetHeight, const ImageU16& sourceHeight,
  83. ImageRgbaU8& targetA, const ImageRgbaU8& sourceA,
  84. ImageRgbaU8& targetB, const ImageRgbaU8& sourceB,
  85. int32_t left = 0, int32_t top = 0, int32_t sourceHeightOffset = 0
  86. );
  87. // 32-bit floating-point depth buffers
  88. // Source height negative infinity is used for invisible pixels
  89. // Negative infinity is expressed using -std::numeric_limits<float>::infinity() from limits.h
  90. // Same pixel size as in ImageRgbaU8 to make aligned reading easier when used together with colors
  91. // Floats allow doing light calculations directly without having to perform expensive conversions from integers
  92. void draw_higher(
  93. ImageF32& targetHeight, const ImageF32& sourceHeight,
  94. int32_t left = 0, int32_t top = 0, float sourceHeightOffset = 0
  95. );
  96. void draw_higher(ImageF32& targetHeight, const ImageF32& sourceHeight,
  97. ImageRgbaU8& targetA, const ImageRgbaU8& sourceA,
  98. int32_t left = 0, int32_t top = 0, float sourceHeightOffset = 0
  99. );
  100. void draw_higher(ImageF32& targetHeight, const ImageF32& sourceHeight,
  101. ImageRgbaU8& targetA, const ImageRgbaU8& sourceA,
  102. ImageRgbaU8& targetB, const ImageRgbaU8& sourceB,
  103. int32_t left = 0, int32_t top = 0, float sourceHeightOffset = 0
  104. );
  105. // Draw one RGBA image to another using alpha clipping
  106. // Source is solid where alpha is greater than threshold, which can be used for animations
  107. void draw_alphaClip(ImageRgbaU8& target, const ImageRgbaU8& source, int32_t left = 0, int32_t top = 0, int32_t threshold = 127);
  108. // Draw a uniform color using a grayscale silhouette as the alpha channel
  109. void draw_silhouette(ImageRgbaU8& target, const ImageU8& silhouette, const ColorRgbaI32& color, int32_t left = 0, int32_t top = 0);
  110. }
  111. #endif