Skip to content

Instantly share code, notes, and snippets.

@SirGordon
Created August 14, 2016 17:22
Show Gist options
  • Save SirGordon/768d6103c9ae1fb48ba550df09fa1914 to your computer and use it in GitHub Desktop.
Save SirGordon/768d6103c9ae1fb48ba550df09fa1914 to your computer and use it in GitHub Desktop.
// simple easy shared access to callback and current attempted profile
private static var supportsCallback:Function;
private static var checkingProfile:String;
public static function supportsProfile(nativeStage:flash.display.Stage, profile:String, callback:Function):void
{
supportsCallback = callback;
checkingProfile = profile;
if (nativeStage.stage3Ds.length > 0) {
var stage3D:Stage3D = nativeStage.stage3Ds[0];
stage3D.addEventListener( flash.events.Event.CONTEXT3D_CREATE, supportsProfileContextCreatedListener, false, 10, true);
stage3D.addEventListener(ErrorEvent.ERROR, supportsProfileContextErroredListener, false, 10, true);
try
{
stage3D.requestContext3D("auto", profile);
}
catch (e:Error)
{
stage3D.removeEventListener( flash.events.Event.CONTEXT3D_CREATE, supportsProfileContextCreatedListener);
stage3D.removeEventListener( ErrorEvent.ERROR, supportsProfileContextErroredListener);
if (supportsCallback) supportsCallback(checkingProfile, false);
}
}
else {
// no Stage3D instances
if (supportsCallback) supportsCallback(checkingProfile, false);
}
}
private static function supportsProfileContextErroredListener(event:flash.events.ErrorEvent):void
{
var targetStage3D:Stage3D = event.target as Stage3D;
if (targetStage3D) {
targetStage3D.removeEventListener( flash.events.Event.CONTEXT3D_CREATE, supportsProfileContextCreatedListener);
targetStage3D.removeEventListener( ErrorEvent.ERROR, supportsProfileContextErroredListener);
}
if (supportsCallback) supportsCallback(checkingProfile, false);
}
private static function supportsProfileContextCreatedListener(event:flash.events.Event):void
{
var targetStage3D:Stage3D = event.target as Stage3D;
if (targetStage3D) {
targetStage3D.removeEventListener( flash.events.Event.CONTEXT3D_CREATE, supportsProfileContextCreatedListener);
targetStage3D.removeEventListener( ErrorEvent.ERROR, supportsProfileContextErroredListener);
if (targetStage3D.context3D) {
// the context is recreated as long as there are listeners on it, but there shouldn't be here.
// Beginning with AIR 3.6, we can guarantee that with an additional parameter of false.
var disposeContext3D:Function = targetStage3D.context3D.dispose;
if (disposeContext3D.length == 1) disposeContext3D(false);
else disposeContext3D();
if (supportsCallback) supportsCallback(checkingProfile, true);
}
}
}
private function onContextProfileChecked( profile:String, success:Boolean):void
{
if (success) {
...
mStarling = new Starling(Game, stage, null, null, Context3DRenderMode.AUTO, profile);
...
}
else if (profile == Context3DProfile.BASELINE_EXTENDED) {
// a delayed call has to be used, otherwise it seems current Stage3D is messed up
// if you try and create a new request for a Context3D with a different profile too soon
setTimeout(Main.supportsProfile, 100, this.stage, Context3DProfile.BASELINE, onContextProfileChecked);
}
else if (profile == Context3DProfile.BASELINE) {
// a delayed call has to be used, otherwise it seems current Stage3D is messed up
// if you try and create a new request for a Context3D with a different profile too soon
setTimeout(Main.supportsProfile, 100, this.stage, Context3DProfile.BASELINE_CONSTRAINED, onContextProfileChecked);
}
else {
// this would imply Context3D failed on BASELINE_CONSTRAINED or a profile string we didn't quite expect
throw new ArgumentError("Failed to create a Context3D profile: " + profile);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment