Shows we are now accessing the pointer position using getPointerX() and getPointerY(), quite a number of times. It would be better to access it just the once and store the values in a variable.
The best place to do this is at the start of the loop, after the screen was updated in the previous loop. For neatness, we can incorporate this into our existing LoopStart() function. But to do this, the variable we use has to be global.
For now, we can pop a couple of extra fields called pointDX#, pointDY# into our display UDT, so that it looks like this.
type displayType
physWidth#
physHeight#
virtWidth#
virtHeight#
iconSize#
textHeight#
lastFPS#
iconImage
pointDX#
pointDY#
pointWX#
pointWY#
endtype
And of course, to keep everything neat, these have their own return functions at the end.
function PointDX()
endfunction display.pointDX#
function PointDY()
endfunction display.pointDY#
function PointWX()
endfunction display.pointWX#
function PointWY()
endfunction display.pointWY#
I mention this before modifying the LoopStart() function so you could use the editor's search and replace function and tell it to do them all.
Now add the following lines to LoopStart().
display.pointDX = getPointerX()
display.pointDY = getPointerY()
display.pointWX = ScreenToWorldX(PointDX())
display.pointWY = ScreenToWorldY(PointDY())
The first two lines simply get the pointer X and Y values and store them in the corresponding fields.
The second two lines take these fields and using the screenToWorldX() and screenToWorldY() commands, convert these from display coordinates to world coordinates.
So while we are at it, now change the lines;
printc( "Pointer: " )
print( str( PointDX() ) + " x " + str( PointDY() ) )
to
printc( "Pointer (Display): " )
print( str( PointDX() ) + " x " + str( PointDY() ) )
and add
printc( "Pointer (World): " )
print( str( PointWX() ) + " x " + str( PointWY() ) )
No comments:
Post a Comment