Quote: "I'm making apps that can run across multiple devices and platforms, and may involve numerous entities to resize, this kind of idea..."
Here are the
generic formulas that I use (percentage system):
Scaling a sprite to its original size
NormalScale# = 100.0 / GetDeviceWidth()
SetSpriteScale(spr, NormalScale#, NormalScale#)
Scaling to fit on any screen (may result in stretching of the sprite)
// The resolution where the sprite will appear normally e.g 1024x600
BaseWidth# = 1024.0
BaseHeight# = 600.0
ScaleX# = ScreenWidth# / BaseWidth#
ScaleY# = ScreenHeight# / BaseHeight#
// Assuming the above scaling hasn't been performed
SetSpriteScale(spr, ScaleX# * NormalScale#, ScaleY# * NormalScale#)
Example code:
rem
rem AGK Application
rem
// Get and store the Screen Width
Global ScreenWidth# as float
ScreenWidth# = GetDeviceWidth()
// Get and store the Screen Height
Global ScreenHeight# as float
ScreenHeight# = GetDeviceHeight()
// Define our base width and height
BaseWidth# = 480.0
BaseHeight# = 480.0
// Set the display aspect according to the screen/device's resolution
SetDisplayAspect( ScreenWidth# / ScreenHeight# )
// The ratio to scale the sprites in order to return them to their normal size
Global NormalSizeRatio# as float
NormalSizeRatio# = 100.0 / ScreenWidth#
// Calculate the scaling values. These will scale the sprite to appear in the same ratio
// the screen resolution is to the base resolution.
ScaleRatioX# = ScreenWidth# / BaseWidth#
ScaleRatioY# = ScreenHeight# / BaseHeight#
// Load an image and create a sprite
Mysprite = CreateSprite( LoadImage( "redcircle.png"))
// Scale the sprite
SetSpriteScale( MySprite, NormalSizeRatio# * ScaleRatioX#, NormalSizeRatio# * ScaleRatioY#)
// Main loop
Do
sync()
Loop
Try out different resolutions via setup.agc file. Note: if you don't like the stretched sprite effect, just scale the sprite using ScaleRatioX# for the x and y parameters. It might not make too much of a difference.
But with the button, it looks like you want it to appear the same size on different resolutions? Try this:
SetSpriteSize(Spr, ImageWidth(GetSpriteImageID(Spr)) / ScreenWidth# * 100, -1)
The sprite should appear the same size regardless of the resolution.