[2D] Line Drawing using Sprites [ALL PLATFORMS]
Posted: Tue Mar 04, 2025 12:00 am
There is no 2D Line function outside of Start/StopDrawing().
The code uses the TransformSprite() function to create 2D Lines with a Sprite.
It is not perfect but a start
Any ideas to improve this further?
Code:
The code uses the TransformSprite() function to create 2D Lines with a Sprite.
It is not perfect but a start

Any ideas to improve this further?
Code:
Code: Select all
EnableExplicit
;Project: SpriteLines
;Author: Mijikai
;There is no 2D Line function outside of Start/StopDrawing()!
;DrawLine() uses the TransformSprite() function to create 2D Lines with a Sprite.
;It is not perfect but a start :)
Procedure.i DrawLine(X1.i,Y1.i,X2.i,Y2.i,Width.i);<- draw the line
Protected.f w,h,r,s,c,ws,wc,hs,hc
w = Sqr(Pow(X1 - X2,2) + Pow(Y1 - Y2,2))
h = Width / 2.0
r = ATan2(X2 - X1,Y2 - Y1)
s = Sin(r)
c = Cos(r)
ws = w * s
wc = w * c
hs = h * s
hc = h * c
TransformSprite(0,hs,-hc,0,wc + hs,ws - hc,0,wc - hs,ws + hc,0,-hs,hc,0)
DisplayTransparentSprite(0,X1,Y1)
ProcedureReturn #Null
EndProcedure
Procedure.i Render();<- examples
DrawLine(200,200,400,200,1)
DrawLine(200,200,200,400,2)
DrawLine(200,200,400,400,3)
DrawLine(200,200,400,300,8)
DrawLine(200,200,40,100,80)
ProcedureReturn #Null
EndProcedure
Procedure.i Sprite();<- dummy sprite
If CreateSprite(0,16,16,#PB_Sprite_AlphaBlending)
If StartDrawing(SpriteOutput(0))
DrawingMode(#PB_2DDrawing_AllChannels)
Box(0,0,OutputWidth(),OutputHeight(),RGBA(255,0,0,255))
StopDrawing()
ProcedureReturn #True
EndIf
FreeSprite(0)
EndIf
ProcedureReturn #False
EndProcedure
Procedure.i Main();<- main loop
Protected.i exit
If InitSprite()
If OpenWindow(0,0,0,800,600,#Null$,#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
If OpenWindowedScreen(WindowID(0),0,0,WindowWidth(0),WindowHeight(0))
SetFrameRate(60)
If Sprite()
Repeat
Repeat
Select WindowEvent()
Case #PB_Event_None
Break
Case #PB_Event_CloseWindow
exit = #True
EndSelect
ForEver
ClearScreen($0)
Render()
ZoomSprite(0,#PB_Default,#PB_Default)
DisplayTransparentSprite(0,200 - 8,200 - 8,255,RGBA(0,255,0,255))
FlipBuffers()
Until exit
EndIf
CloseScreen()
EndIf
CloseWindow(0)
EndIf
EndIf
ProcedureReturn #Null
EndProcedure
End Main()