Skip to content

Instantly share code, notes, and snippets.

@tommyettinger
Created August 2, 2014 05:53
Show Gist options
  • Save tommyettinger/38e0fa337f5804b67bde to your computer and use it in GitHub Desktop.
Save tommyettinger/38e0fa337f5804b67bde to your computer and use it in GitHub Desktop.
Viewport that should only stretch in integer increments, but doesn't
package commanders.unite.utils
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.utils.viewport.Viewport
;
/**
* Created by Tommy Ettinger on 8/1/2014.
*/
class CustomViewport(var minWorldWidth:Float,
var minWorldHeight:Float,
var maxWorldWidth:Float = 1280f,
var maxWorldHeight:Float = 720f,
var cam:Camera = new OrthographicCamera()) extends Viewport
{
camera = cam
override def update (screenWidth:Int, screenHeight:Int, centerCamera:Boolean) {
worldWidth = minWorldWidth
worldHeight = minWorldHeight
viewportWidth = (worldWidth * Math.floor(screenWidth / worldWidth)).toInt
viewportHeight = (worldHeight * Math.floor(screenHeight / worldHeight)).toInt
viewportX = (screenWidth - viewportWidth) / 2
viewportY = (screenHeight - viewportHeight) / 2
super.update(screenWidth, screenHeight, centerCamera)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment