From ebc56db63c054702ad910987aa666d7cfb52d5cc Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 24 Oct 2018 23:05:13 +0200 Subject: Move shader initialization out of renderer Avoid public fields and ! overrides, make almost all fields readonly. --- src/view/MapView.ts | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) (limited to 'src/view/MapView.ts') 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, + private readonly r: Renderer, + private readonly map: MapData, + private readonly tileTexture: WebGLTexture, + private readonly tileMap: Map, ) { 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) { -- cgit v1.2.3