AutoScale - AutoFit alternative

Monkey Forums/Monkey Code/AutoScale - AutoFit alternative

Nobuyuki(Posted 2013) [#1]
Back when I first wrote this over a year ago, AutoFit didn't have all of the features that I wanted to use in my app, or it didn't apply the features properly. I decided to upload this after making some changes to the code recently to more efficiently support devices and targets where the dimensions can be resized at runtime. Today, I'm presuming that AutoFit can do most of the things AutoScale set out to do, but the features I had in mind when creating AutoScale were the following:

1. Aspect ratio preserving (and ignoring) resize ability
2. Sliding aspect-ratio preserved canvases to the center
3. Touch-based delta input values, in addition to mouse-based
4. a DeviceX() and DeviceY() function that returned values relative to DeviceWidth() and DeviceHeight(), in percentages, even when the rest of the screen is already scaled
5. Some way to apply letterboxing, when necessary.

The rest of the differences with AutoFit are mostly stylistic.



How to Use
InitAutoScale(width, height) should be called OnCreate, and ScaleScreen() should be called at the beginning of OnRender. ScreenWidth and ScreenHeight are the virtual screen dimensions. All dimensions in your app can be hardcoded relative to positions in your virtual screen, and will be scaled accordingly, but for mouse clicks and touches to register in the correct location, always poll dTouch or dMouse functions instead of the default ones. You can also use SetScaledScissor to perform a scissor operation in the virtual screen space.

No scissoring is done to provide any letterbox effect in AutoScale by default -- This may be a desired effect for some users. However, you can add your own letterbox effect with the new RenderLetterBox() function. Call it after all of your other rendering operations, supply it an instance of a LetterBoxer, and it automagically fills in the correct area.

There are 3 built-in types which extend LetterBoxer that are included: Color bars, stretched "book-end" images, and a tiled background. You can also make your own completely custom LetterBoxer -- the render routine gives you the correct draw rectangle and side, and SlideAxis will tell you how your letterbox should be oriented. Updates will be coming soon to make BookEndLetterBox be SlideAxis-aware.

Source code:
https://gist.github.com/nobuyukinyuu/8348382


DiTransfers(Posted 2013) [#2]
Awesome Thank You


Nobuyuki(Posted 2013) [#3]
Here is an improved version of AutoScale which fixes the problems with using sloppy stretch mode. There are some functionality-breaking changes in here (function signatures have changed), so if you've used AutoScale for your previous project, don't upgrade unless you're ready to change your references to the functions. Notably, PreserveAspectRatio is now a Global, and not an argument. This was necessary to facilitate behavior that was aspect-ratio dependent.

In addition, a new function SetScaledScissor is now part of the module. This allows a very convenient way to make a scissor region inside the virtual screen, and is something AutoFit doesn't currently support (see edit below). Enjoy.



Edit: haha, I should really read the AutoFit thread. Someone already implemented this a year ago, but it is not official.


SHiLLSiT(Posted 2013) [#4]
Nobuyuki: You pointed me to this module in another thread. Its been working really great, except that I just noticed an issue:


The origin of the sprite is at it's top-left corner which is represented by the bottom set of coordinates. The first set of coordinates represent the output of the DeviceX/DeviceY functions like so: DeviceX(0):DeviceX(1), DeviceY(0):DeviceY(1).

According to what's being printed on the screen, the sprite is currently at the screen origin when it indeed is not.

EDIT: Derp, once again the problem was on my end!


slenkar(Posted 2013) [#5]
the scissoring functions work very well thanks


Nobuyuki(Posted 2014) [#6]
Very few people used the first public version of AutoScale, and the method signatures seem to have stabilized, so I've taken down the v1 code and replaced it with a link to Gist, which will always be up to date.

In addition, I've finally added custom letterboxing support to AutoScale. Now you can specify a custom letterbox to adapt your game to different aspect ratios easily, from the classic black bars to more elaborate backgrounds. The original posting has been updated accordingly to reflect this.


Supertino(Posted 2014) [#7]
Thank Nobuyuki I use your AutoFit so it's good to see some additions. Thanks again, I'll have a play with the new custom letter boxing.


slenkar(Posted 2014) [#8]
thanks for the letter boxing


Sledge(Posted 2015) [#9]
This is ace, BTW -- many thanks!