Skip to content

Commit 78d4e25

Browse files
Fix FLX_RENDER_TRIANGLE not rendering anything (#3505)
* fix FLX_RENDER_TRIANGLE * fix small oversight * more accurate vertex and index ordering * remove `#if !flash` conditional * add types * deprecate colors * back to point pooling * improve readibility * use transformX/Y rather than using a FlxPoint * Last change I promise * deprecate colorsPosition * oops * add missing import --------- Co-authored-by: GeoKureli-BlackbookPro <[email protected]>
1 parent 8b0591c commit 78d4e25

File tree

2 files changed

+74
-92
lines changed

2 files changed

+74
-92
lines changed

flixel/FlxCamera.hx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import openfl.geom.Rectangle;
1111
import flixel.graphics.FlxGraphic;
1212
import flixel.graphics.frames.FlxFrame;
1313
import flixel.graphics.tile.FlxDrawBaseItem;
14+
import flixel.graphics.tile.FlxDrawQuadsItem;
1415
import flixel.graphics.tile.FlxDrawTrianglesItem;
1516
import flixel.math.FlxMath;
1617
import flixel.math.FlxMatrix;
@@ -27,8 +28,6 @@ import openfl.filters.BitmapFilter;
2728

2829
using flixel.util.FlxColorTransformUtil;
2930

30-
private typedef FlxDrawItem = flixel.graphics.tile.FlxDrawQuadsItem;
31-
3231
/**
3332
* The camera class is used to display the game's visuals.
3433
* By default one camera is created automatically, that is the same size as window.
@@ -537,7 +536,7 @@ class FlxCamera extends FlxBasic
537536
/**
538537
* Last draw tiles item
539538
*/
540-
var _headTiles:FlxDrawItem;
539+
var _headTiles:FlxDrawQuadsItem;
541540

542541
/**
543542
* Last draw triangles item
@@ -547,7 +546,7 @@ class FlxCamera extends FlxBasic
547546
/**
548547
* Draw tiles stack items that can be reused
549548
*/
550-
static var _storageTilesHead:FlxDrawItem;
549+
static var _storageTilesHead:FlxDrawQuadsItem;
551550

552551
/**
553552
* Draw triangles stack items that can be reused
@@ -601,7 +600,7 @@ class FlxCamera extends FlxBasic
601600
}
602601
else
603602
{
604-
itemToReturn = new FlxDrawItem();
603+
itemToReturn = new FlxDrawQuadsItem();
605604
}
606605

607606
// TODO: catch this error when the dev actually messes up, not in the draw phase
@@ -765,9 +764,9 @@ class FlxCamera extends FlxBasic
765764
var hasColorOffsets:Bool = (transform != null && transform.hasRGBAOffsets());
766765

767766
#if FLX_RENDER_TRIANGLE
768-
var drawItem:FlxDrawTrianglesItem = startTrianglesBatch(frame.parent, smoothing, isColored, blend);
767+
final drawItem:FlxDrawTrianglesItem = startTrianglesBatch(frame.parent, smoothing, isColored, blend, hasColorOffsets, shader);
769768
#else
770-
var drawItem = startQuadBatch(frame.parent, isColored, hasColorOffsets, blend, smoothing, shader);
769+
final drawItem:FlxDrawQuadsItem = startQuadBatch(frame.parent, isColored, hasColorOffsets, blend, smoothing, shader);
771770
#end
772771
drawItem.addQuad(frame, matrix, transform);
773772
}
@@ -808,10 +807,10 @@ class FlxCamera extends FlxBasic
808807
var isColored = (transform != null && transform.hasRGBMultipliers());
809808
var hasColorOffsets:Bool = (transform != null && transform.hasRGBAOffsets());
810809

811-
#if !FLX_RENDER_TRIANGLE
812-
var drawItem = startQuadBatch(frame.parent, isColored, hasColorOffsets, blend, smoothing, shader);
810+
#if FLX_RENDER_TRIANGLE
811+
final drawItem:FlxDrawTrianglesItem = startTrianglesBatch(frame.parent, smoothing, isColored, blend, hasColorOffsets, shader);
813812
#else
814-
var drawItem:FlxDrawTrianglesItem = startTrianglesBatch(frame.parent, smoothing, isColored, blend);
813+
final drawItem:FlxDrawQuadsItem = startQuadBatch(frame.parent, isColored, hasColorOffsets, blend, smoothing, shader);
815814
#end
816815
drawItem.addQuad(frame, _helperMatrix, transform);
817816
}

flixel/graphics/tile/FlxDrawTrianglesItem.hx

Lines changed: 65 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ typedef DrawData<T> = openfl.Vector<T>;
2020
*/
2121
class FlxDrawTrianglesItem extends FlxDrawBaseItem<FlxDrawTrianglesItem>
2222
{
23+
static inline final INDICES_PER_QUAD = 6;
2324
static var point:FlxPoint = FlxPoint.get();
2425
static var rect:FlxRect = FlxRect.get();
2526

@@ -31,10 +32,12 @@ class FlxDrawTrianglesItem extends FlxDrawBaseItem<FlxDrawTrianglesItem>
3132
public var vertices:DrawData<Float> = new DrawData<Float>();
3233
public var indices:DrawData<Int> = new DrawData<Int>();
3334
public var uvtData:DrawData<Float> = new DrawData<Float>();
35+
@:deprecated("colors is deprecated, use colorMultipliers and colorOffsets")
3436
public var colors:DrawData<Int> = new DrawData<Int>();
3537

3638
public var verticesPosition:Int = 0;
3739
public var indicesPosition:Int = 0;
40+
@:deprecated("colorsPosition is deprecated")
3841
public var colorsPosition:Int = 0;
3942

4043
var bounds:FlxRect = FlxRect.get();
@@ -100,11 +103,9 @@ class FlxDrawTrianglesItem extends FlxDrawBaseItem<FlxDrawTrianglesItem>
100103
vertices.length = 0;
101104
indices.length = 0;
102105
uvtData.length = 0;
103-
colors.length = 0;
104106

105107
verticesPosition = 0;
106108
indicesPosition = 0;
107-
colorsPosition = 0;
108109
alphas.splice(0, alphas.length);
109110
if (colorMultipliers != null)
110111
colorMultipliers.splice(0, colorMultipliers.length);
@@ -119,7 +120,6 @@ class FlxDrawTrianglesItem extends FlxDrawBaseItem<FlxDrawTrianglesItem>
119120
vertices = null;
120121
indices = null;
121122
uvtData = null;
122-
colors = null;
123123
bounds = null;
124124
alphas = null;
125125
colorMultipliers = null;
@@ -140,7 +140,6 @@ class FlxDrawTrianglesItem extends FlxDrawBaseItem<FlxDrawTrianglesItem>
140140
var numberOfVertices:Int = Std.int(verticesLength / 2);
141141
var prevIndicesLength:Int = this.indices.length;
142142
var prevUVTDataLength:Int = this.uvtData.length;
143-
var prevColorsLength:Int = this.colors.length;
144143
var prevNumberOfVertices:Int = this.numVertices;
145144

146145
var tempX:Float, tempY:Float;
@@ -184,16 +183,6 @@ class FlxDrawTrianglesItem extends FlxDrawBaseItem<FlxDrawTrianglesItem>
184183
{
185184
this.indices[prevIndicesLength + i] = indices[i] + prevNumberOfVertices;
186185
}
187-
188-
if (colored)
189-
{
190-
for (i in 0...numberOfVertices)
191-
{
192-
this.colors[prevColorsLength + i] = colors[i];
193-
}
194-
195-
colorsPosition += numberOfVertices;
196-
}
197186

198187
final alphaMultiplier = transform != null ? transform.alphaMultiplier : 1.0;
199188
for (_ in 0...indicesLength)
@@ -280,82 +269,76 @@ class FlxDrawTrianglesItem extends FlxDrawBaseItem<FlxDrawTrianglesItem>
280269

281270
override public function addQuad(frame:FlxFrame, matrix:FlxMatrix, ?transform:ColorTransform):Void
282271
{
283-
var prevVerticesPos:Int = verticesPosition;
284-
var prevIndicesPos:Int = indicesPosition;
285-
var prevColorsPos:Int = colorsPosition;
286-
var prevNumberOfVertices:Int = numVertices;
287-
288-
var point = FlxPoint.get();
289-
point.transform(matrix);
290-
291-
vertices[prevVerticesPos] = point.x;
292-
vertices[prevVerticesPos + 1] = point.y;
293-
294-
uvtData[prevVerticesPos] = frame.uv.left;
272+
final prevVerticesPos = verticesPosition;
273+
final prevNumberOfVertices = numVertices;
274+
275+
final w = frame.frame.width;
276+
final h = frame.frame.height;
277+
vertices[prevVerticesPos + 0] = matrix.transformX(0, 0); // left
278+
vertices[prevVerticesPos + 1] = matrix.transformY(0, 0); // top
279+
vertices[prevVerticesPos + 2] = matrix.transformX(w, 0); // right
280+
vertices[prevVerticesPos + 3] = matrix.transformY(w, 0); // top
281+
vertices[prevVerticesPos + 4] = matrix.transformX(0, h); // left
282+
vertices[prevVerticesPos + 5] = matrix.transformY(0, h); // bottom
283+
vertices[prevVerticesPos + 6] = matrix.transformX(w, h); // right
284+
vertices[prevVerticesPos + 7] = matrix.transformY(w, h); // bottom
285+
286+
uvtData[prevVerticesPos + 0] = frame.uv.left;
295287
uvtData[prevVerticesPos + 1] = frame.uv.top;
296-
297-
point.set(frame.frame.width, 0);
298-
point.transform(matrix);
299-
300-
vertices[prevVerticesPos + 2] = point.x;
301-
vertices[prevVerticesPos + 3] = point.y;
302-
303288
uvtData[prevVerticesPos + 2] = frame.uv.right;
304289
uvtData[prevVerticesPos + 3] = frame.uv.top;
305-
306-
point.set(frame.frame.width, frame.frame.height);
307-
point.transform(matrix);
308-
309-
vertices[prevVerticesPos + 4] = point.x;
310-
vertices[prevVerticesPos + 5] = point.y;
311-
312-
uvtData[prevVerticesPos + 4] = frame.uv.right;
290+
uvtData[prevVerticesPos + 4] = frame.uv.left;
313291
uvtData[prevVerticesPos + 5] = frame.uv.bottom;
314-
315-
point.set(0, frame.frame.height);
316-
point.transform(matrix);
317-
318-
vertices[prevVerticesPos + 6] = point.x;
319-
vertices[prevVerticesPos + 7] = point.y;
320-
321-
point.put();
322-
323-
uvtData[prevVerticesPos + 6] = frame.uv.left;
292+
uvtData[prevVerticesPos + 6] = frame.uv.right;
324293
uvtData[prevVerticesPos + 7] = frame.uv.bottom;
325-
326-
indices[prevIndicesPos] = prevNumberOfVertices;
327-
indices[prevIndicesPos + 1] = prevNumberOfVertices + 1;
328-
indices[prevIndicesPos + 2] = prevNumberOfVertices + 2;
329-
indices[prevIndicesPos + 3] = prevNumberOfVertices + 2;
330-
indices[prevIndicesPos + 4] = prevNumberOfVertices + 3;
331-
indices[prevIndicesPos + 5] = prevNumberOfVertices;
332-
333-
if (colored)
294+
295+
final prevIndicesPos = indicesPosition;
296+
indices[prevIndicesPos + 0] = prevNumberOfVertices + 0; // TL
297+
indices[prevIndicesPos + 1] = prevNumberOfVertices + 1; // TR
298+
indices[prevIndicesPos + 2] = prevNumberOfVertices + 2; // BL
299+
indices[prevIndicesPos + 3] = prevNumberOfVertices + 1; // TR
300+
indices[prevIndicesPos + 4] = prevNumberOfVertices + 2; // BL
301+
indices[prevIndicesPos + 5] = prevNumberOfVertices + 3; // BR
302+
303+
final alphaMultiplier = transform != null ? transform.alphaMultiplier : 1.0;
304+
for (i in 0...INDICES_PER_QUAD)
305+
alphas.push(alphaMultiplier);
306+
307+
if (colored || hasColorOffsets)
334308
{
335-
var red = 1.0;
336-
var green = 1.0;
337-
var blue = 1.0;
338-
var alpha = 1.0;
339-
340-
if (transform != null)
309+
if (colorMultipliers == null)
310+
colorMultipliers = [];
311+
312+
if (colorOffsets == null)
313+
colorOffsets = [];
314+
315+
for (i in 0...INDICES_PER_QUAD)
341316
{
342-
red = transform.redMultiplier;
343-
green = transform.greenMultiplier;
344-
blue = transform.blueMultiplier;
345-
346-
#if !neko
347-
alpha = transform.alphaMultiplier;
348-
#end
317+
if (transform != null)
318+
{
319+
colorMultipliers.push(transform.redMultiplier);
320+
colorMultipliers.push(transform.greenMultiplier);
321+
colorMultipliers.push(transform.blueMultiplier);
322+
323+
colorOffsets.push(transform.redOffset);
324+
colorOffsets.push(transform.greenOffset);
325+
colorOffsets.push(transform.blueOffset);
326+
colorOffsets.push(transform.alphaOffset);
327+
}
328+
else
329+
{
330+
colorMultipliers.push(1);
331+
colorMultipliers.push(1);
332+
colorMultipliers.push(1);
333+
334+
colorOffsets.push(0);
335+
colorOffsets.push(0);
336+
colorOffsets.push(0);
337+
colorOffsets.push(0);
338+
}
339+
340+
colorMultipliers.push(1);
349341
}
350-
351-
var color = FlxColor.fromRGBFloat(red, green, blue, alpha);
352-
353-
colors[prevColorsPos] = color;
354-
colors[prevColorsPos + 1] = color;
355-
colors[prevColorsPos + 2] = color;
356-
colors[prevColorsPos + 3] = color;
357-
358-
colorsPosition += 4;
359342
}
360343

361344
verticesPosition += 8;

0 commit comments

Comments
 (0)