Monday 9 April 2012

Tidy Pointer Use

A quick look at the loop code
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

I have used the names pointDX# and pointDY# rather than simply PointX# and PointY# as a reminder that the positions are using the display coordinate system, you will notice that I have also added a couple more called pointWX# and pointWY# while I was at it.

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#

To get the values back.  Now all occurrences of getPointerX() are replaced with with PointDX() and occurrences of getPointerY() are replaced with PointDY().

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() ) )

Though there is no difference at the moment, eventually there will be and now the functionality is in place ready.

No comments:

Post a Comment