Skip to content

Commit

Permalink
Add System.createTexture and Texture.ctx.
Browse files Browse the repository at this point in the history
Still early and not working yet. Part of #11.
  • Loading branch information
aduros committed Nov 18, 2012
1 parent 5962970 commit b20ba62
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 24 deletions.
10 changes: 10 additions & 0 deletions src/flambe/System.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/flambe/display/Texture.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
28 changes: 13 additions & 15 deletions src/flambe/platform/flash/BitmapDrawingContext.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -219,6 +216,7 @@ class BitmapDrawingContext
flushGraphics();

var texture = Lib.as(texture, BitmapTexture);
texture.flush();
var state = getTopState();
var matrix = state.matrix;

Expand Down Expand Up @@ -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) {
Expand All @@ -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;

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/flambe/platform/flash/BitmapRenderer.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions src/flambe/platform/flash/BitmapTexture.hx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ package flambe.platform.flash;

import flash.display.BitmapData;

import flambe.display.DrawingContext;
import flambe.display.Texture;

class BitmapTexture
implements Texture
{
public var width (getWidth, null) :Int;
public var height (getHeight, null) :Int;
public var ctx (getContext, null) :DrawingContext;

public var bitmapData (default, null) :BitmapData;

Expand All @@ -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;
}
8 changes: 8 additions & 0 deletions src/flambe/platform/flash/Stage3DTexture.hx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import flash.display.BitmapData;
import flash.display3D.Context3D;
import flash.geom.Point;

import flambe.display.DrawingContext;
import flambe.display.Texture;

class Stage3DTexture
implements Texture
{
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;

Expand Down Expand Up @@ -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;
Expand Down
16 changes: 8 additions & 8 deletions src/flambe/platform/html/CanvasDrawingContext.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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));
}
Expand All @@ -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));
}

Expand Down
13 changes: 13 additions & 0 deletions src/flambe/platform/html/CanvasTexture.hx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

package flambe.platform.html;

import flambe.display.DrawingContext;
import flambe.display.Texture;

class CanvasTexture
implements Texture
{
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;
Expand All @@ -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;
}

0 comments on commit b20ba62

Please sign in to comment.