Skip to content

Commit

Permalink
Track drawstate to improve tile quadbatching, fixed memory leak in re…
Browse files Browse the repository at this point in the history
…ndertexture
  • Loading branch information
TerryCavanagh committed Jan 22, 2017
1 parent 038e62a commit 84bc33f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 14 deletions.
32 changes: 22 additions & 10 deletions haxegon/Gfx.hx
Expand Up @@ -146,6 +146,7 @@ class Gfx {
/* Internal function for changing tile index to correct values for tileset */
private static function changetileset(tilesetname:String) {
if (currenttilesetname != tilesetname) {
drawstate = DRAWSTATE_NONE;
if(tilesetindex.exists(tilesetname)){
currenttileset = tilesetindex.get(tilesetname);
currenttilesetname = tilesetname;
Expand Down Expand Up @@ -554,12 +555,12 @@ class Gfx {
shapematrix.translate(x, y);
if (coltransform) {
image.color = imagecolormult;
quadbatch.addImage(image, 1.0, shapematrix);
quadbatch.addImage(image, imagealphamult, shapematrix);
image.color = Col.WHITE;
}else {
quadbatch.addImage(image, 1.0, shapematrix);
}
}
}
}

/** Draws image by name.
Expand All @@ -573,6 +574,7 @@ class Gfx {
//This could definitely be improved later. See #118
endquadbatch();
updatequadbatch();
drawstate = DRAWSTATE_IMAGE;

haxegonimage = images[imageindex.get(imagename)];
x = imagealignx(haxegonimage.width, x); y = imagealigny(haxegonimage.height, y);
Expand Down Expand Up @@ -801,19 +803,13 @@ class Gfx {

//WIP: If we're using a tileset then we can batch draw stuff because it's all on the same texture
//(providing we haven't messed with the tileset by creating images)
if (!tiles[currenttileset].sharedatlas) {
endquadbatch();
}
if (drawstate != DRAWSTATE_TILES) endquadbatch();
updatequadbatch();
drawstate = DRAWSTATE_TILES;

x = tilealignx(x); y = tilealigny(y);

internaldrawimage(x, y, tiles[currenttileset].tiles[tilenum], tiles[currenttileset].width, tiles[currenttileset].height);

if (!tiles[currenttileset].sharedatlas) {
//This could definitely be improved later. See #118
endquadbatch();
}
}

private static function tilealignx(x:Float):Float {
Expand Down Expand Up @@ -846,7 +842,9 @@ class Gfx {

public static function drawline(x1:Float, y1:Float, x2:Float, y2:Float, color:Int, alpha:Float = 1.0) {
if (color == Col.TRANSPARENT || drawto == null) return;
if (drawstate != DRAWSTATE_QUAD) endquadbatch();
updatequadbatch();
drawstate = DRAWSTATE_QUAD;

templine = new Line(x1, y1, x2, y2, linethickness, color);
templine.alpha = alpha;
Expand Down Expand Up @@ -916,7 +914,9 @@ class Gfx {

public static function filltri(x1:Float, y1:Float, x2:Float, y2:Float, x3:Float, y3:Float, color:Int, alpha:Float = 1.0) {
if (color == Col.TRANSPARENT || drawto == null) return;
if (drawstate != DRAWSTATE_POLY4) endquadbatch();
updatequadbatch();
drawstate = DRAWSTATE_POLY4;

temppoly4 = new Poly4(x1, y1, x2, y2, x3, y3, x3, y3, color);
temppoly4.alpha = alpha;
Expand Down Expand Up @@ -944,7 +944,9 @@ class Gfx {

public static function fillbox(x:Float, y:Float, width:Float, height:Float, col:Int, alpha:Float = 1.0) {
if (col == Col.TRANSPARENT) return;
if (drawstate != DRAWSTATE_QUAD) endquadbatch();
updatequadbatch();
drawstate = DRAWSTATE_QUAD;

tempquad.x = x;
tempquad.y = y;
Expand All @@ -967,6 +969,7 @@ class Gfx {

quadbatch.reset();
quadbatchcount = 0;
drawstate = DRAWSTATE_NONE;
}
}

Expand Down Expand Up @@ -1185,6 +1188,7 @@ class Gfx {
}

private static function startframe() {
drawstate = DRAWSTATE_NONE;
drawto.bundlelock();

if (clearcolor != Col.TRANSPARENT) clearscreen(clearcolor);
Expand All @@ -1207,6 +1211,14 @@ class Gfx {
private static var temppoly4:Poly4;
private static var templine:Line;

private static var drawstate:Int = 0;
private static inline var DRAWSTATE_NONE:Int = 0;
private static inline var DRAWSTATE_QUAD:Int = 1;
private static inline var DRAWSTATE_POLY4:Int = 2;
private static inline var DRAWSTATE_IMAGE:Int = 3;
private static inline var DRAWSTATE_TILES:Int = 4;
private static inline var DRAWSTATE_TEXT:Int = 5;

private static var starlingassets:AssetManager;
private static var trect:Rectangle = new Rectangle();
private static var shapematrix:Matrix = new Matrix();
Expand Down
17 changes: 15 additions & 2 deletions haxegon/Text.hx
Expand Up @@ -202,6 +202,9 @@ class Text {

private static function drawstringinput() {
Gfx.endquadbatch();
//if (Gfx.drawstate != Gfx.DRAWSTATE_TEXT) Gfx.endquadbatch();
//Gfx.updatequadbatch();
//Gfx.drawstate = Gfx.DRAWSTATE_TEXT;

if (input_show > 0) {
setfont(input_font, input_textsize);
Expand Down Expand Up @@ -336,6 +339,10 @@ class Text {
public static function display(x:Float, y:Float, text:String, color:Int = 0xFFFFFF) {
if (text == "") return;
Gfx.endquadbatch();
//Future developments!
//if (Gfx.drawstate != Gfx.DRAWSTATE_TEXT) Gfx.endquadbatch();
//Gfx.updatequadbatch();
//Gfx.drawstate = Gfx.DRAWSTATE_TEXT;

if (typeface.length == 0) {
defaultfont();
Expand Down Expand Up @@ -367,11 +374,14 @@ class Text {
}

fontmatrix.translate(x, y);
Gfx.drawto.draw(typeface[currentindex].tf, fontmatrix);
// Clumsy work around to force haxegon to change to the next draw call on TTF fonts.
// to do: implement a pooling system for ttf fonts so that this isn't required.
if (typeface[currentindex].type == "ttf") {
Gfx.drawto.draw(typeface[currentindex].tf, fontmatrix);
Gfx.fillbox(-1, -1, 1, 1, Col.RED);
}else {
Gfx.drawto.draw(typeface[currentindex].tf, fontmatrix);
//Gfx.quadbatch.addQuadBatch(typeface[currentindex].tf.mQuadBatch, 1.0, fontmatrix);
}
}

Expand Down Expand Up @@ -437,6 +447,8 @@ class Text {
static function set_font(fontname:String):String {
if (fontname == "" || fontname.toLowerCase() == "verdana") fontname = "Verdana";
if (fontname == currentfont) return currentfont;

//if (Gfx.drawstate != Gfx.DRAWSTATE_TEXT) Gfx.endquadbatch();
setfont(fontname, 1);
return currentfont;
}
Expand All @@ -448,7 +460,8 @@ class Text {
}

static function set_size(fontsize:Float):Float {
if(currentsize != fontsize){
if (currentsize != fontsize) {
//if (Gfx.drawstate != Gfx.DRAWSTATE_TEXT) Gfx.endquadbatch();
changesize(fontsize);
}
return currentsize;
Expand Down
21 changes: 19 additions & 2 deletions starling/core/RenderSupport.hx
Expand Up @@ -196,8 +196,25 @@ class RenderSupport
/** Changes the modelview matrix to the identity matrix. */
public function loadIdentity():Void
{
mModelViewMatrix.identity();
mModelViewMatrix3D.identity();
mModelViewMatrix.identity();

//mModelViewMatrix3D.identity();
mModelViewMatrix3D.rawData[0] = 1.0;
mModelViewMatrix3D.rawData[1] = 0.0;
mModelViewMatrix3D.rawData[2] = 0.0;
mModelViewMatrix3D.rawData[3] = 0.0;
mModelViewMatrix3D.rawData[4] = 0.0;
mModelViewMatrix3D.rawData[5] = 1.0;
mModelViewMatrix3D.rawData[6] = 0.0;
mModelViewMatrix3D.rawData[7] = 0.0;
mModelViewMatrix3D.rawData[8] = 0.0;
mModelViewMatrix3D.rawData[9] = 0.0;
mModelViewMatrix3D.rawData[10] = 1.0;
mModelViewMatrix3D.rawData[11] = 0.0;
mModelViewMatrix3D.rawData[12] = 0.0;
mModelViewMatrix3D.rawData[13] = 0.0;
mModelViewMatrix3D.rawData[14] = 0.0;
mModelViewMatrix3D.rawData[15] = 1.0;
}

/** Prepends a translation to the modelview matrix. */
Expand Down

0 comments on commit 84bc33f

Please sign in to comment.