Skip to content

Instantly share code, notes, and snippets.

@ibilon
Last active August 29, 2015 14:26
Show Gist options
  • Save ibilon/1c1d9aefe7ab633d9305 to your computer and use it in GitHub Desktop.
Save ibilon/1c1d9aefe7ab633d9305 to your computer and use it in GitHub Desktop.
HaxePunk maintain ratio
import com.haxepunk.Engine;
import com.haxepunk.HXP;
import com.haxepunk.utils.Draw;
class Main extends Engine
{
override public function init()
{
#if debug
HXP.console.enable();
#end
HXP.scene = new MainScene();
}
@:access(com.haxepunk.HXP)
override private function resize()
{
var sw = HXP.stage.stageWidth / baseContainerWidth;
var sh = HXP.stage.stageHeight / baseContainerHeight;
if (baseContainerHeight * sw > HXP.stage.stageHeight)
{
HXP.screen.scale = sh;
HXP.screen.x = Std.int((HXP.stage.stageWidth - baseContainerWidth * sh) / 2);
HXP.screen.y = 0;
}
else
{
HXP.screen.scale = sw;
HXP.screen.x = 0;
HXP.screen.y = Std.int((HXP.stage.stageHeight - baseContainerHeight * sw) / 2);
}
HXP.windowWidth = HXP.stage.stageWidth;
HXP.windowHeight = HXP.stage.stageHeight;
HXP.resize(HXP.stage.stageWidth, HXP.stage.stageHeight);
HXP.width = baseContainerWidth;
HXP.height = baseContainerHeight;
HXP.halfWidth = baseContainerWidth / 2;
HXP.halfHeight = baseContainerHeight / 2;
}
public override function render()
{
super.render();
if (HXP.screen.x > 0)
{
Draw.rect(0, 0, HXP.screen.x, HXP.stage.stageHeight, 0);
Draw.rect(HXP.stage.stageWidth - HXP.screen.x, 0, HXP.screen.x, HXP.stage.stageHeight, 0);
}
else if (HXP.screen.y > 0)
{
Draw.rect(0, 0, HXP.stage.stageWidth, HXP.screen.y, 0);
Draw.rect(0, HXP.stage.stageHeight - HXP.screen.y, HXP.stage.stageWidth, HXP.screen.y, 0);
}
}
private static inline var baseContainerWidth:Int = 800;
private static inline var baseContainerHeight:Int = 600;
public static function main() { new Main(); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment