PlotNRect slower than Lines

Blitz3D Forums/Blitz3D Programming/PlotNRect slower than Lines

Tibit(Posted 2004) [#1]
Plot P\X,P\Y
Rect(P\X,P\Y,P\Size,P\Size)

Much slower than

DrawTriangle(P\X,P\Y,P\Dir,P\Size)


Ross C(Posted 2004) [#2]
i've found rect is fast than line, in drawing horizantalk lines or vertical lines. :) Excuse the spelling, just in from the piub :)


Kozmi(Posted 2004) [#3]
That's ok Ross, My spelling is messed up all the time! ;)


big10p(Posted 2004) [#4]
bldsvnbkrvfnjdbknjwqiuiyg

sorryy, I'm juzzzzzt in from the pub toooooooo!


Rottbott(Posted 2004) [#5]
Rect has to be faster, it just does a simple loop instead of using trigonometry.


Rob Farley(Posted 2004) [#6]
If you want fast horizonal and vertical lines I've got one in the code archives

http://www.blitzbasic.com/codearcs/codearcs.php?code=1067


Tibit(Posted 2004) [#7]
To prove it, LINE are much faster or let's just say they give a better FPS

Change the probe number to fit your computer. ( I have a 2400+ )

This program simulates alot of probes. Press space to change how you want to draw them, lines/triangle/rect/oval/image
OVAL is the slowest! one line Fastest

NOTE plot is much slower than line.

Atleast on my computer, but WHY??

AppTitle "ProbeTEST"

	Const WIDTH  = 1200
	Const HEIGHT = 800

	Const SPACE = 57
	Const BACKSPACE = 14
	Const T=20
	Const F=33

 Global TEST=0;Toggle Test Modes
 Global TEXTON=1
 Global FLIPFALSE=0

 Dim GfxImage(361);To hold a rotated image

Graphics(WIDTH,HEIGHT,16,2);Graphics Mode = Window
SetBuffer BackBuffer()

SeedRnd MilliSecs()

	Type Probe;-----------------------------------------
	
		Field Localnet%			;Local or Remote Probe?
		Field X#,Y#			;Possition 
		Field Xvel#,Yvel#		;Velocity
		Field Dir				;Direction
		Field Speed#,Size		;Size
		Field TopSpeed#			;
		Field TurnSpeed%		;
		Field MoveF%,MoveB%		;Action States
		Field TurnR%,TurnL%,Fire% ;Action States
		Field LastX,LastY		;Action States
		
		;Field KeyData%			;Data of keys held down	
		;Field LastKeyData		;
		
	End Type;-------------------------------------------
	
	
  Const ProbeSize=4
  LoadAndRotateanImage(ProbeSize);<< See function for more info



;C r e a t e  ~ 5 0 0  P r o b e s 

;	CreateProbes( NR ,local?, X , Y )

 	CreateProbes( 500,  1);   ,450,450)
	

; - - -  - M - A - I - N -  - L - O - O - P - - - 

While Not KeyDown(1)
Cls
	
	If TEXTON
	
	  If TEST = 0 Text 10,50," No Mode Selected "+" Nothing is drawn  ...  PRESS SPACE ..."	
	  If TEST = 1 Text 10,50," MODE 1 "+" Each probe is a simple RECT"
	  If TEST = 2 Text 10,50," MODE 2 "+" Probe built up by 3 lines (calculated with Cos N Sin)"
	  If TEST = 3 Text 10,50," MODE 3 "+" Use plot Probe\X,Probe\Y to draw the probes" 
	  If TEST = 4 Text 10,50," MODE 4 "+" Oval() to draw the probes" 
	  If TEST = 5 Text 10,50," MODE 5 "+" Now DrawImage (Created in memory at startup )" 
	  If TEST = 6 Text 10,50," MODE 6 "+" I use one line here, From P\Y-1 to P\Y+2.."
	  If TEST = 7 Text 10,50," MODE 7 "+" Rob Farley's FAST Vertical LINE "		
	EndIf
	
	 If TEXTON Text 10,26,"Press SPACE to toggle TEST-Modes,(ENTER backwards), T toggle TEXT , F Toggle FlipFalse "
		
	 If KeyHit( SPACE )     TEST=TEST+1 :If TEST >=  8 TEST = 0
	 If KeyHit( BACKSPACE ) TEST=TEST-1 :If TEST <=  0 TEST = 7
		 
	 If KeyHit( T ) ;This mode shows how much this Text effects the FPS  
		If TEXTON=1 TEXTON=0 Else TEXTON=1
	 EndIf
	
	 If KeyHit( F ) ;Test your computer's reversed limits
		If FLIPFALSE=1 FLIPFALSE=0 Else FLIPFALSE=1
	 EndIf
	
	
		;FPS_Counter      < Runs and displays the FPS
		;--------------------------------------------
			If FPS_Counter_time+1000 =< MilliSecs()
				FPS=FPS_Counter;<- Frames/Sec
				FPS_Counter=0
				FPS_Counter_time=MilliSecs()
			EndIf
			
			FPS_Counter=FPS_Counter+1	
			
			Text 10,80,"Current FPS: "+FPS	
		;--------------------------------------------
	
		
	;				__UpdateProbes__
					 UpdateProbes()
	;				________________
 

	;Flip limits your FPS to your monitors update Frekv, Flips False does not!
	If FLIPFALSE Flip False Else Flip ;)
	
Wend

; - - -  - M - A - I - N -  - L - O - O - P - - - 

Delete Each Probe

End








; D R A W   T R I A N G L E
;-------------------------------------------------------------------
Function DrawTriangle(X#,Y#,Dir%,Size%=5)

	;Calc the triangle coords
	PHeadX#=x+(Cos(Dir)*Size)
	PHeadY#=Y+(Sin(Dir)*Size)
	PLeftX#=X+(Cos(Dir-140)*Size)
	PLeftY#=Y+(Sin(Dir-140)*Size)
	PRightX#=X+(Cos(Dir+140)*Size)
	PRightY#=Y+(Sin(Dir+140)*Size)
	
	; This draw a V triangle
	Line PHeadX#  ,PHeadY#  ,PRightX# ,PRightY#
	Line PHeadX#  ,PHeadY#  ,PLeftX#  ,PLeftY#
	Line PRightX# ,PRightY# ,PLeftX#  ,PLeftY#
	
End Function






; R O T A T E   I M A G E
;-------------------------------------------------------------------
Function LoadAndRotateanImage(ImageSize=25)

	;The following is EXACTLY the same as useing gfxBox=Loadimage(..)
	
	;If you want to load a REAL image
	;gfxBox=LoadImage(Link$);
	
		
	;>>>If you use loadimage() Skip this part below
	
	
		gfx=CreateImage(ImageSize,ImageSize) ;Create one for this example 
		
		;Draw at this new image
		SetBuffer ImageBuffer(gfx)
		Color 0,0,200
		
		; Note the extra space between the box and the edge of the graphic
		Oval ImageSize/5,ImageSize/5,ImageSize*3/5,ImageSize*3/5  ,1
		Color 0,255,0;Green
		Line ImageSize/2,ImageSize/2-ImageSize/5,ImageSize,ImageSize/2-ImageSize/5    
		Line ImageSize/2,ImageSize/2+ImageSize/5,ImageSize,ImageSize/2+ImageSize/5
		;Hard to read above code
		; ()=  <-- this is what I'm drawing
		; a oval with 2 pipes facing right ;)
		; or in common: A Tank
		SetBuffer BackBuffer()
	;>>>>	
		
	;ROTATE AN IMAGE	
	;Make a tempBackup --> rotate the backup
	;Rotate it 360 times and save these images in an array
	;Note: You don't have to rotate 360 times =) 
	
	TFormFilter = 0;Antialiasing OFF( for speed )
	
	gfxTemp=CopyImage(gfx)
	
	MidHandle gfxTemp;Center Image 
	
	For NR = 0 To 361
		GfxImage(NR)=CopyImage(gfxTemp) ;To start off with an Not rotated image each time SAVES alot of TIME!!!
		RotateImage GfxImage(NR),NR ;Rotate NR Degrees
		If GfxImage(NR) = 0 Stop
	Next



End Function






; C R E A T E   P R O B E
;-------------------------------------------------------------------
; ? LocalNet: 1=LocalControl 0=ExternalControl
;
Function CreateProbes(Number%,Local_%=0,X#=0,Y#=0)
	
	For NR = 1 To Number;of Probes
		
		P.Probe = New Probe 
		
		P\LocalNet   = Local_ ;Local_Control?
	
		P\Size       = ProbeSize;Const 	
		P\TopSpeed#  = 4 
		P\TurnSpeed% = 2 
		
		;Possition 
		If X = 0 P\X = Rnd(0,WIDTH)  Else P\X = X#
		If Y = 0 P\Y = Rnd(0,HEIGHT) Else P\Y = Y#
	
	Next 
	
End Function





	
; U P D A T E   P R O B E
;-------------------------------------------------------------------		
Function UpdateProbes()

 Color 0,255,0;RED PROBES
 For P.Probe = Each Probe 

	Select TEST
		
		Case 1
			Rect(P\X,P\Y,P\Size,P\Size)	
		Case 2
			DrawTriangle(P\X,P\Y,P\Dir,P\Size)	
		Case 3
			Plot P\X,P\Y
		Case 4
			Oval(P\X,P\Y,P\Size,P\Size)
		Case 5
			DrawImage(GfxImage(P\Dir),P\X,P\Y)
		Case 6
			Line (P\X-P\Size,P\Y,P\X+P\Size*2,P\Y)
		Case 7
			FLine(P\X-P\Size,P\Y,P\X+P\Size*2,P\Y);<- Con: Only works with vertical/horisontal lines
	End Select	
				
		P\LastX=P\X
		P\LastY=P\Y
		
		P\X = P\X + P\Xvel
		P\Y = P\Y + P\Yvel

  ;Artificial Intelligence______		
  ; ~ Random Seed ~
  If P\Localnet = 1
	

	Rnd_Thrust=Rand(0,500)
	Rnd_Turn=Rand(0,30);how often to turn
	Rnd_Fire=Rand(0,10000)

	Select Rnd_thrust
		Case 1,2,3,4,5,6,7,8,9,10;Chance to go forward 
			P\MoveF=1:P\MoveB=0
	;	Case 50;Backwards
	;		MoveB=1:MoveF=0
	;	Case 20;Chance to Stop 
	;		MoveB=0:MoveF=0
	End Select
	
	Select Rnd_Turn
		Case 3;Right
			P\TurnR=1:P\TurnL=0
		Case 2;Left
			P\TurnL=1:P\TurnR=0
		Case 10;Forward
			P\TurnL=0:P\TurnR=0
	End Select
		
	;FIRE__________
	Select Rnd_Fire
		Case 500
			Fire1=1
		Case 200
			Fire2=1
		Case 300
			Fire3=1
		Case 900
			Fire4=1			
	End Select;____	
	
	;Put the keydown_DATA to a BYTE in real version
	;If Key_data is changed --> send it

  Else;Means it is remote controlled = enemy probe

	;Read key_Data from byte in real version

  EndIf;


  ;UPDATE This probe weather it is local or external


	If P\TurnR = 1 P\dir = P\dir - P\TurnSpeed
	If P\TurnL = 1 P\dir = P\dir + P\TurnSpeed	
	
	If P\dir > 359 P\dir = 0
	If P\dir < 0   P\dir = 359
	
	If P\MoveF = 1 P\Speed# = P\TopSpeed#
	If P\MoveB = 1 P\Speed# = -1*P\TopSpeed#	

	If P\MoveF = 0 And P\MoveB=0  P\Speed# = 0		
			
	P\Xvel = P\Speed#*Cos(P\dir)
	P\Yvel = P\Speed#*Sin(P\dir)
		
	If P\Fire = 1 
;		Color 255,255,255
;		Rect 10,10,WIDTH-10,HEIGHT-10,1
		P\Fire=0
	EndIf
		
		If P\X > WIDTH-10 P\X = P\LastX
		If P\X < 10 P\X = P\LastX	
		If P\Y > HEIGHT-10 P\Y = P\LastY
		If P\Y < 10 P\Y = P\LastY		

 Next;P.Probe

End Function;UPDATE PROBES
;=================================================


;
Function fLine(x,y,x1,y1,r=255,g=255,b=255)
; fLine will only draw horizonal or vertical lines, no diagonals
; Defaults to a white line.
Local t,n,argb

argb=(b Or (g Shl 8) Or (r Shl 16) Or ($ff000000))
If x=x1
	If y>y1 Then t=y1:y1=y:y=t
	For n=y To y1
	WritePixelFast x,n,argb,BackBuffer()
	Next
EndIf

If y=y1
	If x>x1 Then t=x1:x1=x:x=t
	For n=x To x1
	WritePixelFast n,y,argb,BackBuffer()
	Next
EndIf 
End Function





Tibit(Posted 2004) [#8]
Added Rob's FastLine function, it is a bit faster. Noticable but not much. Why don't anyone make a FastRect function?


Ross C(Posted 2004) [#9]
Rect is plenty fast. Probably the fastest drawing command in blitz. Plot is slower than line, because you can only plot on point at a time. The loop to plot multiple points will be slower really.