shapes.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /**--------------------------------------------------------------------------**\
  2. =================================
  3. y_bitmap - Generate bitmaps.
  4. =================================
  5. Description:
  6. Code to generate images on the server in the bitmap format. This is by far
  7. the simplest format to write to as it is just a huge array of colours (at
  8. least 24-bit bitmaps are, and we only do them).
  9. This file contains functions to draw various shapes.
  10. Legal:
  11. Version: MPL 1.1
  12. The contents of this file are subject to the Mozilla Public License Version
  13. 1.1 (the "License"); you may not use this file except in compliance with
  14. the License. You may obtain a copy of the License at
  15. http://www.mozilla.org/MPL/
  16. Software distributed under the License is distributed on an "AS IS" basis,
  17. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  18. for the specific language governing rights and limitations under the
  19. License.
  20. The Original Code is the YSI utils include.
  21. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  22. Portions created by the Initial Developer are Copyright (C) 2011
  23. the Initial Developer. All Rights Reserved.
  24. Contributors:
  25. ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice
  26. Thanks:
  27. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  28. ZeeX - Very productive conversations.
  29. koolk - IsPlayerinAreaEx code.
  30. TheAlpha - Danish translation.
  31. breadfish - German translation.
  32. Fireburn - Dutch translation.
  33. yom - French translation.
  34. 50p - Polish translation.
  35. Zamaroht - Spanish translation.
  36. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  37. for me to strive to better.
  38. Pixels^ - Running XScripters where the idea was born.
  39. Matite - Pestering me to release it and using it.
  40. Very special thanks to:
  41. Thiadmer - PAWN, whose limits continue to amaze me!
  42. Kye/Kalcor - SA:MP.
  43. SA:MP Team past, present and future - SA:MP.
  44. Version:
  45. 0.1
  46. Changelog:
  47. 29/03/13:
  48. First version.
  49. Functions:
  50. Stock:
  51. -
  52. Inline:
  53. -
  54. Variables:
  55. Global:
  56. -
  57. \**--------------------------------------------------------------------------**/
  58. /**--------------------------------------------------------------------------**\
  59. <summary>Bitmap_DrawRectangle</summary>
  60. <param name="Bitmap:ctx">The bitmap to modify.</param>
  61. <param name="const minX">The left of the array.</param>
  62. <param name="const minY">The top of the array.</param>
  63. <param name="const maxX">The right of the array.</param>
  64. <param name="const maxY">The bottom of the array.</param>
  65. <param name="fillColour">The colour of the inside of the rectangle.</param>
  66. <param name="lineColour = 0">The colour of the rectangle border.</param>
  67. <param name="linePattern[] = \"SOLID\"">How to draw the inside.</param>
  68. <param name="fillPattern[] = \"SOLID\"">How to draw the border.</param>
  69. <returns>
  70. -
  71. </returns>
  72. <remarks>
  73. This function draws a rectangle between two given co-ordinates. It can also
  74. draw a border around the rectangle OUTSIDE the specified area of the
  75. rectangle. If you want the border within the area specified you need to use
  76. a smaller area.
  77. </remarks>
  78. \**--------------------------------------------------------------------------**/
  79. // End the input above because all this code is being written with no testing
  80. // or compiling until I am home again. So in the interests of leaving the code
  81. // stable, this is all ignored.
  82. stock Bitmap_DrawRectangle(Bitmap:ctx, const minX, const minY, const maxX, const maxY, fillColour, lineColour = 0, linePattern[] = "SOLID", fillPattern[] = "SOLID")
  83. {
  84. if (fillColour)
  85. {
  86. _Bitmap_DoRectangle(ctx, minX, minY, maxX, maxY, fillColour, fillPattern);
  87. }
  88. if (lineColour)
  89. {
  90. // Do the borders.
  91. if (strcmp(linePattern, "DOTTED", true, 6))
  92. {
  93. new
  94. border = _Bitmap_Param(linePattern, "border");
  95. if (border == cellmin) border = 8;
  96. //printf("border = %d", border);
  97. //printf("1");
  98. _Bitmap_DoRectangle(ctx, minX - border, minY - border, maxX + border, minY, lineColour, linePattern),
  99. //printf("1");
  100. _Bitmap_DoRectangle(ctx, minX - border, minY, minX, maxY, lineColour, linePattern),
  101. //printf("1");
  102. _Bitmap_DoRectangle(ctx, minX - border, maxY, maxX + border, maxY + border, lineColour, linePattern),
  103. //printf("1");
  104. _Bitmap_DoRectangle(ctx, maxX, minY, maxX + border, maxY, lineColour, linePattern);
  105. //printf("1");
  106. }
  107. else
  108. {
  109. new
  110. border = _Bitmap_Param(linePattern, "border"),
  111. pat[128] = "STRIPED";
  112. strcat(pat, linePattern[6]);
  113. if (border == cellmin) border = 8;
  114. _Bitmap_DoRectangle(ctx, minX - border, minY - border, maxX + border, minY, lineColour, pat),
  115. _Bitmap_DoRectangle(ctx, minX - border, maxY, maxX + border, maxY + border, lineColour, pat),
  116. strcat(pat, ",HORIZONTAL"),
  117. _Bitmap_DoRectangle(ctx, minX - border, minY, minX, maxY, lineColour, pat),
  118. _Bitmap_DoRectangle(ctx, maxX, minY, maxX + border, maxY, lineColour, pat);
  119. }
  120. }
  121. }
  122. static stock _Bitmap_DoRectangle(Bitmap:ctx, minX, minY, maxX, maxY, colour, pattern[])
  123. {
  124. new
  125. width = Bitmap_Width(ctx);
  126. minY = max(0, minY),
  127. maxY = min(maxY, Bitmap_Height(ctx)),
  128. minX = max(0, minX),
  129. maxX = min(maxX, width),
  130. _Bitmap_MakePattern(pattern, minX, minY, 0, 0);
  131. new
  132. ya,
  133. read,
  134. a = (colour & 0xFF) + 1,
  135. na,
  136. suba,
  137. r = ((colour & 0xFF000000) >>> 16),
  138. g = ((colour & 0x00FF0000) >>> 16),
  139. b = ((colour & 0x0000FF00) >>> 8),
  140. orig;
  141. for (new y = minY; y != maxY; ++y)
  142. {
  143. read = Bitmap_IndexInt(ctx, minX, width, y),
  144. ya = y % YSI_gBitmapAlphaY;
  145. for (new x = minX; x != maxX; ++x)
  146. {
  147. suba = YSI_gBitmapAlpha[ya]{x % YSI_gBitmapAlphaX} * a;
  148. if (suba == 0xFF00)
  149. {
  150. YSI_gMallocMemory[read] = colour;
  151. }
  152. else if (suba >= 256)
  153. {
  154. // This is where we compensate for the odd shifts.
  155. na = 0xFF00 - suba,
  156. // Want this pixel in the final image.
  157. orig = YSI_gMallocMemory[read],
  158. YSI_gMallocMemory[read] = 0xFF |
  159. ((((orig & 0xFF000000) >>> 16) * na + r * suba) & 0xFF000000) |
  160. ((((orig & 0x00FF0000) >>> 16) * na + g * suba) & 0x00FF0000) |
  161. (((((orig & 0x0000FF00) >>> 8) * na + b * suba) & 0x00FF0000) >>> 8) ;
  162. }
  163. ++read;
  164. }
  165. }
  166. }
  167. static stock _Bitmap_RectangleRecopy(n, BitArray:pattern<PATTERN_MASK_X>)
  168. {
  169. for (new y = 1; y != n; ++y)
  170. {
  171. YSI_gBitmapAlpha[y] = pattern;
  172. }
  173. }
  174. /**--------------------------------------------------------------------------**\
  175. <summary>Bitmap_DrawRectangle</summary>
  176. <param name="Bitmap:ctx">The bitmap to modify.</param>
  177. <param name="const x">The x co-ordinate of the circle centre.</param>
  178. <param name="const y">The y co-ordinate of the circle centre.</param>
  179. <param name="const Float:radius">The size of the circle.</param>
  180. <param name="fillColour">The colour of the inside of the circle.</param>
  181. <param name="lineColour = 0">The colour of the circle border.</param>
  182. <param name="linePattern[] = \"SOLID\"">How to draw the inside.</param>
  183. <param name="fillPattern[] = \"SOLID\"">How to draw the border.</param>
  184. <returns>
  185. -
  186. </returns>
  187. <remarks>
  188. This function draws a circle centered on the given co-ordinates with the
  189. given FLOAT radius, not an integer size because of diagonals. It can also
  190. draw a border around the circle OUTSIDE the specified area of the circle.
  191. If you want the border within the area specified you need to use a smaller
  192. area.
  193. </remarks>
  194. \**--------------------------------------------------------------------------**/
  195. stock Bitmap_DrawCircle(Bitmap:ctx, const x, const y, const Float:radius, fillColour, lineColour = 0, linePattern[] = "SOLID", fillPattern[] = "SOLID")
  196. {
  197. P:3("Bitmap_DrawCircle called: %d, %d, %d, %.2f, 0x%04x%04x, 0x%04x%04x, \"%s\", \"%s\"", _:ctx, x, y, radius, fillColour >>> 16, fillColour & 0xFFFF, lineColour >>> 16, lineColour & 0xFFFF, linePattern, fillPattern);
  198. Bitmap_DrawRing(ctx, x, y, 0.0, radius, fillColour, fillPattern);
  199. }
  200. /**--------------------------------------------------------------------------**\
  201. <summary>Bitmap_DrawRectangle</summary>
  202. <param name="Bitmap:ctx">The bitmap to modify.</param>
  203. <param name="x">The x co-ordinate of the ring centre.</param>
  204. <param name="y">The y co-ordinate of the ring centre.</param>
  205. <param name="const Float:inner">The inner size of the ring.</param>
  206. <param name="const Float:outer">The outer size of the ring.</param>
  207. <param name="colour">The colour of the ring.</param>
  208. <param name="pattern[] = \"SOLID\"">How to draw the inside.</param>
  209. <returns>
  210. -
  211. </returns>
  212. <remarks>
  213. This function draws a ring centered on the given co-ordinates with the
  214. given FLOAT radius, not an integer size because of diagonals. This does not
  215. draw a border as it is used for borders, and a border on a ring is ambiguous
  216. - do you have it on the inside or not? Better to let the user decide.
  217. </remarks>
  218. \**--------------------------------------------------------------------------**/
  219. stock Bitmap_DrawRing(Bitmap:ctx, x, y, Float:inner, Float:outer, colour, pattern[] = "SOLID")
  220. {
  221. P:3("Bitmap_DrawRing called: %d, %d, %d, %.2f, %.2f, 0x%04x%04x, \"%s\"", _:ctx, x, y, inner, outer, colour >>> 16, colour & 0xFFFF, pattern);
  222. new
  223. rad = max(floatround(outer, floatround_ceil), 0);
  224. if (rad == 0) return;
  225. new
  226. width = Bitmap_Width(ctx),
  227. // Get the circle bounds.
  228. minY = max(0, y - rad),
  229. maxY = min(y + rad + 1, Bitmap_Height(ctx)),
  230. minX = max(0, x - rad),
  231. maxX = min(x + rad + 1, width),
  232. // Get the approximate edges of the circle so we know how much blending
  233. // to use on the outer pixels.
  234. Float:outerMax = outer + 1.0,
  235. Float:rootMax = outer - 1.0,
  236. Float:outerMin = rootMax * rootMax,
  237. Float:innerMax = inner + 1.0,
  238. Float:rootMin = inner - 1.0,
  239. Float:innerMin;
  240. outerMax *= outerMax,
  241. innerMax *= innerMax;
  242. if (inner <= 0.0)
  243. {
  244. inner = 0.0;
  245. //innerMin = 0.0;
  246. }
  247. else innerMin = rootMin * rootMin;
  248. //new
  249. // Float:outerDiff = outerMax - outerMin,
  250. // Float:innerDiff = innerMax - innerMin;
  251. _Bitmap_MakePattern(pattern, minX, minY, 0, 0);
  252. new
  253. ya,
  254. // Get the float co-ordinates for ease of calculation.
  255. Float:fX = float(-rad), // Converted back from the integer version to.
  256. Float:fXX,
  257. Float:fY = float(-rad), // Make up for rounding errors.
  258. Float:fYY,
  259. Float:pos,
  260. read,
  261. a = (colour & 0xFF) + 1,
  262. na,
  263. suba,
  264. r = ((colour & 0xFF000000) >>> 16),
  265. g = ((colour & 0x00FF0000) >>> 16),
  266. b = ((colour & 0x0000FF00) >>> 8),
  267. orig;
  268. P:5("Bitmap_DrawRing: minX = %d, minY = %d, width = %d, height = %d", minX, minY, width, Bitmap_Height(ctx));
  269. for (y = minY; y != maxY; ++y, ++fY)
  270. {
  271. // "y" portion of the radius calculation.
  272. fYY = fY * fY,
  273. read = Bitmap_IndexInt(ctx, minX, width, y),
  274. ya = y % YSI_gBitmapAlphaY;
  275. P:6("Bitmap_DrawRing: 4: %.2f, %d, %d", fYY, read, ya);
  276. fXX = fX;
  277. for (x = minX; x != maxX; ++x, ++fXX, ++read)
  278. {
  279. pos = fXX * fXX + fYY;
  280. P:7("Bitmap_DrawRing: 5: %.2f <= %.2f <= %.2f", outerMin, pos, outerMax);
  281. // Slightly redundant check if "inner" is 0.0, but in that case will
  282. // at least never fail!
  283. if (pos >= innerMin)
  284. {
  285. // Calculate the full colour.
  286. suba = YSI_gBitmapAlpha[ya]{x % YSI_gBitmapAlphaX} * a;
  287. if (inner != 0.0 && pos <= innerMax)
  288. {
  289. // Draw partially (blending for the ring inner edge).
  290. //YSI_gMallocMemory[read] = 0xFF; // Black for now.
  291. suba = floatround((floatsqroot(pos) - rootMin) / 2.0 * float(suba));
  292. }
  293. else if (pos <= outerMin)
  294. {
  295. // Draw fully (part of the ring).
  296. //YSI_gMallocMemory[read] = suba;
  297. // Do nothing, but also don't end.
  298. }
  299. else if (pos <= outerMax)
  300. {
  301. // Draw partially (blending for the ring outer edge).
  302. //YSI_gMallocMemory[read] = 0xFF; // Black for now.
  303. suba = floatround((1.0 - (floatsqroot(pos) - rootMax) / 2.0) * float(suba));
  304. }
  305. else
  306. {
  307. // Otherwise don't draw anything at all - we are outside the ring.
  308. continue;
  309. }
  310. if (suba >= 0xFF00)
  311. {
  312. YSI_gMallocMemory[read] = colour;
  313. }
  314. else if (suba > 0xFF)
  315. {
  316. // This is where we compensate for the odd shifts.
  317. na = 0xFF00 - suba,
  318. // Want this pixel in the final image.
  319. orig = YSI_gMallocMemory[read],
  320. YSI_gMallocMemory[read] = 0xFF |
  321. ((((orig & 0xFF000000) >>> 16) * na + r * suba) & 0xFF000000) |
  322. ((((orig & 0x00FF0000) >>> 16) * na + g * suba) & 0x00FF0000) |
  323. (((((orig & 0x0000FF00) >>> 8) * na + b * suba) & 0x00FF0000) >>> 8) ;
  324. }
  325. }
  326. }
  327. }
  328. }