Skip to content

Commit

Permalink
fix: Rectangle line renderer now respects z order
Browse files Browse the repository at this point in the history
  • Loading branch information
eonarheim committed Feb 13, 2022
1 parent f22e14c commit 0675d81
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

-
- Fixed issue where Rectangle line renderer did not respect z order

### Updates

-
Expand Down
3 changes: 1 addition & 2 deletions src/engine/Graphics/Context/ExcaliburGraphicsContextWebGL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,7 @@ export class ExcaliburGraphicsContextWebGL implements ExcaliburGraphicsContext {
}

public drawLine(start: Vector, end: Vector, color: Color, thickness = 1) {
const rectangleRenderer = this._renderers.get('ex.rectangle') as RectangleRenderer;
rectangleRenderer.drawLine(start, end, color, thickness);
this.draw<RectangleRenderer>('ex.rectangle', start, end, color, thickness);
}

public drawRectangle(pos: Vector, width: number, height: number, color: Color, stroke?: Color, strokeThickness?: number) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ export class RectangleRenderer implements RendererPlugin {
return false;
}

draw(...args: any[]): void {
if (args[0] instanceof Vector && args[1] instanceof Vector) {
this.drawLine.apply(this, args);
} else {
this.drawRectangle.apply(this, args);
}
}

drawLine(start: Vector, end: Vector, color: Color, thickness: number = 1) {

if (this._isFull()) {
Expand Down Expand Up @@ -184,7 +192,13 @@ export class RectangleRenderer implements RendererPlugin {
vertexBuffer[this._vertexIndex++] = strokeThickness / width;
}

draw(pos: Vector, width: number, height: number, color: Color, stroke: Color = Color.Transparent, strokeThickness: number = 0): void {
drawRectangle(
pos: Vector,
width: number,
height: number,
color: Color,
stroke: Color = Color.Transparent,
strokeThickness: number = 0): void {
if (this._isFull()) {
this.flush();
}
Expand Down
11 changes: 7 additions & 4 deletions src/spec/ExcaliburGraphicsContextSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,27 +389,30 @@ describe('The ExcaliburGraphicsContext', () => {
const rectangleRenderer = sut.get('ex.rectangle');
spyOn(rectangleRenderer, 'flush').and.callThrough();

sut.drawLine(ex.vec(0, 0), ex.vec(100, 100), ex.Color.Red, 2);
expect(rectangleRenderer.flush).withContext('rectangle line render not flusehd yet').not.toHaveBeenCalled();

sut.drawCircle(ex.Vector.Zero, 100, ex.Color.Red, ex.Color.Black, 2);
expect(circleRenderer.flush).withContext('circle is batched not flushed yet').not.toHaveBeenCalled();
expect(rectangleRenderer.flush).withContext('rectangle line render').toHaveBeenCalled();

sut.drawImage(tex.image, 0, 0, tex.width, tex.height, 20, 20);
expect(circleRenderer.flush).withContext('circle renderer switched, flush required').toHaveBeenCalled();
expect(imageRenderer.flush).withContext('image batched not yet flushed').not.toHaveBeenCalled();

sut.drawRectangle(ex.Vector.Zero, 50, 50, ex.Color.Blue, ex.Color.Green, 2);
expect(imageRenderer.flush).toHaveBeenCalled();
expect(rectangleRenderer.flush).withContext('rectangle batched').not.toHaveBeenCalled();

sut.flush();
expect(rectangleRenderer.flush).toHaveBeenCalled();

expect(rectangleRenderer.flush).toHaveBeenCalledTimes(1);
// Rectangle renderer handles lines and rectangles why it's twice
expect(rectangleRenderer.flush).toHaveBeenCalledTimes(2);
expect(circleRenderer.flush).toHaveBeenCalledTimes(1);
expect(imageRenderer.flush).toHaveBeenCalledTimes(1);

await expectAsync(flushWebGLCanvasTo2D(canvasElement)).toEqualImage(
'src/spec/images/ExcaliburGraphicsContextSpec/painter-order-circle-image-rect.png',
.95
.97
);
});

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0675d81

Please sign in to comment.