December 04, 2020, 11:20:10 AM

Author Topic: [bb] Fast Line by Rob Farley [ 1+ years ago ]  (Read 1331 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Fast Line by Rob Farley [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : Fast Line
Author : Rob Farley
Posted : 1+ years ago

Description : I know this doesnt sound that exciting, however, if you want to draw only vertical and horizonal lines it's a darn sight faster than the standard line function.

I could have locked and unlocked the backbuffer() in the function however, it's much faster to lockbuffer, draw a bunch of lines, then unlockbuffer.

Usage:
fline(x1,y1,x2,y2,r,g,b)

Simple as that... If you try to draw a line that isn't horizontal or vertical it won't do anything.

Example code at the top, all you actually need is the function.


Code :
Code: BlitzBasic
  1. ; Fast line, 2004 Rob Farley
  2. ; rob@mentalillusion.co.uk
  3.  
  4. ;==========================================================
  5. ; example code
  6. ;==========================================================
  7.  
  8. Graphics 640,480
  9. SetBuffer BackBuffer()
  10. LockBuffer BackBuffer()
  11. For x=0 To 200 Step 20
  12. For y=0 To 200 Step 20
  13. fline(0,y,200,y,255,128,0)
  14. fline(x,0,x,200,255,0,0)
  15. Next
  16. Next
  17. UnlockBuffer BackBuffer()
  18. Flip
  19. WaitKey
  20.  
  21. ;==========================================================
  22. ; end of example code
  23. ;==========================================================
  24.  
  25.  
  26. Function fLine(x,y,x1,y1,r=255,g=255,b=255)
  27. ; fLine will only draw horizonal or vertical lines, no diagonals
  28. ; Defaults to a white line.
  29.  
  30. argb=(b Or (g Shl 8) Or (r Shl 16) Or ($ff000000))
  31.  
  32. If x=x1
  33.         If y>y1 Then t=y1:y1=y:y=t
  34.         For n=y To y1
  35.         WritePixelFast x,n,argb,BackBuffer()
  36.         Next
  37. EndIf
  38.  
  39. If y=y1
  40.         If x>x1 Then t=x1:x1=x:x=t
  41.         For n=x To x1
  42.         WritePixelFast n,y,argb,BackBuffer()
  43.         Next
  44. EndIf
  45. End Function


Comments :


big10p(Posted 1+ years ago)

 Is this even faster than Rect for drawing horz/vert lines, then?


Rob Farley(Posted 1+ years ago)

 Yes.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal