/[gameplay]/gps/linedrawer.lua

https://gitlab.com/yasin3223/mtasa-resources · Lua · 128 lines · 84 code · 27 blank · 17 comment · 15 complexity · 0ae1c0d0f9be686fe927b25a537633ae MD5 · raw file

  1. local ENABLE_FAILISH_ATTEMPT_AT_ANTI_ALIASING = false
  2. local OVERLAY_WIDTH = 256
  3. local OVERLAY_HEIGHT = 256
  4. local OVERLAY_LINE_WIDTH = 5
  5. local OVERLAY_LINE_COLOR = tocolor ( 0, 200, 0, 255 )
  6. local OVERLAY_LINE_AA = tocolor ( 0, 200, 0, 200 )
  7. local linePoints = { }
  8. local renderStuff = { }
  9. function removeLinePoints ( )
  10. linePoints = { }
  11. for name, data in pairs ( renderStuff ) do
  12. unloadTile ( name )
  13. end
  14. end
  15. function addLinePoint ( posX, posY )
  16. -- Calculate the row and column of the radar tile we will be targeting
  17. local row = 11 - math.floor ( ( posY + 3000 ) / 500 )
  18. local col = math.floor ( ( posX + 3000 ) / 500 )
  19. -- If it's off the map, don't bother
  20. if row < 0 or row > 11 or col < 0 or col > 11 then
  21. return false
  22. end
  23. -- Check the start position of the tile
  24. local startX = col * 500 - 3000
  25. local startY = 3000 - row * 500
  26. -- Now get the tile position (We don't want to calculate this for every point on render)
  27. local tileX = ( posX - startX ) / 500 * OVERLAY_WIDTH
  28. local tileY = ( startY - posY ) / 500 * OVERLAY_HEIGHT
  29. -- Now calulcate the ID and get the name of the tile
  30. local id = col + row * 12
  31. local name = string.format ( "radar%02d", id )
  32. -- Make sure the line point table exists
  33. if not linePoints [ name ] then
  34. linePoints [ name ] = { }
  35. end
  36. -- Now add this point
  37. table.insert ( linePoints[name], { posX = tileX, posY = tileY } )
  38. -- Success!
  39. return true
  40. end
  41. function loadTile ( name )
  42. -- Create our fabulous shader. Abort on failure
  43. local shader = dxCreateShader ( "overlay.fx" )
  44. if not shader then
  45. return false
  46. end
  47. -- Create a render target. Again, abort on failure (don't forget to delete the shader!)
  48. local rt = dxCreateRenderTarget ( OVERLAY_WIDTH, OVERLAY_HEIGHT, true )
  49. if not rt then
  50. destroyElement ( shader )
  51. return false
  52. end
  53. -- Mix 'n match
  54. dxSetShaderValue ( shader, "gOverlay", rt )
  55. -- Start drawing
  56. dxSetRenderTarget ( rt )
  57. -- Get the points involved, and get the starting position
  58. local points = linePoints [ name ]
  59. local prevX, prevY = points [ 1 ].posX, points [ 1 ] .posY
  60. -- Loop through all points we have to draw, and draw them
  61. for index, point in ipairs ( points ) do
  62. local newX = point.posX
  63. local newY = point.posY
  64. if ENABLE_FAILISH_ATTEMPT_AT_ANTI_ALIASING then
  65. dxDrawLine ( prevX - 1, prevY - 1, newX - 1, newY - 1, OVERLAY_LINE_AA, OVERLAY_LINE_WIDTH )
  66. dxDrawLine ( prevX + 1, prevY - 1, newX + 1, newY - 1, OVERLAY_LINE_AA, OVERLAY_LINE_WIDTH )
  67. dxDrawLine ( prevX - 1, prevY + 1, newX - 1, newY + 1, OVERLAY_LINE_AA, OVERLAY_LINE_WIDTH )
  68. dxDrawLine ( prevX + 1, prevY + 1, newX + 1, newY + 1, OVERLAY_LINE_AA, OVERLAY_LINE_WIDTH )
  69. end
  70. dxDrawLine ( prevX, prevY, newX, newY, OVERLAY_LINE_COLOR, OVERLAY_LINE_WIDTH )
  71. prevX = newX
  72. prevY = newY
  73. end
  74. -- Now let's show our fabulous work to the commoners!
  75. engineApplyShaderToWorldTexture ( shader, name )
  76. -- Store the stuff in memories
  77. renderStuff [ name ] = { shader = shader, rt = rt }
  78. -- We won
  79. return true
  80. end
  81. function unloadTile ( name )
  82. destroyElement ( renderStuff[name].shader )
  83. destroyElement ( renderStuff[name].rt )
  84. renderStuff[name] = nil
  85. return true
  86. end
  87. addEventHandler ( "onClientHUDRender", getRootElement ( ),
  88. function ( )
  89. local visibleTileNames = table.merge ( engineGetVisibleTextureNames ( "radar??" ), engineGetVisibleTextureNames ( "radar???" ) )
  90. for name, data in pairs ( renderStuff ) do
  91. if not table.find ( visibleTileNames, name ) then
  92. unloadTile ( name )
  93. end
  94. end
  95. for index, name in ipairs ( visibleTileNames ) do
  96. if linePoints [ name ] and not renderStuff [ name ] then
  97. loadTile ( name )
  98. end
  99. end
  100. end
  101. )