Thanks CJB,
That works a treat!
It's these numbers that concerned me:
x = x + ((GetVirtualWidth()-640.0)-8)/4.0
y = y + (GetVirtualHeight()-480.0)/4.0
Which I had taken from Preben's code when I moved it from here:
SetZoomViewArea(100+ ( ((GetVirtualWidth()-640.0)-8) /4.0), 150+ ((GetVirtualHeight()-480.0)/4.0), 100, 100)
With your implementation of the FocusCameraOnSprite() function the above bit of code doesn't need to be there so all of the 'nasty' hard-coded numbers can be removed.
Thanks again, much appreciated!
And for anyone else that stumbles across this whilst looking for something similar, here is the final working implementation:
#constant KEY_SHIFT 16
#constant KEY_SPACE 32
type tDisplay
l as integer
t as integer
r as integer
b as integer
w as integer
h as integer
cx as integer
cy as integer
endtype
type tPoint
x as float
y as float
endtype
type tZoom
view as tDisplay
endtype
restart:
sync()
w = random(500, 1000)
h = random(500, 1000)
SetVirtualResolution(w, h)
SetWindowSize(w, h, 0)
SetSyncRate(60, 0)
SetPrintSize(14)
UseNewDefaultFonts(1)
global spr as integer[2]
global zoom as tZoom
spr[0] = CreateSprite(0)
SetSpriteColor(spr[0], 255, 0, 0, 64)
SetSpriteSize(spr[0], 50, 50)
SetSpritePosition(spr[0], 300, 300)
spr[1] = CreateSprite(0)
SetSpriteColor(spr[1], 0, 255, 0, 64)
SetSpriteSize(spr[1], 100, 100)
SetSpritePosition(spr[1], 100, 200)
spr[2] = CreateSprite(0)
SetSpriteColor(spr[2], 0, 0, 255, 64)
SetSpriteSize(spr[2], 150, 150)
SetSpritePosition(spr[2], 200, 100)
sz = random(50, 150)
SetZoomViewArea(random(10, 400), random(10, 400), sz, sz)
global viewOffset as tPoint
global viewZoom as float
global currentSprite as integer
SetViewZoomMode(1)
c = MakeColor(255, 255, 255)
do
DrawBox(zoom.view.l, zoom.view.t, zoom.view.r, zoom.view.b, c, c, c, c, 0)
GetUserInput()
print("Click and drag mouse to change offset")
print("Press 1 to zoom into RED square")
print("Press 2 to zoom into GREEN square")
print("Press 3 to zoom into BLUE square")
print("")
print("Zoom: "+str(GetViewZoom()))
print("Offset: "+str(GetViewOffsetX())+", "+str(GetViewOffsetY()))
print("Virutal Width: "+str(GetVirtualWidth()))
sync()
loop
function GetUserInput()
if GetRawMouseLeftPressed()
viewOffset.x = GetPointerX()
viewOffset.y = GetPointerY()
endif
if GetRawMouseLeftState()
SetViewOffset(GetViewOffsetX() + GetPointerX() - viewOffset.x, GetViewOffsetY() + GetPointerY() - viewOffset.y)
viewOffset.x = GetPointerX()
viewOffset.y = GetPointerY()
endif
mwdelta# = GetRawMouseWheelDelta()
if mwdelta# <> 0.0
if GetRawKeyState(KEY_SHIFT) then mwdelta# = mwdelta#*3.5
SetViewZoom(GetViewZoom()+mwdelta#*0.01)
endif
for k = 49 to 51
if GetRawKeyPressed(k)
currentSprite = k-49
FocusCameraOnSprite(spr[k-49])
endif
next k
if GetRawKeyPressed(KEY_SPACE) then goto restart
endfunction
function SetZoomViewArea(x, y, w, h)
zoom.view.l = x
zoom.view.t = y
zoom.view.w = w
zoom.view.h = h
zoom.view.r = x+w
zoom.view.b = y+h
zoom.view.cx = x+(w/2)
zoom.view.cy = y+(h/2)
endfunction
function FocusCameraOnSprite(s)
SetViewZoom(zoom.view.w/GetSpriteWidth(s))
gvz#=zoom.view.w/GetSpriteWidth(s)
diffx# = ((GetVirtualWidth()/2.0) - zoom.view.cx)/gvz#
diffy# = ((GetVirtualHeight()/2.0) - zoom.view.cy)/gvz#
SetViewOffset(GetSpriteXByOffset(s)-(GetVirtualWidth()/2.0)+diffx#, GetSpriteYByOffset(s)-(GetVirtualHeight()/2.0)+diffy#)
endfunction