From b20ba620fb4f9b221664b04675a50d30655790e2 Mon Sep 17 00:00:00 2001 From: Bruno Garcia Date: Sun, 18 Nov 2012 01:11:17 -0800 Subject: [PATCH] Add System.createTexture and Texture.ctx. Still early and not working yet. Part of #11. --- src/flambe/System.hx | 10 +++++++ src/flambe/display/Texture.hx | 5 ++++ .../platform/flash/BitmapDrawingContext.hx | 28 +++++++++---------- src/flambe/platform/flash/BitmapRenderer.hx | 2 +- src/flambe/platform/flash/BitmapTexture.hx | 19 +++++++++++++ src/flambe/platform/flash/Stage3DTexture.hx | 8 ++++++ .../platform/html/CanvasDrawingContext.hx | 16 +++++------ src/flambe/platform/html/CanvasTexture.hx | 13 +++++++++ 8 files changed, 77 insertions(+), 24 deletions(-) diff --git a/src/flambe/System.hx b/src/flambe/System.hx index 69ccbc81..a585d765 100644 --- a/src/flambe/System.hx +++ b/src/flambe/System.hx @@ -7,6 +7,7 @@ package flambe; import flambe.asset.AssetPack; import flambe.asset.Manifest; import flambe.display.Stage; +import flambe.display.Texture; import flambe.input.Keyboard; import flambe.input.Mouse; import flambe.input.Pointer; @@ -122,6 +123,15 @@ class System return _platform.callNative(funcName, params); } + /** + * Creates a new blank Texture, initialized to transparent black. + */ + public static function createTexture (width :Int, height :Int) :Texture + { + #if debug assertCalledInit(); #end + return _platform.getRenderer().createEmptyTexture(width, height); + } + /** * Creates a Logger for printing debug messages. In Flash, this uses the native trace() * function. In JS, logging goes to the console object. Logging is stripped from non-debug diff --git a/src/flambe/display/Texture.hx b/src/flambe/display/Texture.hx index b5eac160..e4abfdc7 100644 --- a/src/flambe/display/Texture.hx +++ b/src/flambe/display/Texture.hx @@ -18,4 +18,9 @@ interface Texture * The height of this texture, in pixels. */ var height (getHeight, null) :Int; + + /** + * The DrawingContext that draws to this texture. + */ + var ctx (getContext, null) :DrawingContext; } diff --git a/src/flambe/platform/flash/BitmapDrawingContext.hx b/src/flambe/platform/flash/BitmapDrawingContext.hx index 9c284971..2b222df1 100644 --- a/src/flambe/platform/flash/BitmapDrawingContext.hx +++ b/src/flambe/platform/flash/BitmapDrawingContext.hx @@ -30,10 +30,6 @@ class BitmapDrawingContext _buffer = buffer; _stateList = new DrawingState(); _shape = new Shape(); - _pixel = new BitmapData(1, 1, false); - _scratchRect = new Rectangle(); - _scratchPoint = new Point(); - _scratchMatrix = new Matrix(); } public function save () @@ -135,6 +131,7 @@ class BitmapDrawingContext beginGraphics(); var texture = Lib.as(texture, BitmapTexture); + texture.flush(); _graphics.beginBitmapFill(texture.bitmapData); _graphics.drawRect(x, y, width, height); } @@ -191,8 +188,8 @@ class BitmapDrawingContext _scratchMatrix.ty = y; _scratchMatrix.concat(matrix); - _pixel.setPixel(0, 0, color); - _buffer.draw(_pixel, _scratchMatrix, state.color, state.blendMode); + _scratchPixel.setPixel(0, 0, color); + _buffer.draw(_scratchPixel, _scratchMatrix, state.color, state.blendMode); } } @@ -219,6 +216,7 @@ class BitmapDrawingContext flushGraphics(); var texture = Lib.as(texture, BitmapTexture); + texture.flush(); var state = getTopState(); var matrix = state.matrix; @@ -274,7 +272,7 @@ class BitmapDrawingContext return _stateList; } - private function flushGraphics () + public function flushGraphics () { // If we're in vector graphics mode, push it out to the screen buffer if (_graphics != null) { @@ -292,6 +290,14 @@ class BitmapDrawingContext } } + // Reusable instances to avoid tons of allocation + private static var _scratchPoint = new Point(); + private static var _scratchRect = new Rectangle(); + private static var _scratchMatrix = new Matrix(); + + // A 1x1 BitmapData used to optimize fillRect's worst-case + private static var _scratchPixel = new BitmapData(1, 1, false); + private var _stateList :DrawingState; private var _buffer :BitmapData; @@ -300,14 +306,6 @@ class BitmapDrawingContext // The vector graphic commands pending drawing, or null if we're not in vector graphics mode private var _graphics :Graphics; - - // A 1x1 BitmapData used to optimize fillRect's worst-case - private var _pixel :BitmapData; - - // Reusable instances to avoid tons of allocation - private var _scratchPoint :Point; - private var _scratchRect :Rectangle; - private var _scratchMatrix :Matrix; } private class DrawingState diff --git a/src/flambe/platform/flash/BitmapRenderer.hx b/src/flambe/platform/flash/BitmapRenderer.hx index 90aa8345..890e1ca6 100644 --- a/src/flambe/platform/flash/BitmapRenderer.hx +++ b/src/flambe/platform/flash/BitmapRenderer.hx @@ -33,7 +33,7 @@ class BitmapRenderer public function createEmptyTexture (width :Int, height :Int) :Texture { - throw "Not yet implemented"; + return new BitmapTexture(new BitmapData(width, height, true, 0x00000000)); } public function willRender () :DrawingContext diff --git a/src/flambe/platform/flash/BitmapTexture.hx b/src/flambe/platform/flash/BitmapTexture.hx index 0c9aa2aa..131c7bee 100644 --- a/src/flambe/platform/flash/BitmapTexture.hx +++ b/src/flambe/platform/flash/BitmapTexture.hx @@ -6,6 +6,7 @@ package flambe.platform.flash; import flash.display.BitmapData; +import flambe.display.DrawingContext; import flambe.display.Texture; class BitmapTexture @@ -13,6 +14,7 @@ class BitmapTexture { public var width (getWidth, null) :Int; public var height (getHeight, null) :Int; + public var ctx (getContext, null) :DrawingContext; public var bitmapData (default, null) :BitmapData; @@ -30,4 +32,21 @@ class BitmapTexture { return bitmapData.height; } + + public function flush () + { + if (_ctx != null) { + _ctx.flushGraphics(); + } + } + + private function getContext () :BitmapDrawingContext + { + if (_ctx == null) { + _ctx = new BitmapDrawingContext(bitmapData); + } + return _ctx; + } + + private var _ctx :BitmapDrawingContext = null; } diff --git a/src/flambe/platform/flash/Stage3DTexture.hx b/src/flambe/platform/flash/Stage3DTexture.hx index 208ff005..0ca500cb 100644 --- a/src/flambe/platform/flash/Stage3DTexture.hx +++ b/src/flambe/platform/flash/Stage3DTexture.hx @@ -8,6 +8,7 @@ import flash.display.BitmapData; import flash.display3D.Context3D; import flash.geom.Point; +import flambe.display.DrawingContext; import flambe.display.Texture; class Stage3DTexture @@ -15,6 +16,7 @@ class Stage3DTexture { public var width (getWidth, null) :Int; public var height (getHeight, null) :Int; + public var ctx (getContext, null) :DrawingContext; public var nativeTexture (default, null) :flash.display3D.textures.Texture; @@ -58,6 +60,12 @@ class Stage3DTexture return _height; } + private function getContext () :Stage3DDrawingContext + { + throw "Not yet implemented"; + return null; + } + private static function nextPowerOfTwo (n :Int) { var p = 1; diff --git a/src/flambe/platform/html/CanvasDrawingContext.hx b/src/flambe/platform/html/CanvasDrawingContext.hx index 1dc9f4fc..89beed42 100644 --- a/src/flambe/platform/html/CanvasDrawingContext.hx +++ b/src/flambe/platform/html/CanvasDrawingContext.hx @@ -62,8 +62,8 @@ class CanvasDrawingContext return; } - var htmlTexture :CanvasTexture = cast texture; - _canvasCtx.drawImage(htmlTexture.image, Std.int(x), Std.int(y)); + var texture :CanvasTexture = cast texture; + _canvasCtx.drawImage(texture.image, Std.int(x), Std.int(y)); } public function drawSubImage (texture :Texture, destX :Float, destY :Float, @@ -77,8 +77,8 @@ class CanvasDrawingContext return; } - var htmlTexture :CanvasTexture = cast texture; - _canvasCtx.drawImage(htmlTexture.image, + var texture :CanvasTexture = cast texture; + _canvasCtx.drawImage(texture.image, Std.int(sourceX), Std.int(sourceY), Std.int(sourceW), Std.int(sourceH), Std.int(destX), Std.int(destY), Std.int(sourceW), Std.int(sourceH)); } @@ -93,11 +93,11 @@ class CanvasDrawingContext return; } - var htmlTexture :CanvasTexture = cast texture; - if (htmlTexture.pattern == null) { - htmlTexture.pattern = _canvasCtx.createPattern(htmlTexture.image, "repeat"); + var texture :CanvasTexture = cast texture; + if (texture.pattern == null) { + texture.pattern = _canvasCtx.createPattern(texture.image, "repeat"); } - _canvasCtx.fillStyle = htmlTexture.pattern; + _canvasCtx.fillStyle = texture.pattern; _canvasCtx.fillRect(Std.int(x), Std.int(y), Std.int(width), Std.int(height)); } diff --git a/src/flambe/platform/html/CanvasTexture.hx b/src/flambe/platform/html/CanvasTexture.hx index 07b7acc7..3ee6463b 100644 --- a/src/flambe/platform/html/CanvasTexture.hx +++ b/src/flambe/platform/html/CanvasTexture.hx @@ -4,6 +4,7 @@ package flambe.platform.html; +import flambe.display.DrawingContext; import flambe.display.Texture; class CanvasTexture @@ -11,6 +12,7 @@ class CanvasTexture { public var width (getWidth, null) :Int; public var height (getHeight, null) :Int; + public var ctx (getContext, null) :DrawingContext; // The Image (or sometimes Canvas) used for most draw calls public var image :Dynamic; @@ -31,4 +33,15 @@ class CanvasTexture { return image.height; } + + private function getContext () :CanvasDrawingContext + { + if (_ctx == null) { + // FIXME(bruno): Ensure image is a canvas element + _ctx = new CanvasDrawingContext(image); + } + return _ctx; + } + + private var _ctx :CanvasDrawingContext = null; }