Skip to content

Commit

Permalink
feat: fps & framebuffer improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
load1n9 committed Feb 18, 2022
1 parent 1432070 commit 04fe5e0
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
9 changes: 4 additions & 5 deletions examples/frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ class Game extends Scene {
public test = new FrameBuffer(this.world, buffer);

public setup() {
this.setKeys(['a']);
this.addChild(this.test);
}
// deno-lint-ignore no-explicit-any
public keyDown(_key: any) {
this.test.rawData = this.test.rawData.fill(Math.floor(Math.random() * 255), 300000, 1200000)
public update() {
this.test.rawData[Math.floor(Math.random()* this.test.rawData.length-1)] = Math.floor(Math.random()*255)
this.test.setBuffer(this.test.rawData);
}
}

Expand All @@ -22,5 +21,5 @@ const test = new World({
height: 600,
resizable: true,
}, [Game]);

test.setFPS(10)
await test.start();
2 changes: 1 addition & 1 deletion src/World.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class World extends Canvas {
}

public _draw(): void {
// this._fps()();
this._fps()();
// this.renderer.updateEvents();
// this.renderer.swapBuffers();
if (this.shouldClose()) return;
Expand Down
6 changes: 5 additions & 1 deletion src/entities/sprites/FrameBuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class FrameBuffer extends Entity {
public width: number;
public height: number
public rawData = new Uint8Array(0);

public requestStart = true;
constructor(
world: World,
buffer: Uint8Array,
Expand All @@ -25,4 +25,8 @@ export class FrameBuffer extends Entity {
res(this);
})
}
public setBuffer(buffer: Uint8Array) {
this.rawData = buffer;
this.requestStart = true;
}
}
4 changes: 2 additions & 2 deletions src/renderers/webgl/2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ export class WebGLRenderer2D {
) {
this.renderImage(entity);
} else if (entity instanceof FrameBuffer) {
// deno-lint-ignore no-explicit-any
if (loadTexture(this.gl, (entity as any))! !== (this.buffers.get(entity.id) as any).texture) {
if (entity.requestStart) {
this.setupFrameBuffer(entity);
}
this.renderImage(entity);
Expand Down Expand Up @@ -146,6 +145,7 @@ export class WebGLRenderer2D {
}

private setupFrameBuffer(entity: FrameBuffer): void {
entity.requestStart = false;
const { x, y, width, height } = { x: 0, y: 0, width: entity.width, height: entity.height };
const data = [
x,
Expand Down

0 comments on commit 04fe5e0

Please sign in to comment.