Best way to create a crosshair?

BlitzPlus Forums/BlitzPlus Beginners Area/Best way to create a crosshair?

I want to have a crosshair similar to the image below but I'm not entirely sure the best way to do it.

*Edit* - It would probably help if I added the pic



Ways I can see are;

A. Use lines and an oval, but even with nothing in the program but these, I get a choppy framerate.

B. Use a large image and offset it so it's position at MouseX() and MouseY()

Or

Make the crosshair from images of lines / an oval. I havn't tried this but it seems like quite a fiddly way to go about it.

I want the lines to always appear to come from the sides of the program, similar to the crosshair you can set up in 3DS Max and similarly in AutoCAD.

Any ideas?

draw the lines with rect (1 pixel wide) as it's quicker. Make the circle in a paint package as a bitmap and load it in and use DrawImage to show it. Turn off debug to see what your real final framerate will be.

*Concurs with Grey Alien*

:-)

Its faster with images all around

and here it is, you asked for it...

change the comments around the rect drawing to see the difference with some combinations and possibly increase the loop amount


Const GW = 800
Const GH = 600

Graphics GW, GH, 0, 2

HidePointer

cv = CreateImage(1, GH, 1, 1)
SetBuffer(ImageBuffer(cv))
Rect 0,0,1,GH,1

ch = CreateImage(GW, 1, 1, 1)
SetBuffer(ImageBuffer(ch))
Rect 0,0,GW,1,1

ret = CreateImage(25,25,1,1)
MidHandle ret
SetBuffer(ImageBuffer(ret))
Oval 0,0,25,25,0


framet = CreateTimer(60)

Repeat
	
	SetBuffer(BackBuffer())
	WaitTimer(framet)
	msx = MouseX()
	msy = MouseY()
	Cls
	ms = MilliSecs()
	For a = 1 To 1000
		
		DrawImage cv, msx, 0
		DrawImage ch, 0, msy
		DrawImage ret, msx, msy
		
		
		;Rect msx, 0, 1, GH, 1
		;Rect 0, msy, GW, 1, 1
		;Oval msx-12, msy-12, 25, 25, 0
		
	Next
	Print (MilliSecs() - ms)
	Flip(False)

Until KeyHit(1)


What's wrong with a crosshair image?

Because I never wanted to see the ends of the lines, the image would have to be twice the size of the graphics window.

I guess it's quicker to use smaller, seperate images than one huge one.

Graphics 800,600,0,2
SetBuffer BackBuffer()
HidePointer()
While Not KeyDown(1)
	Cls
		mx=MouseX():my=MouseY()
			Line 0,my,800,my
			Line mx,0,mx,600
			Oval mx-20,my-20,40,40,0
	Flip
Wend


...... simple.

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

And use an image for the circle

Syco: Like Grey Alien said: "draw the lines with rect (1 pixel wide) as it's quicker"

Drawing a rectangle 1 pixel high (or wide) is faster than drawing a line, since the algorithm is much simpler -- it doesn't have to do any calculations for angles, but can just fill a straight chunk of memory with your color value.

Syco's post does help one understand the idea of what you're trying to do but is over 10X slower than my code. (Even uses lines instead of rects, people don't understand simplifications of code can lead to massive slowdows; i.e. x^2 is slower than x*x)

some whipped-up code to show the dif in speed.
c = 10000000

ms = MilliSecs()
For a = 1 To c
	x=x^2
Next
e =  (MilliSecs()-ms)

ms = MilliSecs()
For a = 1 To c
	x=x*x
Next
r = (MilliSecs()-ms)

Print 1.0*e/r

Input()


It is because things like ^ and Line are generalized and you are forgetting the special cases you are dealing with.


Drawing any things like plot, rect, oval, text, whatever, straight to the backbuffer is just ugly and can lead to slowdowns where images were designed to be used (repetition). My code above is nice and worrying about images that go offscreen should only be a concern for cell phone programming, etc.

Just remove the for loop that runs 1000x repeat and ull see the real speed.