Skip to content

Instantly share code, notes, and snippets.

@esidegallery
Last active April 13, 2023 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esidegallery/9e25b12bbfb5dcdeb3fdf4e9be2802b6 to your computer and use it in GitHub Desktop.
Save esidegallery/9e25b12bbfb5dcdeb3fdf4e9be2802b6 to your computer and use it in GitHub Desktop.
starling.display::DisplayObject.drawToBitmapData() Windows Fix
override public function drawToBitmapData(out:BitmapData = null, color:uint = 0, alpha:Number = 0.0):BitmapData
{
var painter:Painter = Starling.painter;
var stage:Stage = Starling.current.stage;
var viewPort:Rectangle = Starling.current.viewPort;
var stageWidth:Number = stage.stageWidth;
var stageHeight:Number = stage.stageHeight;
var scaleX:Number = viewPort.width / stageWidth;
var scaleY:Number = viewPort.height / stageHeight;
var backBufferScale:Number = painter.backBufferScaleFactor;
var totalScaleX:Number = scaleX * backBufferScale;
var totalScaleY:Number = scaleY * backBufferScale;
var projectionX:Number, projectionY:Number;
var bounds:Rectangle;
if (this is Stage)
{
projectionX = viewPort.x < 0 ? -viewPort.x / scaleX : 0.0;
projectionY = viewPort.y < 0 ? -viewPort.y / scaleY : 0.0;
out ||= new BitmapData(
painter.backBufferWidth * backBufferScale,
painter.backBufferHeight * backBufferScale);
}
else
{
bounds = getBounds(parent, Pool.getRectangle());
projectionX = bounds.x;
projectionY = bounds.y;
out ||= new BitmapData(
Math.ceil(bounds.width * totalScaleX),
Math.ceil(bounds.height * totalScaleY));
}
color = Color.multiply(color, alpha); // premultiply alpha
painter.pushState();
painter.setupContextDefaults();
painter.state.renderTarget = null;
painter.state.setModelviewMatricesToIdentity();
painter.setStateTo(transformationMatrix);
// Images that are bigger than the current back buffer are drawn in multiple steps.
var stepX:Number;
var stepY:Number = projectionY;
var stepWidth:int = painter.backBufferWidth / scaleX;
var stepHeight:int = painter.backBufferHeight / scaleY;
var positionInBitmap:Point = Pool.getPoint(0, 0);
var boundsInBuffer:Rectangle = Pool.getRectangle(
0, 0,
Math.floor(painter.backBufferWidth * backBufferScale),
Math.floor(painter.backBufferHeight * backBufferScale));
while (positionInBitmap.y < out.height)
{
stepX = projectionX;
positionInBitmap.x = 0;
while (positionInBitmap.x < out.width)
{
painter.clear(color, alpha);
painter.state.setProjectionMatrix(
stepX, stepY, stepWidth, stepHeight,
stageWidth, stageHeight, stage.cameraPosition);
if (mask)
{
painter.drawMask(mask, this);
}
if (filter)
{
filter.render(painter);
}
else
{
render(painter);
}
if (mask)
{
painter.eraseMask(mask, this);
}
painter.finishMeshBatch();
// For some reason the bitmapdata is distorted depending the size of the stageHeight and stageWidth on windows. Throwing in an additional bitmapdata and using copyPixels method fixes it.
var bmd:BitmapData = new BitmapData(Math.ceil(stepWidth * backBufferScale), Math.ceil(stepHeight * backBufferScale), true, 0x00ffffff);
painter.context.drawToBitmapData(bmd, boundsInBuffer);
out.copyPixels(bmd, boundsInBuffer, positionInBitmap);
stepX += stepWidth;
positionInBitmap.x += Math.floor(stepWidth * totalScaleX);
}
stepY += stepHeight;
positionInBitmap.y += Math.floor(stepHeight * totalScaleY);
}
painter.popState();
Pool.putRectangle(bounds);
Pool.putRectangle(boundsInBuffer);
Pool.putPoint(positionInBitmap);
return out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment