Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/openfl/display/Graphics.hx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ import js.html.CanvasRenderingContext2D;
#end
@:noCompletion private var __bitmap:BitmapData;
@:noCompletion private var __bitmapScale:Float;
@:noCompletion private var __hasTilemap:Bool;

@:noCompletion private function new(owner:DisplayObject)
{
Expand Down Expand Up @@ -200,6 +201,27 @@ import js.html.CanvasRenderingContext2D;
if (alpha > 0) __visible = true;
}

/**
* Fills the drawing area with a tilemap.
* @param tilemap the tilemap to fill.
*/
public function beginTilemapFill(tilemap:Tilemap):Void
{
tilemap.__update(false, true);

var xSign = tilemap.width < 0 ? -1 : 1;
var ySign = tilemap.height < 0 ? -1 : 1;

__inflateBounds(tilemap.x - __strokePadding * xSign, tilemap.y - __strokePadding * ySign);
__inflateBounds(tilemap.x + tilemap.width + __strokePadding * xSign, tilemap.y + tilemap.height + __strokePadding * ySign);

__commands.beginTilemapFill(tilemap);

__dirty = true;
__visible = true;
__hasTilemap = true;
}

/**
Specifies a gradient fill used by subsequent calls to other Graphics
methods (such as `lineTo()` or `drawCircle()`) for
Expand Down Expand Up @@ -654,6 +676,7 @@ import js.html.CanvasRenderingContext2D;
var path:GraphicsPath;
var trianglePath:GraphicsTrianglePath;
var quadPath:GraphicsQuadPath;
var tilemapFill:GraphicsTilemapFill;

for (graphics in graphicsData)
{
Expand Down Expand Up @@ -728,6 +751,9 @@ import js.html.CanvasRenderingContext2D;
case QUAD_PATH:
quadPath = cast graphics;
drawQuads(quadPath.rects, quadPath.indices, quadPath.transforms);
case TILEMAP:
tilemapFill = cast graphics;
beginTilemapFill(tilemapFill.tilemap);
}
}
}
Expand Down
44 changes: 44 additions & 0 deletions src/openfl/display/GraphicsTilemapFill.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package openfl.display;

#if !flash
import openfl.display._internal.GraphicsDataType;
import openfl.display._internal.GraphicsFillType;
import openfl.geom.Matrix;

/**
Defines a bitmap fill. The bitmap can be smoothed, repeated or tiled to
fill the area; or manipulated using a transformation matrix.
Use a GraphicsBitmapFill object with the `Graphics.drawGraphicsData()`
method. Drawing a GraphicsBitmapFill object is the equivalent of calling
the `Graphics.beginBitmapFill()` method.
**/
#if !openfl_debug
@:fileXml('tags="haxe,release"')
@:noDebug
#end
@:final class GraphicsTilemapFill implements IGraphicsData implements IGraphicsFill
{
/**
The tilemap to fill.
**/
public var tilemap:Tilemap;

@:noCompletion private var __graphicsDataType(default, null):GraphicsDataType;
@:noCompletion private var __graphicsFillType(default, null):GraphicsFillType;

/**
Creates a new GraphicsBitmapFill object.

@param tilemap A tilemap that contains tiles to render.
**/
public function new(tilemap:Tilemap = null)
{
this.tilemap = tilemap;

this.__graphicsDataType = TILEMAP;
this.__graphicsFillType = TILEMAP_FILL;
}
}
// #else
// typedef GraphicsBitmapFill = Dynamic;
#end
35 changes: 34 additions & 1 deletion src/openfl/display/_internal/Context3DGraphics.hx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Context3DGraphics
private static var blankBitmapData = new BitmapData(1, 1, false, 0);
private static var maskRender:Bool;
private static var tempColorTransform = new ColorTransform(1, 1, 1, 1, 0, 0, 0, 0);
private static var __tileRenderer:OpenGLRenderer;

private static function buildBuffer(graphics:Graphics, renderer:OpenGLRenderer):Void
{
Expand Down Expand Up @@ -84,6 +85,12 @@ class Context3DGraphics
}
}

case BEGIN_TILEMAP_FILL:
var c = data.readBeginTilemapFill();
c.tilemap;
// Context3DTilemap.buildBuffer(c.tilemap, renderer);
// data.skip(type);

case DRAW_QUADS:
// TODO: Other fill types

Expand Down Expand Up @@ -428,6 +435,9 @@ class Context3DGraphics
return false;
}

case BEGIN_TILEMAP_FILL:
data.skip(type);

case DRAW_RECT:
if (hasColorFill)
{
Expand Down Expand Up @@ -532,7 +542,7 @@ class Context3DGraphics
var width = graphics.__width;
var height = graphics.__height;

if (bounds != null && width >= 1 && height >= 1)
if (bounds != null && ((width >= 1 && height >= 1) || graphics.__hasTilemap))
{
if (graphics.__hardwareDirty
|| (graphics.__quadBuffer == null && graphics.__vertexBuffer == null && graphics.__vertexBufferUVT == null))
Expand Down Expand Up @@ -599,6 +609,29 @@ class Context3DGraphics

fill = null;

case BEGIN_TILEMAP_FILL:
var c = data.readBeginTilemapFill();
// giving the tilemap it's own renderer MAY allow for less gl events to switch between the states made by the Context3DGraphics and Context3DTilemap
if (__tileRenderer == null) __tileRenderer = new OpenGLRenderer(renderer.__context3D);

__tileRenderer.__stage = renderer.__stage;
__tileRenderer.__allowSmoothing = c.tilemap.smoothing;
__tileRenderer.__overrideBlendMode = c.tilemap.blendMode;
__tileRenderer.__pixelRatio = renderer.__pixelRatio;
__tileRenderer.__worldTransform = c.tilemap.transform.matrix;
__tileRenderer.__worldAlpha = 1 / c.tilemap.__worldAlpha;
__tileRenderer.__resize(Math.ceil(c.tilemap.width), Math.ceil(c.tilemap.height));

var tilemapTransform = c.tilemap.__renderTransform;
var newTransform = Matrix.__pool.get();
newTransform.copyFrom(tilemapTransform);
newTransform.concat(graphics.__owner.__transform);
c.tilemap.__renderTransform = newTransform;

Context3DTilemap.renderDrawable(c.tilemap, __tileRenderer);
Matrix.__pool.release(newTransform);
c.tilemap.__renderTransform = tilemapTransform;

case DRAW_QUADS:
if (bitmap != null)
{
Expand Down
8 changes: 8 additions & 0 deletions src/openfl/display/_internal/DrawCommandBuffer.hx
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ class DrawCommandBuffer
b.push(smooth);
}

public function beginTilemapFill(tilemap:Tilemap):Void
{
prepareWrite();

types.push(BEGIN_TILEMAP_FILL);
o.push(tilemap);
}

public function beginFill(color:Int, alpha:Float):Void
{
prepareWrite();
Expand Down
25 changes: 25 additions & 0 deletions src/openfl/display/_internal/DrawCommandReader.hx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ class DrawCommandReader
case BEGIN_SHADER_FILL:
oPos += 1; // shaderBuffer

case BEGIN_TILEMAP_FILL:
oPos += 1; // Tilemap

case CUBIC_CURVE_TO:
fPos += 6; // controlX1, controlY1, controlX2, controlY2, anchorX, anchorY

Expand Down Expand Up @@ -191,6 +194,13 @@ class DrawCommandReader
return new BeginShaderFillView(this);
}

public inline function readBeginTilemapFill():BeginTilemapFillView
{
advance();
prev = BEGIN_TILEMAP_FILL;
return new BeginTilemapFillView(this);
}

public inline function readCubicCurveTo():CubicCurveToView
{
advance();
Expand Down Expand Up @@ -466,6 +476,21 @@ abstract BeginShaderFillView(DrawCommandReader)
}
}

abstract BeginTilemapFillView(DrawCommandReader)
{
public inline function new(d:DrawCommandReader)
{
this = d;
}

public var tilemap(get, never):Tilemap;

private inline function get_tilemap():Tilemap
{
return cast this.obj(0);
}
}

abstract CubicCurveToView(DrawCommandReader)
{
public inline function new(d:DrawCommandReader)
Expand Down
1 change: 1 addition & 0 deletions src/openfl/display/_internal/DrawCommandType.hx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ enum DrawCommandType
BEGIN_FILL;
BEGIN_GRADIENT_FILL;
BEGIN_SHADER_FILL;
BEGIN_TILEMAP_FILL;
CUBIC_CURVE_TO;
CURVE_TO;
DRAW_CIRCLE;
Expand Down
1 change: 1 addition & 0 deletions src/openfl/display/_internal/GraphicsDataType.hx
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ package openfl.display._internal;
var QUAD_PATH = 6;
var TRIANGLE_PATH = 7;
var SHADER = 8;
var TILEMAP = 9;
}
1 change: 1 addition & 0 deletions src/openfl/display/_internal/GraphicsFillType.hx
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ package openfl.display._internal;
var BITMAP_FILL = 2;
var END_FILL = 3;
var SHADER_FILL = 4;
var TILEMAP_FILL = 5;
}
Loading