summaryrefslogtreecommitdiffstats
path: root/src/view/MapView.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/view/MapView.ts')
-rw-r--r--src/view/MapView.ts44
1 files changed, 24 insertions, 20 deletions
diff --git a/src/view/MapView.ts b/src/view/MapView.ts
index 97d5d7d..edcbfbb 100644
--- a/src/view/MapView.ts
+++ b/src/view/MapView.ts
@@ -1,21 +1,21 @@
import {nextPowerOf2} from '../util';
import MapData from '../model/MapData';
-import Renderer from './Renderer';
+import Renderer from './renderer/Renderer';
export default class MapView {
public static readonly tileSize: number = 32;
private redrawPending: boolean = false;
- private vertexBuffer: WebGLBuffer;
- private textureBuffer: WebGLBuffer;
+ private readonly vertexBuffer: WebGLBuffer;
+ private readonly textureBuffer: WebGLBuffer;
constructor(
- private r: Renderer,
- private map: MapData,
- private tileTexture: WebGLTexture,
- private tileMap: Map<string, number>,
+ private readonly r: Renderer,
+ private readonly map: MapData,
+ private readonly tileTexture: WebGLTexture,
+ private readonly tileMap: Map<string, number>,
) {
const vertexData: number[] = [];
const textureData: number[] = [];
@@ -26,29 +26,33 @@ export default class MapView {
for (let y = 0; y < map.height; y++)
this.addTile(vertexData, textureData, x, y, map.layers[0][y][x], tileCount);
+ const gl = r.getContext();
+
this.vertexBuffer = r.createBuffer();
- r.gl.bindBuffer(r.gl.ARRAY_BUFFER, this.vertexBuffer);
- r.gl.bufferData(r.gl.ARRAY_BUFFER, new Float32Array(vertexData), r.gl.STATIC_DRAW);
+ gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertexData), gl.STATIC_DRAW);
this.textureBuffer = r.createBuffer();
- r.gl.bindBuffer(r.gl.ARRAY_BUFFER, this.textureBuffer);
- r.gl.bufferData(r.gl.ARRAY_BUFFER, new Float32Array(textureData), r.gl.STATIC_DRAW);
+ gl.bindBuffer(gl.ARRAY_BUFFER, this.textureBuffer);
+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureData), gl.STATIC_DRAW);
}
public draw(): void {
- this.r.gl.clear(this.r.gl.COLOR_BUFFER_BIT);
+ const gl = this.r.getContext();
+
+ gl.clear(gl.COLOR_BUFFER_BIT);
- this.r.gl.activeTexture(this.r.gl.TEXTURE0);
- this.r.gl.bindTexture(this.r.gl.TEXTURE_2D, this.tileTexture);
- this.r.gl.uniform1i(this.r.samplerLoc, 0);
+ gl.activeTexture(gl.TEXTURE0);
+ gl.bindTexture(gl.TEXTURE_2D, this.tileTexture);
+ gl.uniform1i(this.r.getSamplerLoc(), 0);
- this.r.gl.bindBuffer(this.r.gl.ARRAY_BUFFER, this.vertexBuffer);
- this.r.gl.vertexAttribPointer(this.r.vertexPosLoc, 2, this.r.gl.FLOAT, false, 0, 0);
+ gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
+ gl.vertexAttribPointer(this.r.getVertexPosLoc(), 2, gl.FLOAT, false, 0, 0);
- this.r.gl.bindBuffer(this.r.gl.ARRAY_BUFFER, this.textureBuffer);
- this.r.gl.vertexAttribPointer(this.r.textureCoordLoc, 2, this.r.gl.FLOAT, false, 0, 0);
+ gl.bindBuffer(gl.ARRAY_BUFFER, this.textureBuffer);
+ gl.vertexAttribPointer(this.r.getTextureCoordLoc(), 2, gl.FLOAT, false, 0, 0);
- this.r.gl.drawArrays(this.r.gl.TRIANGLES, 0, 6 * this.map.width * this.map.height);
+ gl.drawArrays(gl.TRIANGLES, 0, 6 * this.map.width * this.map.height);
}
private addTile(vertexData: number[], textureData: number[], x: number, y: number, tile: string, tileCount: number) {