Posted: 12th Sep 2022 11:41
I have the following code that I use to scale a sprite to a window size for drawing purposes etc (that works)
+ Code Snippet
locX=getRawMouseX():locY=getRawMouseY()
dx=locX*canvasSize/windowSize:dy=locY*canvasSize/windowSize


The problem is I want to be able to use view zoom with the scaling and select a window on screen and do something like

+ Code Snippet
setviewoffset(a,b)
z=(SIN(getrawmousewheel()*.5))*(GetRawMouseWheel()*.5)
SetViewZoom(z)
//this works but a point to note it begins at 0,0 scale 


worldtoscreen and screentoworld will not work, at-least not the way I have been trying to use them, so I'm hoping a math Guru may
be able to point me in the direction where i could perhaps use those with an already scaled window. It messes up worse as the scales
get further distant from original
Posted: 12th Sep 2022 13:46
Will this help?


+ Code Snippet
// Project: WindowZoom 
// Created: 22-09-12

// show all errors

SetErrorMode(2)

// set window properties
SetWindowTitle( "WindowZoom" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window

// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 )


global renderImage as integer
global renderSprite as integer
global zoomValue as float


renderImage = CreateRenderImage(GetVirtualWidth(), GetVirtualHeight(), 0, 0)
renderSprite = CreateSprite(renderImage)

topLeft = 0.0
topRight = 0.0
zoomValue = 10.0


SetSpriteSize(renderSprite, 10, 10)
SetSpritePosition(renderSprite, (GetVirtualWidth() / 2) - (GetSpriteWidth(renderSprite) / 2), (GetVirtualHeight() / 2) - (GetSpriteHeight(renderSprite) / 2))

DrawToRenderImage()


do
	if GetRawKeyReleased(27) then exit
	
    Zoom(zoomValue)
    Sync()
    
loop


end




function DrawToRenderImage()
	local x1, y1
	local x2, y2
	local color as integer
	
	SetRenderToImage(renderImage, 0)
	
	color = MakeColor(255, 0, 0)
	x1 = 0
	y1 = 0
	x2 = x1 + (GetWindowWidth() / 2)
	y2 = y1 + (GetVirtualHeight() / 2)
	
	DrawBox(x1, y1, x2, y2, color, color, color, color, 1)
	
	color = MakeColor(0, 255, 0)
	x1 = (GetVirtualWidth() / 2)
	y1 = 0
	x2 = x1 + (GetWindowWidth() / 2)
	y2 = y1 + (GetVirtualHeight() / 2)
	
	DrawBox(x1, y1, x2, y2, color, color, color, color, 1)
	
	color = MakeColor(0, 0, 255)
	x1 = 0
	y1 = (GetVirtualHeight() / 2)
	x2 = x1 + (GetWindowWidth() / 2)
	y2 = y1 + (GetVirtualHeight() / 2)
	
	DrawBox(x1, y1, x2, y2, color, color, color, color, 1)
	
	color = MakeColor(255, 255, 255)
	x1 = (GetVirtualWidth() / 2)
	y1 = (GetVirtualHeight() / 2)
	x2 = x1 + (GetWindowWidth() / 2)
	y2 = y1 + (GetVirtualHeight() / 2)
	
	DrawBox(x1, y1, x2, y2, color, color, color, color, 1)
	
	color = MakeColor(255, 0, 255)
	x1 = (GetVirtualWidth() / 2) - (GetVirtualWidth() / 4)
	y1 = (GetVirtualHeight() / 2) - (GetVirtualHeight() / 4)
	x2 = x1 + (GetWindowWidth() / 2)
	y2 = y1 + (GetVirtualHeight() / 2)
	
	DrawBox(x1, y1, x2, y2, color, color, color, color, 1)
	
	SetRenderToScreen()
	
endfunction


function Zoom(value as float)
	local width as float
	local height as float
	local posX as float
	local posY as float
	
	width = GetSpriteWidth(renderSprite)
	height = GetSpriteHeight(renderSprite)
	
	inc width, value
	inc height, value
	
	SetSpriteSize(renderSprite, width, height)
	
	posX = (GetWindowWidth() / 2 - (GetSpriteWidth(renderSprite) / 2))
	posY = (GetWindowHeight() / 2 - (GetSpriteHeight(renderSprite) / 2))
	
	SetSpritePosition(renderSprite, posX, posY)
	
endfunction

Posted: 12th Sep 2022 19:51
Thanks Steve thats a clever bit of code but not quite the math fix i was after

The below snippet is capable of zooming into anywhere on screen the main issue as I use the scale command ie setspritescale(spr,4,4) already in my code
as i restrict to a smaller size for later conversions. The world view and screen view become quite different, this is noticeable by the mouse pointer
being different than a sprite im holding. which correct positioning can be achieved by my first scale formula until i begin to try to set a view which throws out
the math "dx=locX*canvasSize/windowSize:dy=locY*canvasSize/windowSize" which works until I attempt to change viewoffset. Im thinking the align
world function example in the help files function with some modification may help. Ottherwise my color picking functions fail once zoomed and the
drawing becomes way off view.


+ Code Snippet
SetVirtualResolution(1024,768)
type pos
	x as float
	y as float
endtype
//load background image - change to your own background image
LoadImage (1, "Bg.png")

//set background sprite
BG = CreateSprite (1)
SetSpritesize(Bg,GetVirtualWidth(),getvirtualHeight())
SetViewZoomMode(0)
//main loop
Do
   
    AlignWorld( getSpriteXByOffset( bg) , getSpriteXByOffset( bg ) , getVirtualWidth() * 0.5 , getVirtualHeight() * 0.5 , 2.0 )
    SetViewOffset (512,0)
    
	sync()
Loop

// Function to Align a point on the world plane with a point on the
// display plane at a given zoom level.
//
// Parameters: 
//   worldX# , worldY#       Point on world plane to align
//   displayX# , displayY#   Point on Display to Align to
//   worldZ#                 Zoom Level to align at
//

function AlignWorld( worldX# , worldY# , displayX# , displayY#, worldZ# )
  setViewZoom( worldZ# )
  setViewOffset( worldX# - ( displayX# / worldZ# ) , worldY# - ( displayY# / worldZ# ) )
endfunction


function SetSpritePixelSizeInPercentage(spriteID as integer, pixelWidth as float, pixelHeight as float)
    SetSpriteSize(spriteID, (pixelWidth/GetDeviceWidth())*100, (pixelHeight/GetDeviceHeight())*100)
endfunction

function getPositionAsPercentage(spr,x as integer,y as integer)
	pos as pos
	pos.x=getspriteX(spr)/GetVirtualWidth()
	pos.y=getspriteY(spr)/GetVirtualHeight()
endfunction pos



Edit..all good i managed a fix that works for my desires thanks again