Skip to content

Commit

Permalink
Merge pull request #44 from Starmapo/reduce-update-draw-calls
Browse files Browse the repository at this point in the history
Reduce update & draw calls
  • Loading branch information
ianharrigan authored May 5, 2024
2 parents 03d5440 + c60f717 commit 0f218a5
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 15 deletions.
6 changes: 4 additions & 2 deletions haxe/ui/backend/ComponentGraphicsImpl.hx
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,17 @@ class ComponentGraphicsImpl extends ComponentGraphicsBase {
var byteArray = ByteArray.fromBytes(newPixels);
bitmapData.setPixels(new Rectangle(0, 0, bitmapData.width, bitmapData.height), byteArray);

if (this.sprite == null) {
if (sprite == null) {
sprite = new FlxSprite(0, 0);
sprite.active = false;
_component.add(sprite);
}

sprite.width = w;
sprite.height = h;

this.sprite.pixels = bitmapData;
sprite.pixels = bitmapData;
sprite.visible = (w > 0 && h > 0);
}

public override function resize(width:Null<Float>, height:Null<Float>) {
Expand Down
10 changes: 5 additions & 5 deletions haxe/ui/backend/ComponentImpl.hx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class ComponentImpl extends ComponentBase {
_surface = new FlxSprite();
_surface.makeGraphic(1, 1, 0x0, true);
_surface.pixelPerfectRender = true;
_surface.moves = false;
_surface.active = false;
_surface.visible = false;
add(_surface);

//recursiveReady();
Expand Down Expand Up @@ -228,6 +229,7 @@ class ComponentImpl extends ComponentBase {
if (w <= 0 || h <= 0) {
_surface.graphic.destroy();
_surface.makeGraphic(1, 1, 0x0, true);
_surface.visible = false;
} else {
_surface.graphic.destroy();
_surface.makeGraphic(w, h, 0x0, true);
Expand Down Expand Up @@ -357,9 +359,7 @@ class ComponentImpl extends ComponentBase {
}

for (c in this.childComponents) {
if (!c.hidden) {
c.applyVisibility(show);
}
c.applyVisibility(show && !c.hidden);
}
}

Expand All @@ -380,7 +380,7 @@ class ComponentImpl extends ComponentBase {
}
*/

FlxStyleHelper.applyStyle(_surface, style);
_surface.visible = FlxStyleHelper.applyStyle(_surface, style);
applyFilters(style);
}

Expand Down
1 change: 1 addition & 0 deletions haxe/ui/backend/ImageDisplayImpl.hx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class ImageDisplayImpl extends ImageBase {
public function new() {
super();
this.pixelPerfectRender = true;
this.active = false;
}

private override function validateData():Void {
Expand Down
1 change: 1 addition & 0 deletions haxe/ui/backend/TextDisplayImpl.hx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class TextDisplayImpl extends TextBase {
tf = new FlxText();
tf.pixelPerfectRender = true;
tf.autoSize = true;
tf.active = false;
}

private override function validateData() {
Expand Down
46 changes: 41 additions & 5 deletions haxe/ui/backend/flixel/FlxStyleHelper.hx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import openfl.geom.Point;
import openfl.geom.Rectangle;

class FlxStyleHelper {
public static function applyStyle(sprite:FlxSprite, style:Style) {
public static function applyStyle(sprite:FlxSprite, style:Style):Bool {
if (sprite == null || sprite.pixels == null) {
return;
return false;
}

var pixels:BitmapData = sprite.pixels;
Expand All @@ -28,7 +28,7 @@ class FlxStyleHelper {
var height:Float = sprite.frameHeight;

if (width <= 0 || height <= 0) {
return;
return false;
}

var rc:Rectangle = new Rectangle(top, left, width, height);
Expand All @@ -49,12 +49,14 @@ class FlxStyleHelper {

if (useOpenFLDrawing) {
var g = FlxSpriteUtil.flashGfx;
OpenFLStyleHelper.paintStyleSection(g, style, width, height, left, top);
var painted = OpenFLStyleHelper.paintStyleSection(g, style, width, height, left, top);
FlxSpriteUtil.updateSpriteGraphic(sprite);
return;
return painted;
}
#end

var painted = false;

if (style.borderLeftSize != null && style.borderLeftSize != 0
&& style.borderLeftSize == style.borderRightSize
&& style.borderLeftSize == style.borderBottomSize
Expand All @@ -73,6 +75,8 @@ class FlxStyleHelper {
pixels.fillRect(new Rectangle(rc.left, rc.height - borderSize, rc.width, borderSize), color); // bottom
pixels.fillRect(new Rectangle(rc.left, rc.top + borderSize, borderSize, rc.height - (borderSize * 2)), color); // left
rc.inflate(-borderSize, -borderSize);

painted = true;
} else { // compound border
var org = rc.clone();

Expand All @@ -95,6 +99,10 @@ class FlxStyleHelper {
var color:FlxColor = Std.int(opacity * 0xFF) << 24 | style.borderTopColor;
pixels.fillRect(new Rectangle(rc.left + borderLeftSize, rc.top, org.width - (borderLeftSize + borderRightSize), borderSize), color); // top
rc.top += borderSize;

if (opacity > 0) {
painted = true;
}
}

if (style.borderBottomSize != null && style.borderBottomSize > 0) {
Expand All @@ -103,6 +111,10 @@ class FlxStyleHelper {
var color:FlxColor = Std.int(opacity * 0xFF) << 24 | style.borderBottomColor;
pixels.fillRect(new Rectangle(rc.left, org.height - borderSize, rc.width, borderSize), color); // bottom
rc.bottom -= borderSize;

if (opacity > 0) {
painted = true;
}
}

if (style.borderLeftSize != null && style.borderLeftSize > 0) {
Expand All @@ -111,6 +123,10 @@ class FlxStyleHelper {
var color:FlxColor = Std.int(opacity * 0xFF) << 24 | style.borderLeftColor;
pixels.fillRect(new Rectangle(rc.left, rc.top - borderTopSize, borderSize, org.height - rc.top + borderTopSize), color); // left
rc.left += borderSize;

if (opacity > 0) {
painted = true;
}
}

if (style.borderRightSize != null && style.borderRightSize > 0) {
Expand All @@ -119,6 +135,10 @@ class FlxStyleHelper {
var color:FlxColor = Std.int(opacity * 0xFF) << 24 | style.borderRightColor;
pixels.fillRect(new Rectangle(org.width - borderSize, rc.top - borderTopSize, borderSize, org.height + borderTopSize), color); // right
rc.right -= borderSize;

if (opacity > 0) {
painted = true;
}
}
}

Expand All @@ -141,27 +161,43 @@ class FlxStyleHelper {
pixels.fillRect(rcLine, Std.int(opacity * 0xFF) << 24 | c);
n++;
}

if (opacity > 0 && n > 0) {
painted = true;
}
} else if (gradientType == "horizontal") {
arr = ColorUtil.buildColorArray(style.backgroundColor, style.backgroundColorEnd, Std.int(rc.width));
for (c in arr) {
rcLine.setTo(rc.left + n, rc.top, 1, rc.height);
pixels.fillRect(rcLine, Std.int(opacity * 0xFF) << 24 | c);
n++;
}

if (opacity > 0 && n > 0) {
painted = true;
}
}
} else {
var color:FlxColor = Std.int(opacity * 0xFF) << 24 | style.backgroundColor;
pixels.fillRect(rc, color);

if (opacity > 0) {
painted = true;
}
}
}

if (style.backgroundImage != null) {
Toolkit.assets.getImage(style.backgroundImage, function(info:ImageInfo) {
if (info != null && info.data != null) {
paintBackroundImage(sprite, info.data, style);

painted = true;
}
});
}

return painted;
}

private static function paintBackroundImage(sprite:FlxSprite, data:ImageData, style:Style) {
Expand Down
16 changes: 14 additions & 2 deletions haxe/ui/backend/flixel/OpenFLStyleHelper.hx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ class OpenFLStyleHelper {
public function new() {
}

public static function paintStyleSection(graphics:Graphics, style:Style, width:Float, height:Float, left:Float = 0, top:Float = 0, clear:Bool = true) {
public static function paintStyleSection(graphics:Graphics, style:Style, width:Float, height:Float, left:Float = 0, top:Float = 0, clear:Bool = true):Bool {
if (clear == true) {
graphics.clear();
}

if (width <= 0 || height <= 0) {
return;
return false;
}

/*
Expand All @@ -45,6 +45,8 @@ class OpenFLStyleHelper {
borderRadius = style.borderRadius * Toolkit.scale;
}

var painted = false;

if (style.borderLeftSize != null && style.borderLeftSize != 0
&& style.borderLeftSize == style.borderRightSize
&& style.borderLeftSize == style.borderBottomSize
Expand All @@ -61,6 +63,8 @@ class OpenFLStyleHelper {
rc.bottom -= (style.borderLeftSize * Toolkit.scale) / 2;
rc.right -= (style.borderLeftSize * Toolkit.scale) / 2;
//rc.inflate( -(style.borderLeftSize / 2), -(style.borderLeftSize / 2));

painted = true;
} else { // compound border
if ((style.borderTopSize != null && style.borderTopSize > 0)
|| (style.borderBottomSize != null && style.borderBottomSize > 0)
Expand Down Expand Up @@ -100,6 +104,8 @@ class OpenFLStyleHelper {

rc.right -= (style.borderRightSize * Toolkit.scale);
}

painted = true;
}
}

Expand Down Expand Up @@ -147,6 +153,10 @@ class OpenFLStyleHelper {
} else {
graphics.beginFill(backgroundColor, backgroundOpacity);
}

if (backgroundOpacity > 0) {
painted = true;
}
}

if (borderRadius == 0) {
Expand All @@ -165,5 +175,7 @@ class OpenFLStyleHelper {
}

graphics.endFill();

return painted;
}
}
2 changes: 1 addition & 1 deletion haxe/ui/backend/flixel/textinputs/FlxTextInput.hx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class FlxTextInput extends TextBase {
return tf.visible;
}
private function set_visible(value:Bool):Bool {
tf.visible = value;
tf.active = tf.visible = value; // text input shouldn't be active if it's hidden
return value;
}

Expand Down

0 comments on commit 0f218a5

Please sign in to comment.